Details | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
836 | MikeW | 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 "main.h" |
||
53 | #include "kafi.h" |
||
54 | #include "mymath.h" |
||
55 | #include "mm3.h" |
||
56 | |||
57 | #define sin45 -0.707106 |
||
58 | #define cos45 0.707106 |
||
59 | |||
60 | unsigned char EEPromArray[E2END+1] EEMEM; |
||
61 | extern void InitOSD(void); |
||
62 | extern void InitGPS(void); |
||
63 | |||
64 | extern void SendOSD(void); |
||
65 | extern void MotorControl(void); |
||
66 | extern void SendMotorData(void); |
||
67 | extern void CalculateAverage(void); |
||
68 | extern void AttitudeEstimation(void); |
||
69 | extern void PID_Regler(void); |
||
70 | extern void SetNeutral(void); |
||
71 | |||
72 | /* **************************************************************************** |
||
73 | Functionname: ReadParameterSet */ /*! |
||
74 | Description: -- Parametersatz aus EEPROM lesen --- |
||
75 | number [0..5] |
||
76 | |||
77 | @return void |
||
78 | @pre - |
||
79 | @post - |
||
80 | @author H. Buss / I. Busker |
||
81 | **************************************************************************** */ |
||
82 | void ReadParameterSet(unsigned char number, unsigned char *buffer, unsigned char length) |
||
83 | { |
||
84 | if (number > 5) |
||
85 | { |
||
86 | number = 5; |
||
87 | } |
||
88 | eeprom_read_block(buffer, &EEPromArray[EEPROM_ADR_PARAM_BEGIN + length * number], length); |
||
89 | } |
||
90 | |||
91 | /* **************************************************************************** |
||
92 | Functionname: WriteParameterSet */ /*! |
||
93 | Description: -- Parametersatz ins EEPROM schreiben --- |
||
94 | number [0..5] |
||
95 | |||
96 | @return void |
||
97 | @pre - |
||
98 | @post - |
||
99 | @author H. Buss / I. Busker |
||
100 | **************************************************************************** */ |
||
101 | void WriteParameterSet(unsigned char number, unsigned char *buffer, unsigned char length) |
||
102 | { |
||
103 | if(number > 5) |
||
104 | { |
||
105 | number = 5; |
||
106 | } |
||
107 | eeprom_write_block(buffer, &EEPromArray[EEPROM_ADR_PARAM_BEGIN + length * number], length); |
||
108 | eeprom_write_byte(&EEPromArray[EEPROM_ADR_ACTIVE_SET], number); // diesen Parametersatz als aktuell merken |
||
109 | } |
||
110 | |||
111 | |||
112 | /* **************************************************************************** |
||
113 | Functionname: GetActiveParamSetNumber */ /*! |
||
114 | Description: |
||
115 | |||
116 | @return void |
||
117 | @pre - |
||
118 | @post - |
||
119 | @author H. Buss / I. Busker |
||
120 | **************************************************************************** */ |
||
121 | unsigned char GetActiveParamSetNumber(void) |
||
122 | { |
||
123 | unsigned char set; |
||
124 | set = eeprom_read_byte(&EEPromArray[EEPROM_ADR_ACTIVE_SET]); |
||
125 | if(set > 5) |
||
126 | { |
||
127 | set = 2; |
||
128 | eeprom_write_byte(&EEPromArray[EEPROM_ADR_ACTIVE_SET], set); // diesen Parametersatz als aktuell merken |
||
129 | } |
||
130 | return(set); |
||
131 | } |
||
132 | |||
133 | |||
134 | /* **************************************************************************** |
||
135 | Functionname: main */ /*! |
||
136 | Description: Hauptprogramm |
||
137 | |||
138 | @return void |
||
139 | @pre - |
||
140 | @post - |
||
141 | @author H. Buss / I. Busker |
||
142 | **************************************************************************** */ |
||
143 | int main (void) |
||
144 | { |
||
145 | unsigned int timer; |
||
146 | |||
147 | //unsigned int timer2 = 0; |
||
148 | DDRB = 0x00; |
||
149 | PORTB = 0x00; |
||
150 | for(timer = 0; timer < 1000; timer++); // verzögern |
||
151 | DDRC = 0x81; // SCL |
||
152 | PORTC = 0xff; // Pullup SDA |
||
153 | DDRB = 0x1B; // LEDs und Druckoffset |
||
154 | PORTB = 0x01; // LED_Rot |
||
155 | DDRD = 0x3E; // Speaker & TXD & J3 J4 J5 |
||
156 | DDRD |=0x80; // J7 |
||
157 | PORTD = 0xF7; // LED |
||
158 | |||
159 | MCUSR &=~(1<<WDRF); |
||
160 | WDTCSR |= (1<<WDCE)|(1<<WDE); |
||
161 | WDTCSR = 0; |
||
162 | |||
163 | beeptime = 2000; |
||
164 | |||
165 | StickGier = 0; StickRoll = 0; StickNick = 0; PPM_in[K_GAS] = 0; |
||
166 | |||
167 | Kafi_Init(); |
||
168 | Timer_Init(); |
||
169 | UART_Init(); |
||
170 | InitGPS(); |
||
171 | rc_sum_init(); |
||
172 | ADC_Init(); |
||
173 | i2c_init(); |
||
174 | /* Init the MM3 */ |
||
175 | MM3_Init(); |
||
176 | |||
177 | sei(); |
||
178 | |||
179 | |||
180 | VersionInfo.Hauptversion = VERSION_HAUPTVERSION; |
||
181 | VersionInfo.Nebenversion = VERSION_NEBENVERSION; |
||
182 | VersionInfo.PCKompatibel = VERSION_KOMPATIBEL; |
||
183 | |||
184 | GRN_ON; |
||
185 | |||
186 | #define EE_DATENREVISION 66 // wird angepasst, wenn sich die EEPROM-Daten geändert haben |
||
187 | if(eeprom_read_byte(&EEPromArray[EEPROM_ADR_VALID]) != EE_DATENREVISION) |
||
188 | { |
||
189 | printf("\n\rInit. EEPROM: Generiere Default-Parameter..."); |
||
190 | DefaultKonstanten1(); |
||
191 | for (unsigned char i=0;i<6;i++) |
||
192 | { |
||
193 | if(i==2) DefaultKonstanten2(); // Kamera |
||
194 | if(i==3) DefaultKonstanten3(); // Beginner |
||
195 | if(i>3) DefaultKonstanten2(); // Kamera |
||
196 | WriteParameterSet(i, (unsigned char *) &EE_Parameter.Kanalbelegung[0], STRUCT_PARAM_LAENGE); |
||
197 | } |
||
198 | eeprom_write_byte(&EEPromArray[EEPROM_ADR_ACTIVE_SET], 3); // default-Setting |
||
199 | eeprom_write_byte(&EEPromArray[EEPROM_ADR_VALID], EE_DATENREVISION); |
||
200 | } |
||
201 | |||
202 | /* Init EE_Parameter */ |
||
203 | ReadParameterSet(GetActiveParamSetNumber(), (unsigned char *) &EE_Parameter.Kanalbelegung[0], STRUCT_PARAM_LAENGE); |
||
204 | printf("\n\rBenutze Parametersatz %d", GetActiveParamSetNumber()); |
||
205 | |||
206 | if(EE_Parameter.GlobalConfig & CFG_HOEHENREGELUNG) |
||
207 | { |
||
208 | printf("\n\rAbgleich Luftdrucksensor.."); |
||
209 | timer = SetDelay(1000); |
||
210 | SucheLuftruckOffset(); |
||
211 | while (!CheckDelay(timer)); |
||
212 | printf("OK\n\r"); |
||
213 | } |
||
214 | |||
215 | ROT_ON |
||
216 | Delay_ms(1000); |
||
217 | SetNeutral(); |
||
218 | ROT_OFF |
||
219 | Delay_ms(1000); |
||
220 | SetNeutral(); |
||
221 | ROT_ON |
||
222 | |||
223 | beeptime = 2000; |
||
224 | DebugIn.Analog[1] = 1000; |
||
225 | DebugIn.Digital[0] = 0x55; |
||
226 | |||
227 | /* Calibrate the MM3 Compass? */ |
||
228 | if((PPM_in[EE_Parameter.Kanalbelegung[K_GAS]] > 80) && |
||
229 | (PPM_in[EE_Parameter.Kanalbelegung[K_GIER]] < -75) && |
||
230 | (MotorenEin == 0)) |
||
231 | { |
||
232 | printf("\n\rCalibrating Compass"); |
||
233 | MM3_Calibrate(); |
||
234 | } |
||
235 | |||
236 | |||
237 | /* Init the OSD */ |
||
238 | // InitOSD(); |
||
239 | |||
240 | I2CTimeout = 5000; |
||
241 | while (1) |
||
242 | { |
||
243 | static i32_t OldTime = 0; |
||
244 | if (UpdateMotor) // ReglerIntervall |
||
245 | { |
||
246 | UpdateMotor=0; |
||
247 | |||
248 | /* G e n e r a t e O S D D a t a */ |
||
249 | static char CntOSD = 0; |
||
250 | if (CntOSD % 6 == 1) |
||
251 | { |
||
252 | //SendOSD(); |
||
253 | } |
||
254 | CntOSD++; |
||
255 | |||
256 | /* Set the cycle Time to 110ms */ |
||
257 | if (OldTime != 0) |
||
258 | { |
||
259 | #ifdef USE_Extended_MM3_Measurement_Model |
||
260 | while (((Count8Khz - OldTime) *10) / 8 < 160); |
||
261 | #else |
||
262 | while (((Count8Khz - OldTime) *10) / 8 < 110); |
||
263 | #endif |
||
264 | DebugOut.Analog[7] = ((Count8Khz - OldTime) *10) / 8; |
||
265 | } |
||
266 | OldTime = Count8Khz; |
||
267 | |||
268 | /* Average the Measurements */ |
||
269 | CalculateAverage(); |
||
270 | /* Do the Kalman Filterimg */ |
||
271 | AttitudeEstimation(); |
||
272 | /* MotorControl */ |
||
273 | MotorControl(); |
||
274 | /* Call the PID Control */ |
||
275 | PID_Regler(); |
||
276 | |||
277 | SendMotorData(); |
||
278 | |||
279 | if(PcZugriff) |
||
280 | { |
||
281 | PcZugriff--; |
||
282 | } |
||
283 | if(SenderOkay) |
||
284 | { |
||
285 | SenderOkay--; |
||
286 | } |
||
287 | if(!I2CTimeout) |
||
288 | { |
||
289 | I2CTimeout = 5; |
||
290 | i2c_reset(); |
||
291 | if((BeepMuster == 0xffff) && MotorenEin) |
||
292 | { |
||
293 | beeptime = 10000; |
||
294 | BeepMuster = 0x0080; |
||
295 | } |
||
296 | } |
||
297 | else |
||
298 | { |
||
299 | I2CTimeout--; |
||
300 | } |
||
301 | } |
||
302 | if(SIO_DEBUG) |
||
303 | { |
||
304 | DatenUebertragung(); |
||
305 | BearbeiteRxDaten(); |
||
306 | } |
||
307 | else BearbeiteRxDaten(); |
||
308 | if(CheckDelay(timer)) |
||
309 | { |
||
310 | if(UBat < EE_Parameter.UnterspannungsWarnung) |
||
311 | { |
||
312 | if(BeepMuster == 0xffff) |
||
313 | { |
||
314 | beeptime = 6000; |
||
315 | BeepMuster = 0x0300; |
||
316 | } |
||
317 | } |
||
318 | timer = SetDelay(100); |
||
319 | } |
||
320 | } |
||
321 | return (1); |
||
322 | } |
||
323 |