RC Car mods...



  • Ron and I were trying to figure out what to do with cheap RC toys that don’t have remotes in the hack room after Maker Monday last night… We came up with the idea of replacing the control board entirely with a dual H-bridge and a Wemos mini… Keep the chassis, motors, suspension, steering, battery compartment, and just replace all the brains. Then you have a wifi connected, programmable robotics platform.

    Anyway, we banged out a quick prototype (@Ron_Ron can you take pics?) in about half an hour.

    I just hacked up some completely untested (It compiles, isn’t that good enough for MicroSoft?) code to run a webserver on the Wemos to control it.

    
    #include <ESP8266WiFi.h>
    #include <WiFiClient.h>
    #include <ESP8266WebServer.h>
    #include <ESP8266mDNS.h>
    
    int 
      m1=4,
      m2=3,
      m3=2,
      m4=1;
    
    const char* ssid = "Makerspace";
    const char* password = "4Makerl0ops#";
    
    WiFiServer server(80);
    MDNSResponder mdns;
    
    void carStop() {
      digitalWrite(m1, LOW);
      digitalWrite(m2, LOW);
    }
    void carStraight() {
      digitalWrite(m3, LOW);
      digitalWrite(m4, LOW);
    }
    void carForward(int speed) {
      digitalWrite(m1, LOW);
      analogWrite(m2, speed);
    }
    void carReverse(int speed) {
      digitalWrite(m2, LOW);
      analogWrite(m1, speed);
    }
    void carLeft(int amount) {
      digitalWrite(m3, LOW);
      analogWrite(m4, amount);
    }
    void carRight(int amount) {
      digitalWrite(m4, LOW);
      analogWrite(m3, amount);
    }
    
    void setup() {
      pinMode(m1, OUTPUT);
      pinMode(m2, OUTPUT);
      pinMode(m3, OUTPUT);
      pinMode(m4, OUTPUT);
      carStop();
      carStraight();
    
      WiFi.begin(ssid, password);
     
      while (WiFi.status() != WL_CONNECTED) {
        delay(500);
      }
      
    }
    
    void loop() {
      // put your main code here, to run repeatedly:
      WiFiClient client = server.available();
      if (!client) {
        return;
      }
     
      // Wait until the client sends some data
      while(!client.available()){
        delay(1);
      }
     
      // Read the first line of the request
      String request = client.readStringUntil('\r');
      client.flush();
     
      // Match the request
      if (request.indexOf("/straight") != -1) {
        carStraight();
      }
      if (request.indexOf("/left") != -1) {
        carLeft(255);
      } 
      if (request.indexOf("/right") != -1){
        carRight(255);
      }
     
     
     
      // Send the GUI
      client.println("HTTP/1.1 200 OK");
      client.println("Content-Type: text/html");
      client.println(""); //  do not forget this one
      client.println("<!DOCTYPE HTML>");
      client.println("<html>");
    
      client.println("<br><br>");
      client.println("Click <a href=\"/left\">here</a> turn left<br>");
      client.println("Click <a href=\"/right\">here</a> turn right<br>");
      client.println("</html>");
      client.flush();
      
      delay(1); 
    }
    

    @Ron_Ron I know you are planning on hacking a bit today, so see if you can do anything with this.



  • Have peek at this. I can’t wait for us to do more work on it! :)
    0_1516737416463_P1220530.JPG



  • I updated the code a lot last week, should probably post it here:

    // Wifi Toy Car stuff ... Needs this library:  http://arduiniana.org/libraries/streaming/
    #include <ESP8266WiFi.h>
    #include <WiFiClient.h>
    #include <ESP8266WebServer.h>
    #include <Streaming.h>
    
    
    int 
      m1=4,
      m2=3,
      m3=2,
      m4=1;
    
    const char* ssid = "Makerspace";
    const char* password = "4Makerl0ops#";
    
    ESP8266WebServer server(80);
    
    void carStop() {
      Serial << "Stopping\n";
      digitalWrite(m1, LOW);
      digitalWrite(m2, LOW);
    }
    void carStraight() {
      Serial << "Going Straight\n";
      digitalWrite(m3, LOW);
      digitalWrite(m4, LOW);
    }
    void carForward(int speed) {
      Serial << "Forward: " << speed << "\n";
      digitalWrite(m1, LOW);
      analogWrite(m2, speed);
    }
    void carReverse(int speed) {
      Serial << "Reverse: " << speed << "\n";
      digitalWrite(m2, LOW);
      analogWrite(m1, speed);
    }
    void carLeft(int amount) {
      Serial << "Left " << amount << "\n" ;
      digitalWrite(m3, LOW);
      analogWrite(m4, amount);
    }
    void carRight(int amount) {
      Serial << "Right " << amount << "\n";
      digitalWrite(m4, LOW);
      analogWrite(m3, amount);
    }
    
    void setup() {
      pinMode(m1, OUTPUT);
      pinMode(m2, OUTPUT);
      pinMode(m3, OUTPUT);
      pinMode(m4, OUTPUT);
      carStop();
      carStraight();
    
      WiFi.begin(ssid, password);
     
      Serial.begin(9600);
      Serial << "------\n";
      while (WiFi.status() != WL_CONNECTED) {
        Serial << ".";
        delay(500);
      }
      Serial << "\n";
      
      Serial << "Connected. You can control the car at: http://" << WiFi.localIP() << "\n";
      Serial << "API looks like this:\n";
      Serial << "http://" << WiFi.localIP() << "/stop" << "\n";
      Serial << "http://" << WiFi.localIP() << "/straight" << "\n";
      Serial << "http://" << WiFi.localIP() << "/throttle?amount=number" << "\n";
      Serial << "http://" << WiFi.localIP() << "/turn?amount=number" << "\n";
      
      server.on("/", sendGui);
      server.on("/index.html", sendGui);
      server.on("/straight", handleStraight);
      server.on("/turn", handleTurn);
      server.on("/throttle", handleThrottle);
      server.on("/stop", handleStop);
      server.begin();
    }
    
    void sendGui() {
      server.send(200, "text/html", "<!DOCTYPE HTML><html>  <head>    <style>      .wheel {   width: 70%;   height=25px; }       .throttle {  width: 25%;   height=50px;  -webkit-appearance: slider-vertical; }    </style>  </head>  <body>    <br><br>    <input type=\"range\" min=\"-300\" max=\"300\" value=\"0\" class=\"wheel\" id=\"steer\"/>    <input type=\"range\" min=\"-300\" max=\"300\" value=\"0\" class=\"throttle\" id=\"throttle\"/><script language=\"javascript\">document.getElementById(\"throttle\").oninput=function() {(new XMLHttpRequest).open(\"GET\", \"/throttle?amount=\"+this.value, true);}  </script>  </body></html>  ");
    }
    
    void handleStraight() {
      carStraight();
      server.send(200, "text/plain", "0");
    }
    void handleStop() {
      carStop();
      server.send(200, "text/plain", "0");
    }
    void handleTurn() {
      if (server.arg("amount")== "") {
        carStraight();
        server.send(200, "text/plain", "0");
      } else {
        int amount=server.arg("amount").toInt();
        if (amount < -55) {
          carLeft(amount + 55);
          server.send(200, "text/plain", (String) (amount+55));
        } else if (amount > 55) {
          carRight(amount - 55);
          server.send(200, "text/plain", (String) (amount-55));
        } else {
          carStraight();
          server.send(200, "text/plain", "0");
        }
      }
    }
    void handleThrottle() {
      if (server.arg("amount")== ""){
        carStop();
        server.send(200, "text/plain", "0");
      } else {
        int amount=server.arg("amount").toInt();
        if (amount < -55) {
          carReverse(amount + 55);
          server.send(200, "text/plain", (String) (amount + 55));
        } else if (amount > 55) {
          carForward(amount - 55);
          server.send(200, "text/plain", (String) (amount - 55));
        } else {
          carStop();
          server.send(200, "text/plain", "0");
        }
      }
    }
    
    void loop() {
       server.handleClient();    //Handling of incoming requests
       
      delay(1); 
    }
    

    So if you know the IP of the car (I’ll add a dhcp reservation at the router some time…) you should be able to build a remote that does HTTP get requests like http://ipaddress/throttle?amount=128 or http://ipaddress/turn?amount=-50 and it’ll drive around. Totally unsecured for now. You can also write arbitrary autonomous arduino code and just upload it to the car via the microusb. If you grab http://ipaddress/ it’ll send you a little partially finished gui and you should be able to eventually poke XMLHttpRequests at the needed API endpoints from the web gui.



  • const char* ssid = “Makerspace”;
    const char* password = “4Makerl0ops#”;

    Is that the right password?



  • @DarrenAndSherry Matches the config in my laptop.


Log in to reply