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