Go to most recent revision | Details | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
1538 | killagreg | 1 | // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
2 | // + Copyright (c) Holger Buss, Ingo Busker |
||
3 | // + Nur für den privaten Gebrauch |
||
4 | // + www.MikroKopter.com |
||
5 | // + porting the sources to other systems or using the software on other systems (except hardware from www.mikrokopter.de) is not allowed |
||
6 | // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
||
7 | // + Es gilt für das gesamte Projekt (Hardware, Software, Binärfiles, Sourcecode und Dokumentation), |
||
8 | // + dass eine Nutzung (auch auszugsweise) nur für den privaten (nicht-kommerziellen) Gebrauch zulässig ist. |
||
9 | // + Sollten direkte oder indirekte kommerzielle Absichten verfolgt werden, ist mit uns (info@mikrokopter.de) Kontakt |
||
10 | // + bzgl. der Nutzungsbedingungen aufzunehmen. |
||
11 | // + Eine kommerzielle Nutzung ist z.B.Verkauf von MikroKoptern, Bestückung und Verkauf von Platinen oder Bausätzen, |
||
12 | // + Verkauf von Luftbildaufnahmen, usw. |
||
13 | // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
||
14 | // + Werden Teile des Quellcodes (mit oder ohne Modifikation) weiterverwendet oder veröffentlicht, |
||
15 | // + unterliegen sie auch diesen Nutzungsbedingungen und diese Nutzungsbedingungen incl. Copyright müssen dann beiliegen |
||
16 | // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
||
17 | // + Sollte die Software (auch auszugesweise) oder sonstige Informationen des MikroKopter-Projekts |
||
18 | // + auf anderen Webseiten oder sonstigen Medien veröffentlicht werden, muss unsere Webseite "http://www.mikrokopter.de" |
||
19 | // + eindeutig als Ursprung verlinkt werden |
||
20 | // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
||
21 | // + Keine Gewähr auf Fehlerfreiheit, Vollständigkeit oder Funktion |
||
22 | // + Benutzung auf eigene Gefahr |
||
23 | // + Wir übernehmen keinerlei Haftung für direkte oder indirekte Personen- oder Sachschäden |
||
24 | // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
||
25 | // + Die Portierung der Software (oder Teile davon) auf andere Systeme (ausser der Hardware von www.mikrokopter.de) ist nur |
||
26 | // + mit unserer Zustimmung zulässig |
||
27 | // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
||
28 | // + Die Funktion printf_P() unterliegt ihrer eigenen Lizenz und ist hiervon nicht betroffen |
||
29 | // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
||
30 | // + Redistributions of source code (with or without modifications) must retain the above copyright notice, |
||
31 | // + this list of conditions and the following disclaimer. |
||
32 | // + * Neither the name of the copyright holders nor the names of contributors may be used to endorse or promote products derived |
||
33 | // + from this software without specific prior written permission. |
||
34 | // + * The use of this project (hardware, software, binary files, sources and documentation) is only permittet |
||
35 | // + for non-commercial use (directly or indirectly) |
||
36 | // + Commercial use (for excample: selling of MikroKopters, selling of PCBs, assembly, ...) is only permitted |
||
37 | // + with our written permission |
||
38 | // + * If sources or documentations are redistributet on other webpages, out webpage (http://www.MikroKopter.de) must be |
||
39 | // + clearly linked as origin |
||
40 | // + * porting to systems other than hardware from www.mikrokopter.de is not allowed |
||
41 | // + THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
||
42 | // + AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
||
43 | // + IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
||
44 | // + ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE |
||
45 | // + LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
||
46 | // + CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
||
47 | // + SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
||
48 | // + INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN// + CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
||
49 | // + ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
||
50 | // + POSSIBILITY OF SUCH DAMAGE. |
||
51 | // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
||
52 | #include <avr/io.h> |
||
53 | #include <avr/interrupt.h> |
||
54 | #include "fc.h" |
||
55 | #include "eeprom.h" |
||
56 | #include "uart0.h" |
||
57 | #include "main.h" |
||
58 | #include "rc.h" |
||
59 | |||
60 | volatile int16_t ServoNickValue = 0; |
||
61 | volatile int16_t ServoRollValue = 0; |
||
62 | volatile uint8_t ServoActive = 0; |
||
63 | |||
64 | #define HEF4017R_ON PORTC |= (1<<PORTC6) |
||
65 | #define HEF4017R_OFF PORTC &= ~(1<<PORTC6) |
||
66 | |||
67 | |||
68 | /*****************************************************/ |
||
69 | /* Initialize Timer 2 */ |
||
70 | /*****************************************************/ |
||
71 | // The timer 2 is used to generate the PWM at PD7 (J7) |
||
72 | // to control a camera servo for nick compensation. |
||
73 | void TIMER2_Init(void) |
||
74 | { |
||
75 | uint8_t sreg = SREG; |
||
76 | |||
77 | // disable all interrupts before reconfiguration |
||
78 | cli(); |
||
79 | |||
80 | DDRD &= ~(1<<DDD7); // set PD7 as input of the PWM for servo |
||
81 | //DDRD |= (1<<DDD7); // set PD7 as output of the PWM for servo |
||
82 | PORTD &= ~(1<<PORTD7); // set PD7 to low |
||
83 | |||
84 | DDRC |= (1<<DDC6); // set PC6 as output (Reset for HEF4017) |
||
85 | //PORTC &= ~(1<<PORTC6); // set PC6 to low |
||
86 | HEF4017R_ON; // enable reset |
||
87 | |||
88 | // Timer/Counter 2 Control Register A |
||
89 | |||
90 | // Timer Mode is FastPWM with timer reload at OCR2A (Bits: WGM22 = 1, WGM21 = 1, WGM20 = 1) |
||
91 | // PD7: Normal port operation, OC2A disconnected, (Bits: COM2A1 = 0, COM2A0 = 0) |
||
92 | // PD6: Normal port operation, OC2B disconnected, (Bits: COM2B1 = 0, COM2B0 = 0) |
||
93 | TCCR2A &= ~((1<<COM2A1)|(1<<COM2A0)|(1<<COM2B1)|(1<<COM2B0)); |
||
94 | TCCR2A |= (1<<WGM21)|(1<<WGM20); |
||
95 | |||
96 | // Timer/Counter 2 Control Register B |
||
97 | |||
98 | // Set clock divider for timer 2 to SYSKLOCK/32 = 20MHz / 32 = 625 kHz |
||
99 | // The timer increments from 0x00 to 0xFF with an update rate of 625 kHz or 1.6 us |
||
100 | // hence the timer overflow interrupt frequency is 625 kHz / 256 = 2.44 kHz or 0.4096 ms |
||
101 | |||
102 | // divider 32 (Bits: CS022 = 0, CS21 = 1, CS20 = 1) |
||
103 | TCCR2B &= ~((1<<FOC2A)|(1<<FOC2B)|(1<<CS22)); |
||
104 | TCCR2B |= (1<<CS21)|(1<<CS20)|(1<<WGM22); |
||
105 | |||
106 | // Initialize the Timer/Counter 2 Register |
||
107 | TCNT2 = 0; |
||
108 | |||
109 | // Initialize the Output Compare Register A used for PWM generation on port PD7. |
||
110 | OCR2A = 255; |
||
111 | TCCR2A |= (1<<COM2A1); // set or clear at compare match depends on value of COM2A0 |
||
112 | |||
113 | // Timer/Counter 2 Interrupt Mask Register |
||
114 | // Enable timer output compare match A Interrupt only |
||
115 | TIMSK2 &= ~((1<<OCIE2B)|(1<<TOIE2)); |
||
116 | TIMSK2 |= (1<<OCIE2A); |
||
117 | |||
118 | SREG = sreg; |
||
119 | } |
||
120 | |||
121 | |||
122 | void Servo_On(void) |
||
123 | { |
||
124 | ServoActive = 1; |
||
125 | DDRD |= (1<<DDD7); // set PD7 as output of the PWM for servo |
||
126 | } |
||
127 | |||
128 | void Servo_Off(void) |
||
129 | { |
||
130 | ServoActive = 0; |
||
131 | DDRD &= ~(1<<DDD7); // set PD7 as input |
||
132 | HEF4017R_ON; // enable reset |
||
133 | } |
||
134 | |||
135 | /*****************************************************/ |
||
136 | /* Control Servo Position */ |
||
137 | /*****************************************************/ |
||
138 | |||
139 | ISR(TIMER2_COMPA_vect) |
||
140 | { |
||
141 | |||
142 | // frame len 22.5 ms = 14063 * 1.6 us |
||
143 | // stop pulse: 0.3 ms = 188 * 1.6 us |
||
144 | // min servo pulse: 0.6 ms = 375 * 1.6 us |
||
145 | // max servo pulse: 2.4 ms = 1500 * 1.6 us |
||
146 | // resolution: 1500 - 375 = 1125 steps |
||
147 | |||
148 | #define IRS_RUNTIME 127 |
||
149 | #define PPM_STOPPULSE 188 |
||
150 | //#define PPM_FRAMELEN 14063 |
||
151 | #define PPM_FRAMELEN (1757 * ParamSet.ServoRefresh) // 22.5 ms / 8 Channels = 2.8125ms per Servo Channel |
||
152 | #define MINSERVOPULSE 375 |
||
153 | #define MAXSERVOPULSE 1500 |
||
154 | #define SERVORANGE (MAXSERVOPULSE - MINSERVOPULSE) |
||
155 | |||
156 | static uint8_t PulseOutput = 0; |
||
157 | static uint16_t RemainingPulse = 0; |
||
158 | static uint16_t ServoFrameTime = 0; |
||
159 | static uint8_t ServoIndex = 0; |
||
160 | |||
161 | #define MULTIPLYER 4 |
||
162 | static int16_t ServoNickOffset = (255 / 2) * MULTIPLYER; // initial value near center position |
||
163 | static int16_t ServoRollOffset = (255 / 2) * MULTIPLYER; // initial value near center position |
||
164 | |||
165 | if(BoardRelease < 20) |
||
166 | { |
||
167 | //--------------------------- |
||
168 | // Nick servo state machine |
||
169 | //--------------------------- |
||
170 | if(!PulseOutput) // pulse output complete |
||
171 | { |
||
172 | if(TCCR2A & (1<<COM2A0)) // we had a low pulse |
||
173 | { |
||
174 | TCCR2A &= ~(1<<COM2A0);// make a high pulse |
||
175 | RemainingPulse = MINSERVOPULSE + SERVORANGE/2; // center position ~ 1.5ms |
||
176 | |||
177 | ServoNickOffset = (ServoNickOffset * 3 + (int16_t)FCParam.ServoNickControl * MULTIPLYER) / 4; // lowpass offset |
||
178 | ServoNickValue = ServoNickOffset; // offset (Range from 0 to 255 * 3 = 765) |
||
179 | if(ParamSet.ServoCompInvert & 0x01) |
||
180 | { // inverting movement of servo |
||
181 | ServoNickValue += (int16_t)( ( (int32_t)ParamSet.ServoNickComp * MULTIPLYER * (IntegralGyroNick / 128L ) ) / (256L) ); |
||
182 | } |
||
183 | else |
||
184 | { // non inverting movement of servo |
||
185 | ServoNickValue -= (int16_t)( ( (int32_t)ParamSet.ServoNickComp * MULTIPLYER * (IntegralGyroNick / 128L ) ) / (256L) ); |
||
186 | } |
||
187 | // limit servo value to its parameter range definition |
||
188 | if(ServoNickValue < ((int16_t)ParamSet.ServoNickMin * MULTIPLYER) ) |
||
189 | { |
||
190 | ServoNickValue = (int16_t)ParamSet.ServoNickMin * MULTIPLYER; |
||
191 | } |
||
192 | else |
||
193 | if(ServoNickValue > ((int16_t)ParamSet.ServoNickMax * MULTIPLYER) ) |
||
194 | { |
||
195 | ServoNickValue = (int16_t)ParamSet.ServoNickMax * MULTIPLYER; |
||
196 | } |
||
197 | |||
198 | RemainingPulse += ServoNickValue - (256 / 2) * MULTIPLYER; // shift ServoNickValue to center position |
||
199 | |||
200 | ServoNickValue /= MULTIPLYER; |
||
201 | |||
202 | // range servo pulse width |
||
203 | if(RemainingPulse > MAXSERVOPULSE ) RemainingPulse = MAXSERVOPULSE; // upper servo pulse limit |
||
204 | else if(RemainingPulse < MINSERVOPULSE ) RemainingPulse = MINSERVOPULSE; // lower servo pulse limit |
||
205 | // accumulate time for correct update rate |
||
206 | ServoFrameTime = RemainingPulse; |
||
207 | } |
||
208 | else // we had a high pulse |
||
209 | { |
||
210 | TCCR2A |= (1<<COM2A0); // make a low pulse |
||
211 | RemainingPulse = PPM_FRAMELEN - ServoFrameTime; |
||
212 | } |
||
213 | // set pulse output active |
||
214 | PulseOutput = 1; |
||
215 | } |
||
216 | } // EOF Nick servo state machine |
||
217 | else |
||
218 | { |
||
219 | //----------------------------------------------------- |
||
220 | // PPM state machine, onboard demultiplexed by HEF4017 |
||
221 | //----------------------------------------------------- |
||
222 | if(!PulseOutput) // pulse output complete |
||
223 | { |
||
224 | if(TCCR2A & (1<<COM2A0)) // we had a low pulse |
||
225 | { |
||
226 | TCCR2A &= ~(1<<COM2A0);// make a high pulse |
||
227 | |||
228 | if(ServoIndex == 0) // if we are at the sync gap |
||
229 | { |
||
230 | RemainingPulse = PPM_FRAMELEN - ServoFrameTime; // generate sync gap by filling time to full frame time |
||
231 | ServoFrameTime = 0; // reset servo frame time |
||
232 | HEF4017R_ON; // enable HEF4017 reset |
||
233 | } |
||
234 | else // servo channels |
||
235 | { |
||
236 | RemainingPulse = MINSERVOPULSE + SERVORANGE/2; // center position ~ 1.5ms |
||
237 | switch(ServoIndex) // map servo channels |
||
238 | { |
||
239 | case 1: // Nick Compensation Servo |
||
240 | ServoNickOffset = (ServoNickOffset * 3 + (int16_t)FCParam.ServoNickControl * MULTIPLYER) / 4; // lowpass offset |
||
241 | ServoNickValue = ServoNickOffset; // offset (Range from 0 to 255 * 3 = 765) |
||
242 | if(ParamSet.ServoCompInvert & 0x01) |
||
243 | { // inverting movement of servo |
||
244 | ServoNickValue += (int16_t)( ( (int32_t)ParamSet.ServoNickComp * MULTIPLYER * (IntegralGyroNick / 128L ) ) / (256L) ); |
||
245 | } |
||
246 | else |
||
247 | { // non inverting movement of servo |
||
248 | ServoNickValue -= (int16_t)( ( (int32_t)ParamSet.ServoNickComp * MULTIPLYER * (IntegralGyroNick / 128L ) ) / (256L) ); |
||
249 | } |
||
250 | // limit servo value to its parameter range definition |
||
251 | if(ServoNickValue < ((int16_t)ParamSet.ServoNickMin * MULTIPLYER) ) |
||
252 | { |
||
253 | ServoNickValue = (int16_t)ParamSet.ServoNickMin * MULTIPLYER; |
||
254 | } |
||
255 | else |
||
256 | if(ServoNickValue > ((int16_t)ParamSet.ServoNickMax * MULTIPLYER) ) |
||
257 | { |
||
258 | ServoNickValue = (int16_t)ParamSet.ServoNickMax * MULTIPLYER; |
||
259 | } |
||
260 | |||
261 | RemainingPulse += ServoNickValue - (256 / 2) * MULTIPLYER; // shift ServoNickValue to center position |
||
262 | |||
263 | ServoNickValue /= MULTIPLYER; |
||
264 | break; |
||
265 | |||
266 | |||
267 | case 2: // Roll Compensation Servo |
||
268 | ServoRollOffset = (ServoRollOffset * 3 + (int16_t)FCParam.ServoRollControl * MULTIPLYER) / 4; // lowpass offset |
||
269 | ServoRollValue = ServoRollOffset; // offset (Range from 0 to 255 * 3 = 765) |
||
270 | if(ParamSet.ServoCompInvert & 0x02) |
||
271 | { // inverting movement of servo |
||
272 | ServoRollValue += (int16_t)( ( (int32_t)ParamSet.ServoRollComp * MULTIPLYER * (IntegralGyroRoll / 128L ) ) / (256L) ); |
||
273 | } |
||
274 | else |
||
275 | { // non inverting movement of servo |
||
276 | ServoRollValue -= (int16_t)( ( (int32_t)ParamSet.ServoRollComp * MULTIPLYER * (IntegralGyroRoll / 128L ) ) / (256L) ); |
||
277 | } |
||
278 | // limit servo value to its parameter range definition |
||
279 | if(ServoRollValue < ((int16_t)ParamSet.ServoRollMin * MULTIPLYER) ) |
||
280 | { |
||
281 | ServoRollValue = (int16_t)ParamSet.ServoRollMin * MULTIPLYER; |
||
282 | } |
||
283 | else |
||
284 | if(ServoRollValue > ((int16_t)ParamSet.ServoRollMax * MULTIPLYER) ) |
||
285 | { |
||
286 | ServoRollValue = (int16_t)ParamSet.ServoRollMax * MULTIPLYER; |
||
287 | } |
||
288 | RemainingPulse += ServoRollValue - (256 / 2) * MULTIPLYER; // shift ServoRollValue to center position |
||
289 | ServoRollValue /= MULTIPLYER; |
||
290 | break; |
||
291 | |||
292 | default: // other servo channels |
||
293 | RemainingPulse += 2 * PPM_in[ServoIndex]; // add channel value, factor of 2 because timer 1 increments 3.2µs |
||
294 | break; |
||
295 | } |
||
296 | // range servo pulse width |
||
297 | if(RemainingPulse > MAXSERVOPULSE ) RemainingPulse = MAXSERVOPULSE; // upper servo pulse limit |
||
298 | else if(RemainingPulse < MINSERVOPULSE ) RemainingPulse = MINSERVOPULSE; // lower servo pulse limit |
||
299 | // substract stop pulse width |
||
300 | RemainingPulse -= PPM_STOPPULSE; |
||
301 | // accumulate time for correct sync gap |
||
302 | ServoFrameTime += RemainingPulse; |
||
303 | } |
||
304 | } |
||
305 | else // we had a high pulse |
||
306 | { |
||
307 | TCCR2A |= (1<<COM2A0); // make a low pulse |
||
308 | // set pulsewidth to stop pulse width |
||
309 | RemainingPulse = PPM_STOPPULSE; |
||
310 | // accumulate time for correct sync gap |
||
311 | ServoFrameTime += RemainingPulse; |
||
312 | if(ServoActive && RC_Quality > 180) HEF4017R_OFF; // disable HEF4017 reset |
||
313 | else HEF4017R_ON; // enable reset |
||
314 | ServoIndex++; // change to next servo channel |
||
315 | if(ServoIndex > ParamSet.ServoRefresh) ServoIndex = 0; // reset to the sync gap |
||
316 | } |
||
317 | // set pulse output active |
||
318 | PulseOutput = 1; |
||
319 | } |
||
320 | } // EOF PPM state machine |
||
321 | |||
322 | // General pulse output generator |
||
323 | if(RemainingPulse > (255 + IRS_RUNTIME)) |
||
324 | { |
||
325 | OCR2A = 255; |
||
326 | RemainingPulse -= 255; |
||
327 | } |
||
328 | else |
||
329 | { |
||
330 | if(RemainingPulse > 255) // this is the 2nd last part |
||
331 | { |
||
332 | if((RemainingPulse - 255) < IRS_RUNTIME) |
||
333 | { |
||
334 | OCR2A = 255 - IRS_RUNTIME; |
||
335 | RemainingPulse -= 255 - IRS_RUNTIME; |
||
336 | |||
337 | } |
||
338 | else // last part > ISR_RUNTIME |
||
339 | { |
||
340 | OCR2A = 255; |
||
341 | RemainingPulse -= 255; |
||
342 | } |
||
343 | } |
||
344 | else // this is the last part |
||
345 | { |
||
346 | OCR2A = RemainingPulse; |
||
347 | RemainingPulse = 0; |
||
348 | PulseOutput = 0; // trigger to stop pulse |
||
349 | } |
||
350 | } // EOF general pulse output generator |
||
351 | |||
352 | } |