Rev 1139 | Go to most recent revision | Details | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
1134 | thjac | 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 | |||
54 | unsigned char EEPromArray[E2END + 1] EEMEM; |
||
55 | unsigned char PlatinenVersion = 10; |
||
56 | unsigned char SendVersionToNavi = 1; |
||
57 | // -- Parametersatz aus EEPROM lesen --- |
||
58 | // number [1..5] |
||
59 | |||
60 | void ReadParameterSet(unsigned char number, unsigned char *buffer, unsigned char length) { |
||
61 | if ((number > 5) || (number < 1)) number = 3; |
||
62 | eeprom_read_block(buffer, &EEPromArray[EEPROM_ADR_PARAM_BEGIN + length * (number - 1)], length); |
||
63 | LED_Init(); |
||
64 | } |
||
65 | |||
66 | // -- Parametersatz ins EEPROM schreiben --- |
||
67 | // number [1..5] |
||
68 | |||
69 | void WriteParameterSet(unsigned char number, unsigned char *buffer, unsigned char length) { |
||
70 | if (number > 5) number = 5; |
||
71 | if (number < 1) return; |
||
72 | eeprom_write_block(buffer, &EEPromArray[EEPROM_ADR_PARAM_BEGIN + length * (number - 1)], length); |
||
73 | SetActiveParamSetNumber(number); |
||
74 | LED_Init(); |
||
75 | } |
||
76 | |||
77 | unsigned char GetActiveParamSetNumber(void) { |
||
78 | unsigned char set; |
||
79 | set = eeprom_read_byte(&EEPromArray[EEPROM_ADR_ACTIVE_SET]); |
||
80 | if ((set > 5) || (set < 1)) { |
||
81 | set = 3; |
||
82 | SetActiveParamSetNumber(set); // diesen Parametersatz als aktuell merken |
||
83 | } |
||
84 | return (set); |
||
85 | } |
||
86 | |||
87 | void SetActiveParamSetNumber(unsigned char number) { |
||
88 | if (number > 5) number = 5; |
||
89 | if (number < 1) return; |
||
90 | eeprom_write_byte(&EEPromArray[EEPROM_ADR_ACTIVE_SET], number); // diesen Parametersatz als aktuell merken |
||
91 | } |
||
92 | |||
93 | void CalMk3Mag(void) { |
||
94 | static unsigned char stick = 1; |
||
95 | |||
96 | if (PPM_in[EE_Parameter.Kanalbelegung[K_NICK]] > -20) stick = 0; |
||
97 | if ((PPM_in[EE_Parameter.Kanalbelegung[K_NICK]] < -70) && !stick) { |
||
98 | stick = 1; |
||
99 | WinkelOut.CalcState++; |
||
100 | if (WinkelOut.CalcState > 4) { |
||
101 | // WinkelOut.CalcState = 0; // in Uart.c |
||
102 | beeptime = 1000; |
||
103 | } else Piep(WinkelOut.CalcState); |
||
104 | } |
||
105 | DebugOut.Analog[19] = WinkelOut.CalcState; |
||
106 | } |
||
107 | |||
108 | //############################################################################ |
||
109 | //Hauptprogramm |
||
110 | |||
111 | int main(void) |
||
112 | //############################################################################ |
||
113 | { |
||
114 | unsigned int timer; |
||
115 | |||
116 | //unsigned int timer2 = 0; |
||
117 | DDRB = 0x00; |
||
118 | PORTB = 0x00; |
||
119 | for (timer = 0; timer < 1000; timer++); // verzögern |
||
120 | if (PINB & 0x01) { |
||
121 | if (PINB & 0x02) PlatinenVersion = 13; |
||
122 | else PlatinenVersion = 11; |
||
123 | } else { |
||
124 | if (PINB & 0x02) PlatinenVersion = 20; |
||
125 | else PlatinenVersion = 10; |
||
126 | } |
||
127 | |||
128 | DDRC = 0x81; // SCL |
||
129 | DDRC |= 0x40; // HEF4017 Reset |
||
130 | PORTC = 0xff; // Pullup SDA |
||
131 | DDRB = 0x1B; // LEDs und Druckoffset |
||
132 | PORTB = 0x01; // LED_Rot |
||
133 | DDRD = 0x3E; // Speaker & TXD & J3 J4 J5 |
||
134 | DDRD |= 0x80; // J7 -> Servo signal |
||
135 | PORTD = 0x77; // LED |
||
136 | |||
137 | |||
138 | MCUSR &= ~(1 << WDRF); |
||
139 | WDTCSR |= (1 << WDCE) | (1 << WDE); |
||
140 | WDTCSR = 0; |
||
141 | |||
142 | beeptime = 2000; |
||
143 | |||
144 | StickGier = 0; |
||
145 | PPM_in[K_GAS] = 0; |
||
146 | StickRoll = 0; |
||
147 | StickNick = 0; |
||
148 | |||
149 | ROT_OFF; |
||
150 | |||
151 | Timer_Init(); |
||
152 | UART_Init(); |
||
153 | rc_sum_init(); |
||
154 | ADC_Init(); |
||
155 | i2c_init(); |
||
156 | SPI_MasterInit(); |
||
157 | |||
158 | sei(); |
||
159 | |||
160 | printf("\n\rFlightControl\n\rHardware:%d.%d\n\rSoftware:V%d.%d%c ", PlatinenVersion / 10, PlatinenVersion % 10, VERSION_MAJOR, VERSION_MINOR, VERSION_PATCH + 'a'); |
||
161 | printf("\n\r=============================="); |
||
162 | |||
163 | GRN_ON; |
||
164 | |||
165 | |||
166 | ReadParameterSet(3, (unsigned char *) & EE_Parameter.Kanalbelegung[0], 9); // read only the first bytes |
||
167 | // valid Stick-Settings? |
||
168 | if (eeprom_read_byte(&EEPromArray[EEPROM_ADR_VALID]) == 255 || eeprom_read_byte(&EEPromArray[EEPROM_ADR_VALID]) < EE_DATENREVISION || |
||
169 | EE_Parameter.Kanalbelegung[0] > 9 || EE_Parameter.Kanalbelegung[1] > 9 || EE_Parameter.Kanalbelegung[2] > 9 || EE_Parameter.Kanalbelegung[3] > 9) { |
||
170 | printf("\n\rInit. EEPROM: Generating Default-Parameter and Stick-Settings..."); |
||
171 | DefaultStickMapping(); |
||
172 | } else if (eeprom_read_byte(&EEPromArray[EEPROM_ADR_VALID]) != EE_DATENREVISION) printf("\n\rInit. EEPROM: Generating Default-Parameter using old Stick Settings"); |
||
173 | |||
174 | if (eeprom_read_byte(&EEPromArray[EEPROM_ADR_VALID]) != EE_DATENREVISION) { |
||
175 | DefaultKonstanten1(); |
||
176 | for (unsigned char i = 1; i < 6; i++) { |
||
177 | if (i == 2) DefaultKonstanten2(); // Kamera |
||
178 | if (i == 3) DefaultKonstanten3(); // Beginner |
||
179 | if (i > 3) DefaultKonstanten2(); // Kamera |
||
180 | WriteParameterSet(i, (unsigned char *) & EE_Parameter.Kanalbelegung[0], STRUCT_PARAM_LAENGE); |
||
181 | } |
||
182 | SetActiveParamSetNumber(3); // default-Setting |
||
183 | eeprom_write_byte(&EEPromArray[EEPROM_ADR_VALID], EE_DATENREVISION); |
||
184 | } |
||
185 | |||
186 | if (eeprom_read_byte(&EEPromArray[EEPROM_ADR_ACC_NICK]) > 4) { |
||
187 | printf("\n\rACC nicht abgeglichen!"); |
||
188 | } |
||
189 | |||
190 | ReadParameterSet(GetActiveParamSetNumber(), (unsigned char *) & EE_Parameter.Kanalbelegung[0], STRUCT_PARAM_LAENGE); |
||
191 | printf("\n\rBenutze Parametersatz %d", GetActiveParamSetNumber()); |
||
192 | |||
193 | |||
194 | if (EE_Parameter.GlobalConfig & CFG_HOEHENREGELUNG) { |
||
195 | printf("\n\rAbgleich Luftdrucksensor.."); |
||
196 | timer = SetDelay(1000); |
||
197 | SucheLuftruckOffset(); |
||
198 | while (!CheckDelay(timer)); |
||
199 | printf("OK\n\r"); |
||
200 | } |
||
201 | |||
202 | SetNeutral(); |
||
203 | |||
204 | ROT_OFF; |
||
205 | |||
206 | beeptime = 2000; |
||
207 | ExternControl.Digital[0] = 0x55; |
||
208 | |||
209 | |||
210 | printf("\n\rSteuerung: "); |
||
211 | if (EE_Parameter.GlobalConfig & CFG_HEADING_HOLD) printf("HeadingHold"); |
||
212 | else printf("Neutral"); |
||
213 | |||
214 | printf("\n\n\r"); |
||
215 | |||
216 | LcdClear(); |
||
217 | I2CTimeout = 5000; |
||
218 | WinkelOut.Orientation = 1; |
||
219 | while (1) { |
||
220 | |||
221 | if (UpdateMotor) // ReglerIntervall |
||
222 | { |
||
223 | UpdateMotor = 0; |
||
224 | //PORTD |= 0x08; |
||
225 | if (WinkelOut.CalcState) CalMk3Mag(); |
||
226 | else MotorRegler(); |
||
227 | //PORTD &= ~0x08; |
||
228 | SendMotorData(); |
||
229 | ROT_OFF; |
||
230 | if (PcZugriff) PcZugriff--; |
||
231 | else { |
||
232 | ExternControl.Config = 0; |
||
233 | ExternStickNick = 0; |
||
234 | ExternStickRoll = 0; |
||
235 | ExternStickGier = 0; |
||
236 | } |
||
237 | if (SenderOkay) SenderOkay--; |
||
238 | if (!I2CTimeout) { |
||
239 | I2CTimeout = 5; |
||
240 | i2c_reset(); |
||
241 | if ((BeepMuster == 0xffff) && MotorenEin) { |
||
242 | beeptime = 10000; |
||
243 | BeepMuster = 0x0080; |
||
244 | } |
||
245 | } else { |
||
246 | I2CTimeout--; |
||
247 | ROT_OFF; |
||
248 | } |
||
249 | if (SIO_DEBUG && (!UpdateMotor || !MotorenEin)) { |
||
250 | DatenUebertragung(); |
||
251 | BearbeiteRxDaten(); |
||
252 | } else BearbeiteRxDaten(); |
||
253 | if (CheckDelay(timer)) { |
||
254 | if (UBat < EE_Parameter.UnterspannungsWarnung) { |
||
255 | if (BeepMuster == 0xffff) { |
||
256 | beeptime = 6000; |
||
257 | BeepMuster = 0x0300; |
||
258 | } |
||
259 | } |
||
260 | /* if(SendVersionToNavi) |
||
261 | { |
||
262 | SPI_StartTransmitPacket(SPI_CMD_VERSION);//# |
||
263 | SendVersionToNavi = 0; |
||
264 | } |
||
265 | else SPI_StartTransmitPacket(SPI_CMD_VALUE);//# |
||
266 | */ |
||
267 | SPI_StartTransmitPacket(); //# |
||
268 | |||
269 | SendSPI = 4; |
||
270 | timer = SetDelay(20); |
||
271 | } |
||
272 | //if(UpdateMotor) DebugOut.Analog[26]++; |
||
273 | LED_Update(); |
||
274 | } |
||
275 | if (!SendSPI) { |
||
276 | SPI_TransmitByte(); |
||
277 | } |
||
278 | } |
||
279 | return (1); |
||
280 | } |
||
281 |