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

main.cpp

Committer:
tanmaybangalore
Date:
2016-10-31
Revision:
1:046dd94c57ce
Parent:
0:57cec3469a80
Child:
2:2d87957b577b

File content as of revision 1:046dd94c57ce:

#include "mbed.h"
#include "rtos.h"
 
RawSerial  pc(USBTX, USBRX);
RawSerial  dev(p28,p27);
DigitalOut led1(LED1);
DigitalOut led4(LED4);

// Set up the motor pins
PwmOut motorACtrl(p21);
PwmOut motorBCtrl(p22);
DigitalOut backA(p20);
DigitalOut fwdA(p19);
DigitalOut stby(p18);
DigitalOut fwdB(p17);
DigitalOut backB(p16);
Timer t;
float speed = 0.9f;

float timeout = 0.05f;
int  count,ended;
char buf[256];
char motorCmd[] = "fwrd";
 
/**
 *  Get the reply string from the WiFi module
 */
void getreply()
{
    memset(buf, '\0', sizeof(buf));
    t.start();
    ended=0;
    count=0;
    while(!ended) {
        if(dev.readable()) {
            buf[count] = dev.getc();
            count++;
        }
        if(t.read() > timeout) {
            ended = 1;
            t.stop();
            t.reset();
        }
    }
}

/**
 *
 */
void getMotorCommand() {
    int index = 3;
    // iterate backwards through the message buffer
    for (int i = sizeof(buf) - 1; i > 2; i--){
        // Check if the current character is a null
        if (buf[i] != '\0') {
            motorCmd[index] = buf[i - 2];   // Copy the character
            if (index == 0){
                break;      // Break if we have completed getting the command
            } else {
                index--;    // Decrement otherwise
            }
        }
    }
}

/**
 *  Run the motor based on the command passed through the serial interface.
 */
void runMotor(){
    if (strcmp(motorCmd, (char *)"fwrd") == 0) {
           stby = 1;        // Start H-bridge driver
           fwdA = 1;
           fwdB = 1;
           backA = 0;
           backB = 0;
           motorACtrl = speed;
           motorBCtrl = speed;
    } else if (strcmp(motorCmd, (char *)"back") == 0) {
           stby = 1;        // Start H-bridge driver
           fwdA = 0;
           fwdB = 0;
           backA = 1;
           backB = 1;
           motorACtrl = speed;
           motorBCtrl = speed;
    } else if (strcmp(motorCmd, (char *)"left") == 0) {
           stby = 1;        // Start H-bridge driver
           fwdA = 1;
           fwdB = 0;
           backA = 0;
           backB = 1;
           motorACtrl = speed;
           motorBCtrl = speed;
    } else if (strcmp(motorCmd, (char *)"rght") == 0) {
           stby = 1;        // Start H-bridge driver
           fwdA = 0;
           fwdB = 1;
           backA = 1;
           backB = 0;
           motorACtrl = speed;
           motorBCtrl = speed;
    } else if (strcmp(motorCmd, (char *)"stop") == 0) {
           stby = 1;        // Start H-bridge driver
           fwdA = 0;
           fwdB = 0;
           backA = 0;
           backB = 0;
           motorACtrl = 0.0f;   // Set speed to 0
           motorBCtrl = 0.0f;
           stby = 0;        // Turn off H-bridge driver
    }
}

void dev_recv()
{
    led1 = !led1;
    getreply();         // Get the serial message
    getMotorCommand();  // Extract the motor command from the message
    runMotor();         // Run the motor based on the command
}
 
void pc_recv()
{
    led4 = !led4;
    while(pc.readable()) {
        dev.putc(pc.getc());
    }
}
 
int main()
{
    pc.baud(115200);
    dev.baud(115200);
 
    pc.attach(&pc_recv, Serial::RxIrq);
    dev.attach(&dev_recv, Serial::RxIrq);
    
    // Initialize the motors
    motorACtrl.period(0.01f);
    motorBCtrl.period(0.01f);
 
    while(1) {
        sleep();
    }
}