Chris Styles / Mbed 2 deprecated Snackotron

Dependencies:   EthernetNetIf TextLCD mbed AX12

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 #include "mbed.h"
00002 #include "EthernetNetIf.h"
00003 #include "HTTPClient.h"
00004 #include "AX12.h"
00005 #include "TextLCD.h"
00006 
00007 TextLCD lcd(p15, p16, p17, p18, p19, p20); // rs, e, d4-d7
00008 
00009 DigitalOut led1(LED1);
00010 DigitalOut led2(LED2);
00011 DigitalOut led3(LED3);
00012 DigitalOut led4(LED4);
00013 
00014 AX12 sm1 (p9, p10, 1);
00015 AX12 sm2 (p9, p10, 2);
00016 
00017 EthernetNetIf eth;
00018 HTTPClient http;
00019 
00020 void DeliverSnack1 () {
00021     
00022     float pos = 0.0;
00023     float goal = 0.0;
00024 
00025     led1 = 1;
00026 
00027     // read the position of the servo
00028     pos = sm1.GetPosition();
00029 
00030     // work out where we need to position the servo
00031     // we always align to 60^ boundary
00032     
00033     sm1.SetCRSpeed(0.7);
00034     if ((pos > 0.0) && (pos < 60.0)) {
00035         goal = 60.0;
00036     } else if ((pos > 60.0) && (pos < 120.0)) {
00037         goal = 120.0;
00038     } else if ((pos > 120.0) && (pos < 180.0)) {
00039         goal = 180.0;
00040     } else if ((pos > 180.0) && (pos < 240.0)) {
00041         goal = 240.0;
00042     } else if ((pos > 240.0) && (pos < 295.0)) {
00043         goal = 295.0;
00044     } else {
00045         goal = 2.0;
00046     }
00047 
00048     do {
00049         pos = sm1.GetPosition();
00050         // there is a quirk in the 300-360 dead band of the AX12
00051         // where it reads ~120^, briefly
00052         if ((goal == 2.0) && (pos > 50.0)) {
00053             pos = 0.0;
00054         }
00055         printf("%.2f\n",pos);
00056     } while (pos < goal);
00057 
00058 
00059     sm1.SetCRSpeed(0.0);
00060 
00061     wait (2.0);
00062     led1 = 0;
00063 }
00064 
00065 void DeliverSnack2 () {
00066 
00067     float pos = 0.0;
00068     float goal = 0.0;
00069 
00070     led2 = 1;
00071 
00072     // read the position of the servo
00073     pos = sm2.GetPosition();
00074 
00075     // work out where we need to position the servo
00076     // we always align to 60^ boundary
00077     sm2.SetCRSpeed(0.7);
00078     if ((pos > 0.0) && (pos < 60.0)) {
00079         goal = 60.0;
00080     } else if ((pos > 60.0) && (pos < 120.0)) {
00081         goal = 120.0;
00082     } else if ((pos > 120.0) && (pos < 180.0)) {
00083         goal = 180.0;
00084     } else if ((pos > 180.0) && (pos < 240.0)) {
00085         goal = 240.0;
00086     } else if ((pos > 240.0) && (pos < 295.0)) {
00087         goal = 295.0;
00088     } else {
00089         goal = 2.0;
00090     }
00091 
00092     do {
00093         pos = sm2.GetPosition();
00094         // there is a quirk in the 300-360 dead band of the AX12
00095         // where it reads ~120^, briefly
00096         if ((goal == 2.0) && (pos > 50.0)) {
00097             pos = 0.0;
00098         }
00099         printf("%.2f\n",pos);
00100     } while (pos < goal);
00101 
00102     sm2.SetCRSpeed(0.0);
00103     
00104     wait (2.0);
00105     led2 = 0;
00106 }
00107 
00108 
00109 int main() {
00110 
00111     lcd.cls();
00112     lcd.locate(0,0);
00113     lcd.printf("Snack-o-tron");
00114     lcd.locate(0,1);
00115 
00116     // set Servo to continusous rotation
00117     lcd.printf(" Init Motors");
00118     //sm1.SetMode(1);
00119     wait(0.5);
00120 
00121     lcd.locate(0,1);
00122     lcd.printf(" Done       ");
00123     wait(0.5);
00124 
00125     lcd.locate(0,1);
00126     lcd.printf(" Init Network");
00127 
00128     printf("Setting up...\n");
00129     EthernetErr ethErr = eth.setup();
00130     if (ethErr) {
00131         printf("Error %d in setup.\n", ethErr);
00132         lcd.locate(0,1);
00133         lcd.printf("Network Error %d",ethErr);
00134         return -1;
00135     }
00136 
00137     // let the world know all is well!
00138     printf("Setup OK\n");
00139     lcd.locate(0,1);
00140     lcd.printf(" Done           ");
00141     wait (2.0);
00142     lcd.cls();
00143 
00144     HTTPText txt;
00145 
00146     while (1) {
00147 
00148         lcd.cls();
00149         lcd.locate(0,0);
00150         lcd.printf("Ready");
00151 
00152         led4 = 1; // this LED flashed with every HTTP request, for sanity
00153         HTTPResult r = http.get("http://example.com/snackcheck", &txt);
00154 
00155         if (r==HTTP_OK) {
00156             
00157             char sender[20];
00158             char snack[20];
00159             
00160             sscanf(txt.gets(),"%s\n%s\n",sender,snack);
00161 
00162             if ( (strcmp ("0",sender)) == 0) {
00163                 printf("No snacks today\n");
00164             } else {
00165 
00166                 printf("Sender : %s, Snack : %s\n", sender, snack);
00167                 lcd.cls();
00168                 lcd.locate(0,0);
00169                 lcd.printf("%s",sender);
00170                 lcd.locate(0,1);
00171                 lcd.printf("Snack %s",snack);
00172 
00173                 // What to do if snack 1 is ordered
00174                 if ( (strcmp ("1",snack)) == 0) {
00175                     DeliverSnack1();
00176                 }
00177                 // what to do if snack 2 is ordered
00178                 else if ( (strcmp ("2",snack)) == 0) {
00179                     DeliverSnack2();
00180                 }
00181             }
00182 
00183         } else {
00184             // HTTP error
00185             printf("Error %d\n", r);
00186         }
00187 
00188         // clear LEDs
00189         led4 = 0;
00190         wait (1.0);
00191     }
00192 }