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