Details | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
1755 | - | 1 | /****************************************************************************************************************** |
2 | V0.80g-Arthur-P1 20100922 |
||
3 | ------------------------------------------------------------------------------------------------------------------ |
||
4 | Version includes only support for external HEF4017 for FC1.x hardware, NOT for Twi2Ppm converters for ESCs. |
||
5 | ******************************************************************************************************************/ |
||
6 | // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
||
7 | // + Copyright (c) Holger Buss, Ingo Busker |
||
8 | // + Nur für den privaten Gebrauch / NON-COMMERCIAL USE ONLY |
||
9 | // + www.MikroKopter.com |
||
10 | // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
||
11 | // + Es gilt für das gesamte Projekt (Hardware, Software, Binärfiles, Sourcecode und Dokumentation), |
||
12 | // + dass eine Nutzung (auch auszugsweise) nur für den privaten und nicht-kommerziellen Gebrauch zulässig ist. |
||
13 | // + Sollten direkte oder indirekte kommerzielle Absichten verfolgt werden, ist mit uns (info@mikrokopter.de) Kontakt |
||
14 | // + bzgl. der Nutzungsbedingungen aufzunehmen. |
||
15 | // + Eine kommerzielle Nutzung ist z.B.Verkauf von MikroKoptern, Bestückung und Verkauf von Platinen oder Bausätzen, |
||
16 | // + Verkauf von Luftbildaufnahmen, usw. |
||
17 | // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
||
18 | // + Werden Teile des Quellcodes (mit oder ohne Modifikation) weiterverwendet oder veröffentlicht, |
||
19 | // + unterliegen sie auch diesen Nutzungsbedingungen und diese Nutzungsbedingungen incl. Copyright müssen dann beiliegen |
||
20 | // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
||
21 | // + Sollte die Software (auch auszugesweise) oder sonstige Informationen des MikroKopter-Projekts |
||
22 | // + auf anderen Webseiten oder Medien veröffentlicht werden, muss unsere Webseite "http://www.mikrokopter.de" |
||
23 | // + eindeutig als Ursprung verlinkt und genannt werden |
||
24 | // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
||
25 | // + Keine Gewähr auf Fehlerfreiheit, Vollständigkeit oder Funktion |
||
26 | // + Benutzung auf eigene Gefahr |
||
27 | // + Wir übernehmen keinerlei Haftung für direkte oder indirekte Personen- oder Sachschäden |
||
28 | // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
||
29 | // + Die Portierung oder Nutzung der Software (oder Teile davon) auf andere Systeme (ausser der Hardware von www.mikrokopter.de) ist nur |
||
30 | // + mit unserer Zustimmung zulässig |
||
31 | // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
||
32 | // + Die Funktion printf_P() unterliegt ihrer eigenen Lizenz und ist hiervon nicht betroffen |
||
33 | // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
||
34 | // + Redistributions of source code (with or without modifications) must retain the above copyright notice, |
||
35 | // + this list of conditions and the following disclaimer. |
||
36 | // + * Neither the name of the copyright holders nor the names of contributors may be used to endorse or promote products derived |
||
37 | // + from this software without specific prior written permission. |
||
38 | // + * The use of this project (hardware, software, binary files, sources and documentation) is only permittet |
||
39 | // + for non-commercial use (directly or indirectly) |
||
40 | // + Commercial use (for excample: selling of MikroKopters, selling of PCBs, assembly, ...) is only permitted |
||
41 | // + with our written permission |
||
42 | // + * If sources or documentations are redistributet on other webpages, out webpage (http://www.MikroKopter.de) must be |
||
43 | // + clearly linked as origin |
||
44 | // + * porting the sources to other systems or using the software on other systems (except hardware from www.mikrokopter.de) is not allowed |
||
45 | // + THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
||
46 | // + AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
||
47 | // + IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
||
48 | // + ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE |
||
49 | // + LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
||
50 | // + CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
||
51 | // + SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
||
52 | // + INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
||
53 | // + CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
||
54 | // + ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
||
55 | // + POSSIBILITY OF SUCH DAMAGE. |
||
56 | // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
||
57 | #include "main.h" |
||
58 | |||
59 | |||
60 | unsigned char PlatinenVersion = 10; |
||
61 | unsigned char SendVersionToNavi = 1; |
||
62 | unsigned char BattLowVoltageWarning = 94; |
||
63 | unsigned int FlugMinuten = 0,FlugMinutenGesamt = 0; |
||
64 | unsigned int FlugSekunden = 0; |
||
65 | pVoidFnct_pVoidFnctChar_const_fmt _printf_P; |
||
66 | unsigned char FoundMotors = 0; |
||
67 | |||
68 | void CalMk3Mag(void) |
||
69 | { |
||
70 | static unsigned char stick = 1; |
||
71 | if(PPM_in[EE_Parameter.Kanalbelegung[K_NICK]] > -20) stick = 0; |
||
72 | if((PPM_in[EE_Parameter.Kanalbelegung[K_NICK]] < -70) && !stick) |
||
73 | { |
||
74 | stick = 1; |
||
75 | WinkelOut.CalcState++; |
||
76 | if(WinkelOut.CalcState > 4) |
||
77 | { |
||
78 | // WinkelOut.CalcState = 0; // in Uart.c |
||
79 | beeptime = 1000; |
||
80 | } |
||
81 | else Piep(WinkelOut.CalcState,150); |
||
82 | } |
||
83 | DebugOut.Analog[19] = WinkelOut.CalcState; |
||
84 | } |
||
85 | |||
86 | |||
87 | void LipoDetection(unsigned char print) |
||
88 | { |
||
89 | #define MAX_CELL_VOLTAGE 43 // max cell volatage for LiPO |
||
90 | unsigned int timer, cells; |
||
91 | if(print) printf("\n\rBatt:"); |
||
92 | if(EE_Parameter.UnterspannungsWarnung < 50) // automatische Zellenerkennung |
||
93 | { |
||
94 | timer = SetDelay(500); |
||
95 | if(print) while (!CheckDelay(timer)); |
||
96 | // up to 6s LiPo, less than 2s is technical impossible |
||
97 | for(cells = 2; cells < 7; cells++) |
||
98 | { |
||
99 | if(UBat < cells * MAX_CELL_VOLTAGE) break; |
||
100 | } |
||
101 | |||
102 | BattLowVoltageWarning = cells * EE_Parameter.UnterspannungsWarnung; |
||
103 | if(print) |
||
104 | { |
||
105 | Piep(cells, 200); |
||
106 | printf(" %d Cells ", cells); |
||
107 | } |
||
108 | } |
||
109 | else BattLowVoltageWarning = EE_Parameter.UnterspannungsWarnung; |
||
110 | if(print) printf(" Low warning level: %d.%d",BattLowVoltageWarning/10,BattLowVoltageWarning%10); |
||
111 | } |
||
112 | |||
113 | //############################################################################ |
||
114 | //Hauptprogramm |
||
115 | int main (void) |
||
116 | //############################################################################ |
||
117 | { |
||
118 | unsigned int timer,i,timer2 = 0, timerPolling; |
||
119 | |||
120 | DDRB = 0x00; |
||
121 | PORTB = 0x00; |
||
122 | for(timer = 0; timer < 1000; timer++); // verzögern |
||
123 | #if (defined(__AVR_ATmega1284__) || defined(__AVR_ATmega1284P__)) |
||
124 | PlatinenVersion = 21; |
||
125 | #else |
||
126 | if(PINB & 0x01) |
||
127 | { |
||
128 | if(PINB & 0x02) PlatinenVersion = 13; |
||
129 | else PlatinenVersion = 11; |
||
130 | } |
||
131 | else |
||
132 | { |
||
133 | if(PINB & 0x02) PlatinenVersion = 20; |
||
134 | else PlatinenVersion = 10; |
||
135 | } |
||
136 | #endif |
||
137 | DDRC = 0x81; // SCL |
||
138 | DDRC |=0x40; // HEF4017 Reset |
||
139 | PORTC = 0xff; // Pullup SDA |
||
140 | DDRB = 0x1B; // LEDs und Druckoffset |
||
141 | PORTB = 0x01; // LED_Rot |
||
142 | DDRD = 0x3E; // Speaker & TXD & J3 J4 J5 |
||
143 | PORTD = 0x47; // LED |
||
144 | HEF4017R_ON; |
||
145 | MCUSR &=~(1<<WDRF); |
||
146 | WDTCSR |= (1<<WDCE)|(1<<WDE); |
||
147 | WDTCSR = 0; |
||
148 | |||
149 | beeptime = 2500; |
||
150 | StickGier = 0; PPM_in[K_GAS] = 0; StickRoll = 0; StickNick = 0; |
||
151 | if(PlatinenVersion >= 20) GIER_GRAD_FAKTOR = 1220; else GIER_GRAD_FAKTOR = 1291; // unterschiedlich für ME und ENC |
||
152 | ROT_OFF; |
||
153 | |||
154 | Timer_Init(); |
||
155 | TIMER2_Init(); |
||
156 | UART_Init(); |
||
157 | rc_sum_init(); |
||
158 | ADC_Init(); |
||
159 | I2C_Init(); |
||
160 | SPI_MasterInit(); |
||
161 | Capacity_Init(); |
||
162 | LIBFC_Init(); |
||
163 | GRN_ON; |
||
164 | sei(); |
||
165 | ParamSet_Init(); |
||
166 | |||
167 | |||
168 | // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
||
169 | // + Check connected BL-Ctrls |
||
170 | // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
||
171 | // Check connected BL-Ctrls |
||
172 | BLFlags |= BLFLAG_READ_VERSION; |
||
173 | motor_read = 0; // read the first I2C-Data |
||
174 | SendMotorData(); |
||
175 | timer = SetDelay(500); |
||
176 | while(!(BLFlags & BLFLAG_TX_COMPLETE) && !CheckDelay(timer)); //wait for complete transfer |
||
177 | |||
178 | printf("\n\rFound BL-Ctrl: "); |
||
179 | timer = SetDelay(4000); |
||
180 | for(i=0; i < MAX_MOTORS; i++) |
||
181 | { |
||
182 | SendMotorData(); |
||
183 | while(!(BLFlags & BLFLAG_TX_COMPLETE) && !CheckDelay(timer)); //wait for complete transfer |
||
184 | if(Mixer.Motor[i][0] > 0) // wait max 4 sec for the BL-Ctrls to wake up |
||
185 | { |
||
186 | while(!CheckDelay(timer) && !(Motor[i].State & MOTOR_STATE_PRESENT_MASK) ) |
||
187 | { |
||
188 | SendMotorData(); |
||
189 | while(!(BLFlags & BLFLAG_TX_COMPLETE) && !CheckDelay(timer)); //wait for complete transfer |
||
190 | } |
||
191 | } |
||
192 | if(Motor[i].State & MOTOR_STATE_PRESENT_MASK) |
||
193 | { |
||
194 | printf("%d",i+1); |
||
195 | FoundMotors++; |
||
196 | // if(Motor[i].Version & MOTOR_STATE_NEW_PROTOCOL_MASK) printf("(new) "); |
||
197 | } |
||
198 | } |
||
199 | for(i=0; i < MAX_MOTORS; i++) |
||
200 | { |
||
201 | if(!(Motor[i].State & MOTOR_STATE_PRESENT_MASK) && Mixer.Motor[i][0] > 0) |
||
202 | { |
||
203 | printf("\n\r\n\r!! MISSING BL-CTRL: %d !!",i+1); |
||
204 | ServoActive = 2; // just in case the FC would be used as camera-stabilizer |
||
205 | } |
||
206 | Motor[i].State &= ~MOTOR_STATE_ERROR_MASK; // clear error counter |
||
207 | } |
||
208 | printf("\n\r==================================="); |
||
209 | |||
210 | if(RequiredMotors < FoundMotors) VersionInfo.HardwareError[1] |= DEFEKT_MIXER_ERR; |
||
211 | |||
212 | //if(EE_Parameter.GlobalConfig & CFG_HOEHENREGELUNG) |
||
213 | { |
||
214 | printf("\n\rCalibrating pressure sensor.."); |
||
215 | timer = SetDelay(1000); |
||
216 | SucheLuftruckOffset(); |
||
217 | while (!CheckDelay(timer)); |
||
218 | printf("OK\n\r"); |
||
219 | } |
||
220 | |||
221 | SetNeutral(0); |
||
222 | |||
223 | ROT_OFF; |
||
224 | |||
225 | beeptime = 2000; |
||
226 | ExternControl.Digital[0] = 0x55; |
||
227 | |||
228 | |||
229 | FlugMinuten = (unsigned int)GetParamByte(PID_FLIGHT_MINUTES) * 256 + (unsigned int)GetParamByte(PID_FLIGHT_MINUTES + 1); |
||
230 | FlugMinutenGesamt = (unsigned int)GetParamByte(PID_FLIGHT_MINUTES_TOTAL) * 256 + (unsigned int)GetParamByte(PID_FLIGHT_MINUTES_TOTAL + 1); |
||
231 | |||
232 | if((FlugMinutenGesamt == 0xFFFF) || (FlugMinuten == 0xFFFF)) |
||
233 | { |
||
234 | FlugMinuten = 0; |
||
235 | FlugMinutenGesamt = 0; |
||
236 | } |
||
237 | printf("\n\rFlight-time %u min Total:%u min", FlugMinuten, FlugMinutenGesamt); |
||
238 | |||
239 | printf("\n\rControl: "); |
||
240 | if (EE_Parameter.GlobalConfig & CFG_HEADING_HOLD) printf("HeadingHold"); |
||
241 | else printf("Normal (ACC-Mode)"); |
||
242 | |||
243 | LcdClear(); |
||
244 | I2CTimeout = 5000; |
||
245 | WinkelOut.Orientation = 1; |
||
246 | LipoDetection(1); |
||
247 | |||
248 | LIBFC_ReceiverInit(EE_Parameter.Receiver); |
||
249 | |||
250 | printf("\n\r===================================\n\r"); |
||
251 | //SpektrumBinding(); |
||
252 | timer = SetDelay(2000); |
||
253 | timerPolling = SetDelay(250); |
||
254 | |||
255 | Debug(ANSI_CLEAR "FC-Start!\n\rFlugzeit: %d min", FlugMinutenGesamt); // Note: this won't waste flash memory, if #DEBUG is not active |
||
256 | DebugOut.Status[0] = 0x01 | 0x02; |
||
257 | |||
258 | while (1) |
||
259 | { |
||
260 | if(CheckDelay(timerPolling)) |
||
261 | { |
||
262 | timerPolling = SetDelay(100); |
||
263 | LIBFC_Polling(); |
||
264 | } |
||
265 | if(UpdateMotor && AdReady) // ReglerIntervall |
||
266 | { |
||
267 | UpdateMotor=0; |
||
268 | if(WinkelOut.CalcState) CalMk3Mag(); |
||
269 | else MotorRegler(); |
||
270 | SendMotorData(); |
||
271 | ROT_OFF; |
||
272 | if(SenderOkay) { SenderOkay--; VersionInfo.HardwareError[1] &= ~DEFEKT_PPM_ERR; } |
||
273 | else |
||
274 | { |
||
275 | TIMSK1 |= _BV(ICIE1); // enable PPM-Input |
||
276 | PPM_in[0] = 0; // set RSSI to zero on data timeout |
||
277 | VersionInfo.HardwareError[1] |= DEFEKT_PPM_ERR; |
||
278 | } |
||
279 | //+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
||
280 | //if(HoehenReglerAktiv && NaviDataOkay && SenderOkay < 160 && SenderOkay > 10 && FromNaviCtrl_Value.SerialDataOkay > 220) SenderOkay = 160; |
||
281 | //if(HoehenReglerAktiv && NaviDataOkay && SenderOkay < 101 && SenderOkay > 10 && FromNaviCtrl_Value.SerialDataOkay > 1) SenderOkay = 101; |
||
282 | //+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
||
283 | if(!--I2CTimeout || MissingMotor) |
||
284 | { |
||
285 | if(!I2CTimeout) |
||
286 | { |
||
287 | I2C_Reset(); |
||
288 | I2CTimeout = 5; |
||
289 | DebugOut.Analog[28]++; // I2C-Error |
||
290 | FCFlags |= FCFLAG_I2CERR; |
||
291 | VersionInfo.HardwareError[1] |= DEFEKT_I2C; |
||
292 | DebugOut.Status[1] |= 0x02; // BL-Error-Status |
||
293 | } |
||
294 | if((BeepMuster == 0xffff) && MotorenEin) |
||
295 | { |
||
296 | beeptime = 10000; |
||
297 | BeepMuster = 0x0080; |
||
298 | } |
||
299 | } |
||
300 | else |
||
301 | { |
||
302 | ROT_OFF; |
||
303 | if(!beeptime) |
||
304 | { |
||
305 | FCFlags &= ~FCFLAG_I2CERR; |
||
306 | } |
||
307 | } |
||
308 | if(!UpdateMotor) |
||
309 | { |
||
310 | DatenUebertragung(); |
||
311 | BearbeiteRxDaten(); |
||
312 | if(CheckDelay(timer)) |
||
313 | { |
||
314 | static unsigned char second; |
||
315 | timer += 20; // 20 ms interval |
||
316 | if(MissingMotor) |
||
317 | { |
||
318 | VersionInfo.HardwareError[1] |= DEFEKT_BL_MISSING; |
||
319 | DebugOut.Status[1] |= 0x02; // BL-Error-Status |
||
320 | } |
||
321 | else |
||
322 | { |
||
323 | VersionInfo.HardwareError[1] &= ~DEFEKT_BL_MISSING; |
||
324 | if(I2CTimeout > 6) DebugOut.Status[1] &= ~0x02; // BL-Error-Status |
||
325 | } |
||
326 | |||
327 | if(I2CTimeout > 6) VersionInfo.HardwareError[1] &= ~DEFEKT_I2C; |
||
328 | |||
329 | if(PcZugriff) PcZugriff--; |
||
330 | else |
||
331 | { |
||
332 | ExternControl.Config = 0; |
||
333 | ExternStickNick = 0; |
||
334 | ExternStickRoll = 0; |
||
335 | ExternStickGier = 0; |
||
336 | if(BeepMuster == 0xffff && SenderOkay == 0) |
||
337 | { |
||
338 | beeptime = 15000; |
||
339 | BeepMuster = 0x0c00; |
||
340 | } |
||
341 | } |
||
342 | if(NaviDataOkay > 200) |
||
343 | { |
||
344 | NaviDataOkay--; |
||
345 | FCFlags &= ~FCFLAG_SPI_RX_ERR; |
||
346 | VersionInfo.HardwareError[1] &= ~DEFEKT_SPI_RX_ERR; |
||
347 | } |
||
348 | else |
||
349 | { |
||
350 | if(NC_Version.Compatible) |
||
351 | { |
||
352 | FCFlags |= FCFLAG_SPI_RX_ERR; |
||
353 | VersionInfo.HardwareError[1] |= DEFEKT_SPI_RX_ERR; |
||
354 | if(BeepMuster == 0xffff && MotorenEin) |
||
355 | { |
||
356 | beeptime = 15000; |
||
357 | BeepMuster = 0xA800; |
||
358 | } |
||
359 | } |
||
360 | GPS_Nick = 0; |
||
361 | GPS_Roll = 0; |
||
362 | //if(!beeptime) |
||
363 | FromNaviCtrl.CompassValue = -1; |
||
364 | NaviDataOkay = 0; |
||
365 | } |
||
366 | if(UBat < BattLowVoltageWarning) |
||
367 | { |
||
368 | FCFlags |= FCFLAG_LOWBAT; |
||
369 | if(BeepMuster == 0xffff) |
||
370 | { |
||
371 | beeptime = 6000; |
||
372 | BeepMuster = 0x0300; |
||
373 | } |
||
374 | } |
||
375 | else if(!beeptime) FCFlags &= ~FCFLAG_LOWBAT; |
||
376 | |||
377 | SPI_StartTransmitPacket(); |
||
378 | SendSPI = 4; |
||
379 | if(!MotorenEin) timer2 = 1450; // 0,5 Minuten aufrunden |
||
380 | else |
||
381 | if(++second == 49) |
||
382 | { |
||
383 | second = 0; |
||
384 | FlugSekunden++; |
||
385 | } |
||
386 | if(++timer2 == 2930) // eine Minute |
||
387 | { |
||
388 | timer2 = 0; |
||
389 | FlugMinuten++; |
||
390 | FlugMinutenGesamt++; |
||
391 | SetParamByte(PID_FLIGHT_MINUTES,FlugMinuten / 256); |
||
392 | SetParamByte(PID_FLIGHT_MINUTES+1,FlugMinuten % 256); |
||
393 | SetParamByte(PID_FLIGHT_MINUTES_TOTAL,FlugMinutenGesamt / 256); |
||
394 | SetParamByte(PID_FLIGHT_MINUTES_TOTAL+1,FlugMinutenGesamt % 256); |
||
395 | timer = SetDelay(20); // falls "timer += 20;" mal nicht geht |
||
396 | } |
||
397 | } |
||
398 | LED_Update(); |
||
399 | Capacity_Update(); |
||
400 | } |
||
401 | } |
||
402 | if(!SendSPI) { SPI_TransmitByte(); } |
||
403 | } |
||
404 | return (1); |
||
405 | } |
||
406 | |||
407 |