Rev 2052 | Rev 2088 | Go to most recent revision | Only display areas with differences | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed
Rev 2052 | Rev 2055 | ||
---|---|---|---|
1 | // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
- | |
2 | // + Copyright (c) 04.2007 Holger Buss |
- | |
3 | // + Nur für den privaten Gebrauch |
- | |
4 | // + www.MikroKopter.com |
- | |
5 | // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
- | |
6 | // + Es gilt für das gesamte Projekt (Hardware, Software, Binärfiles, Sourcecode und Dokumentation), |
- | |
7 | // + dass eine Nutzung (auch auszugsweise) nur f�r den privaten und nicht-kommerziellen Gebrauch zulässig ist. |
- | |
8 | // + Sollten direkte oder indirekte kommerzielle Absichten verfolgt werden, ist mit uns (info@mikrokopter.de) Kontakt |
- | |
9 | // + bzgl. der Nutzungsbedingungen aufzunehmen. |
- | |
10 | // + Eine kommerzielle Nutzung ist z.B.Verkauf von MikroKoptern, Bestückung und Verkauf von Platinen oder Bausätzen, |
- | |
11 | // + Verkauf von Luftbildaufnahmen, usw. |
- | |
12 | // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
- | |
13 | // + Werden Teile des Quellcodes (mit oder ohne Modifikation) weiterverwendet oder veröffentlicht, |
- | |
14 | // + unterliegen sie auch diesen Nutzungsbedingungen und diese Nutzungsbedingungen incl. Copyright müssen dann beiliegen |
- | |
15 | // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
- | |
16 | // + Sollte die Software (auch auszugesweise) oder sonstige Informationen des MikroKopter-Projekts |
- | |
17 | // + auf anderen Webseiten oder Medien veröffentlicht werden, muss unsere Webseite "http://www.mikrokopter.de" |
- | |
18 | // + eindeutig als Ursprung verlinkt und genannt werden |
- | |
19 | // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
- | |
20 | // + Keine Gewähr auf Fehlerfreiheit, Vollständigkeit oder Funktion |
- | |
21 | // + Benutzung auf eigene Gefahr |
- | |
22 | // + Wir übernehmen keinerlei Haftung für direkte oder indirekte Personen- oder Sachschäden |
- | |
23 | // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
- | |
24 | // + Die Portierung der Software (oder Teile davon) auf andere Systeme (ausser der Hardware von www.mikrokopter.de) ist nur |
- | |
25 | // + mit unserer Zustimmung zulässig |
- | |
26 | // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
- | |
27 | // + Die Funktion printf_P() unterliegt ihrer eigenen Lizenz und ist hiervon nicht betroffen |
- | |
28 | // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
- | |
29 | // + Redistributions of source code (with or without modifications) must retain the above copyright notice, |
- | |
30 | // + this list of conditions and the following disclaimer. |
- | |
31 | // + * Neither the name of the copyright holders nor the names of contributors may be used to endorse or promote products derived |
- | |
32 | // + from this software without specific prior written permission. |
- | |
33 | // + * The use of this project (hardware, software, binary files, sources and documentation) is only permittet |
- | |
34 | // + for non-commercial use (directly or indirectly) |
- | |
35 | // + Commercial use (for example: selling of MikroKopters, selling of PCBs, assembly, ...) is only permitted |
- | |
36 | // + with our written permission |
- | |
37 | // + * If sources or documentations are redistributet on other webpages, out webpage (http://www.MikroKopter.de) must be |
- | |
38 | // + clearly linked as origin |
- | |
39 | // + * porting to systems other than hardware from www.mikrokopter.de is not allowed |
- | |
40 | // + THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
- | |
41 | // + AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
- | |
42 | // + IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
- | |
43 | // + ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE |
- | |
44 | // + LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
- | |
45 | // + CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
- | |
46 | // + SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
- | |
47 | // + INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
- | |
48 | // + CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
- | |
49 | // + ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
- | |
50 | // + POSSIBILITY OF SUCH DAMAGE. |
- | |
51 | // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
- | |
52 | - | ||
53 | #include <avr/boot.h> |
1 | #include <avr/boot.h> |
54 | #include <avr/io.h> |
2 | #include <avr/io.h> |
55 | #include <avr/interrupt.h> |
3 | #include <avr/interrupt.h> |
56 | #include <util/delay.h> |
4 | #include <util/delay.h> |
57 | 5 | ||
58 | #include "timer0.h" |
6 | #include "timer0.h" |
59 | #include "timer2.h" |
7 | #include "timer2.h" |
60 | #include "uart0.h" |
8 | #include "uart0.h" |
61 | #include "uart1.h" |
9 | #include "uart1.h" |
62 | #include "output.h" |
10 | #include "output.h" |
63 | #include "menu.h" |
11 | #include "menu.h" |
64 | #include "attitude.h" |
12 | #include "attitude.h" |
- | 13 | #include "commands.h" |
|
65 | #include "flight.h" |
14 | #include "flight.h" |
66 | #include "rc.h" |
15 | #include "rc.h" |
67 | #include "analog.h" |
16 | #include "analog.h" |
68 | #include "configuration.h" |
17 | #include "configuration.h" |
69 | #include "printf_P.h" |
18 | #include "printf_P.h" |
70 | #include "twimaster.h" |
19 | #include "twimaster.h" |
71 | #include "controlMixer.h" |
20 | #include "controlMixer.h" |
72 | #ifdef USE_NAVICTRL |
21 | #ifdef USE_NAVICTRL |
73 | #include "spi.h" |
22 | #include "spi.h" |
74 | #endif |
23 | #endif |
75 | #ifdef USE_MK3MAG |
24 | #ifdef USE_MK3MAG |
76 | #include "mk3mag.h" |
25 | #include "mk3mag.h" |
77 | #endif |
26 | #endif |
78 | #include "eeprom.h" |
27 | #include "eeprom.h" |
79 | 28 | ||
80 | int16_t main(void) { |
29 | int16_t main(void) { |
81 | uint16_t timer; |
30 | uint16_t timer; |
82 | 31 | ||
83 | // disable interrupts global |
32 | // disable interrupts global |
84 | cli(); |
33 | cli(); |
85 | 34 | ||
86 | // analyze hardware environment |
35 | // analyze hardware environment |
87 | CPUType = getCPUType(); |
36 | CPUType = getCPUType(); |
88 | boardRelease = getBoardRelease(); |
37 | boardRelease = getBoardRelease(); |
89 | 38 | ||
90 | // disable watchdog |
39 | // disable watchdog |
91 | MCUSR &= ~(1 << WDRF); |
40 | MCUSR &= ~(1 << WDRF); |
92 | WDTCSR |= (1 << WDCE) | (1 << WDE); |
41 | WDTCSR |= (1 << WDCE) | (1 << WDE); |
93 | WDTCSR = 0; |
42 | WDTCSR = 0; |
94 | 43 | ||
95 | // This is strange: It should NOT be necessarty to do. But the call of the same, |
44 | // This is strange: It should NOT be necessarty to do. But the call of the same, |
96 | // in channelMap_readOrDefault (if eeprom read fails) just sets all to 0,0,0,.... |
45 | // in channelMap_readOrDefault (if eeprom read fails) just sets all to 0,0,0,.... |
97 | channelMap_default(); |
46 | channelMap_default(); |
98 | 47 | ||
99 | // initalize modules |
48 | // initalize modules |
100 | output_init(); |
49 | output_init(); |
101 | timer0_init(); |
50 | timer0_init(); |
102 | timer2_init(); |
51 | timer2_init(); |
103 | usart0_init(); |
52 | usart0_init(); |
104 | if (CPUType == ATMEGA644P);// usart1_Init(); |
53 | if (CPUType == ATMEGA644P);// usart1_Init(); |
105 | RC_Init(); |
54 | RC_Init(); |
106 | analog_init(); |
55 | analog_init(); |
107 | I2C_init(); |
56 | I2C_init(); |
108 | #ifdef USE_NAVICTRL |
57 | #ifdef USE_NAVICTRL |
109 | SPI_MasterInit(); |
58 | SPI_MasterInit(); |
110 | #endif |
59 | #endif |
111 | #ifdef USE_MK3MAG |
60 | #ifdef USE_MK3MAG |
112 | MK3MAG_init(); |
61 | MK3MAG_init(); |
113 | #endif |
62 | #endif |
114 | #ifdef USE_DIRECT_GPS |
63 | #ifdef USE_DIRECT_GPS |
115 | usart1_init(); |
64 | usart1_init(); |
116 | #endif |
65 | #endif |
117 | 66 | ||
118 | // Parameter Set handling |
67 | // Parameter Set handling |
119 | channelMap_readOrDefault(); |
68 | channelMap_readOrDefault(); |
120 | mixerMatrix_readOrDefault(); |
69 | mixerMatrix_readOrDefault(); |
121 | paramSet_readOrDefault(); |
70 | paramSet_readOrDefault(); |
122 | 71 | ||
123 | // enable interrupts global |
72 | // enable interrupts global |
124 | sei(); |
73 | sei(); |
125 | 74 | ||
126 | printf("\n\r==================================="); |
75 | printf("\n\r==================================="); |
127 | printf("\n\rFlightControl"); |
76 | printf("\n\rFlightControl"); |
128 | printf("\n\rHardware: Custom"); |
77 | printf("\n\rHardware: Custom"); |
129 | printf("\n\r CPU: Atmega644"); |
78 | printf("\n\r CPU: Atmega644"); |
130 | if (CPUType == ATMEGA644P) |
79 | if (CPUType == ATMEGA644P) |
131 | printf("p"); |
80 | printf("p"); |
132 | printf("\n\rSoftware: V%d.%d%c",VERSION_MAJOR, VERSION_MINOR, VERSION_PATCH + 'a'); |
81 | printf("\n\rSoftware: V%d.%d%c",VERSION_MAJOR, VERSION_MINOR, VERSION_PATCH + 'a'); |
133 | printf("\n\r==================================="); |
82 | printf("\n\r==================================="); |
134 | 83 | ||
135 | // Wait for a short time (otherwise the RC channel check won't work below) |
84 | // Wait for a short time (otherwise the RC channel check won't work below) |
136 | // timer = SetDelay(500); |
85 | // timer = SetDelay(500); |
137 | // while(!CheckDelay(timer)); |
86 | // while(!CheckDelay(timer)); |
138 | 87 | ||
139 | // Instead, while away the time by flashing the 2 outputs: |
88 | // Instead, while away the time by flashing the 2 outputs: |
140 | // First J16, then J17. Makes it easier to see which is which. |
89 | // First J16, then J17. Makes it easier to see which is which. |
141 | timer = setDelay(200); |
90 | timer = setDelay(200); |
142 | outputSet(0,1); |
91 | outputSet(0,1); |
143 | GRN_OFF; |
92 | GRN_OFF; |
144 | RED_ON; |
93 | RED_ON; |
145 | while (!checkDelay(timer)) |
94 | while (!checkDelay(timer)) |
146 | ; |
95 | ; |
147 | 96 | ||
148 | timer = setDelay(200); |
97 | timer = setDelay(200); |
149 | outputSet(0,0); |
98 | outputSet(0,0); |
150 | outputSet(1,1); |
99 | outputSet(1,1); |
151 | RED_OFF; |
100 | RED_OFF; |
152 | GRN_ON; |
101 | GRN_ON; |
153 | while (!checkDelay(timer)) |
102 | while (!checkDelay(timer)) |
154 | ; |
103 | ; |
155 | 104 | ||
156 | timer = setDelay(200); |
105 | timer = setDelay(200); |
157 | while (!checkDelay(timer)) |
106 | while (!checkDelay(timer)) |
158 | ; |
107 | ; |
159 | outputSet(1,0); |
108 | outputSet(1,0); |
160 | GRN_OFF; |
109 | GRN_OFF; |
161 | 110 | ||
162 | twi_diagnostics(); |
111 | twi_diagnostics(); |
163 | 112 | ||
164 | printf("\n\r==================================="); |
113 | printf("\n\r==================================="); |
165 | 114 | ||
166 | #ifdef USE_NAVICTRL |
115 | #ifdef USE_NAVICTRL |
167 | printf("\n\rSupport for NaviCtrl"); |
116 | printf("\n\rSupport for NaviCtrl"); |
168 | #endif |
117 | #endif |
169 | 118 | ||
170 | #ifdef USE_DIRECT_GPS |
119 | #ifdef USE_DIRECT_GPS |
171 | printf("\n\rDirect (no NaviCtrl) navigation"); |
120 | printf("\n\rDirect (no NaviCtrl) navigation"); |
172 | #endif |
121 | #endif |
173 | 122 | ||
174 | controlMixer_setNeutral(); |
123 | controlMixer_setNeutral(); |
175 | 124 | ||
176 | // Cal. attitude sensors and reset integrals. |
125 | // Cal. attitude sensors and reset integrals. |
177 | attitude_setNeutral(); |
126 | attitude_setNeutral(); |
178 | 127 | ||
179 | // Init flight parameters |
128 | // Init flight parameters |
180 | flight_setNeutral(); |
129 | // flight_setNeutral(); |
181 | 130 | ||
182 | beep(2000); |
131 | beep(2000); |
183 | 132 | ||
184 | printf("\n\rControl: "); |
133 | printf("\n\rControl: "); |
185 | if (staticParams.bitConfig & CFG_HEADING_HOLD) |
134 | if (staticParams.bitConfig & CFG_HEADING_HOLD) |
186 | printf("Heading Hold"); |
135 | printf("Heading Hold"); |
187 | else printf("RTL Mode"); |
136 | else printf("RTL Mode"); |
188 | 137 | ||
189 | printf("\n\n\r"); |
138 | printf("\n\n\r"); |
190 | 139 | ||
191 | LCD_clear(); |
140 | LCD_clear(); |
192 | 141 | ||
193 | I2CTimeout = 5000; |
142 | I2CTimeout = 5000; |
194 | 143 | ||
195 | while (1) { |
144 | while (1) { |
196 | if (runFlightControl) { // control interval |
145 | if (runFlightControl) { // control interval |
197 | runFlightControl = 0; // reset Flag, is enabled every 2 ms by ISR of timer0 |
146 | runFlightControl = 0; // reset Flag, is enabled every 2 ms by ISR of timer0 |
198 | if (!analogDataReady) { |
147 | if (!analogDataReady) { |
199 | // Analog data should have been ready but is not!! |
148 | // Analog data should have been ready but is not!! |
200 | debugOut.digital[0] |= DEBUG_MAINLOOP_TIMER; |
149 | debugOut.digital[0] |= DEBUG_MAINLOOP_TIMER; |
201 | } else { |
150 | } else { |
202 | debugOut.digital[0] &= ~DEBUG_MAINLOOP_TIMER; |
151 | debugOut.digital[0] &= ~DEBUG_MAINLOOP_TIMER; |
203 | 152 | ||
204 | J4HIGH; |
153 | J4HIGH; |
205 | // This is probably the correct order: |
154 | // This is probably the correct order: |
206 | // The attitude computation should not depend on anything from control (except maybe the estimation of control activity level) |
155 | // The attitude computation should not depend on anything from control (except maybe the estimation of control activity level) |
207 | // The control may depend on attitude - for example, attitude control uses pitch and roll angles, compass control uses yaw angle etc. |
156 | // The control may depend on attitude - for example, attitude control uses pitch and roll angles, compass control uses yaw angle etc. |
208 | // Flight control uses results from both. |
157 | // Flight control uses results from both. |
209 | calculateFlightAttitude(); |
158 | calculateFlightAttitude(); |
210 | controlMixer_periodicTask(); |
159 | controlMixer_periodicTask(); |
- | 160 | commands_handleCommands(); |
|
211 | flight_control(); |
161 | flight_control(); |
212 | J4LOW; |
162 | J4LOW; |
213 | 163 | ||
214 | if (!--I2CTimeout || missingMotor) { // try to reset the i2c if motor is missing or timeout |
164 | if (!--I2CTimeout || missingMotor) { // try to reset the i2c if motor is missing or timeout |
215 | if (!I2CTimeout) { |
165 | if (!I2CTimeout) { |
216 | I2C_Reset(); |
166 | I2C_Reset(); |
217 | I2CTimeout = 5; |
167 | I2CTimeout = 5; |
218 | } |
168 | } |
219 | beepI2CAlarm(); |
169 | beepI2CAlarm(); |
220 | } |
170 | } |
221 | 171 | ||
222 | // Allow Serial Data Transmit if motors must not updated or motors are not running |
172 | // Allow Serial Data Transmit if motors must not updated or motors are not running |
223 | if (!runFlightControl || !(MKFlags & MKFLAG_MOTOR_RUN)) { |
173 | if (!runFlightControl || !(MKFlags & MKFLAG_MOTOR_RUN)) { |
224 | usart0_transmitTxData(); |
174 | usart0_transmitTxData(); |
225 | } |
175 | } |
226 | 176 | ||
227 | usart0_processRxData(); |
177 | usart0_processRxData(); |
228 | 178 | ||
229 | if (checkDelay(timer)) { |
179 | if (checkDelay(timer)) { |
230 | if (UBat <= UBAT_AT_5V) { |
180 | if (UBat <= UBAT_AT_5V) { |
231 | // Do nothing. The voltage on the input side of the regulator is <5V; |
181 | // Do nothing. The voltage on the input side of the regulator is <5V; |
232 | // we must be running off USB power. Keep it quiet. |
182 | // we must be running off USB power. Keep it quiet. |
233 | } else if (UBat < staticParams.batteryVoltageWarning) { |
183 | } else if (UBat < staticParams.batteryVoltageWarning) { |
234 | beepBatteryAlarm(); |
184 | beepBatteryAlarm(); |
235 | } |
185 | } |
236 | 186 | ||
237 | #ifdef USE_NAVICTRL |
187 | #ifdef USE_NAVICTRL |
238 | SPI_StartTransmitPacket(); |
188 | SPI_StartTransmitPacket(); |
239 | SendSPI = 4; |
189 | SendSPI = 4; |
240 | #endif |
190 | #endif |
241 | timer = setDelay(20); // every 20 ms |
191 | timer = setDelay(20); // every 20 ms |
242 | } |
192 | } |
243 | output_update(); |
193 | output_update(); |
244 | } |
194 | } |
245 | 195 | ||
246 | #ifdef USE_NAVICTRL |
196 | #ifdef USE_NAVICTRL |
247 | if(!SendSPI) { |
197 | if(!SendSPI) { |
248 | // SendSPI is decremented in timer0.c with a rate of 9.765 kHz. |
198 | // SendSPI is decremented in timer0.c with a rate of 9.765 kHz. |
249 | // within the SPI_TransmitByte() routine the value is set to 4. |
199 | // within the SPI_TransmitByte() routine the value is set to 4. |
250 | // I.e. the SPI_TransmitByte() is called at a rate of 9.765 kHz/4= 2441.25 Hz, |
200 | // I.e. the SPI_TransmitByte() is called at a rate of 9.765 kHz/4= 2441.25 Hz, |
251 | // and therefore the time of transmission of a complete spi-packet (32 bytes) is 32*4/9.765 kHz = 13.1 ms. |
201 | // and therefore the time of transmission of a complete spi-packet (32 bytes) is 32*4/9.765 kHz = 13.1 ms. |
252 | SPI_TransmitByte(); |
202 | SPI_TransmitByte(); |
253 | } |
203 | } |
254 | #endif |
204 | #endif |
255 | 205 | ||
256 | calculateServoValues(); |
206 | calculateServoValues(); |
257 | 207 | ||
258 | if (runFlightControl) { // Time for the next iteration was up before the current finished. Signal error. |
208 | if (runFlightControl) { // Time for the next iteration was up before the current finished. Signal error. |
259 | debugOut.digital[1] |= DEBUG_MAINLOOP_TIMER; |
209 | debugOut.digital[1] |= DEBUG_MAINLOOP_TIMER; |
260 | } else { |
210 | } else { |
261 | debugOut.digital[1] &= ~DEBUG_MAINLOOP_TIMER; |
211 | debugOut.digital[1] &= ~DEBUG_MAINLOOP_TIMER; |
262 | } |
212 | } |
263 | } |
213 | } |
264 | } |
214 | } |
265 | return (1); |
215 | return (1); |
266 | } |
216 | } |
267 | 217 |