Rev 342 | Rev 454 | Go to most recent revision | Only display areas with differences | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed
Rev 342 | Rev 360 | ||
---|---|---|---|
1 | /*#######################################################################################*/ |
1 | /*#######################################################################################*/ |
2 | /* !!! THIS IS NOT FREE SOFTWARE !!! */ |
2 | /* !!! THIS IS NOT FREE SOFTWARE !!! */ |
3 | /*#######################################################################################*/ |
3 | /*#######################################################################################*/ |
4 | // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
4 | // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
5 | // + Copyright (c) 2008 Ingo Busker, Holger Buss |
- | |
6 | // + Nur für den privaten Gebrauch / NON-COMMERCIAL USE ONLY |
- | |
7 | // + FOR NON COMMERCIAL USE ONLY |
- | |
8 | // + www.MikroKopter.com |
5 | // + www.MikroKopter.com |
9 | // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
6 | // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
- | 7 | // + Software Nutzungsbedingungen (english version: see below) |
|
10 | // + Es gilt für das gesamte Projekt (Hardware, Software, Binärfiles, Sourcecode und Dokumentation), |
8 | // + der Fa. HiSystems GmbH, Flachsmeerstrasse 2, 26802 Moormerland - nachfolgend Lizenzgeber genannt - |
11 | // + dass eine Nutzung (auch auszugsweise) nur für den privaten (nicht-kommerziellen) Gebrauch zulässig ist. |
9 | // + Der Lizenzgeber räumt dem Kunden ein nicht-ausschließliches, zeitlich und räumlich* unbeschränktes Recht ein, die im den |
12 | // + Sollten direkte oder indirekte kommerzielle Absichten verfolgt werden, ist mit uns (info@mikrokopter.de) Kontakt |
10 | // + Mikrocontroller verwendete Firmware für die Hardware Flight-Ctrl, Navi-Ctrl, BL-Ctrl, MK3Mag & PC-Programm MikroKopter-Tool |
13 | // + bzgl. der Nutzungsbedingungen aufzunehmen. |
11 | // + - nachfolgend Software genannt - nur für private Zwecke zu nutzen. |
14 | // + Eine kommerzielle Nutzung ist z.B.Verkauf von MikroKoptern, Bestückung und Verkauf von Platinen oder Bausätzen, |
12 | // + Der Einsatz dieser Software ist nur auf oder mit Produkten des Lizenzgebers zulässig. |
15 | // + Verkauf von Luftbildaufnahmen, usw. |
- | |
16 | // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
13 | // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
17 | // + Werden Teile des Quellcodes (mit oder ohne Modifikation) weiterverwendet oder veröffentlicht, |
14 | // + Die vom Lizenzgeber gelieferte Software ist urheberrechtlich geschützt. Alle Rechte an der Software sowie an sonstigen im |
18 | // + unterliegen sie auch diesen Nutzungsbedingungen und diese Nutzungsbedingungen incl. Copyright müssen dann beiliegen |
15 | // + Rahmen der Vertragsanbahnung und Vertragsdurchführung überlassenen Unterlagen stehen im Verhältnis der Vertragspartner ausschließlich dem Lizenzgeber zu. |
19 | // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
16 | // + Die in der Software enthaltenen Copyright-Vermerke, Markenzeichen, andere Rechtsvorbehalte, Seriennummern sowie |
20 | // + Sollte die Software (auch auszugesweise) oder sonstige Informationen des MikroKopter-Projekts |
17 | // + sonstige der Programmidentifikation dienenden Merkmale dürfen vom Kunden nicht verändert oder unkenntlich gemacht werden. |
21 | // + auf anderen Webseiten oder sonstigen Medien veröffentlicht werden, muss unsere Webseite "http://www.mikrokopter.de" |
18 | // + Der Kunde trifft angemessene Vorkehrungen für den sicheren Einsatz der Software. Er wird die Software gründlich auf deren |
22 | // + eindeutig als Ursprung verlinkt werden |
19 | // + Verwendbarkeit zu dem von ihm beabsichtigten Zweck testen, bevor er diese operativ einsetzt. |
23 | // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
20 | // + Die Haftung des Lizenzgebers wird - soweit gesetzlich zulässig - begrenzt in Höhe des typischen und vorhersehbaren |
24 | // + Keine Gewähr auf Fehlerfreiheit, Vollständigkeit oder Funktion |
21 | // + Schadens. Die gesetzliche Haftung bei Personenschäden und nach dem Produkthaftungsgesetz bleibt unberührt. Dem Lizenzgeber steht jedoch der Einwand |
25 | // + Benutzung auf eigene Gefahr |
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. |
|
26 | // + Wir übernehmen keinerlei Haftung für direkte oder indirekte Personen- oder Sachschäden |
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 | // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
27 | // + und auf Grundlage der Datenschutzvorschriften erhebt, speichert, verarbeitet und, sofern notwendig, an Dritte übermittelt. |
28 | // + Die Portierung oder Nutzung der Software (oder Teile davon) auf andere Systeme (ausser der Hardware von www.mikrokopter.de) ist nur |
28 | // + *) Die räumliche Nutzung bezieht sich nur auf den Einsatzort, nicht auf die Reichweite der programmierten Software. |
29 | // + mit unserer Zustimmung zulässig |
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. |
|
30 | // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
31 | // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
31 | // + Die Funktion printf_P() unterliegt ihrer eigenen Lizenz und ist hiervon nicht betroffen |
32 | // + Software LICENSING TERMS |
32 | // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
33 | // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
33 | // + Redistributions of source code (with or without modifications) must retain the above copyright notice, |
34 | // + of HiSystems GmbH, Flachsmeerstrasse 2, 26802 Moormerland, Germany - the Licensor - |
34 | // + this list of conditions and the following disclaimer. |
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 |
35 | // + * Neither the name of the copyright holders nor the names of contributors may be used to endorse or promote products derived |
36 | // + (the Software) exclusively for private purposes. The License is unrestricted with respect to time and territory*. |
36 | // + from this software without specific prior written permission. |
37 | // + The Software may only be used with the Licensor's products. |
37 | // + * The use of this project (hardware, software, binary files, sources and documentation) is only permitted |
38 | // + The Software provided by the Licensor is protected by copyright. With respect to the relationship between the parties to this |
38 | // + for non-commercial use (directly or indirectly) |
- | |
39 | // + Commercial use (for excample: selling of MikroKopters, selling of PCBs, assembly, ...) is only permitted |
39 | // + agreement, all rights pertaining to the Software and other documents provided during the preparation and execution of this |
40 | // + with our written permission |
40 | // + agreement shall be the property of the Licensor. |
41 | // + * If sources or documentations are redistributet on other webpages, out webpage (http://www.MikroKopter.de) must be |
41 | // + The information contained in the Software copyright notices, trademarks, other legal reservations, serial numbers and other |
42 | // + clearly linked as origin |
- | |
43 | // + * porting the sources to other systems or using the software on other systems (except hardware from www.mikrokopter.de) is not allowed |
42 | // + features that can be used to identify the program may not be altered or defaced by the customer. |
44 | // |
- | |
45 | // + THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
43 | // + The customer shall be responsible for taking reasonable precautions |
46 | // + AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
44 | // + for the safe use of the Software. The customer shall test the Software thoroughly regarding its suitability for the |
47 | // + IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
45 | // + intended purpose before implementing it for actual operation. The Licensor's liability shall be limited to the extent of typical and |
48 | // + ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE |
46 | // + foreseeable damage to the extent permitted by law, notwithstanding statutory liability for bodily injury and product |
49 | // + LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
47 | // + liability. However, the Licensor shall be entitled to the defense of contributory negligence. |
50 | // + CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
48 | // + The customer will take adequate precautions in the case, that the software is not working properly. The customer will test |
51 | // + SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
49 | // + the software for his purpose before any operational usage. The customer will backup his data before using the software. |
52 | // + INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
50 | // + The customer understands that the Licensor collects, stores and processes, and, where required, forwards, customer data |
53 | // + CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
51 | // + to third parties to the extent necessary for executing the agreement, subject to applicable data protection and privacy regulations. |
54 | // + ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
52 | // + *) The territory aspect only refers to the place where the Software is used, not its programmed range. |
55 | // + POSSIBILITY OF SUCH DAMAGE. |
53 | // + #### END OF LICENSING TERMS #### |
- | 54 | // + Note: For information on license extensions (e.g. commercial use), please contact us at info(@)hisystems.de. |
|
56 | // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
55 | // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
57 | #include <stdio.h> |
56 | #include <stdio.h> |
58 | #include <string.h> |
57 | #include <string.h> |
59 | #include "91x_lib.h" |
58 | #include "91x_lib.h" |
60 | #include "mk3mag.h" |
59 | #include "mk3mag.h" |
61 | #include "i2c.h" |
60 | #include "i2c.h" |
62 | #include "timer1.h" |
61 | #include "timer1.h" |
63 | #include "led.h" |
62 | #include "led.h" |
64 | #include "main.h" |
63 | #include "main.h" |
65 | #include "uart1.h" |
64 | #include "uart1.h" |
66 | #include "compass.h" |
65 | #include "compass.h" |
67 | 66 | ||
68 | #define MK3MAG_SLAVE_ADDRESS 0x50 // i2C slave address |
67 | #define MK3MAG_SLAVE_ADDRESS 0x50 // i2C slave address |
69 | 68 | ||
70 | u8 MK3MAG_Present = 0; |
69 | u8 MK3MAG_Present = 0; |
71 | 70 | ||
72 | typedef struct |
71 | typedef struct |
73 | { |
72 | { |
74 | s16 Nick; |
73 | s16 Nick; |
75 | s16 Roll; |
74 | s16 Roll; |
76 | } __attribute__((packed)) MK3MAG_WriteAttitude_t; |
75 | } __attribute__((packed)) MK3MAG_WriteAttitude_t; |
77 | 76 | ||
78 | typedef struct |
77 | typedef struct |
79 | { |
78 | { |
80 | u8 Major; |
79 | u8 Major; |
81 | u8 Minor; |
80 | u8 Minor; |
82 | u8 Patch; |
81 | u8 Patch; |
83 | u8 Compatible; |
82 | u8 Compatible; |
84 | } __attribute__((packed)) MK3MAG_Version_t; |
83 | } __attribute__((packed)) MK3MAG_Version_t; |
85 | 84 | ||
86 | typedef struct |
85 | typedef struct |
87 | { |
86 | { |
88 | u8 CalByte; |
87 | u8 CalByte; |
89 | u8 Dummy1; |
88 | u8 Dummy1; |
90 | u8 Dummy2; |
89 | u8 Dummy2; |
91 | } __attribute__((packed)) MK3MAG_Cal_t; |
90 | } __attribute__((packed)) MK3MAG_Cal_t; |
92 | 91 | ||
93 | // Transfer buffers |
92 | // Transfer buffers |
94 | volatile MK3MAG_WriteAttitude_t MK3MAG_WriteAttitude; |
93 | volatile MK3MAG_WriteAttitude_t MK3MAG_WriteAttitude; |
95 | volatile MK3MAG_Version_t MK3MAG_Version; |
94 | volatile MK3MAG_Version_t MK3MAG_Version; |
96 | volatile MK3MAG_Cal_t MK3MAG_WriteCal; |
95 | volatile MK3MAG_Cal_t MK3MAG_WriteCal; |
97 | volatile MK3MAG_Cal_t MK3MAG_ReadCal; |
96 | volatile MK3MAG_Cal_t MK3MAG_ReadCal; |
98 | 97 | ||
99 | // ------------------------------------------------------------------------------- |
98 | // ------------------------------------------------------------------------------- |
100 | // the calculation of the MK3MAG packet checksum |
99 | // the calculation of the MK3MAG packet checksum |
101 | 100 | ||
102 | // calculate the crc of bytecount bytes in buffer |
101 | // calculate the crc of bytecount bytes in buffer |
103 | u8 MK3MAG_CalcCRC(u8* pBuffer, u8 bytecount) |
102 | u8 MK3MAG_CalcCRC(u8* pBuffer, u8 bytecount) |
104 | { |
103 | { |
105 | u8 i, crc = 0; |
104 | u8 i, crc = 0; |
106 | for(i=0; i < bytecount; i++) |
105 | for(i=0; i < bytecount; i++) |
107 | { |
106 | { |
108 | crc += pBuffer[i]; |
107 | crc += pBuffer[i]; |
109 | } |
108 | } |
110 | crc = ~crc; |
109 | crc = ~crc; |
111 | return crc; |
110 | return crc; |
112 | } |
111 | } |
113 | 112 | ||
114 | // assuming the last byte in buffer is the crc byte |
113 | // assuming the last byte in buffer is the crc byte |
115 | u8 MK3MAG_CheckCRC(u8* pBuffer, u8 BuffLen) |
114 | u8 MK3MAG_CheckCRC(u8* pBuffer, u8 BuffLen) |
116 | { |
115 | { |
117 | u8 crc = 0, retval = 0; |
116 | u8 crc = 0, retval = 0; |
118 | if(BuffLen == 0) return(retval); |
117 | if(BuffLen == 0) return(retval); |
119 | crc = MK3MAG_CalcCRC(pBuffer, BuffLen-1); |
118 | crc = MK3MAG_CalcCRC(pBuffer, BuffLen-1); |
120 | if(crc == pBuffer[BuffLen-1]) |
119 | if(crc == pBuffer[BuffLen-1]) |
121 | { |
120 | { |
122 | retval = 1; |
121 | retval = 1; |
123 | DebugOut.Analog[15]++; // count mk3mag ok |
122 | DebugOut.Analog[15]++; // count mk3mag ok |
124 | } |
123 | } |
125 | else |
124 | else |
126 | { |
125 | { |
127 | retval = 0; |
126 | retval = 0; |
128 | DebugOut.Analog[14]++; // count mk3mag crc error |
127 | DebugOut.Analog[14]++; // count mk3mag crc error |
129 | } |
128 | } |
130 | return(retval); |
129 | return(retval); |
131 | } |
130 | } |
132 | 131 | ||
133 | // ------------------------------------------------------------------------------- |
132 | // ------------------------------------------------------------------------------- |
134 | // the I2C rx data handler functions |
133 | // the I2C rx data handler functions |
135 | 134 | ||
136 | // rx data handler for version info request |
135 | // rx data handler for version info request |
137 | void MK3MAG_UpdateVersion(u8* pRxBuffer, u8 RxBufferSize) |
136 | void MK3MAG_UpdateVersion(u8* pRxBuffer, u8 RxBufferSize) |
138 | { // if crc is ok and number of byte are matching |
137 | { // if crc is ok and number of byte are matching |
139 | if(MK3MAG_CheckCRC(pRxBuffer, RxBufferSize) && (RxBufferSize == (sizeof(MK3MAG_Version)+1)) ) |
138 | if(MK3MAG_CheckCRC(pRxBuffer, RxBufferSize) && (RxBufferSize == (sizeof(MK3MAG_Version)+1)) ) |
140 | { |
139 | { |
141 | memcpy((u8 *)&MK3MAG_Version, pRxBuffer, sizeof(MK3MAG_Version)); |
140 | memcpy((u8 *)&MK3MAG_Version, pRxBuffer, sizeof(MK3MAG_Version)); |
142 | } |
141 | } |
143 | } |
142 | } |
144 | 143 | ||
145 | // rx data handler for calibration request |
144 | // rx data handler for calibration request |
146 | void MK3MAG_UpdateCalibration(u8* pRxBuffer, u8 RxBufferSize) |
145 | void MK3MAG_UpdateCalibration(u8* pRxBuffer, u8 RxBufferSize) |
147 | { // if crc is ok and number of byte are matching |
146 | { // if crc is ok and number of byte are matching |
148 | if(MK3MAG_CheckCRC(pRxBuffer, RxBufferSize) && (RxBufferSize == (sizeof(MK3MAG_ReadCal)+1)) ) |
147 | if(MK3MAG_CheckCRC(pRxBuffer, RxBufferSize) && (RxBufferSize == (sizeof(MK3MAG_ReadCal)+1)) ) |
149 | { |
148 | { |
150 | memcpy((u8 *)&MK3MAG_ReadCal, pRxBuffer, sizeof(MK3MAG_ReadCal)); |
149 | memcpy((u8 *)&MK3MAG_ReadCal, pRxBuffer, sizeof(MK3MAG_ReadCal)); |
151 | } |
150 | } |
152 | } |
151 | } |
153 | 152 | ||
154 | // rx data handler for magnetic vector request |
153 | // rx data handler for magnetic vector request |
155 | void MK3MAG_UpdateMagVector(u8* pRxBuffer, u8 RxBufferSize) |
154 | void MK3MAG_UpdateMagVector(u8* pRxBuffer, u8 RxBufferSize) |
156 | { // if crc is ok and number of byte are matching |
155 | { // if crc is ok and number of byte are matching |
157 | if(MK3MAG_CheckCRC(pRxBuffer, RxBufferSize) && (RxBufferSize == (sizeof(MagVector)+1)) ) |
156 | if(MK3MAG_CheckCRC(pRxBuffer, RxBufferSize) && (RxBufferSize == (sizeof(MagVector)+1)) ) |
158 | { |
157 | { |
159 | s16vec_t *pMagVector = (s16vec_t*)pRxBuffer; |
158 | s16vec_t *pMagVector = (s16vec_t*)pRxBuffer; |
160 | MagVector.X = pMagVector->Y; |
159 | MagVector.X = pMagVector->Y; |
161 | MagVector.Y = pMagVector->X; |
160 | MagVector.Y = pMagVector->X; |
162 | MagVector.Z = -pMagVector->Z; |
161 | MagVector.Z = -pMagVector->Z; |
163 | Compass_CalcHeading(); |
162 | Compass_CalcHeading(); |
164 | } |
163 | } |
165 | } |
164 | } |
166 | 165 | ||
167 | //---------------------------------------------------------------- |
166 | //---------------------------------------------------------------- |
168 | #define MK3MAG_CMD_VERSION 0x01 |
167 | #define MK3MAG_CMD_VERSION 0x01 |
169 | #define MK3MAG_CMD_READ_MAGVECT 0x02 |
168 | #define MK3MAG_CMD_READ_MAGVECT 0x02 |
170 | #define MK3MAG_CMD_WRITE_CAL 0x04 |
169 | #define MK3MAG_CMD_WRITE_CAL 0x04 |
171 | 170 | ||
172 | void MK3MAG_SendCommand(u8 command) |
171 | void MK3MAG_SendCommand(u8 command) |
173 | { |
172 | { |
174 | // try to catch the I2C buffer |
173 | // try to catch the I2C buffer |
175 | if(I2C_LockBuffer(0)) |
174 | if(I2C_LockBuffer(0)) |
176 | { |
175 | { |
177 | u16 TxBytes = 0; |
176 | u16 TxBytes = 0; |
178 | u16 RxBytes = 0; |
177 | u16 RxBytes = 0; |
179 | I2C_pRxHandler_t pRxHandlerFunc = NULL; |
178 | I2C_pRxHandler_t pRxHandlerFunc = NULL; |
180 | 179 | ||
181 | // update current command id |
180 | // update current command id |
182 | I2C_Buffer[TxBytes++] = command; |
181 | I2C_Buffer[TxBytes++] = command; |
183 | 182 | ||
184 | // set pointers to data area with respect to the command id |
183 | // set pointers to data area with respect to the command id |
185 | switch (command) |
184 | switch (command) |
186 | { |
185 | { |
187 | case MK3MAG_CMD_VERSION: |
186 | case MK3MAG_CMD_VERSION: |
188 | RxBytes = sizeof(MK3MAG_Version)+1; |
187 | RxBytes = sizeof(MK3MAG_Version)+1; |
189 | pRxHandlerFunc = &MK3MAG_UpdateVersion; |
188 | pRxHandlerFunc = &MK3MAG_UpdateVersion; |
190 | break; |
189 | break; |
191 | case MK3MAG_CMD_WRITE_CAL: |
190 | case MK3MAG_CMD_WRITE_CAL: |
192 | RxBytes = sizeof(MK3MAG_ReadCal)+1; |
191 | RxBytes = sizeof(MK3MAG_ReadCal)+1; |
193 | pRxHandlerFunc = &MK3MAG_UpdateCalibration; |
192 | pRxHandlerFunc = &MK3MAG_UpdateCalibration; |
194 | memcpy((u8*)I2C_Buffer+1, (u8*)&MK3MAG_WriteCal, sizeof(MK3MAG_WriteCal)); |
193 | memcpy((u8*)I2C_Buffer+1, (u8*)&MK3MAG_WriteCal, sizeof(MK3MAG_WriteCal)); |
195 | TxBytes += sizeof(MK3MAG_WriteCal); |
194 | TxBytes += sizeof(MK3MAG_WriteCal); |
196 | break; |
195 | break; |
197 | case MK3MAG_CMD_READ_MAGVECT: |
196 | case MK3MAG_CMD_READ_MAGVECT: |
198 | RxBytes = sizeof(MagVector)+1; |
197 | RxBytes = sizeof(MagVector)+1; |
199 | pRxHandlerFunc = &MK3MAG_UpdateMagVector; |
198 | pRxHandlerFunc = &MK3MAG_UpdateMagVector; |
200 | break; |
199 | break; |
201 | default: // unknown command id |
200 | default: // unknown command id |
202 | RxBytes = 0; |
201 | RxBytes = 0; |
203 | pRxHandlerFunc = NULL; |
202 | pRxHandlerFunc = NULL; |
204 | break; |
203 | break; |
205 | } |
204 | } |
206 | // update packet checksum |
205 | // update packet checksum |
207 | I2C_Buffer[TxBytes] = MK3MAG_CalcCRC((u8*)I2C_Buffer, TxBytes); |
206 | I2C_Buffer[TxBytes] = MK3MAG_CalcCRC((u8*)I2C_Buffer, TxBytes); |
208 | TxBytes++; |
207 | TxBytes++; |
209 | // initiate I2C transmission |
208 | // initiate I2C transmission |
210 | I2C_Transmission(MK3MAG_SLAVE_ADDRESS, TxBytes, pRxHandlerFunc, RxBytes); |
209 | I2C_Transmission(MK3MAG_SLAVE_ADDRESS, TxBytes, pRxHandlerFunc, RxBytes); |
211 | } // EOF I2C_State == I2C_IDLE |
210 | } // EOF I2C_State == I2C_IDLE |
212 | } |
211 | } |
213 | 212 | ||
214 | 213 | ||
215 | //---------------------------------------------------------------- |
214 | //---------------------------------------------------------------- |
216 | u8 MK3MAG_Init(void) |
215 | u8 MK3MAG_Init(void) |
217 | { |
216 | { |
218 | u8 msg[64]; |
217 | u8 msg[64]; |
219 | u8 repeat; |
218 | u8 repeat; |
220 | u32 timeout; |
219 | u32 timeout; |
221 | 220 | ||
222 | MK3MAG_Present = 0; |
221 | MK3MAG_Present = 0; |
223 | 222 | ||
224 | MK3MAG_Version.Major = 0xFF; |
223 | MK3MAG_Version.Major = 0xFF; |
225 | MK3MAG_Version.Minor = 0xFF; |
224 | MK3MAG_Version.Minor = 0xFF; |
226 | MK3MAG_Version.Patch = 0xFF; |
225 | MK3MAG_Version.Patch = 0xFF; |
227 | MK3MAG_Version.Compatible = 0xFF; |
226 | MK3MAG_Version.Compatible = 0xFF; |
228 | 227 | ||
229 | Compass_Heading = -1; |
228 | Compass_Heading = -1; |
230 | 229 | ||
231 | // polling of version info |
230 | // polling of version info |
232 | repeat = 0; |
231 | repeat = 0; |
233 | do |
232 | do |
234 | { |
233 | { |
235 | MK3MAG_SendCommand(MK3MAG_CMD_VERSION); |
234 | MK3MAG_SendCommand(MK3MAG_CMD_VERSION); |
236 | if(Version_HW > 11) timeout = SetDelay(100); |
235 | if(Version_HW > 11) timeout = SetDelay(100); |
237 | else timeout = SetDelay(250); |
236 | else timeout = SetDelay(250); |
238 | 237 | ||
239 | do |
238 | do |
240 | { |
239 | { |
241 | if (MK3MAG_Version.Major != 0xFF) break; // break loop on success |
240 | if (MK3MAG_Version.Major != 0xFF) break; // break loop on success |
242 | }while (!CheckDelay(timeout)); |
241 | }while (!CheckDelay(timeout)); |
243 | UART1_PutString("."); |
242 | UART1_PutString("."); |
244 | repeat++; |
243 | repeat++; |
245 | }while ((MK3MAG_Version.Major == 0xFF) && (repeat < 12)); // 12*250ms=3s |
244 | }while ((MK3MAG_Version.Major == 0xFF) && (repeat < 12)); // 12*250ms=3s |
246 | // if we got it |
245 | // if we got it |
247 | if (MK3MAG_Version.Major != 0xFF) |
246 | if (MK3MAG_Version.Major != 0xFF) |
248 | { |
247 | { |
249 | sprintf(msg, " MK3MAG V%d.%d%c", MK3MAG_Version.Major, MK3MAG_Version.Minor, 'a' + MK3MAG_Version.Patch); |
248 | sprintf(msg, " MK3MAG V%d.%d%c", MK3MAG_Version.Major, MK3MAG_Version.Minor, 'a' + MK3MAG_Version.Patch); |
250 | UART1_PutString(msg); |
249 | UART1_PutString(msg); |
251 | if(MK3MAG_Version.Compatible != MK3MAG_I2C_COMPATIBLE) |
250 | if(MK3MAG_Version.Compatible != MK3MAG_I2C_COMPATIBLE) |
252 | { |
251 | { |
253 | UART1_PutString("\n\r MK3MAG not compatible!"); |
252 | UART1_PutString("\n\r MK3MAG not compatible!"); |
254 | UART_VersionInfo.HardwareError[0] |= NC_ERROR0_COMPASS_INCOMPATIBLE; |
253 | UART_VersionInfo.HardwareError[0] |= NC_ERROR0_COMPASS_INCOMPATIBLE; |
255 | LED_RED_ON; |
254 | LED_RED_ON; |
256 | } |
255 | } |
257 | else |
256 | else |
258 | { // version ok |
257 | { // version ok |
259 | MK3MAG_Present = 1; |
258 | MK3MAG_Present = 1; |
260 | } |
259 | } |
261 | } |
260 | } |
262 | return(MK3MAG_Present); |
261 | return(MK3MAG_Present); |
263 | } |
262 | } |
264 | 263 | ||
265 | //---------------------------------------------------------------- |
264 | //---------------------------------------------------------------- |
266 | void MK3MAG_Update(void) |
265 | void MK3MAG_Update(void) |
267 | { |
266 | { |
268 | static u32 TimerUpdate = 0; |
267 | static u32 TimerUpdate = 0; |
269 | static s16 x_max,y_max,z_max,x_min,y_min,z_min; |
268 | static s16 x_max,y_max,z_max,x_min,y_min,z_min; |
270 | static u8 last_state; |
269 | static u8 last_state; |
271 | u8 msg[64]; |
270 | u8 msg[64]; |
272 | u16 MinCaclibration = 500; |
271 | u16 MinCaclibration = 500; |
273 | 272 | ||
274 | if( (I2C_State == I2C_STATE_OFF) || !MK3MAG_Present ) return; |
273 | if( (I2C_State == I2C_STATE_OFF) || !MK3MAG_Present ) return; |
275 | 274 | ||
276 | if(CheckDelay(TimerUpdate)) |
275 | if(CheckDelay(TimerUpdate)) |
277 | { |
276 | { |
278 | // check for incomming compass calibration request |
277 | // check for incomming compass calibration request |
279 | Compass_UpdateCalState(); |
278 | Compass_UpdateCalState(); |
280 | MK3MAG_WriteCal.CalByte = Compass_CalState; |
279 | MK3MAG_WriteCal.CalByte = Compass_CalState; |
281 | 280 | ||
282 | if(MK3MAG_ReadCal.CalByte != MK3MAG_WriteCal.CalByte) |
281 | if(MK3MAG_ReadCal.CalByte != MK3MAG_WriteCal.CalByte) |
283 | { // send new calibration state |
282 | { // send new calibration state |
284 | MK3MAG_SendCommand(MK3MAG_CMD_WRITE_CAL); |
283 | MK3MAG_SendCommand(MK3MAG_CMD_WRITE_CAL); |
285 | } |
284 | } |
286 | else |
285 | else |
287 | { // calibration state matches |
286 | { // calibration state matches |
288 | MK3MAG_SendCommand(MK3MAG_CMD_READ_MAGVECT); // initiate magvector transfer |
287 | MK3MAG_SendCommand(MK3MAG_CMD_READ_MAGVECT); // initiate magvector transfer |
289 | 288 | ||
290 | switch(Compass_CalState) |
289 | switch(Compass_CalState) |
291 | { |
290 | { |
292 | case 1: |
291 | case 1: |
293 | if(last_state != Compass_CalState) |
292 | if(last_state != Compass_CalState) |
294 | { |
293 | { |
295 | UART1_PutString("\r\nMK3Mag calibration\n\r"); |
294 | UART1_PutString("\r\nMK3Mag calibration\n\r"); |
296 | if(EarthMagneticStrengthTheoretic) |
295 | if(EarthMagneticStrengthTheoretic) |
297 | { |
296 | { |
298 | MinCaclibration = (MinCaclibration * EarthMagneticStrengthTheoretic) / 50; |
297 | MinCaclibration = (MinCaclibration * EarthMagneticStrengthTheoretic) / 50; |
299 | sprintf(msg, "Earth field on your location should be: %iuT\r\n",EarthMagneticStrengthTheoretic); |
298 | sprintf(msg, "Earth field on your location should be: %iuT\r\n",EarthMagneticStrengthTheoretic); |
300 | UART1_PutString(msg); |
299 | UART1_PutString(msg); |
301 | } |
300 | } |
302 | else UART1_PutString("without GPS\n\r"); |
301 | else UART1_PutString("without GPS\n\r"); |
303 | } |
302 | } |
304 | x_max = -30000; y_max = -30000; z_max = -30000; |
303 | x_max = -30000; y_max = -30000; z_max = -30000; |
305 | x_min = 30000; y_min = 30000; z_min = 30000; |
304 | x_min = 30000; y_min = 30000; z_min = 30000; |
306 | break; |
305 | break; |
307 | 306 | ||
308 | case 2: |
307 | case 2: |
309 | if(MagVector.X > x_max) { x_max = MagVector.X; BeepTime = 60; } |
308 | if(MagVector.X > x_max) { x_max = MagVector.X; BeepTime = 60; } |
310 | else if(MagVector.X < x_min) { x_min = MagVector.X; BeepTime = 20; } |
309 | else if(MagVector.X < x_min) { x_min = MagVector.X; BeepTime = 20; } |
311 | if(MagVector.Y > y_max) { y_max = MagVector.Y; BeepTime = 60; } |
310 | if(MagVector.Y > y_max) { y_max = MagVector.Y; BeepTime = 60; } |
312 | else if(MagVector.Y < y_min) { y_min = MagVector.Y; BeepTime = 20; } |
311 | else if(MagVector.Y < y_min) { y_min = MagVector.Y; BeepTime = 20; } |
313 | break; |
312 | break; |
314 | 313 | ||
315 | case 4: |
314 | case 4: |
316 | if(MagVector.Z > z_max) { z_max = MagVector.Z; BeepTime = 80; } |
315 | if(MagVector.Z > z_max) { z_max = MagVector.Z; BeepTime = 80; } |
317 | else if(MagVector.Z < z_min) { z_min = MagVector.Z; BeepTime = 80; } |
316 | else if(MagVector.Z < z_min) { z_min = MagVector.Z; BeepTime = 80; } |
318 | break; |
317 | break; |
319 | case 5: |
318 | case 5: |
320 | if(last_state == Compass_CalState) break; |
319 | if(last_state == Compass_CalState) break; |
321 | if(((x_max - x_min) > MinCaclibration) && ((y_max - y_min) > MinCaclibration) && ((z_max - z_min) > MinCaclibration)) |
320 | if(((x_max - x_min) > MinCaclibration) && ((y_max - y_min) > MinCaclibration) && ((z_max - z_min) > MinCaclibration)) |
322 | { |
321 | { |
323 | BeepTime = 2500; |
322 | BeepTime = 2500; |
324 | UART1_PutString("\r\n-> Calibration okay <-\n\r"); |
323 | UART1_PutString("\r\n-> Calibration okay <-\n\r"); |
325 | } |
324 | } |
326 | else |
325 | else |
327 | { |
326 | { |
328 | UART1_PutString("\r\nCalibration FAILED - Values too low: "); |
327 | UART1_PutString("\r\nCalibration FAILED - Values too low: "); |
329 | if((x_max - x_min) < MinCaclibration) UART1_PutString("X! "); |
328 | if((x_max - x_min) < MinCaclibration) UART1_PutString("X! "); |
330 | if((y_max - y_min) < MinCaclibration) UART1_PutString("Y! "); |
329 | if((y_max - y_min) < MinCaclibration) UART1_PutString("Y! "); |
331 | if((z_max - z_min) < MinCaclibration) UART1_PutString("Z! "); |
330 | if((z_max - z_min) < MinCaclibration) UART1_PutString("Z! "); |
332 | UART1_PutString("\r\n"); |
331 | UART1_PutString("\r\n"); |
333 | } |
332 | } |
334 | UART1_PutString(msg); |
333 | UART1_PutString(msg); |
335 | sprintf(msg, "\r\nX: (%i - %i = %i)\r\n",x_max,x_min,x_max - x_min); |
334 | sprintf(msg, "\r\nX: (%i - %i = %i)\r\n",x_max,x_min,x_max - x_min); |
336 | UART1_PutString(msg); |
335 | UART1_PutString(msg); |
337 | sprintf(msg, "Y: (%i - %i = %i)\r\n",y_max,y_min,y_max - y_min); |
336 | sprintf(msg, "Y: (%i - %i = %i)\r\n",y_max,y_min,y_max - y_min); |
338 | UART1_PutString(msg); |
337 | UART1_PutString(msg); |
339 | sprintf(msg, "Z: (%i - %i = %i)\r\n",z_max,z_min,z_max - z_min); |
338 | sprintf(msg, "Z: (%i - %i = %i)\r\n",z_max,z_min,z_max - z_min); |
340 | UART1_PutString(msg); |
339 | UART1_PutString(msg); |
341 | sprintf(msg, "(Minimum ampilitude is: %i)\r\n",MinCaclibration); |
340 | sprintf(msg, "(Minimum ampilitude is: %i)\r\n",MinCaclibration); |
342 | UART1_PutString(msg); |
341 | UART1_PutString(msg); |
343 | break; |
342 | break; |
344 | default: |
343 | default: |
345 | break; |
344 | break; |
346 | } |
345 | } |
347 | last_state = Compass_CalState; |
346 | last_state = Compass_CalState; |
348 | } |
347 | } |
349 | TimerUpdate = SetDelay(20); // every 20 ms are 50 Hz |
348 | TimerUpdate = SetDelay(20); // every 20 ms are 50 Hz |
350 | } |
349 | } |
351 | } |
350 | } |
352 | 351 |