Control a robot over the internet using UDP and a Wifly module (WiFi).
Dependencies: Motor TextLCD WiflyInterface mbed-rtos mbed
Diff: main.cpp
- Revision:
- 0:c0dc3a76f3d4
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/main.cpp Thu Oct 17 13:27:56 2013 +0000 @@ -0,0 +1,161 @@ +#include "mbed.h" +#include "math.h" +#include "TextLCD.h" +#include "rtos.h" +#include "time.h" +#include "Motor.h" +#include "WiflyInterface.h" + +Mutex lock; +WiflyInterface wireless(p9, p10, p19, p20, "mbed", "123456", WPA); +Serial pc(USBTX, USBRX); +Motor m1(p21, p7, p8); +Motor m2(p22, p5, p6); // pwm, fwd, rev +DigitalOut led1(LED1); +DigitalOut led2(LED2); +DigitalOut led4(LED4); +DigitalOut led3(LED3); +TextLCD lcd(p30, p29, p28, p27, p26, p25); // rs, e, d4-d7 + +float left_motor = 0.0; +float right_motor = 0.0; +void ether_f(void const *args); +void motor_f(); +void time_disp(void const *args); +int main() +{ + Thread t1(time_disp); + Thread t2(ether_f); + while(1) { + motor_f(); + Thread::wait(250); + } +} +void time_disp(void const *args) +{ + while(true) { + time_t temp = time(NULL); + lock.lock(); + lcd.cls(); + lcd.printf("%s\n", ctime(&temp)); + lock.unlock(); + Thread::wait(4000); + + lock.lock(); + lcd.cls(); + lcd.printf("%s\n", wireless.getIPAddress()); + lock.unlock(); + Thread::wait(4000); + } +} + +void motor_f() +{ + lock.lock(); + lcd.cls(); + lcd.printf("Testing Motor\n"); + lock.unlock(); + while (1) { + m2.speed(left_motor); + m1.speed(right_motor); + } +} + +void ether_f(void const *args) +{ + pc.printf("Testing Internet Connection!\n"); + led1 = 1; + wireless.init(); + while (!wireless.connect()); // join the network + led2 = 1; + pc.printf("IP Address is %s\n\r", wireless.getIPAddress()); + /* Code to Update RTC on mbed */ + /* + UDPSocket sock; + sock.init(); + Endpoint nist; + nist.set_address("time-a.nist.gov", 37); + char out_buffer[] = "plop"; // Does not matter + sock.sendTo(nist, out_buffer, sizeof(out_buffer)); + + char in_buffer[4]; + int n = sock.receiveFrom(nist, in_buffer, sizeof(in_buffer)); + + unsigned int timeRes = ntohl( *((unsigned int*)in_buffer)); + set_time(timeRes - 2208988800U - 4*3600); + pc.printf("Received %d bytes from server %s on port %d: %u seconds since 1/01/1900 00:00 GMT\n", n, nist.get_address(), nist.get_port(), timeRes); + time_t temp = time(NULL); + lcd.cls(); + lcd.printf("%s\n", ctime(&temp)); + sock.close(); + */ + UDPSocket server; + server.bind(7); + + Endpoint client; + char buffer[256]; + while (true) { + int n = server.receiveFrom(client, buffer, sizeof(buffer)); + lock.lock(); + lcd.cls(); + lcd.printf("Received from:\n %s", client.get_address()); + lock.unlock(); + buffer[5] = NULL; + pc.printf("%s\n", buffer); + if (buffer[0] == '1') led1 = 1; + else led1 = 0; + if (buffer[1] == '1') led2 = 1; + else led2 = 0; + if (buffer[2] == '1') led3 = 1; + else led3 = 0; + if (buffer[3] == '1') led4 = 1; + else led4 = 0; + + if (buffer[4] == '+') { + left_motor += 0.1; + if (left_motor > 1) left_motor = 1; + } else if (buffer[4] == '-') { + left_motor -= 0.1; + if (left_motor < -1) left_motor = -1; + } else if (buffer[4] == 'B') { + left_motor = 0; + } else if (buffer[4] == 'F') { + if (left_motor < 0) left_motor = -1; + else left_motor = 1; + } + + + if (buffer[4] == '+') { + right_motor += 0.1; + if (right_motor > 1) right_motor = 1; + } else if (buffer[4] == '-') { + right_motor -= 0.1; + if (right_motor < -1) right_motor = -1; + } else if (buffer[4] == 'B') { + right_motor = 0; + } else if (buffer[4] == 'F') { + if (right_motor < 0) right_motor = -1; + else right_motor = 1; + } + + if (buffer[4] == 'L') { + right_motor += 0.05; + left_motor -= 0.05; + if (right_motor > 1) right_motor = 1; + if (left_motor < -1) left_motor = -1; + } + + if (buffer[4] == 'R') { + right_motor -= 0.05; + left_motor += 0.05; + if (left_motor > 1) left_motor = 1; + if (right_motor < -1) right_motor = -1; + } + if (buffer[4] == 'S') { + float temp = (right_motor + left_motor)/2; + right_motor = temp; + left_motor = temp; + } + } + wireless.disconnect(); +}