Xbee robot with telemetry, controller code
Dependencies: 4DGL-uLCD-SE DebounceIn PinDetect mbed-rtos mbed
Fork of 2Xbee_Controller by
main.cpp@0:9439ccb44422, 2014-11-28 (annotated)
- Committer:
- 4180_1
- Date:
- Fri Nov 28 18:00:06 2014 +0000
- Revision:
- 0:9439ccb44422
- Child:
- 1:2c67639795a4
ver 1.0
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
4180_1 | 0:9439ccb44422 | 1 | #include "mbed.h" |
4180_1 | 0:9439ccb44422 | 2 | #include "rtos.h" |
4180_1 | 0:9439ccb44422 | 3 | DigitalOut myled(LED1); |
4180_1 | 0:9439ccb44422 | 4 | PwmOut myled2(LED2); |
4180_1 | 0:9439ccb44422 | 5 | PwmOut myled3(LED3); |
4180_1 | 0:9439ccb44422 | 6 | PwmOut myled4(LED4); |
4180_1 | 0:9439ccb44422 | 7 | |
4180_1 | 0:9439ccb44422 | 8 | inline float random_number(){ |
4180_1 | 0:9439ccb44422 | 9 | return (rand()/(float(RAND_MAX))); |
4180_1 | 0:9439ccb44422 | 10 | } |
4180_1 | 0:9439ccb44422 | 11 | |
4180_1 | 0:9439ccb44422 | 12 | void beacon(void const *args){ |
4180_1 | 0:9439ccb44422 | 13 | while(1) { |
4180_1 | 0:9439ccb44422 | 14 | //LED warm up effect using PWM |
4180_1 | 0:9439ccb44422 | 15 | for(int i=0; i<50; i++) { |
4180_1 | 0:9439ccb44422 | 16 | myled2 = i/50.0; |
4180_1 | 0:9439ccb44422 | 17 | Thread::wait(1000.0*0.02); |
4180_1 | 0:9439ccb44422 | 18 | } |
4180_1 | 0:9439ccb44422 | 19 | //LED at full brightness level |
4180_1 | 0:9439ccb44422 | 20 | myled2 = 1.0; |
4180_1 | 0:9439ccb44422 | 21 | Thread::wait(1000.0*0.25); |
4180_1 | 0:9439ccb44422 | 22 | //LED cool down effect using PWM |
4180_1 | 0:9439ccb44422 | 23 | for(int i=49; i>0; i--) { |
4180_1 | 0:9439ccb44422 | 24 | myled2 = i/50.0; |
4180_1 | 0:9439ccb44422 | 25 | Thread::wait(1000.0*0.02); |
4180_1 | 0:9439ccb44422 | 26 | } |
4180_1 | 0:9439ccb44422 | 27 | //LED off |
4180_1 | 0:9439ccb44422 | 28 | myled2 = 0.0; |
4180_1 | 0:9439ccb44422 | 29 | Thread::wait(1000.0*1.5); |
4180_1 | 0:9439ccb44422 | 30 | } |
4180_1 | 0:9439ccb44422 | 31 | } |
4180_1 | 0:9439ccb44422 | 32 | void welding(void const *args) { |
4180_1 | 0:9439ccb44422 | 33 | float x = 0.0; |
4180_1 | 0:9439ccb44422 | 34 | while(1) { |
4180_1 | 0:9439ccb44422 | 35 | //get a new random number for PWM |
4180_1 | 0:9439ccb44422 | 36 | x = random_number(); |
4180_1 | 0:9439ccb44422 | 37 | //add some exponential brightness scaling |
4180_1 | 0:9439ccb44422 | 38 | //for more of a fast flash effect |
4180_1 | 0:9439ccb44422 | 39 | myled3 = x*x*x; |
4180_1 | 0:9439ccb44422 | 40 | //fast update rate for welding flashes |
4180_1 | 0:9439ccb44422 | 41 | Thread::wait(1000.0*0.02); |
4180_1 | 0:9439ccb44422 | 42 | //add a random pause between welds |
4180_1 | 0:9439ccb44422 | 43 | if (random_number()>0.9925) { |
4180_1 | 0:9439ccb44422 | 44 | myled3 = 0.0; |
4180_1 | 0:9439ccb44422 | 45 | Thread::wait(1000.0*4.0*random_number()); |
4180_1 | 0:9439ccb44422 | 46 | } |
4180_1 | 0:9439ccb44422 | 47 | } |
4180_1 | 0:9439ccb44422 | 48 | } |
4180_1 | 0:9439ccb44422 | 49 | |
4180_1 | 0:9439ccb44422 | 50 | void lighthouse(void const *args){ |
4180_1 | 0:9439ccb44422 | 51 | float y=0.0; |
4180_1 | 0:9439ccb44422 | 52 | while(1) { |
4180_1 | 0:9439ccb44422 | 53 | for(double x=0.0; x <= 3.14159; x = x + 0.0314159) { |
4180_1 | 0:9439ccb44422 | 54 | y = sin(x); //nice periodic function 0..1..0 |
4180_1 | 0:9439ccb44422 | 55 | myled4 = y*y*y;//exponential effect - needs a sharp peak |
4180_1 | 0:9439ccb44422 | 56 | Thread::wait(1000.0*.025); |
4180_1 | 0:9439ccb44422 | 57 | } |
4180_1 | 0:9439ccb44422 | 58 | myled4 = 0.0; |
4180_1 | 0:9439ccb44422 | 59 | //most lighthouses have a 5 second delay - so add another 2.5 |
4180_1 | 0:9439ccb44422 | 60 | Thread::wait(1000.0*2.5); |
4180_1 | 0:9439ccb44422 | 61 | } |
4180_1 | 0:9439ccb44422 | 62 | } |
4180_1 | 0:9439ccb44422 | 63 | |
4180_1 | 0:9439ccb44422 | 64 | int main() { |
4180_1 | 0:9439ccb44422 | 65 | Thread thread2(beacon); |
4180_1 | 0:9439ccb44422 | 66 | Thread thread3(welding); |
4180_1 | 0:9439ccb44422 | 67 | Thread thread4(lighthouse); |
4180_1 | 0:9439ccb44422 | 68 | //main runs standard LED blink demo |
4180_1 | 0:9439ccb44422 | 69 | while(1) { |
4180_1 | 0:9439ccb44422 | 70 | myled = 1; |
4180_1 | 0:9439ccb44422 | 71 | Thread::wait(1000.0*0.2); |
4180_1 | 0:9439ccb44422 | 72 | myled = 0; |
4180_1 | 0:9439ccb44422 | 73 | Thread::wait(1000.0*0.2); |
4180_1 | 0:9439ccb44422 | 74 | } |
4180_1 | 0:9439ccb44422 | 75 | } |