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