Rev 360 | Details | Compare with Previous | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
241 | killagreg | 1 | /*#######################################################################################*/ |
2 | /* !!! THIS IS NOT FREE SOFTWARE !!! */ |
||
3 | /*#######################################################################################*/ |
||
4 | // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
||
5 | // + www.MikroKopter.com |
||
6 | // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
||
360 | holgerb | 7 | // + Software Nutzungsbedingungen (english version: see below) |
8 | // + der Fa. HiSystems GmbH, Flachsmeerstrasse 2, 26802 Moormerland - nachfolgend Lizenzgeber genannt - |
||
9 | // + Der Lizenzgeber räumt dem Kunden ein nicht-ausschließliches, zeitlich und räumlich* unbeschränktes Recht ein, die im den |
||
10 | // + Mikrocontroller verwendete Firmware für die Hardware Flight-Ctrl, Navi-Ctrl, BL-Ctrl, MK3Mag & PC-Programm MikroKopter-Tool |
||
11 | // + - nachfolgend Software genannt - nur für private Zwecke zu nutzen. |
||
12 | // + Der Einsatz dieser Software ist nur auf oder mit Produkten des Lizenzgebers zulässig. |
||
241 | killagreg | 13 | // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
360 | holgerb | 14 | // + Die vom Lizenzgeber gelieferte Software ist urheberrechtlich geschützt. Alle Rechte an der Software sowie an sonstigen im |
15 | // + Rahmen der Vertragsanbahnung und Vertragsdurchführung überlassenen Unterlagen stehen im Verhältnis der Vertragspartner ausschließlich dem Lizenzgeber zu. |
||
16 | // + Die in der Software enthaltenen Copyright-Vermerke, Markenzeichen, andere Rechtsvorbehalte, Seriennummern sowie |
||
17 | // + sonstige der Programmidentifikation dienenden Merkmale dürfen vom Kunden nicht verändert oder unkenntlich gemacht werden. |
||
18 | // + Der Kunde trifft angemessene Vorkehrungen für den sicheren Einsatz der Software. Er wird die Software gründlich auf deren |
||
19 | // + Verwendbarkeit zu dem von ihm beabsichtigten Zweck testen, bevor er diese operativ einsetzt. |
||
20 | // + Die Haftung des Lizenzgebers wird - soweit gesetzlich zulässig - begrenzt in Höhe des typischen und vorhersehbaren |
||
21 | // + Schadens. Die gesetzliche Haftung bei Personenschäden und nach dem Produkthaftungsgesetz bleibt unberührt. Dem Lizenzgeber steht jedoch der Einwand |
||
22 | // + des Mitverschuldens offen. |
||
23 | // + Der Kunde trifft angemessene Vorkehrungen für den Fall, dass die Software ganz oder teilweise nicht ordnungsgemäß arbeitet. |
||
24 | // + Er wird die Software gründlich auf deren Verwendbarkeit zu dem von ihm beabsichtigten Zweck testen, bevor er diese operativ einsetzt. |
||
25 | // + Der Kunde wird er seine Daten vor Einsatz der Software nach dem Stand der Technik sichern. |
||
26 | // + Der Kunde ist darüber unterrichtet, dass der Lizenzgeber seine Daten im zur Vertragsdurchführung erforderlichen Umfang |
||
27 | // + und auf Grundlage der Datenschutzvorschriften erhebt, speichert, verarbeitet und, sofern notwendig, an Dritte übermittelt. |
||
28 | // + *) Die räumliche Nutzung bezieht sich nur auf den Einsatzort, nicht auf die Reichweite der programmierten Software. |
||
29 | // + #### ENDE DER NUTZUNGSBEDINGUNGEN ####' |
||
30 | // + Hinweis: Informationen über erweiterte Nutzungsrechte (wie z.B. Nutzung für nicht-private Zwecke) sind auf Anfrage per Email an info(@)hisystems.de verfügbar. |
||
241 | killagreg | 31 | // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
360 | holgerb | 32 | // + Software LICENSING TERMS |
241 | killagreg | 33 | // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
360 | holgerb | 34 | // + of HiSystems GmbH, Flachsmeerstrasse 2, 26802 Moormerland, Germany - the Licensor - |
35 | // + The Licensor grants the customer a non-exclusive license to use the microcontroller firmware of the Flight-Ctrl, Navi-Ctrl, BL-Ctrl, and MK3Mag hardware |
||
36 | // + (the Software) exclusively for private purposes. The License is unrestricted with respect to time and territory*. |
||
37 | // + The Software may only be used with the Licensor's products. |
||
38 | // + The Software provided by the Licensor is protected by copyright. With respect to the relationship between the parties to this |
||
39 | // + agreement, all rights pertaining to the Software and other documents provided during the preparation and execution of this |
||
40 | // + agreement shall be the property of the Licensor. |
||
41 | // + The information contained in the Software copyright notices, trademarks, other legal reservations, serial numbers and other |
||
42 | // + features that can be used to identify the program may not be altered or defaced by the customer. |
||
43 | // + The customer shall be responsible for taking reasonable precautions |
||
44 | // + for the safe use of the Software. The customer shall test the Software thoroughly regarding its suitability for the |
||
45 | // + intended purpose before implementing it for actual operation. The Licensor's liability shall be limited to the extent of typical and |
||
46 | // + foreseeable damage to the extent permitted by law, notwithstanding statutory liability for bodily injury and product |
||
47 | // + liability. However, the Licensor shall be entitled to the defense of contributory negligence. |
||
48 | // + The customer will take adequate precautions in the case, that the software is not working properly. The customer will test |
||
49 | // + the software for his purpose before any operational usage. The customer will backup his data before using the software. |
||
50 | // + The customer understands that the Licensor collects, stores and processes, and, where required, forwards, customer data |
||
51 | // + to third parties to the extent necessary for executing the agreement, subject to applicable data protection and privacy regulations. |
||
52 | // + *) The territory aspect only refers to the place where the Software is used, not its programmed range. |
||
53 | // + #### END OF LICENSING TERMS #### |
||
54 | // + Note: For information on license extensions (e.g. commercial use), please contact us at info(@)hisystems.de. |
||
241 | killagreg | 55 | // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
292 | killagreg | 56 | #include <stdio.h> |
241 | killagreg | 57 | #include <string.h> |
58 | #include "91x_lib.h" |
||
59 | #include "mk3mag.h" |
||
60 | #include "i2c.h" |
||
61 | #include "timer1.h" |
||
242 | killagreg | 62 | #include "led.h" |
241 | killagreg | 63 | #include "main.h" |
64 | #include "uart1.h" |
||
242 | killagreg | 65 | #include "compass.h" |
241 | killagreg | 66 | |
67 | #define MK3MAG_SLAVE_ADDRESS 0x50 // i2C slave address |
||
68 | |||
242 | killagreg | 69 | u8 MK3MAG_Present = 0; |
241 | killagreg | 70 | |
242 | killagreg | 71 | typedef struct |
72 | { |
||
73 | s16 Nick; |
||
74 | s16 Roll; |
||
75 | } __attribute__((packed)) MK3MAG_WriteAttitude_t; |
||
76 | |||
292 | killagreg | 77 | typedef struct |
78 | { |
||
79 | u8 Major; |
||
80 | u8 Minor; |
||
81 | u8 Patch; |
||
82 | u8 Compatible; |
||
83 | } __attribute__((packed)) MK3MAG_Version_t; |
||
84 | |||
85 | typedef struct |
||
86 | { |
||
87 | u8 CalByte; |
||
88 | u8 Dummy1; |
||
89 | u8 Dummy2; |
||
90 | } __attribute__((packed)) MK3MAG_Cal_t; |
||
91 | |||
241 | killagreg | 92 | // Transfer buffers |
93 | volatile MK3MAG_WriteAttitude_t MK3MAG_WriteAttitude; |
||
94 | volatile MK3MAG_Version_t MK3MAG_Version; |
||
95 | volatile MK3MAG_Cal_t MK3MAG_WriteCal; |
||
96 | volatile MK3MAG_Cal_t MK3MAG_ReadCal; |
||
97 | |||
98 | // ------------------------------------------------------------------------------- |
||
99 | // the calculation of the MK3MAG packet checksum |
||
100 | |||
101 | // calculate the crc of bytecount bytes in buffer |
||
102 | u8 MK3MAG_CalcCRC(u8* pBuffer, u8 bytecount) |
||
103 | { |
||
104 | u8 i, crc = 0; |
||
105 | for(i=0; i < bytecount; i++) |
||
106 | { |
||
107 | crc += pBuffer[i]; |
||
108 | } |
||
109 | crc = ~crc; |
||
110 | return crc; |
||
111 | } |
||
112 | |||
113 | // assuming the last byte in buffer is the crc byte |
||
114 | u8 MK3MAG_CheckCRC(u8* pBuffer, u8 BuffLen) |
||
115 | { |
||
116 | u8 crc = 0, retval = 0; |
||
117 | if(BuffLen == 0) return(retval); |
||
118 | crc = MK3MAG_CalcCRC(pBuffer, BuffLen-1); |
||
119 | if(crc == pBuffer[BuffLen-1]) |
||
120 | { |
||
121 | retval = 1; |
||
122 | DebugOut.Analog[15]++; // count mk3mag ok |
||
123 | } |
||
124 | else |
||
125 | { |
||
126 | retval = 0; |
||
127 | DebugOut.Analog[14]++; // count mk3mag crc error |
||
128 | } |
||
129 | return(retval); |
||
130 | } |
||
131 | |||
132 | // ------------------------------------------------------------------------------- |
||
133 | // the I2C rx data handler functions |
||
134 | |||
135 | // rx data handler for version info request |
||
136 | void MK3MAG_UpdateVersion(u8* pRxBuffer, u8 RxBufferSize) |
||
137 | { // if crc is ok and number of byte are matching |
||
138 | if(MK3MAG_CheckCRC(pRxBuffer, RxBufferSize) && (RxBufferSize == (sizeof(MK3MAG_Version)+1)) ) |
||
139 | { |
||
140 | memcpy((u8 *)&MK3MAG_Version, pRxBuffer, sizeof(MK3MAG_Version)); |
||
141 | } |
||
142 | } |
||
143 | |||
144 | // rx data handler for calibration request |
||
145 | void MK3MAG_UpdateCalibration(u8* pRxBuffer, u8 RxBufferSize) |
||
146 | { // if crc is ok and number of byte are matching |
||
147 | if(MK3MAG_CheckCRC(pRxBuffer, RxBufferSize) && (RxBufferSize == (sizeof(MK3MAG_ReadCal)+1)) ) |
||
148 | { |
||
149 | memcpy((u8 *)&MK3MAG_ReadCal, pRxBuffer, sizeof(MK3MAG_ReadCal)); |
||
150 | } |
||
151 | } |
||
152 | |||
153 | // rx data handler for magnetic vector request |
||
154 | void MK3MAG_UpdateMagVector(u8* pRxBuffer, u8 RxBufferSize) |
||
155 | { // if crc is ok and number of byte are matching |
||
242 | killagreg | 156 | if(MK3MAG_CheckCRC(pRxBuffer, RxBufferSize) && (RxBufferSize == (sizeof(MagVector)+1)) ) |
241 | killagreg | 157 | { |
292 | killagreg | 158 | s16vec_t *pMagVector = (s16vec_t*)pRxBuffer; |
159 | MagVector.X = pMagVector->Y; |
||
160 | MagVector.Y = pMagVector->X; |
||
161 | MagVector.Z = -pMagVector->Z; |
||
162 | Compass_CalcHeading(); |
||
241 | killagreg | 163 | } |
164 | } |
||
165 | |||
292 | killagreg | 166 | //---------------------------------------------------------------- |
167 | #define MK3MAG_CMD_VERSION 0x01 |
||
168 | #define MK3MAG_CMD_READ_MAGVECT 0x02 |
||
169 | #define MK3MAG_CMD_WRITE_CAL 0x04 |
||
241 | killagreg | 170 | |
171 | void MK3MAG_SendCommand(u8 command) |
||
172 | { |
||
288 | killagreg | 173 | // try to catch the I2C buffer |
248 | killagreg | 174 | if(I2C_LockBuffer(0)) |
241 | killagreg | 175 | { |
248 | killagreg | 176 | u16 TxBytes = 0; |
177 | u16 RxBytes = 0; |
||
241 | killagreg | 178 | I2C_pRxHandler_t pRxHandlerFunc = NULL; |
248 | killagreg | 179 | |
241 | killagreg | 180 | // update current command id |
248 | killagreg | 181 | I2C_Buffer[TxBytes++] = command; |
241 | killagreg | 182 | |
183 | // set pointers to data area with respect to the command id |
||
184 | switch (command) |
||
185 | { |
||
186 | case MK3MAG_CMD_VERSION: |
||
187 | RxBytes = sizeof(MK3MAG_Version)+1; |
||
188 | pRxHandlerFunc = &MK3MAG_UpdateVersion; |
||
189 | break; |
||
190 | case MK3MAG_CMD_WRITE_CAL: |
||
191 | RxBytes = sizeof(MK3MAG_ReadCal)+1; |
||
192 | pRxHandlerFunc = &MK3MAG_UpdateCalibration; |
||
248 | killagreg | 193 | memcpy((u8*)I2C_Buffer+1, (u8*)&MK3MAG_WriteCal, sizeof(MK3MAG_WriteCal)); |
194 | TxBytes += sizeof(MK3MAG_WriteCal); |
||
241 | killagreg | 195 | break; |
196 | case MK3MAG_CMD_READ_MAGVECT: |
||
242 | killagreg | 197 | RxBytes = sizeof(MagVector)+1; |
241 | killagreg | 198 | pRxHandlerFunc = &MK3MAG_UpdateMagVector; |
199 | break; |
||
200 | default: // unknown command id |
||
201 | RxBytes = 0; |
||
202 | pRxHandlerFunc = NULL; |
||
203 | break; |
||
204 | } |
||
205 | // update packet checksum |
||
248 | killagreg | 206 | I2C_Buffer[TxBytes] = MK3MAG_CalcCRC((u8*)I2C_Buffer, TxBytes); |
207 | TxBytes++; |
||
241 | killagreg | 208 | // initiate I2C transmission |
248 | killagreg | 209 | I2C_Transmission(MK3MAG_SLAVE_ADDRESS, TxBytes, pRxHandlerFunc, RxBytes); |
242 | killagreg | 210 | } // EOF I2C_State == I2C_IDLE |
241 | killagreg | 211 | } |
212 | |||
213 | |||
214 | //---------------------------------------------------------------- |
||
242 | killagreg | 215 | u8 MK3MAG_Init(void) |
241 | killagreg | 216 | { |
217 | u8 msg[64]; |
||
218 | u8 repeat; |
||
219 | u32 timeout; |
||
220 | |||
242 | killagreg | 221 | MK3MAG_Present = 0; |
241 | killagreg | 222 | |
223 | MK3MAG_Version.Major = 0xFF; |
||
224 | MK3MAG_Version.Minor = 0xFF; |
||
225 | MK3MAG_Version.Patch = 0xFF; |
||
226 | MK3MAG_Version.Compatible = 0xFF; |
||
242 | killagreg | 227 | |
254 | killagreg | 228 | Compass_Heading = -1; |
242 | killagreg | 229 | |
241 | killagreg | 230 | // polling of version info |
231 | repeat = 0; |
||
232 | do |
||
233 | { |
||
234 | MK3MAG_SendCommand(MK3MAG_CMD_VERSION); |
||
342 | holgerb | 235 | if(Version_HW > 11) timeout = SetDelay(100); |
236 | else timeout = SetDelay(250); |
||
237 | |||
241 | killagreg | 238 | do |
239 | { |
||
240 | if (MK3MAG_Version.Major != 0xFF) break; // break loop on success |
||
241 | }while (!CheckDelay(timeout)); |
||
242 | UART1_PutString("."); |
||
243 | repeat++; |
||
244 | }while ((MK3MAG_Version.Major == 0xFF) && (repeat < 12)); // 12*250ms=3s |
||
245 | // if we got it |
||
246 | if (MK3MAG_Version.Major != 0xFF) |
||
247 | { |
||
242 | killagreg | 248 | sprintf(msg, " MK3MAG V%d.%d%c", MK3MAG_Version.Major, MK3MAG_Version.Minor, 'a' + MK3MAG_Version.Patch); |
241 | killagreg | 249 | UART1_PutString(msg); |
242 | killagreg | 250 | if(MK3MAG_Version.Compatible != MK3MAG_I2C_COMPATIBLE) |
251 | { |
||
252 | UART1_PutString("\n\r MK3MAG not compatible!"); |
||
256 | killagreg | 253 | UART_VersionInfo.HardwareError[0] |= NC_ERROR0_COMPASS_INCOMPATIBLE; |
242 | killagreg | 254 | LED_RED_ON; |
255 | } |
||
256 | else |
||
257 | { // version ok |
||
258 | MK3MAG_Present = 1; |
||
259 | } |
||
241 | killagreg | 260 | } |
242 | killagreg | 261 | return(MK3MAG_Present); |
241 | killagreg | 262 | } |
263 | |||
264 | //---------------------------------------------------------------- |
||
292 | killagreg | 265 | void MK3MAG_Update(void) |
241 | killagreg | 266 | { |
292 | killagreg | 267 | static u32 TimerUpdate = 0; |
268 | static s16 x_max,y_max,z_max,x_min,y_min,z_min; |
||
339 | holgerb | 269 | static u8 last_state; |
270 | u8 msg[64]; |
||
271 | u16 MinCaclibration = 500; |
||
241 | killagreg | 272 | |
242 | killagreg | 273 | if( (I2C_State == I2C_STATE_OFF) || !MK3MAG_Present ) return; |
241 | killagreg | 274 | |
292 | killagreg | 275 | if(CheckDelay(TimerUpdate)) |
241 | killagreg | 276 | { |
277 | // check for incomming compass calibration request |
||
254 | killagreg | 278 | Compass_UpdateCalState(); |
279 | MK3MAG_WriteCal.CalByte = Compass_CalState; |
||
292 | killagreg | 280 | |
241 | killagreg | 281 | if(MK3MAG_ReadCal.CalByte != MK3MAG_WriteCal.CalByte) |
292 | killagreg | 282 | { // send new calibration state |
241 | killagreg | 283 | MK3MAG_SendCommand(MK3MAG_CMD_WRITE_CAL); |
284 | } |
||
292 | killagreg | 285 | else |
286 | { // calibration state matches |
||
287 | MK3MAG_SendCommand(MK3MAG_CMD_READ_MAGVECT); // initiate magvector transfer |
||
288 | |||
289 | switch(Compass_CalState) |
||
290 | { |
||
291 | case 1: |
||
339 | holgerb | 292 | if(last_state != Compass_CalState) |
293 | { |
||
294 | UART1_PutString("\r\nMK3Mag calibration\n\r"); |
||
342 | holgerb | 295 | if(EarthMagneticStrengthTheoretic) |
296 | { |
||
297 | MinCaclibration = (MinCaclibration * EarthMagneticStrengthTheoretic) / 50; |
||
298 | sprintf(msg, "Earth field on your location should be: %iuT\r\n",EarthMagneticStrengthTheoretic); |
||
299 | UART1_PutString(msg); |
||
300 | } |
||
301 | else UART1_PutString("without GPS\n\r"); |
||
339 | holgerb | 302 | } |
292 | killagreg | 303 | x_max = -30000; y_max = -30000; z_max = -30000; |
304 | x_min = 30000; y_min = 30000; z_min = 30000; |
||
305 | break; |
||
306 | |||
307 | case 2: |
||
308 | if(MagVector.X > x_max) { x_max = MagVector.X; BeepTime = 60; } |
||
309 | else if(MagVector.X < x_min) { x_min = MagVector.X; BeepTime = 20; } |
||
310 | if(MagVector.Y > y_max) { y_max = MagVector.Y; BeepTime = 60; } |
||
311 | else if(MagVector.Y < y_min) { y_min = MagVector.Y; BeepTime = 20; } |
||
312 | break; |
||
313 | |||
314 | case 4: |
||
315 | if(MagVector.Z > z_max) { z_max = MagVector.Z; BeepTime = 80; } |
||
316 | else if(MagVector.Z < z_min) { z_min = MagVector.Z; BeepTime = 80; } |
||
317 | break; |
||
339 | holgerb | 318 | case 5: |
319 | if(last_state == Compass_CalState) break; |
||
320 | if(((x_max - x_min) > MinCaclibration) && ((y_max - y_min) > MinCaclibration) && ((z_max - z_min) > MinCaclibration)) |
||
321 | { |
||
322 | BeepTime = 2500; |
||
342 | holgerb | 323 | UART1_PutString("\r\n-> Calibration okay <-\n\r"); |
339 | holgerb | 324 | } |
325 | else |
||
326 | { |
||
327 | UART1_PutString("\r\nCalibration FAILED - Values too low: "); |
||
328 | if((x_max - x_min) < MinCaclibration) UART1_PutString("X! "); |
||
342 | holgerb | 329 | if((y_max - y_min) < MinCaclibration) UART1_PutString("Y! "); |
339 | holgerb | 330 | if((z_max - z_min) < MinCaclibration) UART1_PutString("Z! "); |
331 | UART1_PutString("\r\n"); |
||
332 | } |
||
333 | UART1_PutString(msg); |
||
334 | sprintf(msg, "\r\nX: (%i - %i = %i)\r\n",x_max,x_min,x_max - x_min); |
||
335 | UART1_PutString(msg); |
||
336 | sprintf(msg, "Y: (%i - %i = %i)\r\n",y_max,y_min,y_max - y_min); |
||
337 | UART1_PutString(msg); |
||
338 | sprintf(msg, "Z: (%i - %i = %i)\r\n",z_max,z_min,z_max - z_min); |
||
339 | UART1_PutString(msg); |
||
342 | holgerb | 340 | sprintf(msg, "(Minimum ampilitude is: %i)\r\n",MinCaclibration); |
341 | UART1_PutString(msg); |
||
339 | holgerb | 342 | break; |
292 | killagreg | 343 | default: |
344 | break; |
||
339 | holgerb | 345 | } |
346 | last_state = Compass_CalState; |
||
287 | holgerb | 347 | } |
292 | killagreg | 348 | TimerUpdate = SetDelay(20); // every 20 ms are 50 Hz |
241 | killagreg | 349 | } |
350 | } |