/*
SD card datalogger
This example shows how to log data from three analog sensors
to an SD card using the SD library.
The circuit:
* analog sensors on analog ins 0, 1, and 2
* SD card attached to SPI bus as follows:
** MOSI - pin 11
** MISO - pin 12
** CLK - pin 13
** CS - pin 4 (for MKRZero SD: SDCARD_SS_PIN)
created 24 Nov 2010
modified 9 Apr 2012
by Tom Igoe
This example code is in the public domain.
*/
#include <SPI.h>
#include <SD.h>
#include "MPU9250.h"
#define AHRS true // Set to false for basic data read
#define SerialDebug true // Set to true to get Serial output for debugging
// Pin definitions
int intPin = 12; // These can be changed, 2 and 3 are the Arduinos ext int pins
int myLed = 13; // Set up pin 13 led for toggling
MPU9250 myIMU;
const int chipSelect = 8;
File dataFile;
void setup() {
// Open serial communications and wait for port to open:
Wire.begin();
Serial.begin(38400);
//while (!Serial) {
; // wait for serial port to connect. Needed for native USB port only
pinMode(intPin, INPUT);
digitalWrite(intPin, LOW);
pinMode(myLed, OUTPUT);
digitalWrite(myLed, HIGH);
myIMU.initAK8963(myIMU.magCalibration);
//}
// Max out the gyroscope range
myIMU.writeByte(MPU9250_ADDRESS, 27, 0b00011000);
// Max out the accelerometer range
//myIMU.writeByte(MPU9250_ADDRESS, 28, 0b00011000);
Serial.print("Initializing SD card...");
// 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:
while (1);
}
Serial.println("card initialized.");
dataFile = SD.open("datalog.csv", FILE_WRITE);
}
void loop() {
long t = millis();
if (myIMU.readByte(MPU9250_ADDRESS, INT_STATUS) & 0x01)
{
myIMU.readAccelData(myIMU.accelCount); // Read the x/y/z adc values
myIMU.getAres();
// Now we'll calculate the accleration value into actual g's
// This depends on scale being set
myIMU.ax = (float)myIMU.accelCount[0]*myIMU.aRes; // - accelBias[0];
myIMU.ay = (float)myIMU.accelCount[1]*myIMU.aRes; // - accelBias[1];
myIMU.az = (float)myIMU.accelCount[2]*myIMU.aRes; // - accelBias[2];
myIMU.readGyroData(myIMU.gyroCount); // Read the x/y/z adc values
myIMU.getGres();
// Calculate the gyro value into actual degrees per second
// This depends on scale being set
myIMU.gx = (float)myIMU.gyroCount[0];
myIMU.gy = (float)myIMU.gyroCount[1];
myIMU.gz = (float)myIMU.gyroCount[2];
myIMU.readMagData(myIMU.magCount); // Read the x/y/z adc values
myIMU.getMres();
// User environmental x-axis correction in milliGauss, should be
// automatically calculated
myIMU.magbias[0] = +470.;
// User environmental x-axis correction in milliGauss TODO axis??
myIMU.magbias[1] = +120.;
// User environmental x-axis correction in milliGauss
myIMU.magbias[2] = +125.;
myIMU.mx = (float)myIMU.magCount[0]*myIMU.mRes*myIMU.magCalibration[0] -
myIMU.magbias[0];
myIMU.my = (float)myIMU.magCount[1]*myIMU.mRes*myIMU.magCalibration[1] -
myIMU.magbias[1];
myIMU.mz = (float)myIMU.magCount[2]*myIMU.mRes*myIMU.magCalibration[2] -
myIMU.magbias[2];
}
myIMU.updateTime();
// make a string for assembling the data to log:
/*String dataString = "";
// read three sensors and append to the string:
for (int analogPin = 0; analogPin < 3; analogPin++) {
int sensor = analogRead(analogPin);
dataString += String(sensor);
if (analogPin < 2) {
dataString += ",";
}
}*/
String ax = String(myIMU.ax);
String ay = String(myIMU.ay);
String az = String(myIMU.az);
String gx = String(myIMU.gx);
String gy = String(myIMU.gy);
String gz = String(myIMU.gz);
String mx = String(myIMU.mx);
String my = String(myIMU.my);
String mz = String(myIMU.mz);
String data = String(ax+","+ay+","+az+","+gx+","+gy+","+gz/*+","+mx+","+my+","+mz*/);
// open the file. note that only one file can be open at a time,
// so you have to close this one before opening another.
// if the file is available, write to it:
if (dataFile) {
dataFile.println(data);
// print to the serial port too:
Serial.print(data);
Serial.print(" ");
Serial.println(myIMU.gRes);
}
// if the file isn't open, pop up an error:
else {
Serial.println("error opening datalog.txt");
}
if (t>15000){
dataFile.close();
}
}