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