Rev 487 | Rev 489 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
242 | 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. |
||
242 | 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. |
||
242 | killagreg | 31 | // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
360 | holgerb | 32 | // + Software LICENSING TERMS |
242 | 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. |
||
242 | killagreg | 55 | // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
254 | killagreg | 56 | #include <math.h> |
292 | killagreg | 57 | #include <stdio.h> |
242 | killagreg | 58 | #include <string.h> |
59 | #include "91x_lib.h" |
||
253 | killagreg | 60 | #include "ncmag.h" |
483 | holgerb | 61 | #include "i2c1.h" |
62 | #include "i2c0.h" |
||
63 | |||
242 | killagreg | 64 | #include "timer1.h" |
65 | #include "led.h" |
||
66 | #include "uart1.h" |
||
254 | killagreg | 67 | #include "eeprom.h" |
256 | killagreg | 68 | #include "mymath.h" |
292 | killagreg | 69 | #include "main.h" |
454 | holgerb | 70 | #include "spi_slave.h" |
242 | killagreg | 71 | |
253 | killagreg | 72 | u8 NCMAG_Present = 0; |
254 | killagreg | 73 | u8 NCMAG_IsCalibrated = 0; |
242 | killagreg | 74 | |
483 | holgerb | 75 | u8 I2C_CompassPort = 1; |
76 | u8 ExtCompassOrientation = 0; |
||
472 | holgerb | 77 | |
483 | holgerb | 78 | u8 *I2C_BufferPnt; |
79 | u8 *I2C_ErrorPnt; |
||
80 | I2C_TransmissionFunc_t I2C_TransmissionFunc; |
||
81 | I2C_LockBufferFunc_t I2C_LockBufferFunc; |
||
82 | I2C_WaitForEndOfTransmissionFunc_t I2C_WaitForEndOfTransmissionFunc; |
||
83 | |||
84 | |||
394 | killagreg | 85 | // supported magnetic sensor types |
86 | #define TYPE_NONE 0 |
||
87 | #define TYPE_HMC5843 1 |
||
88 | #define TYPE_LSM303DLH 2 |
||
89 | #define TYPE_LSM303DLM 3 |
||
242 | killagreg | 90 | |
394 | killagreg | 91 | u8 NCMAG_SensorType = TYPE_NONE; |
92 | |||
254 | killagreg | 93 | Calibration_t Calibration; // calibration data in RAM |
339 | holgerb | 94 | volatile s16vec_t AccRawVector; |
95 | volatile s16vec_t MagRawVector; |
||
254 | killagreg | 96 | |
253 | killagreg | 97 | // i2c MAG interface |
98 | #define MAG_SLAVE_ADDRESS 0x3C // i2C slave address mag. sensor registers |
||
242 | killagreg | 99 | |
253 | killagreg | 100 | // register mapping |
101 | #define REG_MAG_CRA 0x00 |
||
102 | #define REG_MAG_CRB 0x01 |
||
103 | #define REG_MAG_MODE 0x02 |
||
104 | #define REG_MAG_DATAX_MSB 0x03 |
||
105 | #define REG_MAG_DATAX_LSB 0x04 |
||
106 | #define REG_MAG_DATAY_MSB 0x05 |
||
107 | #define REG_MAG_DATAY_LSB 0x06 |
||
108 | #define REG_MAG_DATAZ_MSB 0x07 |
||
109 | #define REG_MAG_DATAZ_LSB 0x08 |
||
110 | #define REG_MAG_STATUS 0x09 |
||
329 | holgerb | 111 | |
253 | killagreg | 112 | #define REG_MAG_IDA 0x0A |
113 | #define REG_MAG_IDB 0x0B |
||
114 | #define REG_MAG_IDC 0x0C |
||
394 | killagreg | 115 | #define REG_MAG_IDF 0x0F // WHO_AM_I _M = 0x03c when LSM303DLM is connected |
242 | killagreg | 116 | |
253 | killagreg | 117 | // bit mask for configuration mode |
118 | #define CRA_MODE_MASK 0x03 |
||
119 | #define CRA_MODE_NORMAL 0x00 //default |
||
120 | #define CRA_MODE_POSBIAS 0x01 |
||
121 | #define CRA_MODE_NEGBIAS 0x02 |
||
122 | #define CRA_MODE_SELFTEST 0x03 |
||
242 | killagreg | 123 | |
253 | killagreg | 124 | // bit mask for measurement mode |
125 | #define MODE_MASK 0xFF |
||
126 | #define MODE_CONTINUOUS 0x00 |
||
127 | #define MODE_SINGLE 0x01 // default |
||
128 | #define MODE_IDLE 0x02 |
||
129 | #define MODE_SLEEP 0x03 |
||
130 | |||
242 | killagreg | 131 | // bit mask for rate |
253 | killagreg | 132 | #define CRA_RATE_MASK 0x1C |
133 | |||
134 | // bit mask for gain |
||
135 | #define CRB_GAIN_MASK 0xE0 |
||
136 | |||
137 | // ids |
||
138 | #define MAG_IDA 0x48 |
||
139 | #define MAG_IDB 0x34 |
||
140 | #define MAG_IDC 0x33 |
||
394 | killagreg | 141 | #define MAG_IDF_LSM303DLM 0x3C |
253 | killagreg | 142 | |
143 | // the special HMC5843 interface |
||
144 | // bit mask for rate |
||
242 | killagreg | 145 | #define HMC5843_CRA_RATE_0_5HZ 0x00 |
146 | #define HMC5843_CRA_RATE_1HZ 0x04 |
||
147 | #define HMC5843_CRA_RATE_2HZ 0x08 |
||
148 | #define HMC5843_CRA_RATE_5HZ 0x0C |
||
149 | #define HMC5843_CRA_RATE_10HZ 0x10 //default |
||
150 | #define HMC5843_CRA_RATE_20HZ 0x14 |
||
151 | #define HMC5843_CRA_RATE_50HZ 0x18 |
||
152 | // bit mask for gain |
||
153 | #define HMC5843_CRB_GAIN_07GA 0x00 |
||
154 | #define HMC5843_CRB_GAIN_10GA 0x20 //default |
||
339 | holgerb | 155 | #define HMC5843_CRB_GAIN_15GA 0x40 // <--- we use this |
242 | killagreg | 156 | #define HMC5843_CRB_GAIN_20GA 0x60 |
157 | #define HMC5843_CRB_GAIN_32GA 0x80 |
||
158 | #define HMC5843_CRB_GAIN_38GA 0xA0 |
||
159 | #define HMC5843_CRB_GAIN_45GA 0xC0 |
||
160 | #define HMC5843_CRB_GAIN_65GA 0xE0 |
||
253 | killagreg | 161 | // self test value |
339 | holgerb | 162 | #define HMC5843_TEST_XSCALE 555 |
163 | #define HMC5843_TEST_YSCALE 555 |
||
164 | #define HMC5843_TEST_ZSCALE 555 |
||
394 | killagreg | 165 | // calibration range |
342 | holgerb | 166 | #define HMC5843_CALIBRATION_RANGE 600 |
242 | killagreg | 167 | |
253 | killagreg | 168 | // the special LSM302DLH interface |
169 | // bit mask for rate |
||
170 | #define LSM303DLH_CRA_RATE_0_75HZ 0x00 |
||
171 | #define LSM303DLH_CRA_RATE_1_5HZ 0x04 |
||
172 | #define LSM303DLH_CRA_RATE_3_0HZ 0x08 |
||
173 | #define LSM303DLH_CRA_RATE_7_5HZ 0x0C |
||
174 | #define LSM303DLH_CRA_RATE_15HZ 0x10 //default |
||
175 | #define LSM303DLH_CRA_RATE_30HZ 0x14 |
||
176 | #define LSM303DLH_CRA_RATE_75HZ 0x18 |
||
338 | holgerb | 177 | |
253 | killagreg | 178 | // bit mask for gain |
179 | #define LSM303DLH_CRB_GAIN_XXGA 0x00 |
||
180 | #define LSM303DLH_CRB_GAIN_13GA 0x20 //default |
||
339 | holgerb | 181 | #define LSM303DLH_CRB_GAIN_19GA 0x40 // <--- we use this |
253 | killagreg | 182 | #define LSM303DLH_CRB_GAIN_25GA 0x60 |
183 | #define LSM303DLH_CRB_GAIN_40GA 0x80 |
||
184 | #define LSM303DLH_CRB_GAIN_47GA 0xA0 |
||
185 | #define LSM303DLH_CRB_GAIN_56GA 0xC0 |
||
186 | #define LSM303DLH_CRB_GAIN_81GA 0xE0 |
||
394 | killagreg | 187 | |
188 | typedef struct |
||
189 | { |
||
190 | u8 A; |
||
191 | u8 B; |
||
192 | u8 C; |
||
193 | } __attribute__((packed)) Identification_t; |
||
194 | volatile Identification_t NCMAG_Identification; |
||
195 | |||
196 | typedef struct |
||
197 | { |
||
198 | u8 Sub; |
||
199 | } __attribute__((packed)) Identification2_t; |
||
200 | volatile Identification2_t NCMAG_Identification2; |
||
201 | |||
202 | typedef struct |
||
203 | { |
||
204 | u8 cra; |
||
205 | u8 crb; |
||
206 | u8 mode; |
||
207 | } __attribute__((packed)) MagConfig_t; |
||
208 | |||
209 | volatile MagConfig_t MagConfig; |
||
210 | |||
483 | holgerb | 211 | |
212 | |||
213 | |||
214 | |||
253 | killagreg | 215 | // self test value |
338 | holgerb | 216 | #define LSM303DLH_TEST_XSCALE 495 |
217 | #define LSM303DLH_TEST_YSCALE 495 |
||
218 | #define LSM303DLH_TEST_ZSCALE 470 |
||
339 | holgerb | 219 | // clibration range |
342 | holgerb | 220 | #define LSM303_CALIBRATION_RANGE 550 |
253 | killagreg | 221 | |
222 | // the i2c ACC interface |
||
223 | #define ACC_SLAVE_ADDRESS 0x30 // i2c slave for acc. sensor registers |
||
394 | killagreg | 224 | |
225 | // multiple byte read/write mask |
||
226 | #define REG_ACC_MASK_AUTOINCREMENT 0x80 |
||
227 | |||
253 | killagreg | 228 | // register mapping |
229 | #define REG_ACC_CTRL1 0x20 |
||
230 | #define REG_ACC_CTRL2 0x21 |
||
231 | #define REG_ACC_CTRL3 0x22 |
||
232 | #define REG_ACC_CTRL4 0x23 |
||
233 | #define REG_ACC_CTRL5 0x24 |
||
234 | #define REG_ACC_HP_FILTER_RESET 0x25 |
||
235 | #define REG_ACC_REFERENCE 0x26 |
||
236 | #define REG_ACC_STATUS 0x27 |
||
237 | #define REG_ACC_X_LSB 0x28 |
||
238 | #define REG_ACC_X_MSB 0x29 |
||
239 | #define REG_ACC_Y_LSB 0x2A |
||
240 | #define REG_ACC_Y_MSB 0x2B |
||
241 | #define REG_ACC_Z_LSB 0x2C |
||
242 | #define REG_ACC_Z_MSB 0x2D |
||
243 | |||
394 | killagreg | 244 | #define ACC_CRTL1_PM_DOWN 0x00 |
245 | #define ACC_CRTL1_PM_NORMAL 0x20 |
||
246 | #define ACC_CRTL1_PM_LOW_0_5HZ 0x40 |
||
247 | #define ACC_CRTL1_PM_LOW_1HZ 0x60 |
||
248 | #define ACC_CRTL1_PM_LOW_2HZ 0x80 |
||
249 | #define ACC_CRTL1_PM_LOW_5HZ 0xA0 |
||
250 | #define ACC_CRTL1_PM_LOW_10HZ 0xC0 |
||
251 | // Output data rate in normal power mode |
||
252 | #define ACC_CRTL1_DR_50HZ 0x00 |
||
253 | #define ACC_CRTL1_DR_100HZ 0x08 |
||
254 | #define ACC_CRTL1_DR_400HZ 0x10 |
||
255 | #define ACC_CRTL1_DR_1000HZ 0x18 |
||
256 | // axis anable flags |
||
257 | #define ACC_CRTL1_XEN 0x01 |
||
258 | #define ACC_CRTL1_YEN 0x02 |
||
259 | #define ACC_CRTL1_ZEN 0x04 |
||
253 | killagreg | 260 | |
397 | holgerb | 261 | #define ACC_CRTL2_FILTER8 0x10 |
262 | #define ACC_CRTL2_FILTER16 0x11 |
||
263 | #define ACC_CRTL2_FILTER32 0x12 |
||
264 | #define ACC_CRTL2_FILTER64 0x13 |
||
395 | holgerb | 265 | |
394 | killagreg | 266 | #define ACC_CTRL4_BDU 0x80 // Block data update, (0: continuos update; 1: output registers not updated between MSB and LSB reading) |
267 | #define ACC_CTRL4_BLE 0x40 // Big/little endian, (0: data LSB @ lower address; 1: data MSB @ lower address) |
||
268 | #define ACC_CTRL4_FS_2G 0x00 |
||
269 | #define ACC_CTRL4_FS_4G 0x10 |
||
270 | #define ACC_CTRL4_FS_8G 0x30 |
||
271 | #define ACC_CTRL4_STSIGN_PLUS 0x00 |
||
272 | #define ACC_CTRL4_STSIGN_MINUS 0x08 |
||
273 | #define ACC_CTRL4_ST_ENABLE 0x02 |
||
253 | killagreg | 274 | |
394 | killagreg | 275 | #define ACC_CTRL5_STW_ON 0x03 |
276 | #define ACC_CTRL5_STW_OFF 0x00 |
||
242 | killagreg | 277 | |
253 | killagreg | 278 | typedef struct |
279 | { |
||
280 | u8 ctrl_1; |
||
281 | u8 ctrl_2; |
||
282 | u8 ctrl_3; |
||
283 | u8 ctrl_4; |
||
284 | u8 ctrl_5; |
||
285 | } __attribute__((packed)) AccConfig_t; |
||
286 | |||
287 | volatile AccConfig_t AccConfig; |
||
288 | |||
483 | holgerb | 289 | u8 NCMag_CalibrationWrite(u8 intern) // two calibrtion sets for extern and intern sensor |
254 | killagreg | 290 | { |
472 | holgerb | 291 | u16 address; |
483 | holgerb | 292 | u8 i, crc = MAG_CALIBRATION_COMPATIBLE; |
254 | killagreg | 293 | EEPROM_Result_t eres; |
294 | u8 *pBuff = (u8*)&Calibration; |
||
483 | holgerb | 295 | Calibration.Version = CALIBRATION_VERSION; |
254 | killagreg | 296 | |
483 | holgerb | 297 | if(intern == I2C_INTERN_1) address = EEPROM_ADR_MAG_CALIBRATION_INTERN; |
298 | else |
||
473 | holgerb | 299 | { |
483 | holgerb | 300 | address = EEPROM_ADR_MAG_CALIBRATION_EXTERN; |
301 | Calibration.Version = CALIBRATION_VERSION + ExtCompassOrientation * 16; |
||
473 | holgerb | 302 | } |
256 | killagreg | 303 | for(i = 0; i<(sizeof(Calibration)-1); i++) |
254 | killagreg | 304 | { |
305 | crc += pBuff[i]; |
||
306 | } |
||
307 | Calibration.crc = ~crc; |
||
472 | holgerb | 308 | eres = EEPROM_WriteBlock(address, pBuff, sizeof(Calibration)); |
254 | killagreg | 309 | if(EEPROM_SUCCESS == eres) i = 1; |
310 | else i = 0; |
||
311 | return(i); |
||
312 | } |
||
313 | |||
483 | holgerb | 314 | u8 NCMag_CalibrationRead(u8 intern) // two calibrtion sets for extern and intern sensor |
254 | killagreg | 315 | { |
483 | holgerb | 316 | u8 i, crc = MAG_CALIBRATION_COMPATIBLE; |
254 | killagreg | 317 | u8 *pBuff = (u8*)&Calibration; |
483 | holgerb | 318 | u16 address; |
254 | killagreg | 319 | |
483 | holgerb | 320 | if(intern == I2C_INTERN_1) address = EEPROM_ADR_MAG_CALIBRATION_INTERN; |
321 | else address = EEPROM_ADR_MAG_CALIBRATION_EXTERN; |
||
472 | holgerb | 322 | |
323 | if(EEPROM_SUCCESS == EEPROM_ReadBlock(address, pBuff, sizeof(Calibration))) |
||
254 | killagreg | 324 | { |
256 | killagreg | 325 | for(i = 0; i<(sizeof(Calibration)-1); i++) |
254 | killagreg | 326 | { |
327 | crc += pBuff[i]; |
||
328 | } |
||
329 | crc = ~crc; |
||
330 | if(Calibration.crc != crc) return(0); // crc mismatch |
||
483 | holgerb | 331 | if((Calibration.Version & 0x0f) == CALIBRATION_VERSION) return(1); |
254 | killagreg | 332 | } |
333 | return(0); |
||
334 | } |
||
335 | |||
336 | |||
337 | void NCMAG_Calibrate(void) |
||
338 | { |
||
330 | holgerb | 339 | u8 msg[64]; |
454 | holgerb | 340 | static u8 speak = 0; |
483 | holgerb | 341 | static s16 Xmin = 0, Xmax = 0, Ymin = 0, Ymax = 0, Zmin = 0, Zmax = 0, Zmin2 = 0, Zmax2 = 0; |
256 | killagreg | 342 | static s16 X = 0, Y = 0, Z = 0; |
254 | killagreg | 343 | static u8 OldCalState = 0; |
394 | killagreg | 344 | s16 MinCalibration = 450; |
254 | killagreg | 345 | |
488 | holgerb | 346 | X = (X + MagRawVector.X)/2; |
347 | Y = (Y + MagRawVector.Y)/2; |
||
348 | Z = (Z + MagRawVector.Z)/2; |
||
256 | killagreg | 349 | |
254 | killagreg | 350 | switch(Compass_CalState) |
351 | { |
||
352 | case 1: |
||
353 | // 1st step of calibration |
||
354 | // initialize ranges |
||
355 | // used to change the orientation of the NC in the horizontal plane |
||
356 | Xmin = 10000; |
||
357 | Xmax = -10000; |
||
358 | Ymin = 10000; |
||
359 | Ymax = -10000; |
||
360 | Zmin = 10000; |
||
361 | Zmax = -10000; |
||
483 | holgerb | 362 | speak = 1; |
488 | holgerb | 363 | CompassValueErrorCount = 0; |
483 | holgerb | 364 | if(Compass_CalState != OldCalState) // avoid continously writing of eeprom! |
475 | holgerb | 365 | { |
483 | holgerb | 366 | UART1_PutString("\r\nStarting compass calibration"); |
367 | if(I2C_CompassPort == I2C_EXTERN_0) |
||
368 | { |
||
369 | if(!ExtCompassOrientation) ExtCompassOrientation = GetExtCompassOrientation(); |
||
370 | UART1_PutString(" - External sensor "); |
||
371 | sprintf(msg, "with orientation: %d ",ExtCompassOrientation); |
||
372 | UART1_PutString(msg); |
||
373 | } |
||
374 | else UART1_PutString(" - Internal sensor "); |
||
375 | } |
||
254 | killagreg | 376 | break; |
377 | case 2: // 2nd step of calibration |
||
378 | // find Min and Max of the X- and Y-Sensors during rotation in the horizontal plane |
||
275 | killagreg | 379 | if(X < Xmin) { Xmin = X; BeepTime = 20;} |
380 | else if(X > Xmax) { Xmax = X; BeepTime = 20;} |
||
381 | if(Y < Ymin) { Ymin = Y; BeepTime = 60;} |
||
382 | else if(Y > Ymax) { Ymax = Y; BeepTime = 60;} |
||
483 | holgerb | 383 | if(Z < Zmin) { Zmin = Z; } // silent |
475 | holgerb | 384 | else if(Z > Zmax) { Zmax = Z; } |
483 | holgerb | 385 | |
454 | holgerb | 386 | if(speak) SpeakHoTT = SPEAK_CALIBRATE; speak = 0; |
254 | killagreg | 387 | break; |
388 | |||
389 | case 3: // 3rd step of calibration |
||
390 | // used to change the orientation of the MK3MAG vertical to the horizontal plane |
||
454 | holgerb | 391 | speak = 1; |
254 | killagreg | 392 | break; |
393 | |||
394 | case 4: |
||
395 | // find Min and Max of the Z-Sensor |
||
483 | holgerb | 396 | if(Z < Zmin2) { Zmin2 = Z; BeepTime = 80;} |
397 | else if(Z > Zmax2) { Zmax2 = Z; BeepTime = 80;} |
||
398 | |||
399 | if(X < Xmin) { Xmin = X; BeepTime = 20;} |
||
400 | else if(X > Xmax) { Xmax = X; BeepTime = 20;} |
||
475 | holgerb | 401 | if(Y < Ymin) { Ymin = Y; BeepTime = 60;} |
402 | else if(Y > Ymax) { Ymax = Y; BeepTime = 60;} |
||
483 | holgerb | 403 | |
454 | holgerb | 404 | if(speak) SpeakHoTT = SPEAK_CALIBRATE; speak = 0; |
254 | killagreg | 405 | break; |
406 | |||
407 | case 5: |
||
408 | // Save values |
||
409 | if(Compass_CalState != OldCalState) // avoid continously writing of eeprom! |
||
410 | { |
||
394 | killagreg | 411 | switch(NCMAG_SensorType) |
412 | { |
||
413 | case TYPE_HMC5843: |
||
483 | holgerb | 414 | UART1_PutString("\r\nFinished: HMC5843 calibration\n\r"); |
415 | MinCalibration = HMC5843_CALIBRATION_RANGE; |
||
416 | break; |
||
394 | killagreg | 417 | |
418 | case TYPE_LSM303DLH: |
||
419 | case TYPE_LSM303DLM: |
||
483 | holgerb | 420 | UART1_PutString("\r\nFinished: LSM303 calibration\n\r"); |
421 | MinCalibration = LSM303_CALIBRATION_RANGE; |
||
422 | break; |
||
394 | killagreg | 423 | } |
342 | holgerb | 424 | if(EarthMagneticStrengthTheoretic) |
483 | holgerb | 425 | { |
426 | MinCalibration = (MinCalibration * EarthMagneticStrengthTheoretic) / 50; |
||
427 | sprintf(msg, "Earth field on your location should be: %iuT\r\n",EarthMagneticStrengthTheoretic); |
||
428 | UART1_PutString(msg); |
||
429 | } |
||
342 | holgerb | 430 | else UART1_PutString("without GPS\n\r"); |
339 | holgerb | 431 | |
483 | holgerb | 432 | if(Zmin2 < Zmin) { Zmin = Zmin2; } |
433 | else if(Zmax2 > Zmax) { Zmax = Zmax2; } |
||
434 | |||
254 | killagreg | 435 | Calibration.MagX.Range = Xmax - Xmin; |
436 | Calibration.MagX.Offset = (Xmin + Xmax) / 2; |
||
437 | Calibration.MagY.Range = Ymax - Ymin; |
||
438 | Calibration.MagY.Offset = (Ymin + Ymax) / 2; |
||
439 | Calibration.MagZ.Range = Zmax - Zmin; |
||
440 | Calibration.MagZ.Offset = (Zmin + Zmax) / 2; |
||
488 | holgerb | 441 | if(CompassValueErrorCount) |
442 | { |
||
443 | SpeakHoTT = SPEAK_ERR_CALIBARTION; |
||
444 | UART1_PutString("\r\nCalibration FAILED - Compass sensor error !!!!\r\n "); |
||
445 | |||
446 | } |
||
447 | else |
||
394 | killagreg | 448 | if((Calibration.MagX.Range > MinCalibration) && (Calibration.MagY.Range > MinCalibration) && (Calibration.MagZ.Range > MinCalibration)) |
254 | killagreg | 449 | { |
483 | holgerb | 450 | NCMAG_IsCalibrated = NCMag_CalibrationWrite(I2C_CompassPort); |
270 | killagreg | 451 | BeepTime = 2500; |
342 | holgerb | 452 | UART1_PutString("\r\n-> Calibration okay <-\n\r"); |
454 | holgerb | 453 | SpeakHoTT = SPEAK_MIKROKOPTER; |
254 | killagreg | 454 | } |
455 | else |
||
456 | { |
||
454 | holgerb | 457 | SpeakHoTT = SPEAK_ERR_CALIBARTION; |
339 | holgerb | 458 | UART1_PutString("\r\nCalibration FAILED - Values too low: "); |
394 | killagreg | 459 | if(Calibration.MagX.Range < MinCalibration) UART1_PutString("X! "); |
460 | if(Calibration.MagY.Range < MinCalibration) UART1_PutString("Y! "); |
||
461 | if(Calibration.MagZ.Range < MinCalibration) UART1_PutString("Z! "); |
||
330 | holgerb | 462 | UART1_PutString("\r\n"); |
339 | holgerb | 463 | |
254 | killagreg | 464 | // restore old calibration data from eeprom |
483 | holgerb | 465 | NCMAG_IsCalibrated = NCMag_CalibrationRead(I2C_CompassPort); |
254 | killagreg | 466 | } |
330 | holgerb | 467 | sprintf(msg, "X: (%i - %i = %i)\r\n",Xmax,Xmin,Xmax - Xmin); |
468 | UART1_PutString(msg); |
||
469 | sprintf(msg, "Y: (%i - %i = %i)\r\n",Ymax,Ymin,Ymax - Ymin); |
||
470 | UART1_PutString(msg); |
||
471 | sprintf(msg, "Z: (%i - %i = %i)\r\n",Zmax,Zmin,Zmax - Zmin); |
||
472 | UART1_PutString(msg); |
||
394 | killagreg | 473 | sprintf(msg, "(Minimum ampilitude is: %i)\r\n",MinCalibration); |
342 | holgerb | 474 | UART1_PutString(msg); |
254 | killagreg | 475 | } |
476 | break; |
||
477 | |||
478 | default: |
||
479 | break; |
||
480 | } |
||
481 | OldCalState = Compass_CalState; |
||
482 | } |
||
483 | |||
242 | killagreg | 484 | // ---------- call back handlers ----------------------------------------- |
485 | |||
486 | // rx data handler for id info request |
||
253 | killagreg | 487 | void NCMAG_UpdateIdentification(u8* pRxBuffer, u8 RxBufferSize) |
254 | killagreg | 488 | { // if number of bytes are matching |
253 | killagreg | 489 | if(RxBufferSize == sizeof(NCMAG_Identification) ) |
242 | killagreg | 490 | { |
253 | killagreg | 491 | memcpy((u8 *)&NCMAG_Identification, pRxBuffer, sizeof(NCMAG_Identification)); |
492 | } |
||
242 | killagreg | 493 | } |
329 | holgerb | 494 | |
495 | void NCMAG_UpdateIdentification_Sub(u8* pRxBuffer, u8 RxBufferSize) |
||
496 | { // if number of bytes are matching |
||
497 | if(RxBufferSize == sizeof(NCMAG_Identification2)) |
||
498 | { |
||
499 | memcpy((u8 *)&NCMAG_Identification2, pRxBuffer, sizeof(NCMAG_Identification2)); |
||
500 | } |
||
501 | } |
||
502 | |||
254 | killagreg | 503 | // rx data handler for magnetic sensor raw data |
253 | killagreg | 504 | void NCMAG_UpdateMagVector(u8* pRxBuffer, u8 RxBufferSize) |
254 | killagreg | 505 | { // if number of bytes are matching |
506 | if(RxBufferSize == sizeof(MagRawVector) ) |
||
243 | killagreg | 507 | { // byte order from big to little endian |
473 | holgerb | 508 | s16 raw, X = 0, Y = 0, Z = 0; |
256 | killagreg | 509 | raw = pRxBuffer[0]<<8; |
510 | raw+= pRxBuffer[1]; |
||
473 | holgerb | 511 | if(raw >= NCMAG_MIN_RAWVALUE && raw <= NCMAG_MAX_RAWVALUE) X = raw; |
487 | holgerb | 512 | else if(CompassValueErrorCount < 35) CompassValueErrorCount++; // invalid data |
513 | |||
256 | killagreg | 514 | raw = pRxBuffer[2]<<8; |
515 | raw+= pRxBuffer[3]; |
||
330 | holgerb | 516 | if(raw >= NCMAG_MIN_RAWVALUE && raw <= NCMAG_MAX_RAWVALUE) |
517 | { |
||
473 | holgerb | 518 | if(NCMAG_SensorType == TYPE_LSM303DLM) Z = raw; // here Z and Y are exchanged |
519 | else Y = raw; |
||
330 | holgerb | 520 | } |
487 | holgerb | 521 | else if(CompassValueErrorCount < 35) CompassValueErrorCount++; // invalid data |
522 | |||
256 | killagreg | 523 | raw = pRxBuffer[4]<<8; |
524 | raw+= pRxBuffer[5]; |
||
330 | holgerb | 525 | if(raw >= NCMAG_MIN_RAWVALUE && raw <= NCMAG_MAX_RAWVALUE) |
526 | { |
||
473 | holgerb | 527 | if(NCMAG_SensorType == TYPE_LSM303DLM) Y = raw; // here Z and Y are exchanged |
528 | else Z = raw; |
||
330 | holgerb | 529 | } |
487 | holgerb | 530 | else if(CompassValueErrorCount < 35) CompassValueErrorCount++; // invalid data |
531 | |||
483 | holgerb | 532 | switch(ExtCompassOrientation) |
473 | holgerb | 533 | { |
483 | holgerb | 534 | case 0: |
535 | case 1: |
||
536 | default: |
||
473 | holgerb | 537 | MagRawVector.X = X; |
538 | MagRawVector.Y = Y; |
||
539 | MagRawVector.Z = Z; |
||
540 | break; |
||
483 | holgerb | 541 | case 2: |
473 | holgerb | 542 | MagRawVector.X = -X; |
543 | MagRawVector.Y = Y; |
||
544 | MagRawVector.Z = -Z; |
||
545 | break; |
||
483 | holgerb | 546 | case 3: |
473 | holgerb | 547 | MagRawVector.X = -Z; |
548 | MagRawVector.Y = Y; |
||
549 | MagRawVector.Z = X; |
||
550 | break; |
||
483 | holgerb | 551 | case 4: |
473 | holgerb | 552 | MagRawVector.X = Z; |
553 | MagRawVector.Y = Y; |
||
554 | MagRawVector.Z = -X; |
||
555 | break; |
||
483 | holgerb | 556 | case 5: |
473 | holgerb | 557 | MagRawVector.X = X; |
558 | MagRawVector.Y = -Z; |
||
559 | MagRawVector.Z = Y; |
||
560 | break; |
||
483 | holgerb | 561 | case 6: |
473 | holgerb | 562 | MagRawVector.X = -X; |
563 | MagRawVector.Y = -Z; |
||
564 | MagRawVector.Z = -Y; |
||
565 | break; |
||
566 | } |
||
242 | killagreg | 567 | } |
254 | killagreg | 568 | if(Compass_CalState || !NCMAG_IsCalibrated) |
284 | killagreg | 569 | { // mark out data invalid |
289 | killagreg | 570 | MagVector.X = MagRawVector.X; |
571 | MagVector.Y = MagRawVector.Y; |
||
572 | MagVector.Z = MagRawVector.Z; |
||
254 | killagreg | 573 | Compass_Heading = -1; |
574 | } |
||
575 | else |
||
576 | { |
||
577 | // update MagVector from MagRaw Vector by Scaling |
||
578 | MagVector.X = (s16)((1024L*(s32)(MagRawVector.X - Calibration.MagX.Offset))/Calibration.MagX.Range); |
||
579 | MagVector.Y = (s16)((1024L*(s32)(MagRawVector.Y - Calibration.MagY.Offset))/Calibration.MagY.Range); |
||
580 | MagVector.Z = (s16)((1024L*(s32)(MagRawVector.Z - Calibration.MagZ.Offset))/Calibration.MagZ.Range); |
||
292 | killagreg | 581 | Compass_CalcHeading(); |
254 | killagreg | 582 | } |
242 | killagreg | 583 | } |
254 | killagreg | 584 | // rx data handler for acceleration raw data |
253 | killagreg | 585 | void NCMAG_UpdateAccVector(u8* pRxBuffer, u8 RxBufferSize) |
483 | holgerb | 586 | { // if number of byte are matching |
254 | killagreg | 587 | if(RxBufferSize == sizeof(AccRawVector) ) |
483 | holgerb | 588 | { |
254 | killagreg | 589 | memcpy((u8*)&AccRawVector, pRxBuffer,sizeof(AccRawVector)); |
253 | killagreg | 590 | } |
473 | holgerb | 591 | } |
483 | holgerb | 592 | |
593 | u8 GetExtCompassOrientation(void) |
||
594 | { |
||
595 | if(I2C_CompassPort != I2C_EXTERN_0) return(0); |
||
596 | |||
597 | if((abs(FromFlightCtrl.AngleNick) > 300) || (abs(FromFlightCtrl.AngleRoll) > 300)) |
||
598 | { |
||
599 | // UART1_PutString("\r\nTilted"); |
||
600 | return(0); |
||
601 | } |
||
602 | if(AccRawVector.Z > 3300) return(1); // Flach - Bestückung oben - Pfeil nach vorn |
||
603 | else |
||
604 | if(AccRawVector.Z < -3300) return(2); // Flach - Bestückung unten - Pfeil nach vorn |
||
605 | else |
||
606 | if(AccRawVector.X > 3300) return(3); // Flach - Bestückung Links - Pfeil nach vorn |
||
607 | else |
||
608 | if(AccRawVector.X < -3300) return(4); // Flach - Bestückung rechts - Pfeil nach vorn |
||
609 | else |
||
610 | if(AccRawVector.Y > 3300) return(5); // Stehend - Pfeil nach oben - 'front' nach vorn |
||
611 | else |
||
612 | if(AccRawVector.Y < -3300) return(6); // Stehend - Pfeil nach unten - 'front' nach vorn |
||
613 | return(0); |
||
614 | } |
||
615 | |||
254 | killagreg | 616 | // rx data handler for reading magnetic sensor configuration |
253 | killagreg | 617 | void NCMAG_UpdateMagConfig(u8* pRxBuffer, u8 RxBufferSize) |
618 | { // if number of byte are matching |
||
619 | if(RxBufferSize == sizeof(MagConfig) ) |
||
620 | { |
||
621 | memcpy((u8*)(&MagConfig), pRxBuffer, sizeof(MagConfig)); |
||
622 | } |
||
623 | } |
||
254 | killagreg | 624 | // rx data handler for reading acceleration sensor configuration |
253 | killagreg | 625 | void NCMAG_UpdateAccConfig(u8* pRxBuffer, u8 RxBufferSize) |
626 | { // if number of byte are matching |
||
627 | if(RxBufferSize == sizeof(AccConfig) ) |
||
628 | { |
||
629 | memcpy((u8*)&AccConfig, pRxBuffer, sizeof(AccConfig)); |
||
630 | } |
||
631 | } |
||
254 | killagreg | 632 | //---------------------------------------------------------------------- |
253 | killagreg | 633 | |
254 | killagreg | 634 | |
635 | // --------------------------------------------------------------------- |
||
253 | killagreg | 636 | u8 NCMAG_SetMagConfig(void) |
637 | { |
||
638 | u8 retval = 0; |
||
639 | // try to catch the i2c buffer within 100 ms timeout |
||
483 | holgerb | 640 | if(I2C_LockBufferFunc(100)) |
253 | killagreg | 641 | { |
642 | u8 TxBytes = 0; |
||
483 | holgerb | 643 | I2C_BufferPnt[TxBytes++] = REG_MAG_CRA; |
644 | memcpy((u8*)(&I2C_BufferPnt[TxBytes]), (u8*)&MagConfig, sizeof(MagConfig)); |
||
253 | killagreg | 645 | TxBytes += sizeof(MagConfig); |
483 | holgerb | 646 | if(I2C_TransmissionFunc(MAG_SLAVE_ADDRESS, TxBytes, 0, 0)) |
253 | killagreg | 647 | { |
483 | holgerb | 648 | if(I2C_WaitForEndOfTransmissionFunc(100)) |
253 | killagreg | 649 | { |
483 | holgerb | 650 | if(*I2C_ErrorPnt == I2C_ERROR_NONE) retval = 1; |
253 | killagreg | 651 | } |
652 | } |
||
653 | } |
||
654 | return(retval); |
||
655 | } |
||
242 | killagreg | 656 | |
253 | killagreg | 657 | // ---------------------------------------------------------------------------------------- |
658 | u8 NCMAG_GetMagConfig(void) |
||
242 | killagreg | 659 | { |
253 | killagreg | 660 | u8 retval = 0; |
252 | killagreg | 661 | // try to catch the i2c buffer within 100 ms timeout |
483 | holgerb | 662 | if(I2C_LockBufferFunc(100)) |
242 | killagreg | 663 | { |
253 | killagreg | 664 | u8 TxBytes = 0; |
483 | holgerb | 665 | I2C_BufferPnt[TxBytes++] = REG_MAG_CRA; |
666 | if(I2C_TransmissionFunc(MAG_SLAVE_ADDRESS, TxBytes, &NCMAG_UpdateMagConfig, sizeof(MagConfig))) |
||
248 | killagreg | 667 | { |
483 | holgerb | 668 | if(I2C_WaitForEndOfTransmissionFunc(100)) |
252 | killagreg | 669 | { |
483 | holgerb | 670 | if(*I2C_ErrorPnt == I2C_ERROR_NONE) retval = 1; |
252 | killagreg | 671 | } |
248 | killagreg | 672 | } |
242 | killagreg | 673 | } |
253 | killagreg | 674 | return(retval); |
242 | killagreg | 675 | } |
676 | |||
677 | // ---------------------------------------------------------------------------------------- |
||
253 | killagreg | 678 | u8 NCMAG_SetAccConfig(void) |
242 | killagreg | 679 | { |
252 | killagreg | 680 | u8 retval = 0; |
483 | holgerb | 681 | // try to catch the i2c buffer within 100 ms timeout |
682 | if(I2C_LockBufferFunc(50)) |
||
242 | killagreg | 683 | { |
253 | killagreg | 684 | u8 TxBytes = 0; |
483 | holgerb | 685 | I2C_BufferPnt[TxBytes++] = REG_ACC_CTRL1|REG_ACC_MASK_AUTOINCREMENT; |
686 | memcpy((u8*)(&I2C_BufferPnt[TxBytes]), (u8*)&AccConfig, sizeof(AccConfig)); |
||
253 | killagreg | 687 | TxBytes += sizeof(AccConfig); |
483 | holgerb | 688 | if(I2C_TransmissionFunc(ACC_SLAVE_ADDRESS, TxBytes, 0, 0)) |
253 | killagreg | 689 | { |
483 | holgerb | 690 | if(I2C_WaitForEndOfTransmissionFunc(50)) |
253 | killagreg | 691 | { |
483 | holgerb | 692 | if(*I2C_ErrorPnt == I2C_ERROR_NONE) retval = 1; |
253 | killagreg | 693 | } |
694 | } |
||
695 | } |
||
696 | return(retval); |
||
697 | } |
||
698 | |||
699 | // ---------------------------------------------------------------------------------------- |
||
700 | u8 NCMAG_GetAccConfig(void) |
||
701 | { |
||
702 | u8 retval = 0; |
||
703 | // try to catch the i2c buffer within 100 ms timeout |
||
483 | holgerb | 704 | if(I2C_LockBufferFunc(100)) |
253 | killagreg | 705 | { |
706 | u8 TxBytes = 0; |
||
483 | holgerb | 707 | I2C_BufferPnt[TxBytes++] = REG_ACC_CTRL1|REG_ACC_MASK_AUTOINCREMENT; |
708 | if(I2C_TransmissionFunc(ACC_SLAVE_ADDRESS, TxBytes, &NCMAG_UpdateAccConfig, sizeof(AccConfig))) |
||
253 | killagreg | 709 | { |
483 | holgerb | 710 | if(I2C_WaitForEndOfTransmissionFunc(100)) |
253 | killagreg | 711 | { |
483 | holgerb | 712 | if(*I2C_ErrorPnt == I2C_ERROR_NONE) retval = 1; |
253 | killagreg | 713 | } |
714 | } |
||
715 | } |
||
716 | return(retval); |
||
717 | } |
||
718 | |||
719 | // ---------------------------------------------------------------------------------------- |
||
720 | u8 NCMAG_GetIdentification(void) |
||
721 | { |
||
722 | u8 retval = 0; |
||
723 | // try to catch the i2c buffer within 100 ms timeout |
||
483 | holgerb | 724 | if(I2C_LockBufferFunc(100)) |
253 | killagreg | 725 | { |
483 | holgerb | 726 | u16 TxBytes = 0; |
253 | killagreg | 727 | NCMAG_Identification.A = 0xFF; |
728 | NCMAG_Identification.B = 0xFF; |
||
729 | NCMAG_Identification.C = 0xFF; |
||
483 | holgerb | 730 | I2C_BufferPnt[TxBytes++] = REG_MAG_IDA; |
248 | killagreg | 731 | // initiate transmission |
483 | holgerb | 732 | if(I2C_TransmissionFunc(MAG_SLAVE_ADDRESS, TxBytes, &NCMAG_UpdateIdentification, sizeof(NCMAG_Identification))) |
248 | killagreg | 733 | { |
483 | holgerb | 734 | if(I2C_WaitForEndOfTransmissionFunc(100)) |
252 | killagreg | 735 | { |
483 | holgerb | 736 | if(*I2C_ErrorPnt == I2C_ERROR_NONE) retval = 1; |
252 | killagreg | 737 | } |
248 | killagreg | 738 | } |
242 | killagreg | 739 | } |
253 | killagreg | 740 | return(retval); |
242 | killagreg | 741 | } |
742 | |||
329 | holgerb | 743 | u8 NCMAG_GetIdentification_Sub(void) |
744 | { |
||
745 | u8 retval = 0; |
||
746 | // try to catch the i2c buffer within 100 ms timeout |
||
483 | holgerb | 747 | if(I2C_LockBufferFunc(100)) |
329 | holgerb | 748 | { |
483 | holgerb | 749 | u16 TxBytes = 0; |
329 | holgerb | 750 | NCMAG_Identification2.Sub = 0xFF; |
483 | holgerb | 751 | I2C_BufferPnt[TxBytes++] = REG_MAG_IDF; |
329 | holgerb | 752 | // initiate transmission |
483 | holgerb | 753 | if(I2C_TransmissionFunc(MAG_SLAVE_ADDRESS, TxBytes, &NCMAG_UpdateIdentification_Sub, sizeof(NCMAG_Identification2))) |
329 | holgerb | 754 | { |
483 | holgerb | 755 | if(I2C_WaitForEndOfTransmissionFunc(100)) |
329 | holgerb | 756 | { |
483 | holgerb | 757 | if(*I2C_ErrorPnt == I2C_ERROR_NONE) retval = 1; |
329 | holgerb | 758 | } |
759 | } |
||
760 | } |
||
761 | return(retval); |
||
762 | } |
||
763 | |||
764 | |||
253 | killagreg | 765 | // ---------------------------------------------------------------------------------------- |
483 | holgerb | 766 | void NCMAG_GetMagVector(void) |
253 | killagreg | 767 | { |
483 | holgerb | 768 | // try to catch the I2C buffer within 0 ms |
769 | if(I2C_LockBufferFunc(5)) |
||
253 | killagreg | 770 | { |
483 | holgerb | 771 | u16 TxBytes = 0; |
253 | killagreg | 772 | // set register pointer |
483 | holgerb | 773 | I2C_BufferPnt[TxBytes++] = REG_MAG_DATAX_MSB; |
253 | killagreg | 774 | // initiate transmission |
483 | holgerb | 775 | I2C_TransmissionFunc(MAG_SLAVE_ADDRESS, TxBytes, &NCMAG_UpdateMagVector, sizeof(MagVector)); |
253 | killagreg | 776 | } |
777 | } |
||
778 | |||
242 | killagreg | 779 | //---------------------------------------------------------------- |
473 | holgerb | 780 | void NCMAG_GetAccVector(u8 timeout) |
243 | killagreg | 781 | { |
483 | holgerb | 782 | // try to catch the I2C buffer within 0 ms |
783 | if(I2C_LockBufferFunc(timeout)) |
||
243 | killagreg | 784 | { |
483 | holgerb | 785 | u16 TxBytes = 0; |
243 | killagreg | 786 | // set register pointer |
483 | holgerb | 787 | I2C_BufferPnt[TxBytes++] = REG_ACC_X_LSB|REG_ACC_MASK_AUTOINCREMENT; |
243 | killagreg | 788 | // initiate transmission |
483 | holgerb | 789 | I2C_TransmissionFunc(ACC_SLAVE_ADDRESS, TxBytes, &NCMAG_UpdateAccVector, sizeof(AccRawVector)); |
790 | //DebugOut.Analog[16] = AccRawVector.X; |
||
791 | //DebugOut.Analog[17] = AccRawVector.Y; |
||
792 | //DebugOut.Analog[18] = AccRawVector.Z; |
||
243 | killagreg | 793 | } |
794 | } |
||
795 | |||
330 | holgerb | 796 | //---------------------------------------------------------------- |
483 | holgerb | 797 | u8 InitNC_MagnetSensor(void) |
330 | holgerb | 798 | { |
799 | u8 crb_gain, cra_rate; |
||
800 | |||
394 | killagreg | 801 | switch(NCMAG_SensorType) |
330 | holgerb | 802 | { |
394 | killagreg | 803 | case TYPE_HMC5843: |
339 | holgerb | 804 | crb_gain = HMC5843_CRB_GAIN_15GA; |
330 | holgerb | 805 | cra_rate = HMC5843_CRA_RATE_50HZ; |
806 | break; |
||
807 | |||
394 | killagreg | 808 | case TYPE_LSM303DLH: |
809 | case TYPE_LSM303DLM: |
||
338 | holgerb | 810 | crb_gain = LSM303DLH_CRB_GAIN_19GA; |
330 | holgerb | 811 | cra_rate = LSM303DLH_CRA_RATE_75HZ; |
812 | break; |
||
813 | |||
814 | default: |
||
394 | killagreg | 815 | return(0); |
330 | holgerb | 816 | } |
817 | |||
818 | MagConfig.cra = cra_rate|CRA_MODE_NORMAL; |
||
819 | MagConfig.crb = crb_gain; |
||
820 | MagConfig.mode = MODE_CONTINUOUS; |
||
394 | killagreg | 821 | return(NCMAG_SetMagConfig()); |
330 | holgerb | 822 | } |
823 | |||
395 | holgerb | 824 | |
394 | killagreg | 825 | //---------------------------------------------------------------- |
826 | u8 NCMAG_Init_ACCSensor(void) |
||
827 | { |
||
395 | holgerb | 828 | AccConfig.ctrl_1 = ACC_CRTL1_PM_NORMAL|ACC_CRTL1_DR_50HZ|ACC_CRTL1_XEN|ACC_CRTL1_YEN|ACC_CRTL1_ZEN; |
473 | holgerb | 829 | AccConfig.ctrl_2 = 0; |
394 | killagreg | 830 | AccConfig.ctrl_3 = 0x00; |
397 | holgerb | 831 | AccConfig.ctrl_4 = ACC_CTRL4_BDU | ACC_CTRL4_FS_8G; |
394 | killagreg | 832 | AccConfig.ctrl_5 = ACC_CTRL5_STW_OFF; |
833 | return(NCMAG_SetAccConfig()); |
||
834 | } |
||
253 | killagreg | 835 | // -------------------------------------------------------- |
480 | holgerb | 836 | void NCMAG_Update(u8 init) |
243 | killagreg | 837 | { |
292 | killagreg | 838 | static u32 TimerUpdate = 0; |
419 | holgerb | 839 | static s8 send_config = 0; |
394 | killagreg | 840 | u32 delay = 20; |
480 | holgerb | 841 | if(init) TimerUpdate = SetDelay(10); |
842 | |||
483 | holgerb | 843 | // todo State Handling for both busses !! |
844 | if((I2C1_State == I2C_STATE_OFF) || (I2C_CompassPort == 0 && I2C0_State == I2C_STATE_OFF)/* || !NCMAG_Present*/ ) |
||
254 | killagreg | 845 | { |
846 | Compass_Heading = -1; |
||
326 | holgerb | 847 | DebugOut.Analog[14]++; // count I2C error |
480 | holgerb | 848 | TimerUpdate = SetDelay(10); |
254 | killagreg | 849 | return; |
850 | } |
||
483 | holgerb | 851 | if(CheckDelay(TimerUpdate))// && I2C0_State == I2C_STATE_IDLE && I2C1_State == I2C_STATE_IDLE) |
243 | killagreg | 852 | { |
394 | killagreg | 853 | if(Compass_Heading != -1) send_config = 0; // no re-configuration if value is valid |
854 | if(++send_config == 25) // 500ms |
||
855 | { |
||
419 | holgerb | 856 | send_config = -25; // next try after 1 second |
483 | holgerb | 857 | InitNC_MagnetSensor(); |
419 | holgerb | 858 | TimerUpdate = SetDelay(20); // back into the old time-slot |
394 | killagreg | 859 | } |
321 | holgerb | 860 | else |
861 | { |
||
473 | holgerb | 862 | static u8 s = 0; |
394 | killagreg | 863 | // check for new calibration state |
864 | Compass_UpdateCalState(); |
||
865 | if(Compass_CalState) NCMAG_Calibrate(); |
||
866 | |||
867 | // in case of LSM303 type |
||
868 | switch(NCMAG_SensorType) |
||
869 | { |
||
483 | holgerb | 870 | case TYPE_HMC5843: |
871 | NCMAG_GetMagVector(); |
||
872 | delay = 20; |
||
394 | killagreg | 873 | break; |
874 | case TYPE_LSM303DLH: |
||
875 | case TYPE_LSM303DLM: |
||
483 | holgerb | 876 | delay = 20; |
877 | //delay = 2; |
||
878 | if(s-- || (I2C_CompassPort == I2C_INTERN_1)) NCMAG_GetMagVector(); |
||
879 | else |
||
880 | { |
||
881 | if(AccRawVector.X + AccRawVector.Y + AccRawVector.Z == 0) NCMAG_Init_ACCSensor(); |
||
882 | NCMAG_GetAccVector(5); |
||
883 | delay = 10; |
||
884 | s = 40; // about 0,8 sec |
||
885 | }; |
||
886 | if(!s) delay = 10; // ACC-Reading in the next step after 10ms |
||
887 | //if(!s) delay = 2; // ACC-Reading in the next step after 10ms |
||
394 | killagreg | 888 | break; |
889 | } |
||
419 | holgerb | 890 | if(send_config == 24) TimerUpdate = SetDelay(15); // next event is the re-configuration |
394 | killagreg | 891 | else TimerUpdate = SetDelay(delay); // every 20 ms are 50 Hz |
321 | holgerb | 892 | } |
243 | killagreg | 893 | } |
894 | } |
||
895 | |||
330 | holgerb | 896 | |
254 | killagreg | 897 | // -------------------------------------------------------- |
253 | killagreg | 898 | u8 NCMAG_SelfTest(void) |
243 | killagreg | 899 | { |
266 | holgerb | 900 | u8 msg[64]; |
275 | killagreg | 901 | static u8 done = 0; |
266 | holgerb | 902 | |
287 | holgerb | 903 | if(done) return(1); // just make it once |
275 | killagreg | 904 | |
271 | holgerb | 905 | #define LIMITS(value, min, max) {min = (80 * value)/100; max = (120 * value)/100;} |
243 | killagreg | 906 | u32 time; |
253 | killagreg | 907 | s32 XMin = 0, XMax = 0, YMin = 0, YMax = 0, ZMin = 0, ZMax = 0; |
908 | s16 xscale, yscale, zscale, scale_min, scale_max; |
||
909 | u8 crb_gain, cra_rate; |
||
910 | u8 i = 0, retval = 1; |
||
243 | killagreg | 911 | |
394 | killagreg | 912 | switch(NCMAG_SensorType) |
253 | killagreg | 913 | { |
394 | killagreg | 914 | case TYPE_HMC5843: |
339 | holgerb | 915 | crb_gain = HMC5843_CRB_GAIN_15GA; |
253 | killagreg | 916 | cra_rate = HMC5843_CRA_RATE_50HZ; |
917 | xscale = HMC5843_TEST_XSCALE; |
||
918 | yscale = HMC5843_TEST_YSCALE; |
||
919 | zscale = HMC5843_TEST_ZSCALE; |
||
920 | break; |
||
921 | |||
394 | killagreg | 922 | case TYPE_LSM303DLH: |
338 | holgerb | 923 | crb_gain = LSM303DLH_CRB_GAIN_19GA; |
253 | killagreg | 924 | cra_rate = LSM303DLH_CRA_RATE_75HZ; |
925 | xscale = LSM303DLH_TEST_XSCALE; |
||
926 | yscale = LSM303DLH_TEST_YSCALE; |
||
927 | zscale = LSM303DLH_TEST_ZSCALE; |
||
928 | break; |
||
929 | |||
394 | killagreg | 930 | case TYPE_LSM303DLM: |
931 | // does not support self test feature |
||
932 | done = retval; |
||
933 | return(retval); |
||
934 | break; |
||
935 | |||
253 | killagreg | 936 | default: |
394 | killagreg | 937 | return(0); |
253 | killagreg | 938 | } |
939 | |||
940 | MagConfig.cra = cra_rate|CRA_MODE_POSBIAS; |
||
941 | MagConfig.crb = crb_gain; |
||
942 | MagConfig.mode = MODE_CONTINUOUS; |
||
943 | // activate positive bias field |
||
944 | NCMAG_SetMagConfig(); |
||
251 | killagreg | 945 | // wait for stable readings |
946 | time = SetDelay(50); |
||
947 | while(!CheckDelay(time)); |
||
243 | killagreg | 948 | // averaging |
253 | killagreg | 949 | #define AVERAGE 20 |
950 | for(i = 0; i<AVERAGE; i++) |
||
243 | killagreg | 951 | { |
483 | holgerb | 952 | NCMAG_GetMagVector(); |
243 | killagreg | 953 | time = SetDelay(20); |
954 | while(!CheckDelay(time)); |
||
254 | killagreg | 955 | XMax += MagRawVector.X; |
956 | YMax += MagRawVector.Y; |
||
957 | ZMax += MagRawVector.Z; |
||
243 | killagreg | 958 | } |
253 | killagreg | 959 | MagConfig.cra = cra_rate|CRA_MODE_NEGBIAS; |
960 | // activate positive bias field |
||
961 | NCMAG_SetMagConfig(); |
||
251 | killagreg | 962 | // wait for stable readings |
963 | time = SetDelay(50); |
||
964 | while(!CheckDelay(time)); |
||
243 | killagreg | 965 | // averaging |
253 | killagreg | 966 | for(i = 0; i < AVERAGE; i++) |
243 | killagreg | 967 | { |
483 | holgerb | 968 | NCMAG_GetMagVector(); |
243 | killagreg | 969 | time = SetDelay(20); |
970 | while(!CheckDelay(time)); |
||
254 | killagreg | 971 | XMin += MagRawVector.X; |
972 | YMin += MagRawVector.Y; |
||
973 | ZMin += MagRawVector.Z; |
||
243 | killagreg | 974 | } |
975 | // setup final configuration |
||
253 | killagreg | 976 | MagConfig.cra = cra_rate|CRA_MODE_NORMAL; |
977 | // activate positive bias field |
||
978 | NCMAG_SetMagConfig(); |
||
266 | holgerb | 979 | // check scale for all axes |
243 | killagreg | 980 | // prepare scale limits |
253 | killagreg | 981 | LIMITS(xscale, scale_min, scale_max); |
267 | holgerb | 982 | xscale = (XMax - XMin)/(2*AVERAGE); |
266 | holgerb | 983 | if((xscale > scale_max) || (xscale < scale_min)) |
394 | killagreg | 984 | { |
985 | retval = 0; |
||
986 | sprintf(msg, "\r\n Value X: %d not %d-%d !", xscale, scale_min,scale_max); |
||
987 | UART1_PutString(msg); |
||
988 | } |
||
267 | holgerb | 989 | LIMITS(yscale, scale_min, scale_max); |
266 | holgerb | 990 | yscale = (YMax - YMin)/(2*AVERAGE); |
991 | if((yscale > scale_max) || (yscale < scale_min)) |
||
394 | killagreg | 992 | { |
993 | retval = 0; |
||
994 | sprintf(msg, "\r\n Value Y: %d not %d-%d !", yscale, scale_min,scale_max); |
||
995 | UART1_PutString(msg); |
||
996 | } |
||
267 | holgerb | 997 | LIMITS(zscale, scale_min, scale_max); |
266 | holgerb | 998 | zscale = (ZMax - ZMin)/(2*AVERAGE); |
999 | if((zscale > scale_max) || (zscale < scale_min)) |
||
394 | killagreg | 1000 | { |
1001 | retval = 0; |
||
1002 | sprintf(msg, "\r\n Value Z: %d not %d-%d !", zscale, scale_min,scale_max); |
||
1003 | UART1_PutString(msg); |
||
1004 | } |
||
275 | killagreg | 1005 | done = retval; |
253 | killagreg | 1006 | return(retval); |
243 | killagreg | 1007 | } |
1008 | |||
1009 | |||
483 | holgerb | 1010 | //---------------------------------------------------------------- |
1011 | void NCMAG_SelectI2CBus(u8 busno) |
||
1012 | { |
||
1013 | if (busno == 0) |
||
1014 | { |
||
1015 | I2C_WaitForEndOfTransmissionFunc = &I2C0_WaitForEndOfTransmission; |
||
1016 | I2C_LockBufferFunc = &I2C0_LockBuffer; |
||
1017 | I2C_TransmissionFunc = &I2C0_Transmission; |
||
1018 | I2C_BufferPnt = I2C0_Buffer; |
||
1019 | I2C_ErrorPnt = &I2C0_Error; |
||
1020 | } |
||
1021 | else |
||
1022 | { |
||
1023 | I2C_WaitForEndOfTransmissionFunc = &I2C1_WaitForEndOfTransmission; |
||
1024 | I2C_LockBufferFunc = &I2C1_LockBuffer; |
||
1025 | I2C_TransmissionFunc = &I2C1_Transmission; |
||
1026 | I2C_BufferPnt = I2C1_Buffer; |
||
1027 | I2C_ErrorPnt = &I2C1_Error; |
||
1028 | } |
||
465 | ingob | 1029 | } |
483 | holgerb | 1030 | |
465 | ingob | 1031 | //---------------------------------------------------------------- |
253 | killagreg | 1032 | u8 NCMAG_Init(void) |
242 | killagreg | 1033 | { |
483 | holgerb | 1034 | u8 msg[64]; |
1035 | u8 retval = 0; |
||
1036 | u8 repeat = 0; |
||
1037 | |||
1038 | // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
||
1039 | // Search external sensor |
||
1040 | // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
||
1041 | I2C_CompassPort = I2C_EXTERN_0; |
||
1042 | NCMAG_SelectI2CBus(I2C_CompassPort); |
||
1043 | |||
1044 | // get id bytes |
||
1045 | retval = 0; |
||
1046 | for(repeat = 0; repeat < 5; repeat++) |
||
472 | holgerb | 1047 | { |
483 | holgerb | 1048 | // retval = NCMAG_GetIdentification(); |
1049 | retval = NCMAG_GetAccConfig(); // only the sensor with ACC is supported |
||
1050 | if(retval) break; // break loop on success |
||
1051 | UART1_PutString("_"); |
||
1052 | } |
||
1053 | //retval = 1; |
||
1054 | if(!retval) |
||
1055 | { |
||
1056 | UART1_PutString(" internal sensor "); |
||
1057 | I2C_CompassPort = I2C_INTERN_1; |
||
1058 | NCMAG_SelectI2CBus(I2C_CompassPort); |
||
472 | holgerb | 1059 | } |
483 | holgerb | 1060 | else |
472 | holgerb | 1061 | { |
483 | holgerb | 1062 | UART1_PutString(" external sensor "); |
1063 | NCMAG_Init_ACCSensor(); |
||
473 | holgerb | 1064 | |
483 | holgerb | 1065 | for(repeat = 0; repeat < 100; repeat++) |
1066 | { |
||
1067 | NCMAG_GetAccVector(10); // only the sensor with ACC is supported |
||
1068 | ExtCompassOrientation = GetExtCompassOrientation(); |
||
1069 | if(ExtCompassOrientation && (ExtCompassOrientation == Calibration.Version / 16)) break; |
||
1070 | //UART1_Putchar('-'); |
||
1071 | } |
||
1072 | //DebugOut.Analog[19] = repeat; |
||
1073 | |||
1074 | if(!ExtCompassOrientation) UART1_PutString(" (Orientation unknown!)"); |
||
1075 | else |
||
1076 | { |
||
1077 | NCMag_CalibrationRead(I2C_CompassPort); |
||
1078 | sprintf(msg, "with orientation: %d ",ExtCompassOrientation ); |
||
1079 | UART1_PutString(msg); |
||
1080 | if(ExtCompassOrientation != Calibration.Version / 16) |
||
1081 | { |
||
1082 | sprintf(msg, "\n\r! Warning: calibrated orientation was %d !",Calibration.Version / 16); |
||
1083 | UART1_PutString(msg); |
||
1084 | } |
||
1085 | else UART1_PutString("ok "); |
||
1086 | } |
||
1087 | |||
1088 | } |
||
1089 | // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
||
1090 | |||
1091 | NCMAG_Present = 0; |
||
1092 | NCMAG_SensorType = TYPE_HMC5843; // assuming having an HMC5843 |
||
1093 | // polling for LSM302DLH/DLM option by ACC address ack |
||
1094 | for(repeat = 0; repeat < 3; repeat++) |
||
1095 | { |
||
1096 | retval = NCMAG_GetAccConfig(); |
||
1097 | if(retval) break; // break loop on success |
||
1098 | } |
||
1099 | if(retval) |
||
1100 | { |
||
1101 | // initialize ACC sensor |
||
1102 | NCMAG_Init_ACCSensor(); |
||
1103 | |||
1104 | NCMAG_SensorType = TYPE_LSM303DLH; |
||
1105 | // polling of sub identification |
||
1106 | for(repeat = 0; repeat < 12; repeat++) |
||
394 | killagreg | 1107 | { |
483 | holgerb | 1108 | retval = NCMAG_GetIdentification_Sub(); |
394 | killagreg | 1109 | if(retval) break; // break loop on success |
480 | holgerb | 1110 | } |
394 | killagreg | 1111 | if(retval) |
1112 | { |
||
483 | holgerb | 1113 | if(NCMAG_Identification2.Sub == MAG_IDF_LSM303DLM) NCMAG_SensorType = TYPE_LSM303DLM; |
1114 | } |
||
1115 | } |
||
1116 | // get id bytes |
||
1117 | for(repeat = 0; repeat < 3; repeat++) |
||
1118 | { |
||
1119 | retval = NCMAG_GetIdentification(); |
||
1120 | if(retval) break; // break loop on success |
||
1121 | } |
||
1122 | |||
1123 | // if we got an answer to id request |
||
1124 | if(retval) |
||
1125 | { |
||
1126 | u8 n1[] = "\n\r HMC5843"; |
||
1127 | u8 n2[] = "\n\r LSM303DLH"; |
||
1128 | u8 n3[] = "\n\r LSM303DLM"; |
||
1129 | u8* pn = n1; |
||
1130 | |||
1131 | switch(NCMAG_SensorType) |
||
329 | holgerb | 1132 | { |
483 | holgerb | 1133 | case TYPE_HMC5843: |
1134 | pn = n1; |
||
1135 | break; |
||
1136 | case TYPE_LSM303DLH: |
||
1137 | pn = n2; |
||
1138 | break; |
||
1139 | case TYPE_LSM303DLM: |
||
1140 | pn = n3; |
||
1141 | break; |
||
329 | holgerb | 1142 | } |
483 | holgerb | 1143 | |
1144 | sprintf(msg, " %s ID 0x%02x/%02x/%02x-%02x", pn, NCMAG_Identification.A, NCMAG_Identification.B, NCMAG_Identification.C,NCMAG_Identification2.Sub); |
||
1145 | UART1_PutString(msg); |
||
1146 | if ( (NCMAG_Identification.A == MAG_IDA) |
||
1147 | && (NCMAG_Identification.B == MAG_IDB) |
||
1148 | && (NCMAG_Identification.C == MAG_IDC)) |
||
242 | killagreg | 1149 | { |
483 | holgerb | 1150 | NCMAG_Present = 1; |
1151 | |||
1152 | if(EEPROM_Init()) |
||
394 | killagreg | 1153 | { |
483 | holgerb | 1154 | NCMAG_IsCalibrated = NCMag_CalibrationRead(I2C_CompassPort); |
1155 | if(!NCMAG_IsCalibrated) UART1_PutString("\r\n Not calibrated!"); |
||
394 | killagreg | 1156 | } |
483 | holgerb | 1157 | else UART1_PutString("\r\n EEPROM data not available!!!!!!!!!!!!!!!"); |
1158 | // perform self test |
||
1159 | if(!NCMAG_SelfTest()) |
||
1160 | { |
||
1161 | UART1_PutString("\r\n Selftest failed!!!!!!!!!!!!!!!!!!!!\r\n"); |
||
329 | holgerb | 1162 | LED_RED_ON; |
483 | holgerb | 1163 | // NCMAG_IsCalibrated = 0; |
1164 | } |
||
1165 | else UART1_PutString("\r\n Selftest ok"); |
||
1166 | |||
1167 | // initialize magnetic sensor configuration |
||
1168 | InitNC_MagnetSensor(); |
||
242 | killagreg | 1169 | } |
483 | holgerb | 1170 | else |
242 | killagreg | 1171 | { |
483 | holgerb | 1172 | UART1_PutString("\n\r Not compatible!"); |
1173 | UART_VersionInfo.HardwareError[0] |= NC_ERROR0_COMPASS_INCOMPATIBLE; |
||
1174 | LED_RED_ON; |
||
242 | killagreg | 1175 | } |
1176 | } |
||
483 | holgerb | 1177 | else // nothing found |
1178 | { |
||
1179 | NCMAG_SensorType = TYPE_NONE; |
||
1180 | UART1_PutString("not found!"); |
||
1181 | } |
||
253 | killagreg | 1182 | return(NCMAG_Present); |
242 | killagreg | 1183 | } |
1184 |