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