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