ISSDK  1.7
IoT Sensing Software Development Kit
orientaion_application_baremetal_agmp03.c
Go to the documentation of this file.
1 /*
2  * The Clear BSD License
3  * Copyright (c) 2016, Freescale Semiconductor, Inc.
4  * Copyright 2016-2017 NXP
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without modification,
8  * are permitted (subject to the limitations in the disclaimer below) provided
9  * that the following conditions are met:
10  *
11  * o Redistributions of source code must retain the above copyright notice, this list
12  * of conditions and the following disclaimer.
13  *
14  * o Redistributions in binary form must reproduce the above copyright notice, this
15  * list of conditions and the following disclaimer in the documentation and/or
16  * other materials provided with the distribution.
17  *
18  * o Neither the name of the copyright holder nor the names of its
19  * contributors may be used to endorse or promote products derived from this
20  * software without specific prior written permission.
21  *
22  * NO EXPRESS OR IMPLIED LICENSES TO ANY PARTY'S PATENT RIGHTS ARE GRANTED BY THIS LICENSE.
23  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
24  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
25  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
26  * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
27  * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
28  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
29  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
30  * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
31  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
32  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33  */
34 
35 /**
36  * @file orientaion_application_baremetal_agmp03.c
37  * @brief The orientaion_application_baremetal_agmp03.c file implements the ISSDK
38  * baremetal orientation application for FRDM-STBC-AGMP03 using sensor
39  * fusion core functional interfaces and host i/o interface.
40  */
41 /**
42  * Orient Packet Structure: 44 Bytes
43  * ------------------------------------------------------------------------------------------------------------------------------------------------------------
44  * | TimeStamp | 9 Axis Sensor data | Quaternion data | Euler angles data | FitError% data | Co-ordinates data | BoardInfo data | BuildInfo data | SysTick data | |
45  * ------------------------------------------------------------------------------------------------------------------------------------------------------------
46  * | 4 Bytes | 18 Bytes | 8 Bytes | 6 Bytes | 2 Bytes | 1 Bytes | 1 Byte | 2 Bytes | 2 Bytes | |
47  * ------------------------------------------------------------------------------------------------------------------------------------------------------------
48  */
49 
50 /* KSDK Headers */
51 #include "board.h"
52 #include "pin_mux.h"
53 #include "clock_config.h"
54 #include "fsl_port.h"
55 #include "fsl_i2c.h"
56 #include "fsl_pit.h"
57 
58 /* CMSIS Headers */
59 #include "Driver_USART.h"
60 #include "fsl_i2c_cmsis.h"
61 #include "fsl_uart_cmsis.h"
62 /* ISSDK Headers */
63 #include "register_io_i2c.h"
64 #include "host_io_uart.h"
65 #include "gpio_driver.h"
66 #include "auto_detection_service.h"
67 #include "systick_utils.h"
68 
69 /* Sensor Fusion Headers */
70 #include "sensor_fusion.h"
71 #include "control.h"
72 #include "status.h"
73 #include "drivers.h"
74 #include "driver_pit.h"
75 
76 /*******************************************************************************
77  * Macro Definitions
78  ******************************************************************************/
79 #define TIMESTAMP_DATA_SIZE (4) /* Orientation Packet: TimeStamp field size */
80 #define NINEAXISSENSOR_DATA_SIZE (18) /* Orientation Packet: 9 Axis Sensor Data, Accel, Mag, Gyro field size */
81 #define QUATERNION_SIZE (8) /* Orientation Packet: Quaternion field size */
82 #define EULERANGLE_DATA_SIZE (6) /* Orientation Packet: Roll,Pitch,Compass Euler angles packet field size */
83 #define FITERROR_DATA_SIZE (2) /* Orientation Packet: FitError% field size */
84 #define COORDINATES_SIZE (1) /* Orientation Packet: coordinates field size */
85 #define BOARDINFO_SIZE (1) /* Orientation Packet: BoardInfo field size */
86 #define BUILDNAME_SIZE (2) /* Orientation Packet: BuildInfo field size */
87 #define SYSTICKINFO_SIZE (2) /* Orientation Packet: SysTick field size */
88 /*! @brief Unique Name for this application which should match the target GUI pkg name. */
89 #define APPLICATION_NAME "9 Axis Orientation Sensor Demo" /* Orientation Application Name */
90 /*! @brief Version to distinguish between instances the same application based on target Shield and updates. */
91 #define APPLICATION_VERSION "2.5"
92 
93 /*! Orientation Streaming Packet Payload Size */
94 #define STREAMING_PAYLOAD_LEN \
95  (TIMESTAMP_DATA_SIZE + NINEAXISSENSOR_DATA_SIZE + QUATERNION_SIZE + EULERANGLE_DATA_SIZE + FITERROR_DATA_SIZE + COORDINATES_SIZE + BOARDINFO_SIZE + BUILDNAME_SIZE + SYSTICKINFO_SIZE)
96 
97 /*******************************************************************************
98  * Global Variables
99  ******************************************************************************/
100 SensorFusionGlobals sfg; /* Sensor Fusion Global Structure Instance */
101 ControlSubsystem gOrientationControlSubsystem; /* Control Subsystem Structure Instance for orientation streaming */
102 StatusSubsystem statusSubsystem; /* Sensor Fusion algorithm execution status indicators (LED) */
103 volatile bool gStreamingEnabled; /* Variable indicating streaming mode. */
104 uint8_t gPrimaryStreamID; /* The auto assigned Stream ID to be used to stream complete data. */
108 
110 struct PhysicalSensor sensors[4]; ///< This implementation uses up to 4 sensors
111 
114  .functionParam = NULL,
115  .idleFunction = NULL
116 };
117 
118 /*******************************************************************************
119  * Local Function Prototype Definitions
120  ******************************************************************************/
121 /* Utility Function to add field bytes to orientation buffer */
122 static void updateOrientBuf(uint8_t *pDest,
123  uint16_t *pIndex,
124  uint8_t *pSrc,
125  uint16_t iBytesToCopy);
126 /* Initialize the orientation application control subsystem */
127 static int8_t initOrientaionAppControlPort (ControlSubsystem *pComm);
128 /* Function to create orient packets for serial streaming over Host I/O */
129 static void encodeOrientPacketStream (SensorFusionGlobals * sfg, uint8_t *sUARTOutputBuffer);
130 /* Utility function for reading common algorithm parameters */
132  Quaternion *fq,
133  int16_t *iPhi,
134  int16_t *iThe,
135  int16_t *iRho,
136  int16_t iOmega[],
137  uint16_t *isystick);
138 
139 /*******************************************************************************
140  * Code
141  ******************************************************************************/
142 /* Handler for Device Info and Streaming Control Commands. */
144  uint8_t tag, uint8_t *hostCommand, uint8_t *hostResponse, size_t *hostMsgSize, size_t respBufferSize)
145 {
146  bool success = false;
147 
148  /* If it is Host requesting Device Info, send Board Name and Shield Name. */
149  if (tag == HOST_PRO_INT_DEV_TAG)
150  { /* Byte 1 : Payload - Length of APPLICATION_NAME (b)
151  * Bytes=b : Payload Application Name
152  * Byte b+1 : Payload - Length of BOARD_NAME (s)
153  * Bytes=s : Payload Board Name
154  * Byte b+s+2 : Payload - Length of SHIELD_NAME (v)
155  * Bytes=v : Payload Shield Name */
156 
157  size_t appNameLen = strlen(embAppName);
158  size_t boardNameLen = strlen(boardString);
159  size_t shieldNameLen = strlen(shieldString);
160 
161  if (respBufferSize >= boardNameLen + shieldNameLen + appNameLen + 3)
162  { /* We have sufficient buffer. */
163  *hostMsgSize = 0;
164  }
165  else
166  {
167  return false;
168  }
169 
170  hostResponse[*hostMsgSize] = appNameLen;
171  *hostMsgSize += 1;
172 
173  memcpy(hostResponse + *hostMsgSize, embAppName, appNameLen);
174  *hostMsgSize += appNameLen;
175 
176  hostResponse[*hostMsgSize] = boardNameLen;
177  *hostMsgSize += 1;
178 
179  memcpy(hostResponse + *hostMsgSize, boardString, boardNameLen);
180  *hostMsgSize += boardNameLen;
181 
182  hostResponse[*hostMsgSize] = shieldNameLen;
183  *hostMsgSize += 1;
184 
185  memcpy(hostResponse + *hostMsgSize, shieldString, shieldNameLen);
186  *hostMsgSize += shieldNameLen;
187 
188  return true;
189  }
190 
191  /* If it is Host sending Streaming Commands, take necessary actions. */
192  if ((tag == (HOST_PRO_INT_CMD_TAG | HOST_PRO_CMD_W_CFG_TAG)) &&
194  { /* Byte 1 : Payload - Operation Code (Start/Stop Operation Code)
195  * Byte 2 : Payload - Stream ID (Target Stream for carrying out operation) */
196  switch (hostCommand[0]) /* Execute desired operation (Start/Stop) on the requested Stream. */
197  {
198  case HOST_CMD_START:
199  if (hostCommand[1] == gPrimaryStreamID && gStreamingEnabled == false)
200  {
201  gStreamingEnabled = true;
202  success = true;
203  }
204  break;
205  case HOST_CMD_STOP:
206  if (hostCommand[1] == gPrimaryStreamID && gStreamingEnabled == true)
207  {
208  gStreamingEnabled = false;
209  success = true;
210  LED_GREEN_ON();
211  }
212  break;
213  default:
214  break;
215  }
216  *hostMsgSize = 0; /* Zero payload in response. */
217  }
218 
219  return success;
220 }
221 
222 /*!
223  * @brief updateOrientBuf utility function
224  */
225 void updateOrientBuf(uint8_t *pDest, uint16_t *pIndex, uint8_t *pSrc, uint16_t iBytesToCopy)
226 {
227  uint16_t counter;
228 
229  for (counter = 0; counter < iBytesToCopy; counter++)
230  {
231  pDest[(*pIndex)++] = pSrc[counter];
232  }
233 
234  return;
235 }
236 
237 /*!
238  * @brief appendZerosOrientBuf utility function
239  */
240 void appendZerosOrientBuf(uint8_t *pDest, uint16_t *pIndex, uint16_t numZeros) {
241  int16_t scratch16 = 0;
242  uint16_t i;
243  for (i=0; i<numZeros; i++) {
244  sBufAppendItem(pDest, pIndex, (uint8_t *) &scratch16, 2);
245  }
246 }
247 
248 /*!
249  * @brief Utility function for reading common algorithm parameters
250  */
252  SV_ptr data, Quaternion *fq, int16_t *iPhi, int16_t *iThe, int16_t *iRho, int16_t iOmega[], uint16_t *isystick)
253 {
254  *fq = data->fq;
255  iOmega[CHX] = (int16_t)(data->fOmega[CHX] * 20.0F);
256  iOmega[CHY] = (int16_t)(data->fOmega[CHY] * 20.0F);
257  iOmega[CHZ] = (int16_t)(data->fOmega[CHZ] * 20.0F);
258  *iPhi = (int16_t)(10.0F * data->fPhi);
259  *iThe = (int16_t)(10.0F * data->fThe);
260  *iRho = (int16_t)(10.0F * data->fRho);
261  *isystick = (uint16_t)(data->systick / 20);
262 }
263 
264 /*!
265  * @brief Initialize the orientation application control subsystem
266  */
267 static int8_t initOrientaionAppControlPort(ControlSubsystem *pComm /* pointer to the control subystem structure */
268  )
269 {
270  if (pComm)
271  {
272  pComm->DefaultQuaternionPacketType = Q3; /* default to simplest algorithm */
273  pComm->QuaternionPacketType = Q3; /* default to simplest algorithm */
274  pComm->AngularVelocityPacketOn = true; /* transmit angular velocity packet */
275  pComm->DebugPacketOn = true; /* transmit debug packet */
276  pComm->RPCPacketOn = true; /* transmit roll, pitch, compass packet */
277  pComm->AltPacketOn = true; /* Altitude packet */
278  pComm->AccelCalPacketOn = 0;
279  pComm->write = NULL;
280  pComm->stream = encodeOrientPacketStream; /* Function to create orient packets for serial stream */
281  memset(orientOutputBuffer, 0, sizeof(orientOutputBuffer));
282  return (0);
283  }
284  else
285  {
286  return (1);
287  }
288 }
289 
290 /*!
291  * @brief Function to create orient packets for serial streaming over Host I/O
292  */
293 void encodeOrientPacketStream(SensorFusionGlobals *sfg, uint8_t *sUARTOutputBuffer)
294 {
295  Quaternion fq; /* quaternion to be transmitted */
296  static uint32_t iTimeStamp = 0; /* 1MHz time stamp */
297  uint16_t iIndex; /* output buffer counter */
298  int32_t scratch32; /* scratch int32_t */
299  int16_t scratch16; /* scratch int16_t */
300  int16_t iPhi, iThe, iRho; /* integer angles to be transmitted */
301  int16_t iOmega[3]; /* scaled angular velocity vector */
302  uint16_t isystick; /* algorithm systick time */
303  uint8_t tmpuint8_t; /* scratch uint8_t */
304  uint8_t flags; /* byte of flags */
305  size_t sUARTOutputBufLen = STREAMING_PAYLOAD_LEN;
306 
307  /* Update the 1 microsecond timestamp counter */
308  iTimeStamp += 1000000 / FUSION_HZ;
309 
310  /* Start the index to store orientation packet payload into the transmit buffer */
311  iIndex = STREAMING_HEADER_LEN;
312 
313  /* Add Time Stamp to Orientation Buffer */
314  /*! Add 1 microsecond timestamp counter (4 bytes) value to Orientation Buffer */
315  updateOrientBuf(sUARTOutputBuffer, &iIndex, (uint8_t *)&iTimeStamp, 4);
316 
317  /* Add Accel X, Y Z values to Orientation Buffer */
318  /*! Add Accel X, Y Z (6 bytes) value to Orientation Buffer. Send non-zero data only if the accelerometer sensor is enabled */
319  if (sfg->iFlags & F_USING_ACCEL)
320  {
321  /* Accelerometer data is used for the selected quaternion so transmit but clip at 4g */
322  scratch32 = (sfg->Accel.iGc[CHX] * 8192) / sfg->Accel.iCountsPerg;
323  if (scratch32 > 32767) scratch32 = 32767;
324  if (scratch32 < -32768) scratch32 = -32768;
325  scratch16 = (int16_t) (scratch32);
326  updateOrientBuf(sUARTOutputBuffer, &iIndex, (uint8_t *) &scratch16, 2);
327 
328  scratch32 = (sfg->Accel.iGc[CHY] * 8192) / sfg->Accel.iCountsPerg;
329  if (scratch32 > 32767) scratch32 = 32767;
330  if (scratch32 < -32768) scratch32 = -32768;
331  scratch16 = (int16_t) (scratch32);
332  updateOrientBuf(sUARTOutputBuffer, &iIndex, (uint8_t *) &scratch16, 2);
333 
334  scratch32 = (sfg->Accel.iGc[CHZ] * 8192) / sfg->Accel.iCountsPerg;
335  if (scratch32 > 32767) scratch32 = 32767;
336  if (scratch32 < -32768) scratch32 = -32768;
337  scratch16 = (int16_t) (scratch32);
338  updateOrientBuf(sUARTOutputBuffer, &iIndex, (uint8_t *) &scratch16, 2);
339  }
340  else
341  {
342  /* Accelerometer structure is not defined so transmit zero */
343  appendZerosOrientBuf(sUARTOutputBuffer, &iIndex, 3);
344  }
345 
346  /* Add MAG X, Y Z values to Orientation Buffer */
347  /*! Add MAG X, Y Z (6 bytes) value to Orientation Buffer. Send non-zero data only if the magnetometer sensor is enabled */
348  if (sfg->iFlags & F_USING_MAG)
349  {
350  /* Magnetometer data is used for the selected quaternion so transmit */
351  scratch16 = (int16_t) (sfg->Mag.iBc[CHX] * 10) / (sfg->Mag.iCountsPeruT);
352  updateOrientBuf(sUARTOutputBuffer, &iIndex, (uint8_t *) &scratch16, 2);
353  scratch16 = (int16_t) ((sfg->Mag.iBc[CHY] * 10) / sfg->Mag.iCountsPeruT);
354  updateOrientBuf(sUARTOutputBuffer, &iIndex, (uint8_t *) &scratch16, 2);
355  scratch16 = (int16_t) ((sfg->Mag.iBc[CHZ] * 10) / sfg->Mag.iCountsPeruT);
356  updateOrientBuf(sUARTOutputBuffer, &iIndex, (uint8_t *) &scratch16, 2);
357  }
358  else
359  {
360  /* Magnetometer structure is not defined so transmit zero */
361  appendZerosOrientBuf(sUARTOutputBuffer, &iIndex, 3);
362  }
363 
364  /* Add Gyro X, Y Z values to Orientation Buffer */
365  /*! Add Gyro X, Y Z (6 bytes) value to Orientation Buffer. Send non-zero data only if the gyro sensor is enabled */
366  if (sfg->iFlags & F_USING_GYRO)
367  {
368  /* Gyro data is used for the selected quaternion so transmit */
369  scratch16 = (int16_t) ((sfg->Gyro.iYs[CHX] * 20) / sfg->Gyro.iCountsPerDegPerSec);
370  updateOrientBuf(sUARTOutputBuffer, &iIndex, (uint8_t *) &scratch16, 2);
371  scratch16 = (int16_t) ((sfg->Gyro.iYs[CHY] * 20) / sfg->Gyro.iCountsPerDegPerSec);
372  updateOrientBuf(sUARTOutputBuffer, &iIndex, (uint8_t *) &scratch16, 2);
373  scratch16 = (int16_t) ((sfg->Gyro.iYs[CHZ] * 20) / sfg->Gyro.iCountsPerDegPerSec);
374  updateOrientBuf(sUARTOutputBuffer, &iIndex, (uint8_t *) &scratch16, 2);
375  }
376  else
377  {
378  /* Gyro structure is not defined so transmit zero */
379  appendZerosOrientBuf(sUARTOutputBuffer, &iIndex, 3);
380  }
381 
382  /* Initialize default quaternion, flags byte, angular velocity and orientation */
383  fq.q0 = 1.0F;
384  fq.q1 = fq.q2 = fq.q3 = 0.0F;
385  flags = 0x00;
386  iOmega[CHX] = iOmega[CHY] = iOmega[CHZ] = 0;
387  iPhi = iThe = iRho = 0;
388  isystick = 0;
389 
390  /* set the quaternion, flags, angular velocity and Euler angles */
391  if (sfg->iFlags & F_9DOF_GBY_KALMAN)
392  {
393  flags |= 0x08;
394  readAlgoParams((SV_ptr)&sfg->SV_9DOF_GBY_KALMAN, &fq, &iPhi, &iThe, &iRho, iOmega, &isystick);
395  }
396 
397  /* Add Quaternion data to Orientation Buffer */
398  /*! Scale the quaternion and add to the Orientation Buffer */
399  scratch16 = (int16_t) (fq.q0 * 30000.0F);
400  updateOrientBuf(sUARTOutputBuffer, &iIndex, (uint8_t *) &scratch16, 2);
401  scratch16 = (int16_t) (fq.q1 * 30000.0F);
402  updateOrientBuf(sUARTOutputBuffer, &iIndex, (uint8_t *) &scratch16, 2);
403  scratch16 = (int16_t) (fq.q2 * 30000.0F);
404  updateOrientBuf(sUARTOutputBuffer, &iIndex, (uint8_t *) &scratch16, 2);
405  scratch16 = (int16_t) (fq.q3 * 30000.0F);
406  updateOrientBuf(sUARTOutputBuffer, &iIndex, (uint8_t *) &scratch16, 2);
407 
408  /* Add Euler angles data to Orientation Buffer */
409  /*! Add the Roll, Pitch, Compass Euler angles packet to the Orientation Buffer */
410  updateOrientBuf(sUARTOutputBuffer, &iIndex, (uint8_t *) &iPhi, 2);
411  updateOrientBuf(sUARTOutputBuffer, &iIndex, (uint8_t *) &iThe, 2);
412  updateOrientBuf(sUARTOutputBuffer, &iIndex, (uint8_t *) &iRho, 2);
413 
414  if (sfg->iFlags & F_USING_MAG)
415  {
416  /* Add Fit error values to Orientation Buffer */
417  if (sfg->MagCal.fFitErrorpc > 327.67F)
418  scratch16 = 32767;
419  else
420  scratch16 = (int16_t) (sfg->MagCal.fFitErrorpc * 100.0F);
421  updateOrientBuf(sUARTOutputBuffer, &iIndex, (uint8_t *) &scratch16, 2);
422  }
423 
424  /* Set the coordinate system bits in flags from default NED (00) */
425 #if THISCOORDSYSTEM == ANDROID
426  /* Set the Android flag bits */
427  flags |= 0x10;
428 #elif THISCOORDSYSTEM == WIN8
429  /* set the Win8 flag bits */
430  flags |= 0x20;
431 #endif // THISCOORDSYSTEM
432 
433  /*! Add the co-ordinate info byte to the Orientation Buffer */
434  updateOrientBuf(sUARTOutputBuffer, &iIndex, &flags, 1);
435 
436  /*! Add the shield (bits 7-5) and Kinetis (bits 4-0) byte to the Orientation Buffer */
437  tmpuint8_t = ((THIS_SHIELD & 0x07) << 5) | (THIS_BOARD & 0x1F);
438  updateOrientBuf(sUARTOutputBuffer, &iIndex, &tmpuint8_t, 1);
439 
440  /*! Add Software version number to the Orientation Buffer */
441  scratch16 = THISBUILD;
442  updateOrientBuf(sUARTOutputBuffer, &iIndex, (uint8_t *) &scratch16, 2);
443 
444  /*! Add systick count to the Orientation Buffer */
445  updateOrientBuf(sUARTOutputBuffer, &iIndex, (uint8_t *) &isystick, 2);
446 
447  /*! Send packet to host only if streaming has been enabled by Host. */
448 // if (gStreamingEnabled)
449  {
450  /*! Add Host I/O ISO streaming header */
451  Host_IO_Add_ISO_Header(gPrimaryStreamID, sUARTOutputBuffer, sUARTOutputBufLen);
452 
453  /*! Send Orientation streaming packet to Host using Host I/O */
454  Host_IO_Send(sUARTOutputBuffer, STREAMING_HEADER_LEN + sUARTOutputBufLen, HOST_FORMAT_HDLC);
455  }
456  return;
457 }
458 
459 /*!
460  * @brief Main function
461  */
462 int main(void)
463 {
464  int32_t status;
465  uint16_t i = 0;
466  uint8_t secondaryStreamID1,secondaryStreamID2,secondaryStreamID3; /* The auto assigned Stream ID not to be used to stream data. */
467  ARM_DRIVER_I2C* I2Cdrv = &I2C_S_DRIVER;
468  ARM_DRIVER_USART *pUartDriver = &HOST_S_DRIVER;
469 
470  /*! Initialize the MCU hardware. */
473 
474  /* Create the Short Application Name String for ADS. */
475  sprintf(embAppName, "%s:%s", APPLICATION_NAME, APPLICATION_VERSION);
476 
477  /*! Run ADS. */
479 
480  /* Create the Full Application Name String for Device Info Response. */
482 
483  /*! Initialize and set the KSDK driver for the I2C port */
484  I2Cdrv->Initialize(I2C_S_SIGNAL_EVENT);
485  I2Cdrv->PowerControl(ARM_POWER_FULL);
486  I2Cdrv->Control(ARM_I2C_BUS_SPEED, ARM_I2C_BUS_SPEED_FAST);
487 
488  /*! Initialize the UART driver. */
489  status = pUartDriver->Initialize(HOST_S_SIGNAL_EVENT);
490  if (ARM_DRIVER_OK != status)
491  {
492  return -1;
493  }
494  /*! Set UART Power mode. */
495  status = pUartDriver->PowerControl(ARM_POWER_FULL);
496  if (ARM_DRIVER_OK != status)
497  {
498  return -1;
499  }
500  /*! Set UART Baud Rate. */
501  status = pUartDriver->Control(ARM_USART_MODE_ASYNCHRONOUS, BOARD_DEBUG_UART_BAUDRATE);
502  if (ARM_DRIVER_OK != status)
503  {
504  return -1;
505  }
506 
507  /*! Initialize control sub-system for orientation packet streaming */
508  initOrientaionAppControlPort(&gOrientationControlSubsystem);
509 
510  /*! Initialize sensor fusion status sub-system */
511  initializeStatusSubsystem(&statusSubsystem);
512 
513  /*! Initialize sensor fusion global metadata */
514  initSensorFusionGlobals(&sfg, &statusSubsystem, &gOrientationControlSubsystem);
515 
516  /*! Install the sensors to be used by sensor fusion */
517  sfg.installSensor(&sfg, &sensors[0], FXAS21002_I2C_ADDR, 1, (void*) I2Cdrv, &i2cBusInfo, FXAS21002_Init, FXAS21002_Read);
518  sfg.installSensor(&sfg, &sensors[1], MAG3110_I2C_ADDR, 1, (void*) I2Cdrv, &i2cBusInfo, MAG3110_Init, MAG3110_Read);
519  sfg.installSensor(&sfg, &sensors[2], MPL3115_I2C_ADDR, 1, (void*) I2Cdrv, &i2cBusInfo, MPL3115_Init, MPL3115_Read);
520  sfg.installSensor(&sfg, &sensors[3], FXLS8962_I2C_ADDR, 1, (void*) I2Cdrv, &i2cBusInfo, FXLS8962_Init, FXLS8962_Read);
521 
522  /*! Initialize streaming and assign Stream IDs. */
523  gStreamingEnabled = false;
524 
525  /* Registering the FXAS21002 slave address with Host I/O for Register Read/Write. */
527  Host_IO_Init(pUartDriver, (void *)sensors[0].bus_driver, &sensors[0].deviceInfo, NULL, FXAS21002_I2C_ADDR);
528  /* Confirm if a valid Stream ID has been allocated for this stream. */
529  if (0 == gPrimaryStreamID)
530  {
531  return -1;
532  }
533 
534  /* Registering the MAG3110 slave address with Host I/O for Register Read/Write. */
535  secondaryStreamID1 =
536  Host_IO_Init(pUartDriver, (void *)sensors[1].bus_driver, &sensors[1].deviceInfo, NULL, MAG3110_I2C_ADDR);
537  /* Confirm if a valid Stream ID has been allocated for this stream. */
538  if (0 == secondaryStreamID1)
539  {
540  return -1;
541  }
542 
543  /* Registering the MPL3115 slave address with Host I/O for Register Read/Write. */
544  secondaryStreamID2 =
545  Host_IO_Init(pUartDriver, (void *)sensors[2].bus_driver, &sensors[2].deviceInfo, NULL, MPL3115_I2C_ADDR);
546  /* Confirm if a valid Stream ID has been allocated for this stream. */
547  if (0 == secondaryStreamID2)
548  {
549  return -1;
550  }
551 
552  /* Registering the FXLS8962 slave address with Host I/O for Register Read/Write. */
553  secondaryStreamID3 =
554  Host_IO_Init(pUartDriver, (void *)sensors[3].bus_driver, &sensors[2].deviceInfo, NULL, FXLS8962_I2C_ADDR);
555  /* Confirm if a valid Stream ID has been allocated for this stream. */
556  if (0 == secondaryStreamID3)
557  {
558  return -1;
559  }
560 
561  /*! Initialize sensor fusion engine */
562  sfg.initializeFusionEngine(&sfg); /* This will initialize sensors and magnetic calibration */
563 
564  /*! pitIsrFlag will be set true at FUSION_HZ periodic intervals */
565  pit_init(1000000/FUSION_HZ);
566 
567  sfg.setStatus(&sfg, NORMAL);
568  while (true)
569  {
570  /*! Check whether occur interupt */
571  if (true == pitIsrFlag)
572  {
573  /*! Check for incoming commands form Host. */
575 
576  if (false == gStreamingEnabled)
577  {
578  SMC_SetPowerModeWait(SMC); /* Power save, wait if nothing to do. */
579  continue;
580  }
581 
582  /*! Reads sensors, applies HAL and does averaging (if applicable) */
583  sfg.readSensors(&sfg, 1);
584 
585  /*! Condition sensor data, magCal is run as part of this */
586  sfg.conditionSensorReadings(&sfg);
587 
588  /*! Run fusion algorithms */
589  sfg.runFusion(&sfg);
590 
591  /*! Apply debug perturbation */
592  sfg.applyPerturbation(&sfg);
593 
594  /*! Required to check incoming host command and send response before host times out */
596 
597  sfg.loopcounter++;
598  i = i + 1;
599  if (i >= 4)
600  { /* Some status codes include a "blink" feature. This loop */
601  i = 0; /* should cycle at least four times for that to operate correctly. */
602  sfg.updateStatus(&sfg); /* This is where pending status updates are made visible */
603  }
604 
605  /*! assume NORMAL status for next pass through the loop */
606  sfg.queueStatus(&sfg, NORMAL);
607 
608  /*! Required to check incoming host command and send response before host times out */
610 
611  /*! Encode Orietantion Stream Packet and send packet to host*/
613 
614  pitIsrFlag = false; // Reset the flag for the next cycle
615  }
616  }
617 }
618 /// \endcode
void initSensorFusionGlobals(SensorFusionGlobals *sfg, StatusSubsystem *pStatusSubsystem, ControlSubsystem *pControlSubsystem)
utility function to insert default values in the top level structure
Definition: sensor_fusion.c:77
The register_io_i2c.h file declares low-level interface functions for reading and writing sensor regi...
volatile quaternion_type QuaternionPacketType
quaternion type transmitted over UART
Definition: control.h:70
#define APPLICATION_VERSION
Version to distinguish between instances the same application based on target Shield and updates...
#define F_USING_GYRO
nominally 0x0004 if a gyro is to be used, 0x0000 otherwise
void * bus_driver
should be of type (ARM_DRIVER_I2C* for I2C-based sensors, ARM_DRIVER_SPI* for SPI) ...
#define CHY
Used to access Y-channel entries in various data data structures.
Definition: sensor_fusion.h:87
#define HOST_PRO_CMD_W_CFG_TAG
Definition: host_io_uart.h:89
struct PhysicalSensor sensors[4]
This implementation uses up to 4 sensors.
int8_t FXAS21002_Read(struct PhysicalSensor *sensor, SensorFusionGlobals *sfg)
#define HOST_S_SIGNAL_EVENT
Definition: frdm_k64f.h:120
void Host_IO_Receive(host_cmd_proc_fn_t process_host_command, uint8_t encoding)
Definition: host_io_uart.c:233
#define BOARD_BootClockRUN
Definition: clock_config.h:45
bool process_host_command(uint8_t tag, uint8_t *hostCommand, uint8_t *hostResponse, size_t *hostMsgSize, size_t respBufferSize)
int32_t systick
systick timer;
#define APPLICATION_NAME
Unique Name for this application which should match the target GUI pkg name.
uint8_t data[FXLS8962_DATA_SIZE]
int32_t status
void initializeStatusSubsystem(StatusSubsystem *pStatus)
Definition: status.c:191
float fOmega[3]
average angular velocity (deg/s)
float q1
x vector component
Definition: orientation.h:49
#define SHIELD_NAME
writePort_t * write
low level function to write a char buffer to the serial stream
Definition: control.h:76
volatile uint8_t AltPacketOn
flag to enable altitude packet
Definition: control.h:74
installSensor_t * installSensor
function for installing a new sensor into t
int8_t MPL3115_Read(struct PhysicalSensor *sensor, SensorFusionGlobals *sfg)
int16_t iBc[3]
averaged calibrated measurement (counts)
#define ADS_MAX_STRING_LENGTH
#define HOST_PRO_INT_DEV_TAG
Definition: host_io_uart.h:85
float fThe
pitch (deg)
updateStatus_t * updateStatus
status=next status
#define HOST_PRO_INT_CMD_TAG
Bit aligned values for Host Protocol Interface IDs (Bits 5-6).
Definition: host_io_uart.h:83
he ControlSubsystem encapsulates command and data streaming functions.
Definition: control.h:68
volatile uint8_t AngularVelocityPacketOn
flag to enable angular velocity packet
Definition: control.h:71
void pit_init(uint32_t microseconds)
Definition: driver_pit.c:70
uint8_t orientOutputBuffer[STREAMING_HEADER_LEN+STREAMING_PAYLOAD_LEN]
#define THIS_BOARD
FRDM_K64F.
Definition: frdm_k64f.h:171
float fRho
compass (deg)
StatusSubsystem() provides an object-like interface for communicating status to the user...
Definition: status.h:48
The top level fusion structure.
quaternion_type DefaultQuaternionPacketType
default quaternion transmitted at power on
Definition: control.h:69
int8_t MAG3110_Read(struct PhysicalSensor *sensor, SensorFusionGlobals *sfg)
#define F_9DOF_GBY_KALMAN
9DOF accel, mag and gyro algorithm selector - 0x4000 to include, 0x0000 otherwise ...
int main(void)
Main function.
applyPerturbation_t * applyPerturbation
apply step function for testing purposes
void readAlgoParams(SV_ptr data, Quaternion *fq, int16_t *iPhi, int16_t *iThe, int16_t *iRho, int16_t iOmega[], uint16_t *isystick)
Utility function for reading common algorithm parameters.
struct MagSensor Mag
magnetometer storage
#define FXLS8962_I2C_ADDR
int8_t FXAS21002_Init(struct PhysicalSensor *sensor, SensorFusionGlobals *sfg)
initializeFusionEngine_t * initializeFusionEngine
set sensor fusion structures to initial values
int8_t MPL3115_Init(struct PhysicalSensor *sensor, SensorFusionGlobals *sfg)
#define THISBUILD
define build number sent in debug packet for display purposes only
volatile bool pitIsrFlag
Definition: driver_pit.c:61
void sBufAppendItem(uint8_t *pDest, uint16_t *pIndex, uint8_t *pSource, uint16_t iBytesToCopy)
Utility function used to place data in output buffer about to be transmitted via UART.
Definition: output_stream.c:63
setStatus_t * setStatus
change status indicator immediately
float fFitErrorpc
current fit error %
Definition: magnetic.h:88
The sensor_fusion.h file implements the top level programming interface.
volatile uint8_t RPCPacketOn
flag to enable roll, pitch, compass packet
Definition: control.h:73
#define LED_GREEN_ON()
Definition: board.h:115
#define CHZ
Used to access Z-channel entries in various data data structures.
Definition: sensor_fusion.h:88
void Host_IO_Add_ISO_Header(uint8_t streamID, uint8_t *pStreamingPacket, size_t sizePayload)
Definition: host_io_uart.c:112
void Host_IO_Send(uint8_t *pMsg, size_t size, uint8_t encoding)
Definition: host_io_uart.c:162
registerDeviceInfo_t deviceInfo
I2C device context.
Provides function prototypes for driver level interfaces.
uint8_t sUARTOutputBuffer[256]
main output buffer defined in control.c
Definition: control.c:63
Quaternion fq
orientation quaternion
void BOARD_SystickEnable(void)
Function to enable systicks framework.
Definition: systick_utils.c:70
#define I2C_S_DEVICE_INDEX
Definition: issdk_hal.h:61
#define CHX
Used to access X-channel entries in various data data structures.
Definition: sensor_fusion.h:86
uint32_t iFlags
a bit-field of sensors and algorithms used
SensorFusionGlobals sfg
void appendZerosOrientBuf(uint8_t *pDest, uint16_t *pIndex, uint16_t numZeros)
appendZerosOrientBuf utility function
int16_t iCountsPeruT
counts per uT
#define FXAS21002_I2C_ADDR
runFusion_t * runFusion
run the fusion routines
#define I2C_S_DRIVER
Definition: issdk_hal.h:59
streamData_t * stream
function to create packets for serial stream
Definition: control.h:77
Provides a simple abstraction for a periodic interval timer.
float q2
y vector component
Definition: orientation.h:50
StatusSubsystem statusSubsystem
Application-specific status subsystem.
float fPhi
roll (deg)
typedef int32_t(DATA_FORMAT_Append_t))(void *pData
The interface function to append the data on the formated stream.
#define BOARD_DEBUG_UART_BAUDRATE
Definition: board.h:57
quaternion structure definition
Definition: orientation.h:46
struct MagCalibration MagCal
mag cal storage
Quaternion derived from 3-axis accel (tilt)
Definition: sensor_fusion.h:75
#define MPL3115_I2C_ADDR
ARM Systick Utilities.
readSensors_t * readSensors
read all physical sensors
#define FUSION_HZ
(int) actual rate of fusion algorithm execution and sensor FIFO reads
void BOARD_RunADS(const char *appName, char *boardString, char *shieldString, size_t bufferLength)
The function to register Application Name and initialte ADS.
conditionSensorReadings_t * conditionSensorReadings
preprocessing step for sensor fusion
Defines control sub-system.
struct ControlSubsystem * pControlSubsystem
volatile bool gStreamingEnabled
uint8_t Host_IO_Init(ARM_DRIVER_USART *pDrv, void *pBus, void *pDevInfo, void *spiSlaveParams, uint16_t slaveAddress)
Definition: host_io_uart.c:126
#define SMC
Definition: lpc54114.h:144
#define F_USING_ACCEL
nominally 0x0001 if an accelerometer is to be used, 0x0000 otherwise
The host_io_uart.h file contains the Host Protocol interface definitions and configuration.
float q3
z vector component
Definition: orientation.h:51
int8_t FXLS8962_Read(struct PhysicalSensor *sensor, SensorFusionGlobals *sfg)
#define STREAMING_HEADER_LEN
Definition: host_io_uart.h:51
float q0
scalar component
Definition: orientation.h:48
status_t SMC_SetPowerModeWait(void *arg)
Configures the system to WAIT power mode. API name used from Kinetis family to maintain compatibility...
Definition: lpc54114.c:181
This structure defines the device specific info required by register I/O.
Definition: sensor_drv.h:128
int8_t FXLS8962_Init(struct PhysicalSensor *sensor, SensorFusionGlobals *sfg)
#define THIS_SHIELD
Operation is Nominal.
setStatus_t * queueStatus
queue status change for next regular interval
ControlSubsystem gOrientationControlSubsystem
int8_t MAG3110_Init(struct PhysicalSensor *sensor, SensorFusionGlobals *sfg)
An instance of PhysicalSensor structure type should be allocated for each physical sensors (combo dev...
#define F_USING_MAG
Definition: magnetic.h:47
#define MAG3110_I2C_ADDR
char boardString[ADS_MAX_STRING_LENGTH]
int32_t loopcounter
counter incrementing each iteration of sensor fusion (typically 25Hz)
registerDeviceInfo_t i2cBusInfo
volatile int8_t AccelCalPacketOn
variable used to coordinate accelerometer calibration
Definition: control.h:75
char shieldString[ADS_MAX_STRING_LENGTH]
#define I2C_S_SIGNAL_EVENT
Definition: issdk_hal.h:60
#define HOST_S_DRIVER
Definition: frdm_k64f.h:119
char embAppName[ADS_MAX_STRING_LENGTH]
volatile uint8_t DebugPacketOn
flag to enable debug packet
Definition: control.h:72