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