1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162
|
#include <Wire.h>
#include "I2Cdev.h"
#include "MPU9150Lib.h"
#include "CalLib.h"
#include <dmpKey.h>
#include <dmpmap.h>
#include <inv_mpu.h>
#include <inv_mpu_dmp_motion_driver.h>
#include <EEPROM.h>
#include <SD.h> //Includes SD shield library
// DEVICE_TO_USE selects whether the IMU at address 0x68 (default) or 0x69 is used
// 0 = use the device at 0x68
// 1 = use the device at ox69
#define DEVICE_TO_USE 0
MPU9150Lib MPU; // the MPU object
// MPU_UPDATE_RATE defines the rate (in Hz) at which the MPU updates the sensor data and DMP output
#define MPU_UPDATE_RATE (20)
// MAG_UPDATE_RATE defines the rate (in Hz) at which the MPU updates the magnetometer data
// MAG_UPDATE_RATE should be less than or equal to the MPU_UPDATE_RATE
#define MAG_UPDATE_RATE (10)
// MPU_MAG_MIX defines the influence that the magnetometer has on the yaw output.
// The magnetometer itself is quite noisy so some mixing with the gyro yaw can help
// significantly. Some example values are defined below:
#define MPU_MAG_MIX_GYRO_ONLY 0 // just use gyro yaw
#define MPU_MAG_MIX_MAG_ONLY 1 // just use magnetometer and no gyro yaw
#define MPU_MAG_MIX_GYRO_AND_MAG 10 // a good mix value (uses this value)
#define MPU_MAG_MIX_GYRO_AND_SOME_MAG 50 // mainly gyros with a bit of mag correction
// MPU_LPF_RATE is the low pas filter rate and can be between 5 and 188Hz
#define MPU_LPF_RATE 40
// SERIAL_PORT_SPEED defines the speed to use for the debug serial port
#define SERIAL_PORT_SPEED 115200
// Chip Select pin is tied to pin 8 on the SparkFun SD Card Shield
//(may have to connect to different Mega pin though
const int chipSelect = 8;
void setup()
{
// Serial port information
Serial.begin(SERIAL_PORT_SPEED);
Serial.print("Arduino9150 starting using device "); Serial.println(DEVICE_TO_USE);
// Start up the MPU device
Wire.begin();
MPU.selectDevice(DEVICE_TO_USE); // only really necessary if using device 1
MPU.init(MPU_UPDATE_RATE, MPU_MAG_MIX_GYRO_AND_MAG, MAG_UPDATE_RATE, MPU_LPF_RATE); // start the MPU
//Start up SD shield
Serial.print("Initializing SD card...");
// make sure that the default chip select pin is set to
// output, even if you don't use it:
pinMode(chipSelect, OUTPUT);
// see if the card is present and can be initialized:
if (!SD.begin(chipSelect)) {
Serial.println("Card failed, or not present");
// don't do anything more:
return;
Serial.println("card initialized.");
}
// Writes open the csv file, prepares column headings
File dataFile = SD.open("DATA.csv", FILE_WRITE);
if (dataFile) {
dataFile.println(",,,,,");
String header = "AccX,AccY,AccZ,AngleX,AngleY,AngleZ";
dataFile.println(header);
dataFile.close();
Serial.println();
Serial.println(header);
} else {
Serial.println("Couldn't open log file");
}
}
void loop()
{
// make a string for assembling the data to log:
String dataString = "";
// Reads data from MPU9150Lib and appends it to the string
if (MPU.read()) // get the latest data if ready yet
{
float AccX = MPU.printVector(MPU.m_calAccel[0]);
dataString += String(AccX);
float AccY = MPU.printVector(MPU.m_calAccel[1]);
dataString += String(AccY);
float AccZ = MPU.printVector(MPU.m_calAccel[2]);
dataString += String(AccZ);
float AngleX = MPU.printAngles(MPU.m_fusedEulerPose[0]);
dataString += String(AccX);
float AngleY = MPU.printAngles(MPU.m_fusedEulerPose[1]);
dataString += String(AccY);
float AngleZ = MPU.printAngles(MPU.m_fusedEulerPose[2]);
dataString += String(AccZ);
}
// open the file. note that only one file can be open at a time,
// so you have to close this one before opening another.
File dataFile = SD.open("DATA.csv", FILE_WRITE);
// if the file is available, write to it:
if (dataFile) {
dataFile.println(dataString);
dataFile.close();
// print to the serial port too:
Serial.println(dataString);
}
// if the file isn't open, pop up an error:
else {
Serial.println("error opening datalog.txt");
}
}
|