Driver for Spektrum (tm) serial receiver, for radio controlled helicopters etc.
main.cpp@1:7b595c4ff156, 2011-10-25 (annotated)
- Committer:
- offroad
- Date:
- Tue Oct 25 18:25:12 2011 +0000
- Revision:
- 1:7b595c4ff156
included example main.cpp
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
offroad | 1:7b595c4ff156 | 1 | /* Example for Spektrum serial receiver driver |
offroad | 1:7b595c4ff156 | 2 | * M. Nentwig, 2011 |
offroad | 1:7b595c4ff156 | 3 | * This program is provided "as is", without any express or implied warranty. */ |
offroad | 1:7b595c4ff156 | 4 | |
offroad | 1:7b595c4ff156 | 5 | #include "mbed.h" |
offroad | 1:7b595c4ff156 | 6 | #include "spektRx.h" |
offroad | 1:7b595c4ff156 | 7 | |
offroad | 1:7b595c4ff156 | 8 | /* Serial input |
offroad | 1:7b595c4ff156 | 9 | * Connections: |
offroad | 1:7b595c4ff156 | 10 | * - orange wire to 3.3V regulated out |
offroad | 1:7b595c4ff156 | 11 | * - black wire to GND |
offroad | 1:7b595c4ff156 | 12 | * - grey wire to pin 10 (for example) |
offroad | 1:7b595c4ff156 | 13 | * - see also http://diydrones.ning.com/profiles/blog/show?id=705844%3ABlogPost%3A64228 */ |
offroad | 1:7b595c4ff156 | 14 | Serial serIn(p9, p10); |
offroad | 1:7b595c4ff156 | 15 | |
offroad | 1:7b595c4ff156 | 16 | PwmOut l1(LED1); |
offroad | 1:7b595c4ff156 | 17 | PwmOut l2(LED2); |
offroad | 1:7b595c4ff156 | 18 | PwmOut l3(LED3); |
offroad | 1:7b595c4ff156 | 19 | PwmOut l4(LED4); |
offroad | 1:7b595c4ff156 | 20 | |
offroad | 1:7b595c4ff156 | 21 | int main(void){ |
offroad | 1:7b595c4ff156 | 22 | serIn.baud(115200); |
offroad | 1:7b595c4ff156 | 23 | |
offroad | 1:7b595c4ff156 | 24 | /* Create parser "object" */ |
offroad | 1:7b595c4ff156 | 25 | spektRx_t p; |
offroad | 1:7b595c4ff156 | 26 | spektRx_init(&p); |
offroad | 1:7b595c4ff156 | 27 | |
offroad | 1:7b595c4ff156 | 28 | while(1) { |
offroad | 1:7b595c4ff156 | 29 | |
offroad | 1:7b595c4ff156 | 30 | /* Process input data from interface, non-blocking */ |
offroad | 1:7b595c4ff156 | 31 | spektRx_runSerial(&p, &serIn); |
offroad | 1:7b595c4ff156 | 32 | |
offroad | 1:7b595c4ff156 | 33 | /* check if (new) frame has been received */ |
offroad | 1:7b595c4ff156 | 34 | if (spektRx_hasValidFrame(&p)){ |
offroad | 1:7b595c4ff156 | 35 | |
offroad | 1:7b595c4ff156 | 36 | /* Dump every 10th received frame */ |
offroad | 1:7b595c4ff156 | 37 | long fn = spektRx_getFrameNum(&p); |
offroad | 1:7b595c4ff156 | 38 | if (fn % 10 == 0){ |
offroad | 1:7b595c4ff156 | 39 | /* Get all channels in bit-accurate form */ |
offroad | 1:7b595c4ff156 | 40 | unsigned short* d = spektRx_getChannelData(&p); |
offroad | 1:7b595c4ff156 | 41 | |
offroad | 1:7b595c4ff156 | 42 | printf("%08li\t", fn); |
offroad | 1:7b595c4ff156 | 43 | printf("ail = %04x\t", (int)d[0]); |
offroad | 1:7b595c4ff156 | 44 | printf("pit = %04x\t", (int)d[1]); |
offroad | 1:7b595c4ff156 | 45 | printf("ele = %04x\t", (int)d[2]); |
offroad | 1:7b595c4ff156 | 46 | printf("rud = %04x\t", (int)d[3]); |
offroad | 1:7b595c4ff156 | 47 | printf("thr = %04x\t", (int)d[4]); |
offroad | 1:7b595c4ff156 | 48 | printf("gear = %04x\t", (int)d[5]); |
offroad | 1:7b595c4ff156 | 49 | printf("aux2 = %04x\t", (int)d[6]); |
offroad | 1:7b595c4ff156 | 50 | printf("\n"); |
offroad | 1:7b595c4ff156 | 51 | } /* every 10th frame */ |
offroad | 1:7b595c4ff156 | 52 | |
offroad | 1:7b595c4ff156 | 53 | /* Get float values and use to dim the LEDs |
offroad | 1:7b595c4ff156 | 54 | * The returned range is (-1 .. 1) if the radio has been set up carefully (endpoints / subtrims) */ |
offroad | 1:7b595c4ff156 | 55 | /* if this is the case, use f=1 */ |
offroad | 1:7b595c4ff156 | 56 | double f = 1.2; /* stretch the range a bit, so that we reach 0/100 % PWM */ |
offroad | 1:7b595c4ff156 | 57 | l1 = spektRx_getChannelValue(&p, 0) * 0.5 * f + 0.5; |
offroad | 1:7b595c4ff156 | 58 | l2 = spektRx_getChannelValue(&p, 1) * 0.5 * f + 0.5; |
offroad | 1:7b595c4ff156 | 59 | l3 = spektRx_getChannelValue(&p, 2) * 0.5 * f + 0.5; |
offroad | 1:7b595c4ff156 | 60 | l4 = spektRx_getChannelValue(&p, 3) * 0.5 * f + 0.5; |
offroad | 1:7b595c4ff156 | 61 | |
offroad | 1:7b595c4ff156 | 62 | /* Finished with the current frame. spectRx_hasValidFrame will return false |
offroad | 1:7b595c4ff156 | 63 | * until the next frame has been received completely. |
offroad | 1:7b595c4ff156 | 64 | * Notes: |
offroad | 1:7b595c4ff156 | 65 | * - It is safe to repeatedly read the same frame. |
offroad | 1:7b595c4ff156 | 66 | * - It is safe to read frame n, while frame n-1 has been partly received (driver uses double buffers) |
offroad | 1:7b595c4ff156 | 67 | */ |
offroad | 1:7b595c4ff156 | 68 | spektRx_invalidateFrame(&p); |
offroad | 1:7b595c4ff156 | 69 | } |
offroad | 1:7b595c4ff156 | 70 | } |
offroad | 1:7b595c4ff156 | 71 | } |