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