Rev 2040 | Rev 2090 | Go to most recent revision | Only display areas with differences | Regard whitespace | Details | Blame | Last modification | View Log | RSS feed
Rev 2040 | Rev 2050 | ||
---|---|---|---|
- | 1 | // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
|
- | 2 | // + www.MikroKopter.com |
|
- | 3 | // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
|
- | 4 | // + Software Nutzungsbedingungen (english version: see below) |
|
- | 5 | // + der Fa. HiSystems GmbH, Flachsmeerstrasse 2, 26802 Moormerland - nachfolgend Lizenzgeber genannt - |
|
- | 6 | // + Der Lizenzgeber räumt dem Kunden ein nicht-ausschließliches, zeitlich und räumlich* unbeschränktes Recht ein, die im den |
|
- | 7 | // + Mikrocontroller verwendete Firmware für die Hardware Flight-Ctrl, Navi-Ctrl, BL-Ctrl, MK3Mag & PC-Programm MikroKopter-Tool |
|
- | 8 | // + - nachfolgend Software genannt - nur für private Zwecke zu nutzen. |
|
- | 9 | // + Der Einsatz dieser Software ist nur auf oder mit Produkten des Lizenzgebers zulässig. |
|
- | 10 | // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
|
- | 11 | // + Die vom Lizenzgeber gelieferte Software ist urheberrechtlich geschützt. Alle Rechte an der Software sowie an sonstigen im |
|
- | 12 | // + Rahmen der Vertragsanbahnung und Vertragsdurchführung überlassenen Unterlagen stehen im Verhältnis der Vertragspartner ausschließlich dem Lizenzgeber zu. |
|
- | 13 | // + Die in der Software enthaltenen Copyright-Vermerke, Markenzeichen, andere Rechtsvorbehalte, Seriennummern sowie |
|
- | 14 | // + sonstige der Programmidentifikation dienenden Merkmale dürfen vom Kunden nicht verändert oder unkenntlich gemacht werden. |
|
- | 15 | // + Der Kunde trifft angemessene Vorkehrungen für den sicheren Einsatz der Software. Er wird die Software gründlich auf deren |
|
- | 16 | // + Verwendbarkeit zu dem von ihm beabsichtigten Zweck testen, bevor er diese operativ einsetzt. |
|
- | 17 | // + Die Haftung des Lizenzgebers wird - soweit gesetzlich zulässig - begrenzt in Höhe des typischen und vorhersehbaren |
|
- | 18 | // + Schadens. Die gesetzliche Haftung bei Personenschäden und nach dem Produkthaftungsgesetz bleibt unberührt. Dem Lizenzgeber steht jedoch der Einwand |
|
- | 19 | // + des Mitverschuldens offen. |
|
- | 20 | // + Der Kunde trifft angemessene Vorkehrungen für den Fall, dass die Software ganz oder teilweise nicht ordnungsgemäß arbeitet. |
|
- | 21 | // + Er wird die Software gründlich auf deren Verwendbarkeit zu dem von ihm beabsichtigten Zweck testen, bevor er diese operativ einsetzt. |
|
- | 22 | // + Der Kunde wird er seine Daten vor Einsatz der Software nach dem Stand der Technik sichern. |
|
- | 23 | // + Der Kunde ist darüber unterrichtet, dass der Lizenzgeber seine Daten im zur Vertragsdurchführung erforderlichen Umfang |
|
- | 24 | // + und auf Grundlage der Datenschutzvorschriften erhebt, speichert, verarbeitet und, sofern notwendig, an Dritte übermittelt. |
|
- | 25 | // + *) Die räumliche Nutzung bezieht sich nur auf den Einsatzort, nicht auf die Reichweite der programmierten Software. |
|
- | 26 | // + #### ENDE DER NUTZUNGSBEDINGUNGEN ####' |
|
- | 27 | // + Hinweis: Informationen über erweiterte Nutzungsrechte (wie z.B. Nutzung für nicht-private Zwecke) sind auf Anfrage per Email an info(@)hisystems.de verfügbar. |
|
- | 28 | // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
|
- | 29 | // + Software LICENSING TERMS |
|
- | 30 | // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
|
- | 31 | // + of HiSystems GmbH, Flachsmeerstrasse 2, 26802 Moormerland, Germany - the Licensor - |
|
- | 32 | // + The Licensor grants the customer a non-exclusive license to use the microcontroller firmware of the Flight-Ctrl, Navi-Ctrl, BL-Ctrl, and MK3Mag hardware |
|
- | 33 | // + (the Software) exclusively for private purposes. The License is unrestricted with respect to time and territory*. |
|
- | 34 | // + The Software may only be used with the Licensor's products. |
|
- | 35 | // + The Software provided by the Licensor is protected by copyright. With respect to the relationship between the parties to this |
|
- | 36 | // + agreement, all rights pertaining to the Software and other documents provided during the preparation and execution of this |
|
- | 37 | // + agreement shall be the property of the Licensor. |
|
- | 38 | // + The information contained in the Software copyright notices, trademarks, other legal reservations, serial numbers and other |
|
- | 39 | // + features that can be used to identify the program may not be altered or defaced by the customer. |
|
- | 40 | // + The customer shall be responsible for taking reasonable precautions |
|
- | 41 | // + for the safe use of the Software. The customer shall test the Software thoroughly regarding its suitability for the |
|
- | 42 | // + intended purpose before implementing it for actual operation. The Licensor's liability shall be limited to the extent of typical and |
|
- | 43 | // + foreseeable damage to the extent permitted by law, notwithstanding statutory liability for bodily injury and product |
|
- | 44 | // + liability. However, the Licensor shall be entitled to the defense of contributory negligence. |
|
- | 45 | // + The customer will take adequate precautions in the case, that the software is not working properly. The customer will test |
|
- | 46 | // + the software for his purpose before any operational usage. The customer will backup his data before using the software. |
|
- | 47 | // + The customer understands that the Licensor collects, stores and processes, and, where required, forwards, customer data |
|
- | 48 | // + to third parties to the extent necessary for executing the agreement, subject to applicable data protection and privacy regulations. |
|
- | 49 | // + *) The territory aspect only refers to the place where the Software is used, not its programmed range. |
|
- | 50 | // + #### END OF LICENSING TERMS #### |
|
- | 51 | // + Note: For information on license extensions (e.g. commercial use), please contact us at info(@)hisystems.de. |
|
- | 52 | // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
|
- | 53 | ||
1 | #include "main.h" |
54 | #include "main.h" |
2 | #define MULTIPLYER 4 |
55 | #define MULTIPLYER 4 |
3 | 56 | ||
4 | volatile unsigned int CountMilliseconds = 0; |
57 | volatile unsigned int CountMilliseconds = 0; |
5 | volatile static unsigned int tim_main; |
58 | volatile static unsigned int tim_main; |
6 | volatile unsigned char UpdateMotor = 0; |
59 | volatile unsigned char UpdateMotor = 0; |
7 | volatile unsigned int cntKompass = 0; |
60 | volatile unsigned int cntKompass = 0; |
8 | volatile unsigned int beeptime = 0; |
61 | volatile unsigned int beeptime = 0; |
9 | volatile unsigned char SendSPI = 0, ServoActive = 0, CalculateServoSignals = 1; |
62 | volatile unsigned char SendSPI = 0, ServoActive = 0, CalculateServoSignals = 1; |
10 | uint16_t RemainingPulse = 0; |
63 | uint16_t RemainingPulse = 0; |
11 | volatile int16_t ServoNickOffset = (255 / 2) * MULTIPLYER * 16; // initial value near center positon |
64 | 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 |
65 | volatile int16_t ServoRollOffset = (255 / 2) * MULTIPLYER * 16; // initial value near center positon |
13 | 66 | ||
14 | unsigned int BeepMuster = 0xffff; |
67 | unsigned int BeepMuster = 0xffff; |
15 | 68 | ||
16 | volatile int16_t ServoNickValue = 0; |
69 | volatile int16_t ServoNickValue = 0; |
17 | volatile int16_t ServoRollValue = 0; |
70 | volatile int16_t ServoRollValue = 0; |
18 | 71 | ||
19 | 72 | ||
20 | enum { |
73 | enum { |
21 | STOP = 0, |
74 | STOP = 0, |
22 | CK = 1, |
75 | CK = 1, |
23 | CK8 = 2, |
76 | CK8 = 2, |
24 | CK64 = 3, |
77 | CK64 = 3, |
25 | CK256 = 4, |
78 | CK256 = 4, |
26 | CK1024 = 5, |
79 | CK1024 = 5, |
27 | T0_FALLING_EDGE = 6, |
80 | T0_FALLING_EDGE = 6, |
28 | T0_RISING_EDGE = 7 |
81 | T0_RISING_EDGE = 7 |
29 | }; |
82 | }; |
30 | 83 | ||
31 | 84 | ||
32 | ISR(TIMER0_OVF_vect) // 9,7kHz |
85 | ISR(TIMER0_OVF_vect) // 9,7kHz |
33 | { |
86 | { |
34 | static unsigned char cnt_1ms = 1,cnt = 0, compass_active = 0; |
87 | static unsigned char cnt_1ms = 1,cnt = 0, compass_active = 0; |
35 | unsigned char pieper_ein = 0; |
88 | unsigned char pieper_ein = 0; |
36 | if(SendSPI) SendSPI--; |
89 | if(SendSPI) SendSPI--; |
37 | if(SpektrumTimer) SpektrumTimer--; |
90 | if(SpektrumTimer) SpektrumTimer--; |
38 | if(!cnt--) |
91 | if(!cnt--) |
39 | { |
92 | { |
40 | cnt = 9; |
93 | cnt = 9; |
41 | CountMilliseconds++; |
94 | CountMilliseconds++; |
42 | cnt_1ms++; |
95 | cnt_1ms++; |
43 | cnt_1ms %= 2; |
96 | cnt_1ms %= 2; |
44 | 97 | ||
45 | if(!cnt_1ms) UpdateMotor = 1; |
98 | if(!cnt_1ms) UpdateMotor = 1; |
46 | if(!(PINC & 0x10)) compass_active = 1; |
99 | if(!(PINC & 0x10)) compass_active = 1; |
47 | 100 | ||
48 | if(beeptime) |
101 | if(beeptime) |
49 | { |
102 | { |
50 | if(beeptime > 10) beeptime -= 10; else beeptime = 0; |
103 | if(beeptime > 10) beeptime -= 10; else beeptime = 0; |
51 | if(beeptime & BeepMuster) |
104 | if(beeptime & BeepMuster) |
52 | { |
105 | { |
53 | pieper_ein = 1; |
106 | pieper_ein = 1; |
54 | } |
107 | } |
55 | else pieper_ein = 0; |
108 | else pieper_ein = 0; |
56 | } |
109 | } |
57 | else |
110 | else |
58 | { |
111 | { |
59 | pieper_ein = 0; |
112 | pieper_ein = 0; |
60 | BeepMuster = 0xffff; |
113 | BeepMuster = 0xffff; |
61 | } |
114 | } |
62 | if(pieper_ein) |
115 | if(pieper_ein) |
63 | { |
116 | { |
64 | if(PlatinenVersion == 10) PORTD |= (1<<2); // Speaker an PORTD.2 |
117 | if(PlatinenVersion == 10) PORTD |= (1<<2); // Speaker an PORTD.2 |
65 | else PORTC |= (1<<7); // Speaker an PORTC.7 |
118 | else PORTC |= (1<<7); // Speaker an PORTC.7 |
66 | } |
119 | } |
67 | else |
120 | else |
68 | { |
121 | { |
69 | if(PlatinenVersion == 10) PORTD &= ~(1<<2); |
122 | if(PlatinenVersion == 10) PORTD &= ~(1<<2); |
70 | else PORTC &= ~(1<<7); |
123 | else PORTC &= ~(1<<7); |
71 | } |
124 | } |
72 | } |
125 | } |
73 | if(compass_active && !NaviDataOkay && Parameter_GlobalConfig & CFG_KOMPASS_AKTIV) |
126 | if(compass_active && !NaviDataOkay && Parameter_GlobalConfig & CFG_KOMPASS_AKTIV) |
74 | { |
127 | { |
75 | if(PINC & 0x10) |
128 | if(PINC & 0x10) |
76 | { |
129 | { |
77 | if(++cntKompass > 1000) compass_active = 0; |
130 | if(++cntKompass > 1000) compass_active = 0; |
78 | } |
131 | } |
79 | else |
132 | else |
80 | { |
133 | { |
81 | if((cntKompass) && (cntKompass < 362)) |
134 | if((cntKompass) && (cntKompass < 362)) |
82 | { |
135 | { |
83 | cntKompass += cntKompass / 41; |
136 | cntKompass += cntKompass / 41; |
84 | if(cntKompass > 10) KompassValue = cntKompass - 10; else KompassValue = 0; |
137 | if(cntKompass > 10) KompassValue = cntKompass - 10; else KompassValue = 0; |
85 | // KompassRichtung = ((540 + KompassValue - KompassSollWert) % 360) - 180; |
138 | // KompassRichtung = ((540 + KompassValue - KompassSollWert) % 360) - 180; |
86 | } |
139 | } |
87 | cntKompass = 0; |
140 | cntKompass = 0; |
88 | } |
141 | } |
89 | } |
142 | } |
90 | } |
143 | } |
91 | 144 | ||
92 | 145 | ||
93 | // ----------------------------------------------------------------------- |
146 | // ----------------------------------------------------------------------- |
94 | unsigned int SetDelay (unsigned int t) |
147 | unsigned int SetDelay (unsigned int t) |
95 | { |
148 | { |
96 | // TIMSK0 &= ~_BV(TOIE0); |
149 | // TIMSK0 &= ~_BV(TOIE0); |
97 | return(CountMilliseconds + t + 1); |
150 | return(CountMilliseconds + t + 1); |
98 | // TIMSK0 |= _BV(TOIE0); |
151 | // TIMSK0 |= _BV(TOIE0); |
99 | } |
152 | } |
100 | 153 | ||
101 | // ----------------------------------------------------------------------- |
154 | // ----------------------------------------------------------------------- |
102 | char CheckDelay(unsigned int t) |
155 | char CheckDelay(unsigned int t) |
103 | { |
156 | { |
104 | // TIMSK0 &= ~_BV(TOIE0); |
157 | // TIMSK0 &= ~_BV(TOIE0); |
105 | return(((t - CountMilliseconds) & 0x8000) >> 9); |
158 | return(((t - CountMilliseconds) & 0x8000) >> 9); |
106 | // TIMSK0 |= _BV(TOIE0); |
159 | // TIMSK0 |= _BV(TOIE0); |
107 | } |
160 | } |
108 | 161 | ||
109 | // ----------------------------------------------------------------------- |
162 | // ----------------------------------------------------------------------- |
110 | void Delay_ms(unsigned int w) |
163 | void Delay_ms(unsigned int w) |
111 | { |
164 | { |
112 | unsigned int akt; |
165 | unsigned int akt; |
113 | akt = SetDelay(w); |
166 | akt = SetDelay(w); |
114 | while (!CheckDelay(akt)); |
167 | while (!CheckDelay(akt)); |
115 | } |
168 | } |
116 | 169 | ||
117 | void Delay_ms_Mess(unsigned int w) |
170 | void Delay_ms_Mess(unsigned int w) |
118 | { |
171 | { |
119 | unsigned int akt; |
172 | unsigned int akt; |
120 | akt = SetDelay(w); |
173 | akt = SetDelay(w); |
121 | while (!CheckDelay(akt)) if(AdReady) {AdReady = 0; ANALOG_ON;} |
174 | while (!CheckDelay(akt)) if(AdReady) {AdReady = 0; ANALOG_ON;} |
122 | } |
175 | } |
123 | 176 | ||
124 | /*****************************************************/ |
177 | /*****************************************************/ |
125 | /* Initialize Timer 2 */ |
178 | /* Initialize Timer 2 */ |
126 | /*****************************************************/ |
179 | /*****************************************************/ |
127 | // The timer 2 is used to generate the PWM at PD7 (J7) |
180 | // The timer 2 is used to generate the PWM at PD7 (J7) |
128 | // to control a camera servo for nick compensation. |
181 | // to control a camera servo for nick compensation. |
129 | void TIMER2_Init(void) |
182 | void TIMER2_Init(void) |
130 | { |
183 | { |
131 | uint8_t sreg = SREG; |
184 | uint8_t sreg = SREG; |
132 | 185 | ||
133 | // disable all interrupts before reconfiguration |
186 | // disable all interrupts before reconfiguration |
134 | cli(); |
187 | cli(); |
135 | 188 | ||
136 | PORTD &= ~(1<<PORTD7); // set PD7 to low |
189 | PORTD &= ~(1<<PORTD7); // set PD7 to low |
137 | 190 | ||
138 | DDRC |= (1<<DDC6); // set PC6 as output (Reset for HEF4017) |
191 | DDRC |= (1<<DDC6); // set PC6 as output (Reset for HEF4017) |
139 | HEF4017R_ON; |
192 | HEF4017R_ON; |
140 | // Timer/Counter 2 Control Register A |
193 | // Timer/Counter 2 Control Register A |
141 | 194 | ||
142 | // Timer Mode is FastPWM with timer reload at OCR2A (Bits: WGM22 = 1, WGM21 = 1, WGM20 = 1) |
195 | // 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) |
196 | // PD7: Normal port operation, OC2A disconnected, (Bits: COM2A1 = 0, COM2A0 = 0) |
144 | // PD6: Normal port operation, OC2B disconnected, (Bits: COM2B1 = 0, COM2B0 = 0) |
197 | // PD6: Normal port operation, OC2B disconnected, (Bits: COM2B1 = 0, COM2B0 = 0) |
145 | TCCR2A &= ~((1<<COM2A1)|(1<<COM2A0)|(1<<COM2B1)|(1<<COM2B0)); |
198 | TCCR2A &= ~((1<<COM2A1)|(1<<COM2A0)|(1<<COM2B1)|(1<<COM2B0)); |
146 | TCCR2A |= (1<<WGM21)|(1<<WGM20); |
199 | TCCR2A |= (1<<WGM21)|(1<<WGM20); |
147 | 200 | ||
148 | // Timer/Counter 2 Control Register B |
201 | // Timer/Counter 2 Control Register B |
149 | 202 | ||
150 | // Set clock divider for timer 2 to SYSKLOCK/32 = 20MHz / 32 = 625 kHz |
203 | // 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 |
204 | // 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 |
205 | // hence the timer overflow interrupt frequency is 625 kHz / 256 = 2.44 kHz or 0.4096 ms |
153 | 206 | ||
154 | // divider 32 (Bits: CS022 = 0, CS21 = 1, CS20 = 1) |
207 | // divider 32 (Bits: CS022 = 0, CS21 = 1, CS20 = 1) |
155 | TCCR2B &= ~((1<<FOC2A)|(1<<FOC2B)|(1<<CS22)); |
208 | TCCR2B &= ~((1<<FOC2A)|(1<<FOC2B)|(1<<CS22)); |
156 | TCCR2B |= (1<<CS21)|(1<<CS20)|(1<<WGM22); |
209 | TCCR2B |= (1<<CS21)|(1<<CS20)|(1<<WGM22); |
157 | 210 | ||
158 | // Initialize the Timer/Counter 2 Register |
211 | // Initialize the Timer/Counter 2 Register |
159 | TCNT2 = 0; |
212 | TCNT2 = 0; |
160 | 213 | ||
161 | // Initialize the Output Compare Register A used for PWM generation on port PD7. |
214 | // Initialize the Output Compare Register A used for PWM generation on port PD7. |
162 | OCR2A = 255; |
215 | OCR2A = 255; |
163 | TCCR2A |= (1<<COM2A1); // set or clear at compare match depends on value of COM2A0 |
216 | TCCR2A |= (1<<COM2A1); // set or clear at compare match depends on value of COM2A0 |
164 | 217 | ||
165 | // Timer/Counter 2 Interrupt Mask Register |
218 | // Timer/Counter 2 Interrupt Mask Register |
166 | // Enable timer output compare match A Interrupt only |
219 | // Enable timer output compare match A Interrupt only |
167 | TIMSK2 &= ~((1<<OCIE2B)|(1<<TOIE2)); |
220 | TIMSK2 &= ~((1<<OCIE2B)|(1<<TOIE2)); |
168 | TIMSK2 |= (1<<OCIE2A); |
221 | TIMSK2 |= (1<<OCIE2A); |
169 | 222 | ||
170 | SREG = sreg; |
223 | SREG = sreg; |
171 | } |
224 | } |
172 | 225 | ||
173 | //---------------------------- |
226 | //---------------------------- |
174 | void Timer_Init(void) |
227 | void Timer_Init(void) |
175 | { |
228 | { |
176 | tim_main = SetDelay(10); |
229 | tim_main = SetDelay(10); |
177 | TCCR0B = CK8; |
230 | TCCR0B = CK8; |
178 | TCCR0A = (1<<COM0A1)|(1<<COM0B1)|3;//fast PWM |
231 | TCCR0A = (1<<COM0A1)|(1<<COM0B1)|3;//fast PWM |
179 | OCR0A = 0; |
232 | OCR0A = 0; |
180 | OCR0B = 180; |
233 | OCR0B = 180; |
181 | TCNT0 = (unsigned char)-TIMER_RELOAD_VALUE; // reload |
234 | TCNT0 = (unsigned char)-TIMER_RELOAD_VALUE; // reload |
182 | //OCR1 = 0x00; |
235 | //OCR1 = 0x00; |
183 | TIMSK0 |= _BV(TOIE0); |
236 | TIMSK0 |= _BV(TOIE0); |
184 | } |
237 | } |
185 | 238 | ||
186 | 239 | ||
187 | /*****************************************************/ |
240 | /*****************************************************/ |
188 | /* Control Servo Position */ |
241 | /* Control Servo Position */ |
189 | /*****************************************************/ |
242 | /*****************************************************/ |
190 | 243 | ||
191 | 244 | ||
192 | void CalculateServo(void) |
245 | void CalculateServo(void) |
193 | { |
246 | { |
194 | signed char cosinus, sinus; |
247 | signed char cosinus, sinus; |
195 | signed long nick, roll; |
248 | signed long nick, roll; |
196 | 249 | ||
197 | cosinus = sintab[EE_Parameter.CamOrientation + 6]; |
250 | cosinus = sintab[EE_Parameter.CamOrientation + 6]; |
198 | sinus = sintab[EE_Parameter.CamOrientation]; |
251 | sinus = sintab[EE_Parameter.CamOrientation]; |
199 | 252 | ||
200 | if(CalculateServoSignals == 1) |
253 | if(CalculateServoSignals == 1) |
201 | { |
254 | { |
202 | nick = (cosinus * IntegralNick) / 128L - (sinus * IntegralRoll) / 128L; |
255 | nick = (cosinus * IntegralNick) / 128L - (sinus * IntegralRoll) / 128L; |
203 | nick -= POI_KameraNick * 7; |
256 | nick -= POI_KameraNick * 7; |
204 | nick = ((long)EE_Parameter.ServoNickComp * nick) / 512L; |
257 | nick = ((long)EE_Parameter.ServoNickComp * nick) / 512L; |
205 | // offset (Range from 0 to 255 * 3 = 765) |
258 | // offset (Range from 0 to 255 * 3 = 765) |
206 | ServoNickOffset += ((int16_t)Parameter_ServoNickControl * (MULTIPLYER*16) - ServoNickOffset) / EE_Parameter.ServoManualControlSpeed; |
259 | ServoNickOffset += ((int16_t)Parameter_ServoNickControl * (MULTIPLYER*16) - ServoNickOffset) / EE_Parameter.ServoManualControlSpeed; |
207 | if(EE_Parameter.ServoCompInvert & 0x01) // inverting movement of servo |
260 | if(EE_Parameter.ServoCompInvert & 0x01) // inverting movement of servo |
208 | { |
261 | { |
209 | nick = ServoNickOffset / 16 + nick; |
262 | nick = ServoNickOffset / 16 + nick; |
210 | } |
263 | } |
211 | else |
264 | else |
212 | { // inverting movement of servo |
265 | { // inverting movement of servo |
213 | nick = ServoNickOffset / 16 - nick; |
266 | nick = ServoNickOffset / 16 - nick; |
214 | } |
267 | } |
215 | if(EE_Parameter.ServoFilterNick) ServoNickValue = ((ServoNickValue * EE_Parameter.ServoFilterNick) + nick) / (EE_Parameter.ServoFilterNick + 1); |
268 | if(EE_Parameter.ServoFilterNick) ServoNickValue = ((ServoNickValue * EE_Parameter.ServoFilterNick) + nick) / (EE_Parameter.ServoFilterNick + 1); |
216 | else ServoNickValue = nick; |
269 | else ServoNickValue = nick; |
217 | // limit servo value to its parameter range definition |
270 | // limit servo value to its parameter range definition |
218 | if(ServoNickValue < ((int16_t)EE_Parameter.ServoNickMin * MULTIPLYER)) |
271 | if(ServoNickValue < ((int16_t)EE_Parameter.ServoNickMin * MULTIPLYER)) |
219 | { |
272 | { |
220 | ServoNickValue = (int16_t)EE_Parameter.ServoNickMin * MULTIPLYER; |
273 | ServoNickValue = (int16_t)EE_Parameter.ServoNickMin * MULTIPLYER; |
221 | } |
274 | } |
222 | else |
275 | else |
223 | if(ServoNickValue > ((int16_t)EE_Parameter.ServoNickMax * MULTIPLYER)) |
276 | if(ServoNickValue > ((int16_t)EE_Parameter.ServoNickMax * MULTIPLYER)) |
224 | { |
277 | { |
225 | ServoNickValue = (int16_t)EE_Parameter.ServoNickMax * MULTIPLYER; |
278 | ServoNickValue = (int16_t)EE_Parameter.ServoNickMax * MULTIPLYER; |
226 | } |
279 | } |
227 | if(PlatinenVersion < 20) CalculateServoSignals = 0; else CalculateServoSignals++; |
280 | if(PlatinenVersion < 20) CalculateServoSignals = 0; else CalculateServoSignals++; |
228 | } |
281 | } |
229 | else |
282 | else |
230 | { |
283 | { |
231 | roll = (cosinus * IntegralRoll) / 128L + (sinus * IntegralNick) / 128L; |
284 | roll = (cosinus * IntegralRoll) / 128L + (sinus * IntegralNick) / 128L; |
232 | roll = ((long)EE_Parameter.ServoRollComp * roll) / 512L; |
285 | roll = ((long)EE_Parameter.ServoRollComp * roll) / 512L; |
233 | ServoRollOffset += ((int16_t)Parameter_ServoRollControl * (MULTIPLYER*16) - ServoRollOffset) / EE_Parameter.ServoManualControlSpeed; |
286 | ServoRollOffset += ((int16_t)Parameter_ServoRollControl * (MULTIPLYER*16) - ServoRollOffset) / EE_Parameter.ServoManualControlSpeed; |
234 | if(EE_Parameter.ServoCompInvert & 0x02) |
287 | if(EE_Parameter.ServoCompInvert & 0x02) |
235 | { // inverting movement of servo |
288 | { // inverting movement of servo |
236 | roll = ServoRollOffset / 16 + roll; |
289 | roll = ServoRollOffset / 16 + roll; |
237 | } |
290 | } |
238 | else |
291 | else |
239 | { // inverting movement of servo |
292 | { // inverting movement of servo |
240 | roll = ServoRollOffset / 16 - roll; |
293 | roll = ServoRollOffset / 16 - roll; |
241 | } |
294 | } |
242 | if(EE_Parameter.ServoFilterRoll) ServoRollValue = ((ServoRollValue * EE_Parameter.ServoFilterRoll) + roll) / (EE_Parameter.ServoFilterRoll + 1); |
295 | if(EE_Parameter.ServoFilterRoll) ServoRollValue = ((ServoRollValue * EE_Parameter.ServoFilterRoll) + roll) / (EE_Parameter.ServoFilterRoll + 1); |
243 | else ServoRollValue = roll; |
296 | else ServoRollValue = roll; |
244 | // limit servo value to its parameter range definition |
297 | // limit servo value to its parameter range definition |
245 | if(ServoRollValue < ((int16_t)EE_Parameter.ServoRollMin * MULTIPLYER)) |
298 | if(ServoRollValue < ((int16_t)EE_Parameter.ServoRollMin * MULTIPLYER)) |
246 | { |
299 | { |
247 | ServoRollValue = (int16_t)EE_Parameter.ServoRollMin * MULTIPLYER; |
300 | ServoRollValue = (int16_t)EE_Parameter.ServoRollMin * MULTIPLYER; |
248 | } |
301 | } |
249 | else |
302 | else |
250 | if(ServoRollValue > ((int16_t)EE_Parameter.ServoRollMax * MULTIPLYER)) |
303 | if(ServoRollValue > ((int16_t)EE_Parameter.ServoRollMax * MULTIPLYER)) |
251 | { |
304 | { |
252 | ServoRollValue = (int16_t)EE_Parameter.ServoRollMax * MULTIPLYER; |
305 | ServoRollValue = (int16_t)EE_Parameter.ServoRollMax * MULTIPLYER; |
253 | } |
306 | } |
254 | CalculateServoSignals = 0; |
307 | CalculateServoSignals = 0; |
255 | } |
308 | } |
256 | } |
309 | } |
257 | 310 | ||
258 | ISR(TIMER2_COMPA_vect) |
311 | ISR(TIMER2_COMPA_vect) |
259 | { |
312 | { |
260 | // frame len 22.5 ms = 14063 * 1.6 us |
313 | // frame len 22.5 ms = 14063 * 1.6 us |
261 | // stop pulse: 0.3 ms = 188 * 1.6 us |
314 | // stop pulse: 0.3 ms = 188 * 1.6 us |
262 | // min servo pulse: 0.6 ms = 375 * 1.6 us |
315 | // min servo pulse: 0.6 ms = 375 * 1.6 us |
263 | // max servo pulse: 2.4 ms = 1500 * 1.6 us |
316 | // max servo pulse: 2.4 ms = 1500 * 1.6 us |
264 | // resolution: 1500 - 375 = 1125 steps |
317 | // resolution: 1500 - 375 = 1125 steps |
265 | 318 | ||
266 | #define IRS_RUNTIME 127 |
319 | #define IRS_RUNTIME 127 |
267 | #define PPM_STOPPULSE 188 |
320 | #define PPM_STOPPULSE 188 |
268 | #define PPM_FRAMELEN (1757 * EE_Parameter.ServoNickRefresh) |
321 | #define PPM_FRAMELEN (1757 * EE_Parameter.ServoNickRefresh) |
269 | #define MINSERVOPULSE 375 |
322 | #define MINSERVOPULSE 375 |
270 | #define MAXSERVOPULSE 1500 |
323 | #define MAXSERVOPULSE 1500 |
271 | #define SERVORANGE (MAXSERVOPULSE - MINSERVOPULSE) |
324 | #define SERVORANGE (MAXSERVOPULSE - MINSERVOPULSE) |
272 | 325 | ||
273 | static uint8_t PulseOutput = 0; |
326 | static uint8_t PulseOutput = 0; |
274 | static uint16_t ServoFrameTime = 0; |
327 | static uint16_t ServoFrameTime = 0; |
275 | static uint8_t ServoIndex = 0; |
328 | static uint8_t ServoIndex = 0; |
276 | 329 | ||
277 | 330 | ||
278 | if(PlatinenVersion < 20) |
331 | if(PlatinenVersion < 20) |
279 | { |
332 | { |
280 | //--------------------------- |
333 | //--------------------------- |
281 | // Nick servo state machine |
334 | // Nick servo state machine |
282 | //--------------------------- |
335 | //--------------------------- |
283 | if(!PulseOutput) // pulse output complete |
336 | if(!PulseOutput) // pulse output complete |
284 | { |
337 | { |
285 | if(TCCR2A & (1<<COM2A0)) // we had a low pulse |
338 | if(TCCR2A & (1<<COM2A0)) // we had a low pulse |
286 | { |
339 | { |
287 | TCCR2A &= ~(1<<COM2A0);// make a high pulse |
340 | TCCR2A &= ~(1<<COM2A0);// make a high pulse |
288 | RemainingPulse = MINSERVOPULSE + SERVORANGE/2; // center position ~ 1.5ms |
341 | RemainingPulse = MINSERVOPULSE + SERVORANGE/2; // center position ~ 1.5ms |
289 | RemainingPulse += ServoNickValue - (256 / 2) * MULTIPLYER; // shift ServoNickValue to center position |
342 | RemainingPulse += ServoNickValue - (256 / 2) * MULTIPLYER; // shift ServoNickValue to center position |
290 | // range servo pulse width |
343 | // range servo pulse width |
291 | if(RemainingPulse > MAXSERVOPULSE ) RemainingPulse = MAXSERVOPULSE; // upper servo pulse limit |
344 | if(RemainingPulse > MAXSERVOPULSE ) RemainingPulse = MAXSERVOPULSE; // upper servo pulse limit |
292 | else if(RemainingPulse < MINSERVOPULSE ) RemainingPulse = MINSERVOPULSE; // lower servo pulse limit |
345 | else if(RemainingPulse < MINSERVOPULSE ) RemainingPulse = MINSERVOPULSE; // lower servo pulse limit |
293 | // accumulate time for correct update rate |
346 | // accumulate time for correct update rate |
294 | ServoFrameTime = RemainingPulse; |
347 | ServoFrameTime = RemainingPulse; |
295 | } |
348 | } |
296 | else // we had a high pulse |
349 | else // we had a high pulse |
297 | { |
350 | { |
298 | TCCR2A |= (1<<COM2A0); // make a low pulse |
351 | TCCR2A |= (1<<COM2A0); // make a low pulse |
299 | RemainingPulse = PPM_FRAMELEN - ServoFrameTime; |
352 | RemainingPulse = PPM_FRAMELEN - ServoFrameTime; |
300 | CalculateServoSignals = 1; |
353 | CalculateServoSignals = 1; |
301 | } |
354 | } |
302 | // set pulse output active |
355 | // set pulse output active |
303 | PulseOutput = 1; |
356 | PulseOutput = 1; |
304 | } |
357 | } |
305 | } // EOF Nick servo state machine |
358 | } // EOF Nick servo state machine |
306 | else |
359 | else |
307 | { |
360 | { |
308 | //----------------------------------------------------- |
361 | //----------------------------------------------------- |
309 | // PPM state machine, onboard demultiplexed by HEF4017 |
362 | // PPM state machine, onboard demultiplexed by HEF4017 |
310 | //----------------------------------------------------- |
363 | //----------------------------------------------------- |
311 | if(!PulseOutput) // pulse output complete |
364 | if(!PulseOutput) // pulse output complete |
312 | { |
365 | { |
313 | if(TCCR2A & (1<<COM2A0)) // we had a low pulse |
366 | if(TCCR2A & (1<<COM2A0)) // we had a low pulse |
314 | { |
367 | { |
315 | TCCR2A &= ~(1<<COM2A0);// make a high pulse |
368 | TCCR2A &= ~(1<<COM2A0);// make a high pulse |
316 | 369 | ||
317 | if(ServoIndex == 0) // if we are at the sync gap |
370 | if(ServoIndex == 0) // if we are at the sync gap |
318 | { |
371 | { |
319 | RemainingPulse = PPM_FRAMELEN - ServoFrameTime; // generate sync gap by filling time to full frame time |
372 | RemainingPulse = PPM_FRAMELEN - ServoFrameTime; // generate sync gap by filling time to full frame time |
320 | ServoFrameTime = 0; // reset servo frame time |
373 | ServoFrameTime = 0; // reset servo frame time |
321 | HEF4017R_ON; // enable HEF4017 reset |
374 | HEF4017R_ON; // enable HEF4017 reset |
322 | } |
375 | } |
323 | else // servo channels |
376 | else // servo channels |
324 | { |
377 | { |
325 | RemainingPulse = MINSERVOPULSE + SERVORANGE/2; // center position ~ 1.5ms |
378 | RemainingPulse = MINSERVOPULSE + SERVORANGE/2; // center position ~ 1.5ms |
326 | switch(ServoIndex) // map servo channels |
379 | switch(ServoIndex) // map servo channels |
327 | { |
380 | { |
328 | case 1: // Nick Compensation Servo |
381 | case 1: // Nick Compensation Servo |
329 | RemainingPulse += ServoNickValue - (256 / 2) * MULTIPLYER; // shift ServoNickValue to center position |
382 | RemainingPulse += ServoNickValue - (256 / 2) * MULTIPLYER; // shift ServoNickValue to center position |
330 | break; |
383 | break; |
331 | case 2: // Roll Compensation Servo |
384 | case 2: // Roll Compensation Servo |
332 | RemainingPulse += ServoRollValue - (256 / 2) * MULTIPLYER; // shift ServoNickValue to center position |
385 | RemainingPulse += ServoRollValue - (256 / 2) * MULTIPLYER; // shift ServoNickValue to center position |
333 | break; |
386 | break; |
334 | case 3: |
387 | case 3: |
335 | RemainingPulse += ((int16_t)Parameter_Servo3 * MULTIPLYER) - (256 / 2) * MULTIPLYER; |
388 | RemainingPulse += ((int16_t)Parameter_Servo3 * MULTIPLYER) - (256 / 2) * MULTIPLYER; |
336 | break; |
389 | break; |
337 | case 4: |
390 | case 4: |
338 | RemainingPulse += ((int16_t)Parameter_Servo4 * MULTIPLYER) - (256 / 2) * MULTIPLYER; |
391 | RemainingPulse += ((int16_t)Parameter_Servo4 * MULTIPLYER) - (256 / 2) * MULTIPLYER; |
339 | break; |
392 | break; |
340 | case 5: |
393 | case 5: |
341 | RemainingPulse += ((int16_t)Parameter_Servo5 * MULTIPLYER) - (256 / 2) * MULTIPLYER; |
394 | RemainingPulse += ((int16_t)Parameter_Servo5 * MULTIPLYER) - (256 / 2) * MULTIPLYER; |
342 | break; |
395 | break; |
343 | default: // other servo channels |
396 | default: // other servo channels |
344 | RemainingPulse += 2 * PPM_in[ServoIndex]; // add channel value, factor of 2 because timer 1 increments 3.2µs |
397 | RemainingPulse += 2 * PPM_in[ServoIndex]; // add channel value, factor of 2 because timer 1 increments 3.2µs |
345 | break; |
398 | break; |
346 | } |
399 | } |
347 | // range servo pulse width |
400 | // range servo pulse width |
348 | if(RemainingPulse > MAXSERVOPULSE ) RemainingPulse = MAXSERVOPULSE; // upper servo pulse limit |
401 | if(RemainingPulse > MAXSERVOPULSE ) RemainingPulse = MAXSERVOPULSE; // upper servo pulse limit |
349 | else if(RemainingPulse < MINSERVOPULSE ) RemainingPulse = MINSERVOPULSE; // lower servo pulse limit |
402 | else if(RemainingPulse < MINSERVOPULSE ) RemainingPulse = MINSERVOPULSE; // lower servo pulse limit |
350 | // substract stop pulse width |
403 | // substract stop pulse width |
351 | RemainingPulse -= PPM_STOPPULSE; |
404 | RemainingPulse -= PPM_STOPPULSE; |
352 | // accumulate time for correct sync gap |
405 | // accumulate time for correct sync gap |
353 | ServoFrameTime += RemainingPulse; |
406 | ServoFrameTime += RemainingPulse; |
354 | } |
407 | } |
355 | } |
408 | } |
356 | else // we had a high pulse |
409 | else // we had a high pulse |
357 | { |
410 | { |
358 | TCCR2A |= (1<<COM2A0); // make a low pulse |
411 | TCCR2A |= (1<<COM2A0); // make a low pulse |
359 | // set pulsewidth to stop pulse width |
412 | // set pulsewidth to stop pulse width |
360 | RemainingPulse = PPM_STOPPULSE; |
413 | RemainingPulse = PPM_STOPPULSE; |
361 | // accumulate time for correct sync gap |
414 | // accumulate time for correct sync gap |
362 | ServoFrameTime += RemainingPulse; |
415 | ServoFrameTime += RemainingPulse; |
363 | if((ServoActive && SenderOkay) || ServoActive == 2) HEF4017R_OFF; // disable HEF4017 reset |
416 | if((ServoActive && SenderOkay) || ServoActive == 2) HEF4017R_OFF; // disable HEF4017 reset |
364 | else HEF4017R_ON; |
417 | else HEF4017R_ON; |
365 | ServoIndex++; // change to next servo channel |
418 | ServoIndex++; // change to next servo channel |
366 | if(ServoIndex > EE_Parameter.ServoNickRefresh) |
419 | if(ServoIndex > EE_Parameter.ServoNickRefresh) |
367 | { |
420 | { |
368 | CalculateServoSignals = 1; |
421 | CalculateServoSignals = 1; |
369 | ServoIndex = 0; // reset to the sync gap |
422 | ServoIndex = 0; // reset to the sync gap |
370 | } |
423 | } |
371 | } |
424 | } |
372 | // set pulse output active |
425 | // set pulse output active |
373 | PulseOutput = 1; |
426 | PulseOutput = 1; |
374 | } |
427 | } |
375 | } // EOF PPM state machine |
428 | } // EOF PPM state machine |
376 | 429 | ||
377 | // General pulse output generator |
430 | // General pulse output generator |
378 | if(RemainingPulse > (255 + IRS_RUNTIME)) |
431 | if(RemainingPulse > (255 + IRS_RUNTIME)) |
379 | { |
432 | { |
380 | OCR2A = 255; |
433 | OCR2A = 255; |
381 | RemainingPulse -= 255; |
434 | RemainingPulse -= 255; |
382 | } |
435 | } |
383 | else |
436 | else |
384 | { |
437 | { |
385 | if(RemainingPulse > 255) // this is the 2nd last part |
438 | if(RemainingPulse > 255) // this is the 2nd last part |
386 | { |
439 | { |
387 | if((RemainingPulse - 255) < IRS_RUNTIME) |
440 | if((RemainingPulse - 255) < IRS_RUNTIME) |
388 | { |
441 | { |
389 | OCR2A = 255 - IRS_RUNTIME; |
442 | OCR2A = 255 - IRS_RUNTIME; |
390 | RemainingPulse -= 255 - IRS_RUNTIME; |
443 | RemainingPulse -= 255 - IRS_RUNTIME; |
391 | 444 | ||
392 | } |
445 | } |
393 | else // last part > ISR_RUNTIME |
446 | else // last part > ISR_RUNTIME |
394 | { |
447 | { |
395 | OCR2A = 255; |
448 | OCR2A = 255; |
396 | RemainingPulse -= 255; |
449 | RemainingPulse -= 255; |
397 | } |
450 | } |
398 | } |
451 | } |
399 | else // this is the last part |
452 | else // this is the last part |
400 | { |
453 | { |
401 | OCR2A = RemainingPulse; |
454 | OCR2A = RemainingPulse; |
402 | RemainingPulse = 0; |
455 | RemainingPulse = 0; |
403 | PulseOutput = 0; // trigger to stop pulse |
456 | PulseOutput = 0; // trigger to stop pulse |
404 | } |
457 | } |
405 | } // EOF general pulse output generator |
458 | } // EOF general pulse output generator |
406 | } |
459 | } |
407 | 460 |