Rev 2109 | Go to most recent revision | Details | 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> |
||
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" |
||
11 | #include "commands.h" |
||
12 | #include "flight.h" |
||
13 | #include "rc.h" |
||
14 | #include "analog.h" |
||
15 | #include "configuration.h" |
||
16 | #include "controlMixer.h" |
||
17 | #include "eeprom.h" |
||
18 | #include "printf_P.h" |
||
19 | |||
20 | int16_t main(void) { |
||
21 | uint16_t timer; |
||
22 | |||
23 | // disable interrupts global |
||
24 | cli(); |
||
25 | |||
26 | // disable watchdog |
||
27 | MCUSR &= ~(1 << WDRF); |
||
28 | WDTCSR |= (1 << WDCE) | (1 << WDE); |
||
29 | WDTCSR = 0; |
||
30 | |||
31 | // This is strange: It should NOT be necessarty to do. But the call of the same, |
||
32 | // in channelMap_readOrDefault (if eeprom read fails) just sets all to 0,0,0,.... |
||
33 | channelMap_default(); |
||
34 | |||
35 | // initalize modules |
||
36 | output_init(); |
||
37 | timer0_init(); |
||
38 | timer2_init(); |
||
39 | usart0_init(); |
||
40 | RC_Init(); |
||
41 | analog_init(); |
||
42 | |||
43 | // Parameter Set handling |
||
44 | IMUConfig_readOrDefault(); |
||
45 | channelMap_readOrDefault(); |
||
46 | paramSet_readOrDefault(); |
||
47 | |||
48 | // enable interrupts global |
||
49 | sei(); |
||
50 | |||
51 | printf("\n\r==================================="); |
||
52 | printf("\n\rFlightControl"); |
||
53 | printf("\n\rHardware: Custom"); |
||
54 | printf("\n\r CPU: Atmega328"); |
||
55 | printf("\n\rSoftware: V%d.%d%c",VERSION_MAJOR, VERSION_MINOR, VERSION_PATCH + 'a'); |
||
56 | printf("\n\r==================================="); |
||
57 | |||
58 | // Wait for a short time (otherwise the RC channel check won't work below) |
||
59 | // timer = SetDelay(500); |
||
60 | // while(!CheckDelay(timer)); |
||
61 | |||
62 | // Instead, while away the time by flashing the 2 outputs: |
||
63 | // First J16, then J17. Makes it easier to see which is which. |
||
64 | timer = setDelay(200); |
||
65 | outputSet(0,1); |
||
66 | GRN_OFF; |
||
67 | RED_ON; |
||
68 | while (!checkDelay(timer)) |
||
69 | ; |
||
70 | |||
71 | timer = setDelay(200); |
||
72 | outputSet(0,0); |
||
73 | outputSet(1,1); |
||
74 | RED_OFF; |
||
75 | GRN_ON; |
||
76 | while (!checkDelay(timer)) |
||
77 | ; |
||
78 | |||
79 | timer = setDelay(200); |
||
80 | while (!checkDelay(timer)) |
||
81 | ; |
||
82 | outputSet(1,0); |
||
83 | GRN_OFF; |
||
84 | |||
85 | printf("\n\r==================================="); |
||
86 | |||
87 | #ifdef USE_NAVICTRL |
||
88 | printf("\n\rSupport for NaviCtrl"); |
||
89 | #endif |
||
90 | |||
91 | #ifdef USE_DIRECT_GPS |
||
92 | printf("\n\rDirect (no NaviCtrl) navigation"); |
||
93 | #endif |
||
94 | |||
95 | controlMixer_setNeutral(); |
||
96 | |||
97 | // Cal. attitude sensors and reset integrals. |
||
98 | attitude_setNeutral(); |
||
99 | |||
100 | // Init flight parameters |
||
101 | // flight_setNeutral(); |
||
102 | |||
103 | beep(2000); |
||
104 | |||
105 | printf("\n\n\r"); |
||
106 | |||
107 | while (1) { |
||
108 | if (runFlightControl) { // control interval |
||
109 | runFlightControl = 0; // reset Flag, is enabled every 2 ms by ISR of timer0 |
||
110 | if (sensorDataReady != ALL_DATA_READY) { |
||
111 | // Analog data should have been ready but is not!! |
||
112 | debugOut.digital[0] |= DEBUG_MAINLOOP_TIMER; |
||
113 | } else { |
||
114 | debugOut.digital[0] &= ~DEBUG_MAINLOOP_TIMER; |
||
115 | |||
116 | J4HIGH; |
||
117 | // This is probably the correct order: |
||
118 | // The attitude computation should not depend on anything from control (except maybe the estimation of control activity level) |
||
119 | // The control may depend on attitude - for example, attitude control uses pitch and roll angles, compass control uses yaw angle etc. |
||
120 | // Flight control uses results from both. |
||
121 | calculateFlightAttitude(); |
||
122 | controlMixer_periodicTask(); |
||
123 | commands_handleCommands(); |
||
124 | flight_control(); |
||
125 | J4LOW; |
||
126 | |||
127 | // Allow Serial Data Transmit if motors must not updated or motors are not running |
||
128 | if (!runFlightControl || !isFlying) { |
||
129 | usart0_transmitTxData(); |
||
130 | } |
||
131 | |||
132 | usart0_processRxData(); |
||
133 | |||
134 | if (checkDelay(timer)) { |
||
135 | if (UBat <= UBAT_AT_5V) { |
||
136 | // Do nothing. The voltage on the input side of the regulator is <5V; |
||
137 | // we must be running off USB power. Keep it quiet. |
||
138 | } else if (UBat < staticParams.batteryVoltageWarning) { |
||
139 | beepBatteryAlarm(); |
||
140 | } |
||
141 | |||
142 | #ifdef USE_NAVICTRL |
||
143 | SPI_StartTransmitPacket(); |
||
144 | SendSPI = 4; |
||
145 | #endif |
||
146 | timer = setDelay(20); // every 20 ms |
||
147 | } |
||
148 | output_update(); |
||
149 | } |
||
150 | |||
151 | #ifdef USE_NAVICTRL |
||
152 | if(!SendSPI) { |
||
153 | // SendSPI is decremented in timer0.c with a rate of 9.765 kHz. |
||
154 | // within the SPI_TransmitByte() routine the value is set to 4. |
||
155 | // I.e. the SPI_TransmitByte() is called at a rate of 9.765 kHz/4= 2441.25 Hz, |
||
156 | // and therefore the time of transmission of a complete spi-packet (32 bytes) is 32*4/9.765 kHz = 13.1 ms. |
||
157 | SPI_TransmitByte(); |
||
158 | } |
||
159 | #endif |
||
160 | calculateFeaturedServoValues(); |
||
161 | |||
162 | if (runFlightControl) { // Time for the next iteration was up before the current finished. Signal error. |
||
163 | debugOut.digital[1] |= DEBUG_MAINLOOP_TIMER; |
||
164 | } else { |
||
165 | debugOut.digital[1] &= ~DEBUG_MAINLOOP_TIMER; |
||
166 | } |
||
167 | } |
||
168 | } |
||
169 | return (1); |
||
170 | } |