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