ISSDK  1.8
IoT Sensing Software Development Kit
orientaion_application_baremetal_agm04.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_agm04.c
11  * @brief The orientaion_application_baremetal_agm04.c file implements the ISSDK
12  * baremetal orientation application for FRDM-STBC-AGM04 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. */
80  shieldString[ADS_MAX_STRING_LENGTH] = {0}, \
81  embAppName[ADS_MAX_STRING_LENGTH] = {0};
82 
84 
85 
86 struct PhysicalSensor sensors[3]; /* This implementation uses three physical sensors */
89  .functionParam = NULL,
90  .idleFunction = NULL
91 };
92 
93 /*******************************************************************************
94  * Local Function Prototype Definitions
95  ******************************************************************************/
96 /* Utility Function to add field bytes to orientation buffer */
97 static void updateOrientBuf(uint8_t *pDest,
98  uint16_t *pIndex,
99  uint8_t *pSrc,
100  uint16_t iBytesToCopy);
101 /* Initialize the orientation application control subsystem */
102 static int8_t initOrientaionAppControlPort (ControlSubsystem *pComm);
103 /* Function to create orient packets for serial streaming over Host I/O */
104 static void encodeOrientPacketStream (SensorFusionGlobals * sfg, uint8_t *sUARTOutputBuffer);
105 /* Utility function for reading common algorithm parameters */
107  Quaternion *fq,
108  int16_t *iPhi,
109  int16_t *iThe,
110  int16_t *iRho,
111  int16_t iOmega[],
112  uint16_t *isystick);
113 
114 /*******************************************************************************
115  * Code
116  ******************************************************************************/
117 /* Handler for Device Info and Streaming Control Commands. */
119  uint8_t tag, uint8_t *hostCommand, uint8_t *hostResponse, size_t *hostMsgSize, size_t respBufferSize)
120 {
121  bool success = false;
122 
123  /* If it is Host requesting Device Info, send Board Name and Shield Name. */
124  if (tag == HOST_PRO_INT_DEV_TAG)
125  { /* Byte 1 : Payload - Length of APPLICATION_NAME (b)
126  * Bytes=b : Payload Application Name
127  * Byte b+1 : Payload - Length of BOARD_NAME (s)
128  * Bytes=s : Payload Board Name
129  * Byte b+s+2 : Payload - Length of SHIELD_NAME (v)
130  * Bytes=v : Payload Shield Name */
131 
132  size_t appNameLen = strlen(embAppName);
133  size_t boardNameLen = strlen(boardString);
134  size_t shieldNameLen = strlen(shieldString);
135 
136  if (respBufferSize >= boardNameLen + shieldNameLen + appNameLen + 3)
137  { /* We have sufficient buffer. */
138  *hostMsgSize = 0;
139  }
140  else
141  {
142  return false;
143  }
144 
145  hostResponse[*hostMsgSize] = appNameLen;
146  *hostMsgSize += 1;
147 
148  memcpy(hostResponse + *hostMsgSize, embAppName, appNameLen);
149  *hostMsgSize += appNameLen;
150 
151  hostResponse[*hostMsgSize] = boardNameLen;
152  *hostMsgSize += 1;
153 
154  memcpy(hostResponse + *hostMsgSize, boardString, boardNameLen);
155  *hostMsgSize += boardNameLen;
156 
157  hostResponse[*hostMsgSize] = shieldNameLen;
158  *hostMsgSize += 1;
159 
160  memcpy(hostResponse + *hostMsgSize, shieldString, shieldNameLen);
161  *hostMsgSize += shieldNameLen;
162 
163  return true;
164  }
165 
166  /* If it is Host sending Streaming Commands, take necessary actions. */
167  if ((tag == (HOST_PRO_INT_CMD_TAG | HOST_PRO_CMD_W_CFG_TAG)) &&
169  { /* Byte 1 : Payload - Operation Code (Start/Stop Operation Code)
170  * Byte 2 : Payload - Stream ID (Target Stream for carrying out operation) */
171  switch (hostCommand[0]) /* Execute desired operation (Start/Stop) on the requested Stream. */
172  {
173  case HOST_CMD_START:
174  if (hostCommand[1] == gPrimaryStreamID && gStreamingEnabled == false)
175  {
176  gStreamingEnabled = true;
177  success = true;
178  }
179  break;
180  case HOST_CMD_STOP:
181  if (hostCommand[1] == gPrimaryStreamID && gStreamingEnabled == true)
182  {
183  gStreamingEnabled = false;
184  success = true;
185  LED_GREEN_ON();
186  }
187  break;
188  default:
189  break;
190  }
191  *hostMsgSize = 0; /* Zero payload in response. */
192  }
193 
194  return success;
195 }
196 
197 /*!
198  * @brief updateOrientBuf utility function
199  */
200 void updateOrientBuf(uint8_t *pDest, uint16_t *pIndex, uint8_t *pSrc, uint16_t iBytesToCopy)
201 {
202  uint16_t counter;
203 
204  for (counter = 0; counter < iBytesToCopy; counter++)
205  {
206  pDest[(*pIndex)++] = pSrc[counter];
207  }
208 
209  return;
210 }
211 
212 /*!
213  * @brief appendZerosOrientBuf utility function
214  */
215 void appendZerosOrientBuf(uint8_t *pDest, uint16_t *pIndex, uint16_t numZeros) {
216  int16_t scratch16 = 0;
217  uint16_t i;
218  for (i=0; i<numZeros; i++) {
219  sBufAppendItem(pDest, pIndex, (uint8_t *) &scratch16, 2);
220  }
221 }
222 
223 /*!
224  * @brief Utility function for reading common algorithm parameters
225  */
227  SV_ptr data, Quaternion *fq, int16_t *iPhi, int16_t *iThe, int16_t *iRho, int16_t iOmega[], uint16_t *isystick)
228 {
229  *fq = data->fq;
230  iOmega[CHX] = (int16_t)(data->fOmega[CHX] * 20.0F);
231  iOmega[CHY] = (int16_t)(data->fOmega[CHY] * 20.0F);
232  iOmega[CHZ] = (int16_t)(data->fOmega[CHZ] * 20.0F);
233  *iPhi = (int16_t)(10.0F * data->fPhi);
234  *iThe = (int16_t)(10.0F * data->fThe);
235  *iRho = (int16_t)(10.0F * data->fRho);
236  *isystick = (uint16_t)(data->systick / 20);
237 }
238 
239 /*!
240  * @brief Initialize the orientation application control subsystem
241  */
242 static int8_t initOrientaionAppControlPort(ControlSubsystem *pComm /* pointer to the control subystem structure */
243  )
244 {
245  if (pComm)
246  {
247  pComm->DefaultQuaternionPacketType = Q3; /* default to simplest algorithm */
248  pComm->QuaternionPacketType = Q3; /* default to simplest algorithm */
249  pComm->AngularVelocityPacketOn = true; /* transmit angular velocity packet */
250  pComm->DebugPacketOn = true; /* transmit debug packet */
251  pComm->RPCPacketOn = true; /* transmit roll, pitch, compass packet */
252  pComm->AltPacketOn = true; /* Altitude packet */
253  pComm->AccelCalPacketOn = 0;
254  pComm->write = NULL;
255  pComm->stream = encodeOrientPacketStream; /* Function to create orient packets for serial stream */
256  memset(orientOutputBuffer, 0, sizeof(orientOutputBuffer));
257  return (0);
258  }
259  else
260  {
261  return (1);
262  }
263 }
264 
265 /*!
266  * @brief Function to create orient packets for serial streaming over Host I/O
267  */
268 void encodeOrientPacketStream(SensorFusionGlobals *sfg, uint8_t *sUARTOutputBuffer)
269 {
270  Quaternion fq; /* quaternion to be transmitted */
271  static uint32_t iTimeStamp = 0; /* 1MHz time stamp */
272  uint16_t iIndex; /* output buffer counter */
273  int32_t scratch32; /* scratch int32_t */
274  int16_t scratch16; /* scratch int16_t */
275  int16_t iPhi, iThe, iRho; /* integer angles to be transmitted */
276  int16_t iOmega[3]; /* scaled angular velocity vector */
277  uint16_t isystick; /* algorithm systick time */
278  uint8_t tmpuint8_t; /* scratch uint8_t */
279  uint8_t flags; /* byte of flags */
280  size_t sUARTOutputBufLen = STREAMING_PAYLOAD_LEN;
281 
282  /* Update the 1 microsecond timestamp counter */
283  iTimeStamp += 1000000 / FUSION_HZ;
284 
285  /* Start the index to store orientation packet payload into the transmit buffer */
286  iIndex = STREAMING_HEADER_LEN;
287 
288  /* Add Time Stamp to Orientation Buffer */
289  /*! Add 1 microsecond timestamp counter (4 bytes) value to Orientation Buffer */
290  updateOrientBuf(sUARTOutputBuffer, &iIndex, (uint8_t *)&iTimeStamp, 4);
291 
292  /* Add Accel X, Y Z values to Orientation Buffer */
293  /*! Add Accel X, Y Z (6 bytes) value to Orientation Buffer. Send non-zero data only if the accelerometer sensor is enabled */
294  if (sfg->iFlags & F_USING_ACCEL)
295  {
296  /* Accelerometer data is used for the selected quaternion so transmit but clip at 4g */
297  scratch32 = (sfg->Accel.iGc[CHX] * 8192) / sfg->Accel.iCountsPerg;
298  if (scratch32 > 32767) scratch32 = 32767;
299  if (scratch32 < -32768) scratch32 = -32768;
300  scratch16 = (int16_t) (scratch32);
301  updateOrientBuf(sUARTOutputBuffer, &iIndex, (uint8_t *) &scratch16, 2);
302 
303  scratch32 = (sfg->Accel.iGc[CHY] * 8192) / sfg->Accel.iCountsPerg;
304  if (scratch32 > 32767) scratch32 = 32767;
305  if (scratch32 < -32768) scratch32 = -32768;
306  scratch16 = (int16_t) (scratch32);
307  updateOrientBuf(sUARTOutputBuffer, &iIndex, (uint8_t *) &scratch16, 2);
308 
309  scratch32 = (sfg->Accel.iGc[CHZ] * 8192) / sfg->Accel.iCountsPerg;
310  if (scratch32 > 32767) scratch32 = 32767;
311  if (scratch32 < -32768) scratch32 = -32768;
312  scratch16 = (int16_t) (scratch32);
313  updateOrientBuf(sUARTOutputBuffer, &iIndex, (uint8_t *) &scratch16, 2);
314  }
315  else
316  {
317  /* Accelerometer structure is not defined so transmit zero */
318  appendZerosOrientBuf(sUARTOutputBuffer, &iIndex, 3);
319  }
320 
321  /* Add MAG X, Y Z values to Orientation Buffer */
322  /*! Add MAG X, Y Z (6 bytes) value to Orientation Buffer. Send non-zero data only if the magnetometer sensor is enabled */
323  if (sfg->iFlags & F_USING_MAG)
324  {
325  /* Magnetometer data is used for the selected quaternion so transmit */
326  scratch16 = (int16_t) (sfg->Mag.iBc[CHX] * 10) / (sfg->Mag.iCountsPeruT);
327  updateOrientBuf(sUARTOutputBuffer, &iIndex, (uint8_t *) &scratch16, 2);
328  scratch16 = (int16_t) ((sfg->Mag.iBc[CHY] * 10) / sfg->Mag.iCountsPeruT);
329  updateOrientBuf(sUARTOutputBuffer, &iIndex, (uint8_t *) &scratch16, 2);
330  scratch16 = (int16_t) ((sfg->Mag.iBc[CHZ] * 10) / sfg->Mag.iCountsPeruT);
331  updateOrientBuf(sUARTOutputBuffer, &iIndex, (uint8_t *) &scratch16, 2);
332  }
333  else
334  {
335  /* Magnetometer structure is not defined so transmit zero */
336  appendZerosOrientBuf(sUARTOutputBuffer, &iIndex, 3);
337  }
338 
339  /* Add Gyro X, Y Z values to Orientation Buffer */
340  /*! Add Gyro X, Y Z (6 bytes) value to Orientation Buffer. Send non-zero data only if the gyro sensor is enabled */
341  if (sfg->iFlags & F_USING_GYRO)
342  {
343  /* Gyro data is used for the selected quaternion so transmit */
344  scratch16 = (int16_t) ((sfg->Gyro.iYs[CHX] * 20) / sfg->Gyro.iCountsPerDegPerSec);
345  updateOrientBuf(sUARTOutputBuffer, &iIndex, (uint8_t *) &scratch16, 2);
346  scratch16 = (int16_t) ((sfg->Gyro.iYs[CHY] * 20) / sfg->Gyro.iCountsPerDegPerSec);
347  updateOrientBuf(sUARTOutputBuffer, &iIndex, (uint8_t *) &scratch16, 2);
348  scratch16 = (int16_t) ((sfg->Gyro.iYs[CHZ] * 20) / sfg->Gyro.iCountsPerDegPerSec);
349  updateOrientBuf(sUARTOutputBuffer, &iIndex, (uint8_t *) &scratch16, 2);
350  }
351  else
352  {
353  /* Gyro structure is not defined so transmit zero */
354  appendZerosOrientBuf(sUARTOutputBuffer, &iIndex, 3);
355  }
356 
357  /* Initialize default quaternion, flags byte, angular velocity and orientation */
358  fq.q0 = 1.0F;
359  fq.q1 = fq.q2 = fq.q3 = 0.0F;
360  flags = 0x00;
361  iOmega[CHX] = iOmega[CHY] = iOmega[CHZ] = 0;
362  iPhi = iThe = iRho = 0;
363  isystick = 0;
364 
365  /* set the quaternion, flags, angular velocity and Euler angles */
366  if (sfg->iFlags & F_9DOF_GBY_KALMAN)
367  {
368  flags |= 0x08;
369  readAlgoParams((SV_ptr)&sfg->SV_9DOF_GBY_KALMAN, &fq, &iPhi, &iThe, &iRho, iOmega, &isystick);
370  }
371 
372  /* Add Quaternion data to Orientation Buffer */
373  /*! Scale the quaternion and add to the Orientation Buffer */
374  scratch16 = (int16_t) (fq.q0 * 30000.0F);
375  updateOrientBuf(sUARTOutputBuffer, &iIndex, (uint8_t *) &scratch16, 2);
376  scratch16 = (int16_t) (fq.q1 * 30000.0F);
377  updateOrientBuf(sUARTOutputBuffer, &iIndex, (uint8_t *) &scratch16, 2);
378  scratch16 = (int16_t) (fq.q2 * 30000.0F);
379  updateOrientBuf(sUARTOutputBuffer, &iIndex, (uint8_t *) &scratch16, 2);
380  scratch16 = (int16_t) (fq.q3 * 30000.0F);
381  updateOrientBuf(sUARTOutputBuffer, &iIndex, (uint8_t *) &scratch16, 2);
382 
383  /* Add Euler angles data to Orientation Buffer */
384  /*! Add the Roll, Pitch, Compass Euler angles packet to the Orientation Buffer */
385  updateOrientBuf(sUARTOutputBuffer, &iIndex, (uint8_t *) &iPhi, 2);
386  updateOrientBuf(sUARTOutputBuffer, &iIndex, (uint8_t *) &iThe, 2);
387  updateOrientBuf(sUARTOutputBuffer, &iIndex, (uint8_t *) &iRho, 2);
388 
389  if (sfg->iFlags & F_USING_MAG)
390  {
391  /* Add Fit error values to Orientation Buffer */
392  if (sfg->MagCal.fFitErrorpc > 327.67F)
393  scratch16 = 32767;
394  else
395  scratch16 = (int16_t) (sfg->MagCal.fFitErrorpc * 100.0F);
396  updateOrientBuf(sUARTOutputBuffer, &iIndex, (uint8_t *) &scratch16, 2);
397  }
398 
399  /* Set the coordinate system bits in flags from default NED (00) */
400 #if THISCOORDSYSTEM == ANDROID
401  /* Set the Android flag bits */
402  flags |= 0x10;
403 #elif THISCOORDSYSTEM == WIN8
404  /* set the Win8 flag bits */
405  flags |= 0x20;
406 #endif // THISCOORDSYSTEM
407 
408  /*! Add the co-ordinate info byte to the Orientation Buffer */
409  updateOrientBuf(sUARTOutputBuffer, &iIndex, &flags, 1);
410 
411  /*! Add the shield (bits 7-5) and Kinetis (bits 4-0) byte to the Orientation Buffer */
412  tmpuint8_t = ((THIS_SHIELD & 0x07) << 5) | (THIS_BOARD & 0x1F);
413  updateOrientBuf(sUARTOutputBuffer, &iIndex, &tmpuint8_t, 1);
414 
415  /*! Add Software version number to the Orientation Buffer */
416  scratch16 = THISBUILD;
417  updateOrientBuf(sUARTOutputBuffer, &iIndex, (uint8_t *) &scratch16, 2);
418 
419  /*! Add systick count to the Orientation Buffer */
420  updateOrientBuf(sUARTOutputBuffer, &iIndex, (uint8_t *) &isystick, 2);
421 
422  /*! Send packet to host only if streaming has been enabled by Host. */
423 // if (gStreamingEnabled)
424  {
425  /*! Add Host I/O ISO streaming header */
426  Host_IO_Add_ISO_Header(gPrimaryStreamID, sUARTOutputBuffer, sUARTOutputBufLen);
427 
428  /*! Send Orientation streaming packet to Host using Host I/O */
429  Host_IO_Send(sUARTOutputBuffer, STREAMING_HEADER_LEN + sUARTOutputBufLen, HOST_FORMAT_HDLC);
430  }
431  return;
432 }
433 
434 /*!
435  * @brief Main function
436  */
437 int main(void)
438 {
439  int32_t status;
440  uint16_t i = 0;
441  uint8_t secondaryStreamID1,secondaryStreamID2; /* The auto assigned Stream ID not to be used to stream data. */
442  ARM_DRIVER_I2C* I2Cdrv = &I2C_S_DRIVER;
443  ARM_DRIVER_USART *pUartDriver = &HOST_S_DRIVER;
444 
445  /*! Initialize the MCU hardware. */
448 
449  /* Create the Short Application Name String for ADS. */
450  sprintf(embAppName, "%s:%s", APPLICATION_NAME, APPLICATION_VERSION);
451 
452  /*! Run ADS. */
454 
455  /* Create the Full Application Name String for Device Info Response. */
457 
458  /*! Initialize and set the KSDK driver for the I2C port */
459  I2Cdrv->Initialize(I2C_S_SIGNAL_EVENT);
460  I2Cdrv->PowerControl(ARM_POWER_FULL);
461  I2Cdrv->Control(ARM_I2C_BUS_SPEED, ARM_I2C_BUS_SPEED_FAST);
462 
463  /*! Initialize the UART driver. */
464  status = pUartDriver->Initialize(HOST_S_SIGNAL_EVENT);
465  if (ARM_DRIVER_OK != status)
466  {
467  return -1;
468  }
469  /*! Set UART Power mode. */
470  status = pUartDriver->PowerControl(ARM_POWER_FULL);
471  if (ARM_DRIVER_OK != status)
472  {
473  return -1;
474  }
475  /*! Set UART Baud Rate. */
476  status = pUartDriver->Control(ARM_USART_MODE_ASYNCHRONOUS, BOARD_DEBUG_UART_BAUDRATE);
477  if (ARM_DRIVER_OK != status)
478  {
479  return -1;
480  }
481 
482  /*! Initialize control sub-system for orientation packet streaming */
483  initOrientaionAppControlPort(&gOrientationControlSubsystem);
484 
485  /*! Initialize sensor fusion status sub-system */
486  initializeStatusSubsystem(&statusSubsystem);
487 
488  /*! Initialize sensor fusion global metadata */
489  initSensorFusionGlobals(&sfg, &statusSubsystem, &gOrientationControlSubsystem);
490 
491  /*! Install the sensors to be used by sensor fusion */
492  sfg.installSensor(&sfg, &sensors[0], MMA8652_I2C_ADDR, 1, (void*) I2Cdrv, &i2cBusInfo, MMA8652_Init, MMA8652_Read);
493  sfg.installSensor(&sfg, &sensors[1], FXAS21002_I2C_ADDR, 1, (void*) I2Cdrv, &i2cBusInfo, FXAS21002_Init, FXAS21002_Read);
494  sfg.installSensor(&sfg, &sensors[2], MAG3110_I2C_ADDR, 1, (void*) I2Cdrv, &i2cBusInfo, MAG3110_Init, MAG3110_Read);
495 
496  /*! Initialize streaming and assign Stream IDs. */
497  gStreamingEnabled = false;
498 
499  /* Registering the MMA8652 slave address with Host I/O for Register Read/Write. */
501  Host_IO_Init(pUartDriver, (void *)sensors[0].bus_driver, &sensors[0].deviceInfo, NULL, MMA8652_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 FXAS21002 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, FXAS21002_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 MAG3110 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, MAG3110_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  /*! Initialize sensor fusion engine */
527  sfg.initializeFusionEngine(&sfg); /* This will initialize sensors and magnetic calibration */
528 
529  /*! pitIsrFlag will be set true at FUSION_HZ periodic intervals */
530  pit_init(1000000/FUSION_HZ);
531 
532  sfg.setStatus(&sfg, NORMAL);
533  LED_GREEN_ON();
534 
535  while (true)
536  {
537  /*! Check whether occur interupt */
538  if (true == pitIsrFlag)
539  {
540  /*! Check for incoming commands form Host. */
542 
543  if (false == gStreamingEnabled)
544  {
545  SMC_SetPowerModeWait(SMC); /* Power save, wait if nothing to do. */
546  continue;
547  }
548 
549  /*! Reads sensors, applies HAL and does averaging (if applicable) */
550  sfg.readSensors(&sfg, 1);
551 
552  /*! Condition sensor data, magCal is run as part of this */
553  sfg.conditionSensorReadings(&sfg);
554 
555  /*! Run fusion algorithms */
556  sfg.runFusion(&sfg);
557 
558  /*! Apply debug perturbation */
559  sfg.applyPerturbation(&sfg);
560 
561  /*! Required to check incoming host command and send response before host times out */
563 
564  sfg.loopcounter++;
565  i = i + 1;
566  if (i >= 4)
567  { /* Some status codes include a "blink" feature. This loop */
568  i = 0; /* should cycle at least four times for that to operate correctly. */
569  sfg.updateStatus(&sfg); /* This is where pending status updates are made visible */
570  }
571 
572  /*! assume NORMAL status for next pass through the loop */
573  sfg.queueStatus(&sfg, NORMAL);
574 
575  /*! Required to check incoming host command and send response before host times out */
577 
578  /*! Encode Orietantion Stream Packet and send packet to host*/
580 
581  pitIsrFlag = false; // Reset the flag for the next cycle
582  }
583  }
584 }
585 /// \endcode
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
registerDeviceInfo_t i2cBusInfo
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
char boardString[ADS_MAX_STRING_LENGTH]
Provides function prototypes for driver level interfaces.
SensorFusionGlobals sfg
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
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)
int8_t MMA8652_Init(struct PhysicalSensor *sensor, SensorFusionGlobals *sfg)
int main(void)
Main function.
#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.
char shieldString[ADS_MAX_STRING_LENGTH]
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
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.
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
struct PhysicalSensor sensors[3]
he ControlSubsystem encapsulates command and data streaming functions.
Definition: control.h:42
#define BOARD_DEBUG_UART_BAUDRATE
Definition: board.h:31
char embAppName[ADS_MAX_STRING_LENGTH]
struct MagCalibration MagCal
mag cal storage
void appendZerosOrientBuf(uint8_t *pDest, uint16_t *pIndex, uint16_t numZeros)
appendZerosOrientBuf utility function
registerDeviceInfo_t deviceInfo
I2C device context.
uint32_t iFlags
a bit-field of sensors and algorithms used
#define BOARD_BootClockRUN
Definition: clock_config.h:19
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
float fOmega[3]
average angular velocity (deg/s)
int8_t MMA8652_Read(struct PhysicalSensor *sensor, SensorFusionGlobals *sfg)
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.
setStatus_t * setStatus
change status indicator immediately
volatile uint8_t AltPacketOn
flag to enable altitude packet
Definition: control.h:48
uint8_t orientOutputBuffer[STREAMING_HEADER_LEN+STREAMING_PAYLOAD_LEN]
#define APPLICATION_VERSION
Version to distinguish between instances the same application based on target Shield and updates...
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
ControlSubsystem gOrientationControlSubsystem
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
#define APPLICATION_NAME
Unique Name for this application which should match the target GUI pkg name.
#define MMA8652_I2C_ADDR
float q1
x vector component
Definition: orientation.h:23
ARM Systick Utilities.
#define CHX
Used to access X-channel entries in various data data structures.
Definition: sensor_fusion.h:60
#define I2C_S_DEVICE_INDEX
Definition: issdk_hal.h:35
int32_t systick
systick timer;
int16_t iBc[3]
averaged calibrated measurement (counts)
int8_t FXAS21002_Read(struct PhysicalSensor *sensor, SensorFusionGlobals *sfg)
volatile bool gStreamingEnabled
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
StatusSubsystem statusSubsystem
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)
bool process_host_command(uint8_t tag, uint8_t *hostCommand, uint8_t *hostResponse, size_t *hostMsgSize, size_t respBufferSize)
#define HOST_S_DRIVER
Definition: frdm_k64f.h:93
Provides a simple abstraction for a periodic interval timer.