Rev 288 | Rev 308 | Go to most recent revision | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed
Rev 288 | Rev 292 | ||
---|---|---|---|
Line 52... | Line 52... | ||
52 | // + INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
52 | // + INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
53 | // + CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
53 | // + CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
54 | // + ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
54 | // + ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
55 | // + POSSIBILITY OF SUCH DAMAGE. |
55 | // + POSSIBILITY OF SUCH DAMAGE. |
56 | // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
56 | // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
- | 57 | #include <stdio.h> |
|
57 | #include <string.h> |
58 | #include <string.h> |
58 | #include "91x_lib.h" |
59 | #include "91x_lib.h" |
59 | #include "mk3mag.h" |
60 | #include "mk3mag.h" |
60 | #include "i2c.h" |
61 | #include "i2c.h" |
61 | #include "timer1.h" |
62 | #include "timer1.h" |
62 | #include "led.h" |
63 | #include "led.h" |
63 | #include "spi_slave.h" |
- | |
64 | #include "main.h" |
64 | #include "main.h" |
65 | #include "uart1.h" |
65 | #include "uart1.h" |
66 | #include "compass.h" |
66 | #include "compass.h" |
Line 67... | Line 67... | ||
67 | 67 | ||
Line 73... | Line 73... | ||
73 | { |
73 | { |
74 | s16 Nick; |
74 | s16 Nick; |
75 | s16 Roll; |
75 | s16 Roll; |
76 | } __attribute__((packed)) MK3MAG_WriteAttitude_t; |
76 | } __attribute__((packed)) MK3MAG_WriteAttitude_t; |
Line -... | Line 77... | ||
- | 77 | ||
- | 78 | typedef struct |
|
- | 79 | { |
|
- | 80 | u8 Major; |
|
- | 81 | u8 Minor; |
|
- | 82 | u8 Patch; |
|
- | 83 | u8 Compatible; |
|
- | 84 | } __attribute__((packed)) MK3MAG_Version_t; |
|
- | 85 | ||
- | 86 | typedef struct |
|
- | 87 | { |
|
- | 88 | u8 CalByte; |
|
- | 89 | u8 Dummy1; |
|
- | 90 | u8 Dummy2; |
|
- | 91 | } __attribute__((packed)) MK3MAG_Cal_t; |
|
77 | 92 | ||
78 | // Transfer buffers |
93 | // Transfer buffers |
79 | volatile MK3MAG_WriteAttitude_t MK3MAG_WriteAttitude; |
94 | volatile MK3MAG_WriteAttitude_t MK3MAG_WriteAttitude; |
80 | volatile MK3MAG_Version_t MK3MAG_Version; |
95 | volatile MK3MAG_Version_t MK3MAG_Version; |
81 | volatile MK3MAG_Cal_t MK3MAG_WriteCal; |
96 | volatile MK3MAG_Cal_t MK3MAG_WriteCal; |
Line 139... | Line 154... | ||
139 | // rx data handler for magnetic vector request |
154 | // rx data handler for magnetic vector request |
140 | void MK3MAG_UpdateMagVector(u8* pRxBuffer, u8 RxBufferSize) |
155 | void MK3MAG_UpdateMagVector(u8* pRxBuffer, u8 RxBufferSize) |
141 | { // if crc is ok and number of byte are matching |
156 | { // if crc is ok and number of byte are matching |
142 | if(MK3MAG_CheckCRC(pRxBuffer, RxBufferSize) && (RxBufferSize == (sizeof(MagVector)+1)) ) |
157 | if(MK3MAG_CheckCRC(pRxBuffer, RxBufferSize) && (RxBufferSize == (sizeof(MagVector)+1)) ) |
143 | { |
158 | { |
144 | memcpy((u8 *)&MagVector, pRxBuffer, sizeof(MagVector)); |
159 | s16vec_t *pMagVector = (s16vec_t*)pRxBuffer; |
145 | } |
- | |
146 | } |
- | |
147 | - | ||
148 | // rx data handler for heading request |
160 | MagVector.X = pMagVector->Y; |
149 | void MK3MAG_UpdateHeading(u8* pRxBuffer, u8 RxBufferSize) |
161 | MagVector.Y = pMagVector->X; |
150 | { // if crc is ok and number of byte are matching |
162 | MagVector.Z = -pMagVector->Z; |
151 | if(MK3MAG_CheckCRC(pRxBuffer, RxBufferSize) && (RxBufferSize == (sizeof(Compass_Heading)+1)) ) |
- | |
152 | { |
- | |
153 | memcpy((u8 *)&Compass_Heading, pRxBuffer, sizeof(Compass_Heading)); |
163 | Compass_CalcHeading(); |
154 | } |
164 | } |
155 | } |
165 | } |
Line 156... | Line 166... | ||
156 | 166 | ||
- | 167 | //---------------------------------------------------------------- |
|
- | 168 | #define MK3MAG_CMD_VERSION 0x01 |
|
- | 169 | #define MK3MAG_CMD_READ_MAGVECT 0x02 |
|
- | 170 | #define MK3MAG_CMD_WRITE_CAL 0x04 |
|
157 | //---------------------------------------------------------------- |
171 | |
158 | void MK3MAG_SendCommand(u8 command) |
172 | void MK3MAG_SendCommand(u8 command) |
159 | { |
173 | { |
160 | // try to catch the I2C buffer |
174 | // try to catch the I2C buffer |
161 | if(I2C_LockBuffer(0)) |
175 | if(I2C_LockBuffer(0)) |
Line 182... | Line 196... | ||
182 | break; |
196 | break; |
183 | case MK3MAG_CMD_READ_MAGVECT: |
197 | case MK3MAG_CMD_READ_MAGVECT: |
184 | RxBytes = sizeof(MagVector)+1; |
198 | RxBytes = sizeof(MagVector)+1; |
185 | pRxHandlerFunc = &MK3MAG_UpdateMagVector; |
199 | pRxHandlerFunc = &MK3MAG_UpdateMagVector; |
186 | break; |
200 | break; |
187 | case MK3MAG_CMD_READ_HEADING: |
- | |
188 | RxBytes = sizeof(Compass_Heading)+1; |
- | |
189 | pRxHandlerFunc = &MK3MAG_UpdateHeading; |
- | |
190 | // update attitude from spi rx buffer |
- | |
191 | VIC_ITCmd(SSP0_ITLine, DISABLE); // avoid spi buffer update during copy |
- | |
192 | MK3MAG_WriteAttitude.Roll = FromFlightCtrl.AngleRoll; |
- | |
193 | MK3MAG_WriteAttitude.Nick = FromFlightCtrl.AngleNick; |
- | |
194 | VIC_ITCmd(SSP0_ITLine, ENABLE); |
- | |
195 | memcpy((u8*)I2C_Buffer+1, (u8*)&MK3MAG_WriteAttitude, sizeof(MK3MAG_WriteAttitude)); |
- | |
196 | TxBytes += sizeof(MK3MAG_WriteAttitude); |
- | |
197 | break; |
- | |
198 | default: // unknown command id |
201 | default: // unknown command id |
199 | RxBytes = 0; |
202 | RxBytes = 0; |
200 | pRxHandlerFunc = NULL; |
203 | pRxHandlerFunc = NULL; |
201 | break; |
204 | break; |
202 | } |
205 | } |
Line 256... | Line 259... | ||
256 | } |
259 | } |
257 | return(MK3MAG_Present); |
260 | return(MK3MAG_Present); |
258 | } |
261 | } |
Line 259... | Line 262... | ||
259 | 262 | ||
260 | //---------------------------------------------------------------- |
263 | //---------------------------------------------------------------- |
261 | void MK3MAG_UpdateCompass(void) |
264 | void MK3MAG_Update(void) |
262 | { |
265 | { |
263 | static u32 TimerCompassUpdate = 0; |
266 | static u32 TimerUpdate = 0; |
Line 264... | Line 267... | ||
264 | static s32 x_max,y_max,z_max,x_min,y_min,z_min; |
267 | static s16 x_max,y_max,z_max,x_min,y_min,z_min; |
Line 265... | Line 268... | ||
265 | 268 | ||
266 | if( (I2C_State == I2C_STATE_OFF) || !MK3MAG_Present ) return; |
269 | if( (I2C_State == I2C_STATE_OFF) || !MK3MAG_Present ) return; |
267 | 270 | ||
268 | if(CheckDelay(TimerCompassUpdate)) |
271 | if(CheckDelay(TimerUpdate)) |
269 | { |
272 | { |
270 | // check for incomming compass calibration request |
- | |
- | 273 | // check for incomming compass calibration request |
|
271 | Compass_UpdateCalState(); |
274 | Compass_UpdateCalState(); |
272 | MK3MAG_WriteCal.CalByte = Compass_CalState; |
275 | MK3MAG_WriteCal.CalByte = Compass_CalState; |
273 | // send new calstate |
276 | |
274 | if(MK3MAG_ReadCal.CalByte != MK3MAG_WriteCal.CalByte) |
- | |
275 | { |
- | |
276 | MK3MAG_SendCommand(MK3MAG_CMD_WRITE_CAL); |
- | |
277 | x_max = -30000; y_max = -30000; z_max = -30000; |
- | |
278 | x_min = 30000; y_min = 30000; z_min = 30000; |
- | |
279 | } |
- | |
280 | if(MK3MAG_WriteCal.CalByte == 2) |
- | |
281 | { |
- | |
282 | MK3MAG_SendCommand(MK3MAG_CMD_READ_MAGVECT); |
- | |
283 | if(MagVector.X > x_max) { x_max = MagVector.X; BeepTime = 30; }; |
- | |
284 | if(MagVector.Y > y_max) { y_max = MagVector.Y; BeepTime = 30; }; |
277 | if(MK3MAG_ReadCal.CalByte != MK3MAG_WriteCal.CalByte) |
- | 278 | { // send new calibration state |
|
- | 279 | MK3MAG_SendCommand(MK3MAG_CMD_WRITE_CAL); |
|
- | 280 | } |
|
- | 281 | else |
|
285 | if(MagVector.X < x_min) { x_min = MagVector.X; BeepTime = 30; }; |
282 | { // calibration state matches |
286 | if(MagVector.Y < y_min) { y_min = MagVector.Y; BeepTime = 30; }; |
283 | MK3MAG_SendCommand(MK3MAG_CMD_READ_MAGVECT); // initiate magvector transfer |
- | 284 | ||
287 | } |
285 | switch(Compass_CalState) |
- | 286 | { |
|
- | 287 | case 1: |
|
- | 288 | x_max = -30000; y_max = -30000; z_max = -30000; |
|
- | 289 | x_min = 30000; y_min = 30000; z_min = 30000; |
|
- | 290 | break; |
|
- | 291 | ||
- | 292 | case 2: |
|
- | 293 | if(MagVector.X > x_max) { x_max = MagVector.X; BeepTime = 60; } |
|
- | 294 | else if(MagVector.X < x_min) { x_min = MagVector.X; BeepTime = 20; } |
|
- | 295 | if(MagVector.Y > y_max) { y_max = MagVector.Y; BeepTime = 60; } |
|
- | 296 | else if(MagVector.Y < y_min) { y_min = MagVector.Y; BeepTime = 20; } |
|
288 | if(MK3MAG_WriteCal.CalByte == 4) |
297 | break; |
289 | { |
298 | |
- | 299 | case 4: |
|
290 | MK3MAG_SendCommand(MK3MAG_CMD_READ_MAGVECT); |
300 | if(MagVector.Z > z_max) { z_max = MagVector.Z; BeepTime = 80; } |
291 | if(MagVector.Z > z_max) { z_max = MagVector.Z; BeepTime = 30; }; |
301 | else if(MagVector.Z < z_min) { z_min = MagVector.Z; BeepTime = 80; } |
- | 302 | break; |
|
292 | if(MagVector.Z < z_min) { z_min = MagVector.Z; BeepTime = 30; }; |
303 | |
293 | } |
- | |
294 | else // request current heading |
304 | default: |
295 | { |
305 | break; |
296 | MK3MAG_SendCommand(MK3MAG_CMD_READ_HEADING); |
306 | } |
297 | } |
307 | } |