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
main.cpp
- Committer:
- mfurseman
- Date:
- 2014-10-31
- Revision:
- 3:ba11f6207550
- Parent:
- 2:50c151183047
- Child:
- 4:1b5c2a2cdeb7
File content as of revision 3:ba11f6207550:
#include "mbed.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 #define debug(x, ...) serial.printf(x, ##__VA_ARGS__); #else #define debug(x, ...) #endif /* Client commands */ #define CMD_NULL 0 #define CMD_C_ECHO 97 #define CMD_C_LED_ON 98 #define CMD_C_LED_OFF 99 using namespace mbed_cc3000; /* On board LED */ DigitalOut led(P0_1); /* Serial library for WiFi module */ cc3000 wifi(p28, p27, p30, SPI(p21, p14, p37)); /* Struct to hold connection data */ tNetappIpconfigRetArgs ipinfo; /* Prints CC3000 connection info */ void printConnectionInfo() { if (( wifi.is_enabled() ) && ( wifi.is_dhcp_configured() )) { wifi.get_ip_config(&ipinfo); } if (! wifi.is_enabled() ) { debug("CC3000 Disabled\r\n"); } else if ( wifi.is_dhcp_configured() ) { debug("SSID : %-33s|\r\n", ipinfo.uaSSID); debug("IP : %-35s|\r\n", wifi.getIPAddress()); } else if ( wifi.is_connected() ) { debug("Connecting, waiting for DHCP\r\n"); } else { debug("Not Connected\r\n"); } } /* WiFi DipCortex board setup */ void init() { NVIC_SetPriority(SSP1_IRQn, 0x0); NVIC_SetPriority(PIN_INT0_IRQn, 0x1); // SysTick set to lower priority than Wi-Fi SPI bus interrupt NVIC_SetPriority(SysTick_IRQn, 0x2); // Enable RAM1 LPC_SYSCON->SYSAHBCLKCTRL |= (0x1 << 26); // This may be neccassary for CC3000 wait(1); } /* Connects WiFi assuming existing SmartConfig */ void connectWifi() { wifi.start(0); wait_ms(750); wifi._wlan.ioctl_set_connection_policy(0, 0, 1); // TODO: Timeout and switch on smart config here // TODO: Use static IP if possible } /* Where it all begins */ int main(void) { init(); debug("Completed init()\r\n"); printConnectionInfo(); connectWifi(); debug("Completed connectWifi()\r\n"); printConnectionInfo(); while(1) { debug("\r\nOne second client attachment loop\r\n"); printConnectionInfo(); debug("Creating server and client sockets\r\n"); wait_ms(15); TCPSocketConnection client; // is_connected not reliable when using non-blocking sockets TCPSocketServer server; int32_t status; server.bind(5678); server.listen(); status = server.accept(client); wait_ms(15); int n_timeout = 1; debug("Accept client returned with status %d\r\n", status); if(status >= 0) { client.set_blocking(false, 1000); // 5 ms time out is min for CC3000 debug("Connection from: %s \r\n", client.get_address()); wait_ms(15); while(1) { debug("\r\nClient connected loop - 0 ms\r\n"); wait_ms(15); char command = 0; client.set_blocking(false, 5); // 5 ms time out is min for CC3000 status = client.receive(&command, 1); if(status == 1) { debug("Recieved data from client: %d with status %d\r\n", command, status); switch(command) { case CMD_C_ECHO: wait_ms(15); char buffer[3]; client.set_blocking(false, 2000); status = client.receive_all(buffer, sizeof(buffer)); debug("Echo test recieved: %s Status: %d\r\n", buffer, status); wait_ms(15); status = client.send_all(buffer, sizeof(buffer)); debug("Echo test send completed with status: %d\r\n"); break; case CMD_C_LED_ON: led = 1; break; case CMD_C_LED_OFF: led = 0; 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 status = client.send("abc\r\n", 5); debug("Single byte send returned with status %d\r\n", status); if(status < 0) { break; } } } debug("Client connection lost\r\n"); wait_ms(15); } debug("Should now return to the top of while(1) after changing LED\r\n"); wait_ms(15); debug("After wait_ms(15)\r\n"); led = !led; debug("After led = !led\r\n"); wait(1.); debug("After wait(1.)\r\n"); } }