Go to most recent revision | Details | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
1227 | killagreg | 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 excample: 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 | #include <avr/boot.h> |
||
53 | |||
54 | #include <avr/io.h> |
||
55 | #include <avr/interrupt.h> |
||
56 | #include <util/delay.h> |
||
57 | |||
58 | #include "main.h" |
||
59 | #include "timer0.h" |
||
60 | #include "timer2.h" |
||
61 | #include "uart0.h" |
||
62 | #include "uart1.h" |
||
63 | #include "led.h" |
||
64 | #include "menu.h" |
||
65 | #include "fc.h" |
||
66 | #include "rc.h" |
||
67 | #include "analog.h" |
||
68 | #include "printf_P.h" |
||
69 | #ifdef USE_KILLAGREG |
||
70 | #include "mm3.h" |
||
71 | #endif |
||
72 | #ifdef USE_NAVICTRL |
||
73 | #include "spi.h" |
||
74 | #endif |
||
75 | #ifdef USE_MK3MAG |
||
76 | #include "mk3mag.h" |
||
77 | #endif |
||
78 | #include "twimaster.h" |
||
79 | #include "eeprom.h" |
||
80 | |||
1342 | Lion | 81 | #include "CapCalc.h" |
1227 | killagreg | 82 | |
1342 | Lion | 83 | |
1227 | killagreg | 84 | uint8_t BoardRelease = 10; |
85 | uint8_t CPUType = ATMEGA644; |
||
86 | |||
87 | |||
88 | uint8_t GetCPUType(void) |
||
89 | { // works only after reset or power on when the registers have default values |
||
90 | uint8_t CPUType = ATMEGA644; |
||
91 | if( (UCSR1A == 0x20) && (UCSR1C == 0x06) ) CPUType = ATMEGA644P; // initial Values for 644P after reset |
||
92 | return CPUType; |
||
93 | } |
||
94 | |||
95 | |||
96 | uint8_t GetBoardRelease(void) |
||
97 | { |
||
98 | uint8_t BoardRelease = 10; |
||
99 | // the board release is coded via the pull up or down the 2 status LED |
||
100 | |||
101 | PORTB &= ~((1 << PORTB1)|(1 << PORTB0)); // set tristate |
||
102 | DDRB &= ~((1 << DDB0)|(1 << DDB0)); // set port direction as input |
||
103 | |||
104 | _delay_loop_2(1000); // make some delay |
||
105 | |||
106 | switch( PINB & ((1<<PINB1)|(1<<PINB0)) ) |
||
107 | { |
||
108 | case 0x00: |
||
109 | BoardRelease = 10; // 1.0 |
||
110 | break; |
||
111 | case 0x01: |
||
112 | BoardRelease = 11; // 1.1 or 1.2 |
||
113 | break; |
||
114 | case 0x02: |
||
115 | BoardRelease = 20; // 2.0 |
||
116 | break; |
||
117 | case 0x03: |
||
118 | BoardRelease = 13; // 1.3 |
||
119 | break; |
||
120 | default: |
||
121 | break; |
||
122 | } |
||
123 | // set LED ports as output |
||
124 | DDRB |= (1<<DDB1)|(1<<DDB0); |
||
125 | RED_ON; |
||
126 | GRN_OFF; |
||
127 | return BoardRelease; |
||
128 | } |
||
129 | |||
130 | |||
131 | int16_t main (void) |
||
132 | { |
||
133 | uint16_t timer; |
||
134 | uint8_t i; |
||
135 | |||
136 | // disable interrupts global |
||
137 | cli(); |
||
138 | |||
139 | // analyze hardware environment |
||
140 | CPUType = GetCPUType(); |
||
141 | BoardRelease = GetBoardRelease(); |
||
142 | |||
143 | // disable watchdog |
||
144 | MCUSR &=~(1<<WDRF); |
||
145 | WDTCSR |= (1<<WDCE)|(1<<WDE); |
||
146 | WDTCSR = 0; |
||
147 | |||
148 | BeepTime = 2000; |
||
149 | |||
150 | PPM_in[CH_GAS] = 0; |
||
151 | StickYaw = 0; |
||
152 | StickRoll = 0; |
||
153 | StickNick = 0; |
||
154 | |||
155 | RED_OFF; |
||
156 | |||
157 | // initalize modules |
||
158 | LED_Init(); |
||
159 | TIMER0_Init(); |
||
160 | TIMER2_Init(); |
||
161 | USART0_Init(); |
||
162 | if(CPUType == ATMEGA644P) USART1_Init(); |
||
163 | RC_Init(); |
||
164 | ADC_Init(); |
||
165 | I2C_Init(); |
||
166 | #ifdef USE_NAVICTRL |
||
167 | SPI_MasterInit(); |
||
168 | #endif |
||
169 | #ifdef USE_KILLAGREG |
||
170 | MM3_Init(); |
||
171 | #endif |
||
172 | #ifdef USE_MK3MAG |
||
173 | MK3MAG_Init(); |
||
174 | #endif |
||
1342 | Lion | 175 | |
176 | CapCalc_Init(); |
||
1227 | killagreg | 177 | |
178 | // enable interrupts global |
||
179 | sei(); |
||
180 | |||
181 | printf("\n\r==================================="); |
||
182 | printf("\n\rFlightControl"); |
||
183 | printf("\n\rHardware: %d.%d", BoardRelease/10, BoardRelease%10); |
||
184 | if(CPUType == ATMEGA644P) |
||
185 | printf("\r\n CPU: Atmega644p"); |
||
186 | else |
||
187 | printf("\r\n CPU: Atmega644"); |
||
188 | printf("\n\rSoftware: V%d.%d%c",VERSION_MAJOR, VERSION_MINOR, VERSION_PATCH + 'a'); |
||
189 | printf("\n\r==================================="); |
||
190 | GRN_ON; |
||
191 | |||
192 | // Parameter Set handling |
||
193 | ParamSet_Init(); |
||
194 | |||
195 | // Check connected BL-Ctrls |
||
196 | printf("\n\rFound BL-Ctrl: "); |
||
197 | motor_read = 0; |
||
198 | UpdateMotor = 0; |
||
199 | SendMotorData(); |
||
200 | while(!UpdateMotor); |
||
201 | motor_read = 0; // read the first I2C-Data |
||
202 | for(i = 0; i < MAX_MOTORS; i++) |
||
203 | { |
||
204 | UpdateMotor = 0; |
||
205 | SendMotorData(); |
||
206 | while(!UpdateMotor); |
||
207 | if(Motor[i].Present) printf("%d ",i+1); |
||
208 | } |
||
209 | for(i = 0; i < MAX_MOTORS; i++) |
||
210 | { |
||
211 | if(!Motor[i].Present && Mixer.Motor[i][MIX_GAS] > 0) printf("\n\r\n\r!! MISSING BL-CTRL: %d !!",i+1); |
||
212 | Motor[i].Error = 0; |
||
213 | } |
||
214 | printf("\n\r==================================="); |
||
215 | |||
216 | |||
217 | if(GetParamWord(PID_ACC_NICK) > 2048) |
||
218 | { |
||
219 | printf("\n\rACC not calibrated!"); |
||
220 | } |
||
221 | |||
222 | //wait for a short time (otherwise the RC channel check won't work below) |
||
223 | timer = SetDelay(500); |
||
224 | while(!CheckDelay(timer)); |
||
225 | |||
226 | if(ParamSet.GlobalConfig & CFG_HEIGHT_CONTROL) |
||
227 | { |
||
228 | printf("\n\rCalibrating air pressure sensor.."); |
||
229 | timer = SetDelay(1000); |
||
230 | SearchAirPressureOffset(); |
||
231 | while (!CheckDelay(timer)); |
||
232 | printf("OK\n\r"); |
||
233 | } |
||
234 | |||
235 | #ifdef USE_NAVICTRL |
||
236 | printf("\n\rSupport for NaviCtrl"); |
||
237 | #ifdef USE_RC_DSL |
||
238 | printf("\r\nSupport for DSL RC at 2nd UART"); |
||
239 | #endif |
||
240 | #ifdef USE_RC_SPECTRUM |
||
241 | printf("\r\nSupport for SPECTRUM RC at 2nd UART"); |
||
242 | #endif |
||
243 | #endif |
||
244 | |||
245 | #ifdef USE_KILLAGREG |
||
246 | printf("\n\rSupport for MicroMag3 Compass"); |
||
247 | #endif |
||
248 | |||
249 | #ifdef USE_MK3MAG |
||
250 | printf("\n\rSupport for MK3MAG Compass"); |
||
251 | #endif |
||
252 | |||
253 | #if (defined (USE_KILLAGREG) || defined (USE_MK3MAG)) |
||
254 | if(CPUType == ATMEGA644P) printf("\n\rSupport for GPS at 2nd UART"); |
||
255 | else printf("\n\rSupport for GPS at 1st UART"); |
||
256 | #endif |
||
257 | |||
258 | |||
259 | SetNeutral(NO_ACC_CALIB); |
||
260 | |||
261 | RED_OFF; |
||
262 | |||
263 | BeepTime = 2000; |
||
264 | ExternControl.Digital[0] = 0x55; |
||
265 | |||
266 | |||
267 | printf("\n\rControl: "); |
||
268 | if (ParamSet.GlobalConfig & CFG_HEADING_HOLD) printf("HeadingHold"); |
||
269 | else printf("Neutral (ACC-Mode)"); |
||
270 | |||
271 | printf("\n\n\r"); |
||
272 | |||
273 | LCD_Clear(); |
||
274 | |||
275 | I2CTimeout = 5000; |
||
276 | while (1) |
||
277 | { |
||
278 | if(UpdateMotor && ADReady) // control interval |
||
279 | { |
||
280 | UpdateMotor = 0; // reset Flag, is enabled every 2 ms by ISR of timer0 |
||
281 | |||
282 | //J4HIGH; |
||
283 | MotorControl(); |
||
284 | //J4LOW; |
||
285 | |||
286 | SendMotorData(); // the flight control code |
||
287 | RED_OFF; |
||
288 | |||
289 | |||
290 | |||
291 | if(PcAccess) PcAccess--; |
||
292 | else |
||
293 | { |
||
294 | ExternControl.Config = 0; |
||
295 | ExternStickNick= 0; |
||
296 | ExternStickRoll = 0; |
||
297 | ExternStickYaw = 0; |
||
298 | } |
||
299 | if(RC_Quality) RC_Quality--; |
||
300 | |||
301 | #ifdef USE_NAVICTRL |
||
302 | if(NCDataOkay) |
||
303 | { |
||
304 | if(--NCDataOkay == 0) // no data from NC |
||
305 | { // set gps control sticks neutral |
||
306 | GPSStickNick = 0; |
||
307 | GPSStickRoll = 0; |
||
308 | NCSerialDataOkay = 0; |
||
309 | } |
||
310 | } |
||
311 | #endif |
||
312 | |||
313 | if(!--I2CTimeout || MissingMotor) // try to reset the i2c if motor is missing ot timeout |
||
314 | { |
||
315 | RED_ON; |
||
316 | if(!I2CTimeout) |
||
317 | { |
||
318 | I2C_Reset(); |
||
319 | I2CTimeout = 5; |
||
320 | } |
||
321 | if((BeepModulation == 0xFFFF) && (MKFlags & MKFLAG_MOTOR_RUN) ) |
||
322 | { |
||
323 | BeepTime = 10000; // 1 second |
||
324 | BeepModulation = 0x0080; |
||
325 | } |
||
326 | } |
||
327 | else |
||
328 | { |
||
329 | RED_OFF; |
||
330 | } |
||
331 | |||
332 | // allow Serial Data Transmit if motors must not updated or motors are not running |
||
333 | if( !UpdateMotor || !(MKFlags & MKFLAG_MOTOR_RUN) ) |
||
334 | { |
||
335 | USART0_TransmitTxData(); |
||
336 | } |
||
337 | USART0_ProcessRxData(); |
||
338 | |||
339 | if(CheckDelay(timer)) |
||
340 | { |
||
341 | if(UBat < ParamSet.LowVoltageWarning) |
||
342 | { |
||
343 | BeepModulation = 0x0300; |
||
344 | if(!BeepTime ) |
||
345 | { |
||
346 | BeepTime = 6000; // 0.6 seconds |
||
347 | } |
||
348 | } |
||
349 | #ifdef USE_NAVICTRL |
||
350 | SPI_StartTransmitPacket(); |
||
351 | SendSPI = 4; |
||
352 | #endif |
||
353 | timer = SetDelay(20); // every 20 ms |
||
354 | } |
||
1342 | Lion | 355 | CapCalc_Update(); |
1227 | killagreg | 356 | LED_Update(); |
357 | } |
||
358 | |||
359 | #ifdef USE_NAVICTRL |
||
360 | if(!SendSPI) |
||
361 | { // SendSPI is decremented in timer0.c with a rate of 9.765 kHz. |
||
362 | // within the SPI_TransmitByte() routine the value is set to 4. |
||
363 | // I.e. the SPI_TransmitByte() is called at a rate of 9.765 kHz/4= 2441.25 Hz, |
||
364 | // and therefore the time of transmission of a complete spi-packet (32 bytes) is 32*4/9.765 kHz = 13.1 ms. |
||
365 | SPI_TransmitByte(); |
||
366 | } |
||
367 | #endif |
||
368 | } |
||
369 | return (1); |
||
370 | } |
||
371 |