Control a robot over the internet using UDP and a Ethernet interface.

Dependencies:   EthernetInterface Motor TextLCD mbed-rtos mbed Socket lwip-eth lwip-sys lwip

Committer:
apatel336
Date:
Thu Oct 17 13:26:38 2013 +0000
Revision:
0:1496281373a5
Initial Release

Who changed what in which revision?

UserRevisionLine numberNew contents of line
apatel336 0:1496281373a5 1 #include "mbed.h"
apatel336 0:1496281373a5 2 #include "math.h"
apatel336 0:1496281373a5 3 #include "TextLCD.h"
apatel336 0:1496281373a5 4 #include "rtos.h"
apatel336 0:1496281373a5 5 #include "time.h"
apatel336 0:1496281373a5 6 #include "Motor.h"
apatel336 0:1496281373a5 7 #include "EthernetInterface.h"
apatel336 0:1496281373a5 8 Mutex lock;
apatel336 0:1496281373a5 9
apatel336 0:1496281373a5 10 EthernetInterface eth;
apatel336 0:1496281373a5 11 Serial pc(USBTX, USBRX);
apatel336 0:1496281373a5 12 Motor m1(p21, p7, p8);
apatel336 0:1496281373a5 13 Motor m2(p22, p5, p6); // pwm, fwd, rev
apatel336 0:1496281373a5 14 DigitalOut led1(LED1);
apatel336 0:1496281373a5 15 DigitalOut led2(LED2);
apatel336 0:1496281373a5 16 DigitalOut led4(LED4);
apatel336 0:1496281373a5 17 DigitalOut led3(LED3);
apatel336 0:1496281373a5 18 TextLCD lcd(p30, p29, p28, p27, p26, p25); // rs, e, d4-d7
apatel336 0:1496281373a5 19
apatel336 0:1496281373a5 20
apatel336 0:1496281373a5 21 float left_motor = 0.0;
apatel336 0:1496281373a5 22 float right_motor = 0.0;
apatel336 0:1496281373a5 23 void ether_f(void const *args);
apatel336 0:1496281373a5 24 void motor_f();
apatel336 0:1496281373a5 25 void time_disp(void const *args);
apatel336 0:1496281373a5 26 int main()
apatel336 0:1496281373a5 27 {
apatel336 0:1496281373a5 28 Thread t1(time_disp);
apatel336 0:1496281373a5 29 Thread t2(ether_f);
apatel336 0:1496281373a5 30 while(1) {
apatel336 0:1496281373a5 31 motor_f();
apatel336 0:1496281373a5 32 Thread::wait(250);
apatel336 0:1496281373a5 33 }
apatel336 0:1496281373a5 34 }
apatel336 0:1496281373a5 35
apatel336 0:1496281373a5 36 void time_disp(void const *args)
apatel336 0:1496281373a5 37 {
apatel336 0:1496281373a5 38 while(true) {
apatel336 0:1496281373a5 39 time_t temp = time(NULL);
apatel336 0:1496281373a5 40 lock.lock();
apatel336 0:1496281373a5 41 lcd.cls();
apatel336 0:1496281373a5 42 lcd.printf("%s\n", ctime(&temp));
apatel336 0:1496281373a5 43 lock.unlock();
apatel336 0:1496281373a5 44 Thread::wait(4000);
apatel336 0:1496281373a5 45
apatel336 0:1496281373a5 46 lock.lock();
apatel336 0:1496281373a5 47 lcd.cls();
apatel336 0:1496281373a5 48 lcd.printf("%s\n", eth.getIPAddress());
apatel336 0:1496281373a5 49 lock.unlock();
apatel336 0:1496281373a5 50 Thread::wait(4000);
apatel336 0:1496281373a5 51 }
apatel336 0:1496281373a5 52 }
apatel336 0:1496281373a5 53
apatel336 0:1496281373a5 54 void motor_f()
apatel336 0:1496281373a5 55 {
apatel336 0:1496281373a5 56 lock.lock();
apatel336 0:1496281373a5 57 lcd.cls();
apatel336 0:1496281373a5 58 lcd.printf("Testing Motor\n");
apatel336 0:1496281373a5 59 lock.unlock();
apatel336 0:1496281373a5 60 while (1) {
apatel336 0:1496281373a5 61 m2.speed(left_motor);
apatel336 0:1496281373a5 62 m1.speed(right_motor);
apatel336 0:1496281373a5 63 }
apatel336 0:1496281373a5 64 }
apatel336 0:1496281373a5 65
apatel336 0:1496281373a5 66 void ether_f(void const *args)
apatel336 0:1496281373a5 67 {
apatel336 0:1496281373a5 68 pc.printf("Testing Internet Connection!\n");
apatel336 0:1496281373a5 69 led1 = 1;
apatel336 0:1496281373a5 70 eth.init(); //Use DHCP
apatel336 0:1496281373a5 71 Thread::wait(1000);
apatel336 0:1496281373a5 72 pc.printf("MAC: %s\n", eth.getMACAddress());
apatel336 0:1496281373a5 73 led2 = 1;
apatel336 0:1496281373a5 74 eth.connect(30000);
apatel336 0:1496281373a5 75 led1 = !led1;
apatel336 0:1496281373a5 76 led2 = !led2;
apatel336 0:1496281373a5 77 pc.printf("IP: %s\n", eth.getIPAddress());
apatel336 0:1496281373a5 78 /* Code to Update RTC on mbed */
apatel336 0:1496281373a5 79 /*
apatel336 0:1496281373a5 80 UDPSocket sock;
apatel336 0:1496281373a5 81 sock.init();
apatel336 0:1496281373a5 82
apatel336 0:1496281373a5 83 Endpoint nist;
apatel336 0:1496281373a5 84 nist.set_address("time-a.nist.gov", 37);
apatel336 0:1496281373a5 85
apatel336 0:1496281373a5 86 char out_buffer[] = "plop"; // Does not matter
apatel336 0:1496281373a5 87 sock.sendTo(nist, out_buffer, sizeof(out_buffer));
apatel336 0:1496281373a5 88
apatel336 0:1496281373a5 89 char in_buffer[4];
apatel336 0:1496281373a5 90 int n = sock.receiveFrom(nist, in_buffer, sizeof(in_buffer));
apatel336 0:1496281373a5 91
apatel336 0:1496281373a5 92 unsigned int timeRes = ntohl( *((unsigned int*)in_buffer));
apatel336 0:1496281373a5 93 set_time(timeRes - 2208988800U - 4*3600);
apatel336 0:1496281373a5 94 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);
apatel336 0:1496281373a5 95 time_t temp = time(NULL);
apatel336 0:1496281373a5 96 lcd.cls();
apatel336 0:1496281373a5 97 lcd.printf("%s\n", ctime(&temp));
apatel336 0:1496281373a5 98 sock.close();
apatel336 0:1496281373a5 99 */
apatel336 0:1496281373a5 100 UDPSocket server;
apatel336 0:1496281373a5 101 server.bind(7);
apatel336 0:1496281373a5 102
apatel336 0:1496281373a5 103 Endpoint client;
apatel336 0:1496281373a5 104 char buffer[256];
apatel336 0:1496281373a5 105 while (true) {
apatel336 0:1496281373a5 106 int n = server.receiveFrom(client, buffer, sizeof(buffer));
apatel336 0:1496281373a5 107 lock.lock();
apatel336 0:1496281373a5 108 lcd.cls();
apatel336 0:1496281373a5 109 lcd.printf("Received from:\n %s", client.get_address());
apatel336 0:1496281373a5 110 lock.unlock();
apatel336 0:1496281373a5 111 buffer[5] = NULL;
apatel336 0:1496281373a5 112 pc.printf("%s\n", buffer);
apatel336 0:1496281373a5 113 if (buffer[0] == '1') led1 = 1;
apatel336 0:1496281373a5 114 else led1 = 0;
apatel336 0:1496281373a5 115 if (buffer[1] == '1') led2 = 1;
apatel336 0:1496281373a5 116 else led2 = 0;
apatel336 0:1496281373a5 117 if (buffer[2] == '1') led3 = 1;
apatel336 0:1496281373a5 118 else led3 = 0;
apatel336 0:1496281373a5 119 if (buffer[3] == '1') led4 = 1;
apatel336 0:1496281373a5 120 else led4 = 0;
apatel336 0:1496281373a5 121
apatel336 0:1496281373a5 122 if (buffer[4] == '+') {
apatel336 0:1496281373a5 123 left_motor += 0.1;
apatel336 0:1496281373a5 124 if (left_motor > 1) left_motor = 1;
apatel336 0:1496281373a5 125 } else if (buffer[4] == '-') {
apatel336 0:1496281373a5 126 left_motor -= 0.1;
apatel336 0:1496281373a5 127 if (left_motor < -1) left_motor = -1;
apatel336 0:1496281373a5 128 } else if (buffer[4] == 'B') {
apatel336 0:1496281373a5 129 left_motor = 0;
apatel336 0:1496281373a5 130 } else if (buffer[4] == 'F') {
apatel336 0:1496281373a5 131 if (left_motor < 0) left_motor = -1;
apatel336 0:1496281373a5 132 else left_motor = 1;
apatel336 0:1496281373a5 133 }
apatel336 0:1496281373a5 134
apatel336 0:1496281373a5 135
apatel336 0:1496281373a5 136 if (buffer[4] == '+') {
apatel336 0:1496281373a5 137 right_motor += 0.1;
apatel336 0:1496281373a5 138 if (right_motor > 1) right_motor = 1;
apatel336 0:1496281373a5 139 } else if (buffer[4] == '-') {
apatel336 0:1496281373a5 140 right_motor -= 0.1;
apatel336 0:1496281373a5 141 if (right_motor < -1) right_motor = -1;
apatel336 0:1496281373a5 142 } else if (buffer[4] == 'B') {
apatel336 0:1496281373a5 143 right_motor = 0;
apatel336 0:1496281373a5 144 } else if (buffer[4] == 'F') {
apatel336 0:1496281373a5 145 if (right_motor < 0) right_motor = -1;
apatel336 0:1496281373a5 146 else right_motor = 1;
apatel336 0:1496281373a5 147 }
apatel336 0:1496281373a5 148
apatel336 0:1496281373a5 149 if (buffer[4] == 'L') {
apatel336 0:1496281373a5 150 right_motor += 0.05;
apatel336 0:1496281373a5 151 left_motor -= 0.05;
apatel336 0:1496281373a5 152 if (right_motor > 1) right_motor = 1;
apatel336 0:1496281373a5 153 if (left_motor < -1) left_motor = -1;
apatel336 0:1496281373a5 154 }
apatel336 0:1496281373a5 155
apatel336 0:1496281373a5 156 if (buffer[4] == 'R') {
apatel336 0:1496281373a5 157 right_motor -= 0.05;
apatel336 0:1496281373a5 158 left_motor += 0.05;
apatel336 0:1496281373a5 159 if (left_motor > 1) left_motor = 1;
apatel336 0:1496281373a5 160 if (right_motor < -1) right_motor = -1;
apatel336 0:1496281373a5 161 }
apatel336 0:1496281373a5 162 if (buffer[4] == 'S') {
apatel336 0:1496281373a5 163 float temp = (right_motor + left_motor)/2;
apatel336 0:1496281373a5 164 right_motor = temp;
apatel336 0:1496281373a5 165 left_motor = temp;
apatel336 0:1496281373a5 166 }
apatel336 0:1496281373a5 167 }
apatel336 0:1496281373a5 168 eth.disconnect();
apatel336 0:1496281373a5 169
apatel336 0:1496281373a5 170 }