Final version
Dependencies: mbed Servo Motor
Revision 3:25e57f22f970, committed 2018-12-11
- Comitter:
- kzar
- Date:
- Tue Dec 11 21:22:30 2018 +0000
- Parent:
- 2:15a043aa08ea
- Commit message:
- ye
Changed in this revision
Servo.lib | Show annotated file Show diff for this revision Revisions of this file |
main.cpp | Show annotated file Show diff for this revision Revisions of this file |
diff -r 15a043aa08ea -r 25e57f22f970 Servo.lib --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/Servo.lib Tue Dec 11 21:22:30 2018 +0000 @@ -0,0 +1,1 @@ +http://mbed.org/users/simon/code/Servo/#36b69a7ced07
diff -r 15a043aa08ea -r 25e57f22f970 main.cpp --- a/main.cpp Sun Dec 09 18:27:35 2018 +0000 +++ b/main.cpp Tue Dec 11 21:22:30 2018 +0000 @@ -3,18 +3,21 @@ #include "mbed.h" #include "Motor.h" +#include "Servo.h" +// Construt serial objects Serial pc(USBTX, USBRX); Serial esp(p28, p27); // tx, rx - +// Construct motor objects Motor left_motor(p25, p6, p5); // pwm, fwd, rev Motor right_motor(p26, p11, p10); //pwm, fwd, rev +// Construct servo objects +Servo servo_x(p22); +Servo servo_y(p23); // Standard Mbed LED definitions DigitalOut led1(LED1); DigitalOut led2(LED2); -DigitalOut led3(LED3); -DigitalOut led4(LED4); // things for sending/receiving data over serial volatile int tx_in=0; @@ -39,30 +42,36 @@ // buffers used for taking float values from string commands char lm_buff[8]; char rm_buff[8]; -char lsv_buff[8]; -char rsv_buff[8]; +char xsv_buff[8]; +char ysv_buff[8]; char* lm; char* rm; -char* lsv; -char* rsv; +char* xsv; int main() { + // Change huzzah baud rate, wait for x amout of time, then set pc/esp.baud pc.baud(9600); esp.baud(9600); - led1=1,led2=0,led3=0, led4=0; + led1=0,led2=0; + left_motor.speed(0); + right_motor.speed(0); + servo_x = 0; + servo_y = 0; // Setup a serial interrupt function to receive data esp.attach(&Rx_interrupt, Serial::RxIrq); // Setup a serial interrupt function to transmit data esp.attach(&Tx_interrupt, Serial::TxIrq); // Start up the server startserver(); + // Indicates the server is set up + led1 = 1; // Everything is interrupt driven, infinite loop while(1) { // Read data if its available if(DataRX==1) { - led1=!led1; + led2=!led2; getcmd(); esp.attach(&Rx_interrupt, Serial::RxIrq); } @@ -72,7 +81,7 @@ // Reads and processes GET and POST web data void getcmd() { - wait_ms(200); + //wait_ms(100); esp.attach(NULL,Serial::RxIrq); DataRX=0; memset(webdata, '\0', sizeof(webdata)); @@ -85,33 +94,39 @@ pc.printf(webdata); // Modify this to check for our commands // check web data for form information - lm = strstr(webdata, "left_motor="); - rm = strstr(webdata, "right_motor="); + lm = strstr(webdata, "l="); + rm = strstr(webdata, "r="); + xsv = strstr(webdata, "x="); + if(lm != NULL ) { - lm = lm + 11; + lm = lm + 2; for (int i = 0; i < 8; ++i){ - if (lm != rm) - lm_buff[i] = lm[i]; - else - break; + lm_buff[i] = lm[i]; } // Convert the lm_buff to a float value and clear buffer left_motor.speed(atof(lm_buff)); memset(lm_buff, '\0', sizeof(lm_buff)); } if( rm != NULL) { - rm = rm + 12; - for (int i = 0; i < 8; ++i){ - if (rm[i] != '>') - rm_buff[i] = rm[i]; - else - break; + rm = rm + 2; + for (int i = 0; i < 8; ++i) { + rm_buff[i] = rm[i]; } // Convert the rm_buff to a float value and clear buffer right_motor.speed(atof(rm_buff)); memset(rm_buff, '\0', sizeof(rm_buff)); } + if(xsv != NULL ) { + xsv = xsv + 2; + for (int i = 0; i < 8; ++i){ + xsv_buff[i] = xsv[i]; + } + // Convert the lm_buff to a float value and clear buffer + servo_x = atof(xsv_buff); + memset(xsv_buff, '\0', sizeof(xsv_buff)); + } + } // Starts webserver void startserver() @@ -120,27 +135,22 @@ pc.printf("++++++++++ Resetting ESP ++++++++++\r\n"); strcpy(cmdbuff,"node.restart()\r\n"); SendCMD(); - getreply(); - wait(2); - + wait(1); // Set up WiFi connection pc.printf("++++++++++ Connecting to WiFi ++++++++++\r\n"); strcpy(cmdbuff,"wifi.setmode(wifi.STATIONAP)\r\n"); SendCMD(); - getreply(); - wait(2); + wait(1); - strcpy(cmdbuff,"wifi.ap.config({ssid=\"WestSide\",pwd=\"uNwieldy_cAbbage87\"})\r\n"); + strcpy(cmdbuff,"wifi.ap.config({ssid=\"Marlon's iPhone\",pwd=\"feelsbadman\"})\r\n"); SendCMD(); - getreply(); - wait(2); + wait(1); strcpy(cmdbuff,"print(\"Server IP Address: \", wifi.ap.getip())\r\n"); SendCMD(); - getreply(); - wait(2); + wait(1); // Start up the server pc.printf("\n++++++++++ Starting Server ++++++++++\r\n> "); @@ -148,53 +158,44 @@ //create server strcpy(cmdbuff, "srv=net.createServer(net.TCP)\r\n"); SendCMD(); - getreply(); - wait(0.5); + wait(1); // Set the sever to listen (named conn) strcpy(cmdbuff,"srv:listen(80,function(conn)\r\n"); SendCMD(); - getreply(); - wait(2); + wait(1); // Define receive function strcpy(cmdbuff,"conn:on(\"receive\", function(conn, receivedData)\r\n"); SendCMD(); - getreply(); - wait(2); + wait(1); //print data to mbed strcpy(cmdbuff,"print(\"Received Data: \" .. receivedData)\r\n"); SendCMD(); - getreply(); - wait(2); + wait(1); // End receive function strcpy(cmdbuff,"end)\r\n"); SendCMD(); - getreply(); - wait(2); + wait(1); // Define sent function strcpy(cmdbuff,"conn:on(\"sent\",function(conn)\r\n"); SendCMD(); - getreply(); - wait(2); + wait(1); //print data to mbed strcpy(cmdbuff,"collectgarbage()\r\n"); SendCMD(); - getreply(); - wait(2); + wait(1); // End sent function strcpy(cmdbuff,"end)\r\n"); SendCMD(); - getreply(); - wait(2); + wait(1); // End server listening strcpy(cmdbuff,"end)\r\n"); SendCMD(); - getreply(); wait(2); }