Details | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
1180 | killagreg | 1 | /*####################################################################################### |
2 | Decodieren eines RC Summen Signals oder Spektrum Empfänger-Satellit |
||
3 | #######################################################################################*/ |
||
4 | |||
1201 | killagreg | 5 | #include "spectrum.h" |
6 | #include rc.h |
||
1180 | killagreg | 7 | |
1201 | killagreg | 8 | |
1180 | killagreg | 9 | // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
10 | // + Copyright (c) Rainer Walther |
||
11 | // + RC-routines from original MK rc.c (c) H&I |
||
12 | // + Useful infos from Walter: http://www.rcgroups.com/forums/showthread.php?t=714299&page=2 |
||
13 | // + only for non-profit use |
||
14 | // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
||
15 | // |
||
16 | // 20080808 rw Modified for Spektrum AR6100 (PPM) |
||
17 | // 20080823 rw Add Spektrum satellite receiver on USART1 (644P only) |
||
18 | // 20081213 rw Add support for Spektrum DS9 Air-Tx-Module (9 channels) |
||
19 | // Replace AR6100-coding with original composit-signal routines |
||
20 | // |
||
21 | // --- |
||
22 | // Entweder Summensignal ODER Spektrum-Receiver anschließen. Nicht beides gleichzeitig betreiben! |
||
23 | // Binding is not implemented. Bind with external Receiver. |
||
24 | // Servo output J3, J4, J5 not serviced |
||
25 | // |
||
26 | // Anschuß Spektrum Receiver |
||
27 | // Orange: 3V von der FC (keinesfalls an 5V anschließen!) |
||
28 | // Schwarz: GND |
||
29 | // Grau: RXD1 (Pin 3) auf 10-Pol FC-Stecker |
||
30 | // |
||
31 | // --- |
||
32 | // Satellite-Reciever connected on USART1: |
||
33 | // |
||
34 | // DX7/DX6i: One data-frame at 115200 baud every 22ms. |
||
35 | // DX7se: One data-frame at 115200 baud every 11ms. |
||
36 | // byte1: unknown |
||
37 | // byte2: unknown |
||
38 | // byte3: and byte4: channel data (FLT-Mode) |
||
39 | // byte5: and byte6: channel data (Roll) |
||
40 | // byte7: and byte8: channel data (Nick) |
||
41 | // byte9: and byte10: channel data (Gier) |
||
42 | // byte11: and byte12: channel data (Gear Switch) |
||
43 | // byte13: and byte14: channel data (Gas) |
||
44 | // byte15: and byte16: channel data (AUX2) |
||
45 | // |
||
46 | // DS9 (9 Channel): One data-frame at 115200 baud every 11ms, alternating frame 1/2 for CH1-7 / CH8-9 |
||
47 | // 1st Frame: |
||
48 | // byte1: unknown |
||
49 | // byte2: unknown |
||
50 | // byte3: and byte4: channel data |
||
51 | // byte5: and byte6: channel data |
||
52 | // byte7: and byte8: channel data |
||
53 | // byte9: and byte10: channel data |
||
54 | // byte11: and byte12: channel data |
||
55 | // byte13: and byte14: channel data |
||
1201 | killagreg | 56 | // byte15: and byte16: channel data |
1180 | killagreg | 57 | // 2nd Frame: |
58 | // byte1: unknown |
||
59 | // byte2: unknown |
||
60 | // byte3: and byte4: channel data |
||
61 | // byte5: and byte6: channel data |
||
62 | // byte7: and byte8: 0xffff |
||
63 | // byte9: and byte10: 0xffff |
||
64 | // byte11: and byte12: 0xffff |
||
65 | // byte13: and byte14: 0xffff |
||
66 | // byte15: and byte16: 0xffff |
||
67 | // |
||
68 | // Each channel data (16 bit= 2byte, first msb, second lsb) is arranged as: |
||
69 | // |
||
70 | // Bits: F 0 C3 C2 C1 C0 D9 D8 D7 D6 D5 D4 D3 D2 D1 D0 |
||
71 | // |
||
72 | // 0 means a '0' bit |
||
73 | // F: 1 = indicates beginning of 2nd frame for CH8-9 (DS9 only) |
||
74 | // C3 to C0 is the channel number. 0 to 9 (4 bit, as assigned in the transmitter) |
||
75 | // D9 to D0 is the channel data (10 bit) 0xaa..0x200..0x356 for 100% transmitter-travel |
||
76 | // |
||
77 | // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
||
78 | |||
1201 | killagreg | 79 | |
80 | void spectrum_parser(uint8_t c) |
||
81 | |||
1180 | killagreg | 82 | { |
1201 | killagreg | 83 | static unsigned int Sync=0, FrameCnt=0, ByteHigh=0, ReSync=1, Frame2=0, FrameTimer; |
1180 | killagreg | 84 | unsigned int Channel, index; |
85 | signed int signal, tmp; |
||
86 | int bCheckDelay; |
||
1201 | killagreg | 87 | |
1180 | killagreg | 88 | if (ReSync == 1) |
89 | { |
||
90 | // wait for beginning of new frame |
||
91 | ReSync = 0; |
||
1201 | killagreg | 92 | |
1180 | killagreg | 93 | FrameTimer = SetDelay(7); // minimum 7ms zwischen den frames |
94 | FrameCnt = 0; |
||
95 | Sync = 0; |
||
96 | ByteHigh = 0; |
||
97 | } |
||
98 | else |
||
1201 | killagreg | 99 | { |
1180 | killagreg | 100 | bCheckDelay = CheckDelay(FrameTimer); |
101 | if ( Sync == 0 ) |
||
102 | { |
||
103 | if(bCheckDelay) |
||
104 | { |
||
105 | // nach einer Pause von mind. 7ms erstes Sync-Character gefunden |
||
106 | // Zeichen ignorieren, da Bedeutung unbekannt |
||
107 | Sync = 1; |
||
108 | FrameCnt ++; |
||
109 | } |
||
110 | else |
||
111 | { |
||
112 | // Zeichen kam vor Ablauf der 7ms Sync-Pause |
||
113 | // warten auf erstes Sync-Zeichen |
||
114 | } |
||
115 | } |
||
116 | else if((Sync == 1) && !bCheckDelay) |
||
117 | { |
||
118 | // zweites Sync-Character ignorieren, Bedeutung unbekannt |
||
119 | Sync = 2; |
||
120 | FrameCnt ++; |
||
121 | } |
||
122 | else if((Sync == 2) && !bCheckDelay) |
||
123 | { |
||
124 | // Datenbyte high |
||
125 | ByteHigh = c; |
||
1201 | killagreg | 126 | |
1180 | killagreg | 127 | if (FrameCnt == 2) |
128 | { |
||
129 | // is 1st Byte of Channel-data |
||
130 | // Frame 1 with Channel 1-7 comming next |
||
131 | Frame2 = 0; |
||
132 | if(ByteHigh & 0x80) |
||
133 | { |
||
134 | // DS9: Frame 2 with Channel 8-9 comming next |
||
135 | Frame2 = 1; |
||
136 | } |
||
1201 | killagreg | 137 | } |
1180 | killagreg | 138 | Sync = 3; |
139 | FrameCnt ++; |
||
140 | } |
||
141 | else if((Sync == 3) && !bCheckDelay) |
||
142 | { |
||
143 | // Datenbyte low |
||
1201 | killagreg | 144 | |
1180 | killagreg | 145 | // High-Byte for next channel comes next |
146 | Sync = 2; |
||
147 | FrameCnt ++; |
||
1201 | killagreg | 148 | |
1180 | killagreg | 149 | index = (ByteHigh >> 2) & 0x0f; |
150 | index ++; |
||
151 | Channel = (ByteHigh << 8) | c; |
||
152 | signal = Channel & 0x3ff; |
||
153 | signal -= 0x200; // Offset, range 0x000..0x3ff? |
||
154 | signal = signal/3; // scaling to fit PPM resolution |
||
1201 | killagreg | 155 | |
1180 | killagreg | 156 | if(index >= 0 && index <= 10) |
157 | { |
||
158 | // Stabiles Signal |
||
159 | if(abs(signal - PPM_in[index]) < 6) { if(SenderOkay < 200) SenderOkay += 10; else SenderOkay = 200;} |
||
1201 | killagreg | 160 | tmp = (3 * (PPM_in[index]) + signal) / 4; |
1180 | killagreg | 161 | if(tmp > signal+1) tmp--; else |
1201 | killagreg | 162 | if(tmp < signal-1) tmp++; |
163 | if(SenderOkay >= 180) PPM_diff[index] = ((tmp - PPM_in[index]) / 3) * 3; |
||
1180 | killagreg | 164 | else PPM_diff[index] = 0; |
165 | PPM_in[index] = tmp; |
||
166 | } |
||
167 | } |
||
168 | else |
||
169 | { |
||
170 | // hier stimmt was nicht: neu synchronisieren |
||
171 | ReSync = 1; |
||
172 | FrameCnt = 0; |
||
173 | Frame2 = 0; |
||
174 | } |
||
1201 | killagreg | 175 | |
1180 | killagreg | 176 | // 16 Bytes per frame |
177 | if(FrameCnt >= 16) |
||
178 | { |
||
179 | // Frame complete |
||
180 | if(Frame2 == 0) |
||
181 | { |
||
182 | // Null bedeutet: Neue Daten |
||
183 | // nur beim ersten Frame (CH 0-7) setzen |
||
1201 | killagreg | 184 | NewPpmData = 0; |
1180 | killagreg | 185 | } |
1201 | killagreg | 186 | |
1180 | killagreg | 187 | // new frame next, nach fruehestens 7ms erwartet |
188 | FrameCnt = 0; |
||
189 | Frame2 = 0; |
||
190 | Sync = 0; |
||
191 | } |
||
192 | // Zeit bis zum nächsten Zeichen messen |
||
193 | FrameTimer = SetDelay(7); |
||
194 | } |
||
1201 | killagreg | 195 | } |
1180 | killagreg | 196 | |
197 |