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
Diff: main.cpp
- Revision:
- 4:1b5c2a2cdeb7
- Parent:
- 3:ba11f6207550
- Child:
- 5:7f5fcee1737d
diff -r ba11f6207550 -r 1b5c2a2cdeb7 main.cpp --- a/main.cpp Fri Oct 31 23:27:35 2014 +0000 +++ b/main.cpp Wed Nov 12 22:45:59 2014 +0000 @@ -1,15 +1,18 @@ -#include "mbed.h" +#include <stdint.h> +#include <mbed.h> +#include "motordriver.h" #include "cc3000.h" #include "TCPSocketConnection.h" #include "TCPSocketServer.h" + /* MAC 08:00:28:57:43:b8 */ /* Quickly change debug flag to remove USB serial code */ //#define DEBUG #ifdef DEBUG #include "USBSerial.h" -USBSerial serial; // This must be instantiated in main +USBSerial serial; #define debug(x, ...) serial.printf(x, ##__VA_ARGS__); #else #define debug(x, ...) @@ -17,9 +20,10 @@ /* Client commands */ #define CMD_NULL 0 -#define CMD_C_ECHO 97 +#define CMD_C_ECHO 0x61 // 'a' #define CMD_C_LED_ON 98 #define CMD_C_LED_OFF 99 +#define CMD_C_PRINT_UINT32 100 using namespace mbed_cc3000; @@ -28,6 +32,9 @@ /* On board LED */ DigitalOut led(P0_1); +/* External LED - Pin 1 */ +PwmOut pwm_led(P0_8); // Pin 15 + /* Serial library for WiFi module */ cc3000 wifi(p28, p27, p30, SPI(p21, p14, p37)); @@ -92,6 +99,9 @@ connectWifi(); debug("Completed connectWifi()\r\n"); printConnectionInfo(); + + debug("Setting led pwm to 0\r\n"); + pwm_led.period_ms(1); // 1 kHz while(1) { debug("\r\nOne second client attachment loop\r\n"); @@ -99,7 +109,7 @@ debug("Creating server and client sockets\r\n"); wait_ms(15); - TCPSocketConnection client; // is_connected not reliable when using non-blocking sockets + TCPSocketConnection client; TCPSocketServer server; int32_t status; @@ -144,16 +154,28 @@ led = 0; break; + case CMD_C_PRINT_UINT32: + wait_ms(15); + uint32_t int_buffer; + client.set_blocking(false, 2000); + status = client.receive_all((char*)&int_buffer, sizeof(int_buffer)); // 4 Bytes + debug("Command print int32 recieved: %u with status: %d\r\n", int_buffer, status); + int_buffer = ntohl(int_buffer); + debug("Converted to host byte order: %u\r\n", int_buffer); + debug("Conversion of int to float: %f\r\n", ((int_buffer*1.0f) / ((uint32_t)-1))); + pwm_led = ((int_buffer*1.0f) / ((uint32_t)-1)); + break; + default: debug("Command %d not recognised\r\n", command); break; } } - + wait_ms(15); /* Check to see if the non-blocking socket is closed */ if((n_timeout++) % 100 == 0) { - client.set_blocking(false, 1000); // 5 ms time out is min for CC3000 + client.set_blocking(false, 1000); status = client.send("abc\r\n", 5); debug("Single byte send returned with status %d\r\n", status); if(status < 0) {