Xbee robot with telemetry, controller code
Dependencies: 4DGL-uLCD-SE DebounceIn PinDetect mbed-rtos mbed
Fork of 2Xbee_Controller by
main.cpp
- Committer:
- yhbyhb4433
- Date:
- 2016-04-29
- Revision:
- 2:887ff47839ed
- Parent:
- 1:2c67639795a4
File content as of revision 2:887ff47839ed:
#include "mbed.h" #include "DebounceIn.h" #include "PinDetect.h" #include "uLCD_4DGL.h" #include "rtos.h" Ticker flipper; uLCD_4DGL uLCD(p13,p14,p28); // serial tx, serial rx, reset pin; Serial xbee1(p9, p10); DigitalOut rst1(p11); DebounceIn forward(p16); DebounceIn left(p17); DebounceIn reverse(p18); DebounceIn right(p24); //forward 16 //left 17 //reverse 18 //right 19 DigitalOut led1(LED1); //four leds to display the command users send in DigitalOut led2(LED2); DigitalOut led3(LED3); DigitalOut led4(LED4); Mutex uLCD_lock; //mutex lock for the uLCD communication volatile int voltage; volatile int distance; void read_battery() //read battery information back from the robot, need a mutex lock when //the information is written to the variable, otherwise "display_battery" //thread will try to access the the variable while it's changed { xbee1.putc(15); //if (xbee1.readable()) { uLCD_lock.lock(); voltage = xbee1.getc(); uLCD_lock.unlock(); //} //pc.printf("voltage: %i\n",voltage); } void read_distance() //read distance information back from the robot, need a mutex lock when //the information is written to the variable, otherwise "display_distance" //thread will try to access the the variable while it's changed { xbee1.putc(16); //if (xbee1.readable()) { uLCD_lock.lock(); distance = xbee1.getc(); uLCD_lock.unlock(); //} //pc.printf("distance: %i\n",distance); } void display_distance(void const *args) //update the distance sensor information in the LCD once every 0.3s { while(1) { uLCD_lock.lock(); uLCD.locate(1,5); float temp = float(1/(float(distance)/232*3.3))*30.53-2.64; //distance*232 scales reading back to a float between 0-1. Then a linear //approximation is used to convert float reading to cm reading uLCD.printf("%3.3fcm",temp); uLCD_lock.unlock(); //pc.printf("%i\n",distance); Thread::wait(300); } } void display_battery(void const *args) //update the battery information in the LCD once every 0.3s { while(1) { uLCD_lock.lock(); uLCD.locate(1,3); uLCD.printf("%2.2fV",float(voltage)/232*3.3*2); //pc.printf("%i\n",voltage); uLCD_lock.unlock(); Thread::wait(300); } } int main() { forward.mode(PullUp); reverse.mode(PullUp); left.mode(PullUp); right.mode(PullUp); // reset the xbees (at least 200ns) rst1 = 0; wait_ms(100); rst1 = 1; wait_ms(100); uLCD.baudrate(3000000); //update the uLCD.cls(); wait(.01); uLCD.locate(1,2); uLCD.printf("Battery:\n"); uLCD.locate(1,4); uLCD.printf("Distance:\n"); Thread thread4(display_distance); Thread thread5(display_battery); while (1) { // loop to check if each pushbutton is pushed. If pushed, send a corresponding command to the robot if (forward==0) { led1 = 1; xbee1.putc(11); } else if (reverse==0) { led3 = 1; xbee1.putc(12); } else if (left==0) { led2 = 1; xbee1.putc(13); } else if (right == 0) { led4 = 1; xbee1.putc(14); } else { xbee1.putc(0); led1=led2=led3=led4=0; //reset all LED at the end of loop } read_distance(); //request distance information at the end of every loop read_battery(); //request battery information at the end of every loop } }