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