Rev 1603 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
1 | ingob | 1 | // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
1338 | ingob | 2 | // + Copyright (c) Holger Buss, Ingo Busker |
3 | // + Nur für den privaten Gebrauch / NON-COMMERCIAL USE ONLY |
||
1 | ingob | 4 | // + www.MikroKopter.com |
5 | // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
||
1051 | killagreg | 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. |
||
1 | ingob | 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 | // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
||
1051 | killagreg | 13 | // + Werden Teile des Quellcodes (mit oder ohne Modifikation) weiterverwendet oder veröffentlicht, |
1 | ingob | 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 | // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
||
1338 | ingob | 24 | // + Die Portierung oder Nutzung der Software (oder Teile davon) auf andere Systeme (ausser der Hardware von www.mikrokopter.de) ist nur |
1 | ingob | 25 | // + mit unserer Zustimmung zulässig |
26 | // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
||
27 | // + Die Funktion printf_P() unterliegt ihrer eigenen Lizenz und ist hiervon nicht betroffen |
||
28 | // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
||
1051 | killagreg | 29 | // + Redistributions of source code (with or without modifications) must retain the above copyright notice, |
1 | ingob | 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. |
||
1051 | killagreg | 33 | // + * The use of this project (hardware, software, binary files, sources and documentation) is only permittet |
1 | ingob | 34 | // + for non-commercial use (directly or indirectly) |
1051 | killagreg | 35 | // + Commercial use (for excample: selling of MikroKopters, selling of PCBs, assembly, ...) is only permitted |
1 | ingob | 36 | // + with our written permission |
1051 | killagreg | 37 | // + * If sources or documentations are redistributet on other webpages, out webpage (http://www.MikroKopter.de) must be |
38 | // + clearly linked as origin |
||
1338 | ingob | 39 | // + * porting the sources to other systems or using the software on other systems (except hardware from www.mikrokopter.de) is not allowed |
1 | ingob | 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 |
||
1051 | killagreg | 50 | // + POSSIBILITY OF SUCH DAMAGE. |
1 | ingob | 51 | // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
52 | #include "main.h" |
||
53 | |||
1438 | ingob | 54 | |
173 | holgerb | 55 | unsigned char PlatinenVersion = 10; |
723 | hbuss | 56 | unsigned char SendVersionToNavi = 1; |
1232 | hbuss | 57 | unsigned char BattLowVoltageWarning = 94; |
1298 | hbuss | 58 | unsigned int FlugMinuten = 0,FlugMinutenGesamt = 0; |
1512 | holgerb | 59 | unsigned int FlugSekunden = 0; |
1444 | ingob | 60 | pVoidFnct_pVoidFnctChar_const_fmt _printf_P; |
61 | |||
1 | ingob | 62 | |
819 | hbuss | 63 | void CalMk3Mag(void) |
64 | { |
||
65 | static unsigned char stick = 1; |
||
66 | |||
67 | if(PPM_in[EE_Parameter.Kanalbelegung[K_NICK]] > -20) stick = 0; |
||
68 | if((PPM_in[EE_Parameter.Kanalbelegung[K_NICK]] < -70) && !stick) |
||
69 | { |
||
70 | stick = 1; |
||
71 | WinkelOut.CalcState++; |
||
1051 | killagreg | 72 | if(WinkelOut.CalcState > 4) |
73 | { |
||
819 | hbuss | 74 | // WinkelOut.CalcState = 0; // in Uart.c |
75 | beeptime = 1000; |
||
1051 | killagreg | 76 | } |
1232 | hbuss | 77 | else Piep(WinkelOut.CalcState,150); |
819 | hbuss | 78 | } |
79 | DebugOut.Analog[19] = WinkelOut.CalcState; |
||
80 | } |
||
81 | |||
1438 | ingob | 82 | |
83 | |||
1268 | hbuss | 84 | void LipoDetection(unsigned char print) |
85 | { |
||
86 | unsigned int timer; |
||
87 | if(print) printf("\n\rBatt:"); |
||
88 | if(EE_Parameter.UnterspannungsWarnung < 50) // automatische Zellenerkennung |
||
89 | { |
||
90 | timer = SetDelay(500); |
||
91 | if(print) while (!CheckDelay(timer)); |
||
92 | if(UBat < 130) |
||
93 | { |
||
94 | BattLowVoltageWarning = 3 * EE_Parameter.UnterspannungsWarnung; |
||
1420 | killagreg | 95 | if(print) |
1268 | hbuss | 96 | { |
97 | Piep(3,200); |
||
98 | printf(" 3 Cells "); |
||
1420 | killagreg | 99 | } |
1268 | hbuss | 100 | } |
101 | else |
||
102 | { |
||
103 | BattLowVoltageWarning = 4 * EE_Parameter.UnterspannungsWarnung; |
||
1420 | killagreg | 104 | if(print) |
1268 | hbuss | 105 | { |
106 | Piep(4,200); |
||
107 | printf(" 4 Cells "); |
||
1420 | killagreg | 108 | } |
1268 | hbuss | 109 | } |
110 | } |
||
111 | else BattLowVoltageWarning = EE_Parameter.UnterspannungsWarnung; |
||
1374 | hbuss | 112 | // if(BattLowVoltageWarning < 93) BattLowVoltageWarning = 93; |
1268 | hbuss | 113 | if(print) printf(" Low warning level: %d.%d",BattLowVoltageWarning/10,BattLowVoltageWarning%10); |
114 | } |
||
115 | |||
1 | ingob | 116 | //############################################################################ |
117 | //Hauptprogramm |
||
118 | int main (void) |
||
119 | //############################################################################ |
||
120 | { |
||
1424 | ingob | 121 | unsigned int timer,i,timer2 = 0, timerPolling; |
1479 | killagreg | 122 | |
1051 | killagreg | 123 | DDRB = 0x00; |
124 | PORTB = 0x00; |
||
188 | holgerb | 125 | for(timer = 0; timer < 1000; timer++); // verzögern |
1051 | killagreg | 126 | if(PINB & 0x01) |
918 | hbuss | 127 | { |
1051 | killagreg | 128 | if(PINB & 0x02) PlatinenVersion = 13; |
129 | else PlatinenVersion = 11; |
||
918 | hbuss | 130 | } |
1051 | killagreg | 131 | else |
1021 | hbuss | 132 | { |
1051 | killagreg | 133 | if(PINB & 0x02) PlatinenVersion = 20; |
134 | else PlatinenVersion = 10; |
||
1021 | hbuss | 135 | } |
136 | |||
173 | holgerb | 137 | DDRC = 0x81; // SCL |
1036 | hbuss | 138 | DDRC |=0x40; // HEF4017 Reset |
1 | ingob | 139 | PORTC = 0xff; // Pullup SDA |
140 | DDRB = 0x1B; // LEDs und Druckoffset |
||
141 | PORTB = 0x01; // LED_Rot |
||
142 | DDRD = 0x3E; // Speaker & TXD & J3 J4 J5 |
||
1153 | hbuss | 143 | PORTD = 0x47; // LED |
1171 | hbuss | 144 | HEF4017R_ON; |
1 | ingob | 145 | MCUSR &=~(1<<WDRF); |
146 | WDTCSR |= (1<<WDCE)|(1<<WDE); |
||
147 | WDTCSR = 0; |
||
148 | |||
149 | beeptime = 2000; |
||
150 | StickGier = 0; PPM_in[K_GAS] = 0;StickRoll = 0; StickNick = 0; |
||
1111 | hbuss | 151 | if(PlatinenVersion >= 20) GIER_GRAD_FAKTOR = 1160; else GIER_GRAD_FAKTOR = 1291; // unterschiedlich für ME und ENC |
1 | ingob | 152 | ROT_OFF; |
1479 | killagreg | 153 | |
1 | ingob | 154 | Timer_Init(); |
1156 | hbuss | 155 | TIMER2_Init(); |
1 | ingob | 156 | UART_Init(); |
157 | rc_sum_init(); |
||
158 | ADC_Init(); |
||
159 | i2c_init(); |
||
597 | ingob | 160 | SPI_MasterInit(); |
1486 | killagreg | 161 | Capacity_Init(); |
1435 | killagreg | 162 | LIBFC_Init(); |
1431 | ingob | 163 | GRN_ON; |
164 | sei(); |
||
1622 | killagreg | 165 | ParamSet_Init(); |
1344 | hbuss | 166 | |
1435 | killagreg | 167 | |
1211 | hbuss | 168 | // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
169 | // + Check connected BL-Ctrls |
||
170 | // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
||
1591 | holgerb | 171 | motorread = 0; UpdateMotor = 0; SendMotorData(); while(!UpdateMotor); motorread = 0; // read the first I2C-Data |
1211 | hbuss | 172 | printf("\n\rFound BL-Ctrl: "); |
1591 | holgerb | 173 | timer = SetDelay(4000); |
1420 | killagreg | 174 | |
1211 | hbuss | 175 | for(i=0; i < MAX_MOTORS; i++) |
1622 | killagreg | 176 | { |
177 | UpdateMotor = 0; |
||
178 | SendMotorData(); |
||
179 | while(!UpdateMotor); |
||
180 | if(Mixer.Motor[i][0] > 0) // wait max 4 sec for the BL-Ctrls to wake up |
||
181 | { |
||
182 | while(!CheckDelay(timer) && !(Motor[i].State & MOTOR_STATE_PRESENT_MASK) ) {UpdateMotor = 0; SendMotorData(); while(!UpdateMotor);}; |
||
1232 | hbuss | 183 | } |
1622 | killagreg | 184 | if(Motor[i].State & MOTOR_STATE_PRESENT_MASK) printf("%d ",i+1); |
185 | } |
||
1211 | hbuss | 186 | for(i=0; i < MAX_MOTORS; i++) |
1051 | killagreg | 187 | { |
1622 | killagreg | 188 | if(!(Motor[i].State & MOTOR_STATE_PRESENT_MASK) && Mixer.Motor[i][0] > 0) |
189 | { |
||
190 | printf("\n\r\n\r!! MISSING BL-CTRL: %d !!",i+1); |
||
191 | ServoActive = 1; // just in case the FC would be used as camera-stabilizer |
||
1111 | hbuss | 192 | } |
1622 | killagreg | 193 | Motor[i].State &= ~MOTOR_STATE_ERROR_MASK; // clear error counter |
1 | ingob | 194 | } |
1622 | killagreg | 195 | printf("\n\r==================================="); |
196 | SendMotorData(); |
||
1435 | killagreg | 197 | |
1529 | killagreg | 198 | //if(EE_Parameter.GlobalConfig & CFG_HOEHENREGELUNG) |
1622 | killagreg | 199 | { |
200 | printf("\n\rCalibrating pressure sensor.."); |
||
201 | timer = SetDelay(1000); |
||
202 | SucheLuftruckOffset(); |
||
203 | while (!CheckDelay(timer)); |
||
204 | printf("OK\n\r"); |
||
1 | ingob | 205 | } |
1051 | killagreg | 206 | |
1622 | killagreg | 207 | SetNeutral(0); |
1 | ingob | 208 | |
209 | ROT_OFF; |
||
1051 | killagreg | 210 | |
1 | ingob | 211 | beeptime = 2000; |
1051 | killagreg | 212 | ExternControl.Digital[0] = 0x55; |
1 | ingob | 213 | |
1051 | killagreg | 214 | |
1622 | killagreg | 215 | FlugMinuten = GetParamWord(PID_FLIGHT_MINUTES); |
216 | FlugMinutenGesamt = GetParamWord(PID_FLIGHT_MINUTES_TOTAL); |
||
217 | |||
218 | if( (FlugMinutenGesamt == 0xFFFF) || (FlugMinuten == 0xFFFF) ) |
||
219 | { |
||
220 | FlugMinuten = 0; |
||
221 | FlugMinutenGesamt = 0; |
||
222 | } |
||
223 | printf("\n\rFlight-time %u min Total:%u min", FlugMinuten, FlugMinutenGesamt); |
||
224 | |||
225 | |||
1211 | hbuss | 226 | printf("\n\rControl: "); |
1 | ingob | 227 | if (EE_Parameter.GlobalConfig & CFG_HEADING_HOLD) printf("HeadingHold"); |
1211 | hbuss | 228 | else printf("Normal (ACC-Mode)"); |
1051 | killagreg | 229 | |
1 | ingob | 230 | LcdClear(); |
173 | holgerb | 231 | I2CTimeout = 5000; |
819 | hbuss | 232 | WinkelOut.Orientation = 1; |
1268 | hbuss | 233 | LipoDetection(1); |
1435 | killagreg | 234 | |
235 | LIBFC_ReceiverInit(); |
||
1444 | ingob | 236 | |
1232 | hbuss | 237 | printf("\n\r===================================\n\r"); |
1622 | killagreg | 238 | //SpektrumBinding(); |
1350 | hbuss | 239 | timer = SetDelay(2000); |
1424 | ingob | 240 | timerPolling = SetDelay(250); |
1529 | killagreg | 241 | |
1 | ingob | 242 | while (1) |
243 | { |
||
1420 | killagreg | 244 | |
1424 | ingob | 245 | if(CheckDelay(timerPolling)) |
1419 | ingob | 246 | { |
1424 | ingob | 247 | timerPolling = SetDelay(100); |
1435 | killagreg | 248 | LIBFC_Polling(); |
1419 | ingob | 249 | } |
1420 | killagreg | 250 | |
1419 | ingob | 251 | if(UpdateMotor && AdReady) // ReglerIntervall |
1051 | killagreg | 252 | { |
1171 | hbuss | 253 | UpdateMotor=0; |
819 | hbuss | 254 | if(WinkelOut.CalcState) CalMk3Mag(); |
1051 | killagreg | 255 | else MotorRegler(); |
1 | ingob | 256 | SendMotorData(); |
257 | ROT_OFF; |
||
258 | if(SenderOkay) SenderOkay--; |
||
1603 | killagreg | 259 | else |
260 | { |
||
261 | TIMSK1 |= _BV(ICIE1); // enable PPM-Input |
||
262 | PPM_in[0] = 0; // set RSSI to zero on data timeout |
||
263 | } |
||
1420 | killagreg | 264 | //+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
1481 | holgerb | 265 | //if(HoehenReglerAktiv && NaviDataOkay && SenderOkay < 160 && SenderOkay > 10 && FromNaviCtrl_Value.SerialDataOkay > 220) SenderOkay = 160; |
266 | //if(HoehenReglerAktiv && NaviDataOkay && SenderOkay < 101 && SenderOkay > 10 && FromNaviCtrl_Value.SerialDataOkay > 1) SenderOkay = 101; |
||
1309 | hbuss | 267 | //+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
1420 | killagreg | 268 | |
1210 | hbuss | 269 | if(!--I2CTimeout || MissingMotor) |
173 | holgerb | 270 | { |
1254 | killagreg | 271 | if(!I2CTimeout) |
1210 | hbuss | 272 | { |
273 | i2c_reset(); |
||
274 | I2CTimeout = 5; |
||
1322 | hbuss | 275 | DebugOut.Analog[28]++; // I2C-Error |
1423 | hbuss | 276 | FCFlags |= FCFLAG_I2CERR; |
1322 | hbuss | 277 | } |
1051 | killagreg | 278 | if((BeepMuster == 0xffff) && MotorenEin) |
279 | { |
||
173 | holgerb | 280 | beeptime = 10000; |
281 | BeepMuster = 0x0080; |
||
1051 | killagreg | 282 | } |
1 | ingob | 283 | } |
1051 | killagreg | 284 | else |
1 | ingob | 285 | { |
1051 | killagreg | 286 | ROT_OFF; |
1423 | hbuss | 287 | if(!beeptime) FCFlags &= ~FCFLAG_I2CERR; |
1 | ingob | 288 | } |
805 | hbuss | 289 | if(SIO_DEBUG && (!UpdateMotor || !MotorenEin)) |
1 | ingob | 290 | { |
291 | DatenUebertragung(); |
||
292 | BearbeiteRxDaten(); |
||
293 | } |
||
1254 | killagreg | 294 | else BearbeiteRxDaten(); |
295 | if(CheckDelay(timer)) |
||
296 | { |
||
1512 | holgerb | 297 | static unsigned char second; |
1420 | killagreg | 298 | timer += 20; // 20 ms interval |
299 | if(PcZugriff) PcZugriff--; |
||
300 | else |
||
301 | { |
||
302 | ExternControl.Config = 0; |
||
303 | ExternStickNick = 0; |
||
304 | ExternStickRoll = 0; |
||
305 | ExternStickGier = 0; |
||
306 | if(BeepMuster == 0xffff && SenderOkay == 0) |
||
307 | { |
||
308 | beeptime = 15000; |
||
309 | BeepMuster = 0x0c00; |
||
310 | } |
||
311 | } |
||
312 | if(NaviDataOkay) |
||
313 | { |
||
314 | NaviDataOkay--; |
||
315 | FCFlags &= ~FCFLAG_SPI_RX_ERR; |
||
316 | } |
||
317 | else |
||
318 | { |
||
319 | GPS_Nick = 0; |
||
320 | GPS_Roll = 0; |
||
1423 | hbuss | 321 | if(!beeptime) FCFlags |= FCFLAG_SPI_RX_ERR; |
1420 | killagreg | 322 | } |
1309 | hbuss | 323 | if(UBat < BattLowVoltageWarning) |
1254 | killagreg | 324 | { |
1420 | killagreg | 325 | FCFlags |= FCFLAG_LOWBAT; |
1254 | killagreg | 326 | if(BeepMuster == 0xffff) |
327 | { |
||
328 | beeptime = 6000; |
||
329 | BeepMuster = 0x0300; |
||
330 | } |
||
331 | } |
||
1423 | hbuss | 332 | else if(!beeptime) FCFlags &= ~FCFLAG_LOWBAT; |
1420 | killagreg | 333 | |
1254 | killagreg | 334 | SPI_StartTransmitPacket(); |
335 | SendSPI = 4; |
||
1320 | hbuss | 336 | if(!MotorenEin) timer2 = 1450; // 0,5 Minuten aufrunden |
1529 | killagreg | 337 | else |
1512 | holgerb | 338 | if(++second == 49) |
339 | { |
||
340 | second = 0; |
||
341 | FlugSekunden++; |
||
342 | } |
||
1529 | killagreg | 343 | |
1320 | hbuss | 344 | if(++timer2 == 2930) // eine Minute |
1298 | hbuss | 345 | { |
346 | timer2 = 0; |
||
347 | FlugMinuten++; |
||
348 | FlugMinutenGesamt++; |
||
1622 | killagreg | 349 | SetParamWord(PID_FLIGHT_MINUTES_TOTAL, FlugMinutenGesamt); |
350 | SetParamWord(PID_FLIGHT_MINUTES, FlugMinuten); |
||
1344 | hbuss | 351 | timer = SetDelay(20); // falls "timer += 20;" mal nicht geht |
1298 | hbuss | 352 | } |
1254 | killagreg | 353 | } |
921 | hbuss | 354 | LED_Update(); |
1486 | killagreg | 355 | Capacity_Update(); |
604 | hbuss | 356 | } |
723 | hbuss | 357 | if(!SendSPI) { SPI_TransmitByte(); } |
1 | ingob | 358 | } |
359 | return (1); |
||
360 | } |
||
361 | |||
1219 | hbuss | 362 |