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