Project ROBot
Dependencies: C12832_lcd HCSR04 TB6612FNG WiflyInterface mbed USBDevice
Welcome to Project ROBot!
ROBot is a robot based on an mbed platform. It is remotely controlled from a PC via a WiFi connection. The vehicle is also equipped with an ultrasonic range finder to detect any obstacles in the direct path of the ROBot and stop any forward motion if a collision is deemed imminent.
Components
The major components of the ROBot are:
- mbed application board
- RN-XV WiFly wireless board (datasheet)
- Toshiba TB6612FNG motor driver (datasheet)
- HC-SR04 ultrasonic range finder (datasheet)
- Redbot kit (just the chassis, motors, and wheels)
- 5V power source
- 6V power source
Schematic
Here is a schematic of the ROBot: schematic
Software Block Diagram
A state machine processes all input and sets the output
Standard Operation
A wireless remote connection from a PC sends commands List of commands:
- 'e' - move forward
- 'c' - move backward
- 'f' - move right
- 's' - move left
- 'd' - stop
commands are case-sensitive
Collision Warning/Avoidance
The range finder ranges every 65 millisecs
When an obstacle is within 50cm, a warning is sent back to the remote host (PC)
When an obstacle is within 30cm, all forward motion ceases. The remote host operator must use other commands to move the ROBot away from the obstacle.
Video
- ROBot and its parts
- Using a terminal emulator (Tera Term) to connect to the ROBot via a USB connection for wireless configuration. Once connected, the ROBot's IP address is shown in the terminal and on the LCD screen.
- Creating a telnet session to remotely control the ROBot. Connect to the IP address given in the previous step.
- Remote host operation of the ROBot
- Collision Warning/Avoidance
Things To Do
I'm not sure when I'll get to these but here are some things I need/would like to do eventually:
- improve the logic for collision
- incorporate the accelerometer on the mbed application board
- develop an Android/iPhone app for remote operation
- add video for true remote operation
Acknowledgements
This project would not have been possible without the following people (in no particular order)... Andrew, Bob, Morgan, Avnish. Thank you for your support! <3
ROBot.cpp
- Committer:
- rabad1
- Date:
- 2013-11-24
- Revision:
- 0:34bcb9bae5ea
- Child:
- 1:e918d1aa9c26
File content as of revision 0:34bcb9bae5ea:
/* File: ROBot.cpp * Author: Robert Abad * Desc: ROBot application. Motorized robot with the following features: * - WiFly interface for remote operation * - ultrasonic range finder * - motor driver for all locomotion */ #include "mbed.h" #include "C12832_lcd.h" // uncomment to enable DEBUG // enabling DEBUG does the following: // - show status of various state machines #define DEBUG // uncomment to enable (or comment to disable) #define ENABLE_WIFI #define ENABLE_RANGE_FINDER #define ENABLE_MOTOR_DRIVER #include "WiflyInterface.h" #include "HCSR04.h" #include "TB6612FNG.h" // WiFi related #define WIFLY_PIN_TX (p9) #define WIFLY_PIN_RX (p10) #define WIFLY_PIN_STATUS (p29) #define WIFLY_PIN_RESET (p30) #define WIFI_ECHO_SERVER_PORT (7) #define WIFI_BUFFER_SIZE (64) //WiflyInterface wifly(WIFLY_PIN_TX, WIFLY_PIN_RX, WIFLY_PIN_RESET, WIFLY_PIN_STATUS, "purpleMonkey", NULL, NONE); WiflyInterface wifly(WIFLY_PIN_TX, WIFLY_PIN_RX, WIFLY_PIN_RESET, WIFLY_PIN_STATUS, "FishNet", "ffeeddccbbaa99887766554433", WEP_128); //WiflyInterface wifly(WIFLY_PIN_TX, WIFLY_PIN_RX, WIFLY_PIN_RESET, WIFLY_PIN_STATUS, "Student", NULL, NONE); TCPSocketServer server; TCPSocketConnection client; char wiflyBuffer[WIFI_BUFFER_SIZE]; // Range Finder #define HCSR04_PIN_TRIGGER (p14) #define HCSR04_PIN_ECHO (p15) #define RANGE_FINDER_MEAS_INTERVAL (0.5) HCSR04 rangeFinder( HCSR04_PIN_TRIGGER, HCSR04_PIN_ECHO ); void rangeFinderTimer_cb(void); Ticker rangeFinderTimer; #define RANGE_WARNING (0.5f) // meters #define RANGE_COLLISION (0.15f) // meters // MotorDriver #define TB6612FNG_PIN_PWMA (p22) #define TB6612FNG_PIN_AIN1 (p17) #define TB6612FNG_PIN_AIN2 (p16) #define TB6612FNG_PIN_PWMB (p21) #define TB6612FNG_PIN_BIN1 (p19) #define TB6612FNG_PIN_BIN2 (p20) #define TB6612FNG_PIN_NSTBY (p18) TB6612FNG motorDriver( TB6612FNG_PIN_PWMA, TB6612FNG_PIN_AIN1, TB6612FNG_PIN_AIN2, TB6612FNG_PIN_PWMB, TB6612FNG_PIN_BIN1, TB6612FNG_PIN_BIN2, TB6612FNG_PIN_NSTBY ); float fPwmPeriod; float fPwmPulsewidth; // ROBot State Machine status typedef enum { STATUS_OK, STATUS_NEAR_COLLISION } etROBot_STATUS; etROBot_STATUS ROBot_STATUS; // ROBot commands typedef enum { CMD_MOVE_STOP = 'd', CMD_MOVE_FORWARD = 'e', CMD_MOVE_BACKWARD = 'c', CMD_MOVE_LEFT = 's', CMD_MOVE_RIGHT = 'f', CMD_MOVE_UNKNOWN = -1 } etROBot_CMD; #define NUM_CMDS (5) void moveStop(void); void moveForward(void); void moveBackward(void); void moveLeft(void); void moveRight(void); typedef struct { etROBot_CMD cmd; void (*cmd_func)(void); } etROBot_COMMAND; const etROBot_COMMAND command[] = { { CMD_MOVE_STOP, moveStop }, { CMD_MOVE_FORWARD, moveForward }, { CMD_MOVE_BACKWARD, moveBackward }, { CMD_MOVE_LEFT, moveLeft }, { CMD_MOVE_RIGHT, moveRight } }; int getCmdIdx(etROBot_CMD cmd); C12832_LCD lcdScreen; //status display // forward declarations void init(void); // intializie void ROBot_sm(void); // ROBot state machine /* Name: main() * Desc: entry point and main thread of execution * Inputs: none * Outputs: none */ int main(void) { init(); while (1) { ROBot_sm(); } } /* Name: init() * Desc: initialize the ROBot * Inputs: none * Outputs: none */ void init(void) { // intialize peripherals lcdScreen.cls(); // connect WiFi #ifdef ENABLE_WIFI #ifdef DEBUG printf("Connecting to WiFi..."); #endif /* DEBUG */ wifly.init(); // use DHCP #ifdef DEBUG printf("DONE\n\r"); #endif /* DEBUG */ while (!wifly.connect()) // join the network { printf("attempting to connect\n\r"); } #ifdef DEBUG printf("IP Address is %s\n\r", wifly.getIPAddress()); #endif /* DEBUG */ lcdScreen.locate(0,0); lcdScreen.printf("IP: %s", wifly.getIPAddress()); server.bind(WIFI_ECHO_SERVER_PORT); server.listen(); #ifdef DEBUG printf("\n\rWait for remote connection...\n\r"); #endif /* DEBUG */ server.accept(client); // wait until remote host connects client.set_blocking(false); #ifdef DEBUG printf("remote host connected!\n\r"); #endif /* DEBUG */ lcdScreen.locate(0,10); lcdScreen.printf("remote host connected!\n\r"); #endif /* ENABLE_WIFI */ // start Range Finder #ifdef ENABLE_RANGE_FINDER #ifdef DEBUG printf("Initialize Range Finder..."); #endif /* DEBUG */ rangeFinderTimer.attach(rangeFinderTimer_cb, RANGE_FINDER_MEAS_INTERVAL); #ifdef DEBUG printf("DONE\n\r"); #endif /* DEBUG */ #endif /* ENABLE_RANGE_FINDER */ // start Motor Driver #ifdef ENABLE_MOTOR_DRIVER #ifdef DEBUG printf("Initialize Motor Driver..."); #endif /* DEBUG */ fPwmPeriod = TB6612FNG_PWM_PERIOD_DEFAULT; fPwmPulsewidth = TB6612FNG_PWM_PULSEWIDTH_DEFAULT; motorDriver.setPwmAperiod(fPwmPeriod); motorDriver.setPwmBperiod(fPwmPeriod); motorDriver.setPwmApulsewidth(fPwmPulsewidth); motorDriver.setPwmBpulsewidth(fPwmPulsewidth); #ifdef DEBUG printf("DONE\n\r"); #endif /* DEBUG */ #endif /* ENABLE_MOTOR_DRIVER */ ROBot_STATUS = STATUS_OK; #ifdef DEBUG printf("Initialization complete!\n\r"); #endif /* DEBUG */ strcpy( wiflyBuffer, "ROBot awaiting commands\n\r"); client.send_all(wiflyBuffer, strlen(wiflyBuffer)); } /* Name: ROBot_sm() * Desc: ROBot state machine. Processes all peripheral data and sets * status accordingly * Inputs: none * Outputs: none */ void ROBot_sm(void) { float range; if ( rangeFinder.getMeas(range) != RANGE_MEAS_INVALID ) { #ifdef DEBUG printf("range = %.3f m\n\r", range); #endif /* DEBUG */ if ( range < RANGE_COLLISION ) { strcpy( wiflyBuffer, "\n\r**** COLLISION AVOIDANCE ****\n\r"); client.send_all(wiflyBuffer, strlen(wiflyBuffer)); if ( ROBot_STATUS != STATUS_NEAR_COLLISION ) { moveStop(); } ROBot_STATUS = STATUS_NEAR_COLLISION; #ifdef DEBUG printf("%s", wiflyBuffer); #endif /* DEBUG */ } else if ( range < RANGE_WARNING ) { strcpy( wiflyBuffer, "\n\r**** COLLISION WARNING ****"); sprintf(wiflyBuffer, "%s %0.3f\n\r", wiflyBuffer, range); client.send_all(wiflyBuffer, strlen(wiflyBuffer)); #ifdef DEBUG printf("%s", wiflyBuffer); #endif /* DEBUG */ if ( ROBot_STATUS == STATUS_NEAR_COLLISION ) { ROBot_STATUS = STATUS_OK; } } else if ( ROBot_STATUS == STATUS_NEAR_COLLISION ) { ROBot_STATUS = STATUS_OK; } } // receive input and process it memset(wiflyBuffer, 0, sizeof(wiflyBuffer)); int numCmds = client.receive(wiflyBuffer, sizeof(wiflyBuffer)); etROBot_CMD *pCmd = (etROBot_CMD *) wiflyBuffer; while ( numCmds > 0 ) { int cmdIdx = getCmdIdx(*pCmd); if ( cmdIdx == CMD_MOVE_UNKNOWN ) { pCmd++; numCmds--; continue; } switch ( ROBot_STATUS ) { case STATUS_OK: command[cmdIdx].cmd_func(); break; case STATUS_NEAR_COLLISION: if ( *pCmd != CMD_MOVE_FORWARD ) { command[cmdIdx].cmd_func(); } break; } pCmd++; numCmds--; } } /* Name: rangeFinderTimer_cb() * Desc: callback function for range finder timer. Timer is used to * initiate range finder measurements * Inputs: none * Outputs: none */ void rangeFinderTimer_cb(void) { rangeFinder.startMeas(); } /* Name: getCmdIdx() * Desc: returns command[] index of the input command * Inputs: none * Outputs: none */ int getCmdIdx(etROBot_CMD cmd) { int i; for ( i = 0; i < NUM_CMDS; i++) { if ( cmd == command[i].cmd ) { return i; } } return CMD_MOVE_UNKNOWN; } void moveStop(void) { motorDriver.standby(); } void moveForward(void) { motorDriver.motorA_ccw(); motorDriver.motorB_ccw(); } void moveBackward(void) { motorDriver.motorA_cw(); motorDriver.motorB_cw(); } void moveLeft(void) { motorDriver.motorA_ccw(); motorDriver.motorB_cw(); } void moveRight(void) { motorDriver.motorA_cw(); motorDriver.motorB_ccw(); }