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