![](/media/cache/img/default_profile.jpg.50x50_q85.jpg)
Xbee robot with telemetry, controller code
Dependencies: 4DGL-uLCD-SE DebounceIn PinDetect mbed-rtos mbed
Fork of 2Xbee_Controller by
Diff: main.cpp
- Revision:
- 1:2c67639795a4
- Parent:
- 0:9439ccb44422
- Child:
- 2:887ff47839ed
--- a/main.cpp Fri Nov 28 18:00:06 2014 +0000 +++ b/main.cpp Fri Apr 29 19:08:24 2016 +0000 @@ -1,75 +1,128 @@ + #include "mbed.h" +#include "DebounceIn.h" +#include "PinDetect.h" +#include "uLCD_4DGL.h" #include "rtos.h" -DigitalOut myled(LED1); -PwmOut myled2(LED2); -PwmOut myled3(LED3); -PwmOut myled4(LED4); + +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 + +Serial pc(USBTX, USBRX); +DigitalOut led1(LED1); +DigitalOut led2(LED2); +DigitalOut led3(LED3); +DigitalOut led4(LED4); +Mutex xbee_lock; +Mutex uLCD_lock; +int a; +volatile int voltage; +volatile int distance; -inline float random_number(){ - return (rand()/(float(RAND_MAX))); + +void read_battery() +{ + + xbee1.putc(15); + //if (xbee1.readable()) { + uLCD_lock.lock(); + voltage = xbee1.getc(); + uLCD_lock.unlock(); + //} + //pc.printf("voltage: %i\n",voltage); } - -void beacon(void const *args){ + +void read_distance() +{ + 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) +{ while(1) { - //LED warm up effect using PWM - for(int i=0; i<50; i++) { - myled2 = i/50.0; - Thread::wait(1000.0*0.02); - } - //LED at full brightness level - myled2 = 1.0; - Thread::wait(1000.0*0.25); - //LED cool down effect using PWM - for(int i=49; i>0; i--) { - myled2 = i/50.0; - Thread::wait(1000.0*0.02); - } - //LED off - myled2 = 0.0; - Thread::wait(1000.0*1.5); - } -} -void welding(void const *args) { - float x = 0.0; - while(1) { - //get a new random number for PWM - x = random_number(); - //add some exponential brightness scaling - //for more of a fast flash effect - myled3 = x*x*x; - //fast update rate for welding flashes - Thread::wait(1000.0*0.02); - //add a random pause between welds - if (random_number()>0.9925) { - myled3 = 0.0; - Thread::wait(1000.0*4.0*random_number()); - } + + uLCD_lock.lock(); + uLCD.locate(1,5); + float temp = float(1/(float(distance)/232*3.3))*30.53-2.64; + uLCD.printf("%3.3fcm",temp); + uLCD_lock.unlock(); + //pc.printf("%i\n",distance); + Thread::wait(300); + } } -void lighthouse(void const *args){ - float y=0.0; +void display_battery(void const *args) +{ while(1) { - for(double x=0.0; x <= 3.14159; x = x + 0.0314159) { - y = sin(x); //nice periodic function 0..1..0 - myled4 = y*y*y;//exponential effect - needs a sharp peak - Thread::wait(1000.0*.025); - } - myled4 = 0.0; - //most lighthouses have a 5 second delay - so add another 2.5 - Thread::wait(1000.0*2.5); + 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() { - Thread thread2(beacon); - Thread thread3(welding); - Thread thread4(lighthouse); - //main runs standard LED blink demo - while(1) { - myled = 1; - Thread::wait(1000.0*0.2); - myled = 0; - Thread::wait(1000.0*0.2); +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); + uLCD.cls(); + wait(.01); + uLCD.locate(1,2); + uLCD.printf("Battery:\n"); + uLCD.locate(1,4); + uLCD.printf("Distance:\n"); + //Thread thread2(read_distance); + //Thread thread3(read_battery); + Thread thread4(display_distance); + Thread thread5(display_battery); + + //led1=1; + //} + while (1) { + 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; + } + read_distance(); + read_battery(); } -} +} \ No newline at end of file