![](/media/cache/img/default_profile.jpg.50x50_q85.jpg)
rud2
Spektrum.h@2:f5c2fa19497b, 2018-11-28 (annotated)
- Committer:
- LukeMar
- Date:
- Wed Nov 28 18:48:00 2018 +0000
- Revision:
- 2:f5c2fa19497b
- Parent:
- 0:8ed926c84e2f
rudder 2
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
LukeMar | 0:8ed926c84e2f | 1 | /* |
LukeMar | 0:8ed926c84e2f | 2 | Spektrum.h |
LukeMar | 0:8ed926c84e2f | 3 | Spektrum serial receiver mbed library |
LukeMar | 0:8ed926c84e2f | 4 | Dennis Evangelista, 2018 |
LukeMar | 0:8ed926c84e2f | 5 | */ |
LukeMar | 0:8ed926c84e2f | 6 | |
LukeMar | 0:8ed926c84e2f | 7 | #ifndef SPEKTRUM_H |
LukeMar | 0:8ed926c84e2f | 8 | #define SPEKTRUM_H |
LukeMar | 0:8ed926c84e2f | 9 | #define SPEKTRUM_VERSION "1.0.1" |
LukeMar | 0:8ed926c84e2f | 10 | |
LukeMar | 0:8ed926c84e2f | 11 | #include "mbed.h" |
LukeMar | 0:8ed926c84e2f | 12 | #include "rtos.h" |
LukeMar | 0:8ed926c84e2f | 13 | |
LukeMar | 0:8ed926c84e2f | 14 | // bind modes |
LukeMar | 0:8ed926c84e2f | 15 | #define SPEKTRUM_INT_DSMX_22MS 7 |
LukeMar | 0:8ed926c84e2f | 16 | #define SPEKTRUM_EXT_DSMX_22MS 8 |
LukeMar | 0:8ed926c84e2f | 17 | #define SPEKTRUM_INT_DSMX_11MS 9 |
LukeMar | 0:8ed926c84e2f | 18 | #define SPEKTRUM_EXT_DSMX_11MS 10 |
LukeMar | 0:8ed926c84e2f | 19 | // DSM2 bind modes not recommended, not implemented |
LukeMar | 0:8ed926c84e2f | 20 | // EXT(ernal) bind modes don't really work on solitary satellite receiver |
LukeMar | 0:8ed926c84e2f | 21 | |
LukeMar | 0:8ed926c84e2f | 22 | // field definitions |
LukeMar | 0:8ed926c84e2f | 23 | // #define SPEKTRUM_MASK_1024_CHANID 0xfc00 |
LukeMar | 0:8ed926c84e2f | 24 | // #define SPEKTRUM_MASK_1024_SXPOS 0x03ff |
LukeMar | 0:8ed926c84e2f | 25 | // first two only used in DSM2 which are not implemented |
LukeMar | 0:8ed926c84e2f | 26 | #define SPEKTRUM_MASK_2048_CHANID 0x7800 |
LukeMar | 0:8ed926c84e2f | 27 | #define SPEKTRUM_MASK_2048_CHANID_MSB 0x78 |
LukeMar | 0:8ed926c84e2f | 28 | #define SPEKTRUM_MASK_2048_SXPOS 0x07ff |
LukeMar | 0:8ed926c84e2f | 29 | |
LukeMar | 0:8ed926c84e2f | 30 | // allowable system field values |
LukeMar | 0:8ed926c84e2f | 31 | // #define SPEKTRUM_22MS_1024_DSM2 0x01 |
LukeMar | 0:8ed926c84e2f | 32 | // #define SPEKTRUM_11MS_2048_DSM2 0x12 |
LukeMar | 0:8ed926c84e2f | 33 | #define SPEKTRUM_22MS_2048_DSMX 0xa2 |
LukeMar | 0:8ed926c84e2f | 34 | #define SPEKTRUM_11MS_2048_DSMX 0xb2 |
LukeMar | 0:8ed926c84e2f | 35 | |
LukeMar | 0:8ed926c84e2f | 36 | #define SPEKTRUM_BAUD 115200 |
LukeMar | 0:8ed926c84e2f | 37 | // Spektrum baud is supposed to be 125000, but the LPC1768 seems not |
LukeMar | 0:8ed926c84e2f | 38 | // to support nonstandard baud rates. |
LukeMar | 0:8ed926c84e2f | 39 | |
LukeMar | 0:8ed926c84e2f | 40 | #define SPEKTRUM_SERVOS 7 |
LukeMar | 0:8ed926c84e2f | 41 | #define SPEKTRUM_PACKET_SIZE 16 |
LukeMar | 0:8ed926c84e2f | 42 | #define SPEKTRUM_CHANNELS 16 |
LukeMar | 0:8ed926c84e2f | 43 | #define SPEKTRUM_COUNT2US(x) (x*600/1024+900) |
LukeMar | 0:8ed926c84e2f | 44 | |
LukeMar | 0:8ed926c84e2f | 45 | |
LukeMar | 0:8ed926c84e2f | 46 | |
LukeMar | 0:8ed926c84e2f | 47 | |
LukeMar | 0:8ed926c84e2f | 48 | /** Spektrum receiver object for connecting to eg SPM9745 receiver |
LukeMar | 0:8ed926c84e2f | 49 | */ |
LukeMar | 0:8ed926c84e2f | 50 | class Spektrum |
LukeMar | 0:8ed926c84e2f | 51 | { |
LukeMar | 0:8ed926c84e2f | 52 | public: |
LukeMar | 0:8ed926c84e2f | 53 | /** Number of fades (failed packets) from receiver */ |
LukeMar | 0:8ed926c84e2f | 54 | unsigned int fades; |
LukeMar | 0:8ed926c84e2f | 55 | |
LukeMar | 0:8ed926c84e2f | 56 | /** Tells if system is in DSMX 11ms or 22ms for example */ |
LukeMar | 0:8ed926c84e2f | 57 | unsigned int system; |
LukeMar | 0:8ed926c84e2f | 58 | |
LukeMar | 0:8ed926c84e2f | 59 | /** Contains 0-2048 values for all channels 0-15 */ |
LukeMar | 0:8ed926c84e2f | 60 | unsigned int channel[SPEKTRUM_CHANNELS]; |
LukeMar | 0:8ed926c84e2f | 61 | |
LukeMar | 0:8ed926c84e2f | 62 | /** Contains approx 900-2100us pulsewidths corresponding to chan 0-15 */ |
LukeMar | 0:8ed926c84e2f | 63 | unsigned int pulsewidth[SPEKTRUM_CHANNELS]; |
LukeMar | 0:8ed926c84e2f | 64 | |
LukeMar | 0:8ed926c84e2f | 65 | /** If true, data is value */ |
LukeMar | 0:8ed926c84e2f | 66 | bool valid; // TODO switch to EventFlags? |
LukeMar | 0:8ed926c84e2f | 67 | |
LukeMar | 0:8ed926c84e2f | 68 | /** 11 or 22 ms */ |
LukeMar | 0:8ed926c84e2f | 69 | unsigned int period_ms; |
LukeMar | 0:8ed926c84e2f | 70 | |
LukeMar | 0:8ed926c84e2f | 71 | Spektrum(PinName tx, PinName rx); // constructor |
LukeMar | 0:8ed926c84e2f | 72 | ~Spektrum(); // destructor |
LukeMar | 0:8ed926c84e2f | 73 | |
LukeMar | 0:8ed926c84e2f | 74 | private: |
LukeMar | 0:8ed926c84e2f | 75 | UARTSerial _rx; |
LukeMar | 0:8ed926c84e2f | 76 | unsigned char _buf[SPEKTRUM_PACKET_SIZE]; |
LukeMar | 0:8ed926c84e2f | 77 | Thread _packet_thread; |
LukeMar | 0:8ed926c84e2f | 78 | void _packet_callback(void); |
LukeMar | 0:8ed926c84e2f | 79 | }; |
LukeMar | 0:8ed926c84e2f | 80 | |
LukeMar | 0:8ed926c84e2f | 81 | |
LukeMar | 0:8ed926c84e2f | 82 | |
LukeMar | 0:8ed926c84e2f | 83 | |
LukeMar | 0:8ed926c84e2f | 84 | |
LukeMar | 0:8ed926c84e2f | 85 | |
LukeMar | 0:8ed926c84e2f | 86 | |
LukeMar | 0:8ed926c84e2f | 87 | /** For binding a Spektrum receiver to transmitter */ |
LukeMar | 0:8ed926c84e2f | 88 | class BindPlug |
LukeMar | 0:8ed926c84e2f | 89 | { |
LukeMar | 0:8ed926c84e2f | 90 | public: |
LukeMar | 0:8ed926c84e2f | 91 | /** e.g. SPEKTRUM_INT_DSMX_11MS, SPEKTRUM_INT_DSMX_22MS */ |
LukeMar | 0:8ed926c84e2f | 92 | int mode; |
LukeMar | 0:8ed926c84e2f | 93 | |
LukeMar | 0:8ed926c84e2f | 94 | BindPlug(PinName tx, PinName rx, int mode = SPEKTRUM_INT_DSMX_11MS); |
LukeMar | 0:8ed926c84e2f | 95 | ~BindPlug(); |
LukeMar | 0:8ed926c84e2f | 96 | void bind(); |
LukeMar | 0:8ed926c84e2f | 97 | |
LukeMar | 0:8ed926c84e2f | 98 | private: |
LukeMar | 0:8ed926c84e2f | 99 | DigitalOut _3Vpin; |
LukeMar | 0:8ed926c84e2f | 100 | DigitalOut _datapin; |
LukeMar | 0:8ed926c84e2f | 101 | }; |
LukeMar | 0:8ed926c84e2f | 102 | |
LukeMar | 0:8ed926c84e2f | 103 | /* LATER |
LukeMar | 0:8ed926c84e2f | 104 | class SpektrumTestDevice{ |
LukeMar | 0:8ed926c84e2f | 105 | public: |
LukeMar | 0:8ed926c84e2f | 106 | unsigned int fades; |
LukeMar | 0:8ed926c84e2f | 107 | unsigned int servo[7]; |
LukeMar | 0:8ed926c84e2f | 108 | SpektrumTestDevice(PinName tx, PinName rx); |
LukeMar | 0:8ed926c84e2f | 109 | ~SpektrumTestDevice(); |
LukeMar | 0:8ed926c84e2f | 110 | private: |
LukeMar | 0:8ed926c84e2f | 111 | Serial _receiver; |
LukeMar | 0:8ed926c84e2f | 112 | }; |
LukeMar | 0:8ed926c84e2f | 113 | */ |
LukeMar | 0:8ed926c84e2f | 114 | |
LukeMar | 0:8ed926c84e2f | 115 | #endif |