The IoTBot is a WiFi-enabled rover built from the Shadow Robot kit. It is controlled from a web interface running on the Adafruit Huzzah ESP8266 WiFi module and implements a pair of Hall-effect sensors on the motors to keep the wheels spinning at the same speed. Additionally, it uses a Sharp IR sensor to detect walls in front of the robot and prevent it from crashing.

Dependencies:   mbed-dev mbed-rtos

Fork of huzzah_helloWorld by ECE 4180 Team Who

Committer:
tanmaybangalore
Date:
Mon Oct 31 07:33:45 2016 +0000
Revision:
1:046dd94c57ce
Parent:
0:57cec3469a80
Child:
2:2d87957b577b
Initial commit;

Who changed what in which revision?

UserRevisionLine numberNew contents of line
jhunter029 0:57cec3469a80 1 #include "mbed.h"
tanmaybangalore 1:046dd94c57ce 2 #include "rtos.h"
jhunter029 0:57cec3469a80 3
tanmaybangalore 1:046dd94c57ce 4 RawSerial pc(USBTX, USBRX);
tanmaybangalore 1:046dd94c57ce 5 RawSerial dev(p28,p27);
jhunter029 0:57cec3469a80 6 DigitalOut led1(LED1);
jhunter029 0:57cec3469a80 7 DigitalOut led4(LED4);
tanmaybangalore 1:046dd94c57ce 8
tanmaybangalore 1:046dd94c57ce 9 // Set up the motor pins
tanmaybangalore 1:046dd94c57ce 10 PwmOut motorACtrl(p21);
tanmaybangalore 1:046dd94c57ce 11 PwmOut motorBCtrl(p22);
tanmaybangalore 1:046dd94c57ce 12 DigitalOut backA(p20);
tanmaybangalore 1:046dd94c57ce 13 DigitalOut fwdA(p19);
tanmaybangalore 1:046dd94c57ce 14 DigitalOut stby(p18);
tanmaybangalore 1:046dd94c57ce 15 DigitalOut fwdB(p17);
tanmaybangalore 1:046dd94c57ce 16 DigitalOut backB(p16);
jhunter029 0:57cec3469a80 17 Timer t;
tanmaybangalore 1:046dd94c57ce 18 float speed = 0.9f;
jhunter029 0:57cec3469a80 19
tanmaybangalore 1:046dd94c57ce 20 float timeout = 0.05f;
tanmaybangalore 1:046dd94c57ce 21 int count,ended;
tanmaybangalore 1:046dd94c57ce 22 char buf[256];
tanmaybangalore 1:046dd94c57ce 23 char motorCmd[] = "fwrd";
jhunter029 0:57cec3469a80 24
tanmaybangalore 1:046dd94c57ce 25 /**
tanmaybangalore 1:046dd94c57ce 26 * Get the reply string from the WiFi module
tanmaybangalore 1:046dd94c57ce 27 */
jhunter029 0:57cec3469a80 28 void getreply()
jhunter029 0:57cec3469a80 29 {
jhunter029 0:57cec3469a80 30 memset(buf, '\0', sizeof(buf));
jhunter029 0:57cec3469a80 31 t.start();
jhunter029 0:57cec3469a80 32 ended=0;
jhunter029 0:57cec3469a80 33 count=0;
jhunter029 0:57cec3469a80 34 while(!ended) {
tanmaybangalore 1:046dd94c57ce 35 if(dev.readable()) {
tanmaybangalore 1:046dd94c57ce 36 buf[count] = dev.getc();
jhunter029 0:57cec3469a80 37 count++;
jhunter029 0:57cec3469a80 38 }
jhunter029 0:57cec3469a80 39 if(t.read() > timeout) {
jhunter029 0:57cec3469a80 40 ended = 1;
jhunter029 0:57cec3469a80 41 t.stop();
jhunter029 0:57cec3469a80 42 t.reset();
jhunter029 0:57cec3469a80 43 }
jhunter029 0:57cec3469a80 44 }
jhunter029 0:57cec3469a80 45 }
tanmaybangalore 1:046dd94c57ce 46
tanmaybangalore 1:046dd94c57ce 47 /**
tanmaybangalore 1:046dd94c57ce 48 *
tanmaybangalore 1:046dd94c57ce 49 */
tanmaybangalore 1:046dd94c57ce 50 void getMotorCommand() {
tanmaybangalore 1:046dd94c57ce 51 int index = 3;
tanmaybangalore 1:046dd94c57ce 52 // iterate backwards through the message buffer
tanmaybangalore 1:046dd94c57ce 53 for (int i = sizeof(buf) - 1; i > 2; i--){
tanmaybangalore 1:046dd94c57ce 54 // Check if the current character is a null
tanmaybangalore 1:046dd94c57ce 55 if (buf[i] != '\0') {
tanmaybangalore 1:046dd94c57ce 56 motorCmd[index] = buf[i - 2]; // Copy the character
tanmaybangalore 1:046dd94c57ce 57 if (index == 0){
tanmaybangalore 1:046dd94c57ce 58 break; // Break if we have completed getting the command
tanmaybangalore 1:046dd94c57ce 59 } else {
tanmaybangalore 1:046dd94c57ce 60 index--; // Decrement otherwise
tanmaybangalore 1:046dd94c57ce 61 }
tanmaybangalore 1:046dd94c57ce 62 }
tanmaybangalore 1:046dd94c57ce 63 }
tanmaybangalore 1:046dd94c57ce 64 }
tanmaybangalore 1:046dd94c57ce 65
tanmaybangalore 1:046dd94c57ce 66 /**
tanmaybangalore 1:046dd94c57ce 67 * Run the motor based on the command passed through the serial interface.
tanmaybangalore 1:046dd94c57ce 68 */
tanmaybangalore 1:046dd94c57ce 69 void runMotor(){
tanmaybangalore 1:046dd94c57ce 70 if (strcmp(motorCmd, (char *)"fwrd") == 0) {
tanmaybangalore 1:046dd94c57ce 71 stby = 1; // Start H-bridge driver
tanmaybangalore 1:046dd94c57ce 72 fwdA = 1;
tanmaybangalore 1:046dd94c57ce 73 fwdB = 1;
tanmaybangalore 1:046dd94c57ce 74 backA = 0;
tanmaybangalore 1:046dd94c57ce 75 backB = 0;
tanmaybangalore 1:046dd94c57ce 76 motorACtrl = speed;
tanmaybangalore 1:046dd94c57ce 77 motorBCtrl = speed;
tanmaybangalore 1:046dd94c57ce 78 } else if (strcmp(motorCmd, (char *)"back") == 0) {
tanmaybangalore 1:046dd94c57ce 79 stby = 1; // Start H-bridge driver
tanmaybangalore 1:046dd94c57ce 80 fwdA = 0;
tanmaybangalore 1:046dd94c57ce 81 fwdB = 0;
tanmaybangalore 1:046dd94c57ce 82 backA = 1;
tanmaybangalore 1:046dd94c57ce 83 backB = 1;
tanmaybangalore 1:046dd94c57ce 84 motorACtrl = speed;
tanmaybangalore 1:046dd94c57ce 85 motorBCtrl = speed;
tanmaybangalore 1:046dd94c57ce 86 } else if (strcmp(motorCmd, (char *)"left") == 0) {
tanmaybangalore 1:046dd94c57ce 87 stby = 1; // Start H-bridge driver
tanmaybangalore 1:046dd94c57ce 88 fwdA = 1;
tanmaybangalore 1:046dd94c57ce 89 fwdB = 0;
tanmaybangalore 1:046dd94c57ce 90 backA = 0;
tanmaybangalore 1:046dd94c57ce 91 backB = 1;
tanmaybangalore 1:046dd94c57ce 92 motorACtrl = speed;
tanmaybangalore 1:046dd94c57ce 93 motorBCtrl = speed;
tanmaybangalore 1:046dd94c57ce 94 } else if (strcmp(motorCmd, (char *)"rght") == 0) {
tanmaybangalore 1:046dd94c57ce 95 stby = 1; // Start H-bridge driver
tanmaybangalore 1:046dd94c57ce 96 fwdA = 0;
tanmaybangalore 1:046dd94c57ce 97 fwdB = 1;
tanmaybangalore 1:046dd94c57ce 98 backA = 1;
tanmaybangalore 1:046dd94c57ce 99 backB = 0;
tanmaybangalore 1:046dd94c57ce 100 motorACtrl = speed;
tanmaybangalore 1:046dd94c57ce 101 motorBCtrl = speed;
tanmaybangalore 1:046dd94c57ce 102 } else if (strcmp(motorCmd, (char *)"stop") == 0) {
tanmaybangalore 1:046dd94c57ce 103 stby = 1; // Start H-bridge driver
tanmaybangalore 1:046dd94c57ce 104 fwdA = 0;
tanmaybangalore 1:046dd94c57ce 105 fwdB = 0;
tanmaybangalore 1:046dd94c57ce 106 backA = 0;
tanmaybangalore 1:046dd94c57ce 107 backB = 0;
tanmaybangalore 1:046dd94c57ce 108 motorACtrl = 0.0f; // Set speed to 0
tanmaybangalore 1:046dd94c57ce 109 motorBCtrl = 0.0f;
tanmaybangalore 1:046dd94c57ce 110 stby = 0; // Turn off H-bridge driver
tanmaybangalore 1:046dd94c57ce 111 }
tanmaybangalore 1:046dd94c57ce 112 }
tanmaybangalore 1:046dd94c57ce 113
tanmaybangalore 1:046dd94c57ce 114 void dev_recv()
tanmaybangalore 1:046dd94c57ce 115 {
tanmaybangalore 1:046dd94c57ce 116 led1 = !led1;
tanmaybangalore 1:046dd94c57ce 117 getreply(); // Get the serial message
tanmaybangalore 1:046dd94c57ce 118 getMotorCommand(); // Extract the motor command from the message
tanmaybangalore 1:046dd94c57ce 119 runMotor(); // Run the motor based on the command
tanmaybangalore 1:046dd94c57ce 120 }
jhunter029 0:57cec3469a80 121
tanmaybangalore 1:046dd94c57ce 122 void pc_recv()
tanmaybangalore 1:046dd94c57ce 123 {
tanmaybangalore 1:046dd94c57ce 124 led4 = !led4;
tanmaybangalore 1:046dd94c57ce 125 while(pc.readable()) {
tanmaybangalore 1:046dd94c57ce 126 dev.putc(pc.getc());
tanmaybangalore 1:046dd94c57ce 127 }
tanmaybangalore 1:046dd94c57ce 128 }
tanmaybangalore 1:046dd94c57ce 129
tanmaybangalore 1:046dd94c57ce 130 int main()
tanmaybangalore 1:046dd94c57ce 131 {
tanmaybangalore 1:046dd94c57ce 132 pc.baud(115200);
tanmaybangalore 1:046dd94c57ce 133 dev.baud(115200);
tanmaybangalore 1:046dd94c57ce 134
tanmaybangalore 1:046dd94c57ce 135 pc.attach(&pc_recv, Serial::RxIrq);
tanmaybangalore 1:046dd94c57ce 136 dev.attach(&dev_recv, Serial::RxIrq);
tanmaybangalore 1:046dd94c57ce 137
tanmaybangalore 1:046dd94c57ce 138 // Initialize the motors
tanmaybangalore 1:046dd94c57ce 139 motorACtrl.period(0.01f);
tanmaybangalore 1:046dd94c57ce 140 motorBCtrl.period(0.01f);
tanmaybangalore 1:046dd94c57ce 141
tanmaybangalore 1:046dd94c57ce 142 while(1) {
tanmaybangalore 1:046dd94c57ce 143 sleep();
tanmaybangalore 1:046dd94c57ce 144 }
tanmaybangalore 1:046dd94c57ce 145 }