Rev 2135 | Details | Compare with Previous | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
2108 | - | 1 | #include <avr/boot.h> |
2 | #include <avr/io.h> |
||
3 | #include <avr/interrupt.h> |
||
2132 | - | 4 | #include <avr/wdt.h> |
2108 | - | 5 | #include <util/delay.h> |
6 | |||
7 | #include "timer0.h" |
||
8 | #include "timer2.h" |
||
9 | #include "uart0.h" |
||
10 | #include "output.h" |
||
11 | #include "attitude.h" |
||
12 | #include "commands.h" |
||
13 | #include "flight.h" |
||
14 | #include "rc.h" |
||
15 | #include "analog.h" |
||
16 | #include "configuration.h" |
||
17 | #include "controlMixer.h" |
||
18 | #include "eeprom.h" |
||
19 | #include "printf_P.h" |
||
20 | |||
2132 | - | 21 | uint8_t resetFlag = 0; |
22 | |||
23 | void reset(void) { |
||
24 | resetFlag = 1; |
||
25 | wdt_enable(WDTO_15MS); |
||
26 | while (1) |
||
27 | ; |
||
28 | } |
||
29 | |||
2108 | - | 30 | int16_t main(void) { |
2132 | - | 31 | uint16_t timer = 0; |
2108 | - | 32 | |
2132 | - | 33 | #ifdef DO_PROFILE |
34 | static uint8_t profileTimer; |
||
35 | #endif |
||
2108 | - | 36 | |
2135 | - | 37 | // Set up LED output. This is here and not in configuration.c because the setBoardRelease function where it used to reside no longer exists. |
38 | DDRB |= ((1<<DDB4)|(1<<DDB5)); |
||
39 | PORTB &= ~((1<<DDB4)|(1<<DDB5)); |
||
40 | |||
2132 | - | 41 | // disable interrupts global |
42 | cli(); |
||
2108 | - | 43 | |
2133 | - | 44 | // wdt_enable(WDTO_2000MS); |
45 | wdt_disable(); |
||
2108 | - | 46 | |
2132 | - | 47 | // initalize modules |
48 | output_init(); |
||
49 | timer0_init(); |
||
50 | timer2_init(); |
||
51 | usart0_init(); |
||
52 | RC_Init(); |
||
53 | analog_init(); |
||
2108 | - | 54 | |
2132 | - | 55 | // Parameter Set handling |
56 | IMUConfig_readOrDefault(); |
||
57 | channelMap_readOrDefault(); |
||
58 | rcTrim_readOrDefault(); |
||
59 | paramSet_readOrDefault(); |
||
2108 | - | 60 | |
2132 | - | 61 | // enable interrupts global |
62 | sei(); |
||
2108 | - | 63 | |
2132 | - | 64 | controlMixer_setNeutral(); |
2108 | - | 65 | |
2132 | - | 66 | // Cal. attitude sensors and reset integrals. |
67 | attitude_setNeutral(); |
||
2108 | - | 68 | |
2132 | - | 69 | // This is not a busy-wait operation and should be OK. |
70 | beep(2000); |
||
2108 | - | 71 | |
2132 | - | 72 | while (1) { |
73 | if (runFlightControl) { // control interval |
||
74 | runFlightControl = 0; // reset Flag, is enabled every 2 ms by ISR of timer0 |
||
2108 | - | 75 | |
2133 | - | 76 | /* |
2132 | - | 77 | if (!resetFlag) { |
78 | wdt_reset(); |
||
79 | } |
||
2133 | - | 80 | */ |
2108 | - | 81 | |
2132 | - | 82 | if (sensorDataReady != ALL_DATA_READY) { |
83 | // Analog data should have been ready but is not!! |
||
84 | debugOut.digital[0] |= DEBUG_MAINLOOP_TIMER; |
||
85 | } else { |
||
86 | debugOut.digital[0] &= ~DEBUG_MAINLOOP_TIMER; |
||
87 | } |
||
88 | PD2HIGH; |
||
89 | // This is probably the correct order: |
||
90 | // The attitude computation should not depend on anything from control (except maybe the estimation of control activity level) |
||
91 | // The control may depend on attitude - for example, attitude control uses pitch and roll angles, compass control uses yaw angle etc. |
||
92 | // Flight control uses results from both. |
||
93 | calculateFlightAttitude(); |
||
94 | controlMixer_periodicTask(); |
||
95 | commands_handleCommands(); |
||
96 | flight_control(); |
||
97 | PD2LOW; |
||
2108 | - | 98 | |
2132 | - | 99 | // Allow Serial Data Transmit if motors must not updated or motors are not running |
100 | if (!runFlightControl || !isFlying) { |
||
101 | usart0_transmitTxData(); |
||
102 | } |
||
2108 | - | 103 | |
2132 | - | 104 | usart0_processRxData(); |
2108 | - | 105 | |
2132 | - | 106 | static uint8_t aboveWarningLimitVoltageSeen = 0; |
2109 | - | 107 | |
2132 | - | 108 | if (checkDelay(timer)) { |
109 | if (UBat >= staticParams.batteryWarningVoltage) { |
||
110 | aboveWarningLimitVoltageSeen = 1; |
||
111 | } else { // If we are above USB voltage, or if we have once been above warning voltage |
||
112 | if (aboveWarningLimitVoltageSeen || UBat > UBAT_AT_5V) { |
||
113 | beepBatteryAlarm(); |
||
114 | } |
||
115 | } |
||
2139 | - | 116 | //calculateFeaturedServoValues(); |
2132 | - | 117 | timer = setDelay(20); // every 20 ms |
118 | } |
||
119 | |||
120 | output_update(); |
||
121 | |||
122 | if (runFlightControl) { // Time for the next iteration was up before the current finished. Signal error. |
||
123 | debugOut.digital[1] |= DEBUG_MAINLOOP_TIMER; |
||
124 | } else { |
||
125 | debugOut.digital[1] &= ~DEBUG_MAINLOOP_TIMER; |
||
126 | } |
||
127 | } |
||
128 | } |
||
129 | return (1); |
||
2108 | - | 130 | } |