Rev 2125 | Rev 2132 | 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 | |||
58 | controlMixer_setNeutral(); |
||
2099 | - | 59 | |
1910 | - | 60 | // Cal. attitude sensors and reset integrals. |
61 | attitude_setNeutral(); |
||
2099 | - | 62 | |
1910 | - | 63 | // Init flight parameters |
2099 | - | 64 | // flight_setNeutral(); |
1910 | - | 65 | |
2130 | - | 66 | // This is not a busy-wait operation and should be OK. |
2099 | - | 67 | beep(2000); |
1910 | - | 68 | |
2130 | - | 69 | while (1) { |
1910 | - | 70 | if (runFlightControl) { // control interval |
71 | runFlightControl = 0; // reset Flag, is enabled every 2 ms by ISR of timer0 |
||
2125 | - | 72 | |
1910 | - | 73 | if (!analogDataReady) { |
2099 | - | 74 | // Analog data should have been ready but is not!! |
75 | debugOut.digital[0] |= DEBUG_MAINLOOP_TIMER; |
||
1910 | - | 76 | } else { |
2099 | - | 77 | debugOut.digital[0] &= ~DEBUG_MAINLOOP_TIMER; |
2110 | - | 78 | } |
2099 | - | 79 | // This is probably the correct order: |
80 | // The attitude computation should not depend on anything from control (except maybe the estimation of control activity level) |
||
81 | // The control may depend on attitude - for example, attitude control uses pitch and roll angles, compass control uses yaw angle etc. |
||
82 | // Flight control uses results from both. |
||
2124 | - | 83 | #ifdef DO_PROFILE |
84 | startProfileTimer(ATTITUDE); |
||
85 | #endif |
||
86 | calculateFlightAttitude(); |
||
87 | #ifdef DO_PROFILE |
||
88 | stopProfileTimer(ATTITUDE); |
||
89 | #endif |
||
90 | |||
91 | #ifdef DO_PROFILE |
||
92 | startProfileTimer(CONTROLMIXER); |
||
93 | #endif |
||
94 | controlMixer_periodicTask(); |
||
95 | #ifdef DO_PROFILE |
||
96 | stopProfileTimer(CONTROLMIXER); |
||
97 | #endif |
||
98 | |||
99 | #ifdef DO_PROFILE |
||
100 | startProfileTimer(COMMANDS); |
||
101 | #endif |
||
102 | commands_handleCommands(); |
||
103 | #ifdef DO_PROFILE |
||
104 | stopProfileTimer(COMMANDS); |
||
105 | #endif |
||
106 | |||
107 | #ifdef DO_PROFILE |
||
108 | startProfileTimer(FLIGHT); |
||
109 | #endif |
||
110 | flight_control(); |
||
111 | #ifdef DO_PROFILE |
||
112 | stopProfileTimer(FLIGHT); |
||
113 | #endif |
||
2125 | - | 114 | |
1910 | - | 115 | // Allow Serial Data Transmit if motors must not updated or motors are not running |
2108 | - | 116 | if (!runFlightControl || !isFlying) { |
2099 | - | 117 | usart0_transmitTxData(); |
1910 | - | 118 | } |
119 | |||
2099 | - | 120 | usart0_processRxData(); |
1910 | - | 121 | |
2122 | - | 122 | static uint8_t aboveWarningLimitVoltageSeen = 0; |
123 | |||
1910 | - | 124 | if (checkDelay(timer)) { |
2122 | - | 125 | if (UBat >= staticParams.batteryWarningVoltage) { |
126 | aboveWarningLimitVoltageSeen = 1; |
||
127 | } else { // If we are above USB voltage, or if we have once been above warning voltage |
||
128 | if (aboveWarningLimitVoltageSeen || UBat > UBAT_AT_5V) { |
||
129 | beepBatteryAlarm(); |
||
130 | } |
||
1910 | - | 131 | } |
2125 | - | 132 | calculateFeaturedServoValues(); |
1910 | - | 133 | timer = setDelay(20); // every 20 ms |
134 | } |
||
2125 | - | 135 | |
1910 | - | 136 | output_update(); |
2125 | - | 137 | |
138 | if (runFlightControl) { // Time for the next iteration was up before the current finished. Signal error. |
||
139 | debugOut.digital[1] |= DEBUG_MAINLOOP_TIMER; |
||
140 | } else { |
||
141 | debugOut.digital[1] &= ~DEBUG_MAINLOOP_TIMER; |
||
142 | } |
||
1910 | - | 143 | } |
144 | } |
||
145 | return (1); |
||
146 | } |