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