Rev 1544 | Only display areas with differences | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed
Rev 1544 | Rev 1545 | ||
---|---|---|---|
1 | // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
1 | // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
2 | // + Copyright (c) Holger Buss, Ingo Busker |
2 | // + Copyright (c) Holger Buss, Ingo Busker |
3 | // + Nur für den privaten Gebrauch |
3 | // + Nur für den privaten Gebrauch |
4 | // + www.MikroKopter.com |
4 | // + www.MikroKopter.com |
5 | // + porting the sources to other systems or using the software on other systems (except hardware from www.mikrokopter.de) is not allowed |
5 | // + porting the sources to other systems or using the software on other systems (except hardware from www.mikrokopter.de) is not allowed |
6 | // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
6 | // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
7 | // + Es gilt für das gesamte Projekt (Hardware, Software, Binärfiles, Sourcecode und Dokumentation), |
7 | // + Es gilt für das gesamte Projekt (Hardware, Software, Binärfiles, Sourcecode und Dokumentation), |
8 | // + dass eine Nutzung (auch auszugsweise) nur für den privaten und nicht-kommerziellen Gebrauch zulässig ist. |
8 | // + dass eine Nutzung (auch auszugsweise) nur für den privaten und nicht-kommerziellen Gebrauch zulässig ist. |
9 | // + Sollten direkte oder indirekte kommerzielle Absichten verfolgt werden, ist mit uns (info@mikrokopter.de) Kontakt |
9 | // + Sollten direkte oder indirekte kommerzielle Absichten verfolgt werden, ist mit uns (info@mikrokopter.de) Kontakt |
10 | // + bzgl. der Nutzungsbedingungen aufzunehmen. |
10 | // + bzgl. der Nutzungsbedingungen aufzunehmen. |
11 | // + Eine kommerzielle Nutzung ist z.B.Verkauf von MikroKoptern, Bestückung und Verkauf von Platinen oder Bausätzen, |
11 | // + Eine kommerzielle Nutzung ist z.B.Verkauf von MikroKoptern, Bestückung und Verkauf von Platinen oder Bausätzen, |
12 | // + Verkauf von Luftbildaufnahmen, usw. |
12 | // + Verkauf von Luftbildaufnahmen, usw. |
13 | // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
13 | // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
14 | // + Werden Teile des Quellcodes (mit oder ohne Modifikation) weiterverwendet oder veröffentlicht, |
14 | // + Werden Teile des Quellcodes (mit oder ohne Modifikation) weiterverwendet oder veröffentlicht, |
15 | // + unterliegen sie auch diesen Nutzungsbedingungen und diese Nutzungsbedingungen incl. Copyright müssen dann beiliegen |
15 | // + unterliegen sie auch diesen Nutzungsbedingungen und diese Nutzungsbedingungen incl. Copyright müssen dann beiliegen |
16 | // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
16 | // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
17 | // + Sollte die Software (auch auszugesweise) oder sonstige Informationen des MikroKopter-Projekts |
17 | // + Sollte die Software (auch auszugesweise) oder sonstige Informationen des MikroKopter-Projekts |
18 | // + auf anderen Webseiten oder Medien veröffentlicht werden, muss unsere Webseite "http://www.mikrokopter.de" |
18 | // + auf anderen Webseiten oder Medien veröffentlicht werden, muss unsere Webseite "http://www.mikrokopter.de" |
19 | // + eindeutig als Ursprung verlinkt und genannt werden |
19 | // + eindeutig als Ursprung verlinkt und genannt werden |
20 | // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
20 | // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
21 | // + Keine Gewähr auf Fehlerfreiheit, Vollständigkeit oder Funktion |
21 | // + Keine Gewähr auf Fehlerfreiheit, Vollständigkeit oder Funktion |
22 | // + Benutzung auf eigene Gefahr |
22 | // + Benutzung auf eigene Gefahr |
23 | // + Wir übernehmen keinerlei Haftung für direkte oder indirekte Personen- oder Sachschäden |
23 | // + Wir übernehmen keinerlei Haftung für direkte oder indirekte Personen- oder Sachschäden |
24 | // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
24 | // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
25 | // + Die Portierung der Software (oder Teile davon) auf andere Systeme (ausser der Hardware von www.mikrokopter.de) ist nur |
25 | // + Die Portierung der Software (oder Teile davon) auf andere Systeme (ausser der Hardware von www.mikrokopter.de) ist nur |
26 | // + mit unserer Zustimmung zulässig |
26 | // + mit unserer Zustimmung zulässig |
27 | // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
27 | // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
28 | // + Die Funktion printf_P() unterliegt ihrer eigenen Lizenz und ist hiervon nicht betroffen |
28 | // + Die Funktion printf_P() unterliegt ihrer eigenen Lizenz und ist hiervon nicht betroffen |
29 | // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
29 | // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
30 | // + Redistributions of source code (with or without modifications) must retain the above copyright notice, |
30 | // + Redistributions of source code (with or without modifications) must retain the above copyright notice, |
31 | // + this list of conditions and the following disclaimer. |
31 | // + this list of conditions and the following disclaimer. |
32 | // + * Neither the name of the copyright holders nor the names of contributors may be used to endorse or promote products derived |
32 | // + * Neither the name of the copyright holders nor the names of contributors may be used to endorse or promote products derived |
33 | // + from this software without specific prior written permission. |
33 | // + from this software without specific prior written permission. |
34 | // + * The use of this project (hardware, software, binary files, sources and documentation) is only permittet |
34 | // + * The use of this project (hardware, software, binary files, sources and documentation) is only permittet |
35 | // + for non-commercial use (directly or indirectly) |
35 | // + for non-commercial use (directly or indirectly) |
36 | // + Commercial use (for excample: selling of MikroKopters, selling of PCBs, assembly, ...) is only permitted |
36 | // + Commercial use (for excample: selling of MikroKopters, selling of PCBs, assembly, ...) is only permitted |
37 | // + with our written permission |
37 | // + with our written permission |
38 | // + * If sources or documentations are redistributet on other webpages, out webpage (http://www.MikroKopter.de) must be |
38 | // + * If sources or documentations are redistributet on other webpages, out webpage (http://www.MikroKopter.de) must be |
39 | // + clearly linked as origin |
39 | // + clearly linked as origin |
40 | // + * porting to systems other than hardware from www.mikrokopter.de is not allowed |
40 | // + * porting to systems other than hardware from www.mikrokopter.de is not allowed |
41 | // + THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
41 | // + THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
42 | // + AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
42 | // + AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
43 | // + IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
43 | // + IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
44 | // + ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE |
44 | // + ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE |
45 | // + LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
45 | // + LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
46 | // + CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
46 | // + CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
47 | // + SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
47 | // + SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
48 | // + INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
48 | // + INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
49 | // + CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
49 | // + CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
50 | // + ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
50 | // + ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
51 | // + POSSIBILITY OF SUCH DAMAGE. |
51 | // + POSSIBILITY OF SUCH DAMAGE. |
52 | // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
52 | // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
53 | #include <avr/boot.h> |
53 | #include <avr/boot.h> |
54 | 54 | ||
55 | #include <avr/io.h> |
55 | #include <avr/io.h> |
56 | #include <avr/interrupt.h> |
56 | #include <avr/interrupt.h> |
57 | #include <util/delay.h> |
57 | #include <util/delay.h> |
58 | 58 | ||
59 | #include "main.h" |
59 | #include "main.h" |
60 | #include "timer0.h" |
60 | #include "timer0.h" |
61 | #include "timer2.h" |
61 | #include "timer2.h" |
62 | #include "uart0.h" |
62 | #include "uart0.h" |
63 | #include "uart1.h" |
63 | #include "uart1.h" |
64 | #include "led.h" |
64 | #include "led.h" |
65 | #include "menu.h" |
65 | #include "menu.h" |
66 | #include "fc.h" |
66 | #include "fc.h" |
67 | #include "rc.h" |
67 | #include "rc.h" |
68 | #include "analog.h" |
68 | #include "analog.h" |
69 | #include "printf_P.h" |
69 | #include "printf_P.h" |
70 | #ifdef USE_KILLAGREG |
70 | #ifdef USE_KILLAGREG |
71 | #include "mm3.h" |
71 | #include "mm3.h" |
72 | #endif |
72 | #endif |
73 | #ifdef USE_NAVICTRL |
73 | #ifdef USE_NAVICTRL |
74 | #include "spi.h" |
74 | #include "spi.h" |
75 | #endif |
75 | #endif |
76 | #ifdef USE_MK3MAG |
76 | #ifdef USE_MK3MAG |
77 | #include "mk3mag.h" |
77 | #include "mk3mag.h" |
78 | #endif |
78 | #endif |
79 | #include "twimaster.h" |
79 | #include "twimaster.h" |
80 | #include "eeprom.h" |
80 | #include "eeprom.h" |
81 | 81 | ||
82 | 82 | ||
83 | uint8_t BoardRelease = 10; |
83 | uint8_t BoardRelease = 10; |
84 | uint8_t CPUType = ATMEGA644; |
84 | uint8_t CPUType = ATMEGA644P; |
85 | uint8_t LowVoltageWarning = 94; |
85 | uint8_t LowVoltageWarning = 94; |
86 | uint16_t FlightMinutes = 0, FlightMinutesTotal = 0; |
86 | uint16_t FlightMinutes = 0, FlightMinutesTotal = 0; |
87 | 87 | ||
88 | uint8_t GetCPUType(void) |
88 | uint8_t GetCPUType(void) |
89 | { // works only after reset or power on when the registers have default values |
89 | { // works only after reset or power on when the registers have default values |
90 | uint8_t CPUType = ATMEGA644; |
90 | uint8_t CPUType = ATMEGA644; |
91 | if( (UCSR1A == 0x20) && (UCSR1C == 0x06) ) CPUType = ATMEGA644P; // initial Values for 644P after reset |
91 | if( (UCSR1A == 0x20) && (UCSR1C == 0x06) ) CPUType = ATMEGA644P; // initial Values for 644P after reset |
92 | return CPUType; |
92 | return CPUType; |
93 | } |
93 | } |
94 | 94 | ||
95 | 95 | ||
96 | uint8_t GetBoardRelease(void) |
96 | uint8_t GetBoardRelease(void) |
97 | { |
97 | { |
98 | uint8_t BoardRelease = 10; |
98 | uint8_t BoardRelease = 10; |
99 | // the board release is coded via the pull up or down the 2 status LED |
99 | // the board release is coded via the pull up or down the 2 status LED |
100 | 100 | ||
101 | PORTB &= ~((1 << PORTB1)|(1 << PORTB0)); // set tristate |
101 | PORTB &= ~((1 << PORTB1)|(1 << PORTB0)); // set tristate |
102 | DDRB &= ~((1 << DDB0)|(1 << DDB0)); // set port direction as input |
102 | DDRB &= ~((1 << DDB0)|(1 << DDB0)); // set port direction as input |
103 | 103 | ||
104 | _delay_loop_2(1000); // make some delay |
104 | _delay_loop_2(1000); // make some delay |
105 | 105 | ||
106 | switch( PINB & ((1<<PINB1)|(1<<PINB0)) ) |
106 | switch( PINB & ((1<<PINB1)|(1<<PINB0)) ) |
107 | { |
107 | { |
108 | case 0x00: |
108 | case 0x00: |
109 | BoardRelease = 10; // 1.0 |
109 | BoardRelease = 10; // 1.0 |
110 | break; |
110 | break; |
111 | case 0x01: |
111 | case 0x01: |
112 | BoardRelease = 11; // 1.1 or 1.2 |
112 | BoardRelease = 11; // 1.1 or 1.2 |
113 | break; |
113 | break; |
114 | case 0x02: |
114 | case 0x02: |
115 | BoardRelease = 20; // 2.0 |
115 | BoardRelease = 20; // 2.0 |
116 | break; |
116 | break; |
117 | case 0x03: |
117 | case 0x03: |
118 | BoardRelease = 13; // 1.3 |
118 | BoardRelease = 13; // 1.3 |
119 | break; |
119 | break; |
120 | default: |
120 | default: |
121 | break; |
121 | break; |
122 | } |
122 | } |
123 | // set LED ports as output |
123 | // set LED ports as output |
124 | DDRB |= (1<<DDB1)|(1<<DDB0); |
124 | DDRB |= (1<<DDB1)|(1<<DDB0); |
125 | RED_ON; |
125 | RED_ON; |
126 | GRN_OFF; |
126 | GRN_OFF; |
127 | return BoardRelease; |
127 | return BoardRelease; |
128 | } |
128 | } |
129 | 129 | ||
130 | void LipoDetection(uint8_t print) |
130 | void LipoDetection(uint8_t print) |
131 | { |
131 | { |
132 | #define MIN_VOLTAGE_WARNING 93 // minimum low voltage warning to protect LiPO |
132 | #define MIN_VOLTAGE_WARNING 93 // minimum low voltage warning to protect LiPO |
133 | uint16_t timer; |
133 | uint16_t timer; |
134 | if(print) printf("\n\rBatt:"); |
134 | if(print) printf("\n\rBatt:"); |
135 | if(ParamSet.LowVoltageWarning < 50) // below 5.0 V the value is interpreted as single cell low volatage warning |
135 | if(ParamSet.LowVoltageWarning < 50) // below 5.0 V the value is interpreted as single cell low volatage warning |
136 | { |
136 | { |
137 | timer = SetDelay(500); // wait at least 500 ms to get stable adc readings |
137 | timer = SetDelay(500); // wait at least 500 ms to get stable adc readings |
138 | if(print) while(!CheckDelay(timer)); |
138 | if(print) while(!CheckDelay(timer)); |
139 | if(UBat < 130) // less than 13.0 V must be a 3S Lipo |
139 | if(UBat < 130) // less than 13.0 V must be a 3S Lipo |
140 | { |
140 | { |
141 | LowVoltageWarning = 3 * ParamSet.LowVoltageWarning; |
141 | LowVoltageWarning = 3 * ParamSet.LowVoltageWarning; |
142 | if(print) |
142 | if(print) |
143 | { |
143 | { |
144 | Beep(3, 200); |
144 | Beep(3, 200); |
145 | printf(" 3 Cells "); |
145 | printf(" 3 Cells "); |
146 | } |
146 | } |
147 | } |
147 | } |
148 | else // <= 13.0V |
148 | else // <= 13.0V |
149 | { |
149 | { |
150 | LowVoltageWarning = 4 * ParamSet.LowVoltageWarning; |
150 | LowVoltageWarning = 4 * ParamSet.LowVoltageWarning; |
151 | if(print) |
151 | if(print) |
152 | { |
152 | { |
153 | Beep(4, 200); |
153 | Beep(4, 200); |
154 | printf(" 4 Cells "); |
154 | printf(" 4 Cells "); |
155 | } |
155 | } |
156 | } |
156 | } |
157 | } |
157 | } |
158 | else // classic settings |
158 | else // classic settings |
159 | { |
159 | { |
160 | LowVoltageWarning = ParamSet.LowVoltageWarning; |
160 | LowVoltageWarning = ParamSet.LowVoltageWarning; |
161 | } |
161 | } |
162 | //if(LowVoltageWarning < MIN_VOLTAGE_WARNING) LowVoltageWarning = MIN_VOLTAGE_WARNING; |
162 | //if(LowVoltageWarning < MIN_VOLTAGE_WARNING) LowVoltageWarning = MIN_VOLTAGE_WARNING; |
163 | if(print) printf("Low Warning level: %d.%dV", LowVoltageWarning/10, LowVoltageWarning%10); |
163 | if(print) printf("Low Warning level: %d.%dV", LowVoltageWarning/10, LowVoltageWarning%10); |
164 | } |
164 | } |
165 | 165 | ||
166 | int16_t main (void) |
166 | int16_t main (void) |
167 | { |
167 | { |
168 | uint16_t timer, flighttimer; |
168 | uint16_t timer, flighttimer; |
169 | uint8_t i; |
169 | uint8_t i; |
170 | 170 | ||
171 | // disable interrupts global |
171 | // disable interrupts global |
172 | cli(); |
172 | cli(); |
173 | 173 | ||
174 | // analyze hardware environment |
174 | // analyze hardware environment |
175 | CPUType = GetCPUType(); |
175 | CPUType = GetCPUType(); |
176 | BoardRelease = GetBoardRelease(); |
176 | BoardRelease = GetBoardRelease(); |
177 | 177 | ||
178 | // disable watchdog |
178 | // disable watchdog |
179 | MCUSR &=~(1<<WDRF); |
179 | MCUSR &=~(1<<WDRF); |
180 | WDTCSR |= (1<<WDCE)|(1<<WDE); |
180 | WDTCSR |= (1<<WDCE)|(1<<WDE); |
181 | WDTCSR = 0; |
181 | WDTCSR = 0; |
182 | 182 | ||
183 | BeepTime = 2000; |
183 | BeepTime = 2000; |
184 | 184 | ||
185 | PPM_in[CH_GAS] = 0; |
185 | PPM_in[CH_GAS] = 0; |
186 | StickYaw = 0; |
186 | StickYaw = 0; |
187 | StickRoll = 0; |
187 | StickRoll = 0; |
188 | StickNick = 0; |
188 | StickNick = 0; |
189 | 189 | ||
190 | RED_OFF; |
190 | RED_OFF; |
191 | 191 | ||
192 | // initalize modules |
192 | // initalize modules |
193 | LED_Init(); |
193 | LED_Init(); |
194 | TIMER0_Init(); |
194 | TIMER0_Init(); |
195 | TIMER2_Init(); |
195 | TIMER2_Init(); |
196 | USART0_Init(); |
196 | USART0_Init(); |
197 | if(CPUType == ATMEGA644P) USART1_Init(); |
197 | if(CPUType == ATMEGA644P) USART1_Init(); |
198 | RC_Init(); |
198 | RC_Init(); |
199 | ADC_Init(); |
199 | ADC_Init(); |
200 | I2C_Init(); |
200 | I2C_Init(); |
201 | #ifdef USE_NAVICTRL |
201 | #ifdef USE_NAVICTRL |
202 | SPI_MasterInit(); |
202 | SPI_MasterInit(); |
203 | #endif |
203 | #endif |
204 | #ifdef USE_KILLAGREG |
204 | #ifdef USE_KILLAGREG |
205 | MM3_Init(); |
205 | MM3_Init(); |
206 | #endif |
206 | #endif |
207 | #ifdef USE_MK3MAG |
207 | #ifdef USE_MK3MAG |
208 | MK3MAG_Init(); |
208 | MK3MAG_Init(); |
209 | #endif |
209 | #endif |
210 | 210 | ||
211 | // enable interrupts global |
211 | // enable interrupts global |
212 | sei(); |
212 | sei(); |
213 | 213 | ||
214 | printf("\n\r==================================="); |
214 | printf("\n\r==================================="); |
215 | printf("\n\rFlightControl"); |
215 | printf("\n\rFlightControl"); |
216 | printf("\n\rHardware: %d.%d", BoardRelease/10, BoardRelease%10); |
216 | printf("\n\rHardware: %d.%d", BoardRelease/10, BoardRelease%10); |
217 | printf("\n\rthe use of this software is only permitted \n\ron original MikroKopter-Hardware"); |
217 | printf("\n\rthe use of this software is only permitted \n\ron original MikroKopter-Hardware"); |
218 | printf("\n\rwww.MikroKopter.de (c) HiSystems GmbH"); |
218 | printf("\n\rwww.MikroKopter.de (c) HiSystems GmbH"); |
219 | printf("\n\r==================================="); |
219 | printf("\n\r==================================="); |
220 | 220 | ||
221 | if(CPUType == ATMEGA644P) |
221 | if(CPUType == ATMEGA644P) |
222 | printf("\r\n CPU: Atmega644p"); |
222 | printf("\r\n CPU: Atmega644p"); |
223 | else |
223 | else |
224 | printf("\r\n CPU: Atmega644"); |
224 | printf("\r\n CPU: Atmega644"); |
225 | printf("\n\rSoftware: V%d.%d%c",VERSION_MAJOR, VERSION_MINOR, VERSION_PATCH + 'a'); |
225 | printf("\n\rSoftware: V%d.%d%c",VERSION_MAJOR, VERSION_MINOR, VERSION_PATCH + 'a'); |
226 | printf("\n\r==================================="); |
226 | printf("\n\r==================================="); |
227 | GRN_ON; |
227 | GRN_ON; |
228 | 228 | ||
229 | // Parameter Set handling |
229 | // Parameter Set handling |
230 | ParamSet_Init(); |
230 | ParamSet_Init(); |
231 | 231 | ||
232 | // Check connected BL-Ctrls |
232 | // Check connected BL-Ctrls |
233 | printf("\n\rFound BL-Ctrl: "); |
233 | printf("\n\rFound BL-Ctrl: "); |
234 | motor_read = 0; |
234 | motor_read = 0; |
235 | UpdateMotor = 0; |
235 | UpdateMotor = 0; |
236 | SendMotorData(); |
236 | SendMotorData(); |
237 | while(!UpdateMotor); |
237 | while(!UpdateMotor); |
238 | motor_read = 0; // read the first I2C-Data |
238 | motor_read = 0; // read the first I2C-Data |
239 | timer = SetDelay(2000); // set timeout to 2 seconds |
239 | timer = SetDelay(2000); // set timeout to 2 seconds |
240 | for(i = 0; i < MAX_MOTORS; i++) |
240 | for(i = 0; i < MAX_MOTORS; i++) |
241 | { |
241 | { |
242 | UpdateMotor = 0; |
242 | UpdateMotor = 0; |
243 | SendMotorData(); |
243 | SendMotorData(); |
244 | while(!UpdateMotor); // wait 2 ms to finish data transfer |
244 | while(!UpdateMotor); // wait 2 ms to finish data transfer |
245 | if(Mixer.Motor[i][MIX_GAS] > 0) // wait max 2 sec for the BL-Ctrls to wake up |
245 | if(Mixer.Motor[i][MIX_GAS] > 0) // wait max 2 sec for the BL-Ctrls to wake up |
246 | { |
246 | { |
247 | while(!CheckDelay(timer) && !(Motor[i].Present) ) // while not timeout and motor is not present |
247 | while(!CheckDelay(timer) && !(Motor[i].Present) ) // while not timeout and motor is not present |
248 | { |
248 | { |
249 | UpdateMotor = 0; |
249 | UpdateMotor = 0; |
250 | SendMotorData(); |
250 | SendMotorData(); |
251 | while(!UpdateMotor); // wait 2 ms |
251 | while(!UpdateMotor); // wait 2 ms |
252 | } |
252 | } |
253 | } |
253 | } |
254 | if(Motor[i].Present) printf("%d ",i+1); |
254 | if(Motor[i].Present) printf("%d ",i+1); |
255 | } |
255 | } |
256 | for(i = 0; i < MAX_MOTORS; i++) |
256 | for(i = 0; i < MAX_MOTORS; i++) |
257 | { |
257 | { |
258 | if(!Motor[i].Present && Mixer.Motor[i][MIX_GAS] > 0) |
258 | if(!Motor[i].Present && Mixer.Motor[i][MIX_GAS] > 0) |
259 | { |
259 | { |
260 | printf("\n\r\n\r!! MISSING BL-CTRL: %d !!",i+1); |
260 | printf("\n\r\n\r!! MISSING BL-CTRL: %d !!",i+1); |
261 | Servo_On(); // just in case the FC would be used as camera-stabilizer |
261 | Servo_On(); // just in case the FC would be used as camera-stabilizer |
262 | } |
262 | } |
263 | Motor[i].Error = 0; |
263 | Motor[i].Error = 0; |
264 | } |
264 | } |
265 | printf("\n\r==================================="); |
265 | printf("\n\r==================================="); |
266 | SendMotorData(); |
266 | SendMotorData(); |
267 | 267 | ||
268 | //wait for a short time (otherwise the RC channel check won't work below) |
268 | //wait for a short time (otherwise the RC channel check won't work below) |
269 | timer = SetDelay(500); |
269 | timer = SetDelay(500); |
270 | while(!CheckDelay(timer)); |
270 | while(!CheckDelay(timer)); |
271 | 271 | ||
272 | if(ParamSet.Config0 & CFG0_AIRPRESS_SENSOR) |
272 | if(ParamSet.Config0 & CFG0_AIRPRESS_SENSOR) |
273 | { |
273 | { |
274 | printf("\n\rCalibrating air pressure sensor.."); |
274 | printf("\n\rCalibrating air pressure sensor.."); |
275 | timer = SetDelay(1000); |
275 | timer = SetDelay(1000); |
276 | SearchAirPressureOffset(); |
276 | SearchAirPressureOffset(); |
277 | while (!CheckDelay(timer)); |
277 | while (!CheckDelay(timer)); |
278 | printf("OK\n\r"); |
278 | printf("OK\n\r"); |
279 | } |
279 | } |
280 | 280 | ||
281 | #ifdef USE_NAVICTRL |
281 | #ifdef USE_NAVICTRL |
282 | printf("\n\rSupport for NaviCtrl"); |
282 | printf("\n\rSupport for NaviCtrl"); |
283 | #ifdef USE_RC_DSL |
283 | #ifdef USE_RC_DSL |
284 | printf("\r\nSupport for DSL RC at 2nd UART"); |
284 | printf("\r\nSupport for DSL RC at 2nd UART"); |
285 | #endif |
285 | #endif |
286 | #ifdef USE_RC_SPECTRUM |
286 | #ifdef USE_RC_SPECTRUM |
287 | printf("\r\nSupport for SPEKTRUM RC at 2nd UART"); |
287 | printf("\r\nSupport for SPEKTRUM RC at 2nd UART"); |
- | 288 | #endif |
|
- | 289 | #ifdef USE_RC_JENNIC // LPD: FC_JN_Receiver support added |
|
- | 290 | printf("\r\nSupport for JENNIC RC at 2nd UART"); |
|
288 | #endif |
291 | #endif |
289 | #endif |
292 | #endif |
290 | 293 | ||
291 | #ifdef USE_KILLAGREG |
294 | #ifdef USE_KILLAGREG |
292 | printf("\n\rSupport for MicroMag3 Compass"); |
295 | printf("\n\rSupport for MicroMag3 Compass"); |
293 | #endif |
296 | #endif |
294 | 297 | ||
295 | #ifdef USE_MK3MAG |
298 | #ifdef USE_MK3MAG |
296 | printf("\n\rSupport for MK3MAG Compass"); |
299 | printf("\n\rSupport for MK3MAG Compass"); |
297 | #endif |
300 | #endif |
298 | 301 | ||
299 | #if (defined (USE_KILLAGREG) || defined (USE_MK3MAG)) |
302 | #if (defined (USE_KILLAGREG) || defined (USE_MK3MAG)) |
300 | if(CPUType == ATMEGA644P) printf("\n\rSupport for GPS at 2nd UART"); |
303 | if(CPUType == ATMEGA644P) printf("\n\rSupport for GPS at 2nd UART"); |
301 | else printf("\n\rSupport for GPS at 1st UART"); |
304 | else printf("\n\rSupport for GPS at 1st UART"); |
302 | #endif |
305 | #endif |
303 | 306 | ||
304 | // init variables |
307 | // init variables |
305 | SetNeutral(NO_ACC_CALIB); |
308 | SetNeutral(NO_ACC_CALIB); |
306 | 309 | ||
307 | RED_OFF; |
310 | RED_OFF; |
308 | 311 | ||
309 | BeepTime = 2000; |
312 | BeepTime = 2000; |
310 | ExternControl.Digital[0] = 0x55; |
313 | ExternControl.Digital[0] = 0x55; |
311 | 314 | ||
312 | 315 | ||
313 | FlightMinutes = GetParamWord(PID_FLIGHT_MINUTES); |
316 | FlightMinutes = GetParamWord(PID_FLIGHT_MINUTES); |
314 | FlightMinutesTotal = GetParamWord(PID_FLIGHT_MINUTES_TOTAL); |
317 | FlightMinutesTotal = GetParamWord(PID_FLIGHT_MINUTES_TOTAL); |
315 | flighttimer = 0; |
318 | flighttimer = 0; |
316 | if( (FlightMinutesTotal == 0xFFFF) || (FlightMinutes == 0xFFFF) ) |
319 | if( (FlightMinutesTotal == 0xFFFF) || (FlightMinutes == 0xFFFF) ) |
317 | { |
320 | { |
318 | FlightMinutes = 0; |
321 | FlightMinutes = 0; |
319 | FlightMinutesTotal = 0; |
322 | FlightMinutesTotal = 0; |
320 | } |
323 | } |
321 | printf("\n\rFlight-time %u min Total:%u min", FlightMinutes, FlightMinutesTotal); |
324 | printf("\n\rFlight-time %u min Total:%u min", FlightMinutes, FlightMinutesTotal); |
322 | 325 | ||
323 | 326 | ||
324 | printf("\n\rControl: "); |
327 | printf("\n\rControl: "); |
325 | if (ParamSet.Config0 & CFG0_HEADING_HOLD) printf("HeadingHold"); |
328 | if (ParamSet.Config0 & CFG0_HEADING_HOLD) printf("HeadingHold"); |
326 | else printf("Neutral (ACC-Mode)"); |
329 | else printf("Neutral (ACC-Mode)"); |
327 | 330 | ||
328 | LCD_Clear(); |
331 | LCD_Clear(); |
329 | I2CTimeout = 5000; |
332 | I2CTimeout = 5000; |
330 | LipoDetection(1); |
333 | LipoDetection(1); |
331 | printf("\n\r===================================\n\r"); |
334 | printf("\n\r===================================\n\r"); |
332 | 335 | ||
333 | timer = SetDelay(2000); |
336 | timer = SetDelay(2000); |
334 | 337 | ||
335 | // begin of main loop |
338 | // begin of main loop |
336 | while (1) |
339 | while (1) |
337 | { |
340 | { |
338 | if(UpdateMotor && ADReady) // control interval |
341 | if(UpdateMotor && ADReady) // control interval |
339 | { |
342 | { |
340 | UpdateMotor = 0; // reset Flag, is enabled every 2 ms by ISR of timer0 |
343 | UpdateMotor = 0; // reset Flag, is enabled every 2 ms by ISR of timer0 |
341 | 344 | ||
342 | //J4HIGH; |
345 | //J4HIGH; |
343 | MotorControl(); |
346 | MotorControl(); |
344 | //J4LOW; |
347 | //J4LOW; |
345 | 348 | ||
346 | SendMotorData(); // the flight control code |
349 | SendMotorData(); // the flight control code |
347 | RED_OFF; |
350 | RED_OFF; |
348 | 351 | ||
349 | if(RC_Quality) RC_Quality--; |
352 | if(RC_Quality) RC_Quality--; |
350 | else PPM_INPUT_ON; // if RC-Quality is lost, enable PPM input (could be disabled by a receiver on uart1) |
353 | else PPM_INPUT_ON; // if RC-Quality is lost, enable PPM input (could be disabled by a receiver on uart1) |
351 | 354 | ||
352 | #ifdef USE_NAVICTRL |
355 | #ifdef USE_NAVICTRL |
353 | if(NCDataOkay) |
356 | if(NCDataOkay) |
354 | { |
357 | { |
355 | if(--NCDataOkay == 0) // no data from NC |
358 | if(--NCDataOkay == 0) // no data from NC |
356 | { // set gps control sticks neutral |
359 | { // set gps control sticks neutral |
357 | GPSStickNick = 0; |
360 | GPSStickNick = 0; |
358 | GPSStickRoll = 0; |
361 | GPSStickRoll = 0; |
359 | NCSerialDataOkay = 0; |
362 | NCSerialDataOkay = 0; |
360 | NCGpsZ = 0; |
363 | NCGpsZ = 0; |
361 | } |
364 | } |
362 | } |
365 | } |
363 | #endif |
366 | #endif |
364 | 367 | ||
365 | if(!--I2CTimeout || MissingMotor) // try to reset the i2c if motor is missing ot timeout |
368 | if(!--I2CTimeout || MissingMotor) // try to reset the i2c if motor is missing ot timeout |
366 | { |
369 | { |
367 | RED_ON; |
370 | RED_ON; |
368 | if(!I2CTimeout) |
371 | if(!I2CTimeout) |
369 | { |
372 | { |
370 | I2C_Reset(); |
373 | I2C_Reset(); |
371 | I2CTimeout = 5; |
374 | I2CTimeout = 5; |
372 | DebugOut.Analog[28]++; // I2C-Error |
375 | DebugOut.Analog[28]++; // I2C-Error |
373 | } |
376 | } |
374 | if((BeepModulation == 0xFFFF) && (MKFlags & MKFLAG_MOTOR_RUN) ) |
377 | if((BeepModulation == 0xFFFF) && (MKFlags & MKFLAG_MOTOR_RUN) ) |
375 | { |
378 | { |
376 | BeepTime = 10000; // 1 second |
379 | BeepTime = 10000; // 1 second |
377 | BeepModulation = 0x0080; |
380 | BeepModulation = 0x0080; |
378 | } |
381 | } |
379 | } |
382 | } |
380 | else |
383 | else |
381 | { |
384 | { |
382 | RED_OFF; |
385 | RED_OFF; |
383 | } |
386 | } |
384 | 387 | ||
385 | // allow Serial Data Transmit if motors must not updated or motors are not running |
388 | // allow Serial Data Transmit if motors must not updated or motors are not running |
386 | if( !UpdateMotor || !(MKFlags & MKFLAG_MOTOR_RUN) ) |
389 | if( !UpdateMotor || !(MKFlags & MKFLAG_MOTOR_RUN) ) |
387 | { |
390 | { |
388 | USART0_TransmitTxData(); |
391 | USART0_TransmitTxData(); |
389 | } |
392 | } |
390 | USART0_ProcessRxData(); |
393 | USART0_ProcessRxData(); |
391 | 394 | ||
392 | if(CheckDelay(timer)) |
395 | if(CheckDelay(timer)) |
393 | { |
396 | { |
394 | timer += 20; // every 20 ms |
397 | timer += 20; // every 20 ms |
395 | if(PcAccess) PcAccess--; |
398 | if(PcAccess) PcAccess--; |
396 | else |
399 | else |
397 | { |
400 | { |
398 | ExternControl.Config = 0; |
401 | ExternControl.Config = 0; |
399 | ExternStickNick= 0; |
402 | ExternStickNick= 0; |
400 | ExternStickRoll = 0; |
403 | ExternStickRoll = 0; |
401 | ExternStickYaw = 0; |
404 | ExternStickYaw = 0; |
402 | if((BeepModulation == 0xFFFF) && (RC_Quality == 0)) |
405 | if((BeepModulation == 0xFFFF) && (RC_Quality == 0)) |
403 | { |
406 | { |
404 | BeepTime = 15000; // 1.5 seconds |
407 | BeepTime = 15000; // 1.5 seconds |
405 | BeepModulation = 0x0C00; |
408 | BeepModulation = 0x0C00; |
406 | } |
409 | } |
407 | } |
410 | } |
408 | if(UBat < LowVoltageWarning) |
411 | if(UBat < LowVoltageWarning) |
409 | { |
412 | { |
410 | MKFlags |= MKFLAG_LOWBAT; |
413 | MKFlags |= MKFLAG_LOWBAT; |
411 | BeepModulation = 0x0300; |
414 | BeepModulation = 0x0300; |
412 | if(!BeepTime ) |
415 | if(!BeepTime ) |
413 | { |
416 | { |
414 | BeepTime = 6000; // 0.6 seconds |
417 | BeepTime = 6000; // 0.6 seconds |
415 | } |
418 | } |
416 | } |
419 | } |
417 | else |
420 | else |
418 | { |
421 | { |
419 | MKFlags &= ~MKFLAG_LOWBAT; |
422 | MKFlags &= ~MKFLAG_LOWBAT; |
420 | } |
423 | } |
421 | #ifdef USE_NAVICTRL |
424 | #ifdef USE_NAVICTRL |
422 | SPI_StartTransmitPacket(); |
425 | SPI_StartTransmitPacket(); |
423 | SendSPI = 4; |
426 | SendSPI = 4; |
424 | #endif |
427 | #endif |
425 | 428 | ||
426 | if(!(MKFlags & MKFLAG_MOTOR_RUN)) flighttimer = 1450; // 0.5 minutes round up |
429 | if(!(MKFlags & MKFLAG_MOTOR_RUN)) flighttimer = 1450; // 0.5 minutes round up |
427 | if(++flighttimer == 2930) // one minute |
430 | if(++flighttimer == 2930) // one minute |
428 | { |
431 | { |
429 | flighttimer = 0; |
432 | flighttimer = 0; |
430 | FlightMinutesTotal++; |
433 | FlightMinutesTotal++; |
431 | FlightMinutes++; |
434 | FlightMinutes++; |
432 | SetParamWord(PID_FLIGHT_MINUTES_TOTAL, FlightMinutesTotal); |
435 | SetParamWord(PID_FLIGHT_MINUTES_TOTAL, FlightMinutesTotal); |
433 | SetParamWord(PID_FLIGHT_MINUTES, FlightMinutes); |
436 | SetParamWord(PID_FLIGHT_MINUTES, FlightMinutes); |
434 | timer = SetDelay(20); // in case "timer += 20;" will not work |
437 | timer = SetDelay(20); // in case "timer += 20;" will not work |
435 | } |
438 | } |
436 | }// EOF CheckDelay(timer) |
439 | }// EOF CheckDelay(timer) |
437 | 440 | ||
438 | LED_Update(); |
441 | LED_Update(); |
439 | } |
442 | } |
440 | 443 | ||
441 | #ifdef USE_NAVICTRL |
444 | #ifdef USE_NAVICTRL |
442 | if(!SendSPI) |
445 | if(!SendSPI) |
443 | { // SendSPI is decremented in timer0.c with a rate of 9.765 kHz. |
446 | { // SendSPI is decremented in timer0.c with a rate of 9.765 kHz. |
444 | // within the SPI_TransmitByte() routine the value is set to 4. |
447 | // within the SPI_TransmitByte() routine the value is set to 4. |
445 | // I.e. the SPI_TransmitByte() is called at a rate of 9.765 kHz/4= 2441.25 Hz, |
448 | // I.e. the SPI_TransmitByte() is called at a rate of 9.765 kHz/4= 2441.25 Hz, |
446 | // and therefore the time of transmission of a complete spi-packet (32 bytes) is 32*4/9.765 kHz = 13.1 ms. |
449 | // and therefore the time of transmission of a complete spi-packet (32 bytes) is 32*4/9.765 kHz = 13.1 ms. |
447 | SPI_TransmitByte(); |
450 | SPI_TransmitByte(); |
448 | } |
451 | } |
449 | #endif |
452 | #endif |
450 | } |
453 | } |
451 | return (1); |
454 | return (1); |
452 | } |
455 | } |
453 | 456 | ||
454 | 457 |