Go to most recent revision | Details | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
1 | ingob | 1 | #include "main.h" |
2 | |||
1424 | ingob | 3 | |
1 | ingob | 4 | volatile unsigned int CountMilliseconds = 0; |
5 | volatile static unsigned int tim_main; |
||
6 | volatile unsigned char UpdateMotor = 0; |
||
7 | volatile unsigned int cntKompass = 0; |
||
8 | volatile unsigned int beeptime = 0; |
||
1171 | hbuss | 9 | volatile unsigned char SendSPI = 0, ServoActive = 0; |
723 | hbuss | 10 | |
173 | holgerb | 11 | unsigned int BeepMuster = 0xffff; |
1 | ingob | 12 | |
1156 | hbuss | 13 | volatile int16_t ServoNickValue = 0; |
14 | volatile int16_t ServoRollValue = 0; |
||
15 | |||
16 | |||
1 | ingob | 17 | enum { |
18 | STOP = 0, |
||
19 | CK = 1, |
||
20 | CK8 = 2, |
||
21 | CK64 = 3, |
||
22 | CK256 = 4, |
||
23 | CK1024 = 5, |
||
24 | T0_FALLING_EDGE = 6, |
||
25 | T0_RISING_EDGE = 7 |
||
26 | }; |
||
27 | |||
28 | |||
1322 | hbuss | 29 | SIGNAL (SIG_OVERFLOW0) // 9,7kHz |
1 | ingob | 30 | { |
31 | static unsigned char cnt_1ms = 1,cnt = 0; |
||
173 | holgerb | 32 | unsigned char pieper_ein = 0; |
723 | hbuss | 33 | if(SendSPI) SendSPI--; |
1469 | killagreg | 34 | if(SpektrumTimer) SpektrumTimer--; |
1320 | hbuss | 35 | |
1 | ingob | 36 | if(!cnt--) |
37 | { |
||
1105 | killagreg | 38 | cnt = 9; |
1 | ingob | 39 | cnt_1ms++; |
40 | cnt_1ms %= 2; |
||
41 | if(!cnt_1ms) UpdateMotor = 1; |
||
42 | CountMilliseconds++; |
||
1105 | killagreg | 43 | } |
1 | ingob | 44 | |
1232 | hbuss | 45 | if(beeptime >= 1) |
1 | ingob | 46 | { |
1105 | killagreg | 47 | beeptime--; |
48 | if(beeptime & BeepMuster) |
||
173 | holgerb | 49 | { |
50 | pieper_ein = 1; |
||
51 | } |
||
52 | else pieper_ein = 0; |
||
1 | ingob | 53 | } |
1105 | killagreg | 54 | else |
173 | holgerb | 55 | { |
56 | pieper_ein = 0; |
||
57 | BeepMuster = 0xffff; |
||
1105 | killagreg | 58 | } |
173 | holgerb | 59 | |
60 | if(pieper_ein) |
||
61 | { |
||
62 | if(PlatinenVersion == 10) PORTD |= (1<<2); // Speaker an PORTD.2 |
||
63 | else PORTC |= (1<<7); // Speaker an PORTC.7 |
||
64 | } |
||
1105 | killagreg | 65 | else |
173 | holgerb | 66 | { |
67 | if(PlatinenVersion == 10) PORTD &= ~(1<<2); |
||
68 | else PORTC &= ~(1<<7); |
||
1105 | killagreg | 69 | } |
70 | |||
1481 | holgerb | 71 | if(!NaviDataOkay && EE_Parameter.GlobalConfig & CFG_KOMPASS_AKTIV) |
1 | ingob | 72 | { |
73 | if(PINC & 0x10) |
||
74 | { |
||
1105 | killagreg | 75 | cntKompass++; |
1 | ingob | 76 | } |
77 | else |
||
78 | { |
||
1105 | killagreg | 79 | if((cntKompass) && (cntKompass < 362)) |
80 | { |
||
693 | hbuss | 81 | cntKompass += cntKompass / 41; |
82 | if(cntKompass > 10) KompassValue = cntKompass - 10; else KompassValue = 0; |
||
1469 | killagreg | 83 | KompassRichtung = ((540 + KompassValue - KompassStartwert) % 360) - 180; |
1105 | killagreg | 84 | } |
1 | ingob | 85 | cntKompass = 0; |
1105 | killagreg | 86 | } |
1 | ingob | 87 | } |
88 | } |
||
89 | |||
90 | |||
91 | // ----------------------------------------------------------------------- |
||
92 | unsigned int SetDelay (unsigned int t) |
||
93 | { |
||
94 | // TIMSK0 &= ~_BV(TOIE0); |
||
1105 | killagreg | 95 | return(CountMilliseconds + t + 1); |
1 | ingob | 96 | // TIMSK0 |= _BV(TOIE0); |
97 | } |
||
98 | |||
99 | // ----------------------------------------------------------------------- |
||
100 | char CheckDelay(unsigned int t) |
||
101 | { |
||
102 | // TIMSK0 &= ~_BV(TOIE0); |
||
103 | return(((t - CountMilliseconds) & 0x8000) >> 9); |
||
104 | // TIMSK0 |= _BV(TOIE0); |
||
105 | } |
||
106 | |||
107 | // ----------------------------------------------------------------------- |
||
108 | void Delay_ms(unsigned int w) |
||
109 | { |
||
110 | unsigned int akt; |
||
111 | akt = SetDelay(w); |
||
112 | while (!CheckDelay(akt)); |
||
113 | } |
||
114 | |||
395 | hbuss | 115 | void Delay_ms_Mess(unsigned int w) |
116 | { |
||
117 | unsigned int akt; |
||
118 | akt = SetDelay(w); |
||
1166 | hbuss | 119 | while (!CheckDelay(akt)) if(AdReady) {AdReady = 0; ANALOG_ON;} |
395 | hbuss | 120 | } |
121 | |||
1156 | hbuss | 122 | /*****************************************************/ |
123 | /* Initialize Timer 2 */ |
||
124 | /*****************************************************/ |
||
125 | // The timer 2 is used to generate the PWM at PD7 (J7) |
||
126 | // to control a camera servo for nick compensation. |
||
127 | void TIMER2_Init(void) |
||
910 | hbuss | 128 | { |
1156 | hbuss | 129 | uint8_t sreg = SREG; |
130 | |||
131 | // disable all interrupts before reconfiguration |
||
132 | cli(); |
||
1469 | killagreg | 133 | |
1156 | hbuss | 134 | PORTD &= ~(1<<PORTD7); // set PD7 to low |
135 | |||
136 | DDRC |= (1<<DDC6); // set PC6 as output (Reset for HEF4017) |
||
1171 | hbuss | 137 | HEF4017R_ON; |
1156 | hbuss | 138 | // Timer/Counter 2 Control Register A |
139 | |||
140 | // Timer Mode is FastPWM with timer reload at OCR2A (Bits: WGM22 = 1, WGM21 = 1, WGM20 = 1) |
||
141 | // PD7: Normal port operation, OC2A disconnected, (Bits: COM2A1 = 0, COM2A0 = 0) |
||
142 | // PD6: Normal port operation, OC2B disconnected, (Bits: COM2B1 = 0, COM2B0 = 0) |
||
143 | TCCR2A &= ~((1<<COM2A1)|(1<<COM2A0)|(1<<COM2B1)|(1<<COM2B0)); |
||
144 | TCCR2A |= (1<<WGM21)|(1<<WGM20); |
||
145 | |||
146 | // Timer/Counter 2 Control Register B |
||
147 | |||
148 | // Set clock divider for timer 2 to SYSKLOCK/32 = 20MHz / 32 = 625 kHz |
||
149 | // The timer increments from 0x00 to 0xFF with an update rate of 625 kHz or 1.6 us |
||
150 | // hence the timer overflow interrupt frequency is 625 kHz / 256 = 2.44 kHz or 0.4096 ms |
||
151 | |||
152 | // divider 32 (Bits: CS022 = 0, CS21 = 1, CS20 = 1) |
||
153 | TCCR2B &= ~((1<<FOC2A)|(1<<FOC2B)|(1<<CS22)); |
||
154 | TCCR2B |= (1<<CS21)|(1<<CS20)|(1<<WGM22); |
||
155 | |||
156 | // Initialize the Timer/Counter 2 Register |
||
157 | TCNT2 = 0; |
||
158 | |||
159 | // Initialize the Output Compare Register A used for PWM generation on port PD7. |
||
160 | OCR2A = 255; |
||
161 | TCCR2A |= (1<<COM2A1); // set or clear at compare match depends on value of COM2A0 |
||
162 | |||
163 | // Timer/Counter 2 Interrupt Mask Register |
||
164 | // Enable timer output compare match A Interrupt only |
||
165 | TIMSK2 &= ~((1<<OCIE2B)|(1<<TOIE2)); |
||
166 | TIMSK2 |= (1<<OCIE2A); |
||
167 | |||
168 | SREG = sreg; |
||
910 | hbuss | 169 | } |
170 | |||
1156 | hbuss | 171 | //---------------------------- |
172 | void Timer_Init(void) |
||
1 | ingob | 173 | { |
1156 | hbuss | 174 | tim_main = SetDelay(10); |
175 | TCCR0B = CK8; |
||
176 | TCCR0A = (1<<COM0A1)|(1<<COM0B1)|3;//fast PWM |
||
177 | OCR0A = 0; |
||
178 | OCR0B = 120; |
||
179 | TCNT0 = (unsigned char)-TIMER_RELOAD_VALUE; // reload |
||
180 | //OCR1 = 0x00; |
||
1105 | killagreg | 181 | |
1156 | hbuss | 182 | TIMSK0 |= _BV(TOIE0); |
183 | } |
||
184 | |||
185 | |||
186 | /*****************************************************/ |
||
187 | /* Control Servo Position */ |
||
188 | /*****************************************************/ |
||
189 | |||
190 | ISR(TIMER2_COMPA_vect) |
||
191 | { |
||
192 | // frame len 22.5 ms = 14063 * 1.6 us |
||
193 | // stop pulse: 0.3 ms = 188 * 1.6 us |
||
194 | // min servo pulse: 0.6 ms = 375 * 1.6 us |
||
195 | // max servo pulse: 2.4 ms = 1500 * 1.6 us |
||
196 | // resolution: 1500 - 375 = 1125 steps |
||
197 | |||
198 | #define IRS_RUNTIME 127 |
||
199 | #define PPM_STOPPULSE 188 |
||
200 | // #define PPM_FRAMELEN (14063 |
||
1171 | hbuss | 201 | #define PPM_FRAMELEN (1757 * EE_Parameter.ServoNickRefresh) |
1156 | hbuss | 202 | #define MINSERVOPULSE 375 |
203 | #define MAXSERVOPULSE 1500 |
||
204 | #define SERVORANGE (MAXSERVOPULSE - MINSERVOPULSE) |
||
205 | |||
206 | static uint8_t PulseOutput = 0; |
||
207 | static uint16_t RemainingPulse = 0; |
||
208 | static uint16_t ServoFrameTime = 0; |
||
209 | static uint8_t ServoIndex = 0; |
||
210 | |||
211 | #define MULTIPLYER 4 |
||
212 | static int16_t ServoNickOffset = (255 / 2) * MULTIPLYER; // initial value near center positon |
||
1224 | hbuss | 213 | static int16_t ServoRollOffset = (255 / 2) * MULTIPLYER; // initial value near center positon |
1156 | hbuss | 214 | |
215 | if(PlatinenVersion < 20) |
||
216 | { |
||
217 | //--------------------------- |
||
218 | // Nick servo state machine |
||
219 | //--------------------------- |
||
220 | if(!PulseOutput) // pulse output complete |
||
221 | { |
||
222 | if(TCCR2A & (1<<COM2A0)) // we had a low pulse |
||
223 | { |
||
224 | TCCR2A &= ~(1<<COM2A0);// make a high pulse |
||
225 | RemainingPulse = MINSERVOPULSE + SERVORANGE/2; // center position ~ 1.5ms |
||
226 | |||
227 | ServoNickOffset = (ServoNickOffset * 3 + (int16_t)Parameter_ServoNickControl * MULTIPLYER) / 4; // lowpass offset |
||
228 | ServoNickValue = ServoNickOffset; // offset (Range from 0 to 255 * 3 = 765) |
||
1232 | hbuss | 229 | if(EE_Parameter.ServoCompInvert & 0x01) |
1156 | hbuss | 230 | { // inverting movement of servo |
231 | ServoNickValue += (int16_t)( ( (int32_t)EE_Parameter.ServoNickComp * MULTIPLYER * (IntegralNick / 128L ) ) / (256L) ); |
||
232 | } |
||
233 | else |
||
234 | { // non inverting movement of servo |
||
235 | ServoNickValue -= (int16_t)( ( (int32_t)EE_Parameter.ServoNickComp * MULTIPLYER * (IntegralNick / 128L ) ) / (256L) ); |
||
236 | } |
||
237 | // limit servo value to its parameter range definition |
||
238 | if(ServoNickValue < ((int16_t)EE_Parameter.ServoNickMin * MULTIPLYER) ) |
||
239 | { |
||
240 | ServoNickValue = (int16_t)EE_Parameter.ServoNickMin * MULTIPLYER; |
||
241 | } |
||
242 | else |
||
243 | if(ServoNickValue > ((int16_t)EE_Parameter.ServoNickMax * MULTIPLYER) ) |
||
244 | { |
||
245 | ServoNickValue = (int16_t)EE_Parameter.ServoNickMax * MULTIPLYER; |
||
246 | } |
||
247 | |||
248 | RemainingPulse += ServoNickValue - (256 / 2) * MULTIPLYER; // shift ServoNickValue to center position |
||
249 | |||
250 | ServoNickValue /= MULTIPLYER; |
||
251 | |||
252 | // range servo pulse width |
||
253 | if(RemainingPulse > MAXSERVOPULSE ) RemainingPulse = MAXSERVOPULSE; // upper servo pulse limit |
||
254 | else if(RemainingPulse < MINSERVOPULSE ) RemainingPulse = MINSERVOPULSE; // lower servo pulse limit |
||
255 | // accumulate time for correct update rate |
||
256 | ServoFrameTime = RemainingPulse; |
||
257 | } |
||
258 | else // we had a high pulse |
||
259 | { |
||
260 | TCCR2A |= (1<<COM2A0); // make a low pulse |
||
261 | RemainingPulse = PPM_FRAMELEN - ServoFrameTime; |
||
262 | } |
||
263 | // set pulse output active |
||
264 | PulseOutput = 1; |
||
265 | } |
||
266 | } // EOF Nick servo state machine |
||
267 | else |
||
268 | { |
||
269 | //----------------------------------------------------- |
||
270 | // PPM state machine, onboard demultiplexed by HEF4017 |
||
271 | //----------------------------------------------------- |
||
272 | if(!PulseOutput) // pulse output complete |
||
273 | { |
||
274 | if(TCCR2A & (1<<COM2A0)) // we had a low pulse |
||
275 | { |
||
276 | TCCR2A &= ~(1<<COM2A0);// make a high pulse |
||
277 | |||
278 | if(ServoIndex == 0) // if we are at the sync gap |
||
279 | { |
||
280 | RemainingPulse = PPM_FRAMELEN - ServoFrameTime; // generate sync gap by filling time to full frame time |
||
281 | ServoFrameTime = 0; // reset servo frame time |
||
282 | HEF4017R_ON; // enable HEF4017 reset |
||
283 | } |
||
284 | else // servo channels |
||
285 | { |
||
286 | RemainingPulse = MINSERVOPULSE + SERVORANGE/2; // center position ~ 1.5ms |
||
287 | switch(ServoIndex) // map servo channels |
||
288 | { |
||
289 | case 1: // Nick Compensation Servo |
||
290 | ServoNickOffset = (ServoNickOffset * 3 + (int16_t)Parameter_ServoNickControl * MULTIPLYER) / 4; // lowpass offset |
||
291 | ServoNickValue = ServoNickOffset; // offset (Range from 0 to 255 * 3 = 765) |
||
1232 | hbuss | 292 | if(EE_Parameter.ServoCompInvert & 0x01) |
1156 | hbuss | 293 | { // inverting movement of servo |
294 | ServoNickValue += (int16_t)( ( (int32_t)EE_Parameter.ServoNickComp * MULTIPLYER * (IntegralNick / 128L ) ) / (256L) ); |
||
295 | } |
||
296 | else |
||
297 | { // non inverting movement of servo |
||
298 | ServoNickValue -= (int16_t)( ( (int32_t)EE_Parameter.ServoNickComp * MULTIPLYER * (IntegralNick / 128L ) ) / (256L) ); |
||
299 | } |
||
300 | // limit servo value to its parameter range definition |
||
301 | if(ServoNickValue < ((int16_t)EE_Parameter.ServoNickMin * MULTIPLYER) ) |
||
302 | { |
||
303 | ServoNickValue = (int16_t)EE_Parameter.ServoNickMin * MULTIPLYER; |
||
304 | } |
||
305 | else |
||
306 | if(ServoNickValue > ((int16_t)EE_Parameter.ServoNickMax * MULTIPLYER) ) |
||
307 | { |
||
308 | ServoNickValue = (int16_t)EE_Parameter.ServoNickMax * MULTIPLYER; |
||
309 | } |
||
310 | RemainingPulse += ServoNickValue - (256 / 2) * MULTIPLYER; // shift ServoNickValue to center position |
||
311 | ServoNickValue /= MULTIPLYER; |
||
312 | break; |
||
1224 | hbuss | 313 | case 2: // Roll Compensation Servo |
1232 | hbuss | 314 | ServoRollOffset = (ServoRollOffset * 3 + (int16_t) Parameter_ServoRollControl * MULTIPLYER) / 4; // lowpass offset |
1224 | hbuss | 315 | ServoRollValue = ServoRollOffset; // offset (Range from 0 to 255 * 3 = 765) |
1232 | hbuss | 316 | if(EE_Parameter.ServoCompInvert & 0x02) |
1224 | hbuss | 317 | { // inverting movement of servo |
1232 | hbuss | 318 | ServoRollValue += (int16_t)( ( (int32_t) EE_Parameter.ServoRollComp * MULTIPLYER * (IntegralRoll / 128L ) ) / (256L) ); |
1224 | hbuss | 319 | } |
320 | else |
||
321 | { // non inverting movement of servo |
||
1232 | hbuss | 322 | ServoRollValue -= (int16_t)( ( (int32_t) EE_Parameter.ServoRollComp * MULTIPLYER * (IntegralRoll / 128L ) ) / (256L) ); |
1224 | hbuss | 323 | } |
1232 | hbuss | 324 | // limit servo value to its parameter range definition |
1224 | hbuss | 325 | if(ServoRollValue < ((int16_t)EE_Parameter.ServoRollMin * MULTIPLYER) ) |
326 | { |
||
327 | ServoRollValue = (int16_t)EE_Parameter.ServoRollMin * MULTIPLYER; |
||
328 | } |
||
329 | else |
||
330 | if(ServoRollValue > ((int16_t)EE_Parameter.ServoRollMax * MULTIPLYER) ) |
||
331 | { |
||
332 | ServoRollValue = (int16_t)EE_Parameter.ServoRollMax * MULTIPLYER; |
||
333 | } |
||
334 | RemainingPulse += ServoRollValue - (256 / 2) * MULTIPLYER; // shift ServoNickValue to center position |
||
335 | ServoRollValue /= MULTIPLYER; |
||
336 | //DebugOut.Analog[20] = ServoRollValue; |
||
1232 | hbuss | 337 | break; |
1543 | killagreg | 338 | case 3: // 2 additions are probably faster than a conversion to u16 and multiplication by 2 |
339 | RemainingPulse += Parameter_Servo3; |
||
340 | RemainingPulse += Parameter_Servo3; |
||
1403 | hbuss | 341 | break; |
1543 | killagreg | 342 | case 4: |
343 | RemainingPulse += Parameter_Servo4; |
||
344 | RemainingPulse += Parameter_Servo4; |
||
1403 | hbuss | 345 | break; |
1543 | killagreg | 346 | case 5: |
347 | RemainingPulse += Parameter_Servo5; |
||
348 | RemainingPulse += Parameter_Servo5; |
||
1403 | hbuss | 349 | break; |
1156 | hbuss | 350 | default: // other servo channels |
351 | RemainingPulse += 2 * PPM_in[ServoIndex]; // add channel value, factor of 2 because timer 1 increments 3.2µs |
||
352 | break; |
||
353 | } |
||
354 | // range servo pulse width |
||
355 | if(RemainingPulse > MAXSERVOPULSE ) RemainingPulse = MAXSERVOPULSE; // upper servo pulse limit |
||
356 | else if(RemainingPulse < MINSERVOPULSE ) RemainingPulse = MINSERVOPULSE; // lower servo pulse limit |
||
357 | // substract stop pulse width |
||
358 | RemainingPulse -= PPM_STOPPULSE; |
||
359 | // accumulate time for correct sync gap |
||
360 | ServoFrameTime += RemainingPulse; |
||
361 | } |
||
362 | } |
||
363 | else // we had a high pulse |
||
364 | { |
||
365 | TCCR2A |= (1<<COM2A0); // make a low pulse |
||
366 | // set pulsewidth to stop pulse width |
||
367 | RemainingPulse = PPM_STOPPULSE; |
||
368 | // accumulate time for correct sync gap |
||
369 | ServoFrameTime += RemainingPulse; |
||
1171 | hbuss | 370 | if(ServoActive && SenderOkay > 180) HEF4017R_OFF; // disable HEF4017 reset |
1232 | hbuss | 371 | else HEF4017R_ON; |
1156 | hbuss | 372 | ServoIndex++; // change to next servo channel |
373 | if(ServoIndex > EE_Parameter.ServoNickRefresh) ServoIndex = 0; // reset to the sync gap |
||
374 | } |
||
375 | // set pulse output active |
||
376 | PulseOutput = 1; |
||
377 | } |
||
378 | } // EOF PPM state machine |
||
379 | |||
380 | // General pulse output generator |
||
381 | if(RemainingPulse > (255 + IRS_RUNTIME)) |
||
382 | { |
||
383 | OCR2A = 255; |
||
384 | RemainingPulse -= 255; |
||
910 | hbuss | 385 | } |
1156 | hbuss | 386 | else |
387 | { |
||
388 | if(RemainingPulse > 255) // this is the 2nd last part |
||
389 | { |
||
390 | if((RemainingPulse - 255) < IRS_RUNTIME) |
||
391 | { |
||
392 | OCR2A = 255 - IRS_RUNTIME; |
||
393 | RemainingPulse -= 255 - IRS_RUNTIME; |
||
394 | |||
395 | } |
||
396 | else // last part > ISR_RUNTIME |
||
397 | { |
||
398 | OCR2A = 255; |
||
399 | RemainingPulse -= 255; |
||
400 | } |
||
401 | } |
||
402 | else // this is the last part |
||
403 | { |
||
404 | OCR2A = RemainingPulse; |
||
405 | RemainingPulse = 0; |
||
406 | PulseOutput = 0; // trigger to stop pulse |
||
407 | } |
||
408 | } // EOF general pulse output generator |
||
1111 | hbuss | 409 | } |