Rev 2130 | Rev 2137 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
1910 | - | 1 | #include <avr/boot.h> |
2 | #include <avr/io.h> |
||
3 | #include <avr/interrupt.h> |
||
2132 | - | 4 | #include <avr/wdt.h> |
1910 | - | 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" |
||
2099 | - | 12 | #include "commands.h" |
1910 | - | 13 | #include "flight.h" |
14 | #include "rc.h" |
||
15 | #include "analog.h" |
||
16 | #include "configuration.h" |
||
2099 | - | 17 | #include "controlMixer.h" |
1910 | - | 18 | #include "eeprom.h" |
2099 | - | 19 | #include "printf_P.h" |
1910 | - | 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 | |||
1910 | - | 30 | int16_t main(void) { |
2132 | - | 31 | uint16_t timer = 0; |
1910 | - | 32 | |
2132 | - | 33 | #ifdef DO_PROFILE |
34 | static uint8_t profileTimer; |
||
35 | #endif |
||
1910 | - | 36 | |
2132 | - | 37 | // disable interrupts global |
38 | cli(); |
||
1910 | - | 39 | |
2132 | - | 40 | wdt_enable(WDTO_120MS); |
1910 | - | 41 | |
2132 | - | 42 | // analyze hardware environment |
43 | setCPUType(); |
||
44 | setBoardRelease(); |
||
1910 | - | 45 | |
2132 | - | 46 | // initalize modules |
47 | output_init(); |
||
48 | timer0_init(); |
||
49 | timer2_init(); |
||
50 | usart0_init(); |
||
51 | //if (CPUType == ATMEGA644P);// usart1_Init(); |
||
52 | RC_Init(); |
||
53 | analog_init(); |
||
1910 | - | 54 | |
2132 | - | 55 | // Parameter Set handling |
56 | IMUConfig_readOrDefault(); |
||
57 | channelMap_readOrDefault(); |
||
58 | rcTrim_readOrDefault(); |
||
59 | paramSet_readOrDefault(); |
||
2099 | - | 60 | |
2132 | - | 61 | // enable interrupts global |
62 | sei(); |
||
1910 | - | 63 | |
2132 | - | 64 | controlMixer_setNeutral(); |
2099 | - | 65 | |
2132 | - | 66 | // Cal. attitude sensors and reset integrals. |
67 | attitude_setNeutral(); |
||
2099 | - | 68 | |
2132 | - | 69 | // Init flight parameters |
70 | // flight_setNeutral(); |
||
1910 | - | 71 | |
2132 | - | 72 | // This is not a busy-wait operation and should be OK. |
73 | beep(2000); |
||
1910 | - | 74 | |
2132 | - | 75 | while (1) { |
76 | if (runFlightControl) { // control interval |
||
77 | runFlightControl = 0; // reset Flag, is enabled every 2 ms by ISR of timer0 |
||
2125 | - | 78 | |
2132 | - | 79 | if (!resetFlag) { |
80 | wdt_reset(); |
||
81 | } |
||
82 | |||
83 | if (!analogDataReady) { |
||
84 | // Analog data should have been ready but is not!! |
||
85 | debugOut.digital[0] |= DEBUG_MAINLOOP_TIMER; |
||
86 | } else { |
||
87 | debugOut.digital[0] &= ~DEBUG_MAINLOOP_TIMER; |
||
88 | } |
||
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. |
||
2124 | - | 93 | #ifdef DO_PROFILE |
2132 | - | 94 | startProfileTimer(ATTITUDE); |
2124 | - | 95 | #endif |
2132 | - | 96 | calculateFlightAttitude(); |
2124 | - | 97 | #ifdef DO_PROFILE |
2132 | - | 98 | stopProfileTimer(ATTITUDE); |
2124 | - | 99 | #endif |
100 | |||
101 | #ifdef DO_PROFILE |
||
2132 | - | 102 | startProfileTimer(CONTROLMIXER); |
2124 | - | 103 | #endif |
2132 | - | 104 | controlMixer_periodicTask(); |
2124 | - | 105 | #ifdef DO_PROFILE |
2132 | - | 106 | stopProfileTimer(CONTROLMIXER); |
2124 | - | 107 | #endif |
108 | |||
109 | #ifdef DO_PROFILE |
||
2132 | - | 110 | startProfileTimer(COMMANDS); |
2124 | - | 111 | #endif |
2132 | - | 112 | commands_handleCommands(); |
2124 | - | 113 | #ifdef DO_PROFILE |
2132 | - | 114 | stopProfileTimer(COMMANDS); |
2124 | - | 115 | #endif |
116 | |||
117 | #ifdef DO_PROFILE |
||
2132 | - | 118 | startProfileTimer(FLIGHT); |
2124 | - | 119 | #endif |
2132 | - | 120 | flight_control(); |
2124 | - | 121 | #ifdef DO_PROFILE |
2132 | - | 122 | stopProfileTimer(FLIGHT); |
2124 | - | 123 | #endif |
2125 | - | 124 | |
2132 | - | 125 | // Allow Serial Data Transmit if motors must not updated or motors are not running |
126 | if (!runFlightControl || !isFlying) { |
||
127 | usart0_transmitTxData(); |
||
128 | } |
||
1910 | - | 129 | |
2132 | - | 130 | usart0_processRxData(); |
1910 | - | 131 | |
2132 | - | 132 | static uint8_t aboveWarningLimitVoltageSeen = 0; |
2122 | - | 133 | |
2132 | - | 134 | if (checkDelay(timer)) { |
135 | if (UBat >= staticParams.batteryWarningVoltage) { |
||
136 | aboveWarningLimitVoltageSeen = 1; |
||
137 | } else { // If we are above USB voltage, or if we have once been above warning voltage |
||
138 | if (aboveWarningLimitVoltageSeen || UBat > UBAT_AT_5V) { |
||
139 | beepBatteryAlarm(); |
||
140 | } |
||
141 | } |
||
142 | calculateFeaturedServoValues(); |
||
143 | timer = setDelay(20); // every 20 ms |
||
144 | } |
||
2125 | - | 145 | |
2132 | - | 146 | output_update(); |
2125 | - | 147 | |
2132 | - | 148 | if (runFlightControl) { // Time for the next iteration was up before the current finished. Signal error. |
149 | debugOut.digital[1] |= DEBUG_MAINLOOP_TIMER; |
||
150 | } else { |
||
151 | debugOut.digital[1] &= ~DEBUG_MAINLOOP_TIMER; |
||
152 | } |
||
153 | } |
||
154 | } |
||
155 | return (1); |
||
1910 | - | 156 | } |