Rev 1155 | Rev 1166 | Go to most recent revision | Show entire file | Regard whitespace | Details | Blame | Last modification | View Log | RSS feed
Rev 1155 | Rev 1156 | ||
---|---|---|---|
Line 9... | Line 9... | ||
9 | volatile unsigned int ServoState = 40; |
9 | volatile unsigned int ServoState = 40; |
Line 10... | Line 10... | ||
10 | 10 | ||
11 | unsigned int BeepMuster = 0xffff; |
11 | unsigned int BeepMuster = 0xffff; |
Line -... | Line 12... | ||
- | 12 | int ServoValue = 0; |
|
- | 13 | ||
- | 14 | volatile int16_t ServoNickValue = 0; |
|
- | 15 | volatile int16_t ServoRollValue = 0; |
|
- | 16 | ||
- | 17 | #define HEF4017R_ON PORTC |= (1<<PORTC6) |
|
- | 18 | #define HEF4017R_OFF PORTC &= ~(1<<PORTC6) |
|
12 | int ServoValue = 0; |
19 | |
13 | 20 | ||
14 | enum { |
21 | enum { |
15 | STOP = 0, |
22 | STOP = 0, |
16 | CK = 1, |
23 | CK = 1, |
Line 84... | Line 91... | ||
84 | cntKompass = 0; |
91 | cntKompass = 0; |
85 | } |
92 | } |
86 | } |
93 | } |
87 | } |
94 | } |
Line 88... | Line -... | ||
88 | - | ||
89 | //---------------------------- |
- | |
90 | void Timer_Init(void) |
- | |
91 | { |
- | |
92 | tim_main = SetDelay(10); |
- | |
93 | TCCR0B = CK8; |
- | |
94 | TCCR0A = (1<<COM0A1)|(1<<COM0B1)|3;//fast PWM |
- | |
95 | OCR0A = 0; |
- | |
96 | OCR0B = 120; |
- | |
97 | TCNT0 = (unsigned char)-TIMER_RELOAD_VALUE; // reload |
- | |
98 | //OCR1 = 0x00; |
- | |
99 | - | ||
100 | TCCR2A=(1<<COM2A1)|(1<<COM2A0)|3; |
- | |
101 | // TCCR2B=(0<<CS20)|(1<<CS21)|(1<<CS22); // clk/256 |
- | |
102 | TCCR2B=(0<<CS20)|(0<<CS21)|(1<<CS22); // clk/64 |
- | |
103 | - | ||
104 | - | ||
105 | TIMSK2 |= _BV(OCIE2A); |
- | |
106 | - | ||
107 | TIMSK0 |= _BV(TOIE0); |
- | |
108 | OCR2A = 10; |
- | |
109 | TCNT2 = 0; |
- | |
110 | - | ||
Line 111... | Line 95... | ||
111 | } |
95 | |
Line 112... | Line 96... | ||
112 | 96 | ||
113 | // ----------------------------------------------------------------------- |
97 | // ----------------------------------------------------------------------- |
Line 140... | Line 124... | ||
140 | unsigned int akt; |
124 | unsigned int akt; |
141 | akt = SetDelay(w); |
125 | akt = SetDelay(w); |
142 | while (!CheckDelay(akt)) ANALOG_ON; |
126 | while (!CheckDelay(akt)) ANALOG_ON; |
143 | } |
127 | } |
Line 144... | Line -... | ||
144 | - | ||
145 | // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
- | |
146 | // Servo ansteuern |
128 | |
147 | // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
- | |
148 | SIGNAL(SIG_OVERFLOW2) |
- | |
149 | { |
129 | /*****************************************************/ |
150 | if (ServoState > 0) PORTD |= 0x80; |
- | |
151 | else PORTD &= ~0x80; |
- | |
152 | TCCR2A =3; |
- | |
153 | TIMSK2 &= ~_BV(TOIE2); |
- | |
154 | } |
- | |
155 | - | ||
156 | SIGNAL(SIG_OUTPUT_COMPARE2A) |
- | |
157 | { |
130 | /* Initialize Timer 2 */ |
158 | static unsigned char postPulse = 0x80; |
131 | /*****************************************************/ |
159 | static int filterServo = 100; |
132 | // The timer 2 is used to generate the PWM at PD7 (J7) |
160 | // static unsigned char restPulse = 50; |
133 | // to control a camera servo for nick compensation. |
161 | #define MULTIPLIER 4 |
- | |
162 | if(PlatinenVersion < 20) |
134 | void TIMER2_Init(void) |
- | 135 | { |
|
- | 136 | uint8_t sreg = SREG; |
|
- | 137 | ||
- | 138 | // disable all interrupts before reconfiguration |
|
- | 139 | cli(); |
|
- | 140 | ||
- | 141 | // set PD7 as output of the PWM for nick servo |
|
- | 142 | DDRD |= (1<<DDD7); |
|
- | 143 | PORTD &= ~(1<<PORTD7); // set PD7 to low |
|
- | 144 | ||
- | 145 | DDRC |= (1<<DDC6); // set PC6 as output (Reset for HEF4017) |
|
- | 146 | PORTC &= ~(1<<PORTC6); // set PC6 to low |
|
- | 147 | ||
- | 148 | // Timer/Counter 2 Control Register A |
|
- | 149 | ||
- | 150 | // Timer Mode is FastPWM with timer reload at OCR2A (Bits: WGM22 = 1, WGM21 = 1, WGM20 = 1) |
|
- | 151 | // PD7: Normal port operation, OC2A disconnected, (Bits: COM2A1 = 0, COM2A0 = 0) |
|
- | 152 | // PD6: Normal port operation, OC2B disconnected, (Bits: COM2B1 = 0, COM2B0 = 0) |
|
- | 153 | TCCR2A &= ~((1<<COM2A1)|(1<<COM2A0)|(1<<COM2B1)|(1<<COM2B0)); |
|
- | 154 | TCCR2A |= (1<<WGM21)|(1<<WGM20); |
|
- | 155 | ||
- | 156 | // Timer/Counter 2 Control Register B |
|
- | 157 | ||
- | 158 | // Set clock divider for timer 2 to SYSKLOCK/32 = 20MHz / 32 = 625 kHz |
|
- | 159 | // The timer increments from 0x00 to 0xFF with an update rate of 625 kHz or 1.6 us |
|
- | 160 | // hence the timer overflow interrupt frequency is 625 kHz / 256 = 2.44 kHz or 0.4096 ms |
|
- | 161 | ||
- | 162 | // divider 32 (Bits: CS022 = 0, CS21 = 1, CS20 = 1) |
|
- | 163 | TCCR2B &= ~((1<<FOC2A)|(1<<FOC2B)|(1<<CS22)); |
|
- | 164 | TCCR2B |= (1<<CS21)|(1<<CS20)|(1<<WGM22); |
|
- | 165 | ||
163 | { |
166 | // Initialize the Timer/Counter 2 Register |
- | 167 | TCNT2 = 0; |
|
- | 168 | ||
- | 169 | // Initialize the Output Compare Register A used for PWM generation on port PD7. |
|
- | 170 | OCR2A = 255; |
|
- | 171 | TCCR2A |= (1<<COM2A1); // set or clear at compare match depends on value of COM2A0 |
|
- | 172 | ||
- | 173 | // Timer/Counter 2 Interrupt Mask Register |
|
- | 174 | // Enable timer output compare match A Interrupt only |
|
- | 175 | TIMSK2 &= ~((1<<OCIE2B)|(1<<TOIE2)); |
|
- | 176 | TIMSK2 |= (1<<OCIE2A); |
|
- | 177 | ||
- | 178 | SREG = sreg; |
|
- | 179 | } |
|
- | 180 | ||
- | 181 | //---------------------------- |
|
164 | if(ServoState == 4) |
182 | void Timer_Init(void) |
165 | { |
183 | { |
- | 184 | tim_main = SetDelay(10); |
|
166 | ServoValue = 0x0030; // Offset Part1 |
185 | TCCR0B = CK8; |
167 | filterServo = (filterServo * 3 + (int) Parameter_ServoNickControl * 2)/4; |
186 | TCCR0A = (1<<COM0A1)|(1<<COM0B1)|3;//fast PWM |
168 | ServoValue += filterServo; |
- | |
169 | if(EE_Parameter.ServoNickCompInvert & 0x01) ServoValue += (int)( ( (long)EE_Parameter.ServoNickComp * (IntegralNick / 128L) ) / (512L/MULTIPLIER) ); |
187 | OCR0A = 0; |
170 | else ServoValue -= (int)( ( (long)EE_Parameter.ServoNickComp * (IntegralNick / 128L) ) / (512L/MULTIPLIER) ); |
188 | OCR0B = 120; |
171 | if((ServoValue) < ((int)EE_Parameter.ServoNickMin*3)) ServoValue = (int)EE_Parameter.ServoNickMin*3; |
189 | TCNT0 = (unsigned char)-TIMER_RELOAD_VALUE; // reload |
Line 172... | Line -... | ||
172 | else if((ServoValue) > ((int)EE_Parameter.ServoNickMax*3)) ServoValue = (int)EE_Parameter.ServoNickMax*3; |
- | |
173 | - | ||
174 | DebugOut.Analog[20] = ServoValue; |
190 | //OCR1 = 0x00; |
175 | if ((ServoValue % 255) < 45) { ServoValue+= 77; postPulse = 0x60 - 77; } else postPulse = 0x60; |
- | |
176 | OCR2A = 255-(ServoValue % 256); |
191 | |
- | 192 | TIMSK0 |= _BV(TOIE0); |
|
- | 193 | } |
|
- | 194 | ||
- | 195 | ||
- | 196 | /*****************************************************/ |
|
- | 197 | /* Control Servo Position */ |
|
- | 198 | /*****************************************************/ |
|
- | 199 | ||
- | 200 | ISR(TIMER2_COMPA_vect) |
|
- | 201 | { |
|
- | 202 | ||
177 | TCCR2A=(1<<COM2A1)|(1<<COM2A0)|3; |
203 | // frame len 22.5 ms = 14063 * 1.6 us |
- | 204 | // stop pulse: 0.3 ms = 188 * 1.6 us |
|
- | 205 | // min servo pulse: 0.6 ms = 375 * 1.6 us |
|
- | 206 | // max servo pulse: 2.4 ms = 1500 * 1.6 us |
|
- | 207 | // resolution: 1500 - 375 = 1125 steps |
|
- | 208 | ||
- | 209 | #define IRS_RUNTIME 127 |
|
- | 210 | #define PPM_STOPPULSE 188 |
|
- | 211 | // #define PPM_FRAMELEN (14063 |
|
- | 212 | #define PPM_FRAMELEN (1757 * EE_Parameter.ServoNickRefresh) |
|
- | 213 | #define MINSERVOPULSE 375 |
|
- | 214 | #define MAXSERVOPULSE 1500 |
|
- | 215 | #define SERVORANGE (MAXSERVOPULSE - MINSERVOPULSE) |
|
- | 216 | ||
- | 217 | static uint8_t PulseOutput = 0; |
|
- | 218 | static uint16_t RemainingPulse = 0; |
|
- | 219 | static uint16_t ServoFrameTime = 0; |
|
- | 220 | static uint8_t ServoIndex = 0; |
|
- | 221 | ||
- | 222 | #define MULTIPLYER 4 |
|
- | 223 | static int16_t ServoNickOffset = (255 / 2) * MULTIPLYER; // initial value near center positon |
|
- | 224 | ||
178 | } |
225 | |
- | 226 | if(PlatinenVersion < 20) |
|
179 | else if ((ServoState > 0) && (ServoState < 4)) |
227 | { |
180 | { |
228 | //--------------------------- |
- | 229 | // Nick servo state machine |
|
- | 230 | //--------------------------- |
|
- | 231 | if(!PulseOutput) // pulse output complete |
|
181 | if(ServoValue > 255) |
232 | { |
- | 233 | if(TCCR2A & (1<<COM2A0)) // we had a low pulse |
|
- | 234 | { |
|
- | 235 | TCCR2A &= ~(1<<COM2A0);// make a high pulse |
|
- | 236 | RemainingPulse = MINSERVOPULSE + SERVORANGE/2; // center position ~ 1.5ms |
|
- | 237 | ||
- | 238 | ServoNickOffset = (ServoNickOffset * 3 + (int16_t)Parameter_ServoNickControl * MULTIPLYER) / 4; // lowpass offset |
|
182 | { PORTD |= 0x80; |
239 | ServoNickValue = ServoNickOffset; // offset (Range from 0 to 255 * 3 = 765) |
- | 240 | if(EE_Parameter.ServoNickCompInvert & 0x01) |
|
183 | TCCR2A =3; |
241 | { // inverting movement of servo |
184 | ServoValue -= 255; |
242 | ServoNickValue += (int16_t)( ( (int32_t)EE_Parameter.ServoNickComp * MULTIPLYER * (IntegralNick / 128L ) ) / (256L) ); |
- | 243 | } |
|
- | 244 | else |
|
- | 245 | { // non inverting movement of servo |
|
- | 246 | ServoNickValue -= (int16_t)( ( (int32_t)EE_Parameter.ServoNickComp * MULTIPLYER * (IntegralNick / 128L ) ) / (256L) ); |
|
- | 247 | } |
|
185 | } |
248 | // limit servo value to its parameter range definition |
186 | else |
- | |
187 | { |
249 | if(ServoNickValue < ((int16_t)EE_Parameter.ServoNickMin * MULTIPLYER) ) |
188 | TCCR2A=(1<<COM2A1)|(0<<COM2A0)|3; |
- | |
189 | OCR2A = postPulse; // Offset Part2 |
250 | { |
- | 251 | ServoNickValue = (int16_t)EE_Parameter.ServoNickMin * MULTIPLYER; |
|
- | 252 | } |
|
- | 253 | else |
|
- | 254 | if(ServoNickValue > ((int16_t)EE_Parameter.ServoNickMax * MULTIPLYER) ) |
|
- | 255 | { |
|
- | 256 | ServoNickValue = (int16_t)EE_Parameter.ServoNickMax * MULTIPLYER; |
|
- | 257 | } |
|
- | 258 | ||
- | 259 | RemainingPulse += ServoNickValue - (256 / 2) * MULTIPLYER; // shift ServoNickValue to center position |
|
- | 260 | ||
- | 261 | ServoNickValue /= MULTIPLYER; |
|
- | 262 | DebugOut.Analog[20] = ServoNickValue; |
|
- | 263 | ||
- | 264 | // range servo pulse width |
|
- | 265 | if(RemainingPulse > MAXSERVOPULSE ) RemainingPulse = MAXSERVOPULSE; // upper servo pulse limit |
|
- | 266 | else if(RemainingPulse < MINSERVOPULSE ) RemainingPulse = MINSERVOPULSE; // lower servo pulse limit |
|
190 | ServoState = 1; |
267 | // accumulate time for correct update rate |
191 | } |
268 | ServoFrameTime = RemainingPulse; |
192 | } |
269 | } |
193 | else if (ServoState == 0) |
270 | else // we had a high pulse |
194 | { |
271 | { |
195 | ServoState = (int) EE_Parameter.ServoNickRefresh * MULTIPLIER; |
- | |
196 | PORTD&=~0x80; |
272 | TCCR2A |= (1<<COM2A0); // make a low pulse |
- | 273 | RemainingPulse = PPM_FRAMELEN - ServoFrameTime; |
|
- | 274 | } |
|
197 | TCCR2A = 3; |
275 | // set pulse output active |
- | 276 | PulseOutput = 1; |
|
198 | } |
277 | } |
199 | } |
278 | } // EOF Nick servo state machine |
- | 279 | else |
|
- | 280 | { |
|
- | 281 | //----------------------------------------------------- |
|
200 | else |
282 | // PPM state machine, onboard demultiplexed by HEF4017 |
201 | { |
283 | //----------------------------------------------------- |
202 | if(ServoState == 4) |
- | |
203 | { |
- | |
204 | PORTD &= ~0x80; |
- | |
205 | PORTC |= _BV(PC6); |
- | |
206 | ServoValue = 0x00030; // Offset Part1 |
- | |
207 | filterServo = (filterServo * 3 + (int) Parameter_ServoNickControl * 2)/4; |
- | |
208 | ServoValue += filterServo; |
- | |
209 | if(EE_Parameter.ServoNickCompInvert & 0x01) ServoValue += (int)( ( (long)EE_Parameter.ServoNickComp * (IntegralNick / 128L) ) / (512L/MULTIPLIER) ); |
- | |
210 | else ServoValue -= (int)( ( (long)EE_Parameter.ServoNickComp * (IntegralNick / 128L) ) / (512L/MULTIPLIER) ); |
- | |
211 | if((ServoValue) < ((int)EE_Parameter.ServoNickMin*3)) ServoValue = (int)EE_Parameter.ServoNickMin*3; |
- | |
212 | else if((ServoValue) > ((int)EE_Parameter.ServoNickMax*3)) ServoValue = (int)EE_Parameter.ServoNickMax*3; |
284 | if(!PulseOutput) // pulse output complete |
213 | PORTD |= 0x80; // dummy clock to skip output Q0 |
- | |
214 | if ((ServoValue % 256) < 1) ServoValue -=2; |
- | |
215 | if ((ServoValue % 256) > 253) ServoValue +=2; |
285 | { |
216 | DebugOut.Analog[20] = ServoValue; |
286 | if(TCCR2A & (1<<COM2A0)) // we had a low pulse |
217 | - | ||
Line 218... | Line 287... | ||
218 | OCR2A = 254-(ServoValue % 255); |
287 | { |
- | 288 | TCCR2A &= ~(1<<COM2A0);// make a high pulse |
|
- | 289 | ||
219 | PORTD &= ~0x80; |
290 | if(ServoIndex == 0) // if we are at the sync gap |
220 | 291 | { |
|
221 | TCCR2A=(1<<COM2A1)|(1<<COM2A0)|3; // set on match |
292 | RemainingPulse = PPM_FRAMELEN - ServoFrameTime; // generate sync gap by filling time to full frame time |
222 | //ServoValue -= OCR2A; |
293 | ServoFrameTime = 0; // reset servo frame time |
223 | PORTC &= ~_BV(PC6); |
294 | HEF4017R_ON; // enable HEF4017 reset |
- | 295 | } |
|
224 | } |
296 | else // servo channels |
225 | else if ((ServoState > 0) && (ServoState < 4)) |
297 | { |
226 | { |
298 | RemainingPulse = MINSERVOPULSE + SERVORANGE/2; // center position ~ 1.5ms |
227 | if(ServoValue > 255) |
- | |
- | 299 | switch(ServoIndex) // map servo channels |
|
228 | { |
300 | { |
229 | PORTD &= ~0x80; |
301 | case 1: // Nick Compensation Servo |
230 | TCCR2A =3; |
302 | ServoNickOffset = (ServoNickOffset * 3 + (int16_t)Parameter_ServoNickControl * MULTIPLYER) / 4; // lowpass offset |
- | 303 | ServoNickValue = ServoNickOffset; // offset (Range from 0 to 255 * 3 = 765) |
|
231 | ServoValue -= 255; |
304 | if(EE_Parameter.ServoNickCompInvert & 0x01) |
232 | OCR2A = postPulse; // Offset Part2 |
305 | { // inverting movement of servo |
- | 306 | ServoNickValue += (int16_t)( ( (int32_t)EE_Parameter.ServoNickComp * MULTIPLYER * (IntegralNick / 128L ) ) / (256L) ); |
|
- | 307 | } |
|
- | 308 | else |
|
- | 309 | { // non inverting movement of servo |
|
- | 310 | ServoNickValue -= (int16_t)( ( (int32_t)EE_Parameter.ServoNickComp * MULTIPLYER * (IntegralNick / 128L ) ) / (256L) ); |
|
233 | //OCR2A = (ServoValue % 256); |
311 | } |
- | 312 | // limit servo value to its parameter range definition |
|
- | 313 | if(ServoNickValue < ((int16_t)EE_Parameter.ServoNickMin * MULTIPLYER) ) |
|
- | 314 | { |
|
- | 315 | ServoNickValue = (int16_t)EE_Parameter.ServoNickMin * MULTIPLYER; |
|
- | 316 | } |
|
- | 317 | else |
|
- | 318 | if(ServoNickValue > ((int16_t)EE_Parameter.ServoNickMax * MULTIPLYER) ) |
|
- | 319 | { |
|
234 | } |
320 | ServoNickValue = (int16_t)EE_Parameter.ServoNickMax * MULTIPLYER; |
- | 321 | } |
|
- | 322 | RemainingPulse += ServoNickValue - (256 / 2) * MULTIPLYER; // shift ServoNickValue to center position |
|
- | 323 | ServoNickValue /= MULTIPLYER; |
|
- | 324 | DebugOut.Analog[20] = ServoNickValue; |
|
- | 325 | break; |
|
- | 326 | ||
- | 327 | default: // other servo channels |
|
235 | else |
328 | RemainingPulse += 2 * PPM_in[ServoIndex]; // add channel value, factor of 2 because timer 1 increments 3.2µs |
- | 329 | break; |
|
- | 330 | } |
|
- | 331 | // range servo pulse width |
|
236 | { |
332 | if(RemainingPulse > MAXSERVOPULSE ) RemainingPulse = MAXSERVOPULSE; // upper servo pulse limit |
- | 333 | else if(RemainingPulse < MINSERVOPULSE ) RemainingPulse = MINSERVOPULSE; // lower servo pulse limit |
|
237 | OCR2A = postPulse; // Offset Part2 |
334 | // substract stop pulse width |
238 | //OCR2A = (ServoValue % 256); |
335 | RemainingPulse -= PPM_STOPPULSE; |
239 | //TCCR2A=(1<<COM2A1)|(1<<COM2A0)|3; |
336 | // accumulate time for correct sync gap |
240 | ServoState = 1; |
337 | ServoFrameTime += RemainingPulse; |
241 | } |
338 | } |
- | 339 | } |
|
- | 340 | else // we had a high pulse |
|
242 | } |
341 | { |
243 | else if (ServoState == 0) |
342 | TCCR2A |= (1<<COM2A0); // make a low pulse |
244 | { |
343 | // set pulsewidth to stop pulse width |
245 | PORTD &= ~0x80; |
344 | RemainingPulse = PPM_STOPPULSE; |
- | 345 | // accumulate time for correct sync gap |
|
- | 346 | ServoFrameTime += RemainingPulse; |
|
246 | TCCR2A=(1<<COM2A1)|(1<<COM2A0)|3; |
347 | HEF4017R_OFF; // disable HEF4017 reset |
- | 348 | ServoIndex++; // change to next servo channel |
|
- | 349 | if(ServoIndex > EE_Parameter.ServoNickRefresh) ServoIndex = 0; // reset to the sync gap |
|
247 | ServoState = (int) EE_Parameter.ServoNickRefresh * MULTIPLIER; |
350 | } |
- | 351 | // set pulse output active |
|
- | 352 | PulseOutput = 1; |
|
- | 353 | } |
|
- | 354 | } // EOF PPM state machine |
|
- | 355 | ||
248 | TCCR2A =3; |
356 | // General pulse output generator |
- | 357 | if(RemainingPulse > (255 + IRS_RUNTIME)) |
|
249 | } |
358 | { |
- | 359 | OCR2A = 255; |
|
- | 360 | RemainingPulse -= 255; |
|
- | 361 | } |
|
- | 362 | else |
|
- | 363 | { |
|
- | 364 | if(RemainingPulse > 255) // this is the 2nd last part |
|
- | 365 | { |
|
- | 366 | if((RemainingPulse - 255) < IRS_RUNTIME) |
|
Line -... | Line 367... | ||
- | 367 | { |
|
- | 368 | OCR2A = 255 - IRS_RUNTIME; |
|
- | 369 | RemainingPulse -= 255 - IRS_RUNTIME; |
|
- | 370 | ||
- | 371 | } |
|
- | 372 | else // last part > ISR_RUNTIME |
|
- | 373 | { |
|
- | 374 | OCR2A = 255; |
|
- | 375 | RemainingPulse -= 255; |
|
- | 376 | } |
|
- | 377 | } |
|
- | 378 | else // this is the last part |
|
- | 379 | { |
|
- | 380 | OCR2A = RemainingPulse; |
|
- | 381 | RemainingPulse = 0; |