Example of using Xbus library to communicate with an MTi-1 series device using a full-duplex UART connection.

Dependencies:   mbed-rtos mbed Xbus

Fork of MTi-1_example by Alex Young

Important Information

This example is deprecated and no longer maintained. There are new embedded examples available in the MT SDK folder of the MT Software Suite. For more information please visit: https://xsenstechnologies.force.com/knowledgebase/s/article/Introduction-to-the-MT-SDK-programming-examples-for-MTi-devices

Overview

The example program demonstrates connecting to an MTi-1 series device, restoring communications settings to default if necessary, and configuring the MTi to send data. For an MTi-1 the device is configured to send inertial sensor data, while MTi-2 and MTi-3 devices are configured to output orientation data using the onboard XKF3i filter.

Communication with the MTi-1 series device is implemented using a either a full-duplex UART, I2C or SPI bus. A reset line is used to reset the MTi during initialization. Data is output to a host PC terminal using a second UART.

For more information on the MTi-1 series communication protocol please refer to the datasheet: https://www.xsens.com/download/pdf/documentation/mti-1/mti-1-series_datasheet.pdf

Supported Platforms

The program has been tested on the following mbed platforms:

Using the Example

  1. To use the example program connect one of the supported mbed boards to the host PC and download the application from the mbed online compiler to the target device.
  2. With the mbed board unpowered (USB disconnected) wire the mbed board to the MTi-1 development board. The following connections are required:
    • In all cases:
      • 5V (or 3V3) main supply to VDD (P300-1)
      • MCU IO voltage (IORef) to VDDIO (P300-2)
      • GND to GND (P300-3)
      • MT_NRESET to nRST (P300-5)
    • For I2C communication:
      • MT_SCL to I2C_SCL (P300-9)
      • MT_SDA to I2C_SDA (P300-11)
      • MT_DRDY to DRDY (P300-15)
      • MT_ADD0 to ADD0 (P300-17)
      • MT_ADD1 to ADD1 (P300-19)
      • MT_ADD2 to ADD2 (P300-21)
    • For SPI communication:
      • MT_DRDY to DRDY (P300-15)
      • MT_SCLK to SPI_SCK (P300-17)
      • MT_MISO to SPI_MISO (P300-19)
      • MT_MOSI to SPI_MOSI (P300-21)
      • MT_nCS to SPI_nCS (P300-23)
    • For UART communication:
      • MT_RX to UART_TX (P300-9)
      • MT_TX to UART_RX (P300-11)

For more information on the MTi-1 development board please refer to the MTi-1 series user manual: https://www.xsens.com/download/pdf/documentation/mti-1/mti-1-series_dk_user_manual.pdf

Information

Check the defines at the top of main.cpp to determine which IO pins are used for the MT_xxx connections on each mbed platform.

Information

The active peripheral (I2C, SPI or UART) is selected on the MTi-1 development board through the PSEL0 and PSEL1 switches. Look on the bottom of the development board for the correct settings.

  1. Connect to the target using a serial terminal. The application is configured for:
    • Baudrate = 921600
    • Stop bits = 1
    • No parity bits
    • No flow control
  2. Reset the mbed board.
  3. You should be presented with a simple user interface as shown below:
MTi-1 series embedded example firmware.
Device ready for operation.
Found device with ID: 03880011.
Device is an MTi-3: Attitude Heading Reference System.
Output configuration set to:
        Packet counter: 65535 Hz
        Sample time fine: 65535 Hz
        Quaternion: 100 Hz
        Status word: 65535 Hz

Press 'm' to start measuring and 'c' to return to config mode.
Committer:
Alex Young
Date:
Wed May 20 16:46:54 2015 +0200
Revision:
28:ae74baf7e5ab
Parent:
21:6015b8be3a00
Child:
48:69d35430fe8b
Make XbusMessage_format be smart about payload formatting.

Just as the xbus parser can convert from xbus payload to native structs
the xbus message formatter can convert from a native struct to a raw
xbus message payload.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
Alex Young 18:2073072bad51 1 /*!
Alex Young 18:2073072bad51 2 * \file
Alex Young 18:2073072bad51 3 * \copyright
Alex Young 18:2073072bad51 4 * Copyright (C) Xsens Technologies B.V., 2015. All rights reserved.
Alex Young 18:2073072bad51 5 *
Alex Young 18:2073072bad51 6 * This source code is intended for use only by Xsens Technologies BV and
Alex Young 18:2073072bad51 7 * those that have explicit written permission to use it from
Alex Young 18:2073072bad51 8 * Xsens Technologies BV.
Alex Young 18:2073072bad51 9 *
Alex Young 18:2073072bad51 10 * THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY
Alex Young 18:2073072bad51 11 * KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
Alex Young 18:2073072bad51 12 * IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A
Alex Young 18:2073072bad51 13 * PARTICULAR PURPOSE.
Alex Young 18:2073072bad51 14 */
Alex Young 18:2073072bad51 15
Alex Young 18:2073072bad51 16 #include "xbusmessage.h"
Alex Young 18:2073072bad51 17 #include "xbusdef.h"
Alex Young 18:2073072bad51 18 #include "xbusutility.h"
Alex Young 18:2073072bad51 19
Alex Young 28:ae74baf7e5ab 20 static uint16_t messageLength(struct XbusMessage const* message)
Alex Young 28:ae74baf7e5ab 21 {
Alex Young 28:ae74baf7e5ab 22 switch (message->mid)
Alex Young 28:ae74baf7e5ab 23 {
Alex Young 28:ae74baf7e5ab 24 case XMID_SetOutputConfig:
Alex Young 28:ae74baf7e5ab 25 return message->length * 2 * sizeof(uint16_t);
Alex Young 28:ae74baf7e5ab 26
Alex Young 28:ae74baf7e5ab 27 default:
Alex Young 28:ae74baf7e5ab 28 return message->length;
Alex Young 28:ae74baf7e5ab 29 }
Alex Young 28:ae74baf7e5ab 30 }
Alex Young 28:ae74baf7e5ab 31
Alex Young 28:ae74baf7e5ab 32 static void formatOutputConfig(uint8_t* raw, struct XbusMessage const* message)
Alex Young 28:ae74baf7e5ab 33 {
Alex Young 28:ae74baf7e5ab 34 struct OutputConfiguration* conf = message->data;
Alex Young 28:ae74baf7e5ab 35 for (int i = 0; i < message->length; ++i)
Alex Young 28:ae74baf7e5ab 36 {
Alex Young 28:ae74baf7e5ab 37 raw = XbusUtility_writeU16(raw, conf->dtype);
Alex Young 28:ae74baf7e5ab 38 raw = XbusUtility_writeU16(raw, conf->freq);
Alex Young 28:ae74baf7e5ab 39 ++conf;
Alex Young 28:ae74baf7e5ab 40 }
Alex Young 28:ae74baf7e5ab 41 }
Alex Young 28:ae74baf7e5ab 42
Alex Young 28:ae74baf7e5ab 43 static void formatPayload(uint8_t* raw, struct XbusMessage const* message)
Alex Young 28:ae74baf7e5ab 44 {
Alex Young 28:ae74baf7e5ab 45 switch (message->mid)
Alex Young 28:ae74baf7e5ab 46 {
Alex Young 28:ae74baf7e5ab 47 case XMID_SetOutputConfig:
Alex Young 28:ae74baf7e5ab 48 formatOutputConfig(raw, message);
Alex Young 28:ae74baf7e5ab 49 break;
Alex Young 28:ae74baf7e5ab 50
Alex Young 28:ae74baf7e5ab 51 default:
Alex Young 28:ae74baf7e5ab 52 for (int i = 0; i < message->length; ++i)
Alex Young 28:ae74baf7e5ab 53 {
Alex Young 28:ae74baf7e5ab 54 *raw++ = ((uint8_t*)message->data)[i];
Alex Young 28:ae74baf7e5ab 55 }
Alex Young 28:ae74baf7e5ab 56 break;
Alex Young 28:ae74baf7e5ab 57 }
Alex Young 28:ae74baf7e5ab 58 }
Alex Young 28:ae74baf7e5ab 59
Alex Young 18:2073072bad51 60 size_t XbusMessage_format(uint8_t* raw, struct XbusMessage const* message)
Alex Young 18:2073072bad51 61 {
Alex Young 18:2073072bad51 62 uint8_t* dptr = raw;
Alex Young 18:2073072bad51 63 *dptr++ = XBUS_PREAMBLE;
Alex Young 18:2073072bad51 64
Alex Young 18:2073072bad51 65 *dptr = XBUS_BUS_ID_STANDALONE;
Alex Young 18:2073072bad51 66 uint8_t checksum = 0;
Alex Young 18:2073072bad51 67 checksum -= *dptr++;
Alex Young 18:2073072bad51 68
Alex Young 18:2073072bad51 69 *dptr = message->mid;
Alex Young 18:2073072bad51 70 checksum -= *dptr++;
Alex Young 18:2073072bad51 71
Alex Young 28:ae74baf7e5ab 72 uint16_t length = messageLength(message);
Alex Young 28:ae74baf7e5ab 73
Alex Young 28:ae74baf7e5ab 74 if (length < XBUS_EXTENDED_LENGTH)
Alex Young 18:2073072bad51 75 {
Alex Young 28:ae74baf7e5ab 76 *dptr = length;
Alex Young 18:2073072bad51 77 checksum -= *dptr++;
Alex Young 18:2073072bad51 78 }
Alex Young 18:2073072bad51 79 else
Alex Young 18:2073072bad51 80 {
Alex Young 18:2073072bad51 81 *dptr = XBUS_EXTENDED_LENGTH;
Alex Young 18:2073072bad51 82 checksum -= *dptr++;
Alex Young 28:ae74baf7e5ab 83 *dptr = length >> 8;
Alex Young 18:2073072bad51 84 checksum -= *dptr++;
Alex Young 28:ae74baf7e5ab 85 *dptr = length & 0xFF;
Alex Young 18:2073072bad51 86 checksum -= *dptr++;
Alex Young 18:2073072bad51 87 }
Alex Young 18:2073072bad51 88
Alex Young 28:ae74baf7e5ab 89 formatPayload(dptr, message);
Alex Young 28:ae74baf7e5ab 90 for (int i = 0; i < length; ++i)
Alex Young 18:2073072bad51 91 {
Alex Young 18:2073072bad51 92 checksum -= *dptr++;
Alex Young 18:2073072bad51 93 }
Alex Young 18:2073072bad51 94 *dptr++ = checksum;
Alex Young 18:2073072bad51 95
Alex Young 18:2073072bad51 96 return dptr - raw;
Alex Young 18:2073072bad51 97 }
Alex Young 18:2073072bad51 98
Alex Young 18:2073072bad51 99 static uint8_t const* getPointerToData(enum XsDataIdentifier id, uint8_t const* data, uint16_t dataLength)
Alex Young 18:2073072bad51 100 {
Alex Young 18:2073072bad51 101 uint8_t const* dptr = data;
Alex Young 18:2073072bad51 102 while (dptr < data + dataLength)
Alex Young 18:2073072bad51 103 {
Alex Young 18:2073072bad51 104 uint16_t itemId;
Alex Young 18:2073072bad51 105 uint8_t itemSize;
Alex Young 18:2073072bad51 106 dptr = XbusUtility_readU16(&itemId, dptr);
Alex Young 18:2073072bad51 107 dptr = XbusUtility_readU8(&itemSize, dptr);
Alex Young 18:2073072bad51 108
Alex Young 18:2073072bad51 109 if (id == itemId)
Alex Young 18:2073072bad51 110 return dptr;
Alex Young 18:2073072bad51 111
Alex Young 18:2073072bad51 112 dptr += itemSize;
Alex Young 18:2073072bad51 113 }
Alex Young 18:2073072bad51 114 return NULL;
Alex Young 18:2073072bad51 115 }
Alex Young 18:2073072bad51 116
Alex Young 18:2073072bad51 117 static void readFloats(float* out, uint8_t const* raw, uint8_t floats)
Alex Young 18:2073072bad51 118 {
Alex Young 18:2073072bad51 119 for (int i = 0; i < floats; ++i)
Alex Young 18:2073072bad51 120 {
Alex Young 18:2073072bad51 121 raw = XbusUtility_readU32((uint32_t*)&out[i], raw);
Alex Young 18:2073072bad51 122 }
Alex Young 18:2073072bad51 123 }
Alex Young 18:2073072bad51 124
Alex Young 18:2073072bad51 125 bool XbusMessage_getDataItem(void* item, enum XsDataIdentifier id, struct XbusMessage const* message)
Alex Young 18:2073072bad51 126 {
Alex Young 18:2073072bad51 127 uint8_t const* raw = getPointerToData(id, message->data, message->length);
Alex Young 18:2073072bad51 128 if (raw)
Alex Young 18:2073072bad51 129 {
Alex Young 18:2073072bad51 130 switch (id)
Alex Young 18:2073072bad51 131 {
Alex Young 18:2073072bad51 132 case XDI_PacketCounter:
Alex Young 18:2073072bad51 133 raw = XbusUtility_readU16(item, raw);
Alex Young 18:2073072bad51 134 break;
Alex Young 18:2073072bad51 135
Alex Young 18:2073072bad51 136 case XDI_SampleTimeFine:
Alex Young 18:2073072bad51 137 case XDI_StatusWord:
Alex Young 18:2073072bad51 138 raw = XbusUtility_readU32(item, raw);
Alex Young 18:2073072bad51 139 break;
Alex Young 18:2073072bad51 140
Alex Young 18:2073072bad51 141 case XDI_Quaternion:
Alex Young 18:2073072bad51 142 case XDI_DeltaQ:
Alex Young 18:2073072bad51 143 readFloats(item, raw, 4);
Alex Young 18:2073072bad51 144 break;
Alex Young 18:2073072bad51 145
Alex Young 18:2073072bad51 146 case XDI_DeltaV:
Alex Young 18:2073072bad51 147 case XDI_Acceleration:
Alex Young 18:2073072bad51 148 case XDI_RateOfTurn:
Alex Young 18:2073072bad51 149 case XDI_MagneticField:
Alex Young 18:2073072bad51 150 readFloats(item, raw, 3);
Alex Young 18:2073072bad51 151 break;
Alex Young 18:2073072bad51 152
Alex Young 18:2073072bad51 153 default:
Alex Young 18:2073072bad51 154 return false;
Alex Young 18:2073072bad51 155 }
Alex Young 18:2073072bad51 156 return true;
Alex Young 18:2073072bad51 157 }
Alex Young 18:2073072bad51 158 else
Alex Young 18:2073072bad51 159 {
Alex Young 18:2073072bad51 160 return false;
Alex Young 18:2073072bad51 161 }
Alex Young 18:2073072bad51 162 }
Alex Young 18:2073072bad51 163
Alex Young 21:6015b8be3a00 164 char const* XbusMessage_dataDescription(enum XsDataIdentifier id)
Alex Young 21:6015b8be3a00 165 {
Alex Young 21:6015b8be3a00 166 switch (id)
Alex Young 21:6015b8be3a00 167 {
Alex Young 21:6015b8be3a00 168 case XDI_PacketCounter:
Alex Young 21:6015b8be3a00 169 return "Packet counter";
Alex Young 21:6015b8be3a00 170
Alex Young 21:6015b8be3a00 171 case XDI_SampleTimeFine:
Alex Young 21:6015b8be3a00 172 return "Sample time fine";
Alex Young 21:6015b8be3a00 173
Alex Young 21:6015b8be3a00 174 case XDI_Quaternion:
Alex Young 21:6015b8be3a00 175 return "Quaternion";
Alex Young 21:6015b8be3a00 176
Alex Young 21:6015b8be3a00 177 case XDI_DeltaV:
Alex Young 21:6015b8be3a00 178 return "Velocity increment";
Alex Young 21:6015b8be3a00 179
Alex Young 21:6015b8be3a00 180 case XDI_Acceleration:
Alex Young 21:6015b8be3a00 181 return "Acceleration";
Alex Young 21:6015b8be3a00 182
Alex Young 21:6015b8be3a00 183 case XDI_RateOfTurn:
Alex Young 21:6015b8be3a00 184 return "Rate of turn";
Alex Young 21:6015b8be3a00 185
Alex Young 21:6015b8be3a00 186 case XDI_DeltaQ:
Alex Young 21:6015b8be3a00 187 return "Orientation increment";
Alex Young 21:6015b8be3a00 188
Alex Young 21:6015b8be3a00 189 case XDI_MagneticField:
Alex Young 21:6015b8be3a00 190 return "Magnetic field";
Alex Young 21:6015b8be3a00 191
Alex Young 21:6015b8be3a00 192 case XDI_StatusWord:
Alex Young 21:6015b8be3a00 193 return "Status word";
Alex Young 21:6015b8be3a00 194
Alex Young 21:6015b8be3a00 195 default:
Alex Young 21:6015b8be3a00 196 return "Unknown data type";
Alex Young 21:6015b8be3a00 197 }
Alex Young 21:6015b8be3a00 198 }