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--; |
1320 | hbuss | 34 | if(SpektrumTimer) SpektrumTimer--; |
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 | |||
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 | } |
1320 | hbuss | 84 | // if(cntKompass < 10) cntKompass =r 10; |
1 | ingob | 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 | 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(); |
||
1232 | hbuss | 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; |
||
180 | OCR0B = 120; |
||
181 | TCNT0 = (unsigned char)-TIMER_RELOAD_VALUE; // reload |
||
182 | //OCR1 = 0x00; |
||
1105 | killagreg | 183 | |
1156 | hbuss | 184 | TIMSK0 |= _BV(TOIE0); |
185 | } |
||
186 | |||
187 | |||
188 | /*****************************************************/ |
||
189 | /* Control Servo Position */ |
||
190 | /*****************************************************/ |
||
191 | |||
192 | ISR(TIMER2_COMPA_vect) |
||
193 | { |
||
194 | // frame len 22.5 ms = 14063 * 1.6 us |
||
195 | // stop pulse: 0.3 ms = 188 * 1.6 us |
||
196 | // min servo pulse: 0.6 ms = 375 * 1.6 us |
||
197 | // max servo pulse: 2.4 ms = 1500 * 1.6 us |
||
198 | // resolution: 1500 - 375 = 1125 steps |
||
199 | |||
200 | #define IRS_RUNTIME 127 |
||
201 | #define PPM_STOPPULSE 188 |
||
202 | // #define PPM_FRAMELEN (14063 |
||
1171 | hbuss | 203 | #define PPM_FRAMELEN (1757 * EE_Parameter.ServoNickRefresh) |
1156 | hbuss | 204 | #define MINSERVOPULSE 375 |
205 | #define MAXSERVOPULSE 1500 |
||
206 | #define SERVORANGE (MAXSERVOPULSE - MINSERVOPULSE) |
||
207 | |||
208 | static uint8_t PulseOutput = 0; |
||
209 | static uint16_t RemainingPulse = 0; |
||
210 | static uint16_t ServoFrameTime = 0; |
||
211 | static uint8_t ServoIndex = 0; |
||
212 | |||
213 | #define MULTIPLYER 4 |
||
214 | static int16_t ServoNickOffset = (255 / 2) * MULTIPLYER; // initial value near center positon |
||
1224 | hbuss | 215 | static int16_t ServoRollOffset = (255 / 2) * MULTIPLYER; // initial value near center positon |
1156 | hbuss | 216 | |
217 | if(PlatinenVersion < 20) |
||
218 | { |
||
219 | //--------------------------- |
||
220 | // Nick servo state machine |
||
221 | //--------------------------- |
||
222 | if(!PulseOutput) // pulse output complete |
||
223 | { |
||
224 | if(TCCR2A & (1<<COM2A0)) // we had a low pulse |
||
225 | { |
||
226 | TCCR2A &= ~(1<<COM2A0);// make a high pulse |
||
227 | RemainingPulse = MINSERVOPULSE + SERVORANGE/2; // center position ~ 1.5ms |
||
228 | |||
229 | ServoNickOffset = (ServoNickOffset * 3 + (int16_t)Parameter_ServoNickControl * MULTIPLYER) / 4; // lowpass offset |
||
230 | ServoNickValue = ServoNickOffset; // offset (Range from 0 to 255 * 3 = 765) |
||
1232 | hbuss | 231 | if(EE_Parameter.ServoCompInvert & 0x01) |
1156 | hbuss | 232 | { // inverting movement of servo |
233 | ServoNickValue += (int16_t)( ( (int32_t)EE_Parameter.ServoNickComp * MULTIPLYER * (IntegralNick / 128L ) ) / (256L) ); |
||
234 | } |
||
235 | else |
||
236 | { // non inverting movement of servo |
||
237 | ServoNickValue -= (int16_t)( ( (int32_t)EE_Parameter.ServoNickComp * MULTIPLYER * (IntegralNick / 128L ) ) / (256L) ); |
||
238 | } |
||
239 | // limit servo value to its parameter range definition |
||
240 | if(ServoNickValue < ((int16_t)EE_Parameter.ServoNickMin * MULTIPLYER) ) |
||
241 | { |
||
242 | ServoNickValue = (int16_t)EE_Parameter.ServoNickMin * MULTIPLYER; |
||
243 | } |
||
244 | else |
||
245 | if(ServoNickValue > ((int16_t)EE_Parameter.ServoNickMax * MULTIPLYER) ) |
||
246 | { |
||
247 | ServoNickValue = (int16_t)EE_Parameter.ServoNickMax * MULTIPLYER; |
||
248 | } |
||
249 | |||
250 | RemainingPulse += ServoNickValue - (256 / 2) * MULTIPLYER; // shift ServoNickValue to center position |
||
251 | |||
252 | ServoNickValue /= MULTIPLYER; |
||
253 | |||
254 | // range servo pulse width |
||
255 | if(RemainingPulse > MAXSERVOPULSE ) RemainingPulse = MAXSERVOPULSE; // upper servo pulse limit |
||
256 | else if(RemainingPulse < MINSERVOPULSE ) RemainingPulse = MINSERVOPULSE; // lower servo pulse limit |
||
257 | // accumulate time for correct update rate |
||
258 | ServoFrameTime = RemainingPulse; |
||
259 | } |
||
260 | else // we had a high pulse |
||
261 | { |
||
262 | TCCR2A |= (1<<COM2A0); // make a low pulse |
||
263 | RemainingPulse = PPM_FRAMELEN - ServoFrameTime; |
||
264 | } |
||
265 | // set pulse output active |
||
266 | PulseOutput = 1; |
||
267 | } |
||
268 | } // EOF Nick servo state machine |
||
269 | else |
||
270 | { |
||
271 | //----------------------------------------------------- |
||
272 | // PPM state machine, onboard demultiplexed by HEF4017 |
||
273 | //----------------------------------------------------- |
||
274 | if(!PulseOutput) // pulse output complete |
||
275 | { |
||
276 | if(TCCR2A & (1<<COM2A0)) // we had a low pulse |
||
277 | { |
||
278 | TCCR2A &= ~(1<<COM2A0);// make a high pulse |
||
279 | |||
280 | if(ServoIndex == 0) // if we are at the sync gap |
||
281 | { |
||
282 | RemainingPulse = PPM_FRAMELEN - ServoFrameTime; // generate sync gap by filling time to full frame time |
||
283 | ServoFrameTime = 0; // reset servo frame time |
||
284 | HEF4017R_ON; // enable HEF4017 reset |
||
285 | } |
||
286 | else // servo channels |
||
287 | { |
||
288 | RemainingPulse = MINSERVOPULSE + SERVORANGE/2; // center position ~ 1.5ms |
||
289 | switch(ServoIndex) // map servo channels |
||
290 | { |
||
291 | case 1: // Nick Compensation Servo |
||
292 | ServoNickOffset = (ServoNickOffset * 3 + (int16_t)Parameter_ServoNickControl * MULTIPLYER) / 4; // lowpass offset |
||
293 | ServoNickValue = ServoNickOffset; // offset (Range from 0 to 255 * 3 = 765) |
||
1232 | hbuss | 294 | if(EE_Parameter.ServoCompInvert & 0x01) |
1156 | hbuss | 295 | { // inverting movement of servo |
296 | ServoNickValue += (int16_t)( ( (int32_t)EE_Parameter.ServoNickComp * MULTIPLYER * (IntegralNick / 128L ) ) / (256L) ); |
||
297 | } |
||
298 | else |
||
299 | { // non inverting movement of servo |
||
300 | ServoNickValue -= (int16_t)( ( (int32_t)EE_Parameter.ServoNickComp * MULTIPLYER * (IntegralNick / 128L ) ) / (256L) ); |
||
301 | } |
||
302 | // limit servo value to its parameter range definition |
||
303 | if(ServoNickValue < ((int16_t)EE_Parameter.ServoNickMin * MULTIPLYER) ) |
||
304 | { |
||
305 | ServoNickValue = (int16_t)EE_Parameter.ServoNickMin * MULTIPLYER; |
||
306 | } |
||
307 | else |
||
308 | if(ServoNickValue > ((int16_t)EE_Parameter.ServoNickMax * MULTIPLYER) ) |
||
309 | { |
||
310 | ServoNickValue = (int16_t)EE_Parameter.ServoNickMax * MULTIPLYER; |
||
311 | } |
||
312 | RemainingPulse += ServoNickValue - (256 / 2) * MULTIPLYER; // shift ServoNickValue to center position |
||
313 | ServoNickValue /= MULTIPLYER; |
||
314 | break; |
||
1224 | hbuss | 315 | case 2: // Roll Compensation Servo |
1232 | hbuss | 316 | ServoRollOffset = (ServoRollOffset * 3 + (int16_t) Parameter_ServoRollControl * MULTIPLYER) / 4; // lowpass offset |
1224 | hbuss | 317 | ServoRollValue = ServoRollOffset; // offset (Range from 0 to 255 * 3 = 765) |
1232 | hbuss | 318 | if(EE_Parameter.ServoCompInvert & 0x02) |
1224 | hbuss | 319 | { // inverting movement of servo |
1232 | hbuss | 320 | ServoRollValue += (int16_t)( ( (int32_t) EE_Parameter.ServoRollComp * MULTIPLYER * (IntegralRoll / 128L ) ) / (256L) ); |
1224 | hbuss | 321 | } |
322 | else |
||
323 | { // non inverting movement of servo |
||
1232 | hbuss | 324 | ServoRollValue -= (int16_t)( ( (int32_t) EE_Parameter.ServoRollComp * MULTIPLYER * (IntegralRoll / 128L ) ) / (256L) ); |
1224 | hbuss | 325 | } |
1232 | hbuss | 326 | // limit servo value to its parameter range definition |
1224 | hbuss | 327 | if(ServoRollValue < ((int16_t)EE_Parameter.ServoRollMin * MULTIPLYER) ) |
328 | { |
||
329 | ServoRollValue = (int16_t)EE_Parameter.ServoRollMin * MULTIPLYER; |
||
330 | } |
||
331 | else |
||
332 | if(ServoRollValue > ((int16_t)EE_Parameter.ServoRollMax * MULTIPLYER) ) |
||
333 | { |
||
334 | ServoRollValue = (int16_t)EE_Parameter.ServoRollMax * MULTIPLYER; |
||
335 | } |
||
336 | RemainingPulse += ServoRollValue - (256 / 2) * MULTIPLYER; // shift ServoNickValue to center position |
||
337 | ServoRollValue /= MULTIPLYER; |
||
338 | //DebugOut.Analog[20] = ServoRollValue; |
||
1232 | hbuss | 339 | break; |
1403 | hbuss | 340 | case 3: RemainingPulse += 2 * Parameter_Servo3; |
341 | break; |
||
342 | case 4: RemainingPulse += 2 * Parameter_Servo4; |
||
343 | break; |
||
344 | case 5: RemainingPulse += 2 * Parameter_Servo5; |
||
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; |
||
1171 | hbuss | 366 | if(ServoActive && SenderOkay > 180) HEF4017R_OFF; // disable HEF4017 reset |
1232 | hbuss | 367 | else HEF4017R_ON; |
1156 | hbuss | 368 | ServoIndex++; // change to next servo channel |
369 | if(ServoIndex > EE_Parameter.ServoNickRefresh) ServoIndex = 0; // reset to the sync gap |
||
370 | } |
||
371 | // set pulse output active |
||
372 | PulseOutput = 1; |
||
373 | } |
||
374 | } // EOF PPM state machine |
||
375 | |||
376 | // General pulse output generator |
||
377 | if(RemainingPulse > (255 + IRS_RUNTIME)) |
||
378 | { |
||
379 | OCR2A = 255; |
||
380 | RemainingPulse -= 255; |
||
910 | hbuss | 381 | } |
1156 | hbuss | 382 | else |
383 | { |
||
384 | if(RemainingPulse > 255) // this is the 2nd last part |
||
385 | { |
||
386 | if((RemainingPulse - 255) < IRS_RUNTIME) |
||
387 | { |
||
388 | OCR2A = 255 - IRS_RUNTIME; |
||
389 | RemainingPulse -= 255 - IRS_RUNTIME; |
||
390 | |||
391 | } |
||
392 | else // last part > ISR_RUNTIME |
||
393 | { |
||
394 | OCR2A = 255; |
||
395 | RemainingPulse -= 255; |
||
396 | } |
||
397 | } |
||
398 | else // this is the last part |
||
399 | { |
||
400 | OCR2A = RemainingPulse; |
||
401 | RemainingPulse = 0; |
||
402 | PulseOutput = 0; // trigger to stop pulse |
||
403 | } |
||
404 | } // EOF general pulse output generator |
||
1111 | hbuss | 405 | } |