Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Revision 0:c23e915f255b, committed 2018-08-05
- Comitter:
- JJting
- Date:
- Sun Aug 05 13:15:56 2018 +0000
- Child:
- 1:2823a39f70a9
- Commit message:
- ver1
Changed in this revision
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/LSM9DS1.cpp Sun Aug 05 13:15:56 2018 +0000
@@ -0,0 +1,401 @@
+#include "LSM9DS1.h"
+
+LSM9DS1::LSM9DS1(PinName sda, PinName scl, uint8_t xgAddr, uint8_t mAddr) : i2c(sda, scl)
+{
+ // xgAddress and mAddress will store the 7-bit I2C address, if using I2C.
+ xgAddress = xgAddr;
+ mAddress = mAddr;
+}
+
+uint16_t LSM9DS1::begin(gyro_scale gScl, accel_scale aScl, mag_scale mScl,
+ gyro_odr gODR, accel_odr aODR, mag_odr mODR)
+{
+ // Store the given scales in class variables. These scale variables
+ // are used throughout to calculate the actual g's, DPS,and Gs's.
+ gScale = gScl;
+ aScale = aScl;
+ mScale = mScl;
+
+ // Once we have the scale values, we can calculate the resolution
+ // of each sensor. That's what these functions are for. One for each sensor
+ calcgRes(); // Calculate DPS / ADC tick, stored in gRes variable
+ calcmRes(); // Calculate Gs / ADC tick, stored in mRes variable
+ calcaRes(); // Calculate g / ADC tick, stored in aRes variable
+
+
+ // To verify communication, we can read from the WHO_AM_I register of
+ // each device. Store those in a variable so we can return them.
+ // The start of the addresses we want to read from
+ char cmd[2] = {
+ WHO_AM_I_XG,
+ 0
+ };
+
+ // Write the address we are going to read from and don't end the transaction
+ i2c.write(xgAddress, cmd, 1, true);
+ // Read in all the 8 bits of data
+ i2c.read(xgAddress, cmd+1, 1);
+ uint8_t xgTest = cmd[1]; // Read the accel/gyro WHO_AM_I
+
+ // Reset to the address of the mag who am i
+ cmd[1] = WHO_AM_I_M;
+ // Write the address we are going to read from and don't end the transaction
+ i2c.write(mAddress, cmd, 1, true);
+ // Read in all the 8 bits of data
+ i2c.read(mAddress, cmd+1, 1);
+ uint8_t mTest = cmd[1]; // Read the mag WHO_AM_I
+
+ // Gyro initialization stuff:
+ initGyro(); // This will "turn on" the gyro. Setting up interrupts, etc.
+ setGyroODR(gODR); // Set the gyro output data rate and bandwidth.
+ setGyroScale(gScale); // Set the gyro range
+
+ // Accelerometer initialization stuff:
+ initAccel(); // "Turn on" all axes of the accel. Set up interrupts, etc.
+ setAccelODR(aODR); // Set the accel data rate.
+ setAccelScale(aScale); // Set the accel range.
+
+ // Magnetometer initialization stuff:
+ initMag(); // "Turn on" all axes of the mag. Set up interrupts, etc.
+ setMagODR(mODR); // Set the magnetometer output data rate.
+ setMagScale(mScale); // Set the magnetometer's range.
+
+ // Once everything is initialized, return the WHO_AM_I registers we read:
+ return (xgTest << 8) | mTest;
+}
+
+void LSM9DS1::initGyro()
+{
+ char cmd[4] = {
+ CTRL_REG1_G,
+ gScale | G_ODR_119_BW_14,
+ 0, // Default data out and int out
+ 0 // Default power mode and high pass settings
+ };
+
+ // Write the data to the gyro control registers
+ i2c.write(xgAddress, cmd, 4);
+}
+
+void LSM9DS1::initAccel()
+{
+ char cmd[4] = {
+ CTRL_REG5_XL,
+ 0x38, // Enable all axis and don't decimate data in out Registers
+ (A_ODR_119 << 5) | (aScale << 3) | (A_BW_AUTO_SCALE), // 119 Hz ODR, set scale, and auto BW
+ 0 // Default resolution mode and filtering settings
+ };
+
+ // Write the data to the accel control registers
+ i2c.write(xgAddress, cmd, 4);
+}
+
+void LSM9DS1::initMag()
+{
+ char cmd[4] = {
+ CTRL_REG1_M,
+ 0x10, // Default data rate, xy axes mode, and temp comp
+ mScale << 5, // Set mag scale
+ 0 // Enable I2C, write only SPI, not LP mode, Continuous conversion mode
+ };
+
+ // Write the data to the mag control registers
+ i2c.write(mAddress, cmd, 4);
+}
+
+void LSM9DS1::readAccel()
+{
+ // The data we are going to read from the accel
+ char data[6];
+
+ // The start of the addresses we want to read from
+ char subAddress = OUT_X_L_XL;
+
+ // Write the address we are going to read from and don't end the transaction
+ i2c.write(xgAddress, &subAddress, 1, true);
+ // Read in all 8 bit registers containing the axes data
+ i2c.read(xgAddress, data, 6);
+
+ // Reassemble the data and convert to g
+ ax_raw = data[0] | (data[1] << 8);
+ ay_raw = data[2] | (data[3] << 8);
+ az_raw = data[4] | (data[5] << 8);
+ ax = ax_raw * aRes;
+ ay = ay_raw * aRes;
+ az = az_raw * aRes;
+}
+
+void LSM9DS1::readMag()
+{
+ // The data we are going to read from the mag
+ char data[6];
+
+ // The start of the addresses we want to read from
+ char subAddress = OUT_X_L_M;
+
+ // Write the address we are going to read from and don't end the transaction
+ i2c.write(mAddress, &subAddress, 1, true);
+ // Read in all 8 bit registers containing the axes data
+ i2c.read(mAddress, data, 6);
+
+ // Reassemble the data and convert to degrees
+ mx_raw = data[0] | (data[1] << 8);
+ my_raw = data[2] | (data[3] << 8);
+ mz_raw = data[4] | (data[5] << 8);
+ mx = mx_raw * mRes;
+ my = my_raw * mRes;
+ mz = mz_raw * mRes;
+}
+
+void LSM9DS1::readTemp()
+{
+ // The data we are going to read from the temp
+ char data[2];
+
+ // The start of the addresses we want to read from
+ char subAddress = OUT_TEMP_L;
+
+ // Write the address we are going to read from and don't end the transaction
+ i2c.write(xgAddress, &subAddress, 1, true);
+ // Read in all 8 bit registers containing the axes data
+ i2c.read(xgAddress, data, 2);
+
+ // Temperature is a 12-bit signed integer
+ temperature_raw = data[0] | (data[1] << 8);
+
+ temperature_c = (float)temperature_raw / 8.0 + 25;
+ temperature_f = temperature_c * 1.8 + 32;
+}
+
+
+void LSM9DS1::readGyro()
+{
+ // The data we are going to read from the gyro
+ char data[6];
+
+ // The start of the addresses we want to read from
+ char subAddress = OUT_X_L_G;
+
+ // Write the address we are going to read from and don't end the transaction
+ i2c.write(xgAddress, &subAddress, 1, true);
+ // Read in all 8 bit registers containing the axes data
+ i2c.read(xgAddress, data, 6);
+
+ // Reassemble the data and convert to degrees/sec
+ gx_raw = data[0] | (data[1] << 8);
+ gy_raw = data[2] | (data[3] << 8);
+ gz_raw = data[4] | (data[5] << 8);
+ gx = gx_raw * gRes;
+ gy = gy_raw * gRes;
+ gz = gz_raw * gRes;
+}
+
+void LSM9DS1::setGyroScale(gyro_scale gScl)
+{
+ // The start of the addresses we want to read from
+ char cmd[2] = {
+ CTRL_REG1_G,
+ 0
+ };
+
+ // Write the address we are going to read from and don't end the transaction
+ i2c.write(xgAddress, cmd, 1, true);
+ // Read in all the 8 bits of data
+ i2c.read(xgAddress, cmd+1, 1);
+
+ // Then mask out the gyro scale bits:
+ cmd[1] &= 0xFF^(0x3 << 3);
+ // Then shift in our new scale bits:
+ cmd[1] |= gScl << 3;
+
+ // Write the gyroscale out to the gyro
+ i2c.write(xgAddress, cmd, 2);
+
+ // We've updated the sensor, but we also need to update our class variables
+ // First update gScale:
+ gScale = gScl;
+ // Then calculate a new gRes, which relies on gScale being set correctly:
+ calcgRes();
+}
+
+void LSM9DS1::setAccelScale(accel_scale aScl)
+{
+ // The start of the addresses we want to read from
+ char cmd[2] = {
+ CTRL_REG6_XL,
+ 0
+ };
+
+ // Write the address we are going to read from and don't end the transaction
+ i2c.write(xgAddress, cmd, 1, true);
+ // Read in all the 8 bits of data
+ i2c.read(xgAddress, cmd+1, 1);
+
+ // Then mask out the accel scale bits:
+ cmd[1] &= 0xFF^(0x3 << 3);
+ // Then shift in our new scale bits:
+ cmd[1] |= aScl << 3;
+
+ // Write the accelscale out to the accel
+ i2c.write(xgAddress, cmd, 2);
+
+ // We've updated the sensor, but we also need to update our class variables
+ // First update aScale:
+ aScale = aScl;
+ // Then calculate a new aRes, which relies on aScale being set correctly:
+ calcaRes();
+}
+
+void LSM9DS1::setMagScale(mag_scale mScl)
+{
+ // The start of the addresses we want to read from
+ char cmd[2] = {
+ CTRL_REG2_M,
+ 0
+ };
+
+ // Write the address we are going to read from and don't end the transaction
+ i2c.write(mAddress, cmd, 1, true);
+ // Read in all the 8 bits of data
+ i2c.read(mAddress, cmd+1, 1);
+
+ // Then mask out the mag scale bits:
+ cmd[1] &= 0xFF^(0x3 << 5);
+ // Then shift in our new scale bits:
+ cmd[1] |= mScl << 5;
+
+ // Write the magscale out to the mag
+ i2c.write(mAddress, cmd, 2);
+
+ // We've updated the sensor, but we also need to update our class variables
+ // First update mScale:
+ mScale = mScl;
+ // Then calculate a new mRes, which relies on mScale being set correctly:
+ calcmRes();
+}
+
+void LSM9DS1::setGyroODR(gyro_odr gRate)
+{
+ // The start of the addresses we want to read from
+ char cmd[2] = {
+ CTRL_REG1_G,
+ 0
+ };
+
+ // Write the address we are going to read from and don't end the transaction
+ i2c.write(xgAddress, cmd, 1, true);
+ // Read in all the 8 bits of data
+ i2c.read(xgAddress, cmd+1, 1);
+
+ // Then mask out the gyro odr bits:
+ cmd[1] &= (0x3 << 3);
+ // Then shift in our new odr bits:
+ cmd[1] |= gRate;
+
+ // Write the gyroodr out to the gyro
+ i2c.write(xgAddress, cmd, 2);
+}
+
+void LSM9DS1::setAccelODR(accel_odr aRate)
+{
+ // The start of the addresses we want to read from
+ char cmd[2] = {
+ CTRL_REG6_XL,
+ 0
+ };
+
+ // Write the address we are going to read from and don't end the transaction
+ i2c.write(xgAddress, cmd, 1, true);
+ // Read in all the 8 bits of data
+ i2c.read(xgAddress, cmd+1, 1);
+
+ // Then mask out the accel odr bits:
+ cmd[1] &= 0xFF^(0x7 << 5);
+ // Then shift in our new odr bits:
+ cmd[1] |= aRate << 5;
+
+ // Write the accelodr out to the accel
+ i2c.write(xgAddress, cmd, 2);
+}
+
+void LSM9DS1::setMagODR(mag_odr mRate)
+{
+ // The start of the addresses we want to read from
+ char cmd[2] = {
+ CTRL_REG1_M,
+ 0
+ };
+
+ // Write the address we are going to read from and don't end the transaction
+ i2c.write(mAddress, cmd, 1, true);
+ // Read in all the 8 bits of data
+ i2c.read(mAddress, cmd+1, 1);
+
+ // Then mask out the mag odr bits:
+ cmd[1] &= 0xFF^(0x7 << 2);
+ // Then shift in our new odr bits:
+ cmd[1] |= mRate << 2;
+
+ // Write the magodr out to the mag
+ i2c.write(mAddress, cmd, 2);
+}
+
+void LSM9DS1::calcgRes()
+{
+ // Possible gyro scales (and their register bit settings) are:
+ // 245 DPS (00), 500 DPS (01), 2000 DPS (10).
+ switch (gScale)
+ {
+ case G_SCALE_245DPS:
+ gRes = 245.0 / 32768.0;
+ break;
+ case G_SCALE_500DPS:
+ gRes = 500.0 / 32768.0;
+ break;
+ case G_SCALE_2000DPS:
+ gRes = 2000.0 / 32768.0;
+ break;
+ }
+}
+
+void LSM9DS1::calcaRes()
+{
+ // Possible accelerometer scales (and their register bit settings) are:
+ // 2 g (000), 4g (001), 6g (010) 8g (011), 16g (100).
+ switch (aScale)
+ {
+ case A_SCALE_2G:
+ aRes = 2.0 / 32768.0;
+ break;
+ case A_SCALE_4G:
+ aRes = 4.0 / 32768.0;
+ break;
+ case A_SCALE_8G:
+ aRes = 8.0 / 32768.0;
+ break;
+ case A_SCALE_16G:
+ aRes = 16.0 / 32768.0;
+ break;
+ }
+}
+
+void LSM9DS1::calcmRes()
+{
+ // Possible magnetometer scales (and their register bit settings) are:
+ // 2 Gs (00), 4 Gs (01), 8 Gs (10) 12 Gs (11).
+ switch (mScale)
+ {
+ case M_SCALE_4GS:
+ mRes = 4.0 / 32768.0;
+ break;
+ case M_SCALE_8GS:
+ mRes = 8.0 / 32768.0;
+ break;
+ case M_SCALE_12GS:
+ mRes = 12.0 / 32768.0;
+ break;
+ case M_SCALE_16GS:
+ mRes = 16.0 / 32768.0;
+ break;
+ }
+}
\ No newline at end of file
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/LSM9DS1.h Sun Aug 05 13:15:56 2018 +0000
@@ -0,0 +1,386 @@
+#ifndef _LSM9DS1_H__
+#define _LSM9DS1_H__
+
+#include "mbed.h"
+
+#define Acce_gain_z_2 -9.825751072961373f
+#define Acce_gain_y_2 -9.810204081632654f
+#define Acce_gain_x_2 -9.579654120040694
+#define Acce_gain_z -10.0512295081967213f
+#define Acce_gain_y -9.9090909090909090f
+#define Acce_gain_x -9.4236311239193f
+#define Gyro_gain_x 0.02035972509905115f
+#define Gyro_gain_y 0.01825192507334282f
+#define Gyro_gain_z 0.02154843475f
+#define Gyro_gain_x_2 -0.02035972509905115f
+#define Gyro_gain_y_2 -0.01825192507334282f
+#define Gyro_gain_z_2 -0.02154843475f
+#define pi 3.1415926f
+/////////////////////////////////////////
+// LSM9DS1 Accel/Gyro (XL/G) Registers //
+/////////////////////////////////////////
+#define ACT_THS 0x04
+#define ACT_DUR 0x05
+#define INT_GEN_CFG_XL 0x06
+#define INT_GEN_THS_X_XL 0x07
+#define INT_GEN_THS_Y_XL 0x08
+#define INT_GEN_THS_Z_XL 0x09
+#define INT_GEN_DUR_XL 0x0A
+#define REFERENCE_G 0x0B
+#define INT1_CTRL 0x0C
+#define INT2_CTRL 0x0D
+#define WHO_AM_I_XG 0x0F
+#define CTRL_REG1_G 0x10
+#define CTRL_REG2_G 0x11
+#define CTRL_REG3_G 0x12
+#define ORIENT_CFG_G 0x13
+#define INT_GEN_SRC_G 0x14
+#define OUT_TEMP_L 0x15
+#define OUT_TEMP_H 0x16
+#define STATUS_REG_0 0x17
+#define OUT_X_L_G 0x18
+#define OUT_X_H_G 0x19
+#define OUT_Y_L_G 0x1A
+#define OUT_Y_H_G 0x1B
+#define OUT_Z_L_G 0x1C
+#define OUT_Z_H_G 0x1D
+#define CTRL_REG4 0x1E
+#define CTRL_REG5_XL 0x1F
+#define CTRL_REG6_XL 0x20
+#define CTRL_REG7_XL 0x21
+#define CTRL_REG8 0x22
+#define CTRL_REG9 0x23
+#define CTRL_REG10 0x24
+#define INT_GEN_SRC_XL 0x26
+#define STATUS_REG_1 0x27
+#define OUT_X_L_XL 0x28
+#define OUT_X_H_XL 0x29
+#define OUT_Y_L_XL 0x2A
+#define OUT_Y_H_XL 0x2B
+#define OUT_Z_L_XL 0x2C
+#define OUT_Z_H_XL 0x2D
+#define FIFO_CTRL 0x2E
+#define FIFO_SRC 0x2F
+#define INT_GEN_CFG_G 0x30
+#define INT_GEN_THS_XH_G 0x31
+#define INT_GEN_THS_XL_G 0x32
+#define INT_GEN_THS_YH_G 0x33
+#define INT_GEN_THS_YL_G 0x34
+#define INT_GEN_THS_ZH_G 0x35
+#define INT_GEN_THS_ZL_G 0x36
+#define INT_GEN_DUR_G 0x37
+
+///////////////////////////////
+// LSM9DS1 Magneto Registers //
+///////////////////////////////
+#define OFFSET_X_REG_L_M 0x05
+#define OFFSET_X_REG_H_M 0x06
+#define OFFSET_Y_REG_L_M 0x07
+#define OFFSET_Y_REG_H_M 0x08
+#define OFFSET_Z_REG_L_M 0x09
+#define OFFSET_Z_REG_H_M 0x0A
+#define WHO_AM_I_M 0x0F
+#define CTRL_REG1_M 0x20
+#define CTRL_REG2_M 0x21
+#define CTRL_REG3_M 0x22
+#define CTRL_REG4_M 0x23
+#define CTRL_REG5_M 0x24
+#define STATUS_REG_M 0x27
+#define OUT_X_L_M 0x28
+#define OUT_X_H_M 0x29
+#define OUT_Y_L_M 0x2A
+#define OUT_Y_H_M 0x2B
+#define OUT_Z_L_M 0x2C
+#define OUT_Z_H_M 0x2D
+#define INT_CFG_M 0x30
+#define INT_SRC_M 0x30
+#define INT_THS_L_M 0x32
+#define INT_THS_H_M 0x33
+
+////////////////////////////////
+// LSM9DS1 WHO_AM_I Responses //
+////////////////////////////////
+#define WHO_AM_I_AG_RSP 0x68
+#define WHO_AM_I_M_RSP 0x3D
+
+// Possible I2C addresses for the accel/gyro and mag
+#define LSM9DS1_AG_I2C_ADDR(sa0) ((sa0) ? 0xD6 : 0xD4)
+#define LSM9DS1_M_I2C_ADDR(sa1) ((sa1) ? 0x3C : 0x38)
+
+/**
+ * LSM9DS1 Class - driver for the 9 DoF IMU
+ */
+class LSM9DS1
+{
+public:
+
+ /// gyro_scale defines the possible full-scale ranges of the gyroscope:
+ enum gyro_scale
+ {
+ G_SCALE_245DPS = 0x0 << 3, // 00 << 3: +/- 245 degrees per second
+ G_SCALE_500DPS = 0x1 << 3, // 01 << 3: +/- 500 dps
+ G_SCALE_2000DPS = 0x3 << 3 // 11 << 3: +/- 2000 dps
+ };
+
+ /// gyro_odr defines all possible data rate/bandwidth combos of the gyro:
+ enum gyro_odr
+ { // ODR (Hz) --- Cutoff
+ G_POWER_DOWN = 0x00, // 0 0
+ G_ODR_15_BW_0 = 0x20, // 14.9 0
+ G_ODR_60_BW_16 = 0x40, // 59.5 16
+ G_ODR_119_BW_14 = 0x60, // 119 14
+ G_ODR_119_BW_31 = 0x61, // 119 31
+ G_ODR_238_BW_14 = 0x80, // 238 14
+ G_ODR_238_BW_29 = 0x81, // 238 29
+ G_ODR_238_BW_63 = 0x82, // 238 63
+ G_ODR_238_BW_78 = 0x83, // 238 78
+ G_ODR_476_BW_21 = 0xA0, // 476 21
+ G_ODR_476_BW_28 = 0xA1, // 476 28
+ G_ODR_476_BW_57 = 0xA2, // 476 57
+ G_ODR_476_BW_100 = 0xA3, // 476 100
+ G_ODR_952_BW_33 = 0xC0, // 952 33
+ G_ODR_952_BW_40 = 0xC1, // 952 40
+ G_ODR_952_BW_58 = 0xC2, // 952 58
+ G_ODR_952_BW_100 = 0xC3 // 952 100
+ };
+
+ /// accel_scale defines all possible FSR's of the accelerometer:
+ enum accel_scale
+ {
+ A_SCALE_2G, // 00: +/- 2g
+ A_SCALE_16G,// 01: +/- 16g
+ A_SCALE_4G, // 10: +/- 4g
+ A_SCALE_8G // 11: +/- 8g
+ };
+
+ /// accel_oder defines all possible output data rates of the accelerometer:
+ enum accel_odr
+ {
+ A_POWER_DOWN, // Power-down mode (0x0)
+ A_ODR_10, // 10 Hz (0x1)
+ A_ODR_50, // 50 Hz (0x2)
+ A_ODR_119, // 119 Hz (0x3)
+ A_ODR_238, // 238 Hz (0x4)
+ A_ODR_476, // 476 Hz (0x5)
+ A_ODR_952 // 952 Hz (0x6)
+ };
+
+ // accel_bw defines all possible bandwiths for low-pass filter of the accelerometer:
+ enum accel_bw
+ {
+ A_BW_AUTO_SCALE = 0x0, // Automatic BW scaling (0x0)
+ A_BW_408 = 0x4, // 408 Hz (0x4)
+ A_BW_211 = 0x5, // 211 Hz (0x5)
+ A_BW_105 = 0x6, // 105 Hz (0x6)
+ A_BW_50 = 0x7 // 50 Hz (0x7)
+ };
+
+ /// mag_scale defines all possible FSR's of the magnetometer:
+ enum mag_scale
+ {
+ M_SCALE_4GS, // 00: +/- 4Gs
+ M_SCALE_8GS, // 01: +/- 8Gs
+ M_SCALE_12GS, // 10: +/- 12Gs
+ M_SCALE_16GS, // 11: +/- 16Gs
+ };
+
+ /// mag_odr defines all possible output data rates of the magnetometer:
+ enum mag_odr
+ {
+ M_ODR_0625, // 0.625 Hz (0x00)
+ M_ODR_125, // 1.25 Hz (0x01)
+ M_ODR_25, // 2.5 Hz (0x02)
+ M_ODR_5, // 5 Hz (0x03)
+ M_ODR_10, // 10 (0x04)
+ M_ODR_20, // 20 Hz (0x05)
+ M_ODR_40, // 40 Hz (0x06)
+ M_ODR_80 // 80 Hz (0x07)
+ };
+
+ // We'll store the gyro, accel, and magnetometer readings in a series of
+ // public class variables. Each sensor gets three variables -- one for each
+ // axis. Call readGyro(), readAccel(), and readMag() first, before using
+ // these variables!
+ // These values are the RAW signed 16-bit readings from the sensors.
+ int16_t gx_raw, gy_raw, gz_raw; // x, y, and z axis readings of the gyroscope
+ int16_t ax_raw, ay_raw, az_raw; // x, y, and z axis readings of the accelerometer
+ int16_t mx_raw, my_raw, mz_raw; // x, y, and z axis readings of the magnetometer
+ int16_t temperature_raw;
+
+ // floating-point values of scaled data in real-world units
+ float gx, gy, gz;
+ float ax, ay, az;
+ float mx, my, mz;
+ float temperature_c, temperature_f; // temperature in celcius and fahrenheit
+
+
+ /** LSM9DS1 -- LSM9DS1 class constructor
+ * The constructor will set up a handful of private variables, and set the
+ * communication mode as well.
+ * Input:
+ * - interface = Either MODE_SPI or MODE_I2C, whichever you're using
+ * to talk to the IC.
+ * - xgAddr = If MODE_I2C, this is the I2C address of the accel/gyro.
+ * If MODE_SPI, this is the chip select pin of the accel/gyro (CS_A/G)
+ * - mAddr = If MODE_I2C, this is the I2C address of the mag.
+ * If MODE_SPI, this is the cs pin of the mag (CS_M)
+ */
+ LSM9DS1(PinName sda, PinName scl, uint8_t xgAddr = LSM9DS1_AG_I2C_ADDR(1), uint8_t mAddr = LSM9DS1_M_I2C_ADDR(1));
+
+ /** begin() -- Initialize the gyro, accelerometer, and magnetometer.
+ * This will set up the scale and output rate of each sensor. It'll also
+ * "turn on" every sensor and every axis of every sensor.
+ * Input:
+ * - gScl = The scale of the gyroscope. This should be a gyro_scale value.
+ * - aScl = The scale of the accelerometer. Should be a accel_scale value.
+ * - mScl = The scale of the magnetometer. Should be a mag_scale value.
+ * - gODR = Output data rate of the gyroscope. gyro_odr value.
+ * - aODR = Output data rate of the accelerometer. accel_odr value.
+ * - mODR = Output data rate of the magnetometer. mag_odr value.
+ * Output: The function will return an unsigned 16-bit value. The most-sig
+ * bytes of the output are the WHO_AM_I reading of the accel/gyro. The
+ * least significant two bytes are the WHO_AM_I reading of the mag.
+ * All parameters have a defaulted value, so you can call just "begin()".
+ * Default values are FSR's of: +/- 245DPS, 4g, 2Gs; ODRs of 119 Hz for
+ * gyro, 119 Hz for accelerometer, 80 Hz for magnetometer.
+ * Use the return value of this function to verify communication.
+ */
+ uint16_t begin(gyro_scale gScl = G_SCALE_245DPS,
+ accel_scale aScl = A_SCALE_2G, mag_scale mScl = M_SCALE_4GS,
+ gyro_odr gODR = G_ODR_119_BW_14, accel_odr aODR = A_ODR_119,
+ mag_odr mODR = M_ODR_80);
+
+ /** readGyro() -- Read the gyroscope output registers.
+ * This function will read all six gyroscope output registers.
+ * The readings are stored in the class' gx_raw, gy_raw, and gz_raw variables. Read
+ * those _after_ calling readGyro().
+ */
+ void readGyro();
+
+ /** readAccel() -- Read the accelerometer output registers.
+ * This function will read all six accelerometer output registers.
+ * The readings are stored in the class' ax_raw, ay_raw, and az_raw variables. Read
+ * those _after_ calling readAccel().
+ */
+ void readAccel();
+
+ /** readMag() -- Read the magnetometer output registers.
+ * This function will read all six magnetometer output registers.
+ * The readings are stored in the class' mx_raw, my_raw, and mz_raw variables. Read
+ * those _after_ calling readMag().
+ */
+ void readMag();
+
+ /** readTemp() -- Read the temperature output register.
+ * This function will read two temperature output registers.
+ * The combined readings are stored in the class' temperature variables. Read
+ * those _after_ calling readTemp().
+ */
+ void readTemp();
+
+ /** setGyroScale() -- Set the full-scale range of the gyroscope.
+ * This function can be called to set the scale of the gyroscope to
+ * 245, 500, or 2000 degrees per second.
+ * Input:
+ * - gScl = The desired gyroscope scale. Must be one of three possible
+ * values from the gyro_scale enum.
+ */
+ void setGyroScale(gyro_scale gScl);
+
+ /** setAccelScale() -- Set the full-scale range of the accelerometer.
+ * This function can be called to set the scale of the accelerometer to
+ * 2, 4, 8, or 16 g's.
+ * Input:
+ * - aScl = The desired accelerometer scale. Must be one of five possible
+ * values from the accel_scale enum.
+ */
+ void setAccelScale(accel_scale aScl);
+
+ /** setMagScale() -- Set the full-scale range of the magnetometer.
+ * This function can be called to set the scale of the magnetometer to
+ * 4, 8, 12, or 16 Gs.
+ * Input:
+ * - mScl = The desired magnetometer scale. Must be one of four possible
+ * values from the mag_scale enum.
+ */
+ void setMagScale(mag_scale mScl);
+
+ /** setGyroODR() -- Set the output data rate and bandwidth of the gyroscope
+ * Input:
+ * - gRate = The desired output rate and cutoff frequency of the gyro.
+ * Must be a value from the gyro_odr enum (check above).
+ */
+ void setGyroODR(gyro_odr gRate);
+
+ /** setAccelODR() -- Set the output data rate of the accelerometer
+ * Input:
+ * - aRate = The desired output rate of the accel.
+ * Must be a value from the accel_odr enum (check above).
+ */
+ void setAccelODR(accel_odr aRate);
+
+ /** setMagODR() -- Set the output data rate of the magnetometer
+ * Input:
+ * - mRate = The desired output rate of the mag.
+ * Must be a value from the mag_odr enum (check above).
+ */
+ void setMagODR(mag_odr mRate);
+
+
+private:
+ /** xgAddress and mAddress store the I2C address
+ * for each sensor.
+ */
+ uint8_t xgAddress, mAddress;
+
+ // I2C bus
+ I2C i2c;
+
+ /** gScale, aScale, and mScale store the current scale range for each
+ * sensor. Should be updated whenever that value changes.
+ */
+ gyro_scale gScale;
+ accel_scale aScale;
+ mag_scale mScale;
+
+ /** gRes, aRes, and mRes store the current resolution for each sensor.
+ * Units of these values would be DPS (or g's or Gs's) per ADC tick.
+ * This value is calculated as (sensor scale) / (2^15).
+ */
+ float gRes, aRes, mRes;
+
+ /** initGyro() -- Sets up the gyroscope to begin reading.
+ * This function steps through all three gyroscope control registers.
+ */
+ void initGyro();
+
+ /** initAccel() -- Sets up the accelerometer to begin reading.
+ * This function steps through all accelerometer related control registers.
+ */
+ void initAccel();
+
+ /** initMag() -- Sets up the magnetometer to begin reading.
+ * This function steps through all magnetometer-related control registers.
+ */
+ void initMag();
+
+ /** calcgRes() -- Calculate the resolution of the gyroscope.
+ * This function will set the value of the gRes variable. gScale must
+ * be set prior to calling this function.
+ */
+ void calcgRes();
+
+ /** calcmRes() -- Calculate the resolution of the magnetometer.
+ * This function will set the value of the mRes variable. mScale must
+ * be set prior to calling this function.
+ */
+ void calcmRes();
+
+ /** calcaRes() -- Calculate the resolution of the accelerometer.
+ * This function will set the value of the aRes variable. aScale must
+ * be set prior to calling this function.
+ */
+ void calcaRes();
+};
+
+#endif // _LSM9DS1_H //
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/MX28.lib Sun Aug 05 13:15:56 2018 +0000 @@ -0,0 +1,1 @@ +https://os.mbed.com/users/alan82914/code/MX28/#ca1e55903d09
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/PID.lib Sun Aug 05 13:15:56 2018 +0000 @@ -0,0 +1,1 @@ +http://developer.mbed.org/teams/LDSC_Robotics_TAs/code/PID/#4df4895863cd
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/encoder.cpp Sun Aug 05 13:15:56 2018 +0000
@@ -0,0 +1,56 @@
+#include "mbed.h"
+#include "encoder.h"
+
+DigitalOut encoder_cs(D9);
+//SPI spi_encoder(D11, D12, D13); // mosi, miso, sclk
+SPI spi_encoder(D4, D5, D3); // mosi, miso, sclk
+
+unsigned short encoder_value;
+unsigned short angle = 0;
+unsigned short angle_old;
+int angle_dif;
+int a_dif;
+int Angle = 0;
+unsigned short k = 0;
+
+void init_SPI_encoder()
+{
+ spi_encoder.format(16,3);
+ spi_encoder.frequency(1000000); // 1MHz clock rate
+}
+
+void init_encoder()
+{
+ encoder_cs = 1; // high:disable the device
+}
+
+void angle_measure()
+{
+ encoder_cs = 0; // Select the device by seting chip select low
+ encoder_value = spi_encoder.write(0x00);
+ angle = encoder_value >> 3;
+ encoder_cs = 1; // Deselect the device
+
+ if (k == 0) {
+ Angle = 0;
+ angle_old = angle;
+ k++;
+ } else {
+ angle_dif = angle_count(angle, angle_old);
+ Angle = Angle + angle_dif;
+ angle_old = angle;
+ }
+}
+
+int angle_count(unsigned short now,unsigned short old)
+{
+ a_dif = now - old;
+ if (a_dif > 4096/2) {
+ a_dif = -(4096 - a_dif);
+ } else if (a_dif < -4096/2) {
+ a_dif = 4096 + a_dif;
+ } else {
+ a_dif = a_dif;
+ }
+ return a_dif;
+}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/encoder.h Sun Aug 05 13:15:56 2018 +0000 @@ -0,0 +1,23 @@ +#ifndef _ENCODER_H_ +#define _ENCODER_H_ + +#include "mbed.h" + +extern void init_SPI_encoder(); +extern void init_encoder(); +extern void angle_measure(); +extern int angle_count(unsigned short,unsigned short); + +extern DigitalOut encoder_cs; +extern SPI spi_encoder; + +extern unsigned short encoder_value; +extern unsigned short angle; +extern unsigned short angle_old; +extern int angle_dif; +extern int a_dif; +extern int Angle; +extern unsigned short k; + + +#endif \ No newline at end of file
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/main.cpp Sun Aug 05 13:15:56 2018 +0000
@@ -0,0 +1,209 @@
+#include "mbed.h"
+#include "encoder.h"
+#include "Mx28.h"
+#include "PID.h"
+
+//********************* Dynamxiel ******************************
+#define SERVO_ID 0x01 // ID of which we will set Dynamixel too
+#define SERVO_ControlPin A2 // Control pin of buffer chip, NOTE: this does not matter becasue we are not using a half to full contorl buffer.
+#define SERVO_SET_Baudrate 1000000 // Baud rate speed which the Dynamixel will be set too (1Mbps)
+#define TxPin A0
+#define RxPin A1
+#define CW_LIMIT_ANGLE 1 // lowest clockwise angle is 1, as when set to 0 it set servo to wheel mode
+#define CCW_LIMIT_ANGLE 4095 // Highest anit-clockwise angle is 0XFFF, as when set to 0 it set servo to wheel mode
+#define PI 3.14159265f
+//***************************************************************
+
+Serial uart(USBTX, USBRX);
+//Serial uart(D10,D2); // TX : D10 RX : D2 // blueteeth
+DigitalOut LED(A4); // check if the code is running
+DigitalOut led2(A5); // check if the code is running interrupt
+uint8_t led2f;
+
+// Timer
+Ticker timer1;
+float ITR_time1 = 10000.0; // unit:us
+float Ts = ITR_time1/1000000;
+uint8_t flag;
+
+// uart_tx
+union splitter {
+ short j;
+ char C[2];
+ // C[0] is lowbyte of j, C[1] is highbyte of j
+};
+char T[16] = {255,255,0,0,0,0,0,0,0,0,0,0,0,0,0,0};
+int i = 0;
+
+// PID
+PID motor_pid(7.2, 0, 0.13, Ts);// 6.4 0.13 7.2 0.13
+float PIDout = 0.0f;
+
+// Dynamixel
+DynamixelClass dynamixelClass(SERVO_SET_Baudrate,SERVO_ControlPin,TxPin,RxPin);
+int servo_cmd;
+int D_angle = 0;
+int D_angle_dif = 0;
+int D_Angle;
+int D_angle_old;
+unsigned short d = 0;
+// Find Torque
+double angle_difference = 0.0;
+float torque_measured = 0.0;
+float ks = 2.6393*2; //spring constant
+//int angle_dif = 0;
+float torque_ref = 0.0;
+float friction = 0.18f;
+float rate = 0.8;
+//float friction = 0.0f;
+//float check = 0.0f;
+
+// function
+void init_UART();
+void init_TIMER();
+void timer1_ITR();
+void uart_tx();
+
+void init_DYNAMIXEL();
+void D_angle_measure();
+void find_torque();
+
+int main()
+{
+ LED = 1; // darken
+ wait_ms(500);
+ // initial sensor
+ init_SPI_encoder();
+ init_encoder();
+ init_DYNAMIXEL();
+ // initial uart
+ init_UART();
+
+ wait_ms(500);
+
+ led2 = 1;
+ led2f = 0;
+ LED = 0; // lighten
+
+ init_TIMER();
+
+ while(1) {
+ if (flag==1) {
+ led2 = !led2;
+ angle_measure();
+ D_angle_measure();
+ find_torque();
+ motor_pid.Compute(torque_ref, torque_measured);
+ PIDout = motor_pid.output;
+ servo_cmd = PIDout*121.8f; // 1023/8.4Nm = 121.7857
+
+ if (servo_cmd > 0) {
+ servo_cmd = servo_cmd + ((-torque_ref)*rate+friction)*121.8f;
+ if (servo_cmd >= 1023)
+ servo_cmd = 1023;
+ } else {
+ servo_cmd = -servo_cmd + 1024 + ((torque_ref)*rate+friction)*121.8f;
+ if (servo_cmd >= 2047)
+ servo_cmd = 2047;
+ }
+// dynamixelClass.torque(SERVO_ID, servo_cmd);
+ uart_tx();
+ flag = 0;
+ }
+ }
+}
+
+void init_DYNAMIXEL()
+{
+ dynamixelClass.torqueMode(SERVO_ID, 1);
+ wait_ms(1);
+}
+
+void init_UART()
+{
+ uart.baud(115200);
+}
+
+void init_TIMER()
+{
+ timer1.attach_us(&timer1_ITR, ITR_time1);
+}
+
+void timer1_ITR()
+{
+ flag = 1;
+ if (led2f == 5) {
+
+ led2f = 0;
+ } else {
+ led2f++;
+ }
+}
+
+void uart_tx()
+{
+ splitter s1;
+ splitter s2;
+ splitter s3;
+ splitter s4;
+ splitter s5;
+ splitter s6;
+ splitter s7;
+
+ s1.j = angle;
+ s2.j = Angle;
+ s3.j = 2;
+// s4.j = 1;
+// s5.j = 3;
+ s4.j = D_angle;
+ s5.j = D_Angle;
+ s6.j = torque_measured*1000;
+ s7.j = 3;
+
+ T[2] = s1.C[0];
+ T[3] = s1.C[1];
+ T[4] = s2.C[0];
+ T[5] = s2.C[1];
+ T[6] = s3.C[0];
+ T[7] = s3.C[1];
+ T[8] = s4.C[0];
+ T[9] = s4.C[1];
+ T[10] = s5.C[0];
+ T[11] = s5.C[1];
+ T[12] = s6.C[0];
+ T[13] = s6.C[1];
+ T[14] = s7.C[0];
+ T[15] = s7.C[1];
+
+ while(1) {
+ if (uart.writeable() == 1) {
+ uart.putc(T[i]);
+ i++;
+ }
+ if (i >= (sizeof(T)-1)) {
+ i = 0;
+ break;
+ }
+ }
+}
+
+void D_angle_measure()
+{
+ D_angle = dynamixelClass.readPosition(SERVO_ID);
+
+ if (d == 0) {
+ D_Angle = 0;
+ D_angle_old = D_angle;
+ d++;
+ } else {
+ D_angle_dif = D_angle - D_angle_old;
+ D_Angle = D_Angle + D_angle_dif;
+ D_angle_old = D_angle;
+ }
+}
+
+void find_torque()
+{
+ angle_difference = (Angle*3-D_Angle)/4096.0f*2*PI;
+ torque_measured = angle_difference*ks;
+}
\ No newline at end of file
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/mbed.bld Sun Aug 05 13:15:56 2018 +0000 @@ -0,0 +1,1 @@ +https://os.mbed.com/users/mbed_official/code/mbed/builds/25aea2a3f4e3 \ No newline at end of file