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