Rev 1544 | Details | Compare with Previous | 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 und 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 Medien veröffentlicht werden, muss unsere Webseite "http://www.mikrokopter.de" |
||
19 | // + eindeutig als Ursprung verlinkt und genannt 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 |
||
49 | // + CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
||
50 | // + ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
||
51 | // + POSSIBILITY OF SUCH DAMAGE. |
||
52 | // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
||
53 | #include <avr/boot.h> |
||
54 | |||
55 | #include <avr/io.h> |
||
56 | #include <avr/interrupt.h> |
||
57 | #include <util/delay.h> |
||
58 | |||
59 | #include "main.h" |
||
60 | #include "timer0.h" |
||
61 | #include "timer2.h" |
||
62 | #include "uart0.h" |
||
63 | #include "uart1.h" |
||
64 | #include "led.h" |
||
65 | #include "menu.h" |
||
66 | #include "fc.h" |
||
67 | #include "rc.h" |
||
68 | #include "analog.h" |
||
69 | #include "printf_P.h" |
||
70 | #ifdef USE_KILLAGREG |
||
71 | #include "mm3.h" |
||
72 | #endif |
||
73 | #ifdef USE_NAVICTRL |
||
74 | #include "spi.h" |
||
75 | #endif |
||
76 | #ifdef USE_MK3MAG |
||
77 | #include "mk3mag.h" |
||
78 | #endif |
||
79 | #include "twimaster.h" |
||
80 | #include "eeprom.h" |
||
81 | |||
82 | |||
83 | uint8_t BoardRelease = 10; |
||
1545 | LPDunwell | 84 | uint8_t CPUType = ATMEGA644P; |
1538 | killagreg | 85 | uint8_t LowVoltageWarning = 94; |
86 | uint16_t FlightMinutes = 0, FlightMinutesTotal = 0; |
||
87 | |||
88 | uint8_t GetCPUType(void) |
||
89 | { // works only after reset or power on when the registers have default values |
||
90 | uint8_t CPUType = ATMEGA644; |
||
91 | if( (UCSR1A == 0x20) && (UCSR1C == 0x06) ) CPUType = ATMEGA644P; // initial Values for 644P after reset |
||
92 | return CPUType; |
||
93 | } |
||
94 | |||
95 | |||
96 | uint8_t GetBoardRelease(void) |
||
97 | { |
||
98 | uint8_t BoardRelease = 10; |
||
99 | // the board release is coded via the pull up or down the 2 status LED |
||
100 | |||
101 | PORTB &= ~((1 << PORTB1)|(1 << PORTB0)); // set tristate |
||
102 | DDRB &= ~((1 << DDB0)|(1 << DDB0)); // set port direction as input |
||
103 | |||
104 | _delay_loop_2(1000); // make some delay |
||
105 | |||
106 | switch( PINB & ((1<<PINB1)|(1<<PINB0)) ) |
||
107 | { |
||
108 | case 0x00: |
||
109 | BoardRelease = 10; // 1.0 |
||
110 | break; |
||
111 | case 0x01: |
||
112 | BoardRelease = 11; // 1.1 or 1.2 |
||
113 | break; |
||
114 | case 0x02: |
||
115 | BoardRelease = 20; // 2.0 |
||
116 | break; |
||
117 | case 0x03: |
||
118 | BoardRelease = 13; // 1.3 |
||
119 | break; |
||
120 | default: |
||
121 | break; |
||
122 | } |
||
123 | // set LED ports as output |
||
124 | DDRB |= (1<<DDB1)|(1<<DDB0); |
||
125 | RED_ON; |
||
126 | GRN_OFF; |
||
127 | return BoardRelease; |
||
128 | } |
||
129 | |||
130 | void LipoDetection(uint8_t print) |
||
131 | { |
||
132 | #define MIN_VOLTAGE_WARNING 93 // minimum low voltage warning to protect LiPO |
||
133 | uint16_t timer; |
||
134 | if(print) printf("\n\rBatt:"); |
||
135 | if(ParamSet.LowVoltageWarning < 50) // below 5.0 V the value is interpreted as single cell low volatage warning |
||
136 | { |
||
137 | timer = SetDelay(500); // wait at least 500 ms to get stable adc readings |
||
138 | if(print) while(!CheckDelay(timer)); |
||
139 | if(UBat < 130) // less than 13.0 V must be a 3S Lipo |
||
140 | { |
||
141 | LowVoltageWarning = 3 * ParamSet.LowVoltageWarning; |
||
142 | if(print) |
||
143 | { |
||
144 | Beep(3, 200); |
||
145 | printf(" 3 Cells "); |
||
146 | } |
||
147 | } |
||
148 | else // <= 13.0V |
||
149 | { |
||
150 | LowVoltageWarning = 4 * ParamSet.LowVoltageWarning; |
||
151 | if(print) |
||
152 | { |
||
153 | Beep(4, 200); |
||
154 | printf(" 4 Cells "); |
||
155 | } |
||
156 | } |
||
157 | } |
||
158 | else // classic settings |
||
159 | { |
||
160 | LowVoltageWarning = ParamSet.LowVoltageWarning; |
||
161 | } |
||
162 | //if(LowVoltageWarning < MIN_VOLTAGE_WARNING) LowVoltageWarning = MIN_VOLTAGE_WARNING; |
||
163 | if(print) printf("Low Warning level: %d.%dV", LowVoltageWarning/10, LowVoltageWarning%10); |
||
164 | } |
||
165 | |||
166 | int16_t main (void) |
||
167 | { |
||
168 | uint16_t timer, flighttimer; |
||
169 | uint8_t i; |
||
170 | |||
171 | // disable interrupts global |
||
172 | cli(); |
||
173 | |||
174 | // analyze hardware environment |
||
175 | CPUType = GetCPUType(); |
||
176 | BoardRelease = GetBoardRelease(); |
||
177 | |||
178 | // disable watchdog |
||
179 | MCUSR &=~(1<<WDRF); |
||
180 | WDTCSR |= (1<<WDCE)|(1<<WDE); |
||
181 | WDTCSR = 0; |
||
182 | |||
183 | BeepTime = 2000; |
||
184 | |||
185 | PPM_in[CH_GAS] = 0; |
||
186 | StickYaw = 0; |
||
187 | StickRoll = 0; |
||
188 | StickNick = 0; |
||
189 | |||
190 | RED_OFF; |
||
191 | |||
192 | // initalize modules |
||
193 | LED_Init(); |
||
194 | TIMER0_Init(); |
||
195 | TIMER2_Init(); |
||
196 | USART0_Init(); |
||
197 | if(CPUType == ATMEGA644P) USART1_Init(); |
||
198 | RC_Init(); |
||
199 | ADC_Init(); |
||
200 | I2C_Init(); |
||
201 | #ifdef USE_NAVICTRL |
||
202 | SPI_MasterInit(); |
||
203 | #endif |
||
204 | #ifdef USE_KILLAGREG |
||
205 | MM3_Init(); |
||
206 | #endif |
||
207 | #ifdef USE_MK3MAG |
||
208 | MK3MAG_Init(); |
||
209 | #endif |
||
210 | |||
211 | // enable interrupts global |
||
212 | sei(); |
||
213 | |||
214 | printf("\n\r==================================="); |
||
215 | printf("\n\rFlightControl"); |
||
216 | printf("\n\rHardware: %d.%d", BoardRelease/10, BoardRelease%10); |
||
217 | printf("\n\rthe use of this software is only permitted \n\ron original MikroKopter-Hardware"); |
||
218 | printf("\n\rwww.MikroKopter.de (c) HiSystems GmbH"); |
||
219 | printf("\n\r==================================="); |
||
220 | |||
221 | if(CPUType == ATMEGA644P) |
||
222 | printf("\r\n CPU: Atmega644p"); |
||
223 | else |
||
224 | printf("\r\n CPU: Atmega644"); |
||
225 | printf("\n\rSoftware: V%d.%d%c",VERSION_MAJOR, VERSION_MINOR, VERSION_PATCH + 'a'); |
||
226 | printf("\n\r==================================="); |
||
227 | GRN_ON; |
||
228 | |||
229 | // Parameter Set handling |
||
230 | ParamSet_Init(); |
||
231 | |||
232 | // Check connected BL-Ctrls |
||
233 | printf("\n\rFound BL-Ctrl: "); |
||
234 | motor_read = 0; |
||
235 | UpdateMotor = 0; |
||
236 | SendMotorData(); |
||
237 | while(!UpdateMotor); |
||
238 | motor_read = 0; // read the first I2C-Data |
||
239 | timer = SetDelay(2000); // set timeout to 2 seconds |
||
240 | for(i = 0; i < MAX_MOTORS; i++) |
||
241 | { |
||
242 | UpdateMotor = 0; |
||
243 | SendMotorData(); |
||
244 | while(!UpdateMotor); // wait 2 ms to finish data transfer |
||
245 | if(Mixer.Motor[i][MIX_GAS] > 0) // wait max 2 sec for the BL-Ctrls to wake up |
||
246 | { |
||
247 | while(!CheckDelay(timer) && !(Motor[i].Present) ) // while not timeout and motor is not present |
||
248 | { |
||
249 | UpdateMotor = 0; |
||
250 | SendMotorData(); |
||
251 | while(!UpdateMotor); // wait 2 ms |
||
252 | } |
||
253 | } |
||
254 | if(Motor[i].Present) printf("%d ",i+1); |
||
255 | } |
||
256 | for(i = 0; i < MAX_MOTORS; i++) |
||
257 | { |
||
258 | if(!Motor[i].Present && Mixer.Motor[i][MIX_GAS] > 0) |
||
259 | { |
||
260 | printf("\n\r\n\r!! MISSING BL-CTRL: %d !!",i+1); |
||
261 | Servo_On(); // just in case the FC would be used as camera-stabilizer |
||
262 | } |
||
263 | Motor[i].Error = 0; |
||
264 | } |
||
265 | printf("\n\r==================================="); |
||
266 | SendMotorData(); |
||
267 | |||
268 | //wait for a short time (otherwise the RC channel check won't work below) |
||
269 | timer = SetDelay(500); |
||
270 | while(!CheckDelay(timer)); |
||
271 | |||
272 | if(ParamSet.Config0 & CFG0_AIRPRESS_SENSOR) |
||
273 | { |
||
274 | printf("\n\rCalibrating air pressure sensor.."); |
||
275 | timer = SetDelay(1000); |
||
276 | SearchAirPressureOffset(); |
||
277 | while (!CheckDelay(timer)); |
||
278 | printf("OK\n\r"); |
||
279 | } |
||
280 | |||
281 | #ifdef USE_NAVICTRL |
||
1545 | LPDunwell | 282 | printf("\n\rSupport for NaviCtrl"); |
283 | #ifdef USE_RC_DSL |
||
284 | printf("\r\nSupport for DSL RC at 2nd UART"); |
||
285 | #endif |
||
286 | #ifdef USE_RC_SPECTRUM |
||
287 | printf("\r\nSupport for SPEKTRUM RC at 2nd UART"); |
||
288 | #endif |
||
289 | #ifdef USE_RC_JENNIC // LPD: FC_JN_Receiver support added |
||
290 | printf("\r\nSupport for JENNIC RC at 2nd UART"); |
||
291 | #endif |
||
1538 | killagreg | 292 | #endif |
293 | |||
294 | #ifdef USE_KILLAGREG |
||
1545 | LPDunwell | 295 | printf("\n\rSupport for MicroMag3 Compass"); |
1538 | killagreg | 296 | #endif |
297 | |||
298 | #ifdef USE_MK3MAG |
||
1545 | LPDunwell | 299 | printf("\n\rSupport for MK3MAG Compass"); |
1538 | killagreg | 300 | #endif |
301 | |||
302 | #if (defined (USE_KILLAGREG) || defined (USE_MK3MAG)) |
||
1545 | LPDunwell | 303 | if(CPUType == ATMEGA644P) printf("\n\rSupport for GPS at 2nd UART"); |
304 | else printf("\n\rSupport for GPS at 1st UART"); |
||
1538 | killagreg | 305 | #endif |
306 | |||
307 | // init variables |
||
308 | SetNeutral(NO_ACC_CALIB); |
||
309 | |||
310 | RED_OFF; |
||
311 | |||
312 | BeepTime = 2000; |
||
313 | ExternControl.Digital[0] = 0x55; |
||
314 | |||
315 | |||
316 | FlightMinutes = GetParamWord(PID_FLIGHT_MINUTES); |
||
317 | FlightMinutesTotal = GetParamWord(PID_FLIGHT_MINUTES_TOTAL); |
||
318 | flighttimer = 0; |
||
319 | if( (FlightMinutesTotal == 0xFFFF) || (FlightMinutes == 0xFFFF) ) |
||
320 | { |
||
321 | FlightMinutes = 0; |
||
322 | FlightMinutesTotal = 0; |
||
323 | } |
||
324 | printf("\n\rFlight-time %u min Total:%u min", FlightMinutes, FlightMinutesTotal); |
||
325 | |||
326 | |||
327 | printf("\n\rControl: "); |
||
328 | if (ParamSet.Config0 & CFG0_HEADING_HOLD) printf("HeadingHold"); |
||
329 | else printf("Neutral (ACC-Mode)"); |
||
330 | |||
331 | LCD_Clear(); |
||
332 | I2CTimeout = 5000; |
||
333 | LipoDetection(1); |
||
334 | printf("\n\r===================================\n\r"); |
||
335 | |||
336 | timer = SetDelay(2000); |
||
337 | |||
338 | // begin of main loop |
||
339 | while (1) |
||
340 | { |
||
341 | if(UpdateMotor && ADReady) // control interval |
||
342 | { |
||
343 | UpdateMotor = 0; // reset Flag, is enabled every 2 ms by ISR of timer0 |
||
344 | |||
345 | //J4HIGH; |
||
346 | MotorControl(); |
||
347 | //J4LOW; |
||
348 | |||
349 | SendMotorData(); // the flight control code |
||
350 | RED_OFF; |
||
351 | |||
352 | if(RC_Quality) RC_Quality--; |
||
353 | else PPM_INPUT_ON; // if RC-Quality is lost, enable PPM input (could be disabled by a receiver on uart1) |
||
354 | |||
355 | #ifdef USE_NAVICTRL |
||
356 | if(NCDataOkay) |
||
357 | { |
||
358 | if(--NCDataOkay == 0) // no data from NC |
||
359 | { // set gps control sticks neutral |
||
360 | GPSStickNick = 0; |
||
361 | GPSStickRoll = 0; |
||
362 | NCSerialDataOkay = 0; |
||
363 | NCGpsZ = 0; |
||
364 | } |
||
365 | } |
||
366 | #endif |
||
367 | |||
368 | if(!--I2CTimeout || MissingMotor) // try to reset the i2c if motor is missing ot timeout |
||
369 | { |
||
370 | RED_ON; |
||
371 | if(!I2CTimeout) |
||
372 | { |
||
373 | I2C_Reset(); |
||
374 | I2CTimeout = 5; |
||
375 | DebugOut.Analog[28]++; // I2C-Error |
||
376 | } |
||
377 | if((BeepModulation == 0xFFFF) && (MKFlags & MKFLAG_MOTOR_RUN) ) |
||
378 | { |
||
379 | BeepTime = 10000; // 1 second |
||
380 | BeepModulation = 0x0080; |
||
381 | } |
||
382 | } |
||
383 | else |
||
384 | { |
||
385 | RED_OFF; |
||
386 | } |
||
387 | |||
388 | // allow Serial Data Transmit if motors must not updated or motors are not running |
||
389 | if( !UpdateMotor || !(MKFlags & MKFLAG_MOTOR_RUN) ) |
||
390 | { |
||
391 | USART0_TransmitTxData(); |
||
392 | } |
||
393 | USART0_ProcessRxData(); |
||
394 | |||
395 | if(CheckDelay(timer)) |
||
396 | { |
||
397 | timer += 20; // every 20 ms |
||
398 | if(PcAccess) PcAccess--; |
||
399 | else |
||
400 | { |
||
401 | ExternControl.Config = 0; |
||
402 | ExternStickNick= 0; |
||
403 | ExternStickRoll = 0; |
||
404 | ExternStickYaw = 0; |
||
405 | if((BeepModulation == 0xFFFF) && (RC_Quality == 0)) |
||
406 | { |
||
407 | BeepTime = 15000; // 1.5 seconds |
||
408 | BeepModulation = 0x0C00; |
||
409 | } |
||
410 | } |
||
411 | if(UBat < LowVoltageWarning) |
||
412 | { |
||
413 | MKFlags |= MKFLAG_LOWBAT; |
||
414 | BeepModulation = 0x0300; |
||
415 | if(!BeepTime ) |
||
416 | { |
||
417 | BeepTime = 6000; // 0.6 seconds |
||
418 | } |
||
419 | } |
||
420 | else |
||
421 | { |
||
422 | MKFlags &= ~MKFLAG_LOWBAT; |
||
423 | } |
||
424 | #ifdef USE_NAVICTRL |
||
425 | SPI_StartTransmitPacket(); |
||
426 | SendSPI = 4; |
||
427 | #endif |
||
428 | |||
429 | if(!(MKFlags & MKFLAG_MOTOR_RUN)) flighttimer = 1450; // 0.5 minutes round up |
||
430 | if(++flighttimer == 2930) // one minute |
||
431 | { |
||
432 | flighttimer = 0; |
||
433 | FlightMinutesTotal++; |
||
434 | FlightMinutes++; |
||
435 | SetParamWord(PID_FLIGHT_MINUTES_TOTAL, FlightMinutesTotal); |
||
436 | SetParamWord(PID_FLIGHT_MINUTES, FlightMinutes); |
||
437 | timer = SetDelay(20); // in case "timer += 20;" will not work |
||
438 | } |
||
439 | }// EOF CheckDelay(timer) |
||
440 | |||
441 | LED_Update(); |
||
442 | } |
||
443 | |||
444 | #ifdef USE_NAVICTRL |
||
445 | if(!SendSPI) |
||
446 | { // SendSPI is decremented in timer0.c with a rate of 9.765 kHz. |
||
447 | // within the SPI_TransmitByte() routine the value is set to 4. |
||
448 | // I.e. the SPI_TransmitByte() is called at a rate of 9.765 kHz/4= 2441.25 Hz, |
||
449 | // and therefore the time of transmission of a complete spi-packet (32 bytes) is 32*4/9.765 kHz = 13.1 ms. |
||
450 | SPI_TransmitByte(); |
||
451 | } |
||
452 | #endif |
||
453 | } |
||
454 | return (1); |
||
455 | } |
||
456 |