Rev 1840 | Rev 1855 | Go to most recent revision | Only display areas with differences | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed
Rev 1840 | Rev 1848 | ||
---|---|---|---|
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 | 196 | ||
197 | cosinus = sintab[EE_Parameter.CamOrientation + 6]; |
197 | cosinus = sintab[EE_Parameter.CamOrientation + 6]; |
198 | sinus = sintab[EE_Parameter.CamOrientation]; |
198 | sinus = sintab[EE_Parameter.CamOrientation]; |
199 | 199 | ||
200 | if(CalculateServoSignals == 1) |
200 | if(CalculateServoSignals == 1) |
201 | { |
201 | { |
202 | nick = (cosinus * IntegralNick) / 128L - (sinus * IntegralRoll) / 128L; |
202 | nick = (cosinus * IntegralNick) / 128L - (sinus * IntegralRoll) / 128L; |
203 | - | ||
204 | //POI_KameraNick = 450; |
- | |
205 | nick -= POI_KameraNick * 7; |
203 | nick -= POI_KameraNick * 7; |
- | 204 | ||
206 | DebugOut.Analog[16] = POI_KameraNick; |
205 | DebugOut.Analog[16] = POI_KameraNick; |
207 | 206 | ||
208 | nick = ((long)EE_Parameter.ServoNickComp * nick) / 512L; |
207 | nick = ((long)EE_Parameter.ServoNickComp * nick) / 512L; |
209 | ServoNickOffset += ((int16_t)Parameter_ServoNickControl * (MULTIPLYER*16) - ServoNickOffset) / EE_Parameter.ServoManualControlSpeed; |
208 | ServoNickOffset += ((int16_t)Parameter_ServoNickControl * (MULTIPLYER*16) - ServoNickOffset) / EE_Parameter.ServoManualControlSpeed; |
210 | ServoNickValue = ServoNickOffset / 16; // offset (Range from 0 to 255 * 3 = 765) |
209 | ServoNickValue = ServoNickOffset / 16; // offset (Range from 0 to 255 * 3 = 765) |
211 | 210 | ||
212 | if(EE_Parameter.ServoCompInvert & 0x01) |
211 | if(EE_Parameter.ServoCompInvert & 0x01) |
213 | { // inverting movement of servo |
212 | { // inverting movement of servo |
214 | ServoNickValue += nick;//(int16_t)( ( (int32_t)EE_Parameter.ServoNickComp * nick) / (256L) ); |
213 | ServoNickValue += nick;//(int16_t)( ( (int32_t)EE_Parameter.ServoNickComp * nick) / (256L) ); |
215 | } |
214 | } |
216 | else |
215 | else |
217 | { // non inverting movement of servo |
216 | { // non inverting movement of servo |
218 | ServoNickValue -= nick; |
217 | ServoNickValue -= nick; |
219 | } |
218 | } |
220 | // limit servo value to its parameter range definition |
219 | // limit servo value to its parameter range definition |
221 | if(ServoNickValue < ((int16_t)EE_Parameter.ServoNickMin * MULTIPLYER) ) |
220 | if(ServoNickValue < ((int16_t)EE_Parameter.ServoNickMin * MULTIPLYER) ) |
222 | { |
221 | { |
223 | ServoNickValue = (int16_t)EE_Parameter.ServoNickMin * MULTIPLYER; |
222 | ServoNickValue = (int16_t)EE_Parameter.ServoNickMin * MULTIPLYER; |
224 | } |
223 | } |
225 | else |
224 | else |
226 | if(ServoNickValue > ((int16_t)EE_Parameter.ServoNickMax * MULTIPLYER) ) |
225 | if(ServoNickValue > ((int16_t)EE_Parameter.ServoNickMax * MULTIPLYER) ) |
227 | { |
226 | { |
228 | ServoNickValue = (int16_t)EE_Parameter.ServoNickMax * MULTIPLYER; |
227 | ServoNickValue = (int16_t)EE_Parameter.ServoNickMax * MULTIPLYER; |
229 | } |
228 | } |
230 | if(PlatinenVersion < 20) CalculateServoSignals = 0; else CalculateServoSignals++; |
229 | if(PlatinenVersion < 20) CalculateServoSignals = 0; else CalculateServoSignals++; |
231 | } |
230 | } |
232 | else |
231 | else |
233 | { |
232 | { |
234 | roll = (cosinus * IntegralRoll) / 128L + (sinus * IntegralNick) / 128L; |
233 | roll = (cosinus * IntegralRoll) / 128L + (sinus * IntegralNick) / 128L; |
235 | roll = ((long)EE_Parameter.ServoRollComp * roll) / 512L; |
234 | roll = ((long)EE_Parameter.ServoRollComp * roll) / 512L; |
236 | ServoRollOffset += ((int16_t)Parameter_ServoRollControl * (MULTIPLYER*16) - ServoRollOffset) / EE_Parameter.ServoManualControlSpeed; |
235 | ServoRollOffset += ((int16_t)Parameter_ServoRollControl * (MULTIPLYER*16) - ServoRollOffset) / EE_Parameter.ServoManualControlSpeed; |
237 | ServoRollValue = ServoRollOffset/16; // offset (Range from 0 to 255 * 3 = 765) |
236 | ServoRollValue = ServoRollOffset/16; // offset (Range from 0 to 255 * 3 = 765) |
238 | if(EE_Parameter.ServoCompInvert & 0x02) |
237 | if(EE_Parameter.ServoCompInvert & 0x02) |
239 | { // inverting movement of servo |
238 | { // inverting movement of servo |
240 | ServoRollValue += roll; |
239 | ServoRollValue += roll; |
241 | } |
240 | } |
242 | else |
241 | else |
243 | { // non inverting movement of servo |
242 | { // non inverting movement of servo |
244 | ServoRollValue -= roll; |
243 | ServoRollValue -= roll; |
245 | } |
244 | } |
246 | // limit servo value to its parameter range definition |
245 | // limit servo value to its parameter range definition |
247 | if(ServoRollValue < ((int16_t)EE_Parameter.ServoRollMin * MULTIPLYER) ) |
246 | if(ServoRollValue < ((int16_t)EE_Parameter.ServoRollMin * MULTIPLYER) ) |
248 | { |
247 | { |
249 | ServoRollValue = (int16_t)EE_Parameter.ServoRollMin * MULTIPLYER; |
248 | ServoRollValue = (int16_t)EE_Parameter.ServoRollMin * MULTIPLYER; |
250 | } |
249 | } |
251 | else |
250 | else |
252 | if(ServoRollValue > ((int16_t)EE_Parameter.ServoRollMax * MULTIPLYER) ) |
251 | if(ServoRollValue > ((int16_t)EE_Parameter.ServoRollMax * MULTIPLYER) ) |
253 | { |
252 | { |
254 | ServoRollValue = (int16_t)EE_Parameter.ServoRollMax * MULTIPLYER; |
253 | ServoRollValue = (int16_t)EE_Parameter.ServoRollMax * MULTIPLYER; |
255 | } |
254 | } |
256 | CalculateServoSignals = 0; |
255 | CalculateServoSignals = 0; |
257 | } |
256 | } |
258 | } |
257 | } |
259 | 258 | ||
260 | ISR(TIMER2_COMPA_vect) |
259 | ISR(TIMER2_COMPA_vect) |
261 | { |
260 | { |
262 | // frame len 22.5 ms = 14063 * 1.6 us |
261 | // frame len 22.5 ms = 14063 * 1.6 us |
263 | // stop pulse: 0.3 ms = 188 * 1.6 us |
262 | // stop pulse: 0.3 ms = 188 * 1.6 us |
264 | // min servo pulse: 0.6 ms = 375 * 1.6 us |
263 | // min servo pulse: 0.6 ms = 375 * 1.6 us |
265 | // max servo pulse: 2.4 ms = 1500 * 1.6 us |
264 | // max servo pulse: 2.4 ms = 1500 * 1.6 us |
266 | // resolution: 1500 - 375 = 1125 steps |
265 | // resolution: 1500 - 375 = 1125 steps |
267 | 266 | ||
268 | #define IRS_RUNTIME 127 |
267 | #define IRS_RUNTIME 127 |
269 | #define PPM_STOPPULSE 188 |
268 | #define PPM_STOPPULSE 188 |
270 | #define PPM_FRAMELEN (1757 * EE_Parameter.ServoNickRefresh) |
269 | #define PPM_FRAMELEN (1757 * EE_Parameter.ServoNickRefresh) |
271 | #define MINSERVOPULSE 375 |
270 | #define MINSERVOPULSE 375 |
272 | #define MAXSERVOPULSE 1500 |
271 | #define MAXSERVOPULSE 1500 |
273 | #define SERVORANGE (MAXSERVOPULSE - MINSERVOPULSE) |
272 | #define SERVORANGE (MAXSERVOPULSE - MINSERVOPULSE) |
274 | 273 | ||
275 | static uint8_t PulseOutput = 0; |
274 | static uint8_t PulseOutput = 0; |
276 | static uint16_t ServoFrameTime = 0; |
275 | static uint16_t ServoFrameTime = 0; |
277 | static uint8_t ServoIndex = 0; |
276 | static uint8_t ServoIndex = 0; |
278 | 277 | ||
279 | 278 | ||
280 | if(PlatinenVersion < 20) |
279 | if(PlatinenVersion < 20) |
281 | { |
280 | { |
282 | //--------------------------- |
281 | //--------------------------- |
283 | // Nick servo state machine |
282 | // Nick servo state machine |
284 | //--------------------------- |
283 | //--------------------------- |
285 | if(!PulseOutput) // pulse output complete |
284 | if(!PulseOutput) // pulse output complete |
286 | { |
285 | { |
287 | if(TCCR2A & (1<<COM2A0)) // we had a low pulse |
286 | if(TCCR2A & (1<<COM2A0)) // we had a low pulse |
288 | { |
287 | { |
289 | TCCR2A &= ~(1<<COM2A0);// make a high pulse |
288 | TCCR2A &= ~(1<<COM2A0);// make a high pulse |
290 | RemainingPulse = MINSERVOPULSE + SERVORANGE/2; // center position ~ 1.5ms |
289 | RemainingPulse = MINSERVOPULSE + SERVORANGE/2; // center position ~ 1.5ms |
291 | RemainingPulse += ServoNickValue - (256 / 2) * MULTIPLYER; // shift ServoNickValue to center position |
290 | RemainingPulse += ServoNickValue - (256 / 2) * MULTIPLYER; // shift ServoNickValue to center position |
292 | // range servo pulse width |
291 | // range servo pulse width |
293 | if(RemainingPulse > MAXSERVOPULSE ) RemainingPulse = MAXSERVOPULSE; // upper servo pulse limit |
292 | if(RemainingPulse > MAXSERVOPULSE ) RemainingPulse = MAXSERVOPULSE; // upper servo pulse limit |
294 | else if(RemainingPulse < MINSERVOPULSE ) RemainingPulse = MINSERVOPULSE; // lower servo pulse limit |
293 | else if(RemainingPulse < MINSERVOPULSE ) RemainingPulse = MINSERVOPULSE; // lower servo pulse limit |
295 | // accumulate time for correct update rate |
294 | // accumulate time for correct update rate |
296 | ServoFrameTime = RemainingPulse; |
295 | ServoFrameTime = RemainingPulse; |
297 | } |
296 | } |
298 | else // we had a high pulse |
297 | else // we had a high pulse |
299 | { |
298 | { |
300 | TCCR2A |= (1<<COM2A0); // make a low pulse |
299 | TCCR2A |= (1<<COM2A0); // make a low pulse |
301 | RemainingPulse = PPM_FRAMELEN - ServoFrameTime; |
300 | RemainingPulse = PPM_FRAMELEN - ServoFrameTime; |
302 | CalculateServoSignals = 1; |
301 | CalculateServoSignals = 1; |
303 | } |
302 | } |
304 | // set pulse output active |
303 | // set pulse output active |
305 | PulseOutput = 1; |
304 | PulseOutput = 1; |
306 | } |
305 | } |
307 | } // EOF Nick servo state machine |
306 | } // EOF Nick servo state machine |
308 | else |
307 | else |
309 | { |
308 | { |
310 | //----------------------------------------------------- |
309 | //----------------------------------------------------- |
311 | // PPM state machine, onboard demultiplexed by HEF4017 |
310 | // PPM state machine, onboard demultiplexed by HEF4017 |
312 | //----------------------------------------------------- |
311 | //----------------------------------------------------- |
313 | if(!PulseOutput) // pulse output complete |
312 | if(!PulseOutput) // pulse output complete |
314 | { |
313 | { |
315 | if(TCCR2A & (1<<COM2A0)) // we had a low pulse |
314 | if(TCCR2A & (1<<COM2A0)) // we had a low pulse |
316 | { |
315 | { |
317 | TCCR2A &= ~(1<<COM2A0);// make a high pulse |
316 | TCCR2A &= ~(1<<COM2A0);// make a high pulse |
318 | 317 | ||
319 | if(ServoIndex == 0) // if we are at the sync gap |
318 | if(ServoIndex == 0) // if we are at the sync gap |
320 | { |
319 | { |
321 | RemainingPulse = PPM_FRAMELEN - ServoFrameTime; // generate sync gap by filling time to full frame time |
320 | RemainingPulse = PPM_FRAMELEN - ServoFrameTime; // generate sync gap by filling time to full frame time |
322 | ServoFrameTime = 0; // reset servo frame time |
321 | ServoFrameTime = 0; // reset servo frame time |
323 | HEF4017R_ON; // enable HEF4017 reset |
322 | HEF4017R_ON; // enable HEF4017 reset |
324 | } |
323 | } |
325 | else // servo channels |
324 | else // servo channels |
326 | { |
325 | { |
327 | RemainingPulse = MINSERVOPULSE + SERVORANGE/2; // center position ~ 1.5ms |
326 | RemainingPulse = MINSERVOPULSE + SERVORANGE/2; // center position ~ 1.5ms |
328 | switch(ServoIndex) // map servo channels |
327 | switch(ServoIndex) // map servo channels |
329 | { |
328 | { |
330 | case 1: // Nick Compensation Servo |
329 | case 1: // Nick Compensation Servo |
331 | RemainingPulse += ServoNickValue - (256 / 2) * MULTIPLYER; // shift ServoNickValue to center position |
330 | RemainingPulse += ServoNickValue - (256 / 2) * MULTIPLYER; // shift ServoNickValue to center position |
332 | break; |
331 | break; |
333 | case 2: // Roll Compensation Servo |
332 | case 2: // Roll Compensation Servo |
334 | RemainingPulse += ServoRollValue - (256 / 2) * MULTIPLYER; // shift ServoNickValue to center position |
333 | RemainingPulse += ServoRollValue - (256 / 2) * MULTIPLYER; // shift ServoNickValue to center position |
335 | break; |
334 | break; |
336 | case 3: |
335 | case 3: |
337 | RemainingPulse += ((int16_t)Parameter_Servo3 * MULTIPLYER) - (256 / 2) * MULTIPLYER; |
336 | RemainingPulse += ((int16_t)Parameter_Servo3 * MULTIPLYER) - (256 / 2) * MULTIPLYER; |
338 | break; |
337 | break; |
339 | case 4: |
338 | case 4: |
340 | RemainingPulse += ((int16_t)Parameter_Servo4 * MULTIPLYER) - (256 / 2) * MULTIPLYER; |
339 | RemainingPulse += ((int16_t)Parameter_Servo4 * MULTIPLYER) - (256 / 2) * MULTIPLYER; |
341 | break; |
340 | break; |
342 | case 5: |
341 | case 5: |
343 | RemainingPulse += ((int16_t)Parameter_Servo5 * MULTIPLYER) - (256 / 2) * MULTIPLYER; |
342 | RemainingPulse += ((int16_t)Parameter_Servo5 * MULTIPLYER) - (256 / 2) * MULTIPLYER; |
344 | break; |
343 | break; |
345 | default: // other servo channels |
344 | default: // other servo channels |
346 | RemainingPulse += 2 * PPM_in[ServoIndex]; // add channel value, factor of 2 because timer 1 increments 3.2µs |
345 | RemainingPulse += 2 * PPM_in[ServoIndex]; // add channel value, factor of 2 because timer 1 increments 3.2µs |
347 | break; |
346 | break; |
348 | } |
347 | } |
349 | // range servo pulse width |
348 | // range servo pulse width |
350 | if(RemainingPulse > MAXSERVOPULSE ) RemainingPulse = MAXSERVOPULSE; // upper servo pulse limit |
349 | if(RemainingPulse > MAXSERVOPULSE ) RemainingPulse = MAXSERVOPULSE; // upper servo pulse limit |
351 | else if(RemainingPulse < MINSERVOPULSE ) RemainingPulse = MINSERVOPULSE; // lower servo pulse limit |
350 | else if(RemainingPulse < MINSERVOPULSE ) RemainingPulse = MINSERVOPULSE; // lower servo pulse limit |
352 | // substract stop pulse width |
351 | // substract stop pulse width |
353 | RemainingPulse -= PPM_STOPPULSE; |
352 | RemainingPulse -= PPM_STOPPULSE; |
354 | // accumulate time for correct sync gap |
353 | // accumulate time for correct sync gap |
355 | ServoFrameTime += RemainingPulse; |
354 | ServoFrameTime += RemainingPulse; |
356 | } |
355 | } |
357 | } |
356 | } |
358 | else // we had a high pulse |
357 | else // we had a high pulse |
359 | { |
358 | { |
360 | TCCR2A |= (1<<COM2A0); // make a low pulse |
359 | TCCR2A |= (1<<COM2A0); // make a low pulse |
361 | // set pulsewidth to stop pulse width |
360 | // set pulsewidth to stop pulse width |
362 | RemainingPulse = PPM_STOPPULSE; |
361 | RemainingPulse = PPM_STOPPULSE; |
363 | // accumulate time for correct sync gap |
362 | // accumulate time for correct sync gap |
364 | ServoFrameTime += RemainingPulse; |
363 | ServoFrameTime += RemainingPulse; |
365 | if((ServoActive && SenderOkay > 50) || ServoActive == 2) HEF4017R_OFF; // disable HEF4017 reset |
364 | if((ServoActive && SenderOkay > 50) || ServoActive == 2) HEF4017R_OFF; // disable HEF4017 reset |
366 | else HEF4017R_ON; |
365 | else HEF4017R_ON; |
367 | ServoIndex++; // change to next servo channel |
366 | ServoIndex++; // change to next servo channel |
368 | if(ServoIndex > EE_Parameter.ServoNickRefresh) |
367 | if(ServoIndex > EE_Parameter.ServoNickRefresh) |
369 | { |
368 | { |
370 | CalculateServoSignals = 1; |
369 | CalculateServoSignals = 1; |
371 | ServoIndex = 0; // reset to the sync gap |
370 | ServoIndex = 0; // reset to the sync gap |
372 | } |
371 | } |
373 | } |
372 | } |
374 | // set pulse output active |
373 | // set pulse output active |
375 | PulseOutput = 1; |
374 | PulseOutput = 1; |
376 | } |
375 | } |
377 | } // EOF PPM state machine |
376 | } // EOF PPM state machine |
378 | 377 | ||
379 | // General pulse output generator |
378 | // General pulse output generator |
380 | if(RemainingPulse > (255 + IRS_RUNTIME)) |
379 | if(RemainingPulse > (255 + IRS_RUNTIME)) |
381 | { |
380 | { |
382 | OCR2A = 255; |
381 | OCR2A = 255; |
383 | RemainingPulse -= 255; |
382 | RemainingPulse -= 255; |
384 | } |
383 | } |
385 | else |
384 | else |
386 | { |
385 | { |
387 | if(RemainingPulse > 255) // this is the 2nd last part |
386 | if(RemainingPulse > 255) // this is the 2nd last part |
388 | { |
387 | { |
389 | if((RemainingPulse - 255) < IRS_RUNTIME) |
388 | if((RemainingPulse - 255) < IRS_RUNTIME) |
390 | { |
389 | { |
391 | OCR2A = 255 - IRS_RUNTIME; |
390 | OCR2A = 255 - IRS_RUNTIME; |
392 | RemainingPulse -= 255 - IRS_RUNTIME; |
391 | RemainingPulse -= 255 - IRS_RUNTIME; |
393 | 392 | ||
394 | } |
393 | } |
395 | else // last part > ISR_RUNTIME |
394 | else // last part > ISR_RUNTIME |
396 | { |
395 | { |
397 | OCR2A = 255; |
396 | OCR2A = 255; |
398 | RemainingPulse -= 255; |
397 | RemainingPulse -= 255; |
399 | } |
398 | } |
400 | } |
399 | } |
401 | else // this is the last part |
400 | else // this is the last part |
402 | { |
401 | { |
403 | OCR2A = RemainingPulse; |
402 | OCR2A = RemainingPulse; |
404 | RemainingPulse = 0; |
403 | RemainingPulse = 0; |
405 | PulseOutput = 0; // trigger to stop pulse |
404 | PulseOutput = 0; // trigger to stop pulse |
406 | } |
405 | } |
407 | } // EOF general pulse output generator |
406 | } // EOF general pulse output generator |
408 | } |
407 | } |
409 | 408 |