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