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