A WiFiDipCortex based robot. Control is via sockets over WiFi. See also: https://github.com/mfurseman/robo-android

Dependencies:   Motordriver USBDevice cc3000_hostdriver_mbedsocket_hacked mbed

Revision:
7:ee8b630b0a33
Parent:
5:7f5fcee1737d
diff -r 5ae9ebf738c6 -r ee8b630b0a33 main.cpp
--- a/main.cpp	Mon Nov 17 19:29:07 2014 +0000
+++ b/main.cpp	Wed Nov 19 22:08:14 2014 +0000
@@ -20,6 +20,7 @@
 
 /* Network constants */
 #define SERVER_PORT 5678
+#define CC3000_WAIT 15
 
 /* Client commands */
 #define CMD_NULL 0
@@ -32,7 +33,7 @@
 #define CMD_C_MOTOR_LEFT_COAST 0x67 // 'g'
 #define CMD_C_MOTOR_RIGHT_COAST 0x68 // 'h'
 #define CMD_C_MOTOR_LEFT_STOP 0x69 // 'i'
-#define CMD_C_MOTOR_RIGHT_STOP 0x70 // 'j'
+#define CMD_C_MOTOR_RIGHT_STOP 0x6a // 'j'
 
 
 using namespace mbed_cc3000;
@@ -111,8 +112,10 @@
  * called.
  */
 bool isConnected(TCPSocketConnection *connection) {
-    connection->set_blocking(false, 1000);
-    return connection->send("abc\r\n", 5) < 0;
+    wait_ms(CC3000_WAIT);
+    debug("Checking conneciton is connected\r\n");
+    connection->set_blocking(false, 2000);
+    return connection->send("xxx\r\n", 5) > 0;
 }
 
 
@@ -144,7 +147,7 @@
     int status = connection->receive_all((char*)&char_buffer, sizeof(char_buffer)); // 1 Byte
     debug("Command left motor duty received: %d with status %d\r\n", char_buffer, status);
     debug("Converstion from uint8_t to float: %f\r\n", ((char_buffer*1.0f) / ((uint8_t)-1)));
-    return (char_buffer*1.0f) / ((uint8_t)-1);
+    return ((char_buffer*1.0f) / ((uint8_t)-1));
 }
 
 
@@ -156,7 +159,7 @@
  void monitorConnection(TCPSocketConnection* connection) {
     int timeout_counter = 1;
     while(1) {      
-        wait_ms(15);
+        wait_ms(CC3000_WAIT);
         char command = 0;
         connection->set_blocking(false, 5); //  5 ms time out is min for CC3000
         int status = connection->receive(&command, 1);
@@ -164,13 +167,13 @@
             debug("Recieved data from connection: %d with status %d\r\n", command, status);
             switch(command) {
                 case CMD_C_ECHO:
-                    wait_ms(15);
+                    wait_ms(CC3000_WAIT);
                     char buffer[3];
                     connection->set_blocking(false, 2000);
                     status = connection->receive_all(buffer, sizeof(buffer));
                     debug("Echo test recieved: %s Status: %d\r\n", buffer, status);
                     
-                    wait_ms(15);
+                    wait_ms(CC3000_WAIT);
                     status = connection->send_all(buffer, sizeof(buffer));
                     debug("Echo test send completed with status: %d\r\n");
                     break;
@@ -184,7 +187,7 @@
                     break;
                     
                 case CMD_C_PRINT_UINT32:
-                    wait_ms(15);
+                    wait_ms(CC3000_WAIT);
                     {   // Variable declerations inside switch must be in higher scope.
                         // This is only okay as it's debugging code.
                         float printFloat = getUint32AsFloat(connection);
@@ -193,12 +196,12 @@
                     break;
 
                 case CMD_C_MOTOR_LEFT_DUTY:
-                    wait_ms(15);
+                    wait_ms(CC3000_WAIT);
                     leftMotor.speed(2.0f * getUint8AsFloat(connection) - 1.0f);  // Convert to +/- 1
                     break;
                     
                 case CMD_C_MOTOR_RIGHT_DUTY:
-                    wait_ms(15);
+                    wait_ms(CC3000_WAIT);
                     rightMotor.speed(2.0f * getUint8AsFloat(connection) - 1.0f);  // Convert to +/- 1
                     break;
                     
@@ -211,12 +214,12 @@
                     break;
                     
                 case CMD_C_MOTOR_LEFT_STOP:
-                    wait_ms(15);
+                    wait_ms(CC3000_WAIT);
                     leftMotor.stop(getUint8AsFloat(connection));
                     break;
                     
                 case CMD_C_MOTOR_RIGHT_STOP:
-                    wait_ms(15);
+                    wait_ms(CC3000_WAIT);
                     rightMotor.stop(getUint8AsFloat(connection));
                     break;
                                           
@@ -226,11 +229,12 @@
             }
         }
 
-        wait_ms(15);
         /* Check to see if the non-blocking socket is closed */
         if((timeout_counter++) % 100 == 0) {
             if(!isConnected(connection)) {
                 debug("Client disconected\r\n");
+                leftMotor.coast();
+                rightMotor.coast();
                 break;
             }
         }
@@ -258,13 +262,13 @@
         TCPSocketConnection client;
         TCPSocketServer server;
 
-        wait_ms(15);
+        wait_ms(CC3000_WAIT);
         server.bind(SERVER_PORT);
         server.listen();
         int32_t status = server.accept(client);
         debug("Accept client returned with status %d\r\n", status);
         if(status >= 0) {
-            wait_ms(15);
+            wait_ms(CC3000_WAIT);
             client.set_blocking(false, 1000);
             debug("Connection from: %s \r\n", client.get_address());