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:
Fri Jun 12 13:23:26 2015 +0200
Revision:
61:b9d3e7e5ba0c
Parent:
50:f581d5428ea6
Change to Apache license

Who changed what in which revision?

UserRevisionLine numberNew contents of line
Alex Young 18:2073072bad51 1 /*!
Alex Young 18:2073072bad51 2 * \file
Alex Young 61:b9d3e7e5ba0c 3 * \copyright Copyright (C) Xsens Technologies B.V., 2015.
Alex Young 61:b9d3e7e5ba0c 4 *
Alex Young 61:b9d3e7e5ba0c 5 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
Alex Young 61:b9d3e7e5ba0c 6 * use this file except in compliance with the License. You may obtain a copy
Alex Young 61:b9d3e7e5ba0c 7 * of the License at
Alex Young 18:2073072bad51 8 *
Alex Young 61:b9d3e7e5ba0c 9 * http://www.apache.org/licenses/LICENSE-2.0
Alex Young 18:2073072bad51 10 *
Alex Young 61:b9d3e7e5ba0c 11 * Unless required by applicable law or agreed to in writing, software
Alex Young 61:b9d3e7e5ba0c 12 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
Alex Young 61:b9d3e7e5ba0c 13 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
Alex Young 61:b9d3e7e5ba0c 14 * License for the specific language governing permissions and limitations
Alex Young 61:b9d3e7e5ba0c 15 * under the License.
Alex Young 18:2073072bad51 16 */
Alex Young 18:2073072bad51 17
Alex Young 18:2073072bad51 18 #include "xbusmessage.h"
Alex Young 18:2073072bad51 19 #include "xbusdef.h"
Alex Young 18:2073072bad51 20 #include "xbusutility.h"
Alex Young 18:2073072bad51 21
Alex Young 48:69d35430fe8b 22 /*!
Alex Young 48:69d35430fe8b 23 * \brief Calculate the number of bytes needed for \a message payload.
Alex Young 48:69d35430fe8b 24 */
Alex Young 28:ae74baf7e5ab 25 static uint16_t messageLength(struct XbusMessage const* message)
Alex Young 28:ae74baf7e5ab 26 {
Alex Young 28:ae74baf7e5ab 27 switch (message->mid)
Alex Young 28:ae74baf7e5ab 28 {
Alex Young 28:ae74baf7e5ab 29 case XMID_SetOutputConfig:
Alex Young 28:ae74baf7e5ab 30 return message->length * 2 * sizeof(uint16_t);
Alex Young 28:ae74baf7e5ab 31
Alex Young 28:ae74baf7e5ab 32 default:
Alex Young 28:ae74baf7e5ab 33 return message->length;
Alex Young 28:ae74baf7e5ab 34 }
Alex Young 28:ae74baf7e5ab 35 }
Alex Young 28:ae74baf7e5ab 36
Alex Young 48:69d35430fe8b 37 /*!
Alex Young 48:69d35430fe8b 38 * \brief Format a message with a pointer to an array of OutputConfiguration elements.
Alex Young 48:69d35430fe8b 39 */
Alex Young 28:ae74baf7e5ab 40 static void formatOutputConfig(uint8_t* raw, struct XbusMessage const* message)
Alex Young 28:ae74baf7e5ab 41 {
Alex Young 28:ae74baf7e5ab 42 struct OutputConfiguration* conf = message->data;
Alex Young 28:ae74baf7e5ab 43 for (int i = 0; i < message->length; ++i)
Alex Young 28:ae74baf7e5ab 44 {
Alex Young 28:ae74baf7e5ab 45 raw = XbusUtility_writeU16(raw, conf->dtype);
Alex Young 28:ae74baf7e5ab 46 raw = XbusUtility_writeU16(raw, conf->freq);
Alex Young 28:ae74baf7e5ab 47 ++conf;
Alex Young 28:ae74baf7e5ab 48 }
Alex Young 28:ae74baf7e5ab 49 }
Alex Young 28:ae74baf7e5ab 50
Alex Young 48:69d35430fe8b 51 /*!
Alex Young 48:69d35430fe8b 52 * \brief Format the payload of a message from a native data type to
Alex Young 48:69d35430fe8b 53 * raw bytes.
Alex Young 48:69d35430fe8b 54 */
Alex Young 28:ae74baf7e5ab 55 static void formatPayload(uint8_t* raw, struct XbusMessage const* message)
Alex Young 28:ae74baf7e5ab 56 {
Alex Young 28:ae74baf7e5ab 57 switch (message->mid)
Alex Young 28:ae74baf7e5ab 58 {
Alex Young 28:ae74baf7e5ab 59 case XMID_SetOutputConfig:
Alex Young 28:ae74baf7e5ab 60 formatOutputConfig(raw, message);
Alex Young 28:ae74baf7e5ab 61 break;
Alex Young 28:ae74baf7e5ab 62
Alex Young 28:ae74baf7e5ab 63 default:
Alex Young 28:ae74baf7e5ab 64 for (int i = 0; i < message->length; ++i)
Alex Young 28:ae74baf7e5ab 65 {
Alex Young 28:ae74baf7e5ab 66 *raw++ = ((uint8_t*)message->data)[i];
Alex Young 28:ae74baf7e5ab 67 }
Alex Young 28:ae74baf7e5ab 68 break;
Alex Young 28:ae74baf7e5ab 69 }
Alex Young 28:ae74baf7e5ab 70 }
Alex Young 28:ae74baf7e5ab 71
Alex Young 48:69d35430fe8b 72 /*!
Alex Young 48:69d35430fe8b 73 * \brief Format a message into the raw Xbus format ready for transmission to
Alex Young 48:69d35430fe8b 74 * a motion tracker.
Alex Young 48:69d35430fe8b 75 */
Alex Young 18:2073072bad51 76 size_t XbusMessage_format(uint8_t* raw, struct XbusMessage const* message)
Alex Young 18:2073072bad51 77 {
Alex Young 18:2073072bad51 78 uint8_t* dptr = raw;
Alex Young 18:2073072bad51 79 *dptr++ = XBUS_PREAMBLE;
Alex Young 18:2073072bad51 80
Alex Young 50:f581d5428ea6 81 *dptr = XBUS_MASTERDEVICE;
Alex Young 18:2073072bad51 82 uint8_t checksum = 0;
Alex Young 18:2073072bad51 83 checksum -= *dptr++;
Alex Young 18:2073072bad51 84
Alex Young 18:2073072bad51 85 *dptr = message->mid;
Alex Young 18:2073072bad51 86 checksum -= *dptr++;
Alex Young 18:2073072bad51 87
Alex Young 28:ae74baf7e5ab 88 uint16_t length = messageLength(message);
Alex Young 28:ae74baf7e5ab 89
Alex Young 28:ae74baf7e5ab 90 if (length < XBUS_EXTENDED_LENGTH)
Alex Young 18:2073072bad51 91 {
Alex Young 28:ae74baf7e5ab 92 *dptr = length;
Alex Young 18:2073072bad51 93 checksum -= *dptr++;
Alex Young 18:2073072bad51 94 }
Alex Young 18:2073072bad51 95 else
Alex Young 18:2073072bad51 96 {
Alex Young 18:2073072bad51 97 *dptr = XBUS_EXTENDED_LENGTH;
Alex Young 18:2073072bad51 98 checksum -= *dptr++;
Alex Young 28:ae74baf7e5ab 99 *dptr = length >> 8;
Alex Young 18:2073072bad51 100 checksum -= *dptr++;
Alex Young 28:ae74baf7e5ab 101 *dptr = length & 0xFF;
Alex Young 18:2073072bad51 102 checksum -= *dptr++;
Alex Young 18:2073072bad51 103 }
Alex Young 18:2073072bad51 104
Alex Young 28:ae74baf7e5ab 105 formatPayload(dptr, message);
Alex Young 28:ae74baf7e5ab 106 for (int i = 0; i < length; ++i)
Alex Young 18:2073072bad51 107 {
Alex Young 18:2073072bad51 108 checksum -= *dptr++;
Alex Young 18:2073072bad51 109 }
Alex Young 18:2073072bad51 110 *dptr++ = checksum;
Alex Young 18:2073072bad51 111
Alex Young 18:2073072bad51 112 return dptr - raw;
Alex Young 18:2073072bad51 113 }
Alex Young 18:2073072bad51 114
Alex Young 48:69d35430fe8b 115 /*!
Alex Young 49:38ecfbff5391 116 * \brief Get a pointer to the data corresponding to \a id.
Alex Young 48:69d35430fe8b 117 * \param id The data identifier to find in the message.
Alex Young 48:69d35430fe8b 118 * \param data Pointer to the raw message payload.
Alex Young 48:69d35430fe8b 119 * \param dataLength The length of the payload in bytes.
Alex Young 48:69d35430fe8b 120 * \returns Pointer to data item, or NULL if the identifier is not present in
Alex Young 48:69d35430fe8b 121 * the message.
Alex Young 48:69d35430fe8b 122 */
Alex Young 18:2073072bad51 123 static uint8_t const* getPointerToData(enum XsDataIdentifier id, uint8_t const* data, uint16_t dataLength)
Alex Young 18:2073072bad51 124 {
Alex Young 18:2073072bad51 125 uint8_t const* dptr = data;
Alex Young 18:2073072bad51 126 while (dptr < data + dataLength)
Alex Young 18:2073072bad51 127 {
Alex Young 18:2073072bad51 128 uint16_t itemId;
Alex Young 18:2073072bad51 129 uint8_t itemSize;
Alex Young 18:2073072bad51 130 dptr = XbusUtility_readU16(&itemId, dptr);
Alex Young 18:2073072bad51 131 dptr = XbusUtility_readU8(&itemSize, dptr);
Alex Young 18:2073072bad51 132
Alex Young 18:2073072bad51 133 if (id == itemId)
Alex Young 18:2073072bad51 134 return dptr;
Alex Young 18:2073072bad51 135
Alex Young 18:2073072bad51 136 dptr += itemSize;
Alex Young 18:2073072bad51 137 }
Alex Young 18:2073072bad51 138 return NULL;
Alex Young 18:2073072bad51 139 }
Alex Young 18:2073072bad51 140
Alex Young 48:69d35430fe8b 141 /*!
Alex Young 48:69d35430fe8b 142 * \brief Read a number of floats from a message payload.
Alex Young 48:69d35430fe8b 143 * \param out Pointer to where to output data.
Alex Young 48:69d35430fe8b 144 * \param raw Pointer to the start of the raw float data.
Alex Young 48:69d35430fe8b 145 * \param floats The number of floats to read.
Alex Young 48:69d35430fe8b 146 */
Alex Young 18:2073072bad51 147 static void readFloats(float* out, uint8_t const* raw, uint8_t floats)
Alex Young 18:2073072bad51 148 {
Alex Young 18:2073072bad51 149 for (int i = 0; i < floats; ++i)
Alex Young 18:2073072bad51 150 {
Alex Young 18:2073072bad51 151 raw = XbusUtility_readU32((uint32_t*)&out[i], raw);
Alex Young 18:2073072bad51 152 }
Alex Young 18:2073072bad51 153 }
Alex Young 18:2073072bad51 154
Alex Young 48:69d35430fe8b 155 /*!
Alex Young 49:38ecfbff5391 156 * \brief Get a data item from an XMID_MtData2 Xbus message.
Alex Young 48:69d35430fe8b 157 * \param item Pointer to where to store the data.
Alex Young 48:69d35430fe8b 158 * \param id The data identifier to get.
Alex Young 48:69d35430fe8b 159 * \param message The message to read the data item from.
Alex Young 48:69d35430fe8b 160 * \returns true if the data item is found in the message, else false.
Alex Young 48:69d35430fe8b 161 */
Alex Young 18:2073072bad51 162 bool XbusMessage_getDataItem(void* item, enum XsDataIdentifier id, struct XbusMessage const* message)
Alex Young 18:2073072bad51 163 {
Alex Young 18:2073072bad51 164 uint8_t const* raw = getPointerToData(id, message->data, message->length);
Alex Young 18:2073072bad51 165 if (raw)
Alex Young 18:2073072bad51 166 {
Alex Young 18:2073072bad51 167 switch (id)
Alex Young 18:2073072bad51 168 {
Alex Young 18:2073072bad51 169 case XDI_PacketCounter:
Alex Young 18:2073072bad51 170 raw = XbusUtility_readU16(item, raw);
Alex Young 18:2073072bad51 171 break;
Alex Young 18:2073072bad51 172
Alex Young 18:2073072bad51 173 case XDI_SampleTimeFine:
Alex Young 18:2073072bad51 174 case XDI_StatusWord:
Alex Young 18:2073072bad51 175 raw = XbusUtility_readU32(item, raw);
Alex Young 18:2073072bad51 176 break;
Alex Young 18:2073072bad51 177
Alex Young 18:2073072bad51 178 case XDI_Quaternion:
Alex Young 18:2073072bad51 179 case XDI_DeltaQ:
Alex Young 18:2073072bad51 180 readFloats(item, raw, 4);
Alex Young 18:2073072bad51 181 break;
Alex Young 18:2073072bad51 182
Alex Young 18:2073072bad51 183 case XDI_DeltaV:
Alex Young 18:2073072bad51 184 case XDI_Acceleration:
Alex Young 18:2073072bad51 185 case XDI_RateOfTurn:
Alex Young 18:2073072bad51 186 case XDI_MagneticField:
Alex Young 18:2073072bad51 187 readFloats(item, raw, 3);
Alex Young 18:2073072bad51 188 break;
Alex Young 18:2073072bad51 189
Alex Young 18:2073072bad51 190 default:
Alex Young 18:2073072bad51 191 return false;
Alex Young 18:2073072bad51 192 }
Alex Young 18:2073072bad51 193 return true;
Alex Young 18:2073072bad51 194 }
Alex Young 18:2073072bad51 195 else
Alex Young 18:2073072bad51 196 {
Alex Young 18:2073072bad51 197 return false;
Alex Young 18:2073072bad51 198 }
Alex Young 18:2073072bad51 199 }
Alex Young 18:2073072bad51 200
Alex Young 48:69d35430fe8b 201 /*!
Alex Young 49:38ecfbff5391 202 * \brief Get a string description for the passed data identifier.
Alex Young 48:69d35430fe8b 203 */
Alex Young 21:6015b8be3a00 204 char const* XbusMessage_dataDescription(enum XsDataIdentifier id)
Alex Young 21:6015b8be3a00 205 {
Alex Young 21:6015b8be3a00 206 switch (id)
Alex Young 21:6015b8be3a00 207 {
Alex Young 21:6015b8be3a00 208 case XDI_PacketCounter:
Alex Young 21:6015b8be3a00 209 return "Packet counter";
Alex Young 21:6015b8be3a00 210
Alex Young 21:6015b8be3a00 211 case XDI_SampleTimeFine:
Alex Young 21:6015b8be3a00 212 return "Sample time fine";
Alex Young 21:6015b8be3a00 213
Alex Young 21:6015b8be3a00 214 case XDI_Quaternion:
Alex Young 21:6015b8be3a00 215 return "Quaternion";
Alex Young 21:6015b8be3a00 216
Alex Young 21:6015b8be3a00 217 case XDI_DeltaV:
Alex Young 21:6015b8be3a00 218 return "Velocity increment";
Alex Young 21:6015b8be3a00 219
Alex Young 21:6015b8be3a00 220 case XDI_Acceleration:
Alex Young 21:6015b8be3a00 221 return "Acceleration";
Alex Young 21:6015b8be3a00 222
Alex Young 21:6015b8be3a00 223 case XDI_RateOfTurn:
Alex Young 21:6015b8be3a00 224 return "Rate of turn";
Alex Young 21:6015b8be3a00 225
Alex Young 21:6015b8be3a00 226 case XDI_DeltaQ:
Alex Young 21:6015b8be3a00 227 return "Orientation increment";
Alex Young 21:6015b8be3a00 228
Alex Young 21:6015b8be3a00 229 case XDI_MagneticField:
Alex Young 21:6015b8be3a00 230 return "Magnetic field";
Alex Young 21:6015b8be3a00 231
Alex Young 21:6015b8be3a00 232 case XDI_StatusWord:
Alex Young 21:6015b8be3a00 233 return "Status word";
Alex Young 21:6015b8be3a00 234
Alex Young 21:6015b8be3a00 235 default:
Alex Young 21:6015b8be3a00 236 return "Unknown data type";
Alex Young 21:6015b8be3a00 237 }
Alex Young 21:6015b8be3a00 238 }