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