Rev 2124 | Rev 2130 | 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> |
||
4 | #include <util/delay.h> |
||
5 | |||
6 | #include "timer0.h" |
||
7 | #include "timer2.h" |
||
8 | #include "uart0.h" |
||
9 | #include "output.h" |
||
10 | #include "attitude.h" |
||
2099 | - | 11 | #include "commands.h" |
1910 | - | 12 | #include "flight.h" |
13 | #include "rc.h" |
||
14 | #include "analog.h" |
||
15 | #include "configuration.h" |
||
2099 | - | 16 | #include "controlMixer.h" |
1910 | - | 17 | #include "eeprom.h" |
2099 | - | 18 | #include "printf_P.h" |
1910 | - | 19 | |
20 | int16_t main(void) { |
||
21 | uint16_t timer; |
||
2125 | - | 22 | static uint8_t profileTimer; |
1910 | - | 23 | |
24 | // disable interrupts global |
||
25 | cli(); |
||
26 | |||
27 | // analyze hardware environment |
||
2099 | - | 28 | setCPUType(); |
29 | setBoardRelease(); |
||
1910 | - | 30 | |
31 | // disable watchdog |
||
32 | MCUSR &= ~(1 << WDRF); |
||
33 | WDTCSR |= (1 << WDCE) | (1 << WDE); |
||
34 | WDTCSR = 0; |
||
35 | |||
2099 | - | 36 | // This is strange: It should NOT be necessarty to do. But the call of the same, |
37 | // in channelMap_readOrDefault (if eeprom read fails) just sets all to 0,0,0,.... |
||
2122 | - | 38 | // channelMap_default(); |
1910 | - | 39 | |
40 | // initalize modules |
||
41 | output_init(); |
||
42 | timer0_init(); |
||
2099 | - | 43 | timer2_init(); |
44 | usart0_init(); |
||
45 | //if (CPUType == ATMEGA644P);// usart1_Init(); |
||
1910 | - | 46 | RC_Init(); |
47 | analog_init(); |
||
48 | |||
2099 | - | 49 | // Parameter Set handling |
50 | IMUConfig_readOrDefault(); |
||
51 | channelMap_readOrDefault(); |
||
2122 | - | 52 | rcTrim_readOrDefault(); |
2099 | - | 53 | paramSet_readOrDefault(); |
54 | |||
1910 | - | 55 | // enable interrupts global |
56 | sei(); |
||
57 | |||
2099 | - | 58 | printf("\n\r==================================="); |
59 | printf("\n\rFlightControl"); |
||
60 | printf("\n\rHardware: Custom"); |
||
61 | printf("\n\r CPU: Atmega644"); |
||
62 | if (CPUType == ATMEGA644P) |
||
63 | printf("p"); |
||
64 | printf("\n\rSoftware: V%d.%d%c",VERSION_MAJOR, VERSION_MINOR, VERSION_PATCH + 'a'); |
||
65 | printf("\n\r==================================="); |
||
1910 | - | 66 | |
67 | // Wait for a short time (otherwise the RC channel check won't work below) |
||
68 | // timer = SetDelay(500); |
||
69 | // while(!CheckDelay(timer)); |
||
70 | |||
71 | // Instead, while away the time by flashing the 2 outputs: |
||
72 | // First J16, then J17. Makes it easier to see which is which. |
||
2099 | - | 73 | timer = setDelay(200); |
74 | outputSet(0,1); |
||
1910 | - | 75 | GRN_OFF; |
76 | RED_ON; |
||
77 | while (!checkDelay(timer)) |
||
78 | ; |
||
79 | |||
2099 | - | 80 | timer = setDelay(200); |
2110 | - | 81 | outputSet(0, 0); |
82 | outputSet(1, 1); |
||
1910 | - | 83 | RED_OFF; |
84 | GRN_ON; |
||
85 | while (!checkDelay(timer)) |
||
86 | ; |
||
87 | |||
2099 | - | 88 | timer = setDelay(200); |
1910 | - | 89 | while (!checkDelay(timer)) |
90 | ; |
||
2099 | - | 91 | outputSet(1,0); |
92 | GRN_OFF; |
||
1910 | - | 93 | |
2099 | - | 94 | printf("\n\r==================================="); |
1910 | - | 95 | |
2099 | - | 96 | #ifdef USE_NAVICTRL |
97 | printf("\n\rSupport for NaviCtrl"); |
||
98 | #endif |
||
1910 | - | 99 | |
2099 | - | 100 | #ifdef USE_DIRECT_GPS |
101 | printf("\n\rDirect (no NaviCtrl) navigation"); |
||
102 | #endif |
||
1910 | - | 103 | |
104 | controlMixer_setNeutral(); |
||
2099 | - | 105 | |
1910 | - | 106 | // Cal. attitude sensors and reset integrals. |
107 | attitude_setNeutral(); |
||
2099 | - | 108 | |
1910 | - | 109 | // Init flight parameters |
2099 | - | 110 | // flight_setNeutral(); |
1910 | - | 111 | |
2099 | - | 112 | beep(2000); |
1910 | - | 113 | |
2099 | - | 114 | printf("\n\n\r"); |
1910 | - | 115 | |
2099 | - | 116 | while (1) { |
1910 | - | 117 | if (runFlightControl) { // control interval |
118 | runFlightControl = 0; // reset Flag, is enabled every 2 ms by ISR of timer0 |
||
2125 | - | 119 | |
1910 | - | 120 | if (!analogDataReady) { |
2099 | - | 121 | // Analog data should have been ready but is not!! |
122 | debugOut.digital[0] |= DEBUG_MAINLOOP_TIMER; |
||
1910 | - | 123 | } else { |
2099 | - | 124 | debugOut.digital[0] &= ~DEBUG_MAINLOOP_TIMER; |
2110 | - | 125 | } |
2099 | - | 126 | // This is probably the correct order: |
127 | // The attitude computation should not depend on anything from control (except maybe the estimation of control activity level) |
||
128 | // The control may depend on attitude - for example, attitude control uses pitch and roll angles, compass control uses yaw angle etc. |
||
129 | // Flight control uses results from both. |
||
2124 | - | 130 | #ifdef DO_PROFILE |
131 | startProfileTimer(ATTITUDE); |
||
132 | #endif |
||
133 | calculateFlightAttitude(); |
||
134 | #ifdef DO_PROFILE |
||
135 | stopProfileTimer(ATTITUDE); |
||
136 | #endif |
||
137 | |||
138 | #ifdef DO_PROFILE |
||
139 | startProfileTimer(CONTROLMIXER); |
||
140 | #endif |
||
141 | controlMixer_periodicTask(); |
||
142 | #ifdef DO_PROFILE |
||
143 | stopProfileTimer(CONTROLMIXER); |
||
144 | #endif |
||
145 | |||
146 | #ifdef DO_PROFILE |
||
147 | startProfileTimer(COMMANDS); |
||
148 | #endif |
||
149 | commands_handleCommands(); |
||
150 | #ifdef DO_PROFILE |
||
151 | stopProfileTimer(COMMANDS); |
||
152 | #endif |
||
153 | |||
154 | #ifdef DO_PROFILE |
||
155 | startProfileTimer(FLIGHT); |
||
156 | #endif |
||
157 | flight_control(); |
||
158 | #ifdef DO_PROFILE |
||
159 | stopProfileTimer(FLIGHT); |
||
160 | #endif |
||
2125 | - | 161 | |
1910 | - | 162 | // Allow Serial Data Transmit if motors must not updated or motors are not running |
2108 | - | 163 | if (!runFlightControl || !isFlying) { |
2099 | - | 164 | usart0_transmitTxData(); |
1910 | - | 165 | } |
166 | |||
2099 | - | 167 | usart0_processRxData(); |
1910 | - | 168 | |
2122 | - | 169 | static uint8_t aboveWarningLimitVoltageSeen = 0; |
170 | |||
1910 | - | 171 | if (checkDelay(timer)) { |
2122 | - | 172 | if (UBat >= staticParams.batteryWarningVoltage) { |
173 | aboveWarningLimitVoltageSeen = 1; |
||
174 | } else { // If we are above USB voltage, or if we have once been above warning voltage |
||
175 | if (aboveWarningLimitVoltageSeen || UBat > UBAT_AT_5V) { |
||
176 | beepBatteryAlarm(); |
||
177 | } |
||
1910 | - | 178 | } |
2125 | - | 179 | calculateFeaturedServoValues(); |
1910 | - | 180 | timer = setDelay(20); // every 20 ms |
181 | } |
||
2125 | - | 182 | |
1910 | - | 183 | output_update(); |
2125 | - | 184 | |
185 | if (runFlightControl) { // Time for the next iteration was up before the current finished. Signal error. |
||
186 | debugOut.digital[1] |= DEBUG_MAINLOOP_TIMER; |
||
187 | } else { |
||
188 | debugOut.digital[1] &= ~DEBUG_MAINLOOP_TIMER; |
||
189 | } |
||
1910 | - | 190 | } |
191 | } |
||
192 | return (1); |
||
193 | } |