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