Rev 2240 | Go to most recent revision | Only display areas with differences | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed
Rev 2240 | Rev 2296 | ||
---|---|---|---|
1 | // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
1 | // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
2 | // + www.MikroKopter.com |
2 | // + www.MikroKopter.com |
3 | // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
3 | // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
4 | // + Software Nutzungsbedingungen (english version: see below) |
4 | // + Software Nutzungsbedingungen (english version: see below) |
5 | // + der Fa. HiSystems GmbH, Flachsmeerstrasse 2, 26802 Moormerland - nachfolgend Lizenzgeber genannt - |
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 |
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 |
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. |
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. |
9 | // + Der Einsatz dieser Software ist nur auf oder mit Produkten des Lizenzgebers zulässig. |
10 | // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
10 | // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
11 | // + Die vom Lizenzgeber gelieferte Software ist urheberrechtlich geschützt. Alle Rechte an der Software sowie an sonstigen im |
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. |
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 |
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. |
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 |
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. |
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 |
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 |
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. |
19 | // + des Mitverschuldens offen. |
20 | // + Der Kunde trifft angemessene Vorkehrungen für den Fall, dass die Software ganz oder teilweise nicht ordnungsgemäß arbeitet. |
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. |
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. |
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 |
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. |
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. |
25 | // + *) Die räumliche Nutzung bezieht sich nur auf den Einsatzort, nicht auf die Reichweite der programmierten Software. |
26 | // + #### ENDE DER NUTZUNGSBEDINGUNGEN ####' |
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. |
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 | // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
28 | // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
29 | // + Software LICENSING TERMS |
29 | // + Software LICENSING TERMS |
30 | // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
30 | // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
31 | // + of HiSystems GmbH, Flachsmeerstrasse 2, 26802 Moormerland, Germany - the Licensor - |
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 |
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*. |
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. |
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 |
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 |
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. |
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 |
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. |
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 |
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 |
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 |
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 |
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. |
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 |
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. |
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 |
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. |
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. |
49 | // + *) The territory aspect only refers to the place where the Software is used, not its programmed range. |
50 | // + #### END OF LICENSING TERMS #### |
50 | // + #### END OF LICENSING TERMS #### |
51 | // + Note: For information on license extensions (e.g. commercial use), please contact us at info(@)hisystems.de. |
51 | // + Note: For information on license extensions (e.g. commercial use), please contact us at info(@)hisystems.de. |
52 | // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
52 | // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
53 | 53 | ||
54 | #include "main.h" |
54 | #include "main.h" |
55 | #define MULTIPLYER 4 |
55 | #define MULTIPLYER 4 |
56 | 56 | ||
57 | volatile unsigned int CountMilliseconds = 0; |
57 | volatile unsigned int CountMilliseconds = 0; |
58 | volatile unsigned int tim_main; |
58 | volatile unsigned int tim_main; |
59 | volatile unsigned char UpdateMotor = 0; |
59 | volatile unsigned char UpdateMotor = 0; |
60 | volatile unsigned int cntKompass = 0; |
60 | volatile unsigned int cntKompass = 0; |
61 | volatile unsigned int beeptime = 0; |
61 | volatile unsigned int beeptime = 0; |
62 | volatile unsigned char SendSPI = 0, ServoActive = 0, CalculateServoSignals = 1; |
62 | volatile unsigned char SendSPI = 0, ServoActive = 0, CalculateServoSignals = 1; |
63 | uint16_t RemainingPulse = 0; |
63 | uint16_t RemainingPulse = 0; |
64 | 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 |
65 | 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 |
66 | 66 | ||
67 | unsigned int BeepMuster = 0xffff; |
67 | unsigned int BeepMuster = 0xffff; |
68 | signed int NickServoValue = 128 * MULTIPLYER * 16; |
68 | signed int NickServoValue = 128 * MULTIPLYER * 16; |
69 | 69 | ||
70 | volatile int16_t ServoNickValue = 0; |
70 | volatile int16_t ServoNickValue = 0; |
71 | volatile int16_t ServoRollValue = 0; |
71 | volatile int16_t ServoRollValue = 0; |
72 | 72 | ||
73 | 73 | ||
74 | enum { |
74 | enum { |
75 | STOP = 0, |
75 | STOP = 0, |
76 | CK = 1, |
76 | CK = 1, |
77 | CK8 = 2, |
77 | CK8 = 2, |
78 | CK64 = 3, |
78 | CK64 = 3, |
79 | CK256 = 4, |
79 | CK256 = 4, |
80 | CK1024 = 5, |
80 | CK1024 = 5, |
81 | T0_FALLING_EDGE = 6, |
81 | T0_FALLING_EDGE = 6, |
82 | T0_RISING_EDGE = 7 |
82 | T0_RISING_EDGE = 7 |
83 | }; |
83 | }; |
84 | 84 | ||
85 | 85 | ||
86 | ISR(TIMER0_OVF_vect) // 9,7kHz |
86 | ISR(TIMER0_OVF_vect) // 9,7kHz |
87 | { |
87 | { |
88 | static unsigned char cnt_1ms = 1,cnt = 0, compass_active = 0; |
88 | static unsigned char cnt_1ms = 1,cnt = 0, compass_active = 0; |
89 | unsigned char pieper_ein = 0; |
89 | unsigned char pieper_ein = 0; |
90 | if(SendSPI) SendSPI--; |
90 | if(SendSPI) SendSPI--; |
91 | if(SpektrumTimer) SpektrumTimer--; |
91 | if(SpektrumTimer) SpektrumTimer--; |
92 | if(!cnt--) |
92 | if(!cnt--) |
93 | { |
93 | { |
94 | cnt = 9; |
94 | cnt = 9; |
95 | CountMilliseconds++; |
95 | CountMilliseconds++; |
96 | cnt_1ms++; |
96 | cnt_1ms++; |
97 | cnt_1ms %= 2; |
97 | cnt_1ms %= 2; |
98 | 98 | ||
99 | if(!cnt_1ms) UpdateMotor = 1; |
99 | if(!cnt_1ms) UpdateMotor = 1; |
100 | if(!(PINC & 0x10)) compass_active = 1; |
100 | if(!(PINC & 0x10)) compass_active = 1; |
101 | 101 | ||
102 | if(beeptime) |
102 | if(beeptime) |
103 | { |
103 | { |
104 | if(beeptime > 10) beeptime -= 10; else beeptime = 0; |
104 | if(beeptime > 10) beeptime -= 10; else beeptime = 0; |
105 | if(beeptime & BeepMuster) |
105 | if(beeptime & BeepMuster) |
106 | { |
106 | { |
107 | pieper_ein = 1; |
107 | pieper_ein = 1; |
108 | } |
108 | } |
109 | else pieper_ein = 0; |
109 | else pieper_ein = 0; |
110 | } |
110 | } |
111 | else |
111 | else |
112 | { |
112 | { |
113 | pieper_ein = 0; |
113 | pieper_ein = 0; |
114 | BeepMuster = 0xffff; |
114 | BeepMuster = 0xffff; |
115 | } |
115 | } |
116 | if(pieper_ein) |
116 | if(pieper_ein) |
117 | { |
117 | { |
118 | if(PlatinenVersion == 10) PORTD |= (1<<2); // Speaker an PORTD.2 |
118 | if(PlatinenVersion == 10) PORTD |= (1<<2); // Speaker an PORTD.2 |
119 | else PORTC |= (1<<7); // Speaker an PORTC.7 |
119 | else PORTC |= (1<<7); // Speaker an PORTC.7 |
120 | } |
120 | } |
121 | else |
121 | else |
122 | { |
122 | { |
123 | if(PlatinenVersion == 10) PORTD &= ~(1<<2); |
123 | if(PlatinenVersion == 10) PORTD &= ~(1<<2); |
124 | else PORTC &= ~(1<<7); |
124 | else PORTC &= ~(1<<7); |
125 | } |
125 | } |
126 | } |
126 | } |
127 | if(compass_active && !NaviDataOkay && Parameter_GlobalConfig & CFG_KOMPASS_AKTIV) |
127 | if(compass_active && !NaviDataOkay && Parameter_GlobalConfig & CFG_KOMPASS_AKTIV) |
128 | { |
128 | { |
129 | if(PINC & 0x10) |
129 | if(PINC & 0x10) |
130 | { |
130 | { |
131 | if(++cntKompass > 1000) compass_active = 0; |
131 | if(++cntKompass > 1000) compass_active = 0; |
132 | } |
132 | } |
133 | else |
133 | else |
134 | { |
134 | { |
135 | if((cntKompass) && (cntKompass < 362)) |
135 | if((cntKompass) && (cntKompass < 362)) |
136 | { |
136 | { |
137 | cntKompass += cntKompass / 41; |
137 | cntKompass += cntKompass / 41; |
138 | if(cntKompass > 10) KompassValue = cntKompass - 10; else KompassValue = 0; |
138 | if(cntKompass > 10) KompassValue = cntKompass - 10; else KompassValue = 0; |
139 | // KompassRichtung = ((540 + KompassValue - KompassSollWert) % 360) - 180; |
139 | // KompassRichtung = ((540 + KompassValue - KompassSollWert) % 360) - 180; |
140 | } |
140 | } |
141 | cntKompass = 0; |
141 | cntKompass = 0; |
142 | } |
142 | } |
143 | } |
143 | } |
144 | } |
144 | } |
145 | 145 | ||
146 | 146 | ||
147 | // ----------------------------------------------------------------------- |
147 | // ----------------------------------------------------------------------- |
148 | unsigned int SetDelay(unsigned int t) |
148 | unsigned int SetDelay(unsigned int t) |
149 | { |
149 | { |
150 | // TIMSK0 &= ~_BV(TOIE0); |
150 | // TIMSK0 &= ~_BV(TOIE0); |
151 | return(CountMilliseconds + t + 1); |
151 | return(CountMilliseconds + t + 1); |
152 | // TIMSK0 |= _BV(TOIE0); |
152 | // TIMSK0 |= _BV(TOIE0); |
153 | } |
153 | } |
154 | 154 | ||
155 | // ----------------------------------------------------------------------- |
155 | // ----------------------------------------------------------------------- |
156 | char CheckDelay(unsigned int t) |
156 | char CheckDelay(unsigned int t) |
157 | { |
157 | { |
158 | // TIMSK0 &= ~_BV(TOIE0); |
158 | // TIMSK0 &= ~_BV(TOIE0); |
159 | return(((t - CountMilliseconds) & 0x8000) >> 9); |
159 | return(((t - CountMilliseconds) & 0x8000) >> 9); |
160 | // TIMSK0 |= _BV(TOIE0); |
160 | // TIMSK0 |= _BV(TOIE0); |
161 | } |
161 | } |
162 | 162 | ||
163 | // ----------------------------------------------------------------------- |
163 | // ----------------------------------------------------------------------- |
164 | void Delay_ms(unsigned int w) |
164 | void Delay_ms(unsigned int w) |
165 | { |
165 | { |
166 | unsigned int akt; |
166 | unsigned int akt; |
167 | akt = SetDelay(w); |
167 | akt = SetDelay(w); |
168 | while (!CheckDelay(akt)); |
168 | while (!CheckDelay(akt)); |
169 | } |
169 | } |
170 | 170 | ||
171 | void Delay_ms_Mess(unsigned int w) |
171 | void Delay_ms_Mess(unsigned int w) |
172 | { |
172 | { |
173 | unsigned int akt; |
173 | unsigned int akt; |
174 | akt = SetDelay(w); |
174 | akt = SetDelay(w); |
175 | while (!CheckDelay(akt)) if(AdReady) {AdReady = 0; ANALOG_ON;} |
175 | while (!CheckDelay(akt)) if(AdReady) {AdReady = 0; ANALOG_ON;} |
176 | } |
176 | } |
177 | 177 | ||
178 | /*****************************************************/ |
178 | /*****************************************************/ |
179 | /* Initialize Timer 2 */ |
179 | /* Initialize Timer 2 */ |
180 | /*****************************************************/ |
180 | /*****************************************************/ |
181 | // The timer 2 is used to generate the PWM at PD7 (J7) |
181 | // The timer 2 is used to generate the PWM at PD7 (J7) |
182 | // to control a camera servo for nick compensation. |
182 | // to control a camera servo for nick compensation. |
183 | void TIMER2_Init(void) |
183 | void TIMER2_Init(void) |
184 | { |
184 | { |
185 | uint8_t sreg = SREG; |
185 | uint8_t sreg = SREG; |
186 | 186 | ||
187 | // disable all interrupts before reconfiguration |
187 | // disable all interrupts before reconfiguration |
188 | cli(); |
188 | cli(); |
189 | 189 | ||
190 | PORTD &= ~(1<<PORTD7); // set PD7 to low |
190 | PORTD &= ~(1<<PORTD7); // set PD7 to low |
191 | 191 | ||
192 | DDRC |= (1<<DDC6); // set PC6 as output (Reset for HEF4017) |
192 | DDRC |= (1<<DDC6); // set PC6 as output (Reset for HEF4017) |
193 | HEF4017Reset_ON; |
193 | HEF4017Reset_ON; |
194 | // Timer/Counter 2 Control Register A |
194 | // Timer/Counter 2 Control Register A |
195 | 195 | ||
196 | // Timer Mode is FastPWM with timer reload at OCR2A (Bits: WGM22 = 1, WGM21 = 1, WGM20 = 1) |
196 | // Timer Mode is FastPWM with timer reload at OCR2A (Bits: WGM22 = 1, WGM21 = 1, WGM20 = 1) |
197 | // PD7: Normal port operation, OC2A disconnected, (Bits: COM2A1 = 0, COM2A0 = 0) |
197 | // PD7: Normal port operation, OC2A disconnected, (Bits: COM2A1 = 0, COM2A0 = 0) |
198 | // PD6: Normal port operation, OC2B disconnected, (Bits: COM2B1 = 0, COM2B0 = 0) |
198 | // PD6: Normal port operation, OC2B disconnected, (Bits: COM2B1 = 0, COM2B0 = 0) |
199 | TCCR2A &= ~((1<<COM2A1)|(1<<COM2A0)|(1<<COM2B1)|(1<<COM2B0)); |
199 | TCCR2A &= ~((1<<COM2A1)|(1<<COM2A0)|(1<<COM2B1)|(1<<COM2B0)); |
200 | TCCR2A |= (1<<WGM21)|(1<<WGM20); |
200 | TCCR2A |= (1<<WGM21)|(1<<WGM20); |
201 | 201 | ||
202 | // Timer/Counter 2 Control Register B |
202 | // Timer/Counter 2 Control Register B |
203 | 203 | ||
204 | // Set clock divider for timer 2 to SYSKLOCK/32 = 20MHz / 32 = 625 kHz |
204 | // Set clock divider for timer 2 to SYSKLOCK/32 = 20MHz / 32 = 625 kHz |
205 | // The timer increments from 0x00 to 0xFF with an update rate of 625 kHz or 1.6 us |
205 | // The timer increments from 0x00 to 0xFF with an update rate of 625 kHz or 1.6 us |
206 | // hence the timer overflow interrupt frequency is 625 kHz / 256 = 2.44 kHz or 0.4096 ms |
206 | // hence the timer overflow interrupt frequency is 625 kHz / 256 = 2.44 kHz or 0.4096 ms |
207 | 207 | ||
208 | // divider 32 (Bits: CS022 = 0, CS21 = 1, CS20 = 1) |
208 | // divider 32 (Bits: CS022 = 0, CS21 = 1, CS20 = 1) |
209 | TCCR2B &= ~((1<<FOC2A)|(1<<FOC2B)|(1<<CS22)); |
209 | TCCR2B &= ~((1<<FOC2A)|(1<<FOC2B)|(1<<CS22)); |
210 | TCCR2B |= (1<<CS21)|(1<<CS20)|(1<<WGM22); |
210 | TCCR2B |= (1<<CS21)|(1<<CS20)|(1<<WGM22); |
211 | 211 | ||
212 | // Initialize the Timer/Counter 2 Register |
212 | // Initialize the Timer/Counter 2 Register |
213 | TCNT2 = 0; |
213 | TCNT2 = 0; |
214 | 214 | ||
215 | // Initialize the Output Compare Register A used for PWM generation on port PD7. |
215 | // Initialize the Output Compare Register A used for PWM generation on port PD7. |
216 | OCR2A = 255; |
216 | OCR2A = 255; |
217 | TCCR2A |= (1<<COM2A1); // set or clear at compare match depends on value of COM2A0 |
217 | TCCR2A |= (1<<COM2A1); // set or clear at compare match depends on value of COM2A0 |
218 | 218 | ||
219 | // Timer/Counter 2 Interrupt Mask Register |
219 | // Timer/Counter 2 Interrupt Mask Register |
220 | // Enable timer output compare match A Interrupt only |
220 | // Enable timer output compare match A Interrupt only |
221 | TIMSK2 &= ~((1<<OCIE2B)|(1<<TOIE2)); |
221 | TIMSK2 &= ~((1<<OCIE2B)|(1<<TOIE2)); |
222 | TIMSK2 |= (1<<OCIE2A); |
222 | TIMSK2 |= (1<<OCIE2A); |
223 | 223 | ||
224 | SREG = sreg; |
224 | SREG = sreg; |
225 | } |
225 | } |
226 | 226 | ||
227 | //---------------------------- |
227 | //---------------------------- |
228 | void Timer_Init(void) |
228 | void Timer_Init(void) |
229 | { |
229 | { |
230 | tim_main = SetDelay(10); |
230 | tim_main = SetDelay(10); |
231 | TCCR0B = CK8; |
231 | TCCR0B = CK8; |
232 | TCCR0A = (1<<COM0A1)|(1<<COM0B1)|3;//fast PWM |
232 | TCCR0A = (1<<COM0A1)|(1<<COM0B1)|3;//fast PWM |
233 | OCR0A = 0; |
233 | OCR0A = 0; |
234 | OCR0B = 180; |
234 | OCR0B = 180; |
235 | TCNT0 = (unsigned char)-TIMER_RELOAD_VALUE; // reload |
235 | TCNT0 = (unsigned char)-TIMER_RELOAD_VALUE; // reload |
236 | //OCR1 = 0x00; |
236 | //OCR1 = 0x00; |
237 | TIMSK0 |= _BV(TOIE0); |
237 | TIMSK0 |= _BV(TOIE0); |
238 | } |
238 | } |
239 | 239 | ||
240 | 240 | ||
241 | /*****************************************************/ |
241 | /*****************************************************/ |
242 | /* Control Servo Position */ |
242 | /* Control Servo Position */ |
243 | /*****************************************************/ |
243 | /*****************************************************/ |
244 | void CalcNickServoValue(void) |
244 | void CalcNickServoValue(void) |
245 | { |
245 | { |
246 | signed int max, min; |
246 | signed int max, min; |
247 | 247 | ||
248 | if(EE_Parameter.ServoCompInvert & SERVO_RELATIVE) // relative moving of the servo value |
248 | if(EE_Parameter.ServoCompInvert & SERVO_RELATIVE) // relative moving of the servo value |
249 | { |
249 | { |
250 | max = ((unsigned int) EE_Parameter.ServoNickMax * MULTIPLYER * 15); |
250 | max = ((unsigned int) EE_Parameter.ServoNickMax * MULTIPLYER * 15); |
251 | min = ((unsigned int) EE_Parameter.ServoNickMin * MULTIPLYER * 20); |
251 | min = ((unsigned int) EE_Parameter.ServoNickMin * MULTIPLYER * 20); |
252 | NickServoValue -= ((signed char) (Parameter_ServoNickControl - 128) / 4) * 6; |
252 | NickServoValue -= ((signed char) (Parameter_ServoNickControl - 128) / 4) * 6; |
253 | LIMIT_MIN_MAX(NickServoValue,min, max); |
253 | LIMIT_MIN_MAX(NickServoValue,min, max); |
254 | } |
254 | } |
255 | else NickServoValue = (int16_t)Parameter_ServoNickControl * (MULTIPLYER*16); // direct poti control |
255 | else NickServoValue = (int16_t)Parameter_ServoNickControl * (MULTIPLYER*16); // direct poti control |
256 | } |
256 | } |
257 | 257 | ||
258 | void CalculateServo(void) |
258 | void CalculateServo(void) |
259 | { |
259 | { |
260 | signed char cosinus, sinus; |
260 | signed char cosinus, sinus; |
261 | signed long nick, roll; |
261 | signed long nick, roll; |
262 | 262 | ||
263 | cosinus = sintab[EE_Parameter.CamOrientation + 6]; |
263 | cosinus = sintab[EE_Parameter.CamOrientation + 6]; |
264 | sinus = sintab[EE_Parameter.CamOrientation]; |
264 | sinus = sintab[EE_Parameter.CamOrientation]; |
265 | 265 | ||
266 | if(CalculateServoSignals == 1) |
266 | if(CalculateServoSignals == 1) |
267 | { |
267 | { |
268 | nick = (cosinus * IntegralNick) / 128L - (sinus * IntegralRoll) / 128L; |
268 | nick = (cosinus * IntegralNick) / 128L - (sinus * IntegralRoll) / 128L; |
269 | nick -= POI_KameraNick * 7; |
269 | nick -= POI_KameraNick * 7; |
270 | nick = ((long)EE_Parameter.ServoNickComp * nick) / 512L; |
270 | nick = ((long)Parameter_ServoNickComp * nick) / 512L; |
271 | // offset (Range from 0 to 255 * 3 = 765) |
271 | // offset (Range from 0 to 255 * 3 = 765) |
272 | if(EE_Parameter.ServoCompInvert & SERVO_RELATIVE) ServoNickOffset = NickServoValue; |
272 | if(EE_Parameter.ServoCompInvert & SERVO_RELATIVE) ServoNickOffset = NickServoValue; |
273 | else ServoNickOffset += (NickServoValue - ServoNickOffset) / EE_Parameter.ServoManualControlSpeed; |
273 | else ServoNickOffset += (NickServoValue - ServoNickOffset) / EE_Parameter.ServoManualControlSpeed; |
274 | 274 | ||
275 | if(EE_Parameter.ServoCompInvert & SERVO_NICK_INV) // inverting movement of servo |
275 | if(EE_Parameter.ServoCompInvert & SERVO_NICK_INV) // inverting movement of servo |
276 | { |
276 | { |
277 | nick = ServoNickOffset / 16 + nick; |
277 | nick = ServoNickOffset / 16 + nick; |
278 | } |
278 | } |
279 | else |
279 | else |
280 | { // inverting movement of servo |
280 | { // inverting movement of servo |
281 | nick = ServoNickOffset / 16 - nick; |
281 | nick = ServoNickOffset / 16 - nick; |
282 | } |
282 | } |
283 | if(EE_Parameter.ServoFilterNick) ServoNickValue = ((ServoNickValue * EE_Parameter.ServoFilterNick) + nick) / (EE_Parameter.ServoFilterNick + 1); |
283 | if(EE_Parameter.ServoFilterNick) ServoNickValue = ((ServoNickValue * EE_Parameter.ServoFilterNick) + nick) / (EE_Parameter.ServoFilterNick + 1); |
284 | else ServoNickValue = nick; |
284 | else ServoNickValue = nick; |
285 | // limit servo value to its parameter range definition |
285 | // limit servo value to its parameter range definition |
286 | if(ServoNickValue < ((int16_t)EE_Parameter.ServoNickMin * MULTIPLYER)) |
286 | if(ServoNickValue < ((int16_t)EE_Parameter.ServoNickMin * MULTIPLYER)) |
287 | { |
287 | { |
288 | ServoNickValue = (int16_t)EE_Parameter.ServoNickMin * MULTIPLYER; |
288 | ServoNickValue = (int16_t)EE_Parameter.ServoNickMin * MULTIPLYER; |
289 | } |
289 | } |
290 | else |
290 | else |
291 | if(ServoNickValue > ((int16_t)EE_Parameter.ServoNickMax * MULTIPLYER)) |
291 | if(ServoNickValue > ((int16_t)EE_Parameter.ServoNickMax * MULTIPLYER)) |
292 | { |
292 | { |
293 | ServoNickValue = (int16_t)EE_Parameter.ServoNickMax * MULTIPLYER; |
293 | ServoNickValue = (int16_t)EE_Parameter.ServoNickMax * MULTIPLYER; |
294 | } |
294 | } |
295 | if(PlatinenVersion < 20) CalculateServoSignals = 0; else CalculateServoSignals++; |
295 | if(PlatinenVersion < 20) CalculateServoSignals = 0; else CalculateServoSignals++; |
296 | } |
296 | } |
297 | else |
297 | else |
298 | { |
298 | { |
299 | roll = (cosinus * IntegralRoll) / 128L + (sinus * IntegralNick) / 128L; |
299 | roll = (cosinus * IntegralRoll) / 128L + (sinus * IntegralNick) / 128L; |
300 | roll = ((long)EE_Parameter.ServoRollComp * roll) / 512L; |
300 | roll = ((long)Parameter_ServoRollComp * roll) / 512L; |
301 | ServoRollOffset += ((int16_t)Parameter_ServoRollControl * (MULTIPLYER*16) - ServoRollOffset) / EE_Parameter.ServoManualControlSpeed; |
301 | ServoRollOffset += ((int16_t)Parameter_ServoRollControl * (MULTIPLYER*16) - ServoRollOffset) / EE_Parameter.ServoManualControlSpeed; |
302 | if(EE_Parameter.ServoCompInvert & SERVO_ROLL_INV) |
302 | if(EE_Parameter.ServoCompInvert & SERVO_ROLL_INV) |
303 | { // inverting movement of servo |
303 | { // inverting movement of servo |
304 | roll = ServoRollOffset / 16 + roll; |
304 | roll = ServoRollOffset / 16 + roll; |
305 | } |
305 | } |
306 | else |
306 | else |
307 | { // inverting movement of servo |
307 | { // inverting movement of servo |
308 | roll = ServoRollOffset / 16 - roll; |
308 | roll = ServoRollOffset / 16 - roll; |
309 | } |
309 | } |
310 | if(EE_Parameter.ServoFilterRoll) ServoRollValue = ((ServoRollValue * EE_Parameter.ServoFilterRoll) + roll) / (EE_Parameter.ServoFilterRoll + 1); |
310 | if(EE_Parameter.ServoFilterRoll) ServoRollValue = ((ServoRollValue * EE_Parameter.ServoFilterRoll) + roll) / (EE_Parameter.ServoFilterRoll + 1); |
311 | else ServoRollValue = roll; |
311 | else ServoRollValue = roll; |
312 | // limit servo value to its parameter range definition |
312 | // limit servo value to its parameter range definition |
313 | if(ServoRollValue < ((int16_t)EE_Parameter.ServoRollMin * MULTIPLYER)) |
313 | if(ServoRollValue < ((int16_t)EE_Parameter.ServoRollMin * MULTIPLYER)) |
314 | { |
314 | { |
315 | ServoRollValue = (int16_t)EE_Parameter.ServoRollMin * MULTIPLYER; |
315 | ServoRollValue = (int16_t)EE_Parameter.ServoRollMin * MULTIPLYER; |
316 | } |
316 | } |
317 | else |
317 | else |
318 | if(ServoRollValue > ((int16_t)EE_Parameter.ServoRollMax * MULTIPLYER)) |
318 | if(ServoRollValue > ((int16_t)EE_Parameter.ServoRollMax * MULTIPLYER)) |
319 | { |
319 | { |
320 | ServoRollValue = (int16_t)EE_Parameter.ServoRollMax * MULTIPLYER; |
320 | ServoRollValue = (int16_t)EE_Parameter.ServoRollMax * MULTIPLYER; |
321 | } |
321 | } |
322 | CalculateServoSignals = 0; |
322 | CalculateServoSignals = 0; |
323 | } |
323 | } |
324 | } |
324 | } |
325 | 325 | ||
326 | ISR(TIMER2_COMPA_vect) |
326 | ISR(TIMER2_COMPA_vect) |
327 | { |
327 | { |
328 | // frame len 22.5 ms = 14063 * 1.6 us |
328 | // frame len 22.5 ms = 14063 * 1.6 us |
329 | // stop pulse: 0.3 ms = 188 * 1.6 us |
329 | // stop pulse: 0.3 ms = 188 * 1.6 us |
330 | // min servo pulse: 0.6 ms = 375 * 1.6 us |
330 | // min servo pulse: 0.6 ms = 375 * 1.6 us |
331 | // max servo pulse: 2.4 ms = 1500 * 1.6 us |
331 | // max servo pulse: 2.4 ms = 1500 * 1.6 us |
332 | // resolution: 1500 - 375 = 1125 steps |
332 | // resolution: 1500 - 375 = 1125 steps |
333 | 333 | ||
334 | #define IRS_RUNTIME 127 |
334 | #define IRS_RUNTIME 127 |
335 | #define PPM_STOPPULSE 188 |
335 | #define PPM_STOPPULSE 188 |
336 | #define PPM_FRAMELEN (1757 * EE_Parameter.ServoNickRefresh) |
336 | #define PPM_FRAMELEN (1757 * EE_Parameter.ServoNickRefresh) |
337 | #define MINSERVOPULSE 375 |
337 | #define MINSERVOPULSE 375 |
338 | #define MAXSERVOPULSE 1500 |
338 | #define MAXSERVOPULSE 1500 |
339 | #define SERVORANGE (MAXSERVOPULSE - MINSERVOPULSE) |
339 | #define SERVORANGE (MAXSERVOPULSE - MINSERVOPULSE) |
340 | 340 | ||
341 | static uint8_t PulseOutput = 0; |
341 | static uint8_t PulseOutput = 0; |
342 | static uint16_t ServoFrameTime = 0; |
342 | static uint16_t ServoFrameTime = 0; |
343 | static uint8_t ServoIndex = 0; |
343 | static uint8_t ServoIndex = 0; |
344 | 344 | ||
345 | 345 | ||
346 | if(PlatinenVersion < 20) |
346 | if(PlatinenVersion < 20) |
347 | { |
347 | { |
348 | //--------------------------- |
348 | //--------------------------- |
349 | // Nick servo state machine |
349 | // Nick servo state machine |
350 | //--------------------------- |
350 | //--------------------------- |
351 | if(!PulseOutput) // pulse output complete |
351 | if(!PulseOutput) // pulse output complete |
352 | { |
352 | { |
353 | if(TCCR2A & (1<<COM2A0)) // we had a low pulse |
353 | if(TCCR2A & (1<<COM2A0)) // we had a low pulse |
354 | { |
354 | { |
355 | TCCR2A &= ~(1<<COM2A0);// make a high pulse |
355 | TCCR2A &= ~(1<<COM2A0);// make a high pulse |
356 | RemainingPulse = MINSERVOPULSE + SERVORANGE/2; // center position ~ 1.5ms |
356 | RemainingPulse = MINSERVOPULSE + SERVORANGE/2; // center position ~ 1.5ms |
357 | RemainingPulse += ServoNickValue - (256 / 2) * MULTIPLYER; // shift ServoNickValue to center position |
357 | RemainingPulse += ServoNickValue - (256 / 2) * MULTIPLYER; // shift ServoNickValue to center position |
358 | // range servo pulse width |
358 | // range servo pulse width |
359 | if(RemainingPulse > MAXSERVOPULSE ) RemainingPulse = MAXSERVOPULSE; // upper servo pulse limit |
359 | if(RemainingPulse > MAXSERVOPULSE ) RemainingPulse = MAXSERVOPULSE; // upper servo pulse limit |
360 | else if(RemainingPulse < MINSERVOPULSE ) RemainingPulse = MINSERVOPULSE; // lower servo pulse limit |
360 | else if(RemainingPulse < MINSERVOPULSE ) RemainingPulse = MINSERVOPULSE; // lower servo pulse limit |
361 | // accumulate time for correct update rate |
361 | // accumulate time for correct update rate |
362 | ServoFrameTime = RemainingPulse; |
362 | ServoFrameTime = RemainingPulse; |
363 | } |
363 | } |
364 | else // we had a high pulse |
364 | else // we had a high pulse |
365 | { |
365 | { |
366 | TCCR2A |= (1<<COM2A0); // make a low pulse |
366 | TCCR2A |= (1<<COM2A0); // make a low pulse |
367 | RemainingPulse = PPM_FRAMELEN - ServoFrameTime; |
367 | RemainingPulse = PPM_FRAMELEN - ServoFrameTime; |
368 | CalculateServoSignals = 1; |
368 | CalculateServoSignals = 1; |
369 | } |
369 | } |
370 | // set pulse output active |
370 | // set pulse output active |
371 | PulseOutput = 1; |
371 | PulseOutput = 1; |
372 | } |
372 | } |
373 | } // EOF Nick servo state machine |
373 | } // EOF Nick servo state machine |
374 | else |
374 | else |
375 | { |
375 | { |
376 | //----------------------------------------------------- |
376 | //----------------------------------------------------- |
377 | // PPM state machine, onboard demultiplexed by HEF4017 |
377 | // PPM state machine, onboard demultiplexed by HEF4017 |
378 | //----------------------------------------------------- |
378 | //----------------------------------------------------- |
379 | if(!PulseOutput) // pulse output complete |
379 | if(!PulseOutput) // pulse output complete |
380 | { |
380 | { |
381 | if(TCCR2A & (1<<COM2A0)) // we had a low pulse |
381 | if(TCCR2A & (1<<COM2A0)) // we had a low pulse |
382 | { |
382 | { |
383 | TCCR2A &= ~(1<<COM2A0);// make a high pulse |
383 | TCCR2A &= ~(1<<COM2A0);// make a high pulse |
384 | if(ServoIndex == 0) // if we are at the sync gap |
384 | if(ServoIndex == 0) // if we are at the sync gap |
385 | { |
385 | { |
386 | RemainingPulse = PPM_FRAMELEN - ServoFrameTime; // generate sync gap by filling time to full frame time |
386 | RemainingPulse = PPM_FRAMELEN - ServoFrameTime; // generate sync gap by filling time to full frame time |
387 | ServoFrameTime = 0; // reset servo frame time |
387 | ServoFrameTime = 0; // reset servo frame time |
388 | HEF4017Reset_ON; // enable HEF4017 reset |
388 | HEF4017Reset_ON; // enable HEF4017 reset |
389 | } |
389 | } |
390 | else // servo channels |
390 | else // servo channels |
391 | if(ServoIndex > EE_Parameter.ServoNickRefresh) |
391 | if(ServoIndex > EE_Parameter.ServoNickRefresh) |
392 | { |
392 | { |
393 | RemainingPulse = 10; // end it here |
393 | RemainingPulse = 10; // end it here |
394 | } |
394 | } |
395 | else |
395 | else |
396 | { |
396 | { |
397 | RemainingPulse = MINSERVOPULSE + SERVORANGE/2; // center position ~ 1.5ms |
397 | RemainingPulse = MINSERVOPULSE + SERVORANGE/2; // center position ~ 1.5ms |
398 | switch(ServoIndex) // map servo channels |
398 | switch(ServoIndex) // map servo channels |
399 | { |
399 | { |
400 | case 1: // Nick Compensation Servo |
400 | case 1: // Nick Compensation Servo |
401 | RemainingPulse += ServoNickValue - (256 / 2) * MULTIPLYER; // shift ServoNickValue to center position |
401 | RemainingPulse += ServoNickValue - (256 / 2) * MULTIPLYER; // shift ServoNickValue to center position |
402 | break; |
402 | break; |
403 | case 2: // Roll Compensation Servo |
403 | case 2: // Roll Compensation Servo |
404 | RemainingPulse += ServoRollValue - (256 / 2) * MULTIPLYER; // shift ServoNickValue to center position |
404 | RemainingPulse += ServoRollValue - (256 / 2) * MULTIPLYER; // shift ServoNickValue to center position |
405 | break; |
405 | break; |
406 | case 3: |
406 | case 3: |
407 | RemainingPulse += ((int16_t)Parameter_Servo3 * MULTIPLYER) - (256 / 2) * MULTIPLYER; |
407 | RemainingPulse += ((int16_t)Parameter_Servo3 * MULTIPLYER) - (256 / 2) * MULTIPLYER; |
408 | break; |
408 | break; |
409 | case 4: |
409 | case 4: |
410 | RemainingPulse += ((int16_t)Parameter_Servo4 * MULTIPLYER) - (256 / 2) * MULTIPLYER; |
410 | RemainingPulse += ((int16_t)Parameter_Servo4 * MULTIPLYER) - (256 / 2) * MULTIPLYER; |
411 | break; |
411 | break; |
412 | case 5: |
412 | case 5: |
413 | RemainingPulse += ((int16_t)Parameter_Servo5 * MULTIPLYER) - (256 / 2) * MULTIPLYER; |
413 | RemainingPulse += ((int16_t)Parameter_Servo5 * MULTIPLYER) - (256 / 2) * MULTIPLYER; |
414 | break; |
414 | break; |
415 | default: // other servo channels |
415 | default: // other servo channels |
416 | RemainingPulse += 2 * PPM_in[ServoIndex]; // add channel value, factor of 2 because timer 1 increments 3.2µs |
416 | RemainingPulse += 2 * PPM_in[ServoIndex]; // add channel value, factor of 2 because timer 1 increments 3.2µs |
417 | break; |
417 | break; |
418 | } |
418 | } |
419 | // range servo pulse width |
419 | // range servo pulse width |
420 | if(RemainingPulse > MAXSERVOPULSE) RemainingPulse = MAXSERVOPULSE; // upper servo pulse limit |
420 | if(RemainingPulse > MAXSERVOPULSE) RemainingPulse = MAXSERVOPULSE; // upper servo pulse limit |
421 | else if(RemainingPulse < MINSERVOPULSE) RemainingPulse = MINSERVOPULSE; // lower servo pulse limit |
421 | else if(RemainingPulse < MINSERVOPULSE) RemainingPulse = MINSERVOPULSE; // lower servo pulse limit |
422 | // substract stop pulse width |
422 | // substract stop pulse width |
423 | RemainingPulse -= PPM_STOPPULSE; |
423 | RemainingPulse -= PPM_STOPPULSE; |
424 | // accumulate time for correct sync gap |
424 | // accumulate time for correct sync gap |
425 | ServoFrameTime += RemainingPulse; |
425 | ServoFrameTime += RemainingPulse; |
426 | } |
426 | } |
427 | } |
427 | } |
428 | else // we had a high pulse |
428 | else // we had a high pulse |
429 | { |
429 | { |
430 | TCCR2A |= (1<<COM2A0); // make a low pulse |
430 | TCCR2A |= (1<<COM2A0); // make a low pulse |
431 | // set pulsewidth to stop pulse width |
431 | // set pulsewidth to stop pulse width |
432 | RemainingPulse = PPM_STOPPULSE; |
432 | RemainingPulse = PPM_STOPPULSE; |
433 | // accumulate time for correct sync gap |
433 | // accumulate time for correct sync gap |
434 | ServoFrameTime += RemainingPulse; |
434 | ServoFrameTime += RemainingPulse; |
435 | if((ServoActive && SenderOkay) || ServoActive == 2) HEF4017Reset_OFF; // disable HEF4017 reset |
435 | if((ServoActive && SenderOkay) || ServoActive == 2) HEF4017Reset_OFF; // disable HEF4017 reset |
436 | else HEF4017Reset_ON; |
436 | else HEF4017Reset_ON; |
437 | ServoIndex++; |
437 | ServoIndex++; |
438 | if(ServoIndex > EE_Parameter.ServoNickRefresh+1) |
438 | if(ServoIndex > EE_Parameter.ServoNickRefresh+1) |
439 | { |
439 | { |
440 | CalculateServoSignals = 1; |
440 | CalculateServoSignals = 1; |
441 | ServoIndex = 0; // reset to the sync gap |
441 | ServoIndex = 0; // reset to the sync gap |
442 | } |
442 | } |
443 | } |
443 | } |
444 | // set pulse output active |
444 | // set pulse output active |
445 | PulseOutput = 1; |
445 | PulseOutput = 1; |
446 | } |
446 | } |
447 | } // EOF PPM state machine |
447 | } // EOF PPM state machine |
448 | 448 | ||
449 | // General pulse output generator |
449 | // General pulse output generator |
450 | if(RemainingPulse > (255 + IRS_RUNTIME)) |
450 | if(RemainingPulse > (255 + IRS_RUNTIME)) |
451 | { |
451 | { |
452 | OCR2A = 255; |
452 | OCR2A = 255; |
453 | RemainingPulse -= 255; |
453 | RemainingPulse -= 255; |
454 | } |
454 | } |
455 | else |
455 | else |
456 | { |
456 | { |
457 | if(RemainingPulse > 255) // this is the 2nd last part |
457 | if(RemainingPulse > 255) // this is the 2nd last part |
458 | { |
458 | { |
459 | if((RemainingPulse - 255) < IRS_RUNTIME) |
459 | if((RemainingPulse - 255) < IRS_RUNTIME) |
460 | { |
460 | { |
461 | OCR2A = 255 - IRS_RUNTIME; |
461 | OCR2A = 255 - IRS_RUNTIME; |
462 | RemainingPulse -= 255 - IRS_RUNTIME; |
462 | RemainingPulse -= 255 - IRS_RUNTIME; |
463 | 463 | ||
464 | } |
464 | } |
465 | else // last part > ISR_RUNTIME |
465 | else // last part > ISR_RUNTIME |
466 | { |
466 | { |
467 | OCR2A = 255; |
467 | OCR2A = 255; |
468 | RemainingPulse -= 255; |
468 | RemainingPulse -= 255; |
469 | } |
469 | } |
470 | } |
470 | } |
471 | else // this is the last part |
471 | else // this is the last part |
472 | { |
472 | { |
473 | OCR2A = RemainingPulse; |
473 | OCR2A = RemainingPulse; |
474 | RemainingPulse = 0; |
474 | RemainingPulse = 0; |
475 | PulseOutput = 0; // trigger to stop pulse |
475 | PulseOutput = 0; // trigger to stop pulse |
476 | } |
476 | } |
477 | } // EOF general pulse output generator |
477 | } // EOF general pulse output generator |
478 | } |
478 | } |
479 | 479 |