-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathCPPM.cpp
417 lines (341 loc) · 10.8 KB
/
CPPM.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
#include <CPPM.h>
//#include <avr/io.h>
//#include <Arduino.h>
//------------------------------------------------------------------------------
void iservos_reset(bool errored);
//------------------------------------------------------------------------------
volatile uint16_t CPPM_T_W; // extended TCNT0 timer maintained by cycle function
volatile uint16_t CPPM_T_X; // extended TCNT0 timer maintained by cycle function
volatile uint16_t CPPM_T_T; // extended TCNT0 timer timeout
volatile bool CPPM_T_cycling; // GPIOR0.0 = CPPM_T_cycle() in action, could be stopped by CPPM_T_interrupt()
volatile bool CPPM_T_syncing; // GPIOR0.1 = CPPM sync falling edge detected
volatile bool CPPM_T_checking; // GPIOR0.2 = CPPM check timeout
//------------------------------------------------------------------------------
uint16_t CPPM_T_get()
{
uint16_t tcnt_x;
cli();
tcnt_x = CPPM_T_X;
sei();
return tcnt_x;
}
void CPPM_T_set(uint16_t tcnt_x)
{
cli();
CPPM_T_X = tcnt_x;
sei();
}
void CPPM_T_cycle()
{
CPPM_T_cycling = true;
cli();
CPPM_T_W = TCNT1;
sei();
cli();
if (CPPM_T_cycling) CPPM_T_X = CPPM_T_W;
sei();
CPPM_T_cycling = false;
}
uint16_t CPPM_T_interrupt()
{
uint16_t tcnt_x;
tcnt_x = ICR1;
CPPM_T_X = tcnt_x;
CPPM_T_W = tcnt_x;
CPPM_T_cycling = false;
return tcnt_x;
}
uint16_t CPPM_T_timeout()
{
uint16_t tcnt_x;
cli();
tcnt_x = CPPM_T_X - CPPM_T_T;
sei();
return tcnt_x;
}
void CPPM_T_check()
{
if (CPPM_T_checking && (uint8_t) (CPPM_T_timeout()>>8)==0) iservos_reset(true);
}
void CPPM_T_setup()
{
ICR1 = TCNT0; // init the Input Capture Register
OCR1A = TCNT0; // init the Output Compare Register
// Configure timer1: disable PWM, set prescaler /8 (0.5 usec ticks)
TCCR1A = (1<<COM1A0); // Toggle OC1A/OC1B on Compare Match.
TCCR1B = (1<<ICNC1) | (0<<ICES1) | (1<<CS11); // falling edge
TCCR1C = 0;
CPPM_T_W = CPPM_T_X = TCNT0; // init CPPM_T_X
}
//------------------------------------------------------------------------------
void iservos_reset(bool errored)
{
// disable "pin change" interrupt from CPPM input frame...
// cbi(PCMSK,PCINT4);
bitClear(TIMSK1, ICIE1); // disable interrupt
// cbi(GPIOR0,CPPM_T_syncing);
// cbi(GPIOR0,CPPM_T_checking);
CPPM_T_syncing = false;
CPPM_T_checking = false;
// PORTB = (PORTB & ~CPPM_PBMASK); // clear PWM channels
CPPM.state = 0;
if (errored) CPPM.errors++; else CPPM.errors = 0;
CPPM.iservo = 0;
CPPM.nservo = CPPM_MSERVO;
CPPM.jservo = 0;
// CPPM.kservo = 0;
// enable "pin change" interrupt from CPPM input frame...
// sbi(PCMSK,PCINT4);
// Enable Timer1 input capture interrupt...
TCCR1B = (1<<ICNC1) | (0<<ICES1) | (1<<CS11); // falling edge
bitSet(TIFR1, ICF1); // clr pending interrupt
bitSet(TIMSK1, ICIE1); // enable interrupt
}
void iservos_setup()
{
// Configure the input capture pin
pinMode(CPPM_ICP1, INPUT_PULLUP);
iservos_reset(false);
}
void oservos_setup()
{
// Configure the output compare pin
digitalWrite(CPPM_OC1A, HIGH);
pinMode(CPPM_OC1A, OUTPUT);
CPPM.oservo = 0;
for (int i=0; i<CPPM_MSERVO; i++) CPPM.oservos[i] = CPPM_T_round(R615X_PULSE_CENTER);
// start CPPM frame after 22ms...
OCR1A += CPPM_T_round(R615X_FRAME_LENGTH);
// Enable Timer1 output compare interrupt...
bitSet(TIFR1, OCF1A); // clr pending interrupt
bitSet(TIMSK1, OCIE1A); // enable interrupt
}
//------------------------------------------------------------------------------
//ISR(PCINT0_vect)
ISR(TIMER1_CAPT_vect)
{
uint16_t tcnt0 = CPPM_T_interrupt(); // get time from extended TCNT0 timer
// if (PB_tst(PB4)) // ? rising edge => end 300us synchro pulse
if (TCCR1B & (1<<ICES1)) // rising edge => end 300us synchro pulse ?
{
TCCR1B = (1<<ICNC1) | (0<<ICES1) | (1<<CS11); // next falling edge
// if (tbi(GPIOR0,CPPM_T_syncing)) // ? follow a start edge
if (CPPM_T_syncing) // ? follow a start edge
{
// cbi(GPIOR0,CPPM_T_syncing);
CPPM_T_syncing = false;
CPPM.sync2 = tcnt0 - CPPM.time0; // compute width of synch pulse
CPPM.time1 = tcnt0;
CPPM._sync2[CPPM.iservo] = CPPM.sync2; // store sync width of current PWM servo pulse
// if( CPPM.sync2 < CPPM_T_floor(FRSKY_PULSE_SYNC-10) ||
// CPPM.sync2 > CPPM_T_ceil(FRSKY_PULSE_SYNC+50) ) // check sync width... (FRSKY_PULSE_SYNC+20) is too short with R615X
if( CPPM.sync2 < CPPM_PULSE_SYNC_MIN_FLOOR ||
CPPM.sync2 > CPPM_PULSE_SYNC_MAX_CEIL )
{
iservos_reset(true);
}
else
{
if (CPPM.state==0) CPPM.state = 1; // 1st well formed sync pulse found.
if (CPPM.state==1) // ? get pulse until gap pulse found
{
// CPPM_T_T = tcnt0 + CPPM_T_ceil(R615X_FRAME_NOTSYNC-FRSKY_PULSE_SYNC); // could be a stange frame if wait so long !
CPPM_T_T = CPPM.time5 + CPPM_FRAME_NOTSYNC_CEIL; // could be a stange frame if wait so long !
}
else // : gap pulse found => start time of frame known.
if (CPPM.state==2) // ? get pulses and compute nservo.
{
// CPPM_T_T = CPPM.time5 + CPPM_T_ceil(R615X_FRAME_NOTSYNC); // 2% max oscillator error
CPPM_T_T = CPPM.time5 + CPPM_FRAME_NOTSYNC_CEIL; // set frame timeout
}
else // : check pulses and gap.
{
if (CPPM.iservo<CPPM.nservo)
// CPPM_T_T = CPPM.time0 + CPPM_T_ceil(R615X_PULSE_CENTER+R615X_PULSE_C150PC); // middle stick+150%
// CPPM_T_T = CPPM.time0 + CPPM_T_ceil(R615X_PULSE_CENTER+R615X_PULSE_C200PC); // middle stick+200%
CPPM_T_T = CPPM.time0 + CPPM_PULSE_CENTER_PLUS_C200PC_CEIL; // middle stick+200%
else
// CPPM_T_T = CPPM.time5 + CPPM_T_ceil(R615X_FRAME_NOTSYNC);
CPPM_T_T = CPPM.time5 + CPPM_FRAME_NOTSYNC_CEIL;
}
// sbi(GPIOR0,CPPM_T_checking);
CPPM_T_syncing = true;
}
}
}
else // : falling edge => start 300us sync pulse and start PWM servo pulse.
{
TCCR1B = (1<<ICNC1) | (1<<ICES1) | (1<<CS11); // next rising edge
// if (state==3) PORTB = (PORTB & ~CPPM_PBMASK) | (kservo & CPPM_PBMASK); // update PWM channels, stop current pulse, start next pulse
CPPM._received = false;
// sbi(GPIOR0,CPPM_T_syncing);
CPPM_T_syncing = true;
// CPPM_T_T = tcnt0 + CPPM_T_ceil(FRSKY_PULSE_SYNC+50);
CPPM_T_T = tcnt0 + CPPM_PULSE_SYNC_MAX_CEIL;
// sbi(GPIOR0,CPPM_T_checking);
CPPM_T_checking = true;
if (CPPM.state==0)
{
CPPM.time5 = CPPM.time0 = tcnt0; // set start time of next PWM servo pulse (and sync pulse)
}
else // : state>=1
{
CPPM.puls3 = tcnt0 - CPPM.time0; // compute width of elapsed pulse
CPPM.time0 = tcnt0; // set start time of next PWM servo pulse (and sync pulse)
CPPM._puls3[CPPM.iservo] = CPPM.puls3; // store width of servo pulse
int puls3i = ((signed) CPPM.puls3 - R615X_PULSE_CENTER) / 4; // middle centered servo pulse
// int puls3i = (signed) CPPM.puls3 / 4 - (CPPM.sync2 + CPPM.sync2/4); // middle centered servo pulse
if (puls3i > 127) puls3i = 127; else if (puls3i < -128) puls3i= -128;
CPPM._puls3i8[CPPM.iservo] = puls3i;
// if (CPPM.puls3 < CPPM_T_floor(R615X_PULSE_CENTER-R615X_PULSE_C150PC)) // too short servo pulse (middle stick-150%) ?
// if (CPPM.puls3 < CPPM_T_floor(R615X_PULSE_CENTER-R615X_PULSE_C200PC)) // too short servo pulse (middle stick-200%) ?
if (CPPM.puls3 < CPPM_PULSE_CENTER_MINUS_C200PC_FLOOR) // too short servo pulse (middle stick-200%) ?
{
iservos_reset(true);
}
else
// if (CPPM.puls3 > CPPM_T_ceil(R615X_PULSE_CENTER+R615X_PULSE_C150PC)) // is a gap pulse (middle stick+150%) ?
// if (CPPM.puls3 > CPPM_T_ceil(R615X_PULSE_CENTER+R615X_PULSE_C200PC)) // is a gap pulse (middle stick+200%) ?
if (CPPM.puls3 > CPPM_PULSE_CENTER_PLUS_C200PC_CEIL) // is a gap pulse (middle stick+200%) ?
{
CPPM.cppm4 = tcnt0 - CPPM.time5; // compute length of elapsed CPPM frame
CPPM.time5 = tcnt0; // set start time of next CPPM frame
CPPM._puls3[CPPM.iservo+1] = CPPM.cppm4; // store CPPM frame length
CPPM._sync2[CPPM.iservo+1] = 0;
// if( (CPPM.state==3 && CPPM.cppm4 < CPPM_T_floor(R615X_FRAME_LENGTH)) || // frame length too short ?
if( (CPPM.state==3 && CPPM.cppm4 < CPPM_FRAME_LENGTH_FLOOR) || // frame length too short ?
// CPPM.cppm4 > CPPM_T_ceil(R615X_FRAME_NOTSYNC) ) // frame length too long ?
CPPM.cppm4 > CPPM_FRAME_NOTSYNC_CEIL ) // frame length too long ?
{
iservos_reset(true);
}
else
{
if (CPPM.state==2) CPPM.nservo = CPPM.iservo;
CPPM.iservo = 0; // set crnt servo (1st)
CPPM.jservo = 1; // set next servo (2nd)
// CPPM.kservo = CPPM.lservo[1]; // set mask of next servo
if (CPPM.state < 3) CPPM.state++;
}
}
else // valid servo pulse.
{
CPPM.iservo = CPPM.jservo; // set index of current servo pulse
if (CPPM.jservo > CPPM_MSERVO) // servos overflow ?
{
iservos_reset(true);
}
else
if (CPPM.jservo > CPPM.nservo) // servos overflow ?
{
iservos_reset(true);
}
else
if (CPPM.jservo == CPPM.nservo)
{
CPPM.jservo = 0;
// kservo = lservo[0]; // set next mask of 1st servo
CPPM._received = true;
}
else
{
CPPM.jservo++;
// kservo = lservo[jservo]; // set next mask of next servo
}
}
}
}
}
//------------------------------------------------------------------------------
ISR(TIMER1_COMPA_vect) // *2015-06-05,+2015-02-05
{
static uint16_t time5 = 0;
if (CPPM.oservo < CPPM_MSERVO) // PPM pulse ?
{
if ((PINB & _BV(PINB1))) // rising edge ?
{
OCR1A += CPPM.oservos[CPPM.oservo] - CPPM_T_round(R615X_PULSE_SYNC);
CPPM.oservo++; // next PPM pulse (or gap)
}
else // falling edge.
{
if (CPPM.oservo == 0) time5 = OCR1A;
OCR1A += CPPM_T_round(R615X_PULSE_SYNC);
// CPPM._sent = false;
}
}
else // gap pulse.
{
if ((PINB & _BV(PINB1))) // rising edge ?
{
OCR1A = time5 + CPPM_T_round( R615X_FRAME_LENGTH );
CPPM.oservo = 0; // next PPM pulse
}
else // falling edge.
{
OCR1A += CPPM_T_round(R615X_GAP_SYNC);
CPPM._sent = true;
}
}
}
//------------------------------------------------------------------------------
void CPPM_Class::begin()
{
CPPM_T_setup();
iservos_setup();
oservos_setup();
}
void CPPM_Class::end()
{
bitClear(TIMSK1, OCIE1A); // disable interrupt
bitClear(TIMSK1, ICIE1); // disable interrupt
}
void CPPM_Class::cycle()
{
CPPM_T_cycle();
CPPM_T_check();
}
bool CPPM_Class::synchronized()
{
return state == 3;
}
bool CPPM_Class::received(void) // +2015-02-05
{
bool received = _received;
_received = false;
return received;
}
bool CPPM_Class::sent(void) // +2015-06-23
{
bool sent = _sent;
_sent = false;
return sent;
}
int CPPM_Class::read(int n)
{
uint16_t *servo2_p = &_puls3[n];
cli();
uint16_t servo2 = *servo2_p;
sei();
return (int) servo2;
}
void CPPM_Class::write(int n, int v) // +2015-04-01
{
uint16_t *oservo_p = &oservos[n];
cli();
*oservo_p = v;
sei();
}
int CPPM_Class::read_us(int n)
{
return (CPPM_T_div*(long)read(n)+(CPPM_T_mul-1)/2)/CPPM_T_mul; //round
}
void CPPM_Class::write_us(int n, int v) // +2015-04-01
{
write(n, CPPM_T_round(v));
}
CPPM_Class::operator bool()
{
return true;
}
CPPM_Class CPPM;