Hi,
I'm working on a data collection device and am using the teensy 4.1 for its fast performance rate. However, while working on my project I have hit two road blocks:
1) The accelerometer I have been provided (Adafruit MMA8451) seems to cap at a sampling rate of 800Hz (from experiment and from the data sheet), however ideally I would want a sampling rate of 1.67kHz.
2) The SD card seems to cap at an upload rate of 500Hz, and I'm not sure how I would manage to be able to upload all the data I'm capturing then.
Are there any workarounds for this? I'm pretty new to microcontrollers as a whole so any advice would be appreciated. I've looked a bit into data buffers and SPI commands, but I'm still not completely sure on whether or not they could help improve my upload rates.
The code I have so far is provided below, but it's mostly just set up and functions given that I've mainly just been gathering data on the performance of the device. Any advice would be greatly appreciated!
I'm working on a data collection device and am using the teensy 4.1 for its fast performance rate. However, while working on my project I have hit two road blocks:
1) The accelerometer I have been provided (Adafruit MMA8451) seems to cap at a sampling rate of 800Hz (from experiment and from the data sheet), however ideally I would want a sampling rate of 1.67kHz.
2) The SD card seems to cap at an upload rate of 500Hz, and I'm not sure how I would manage to be able to upload all the data I'm capturing then.
Are there any workarounds for this? I'm pretty new to microcontrollers as a whole so any advice would be appreciated. I've looked a bit into data buffers and SPI commands, but I'm still not completely sure on whether or not they could help improve my upload rates.
The code I have so far is provided below, but it's mostly just set up and functions given that I've mainly just been gathering data on the performance of the device. Any advice would be greatly appreciated!
Code:
#include <Adafruit_MAX31856.h>
#include <Wire.h>
#include <Adafruit_MMA8451.h>
#include <Adafruit_Sensor.h>
#include <SD.h>
#define DRDY_PIN 5
// Use software SPI: CS, DI, DO, CLK
//Adafruit_MAX31856 maxthermo = Adafruit_MAX31856(10, 11, 12, 13);
// use hardware SPI, just pass in the CS pin
Adafruit_MAX31856 maxthermo = Adafruit_MAX31856(10);
Adafruit_MMA8451 mma = Adafruit_MMA8451();
// GLOBAL VARIABLES ****************
const int chipSelect = BUILTIN_SDCARD;
const int arraySize = 32;
float temp_buffer[arraySize] = {1};
float accel_buffer[arraySize] = {1};
float freq_buffer[arraySize] = {1};
const char filename[] = "data.csv";
File dataFile;
//**********************************
//##################################
//**********TEENSY SETUP************
void setup() {
// put your setup code here, to run once:
Serial.begin(115200);
while (!Serial) delay(10);
Serial.println("Testing MAX31856 and MMA8451");
//********************************* MMA8451 SETUP **************************************************************************
if (! mma.begin()) {
Serial.println("MMA8451 couldnt start");
while (1);
}
Serial.println("MMA8451 found!");
mma.setRange(MMA8451_RANGE_2_G);
Serial.print("Range = "); Serial.print(2 << mma.getRange());
Serial.println("G");
//**************************************************************************************************************************
//##########################################################################################################################
//******************************* MAX31856 SETUP ***************************************************************************
if (!maxthermo.begin()) {
Serial.println("Could not initialize thermocouple.");
while (1) delay(10);
}
maxthermo.setThermocoupleType(MAX31856_TCTYPE_K);
Serial.print("Thermocouple type: ");
switch (maxthermo.getThermocoupleType() ) {
case MAX31856_TCTYPE_B: Serial.println("B Type"); break;
case MAX31856_TCTYPE_E: Serial.println("E Type"); break;
case MAX31856_TCTYPE_J: Serial.println("J Type"); break;
case MAX31856_TCTYPE_K: Serial.println("K Type"); break;
case MAX31856_TCTYPE_N: Serial.println("N Type"); break;
case MAX31856_TCTYPE_R: Serial.println("R Type"); break;
case MAX31856_TCTYPE_S: Serial.println("S Type"); break;
case MAX31856_TCTYPE_T: Serial.println("T Type"); break;
case MAX31856_VMODE_G8: Serial.println("Voltage x8 Gain mode"); break;
case MAX31856_VMODE_G32: Serial.println("Voltage x8 Gain mode"); break;
default: Serial.println("Unknown"); break;
}
maxthermo.setConversionMode(MAX31856_CONTINUOUS);
/*
while(maxthermo.readThermocoupleTemperature() == 0){
Serial.println("Working on retrieving proper data...");
delay(100);
}
*/
//*************************************************************************************************************************
//#########################################################################################################################
//******************************SD CARD INITIALIZATION*********************************************************************
// init the SD card
if (!SD.begin(chipSelect)) {
Serial.println("Card failed, or not present");
// don't do anything more:
while (1);
}
else {
Serial.println("Found the card!");
}
// If you want to start from an empty file,
// uncomment the next line:
//SD.remove(filename);
// try to open the file for writing
dataFile = SD.open(filename, O_CREAT | O_WRITE);
if (!dataFile) {
Serial.print("error opening ");
Serial.println(filename);
while (1);
}
//*************************************************************************************************************************
}
void loop() {
/*fillBuffers();
writeToFile();
float temp_buffer[arraySize] = {-280};
float accel_buffer[arraySize] = {-1};
float freq_buffer[arraySize] = {-1};*/
int beforeTime = micros();
writeToFile();
//float dataCheck = accelVal();
//Serial.println(dataCheck);
int afterTime = micros();
int timeDiff = abs(afterTime - beforeTime);
//Serial.println("time diff: " + String(timeDiff));
}
void fillBuffers(){
for(int i = 0; i < arraySize; i++){
temp_buffer[i] = tempVal();
accel_buffer[i] = accelVal();
}
}
float accelVal() {
/* Get a new sensor event */
sensors_event_t event;
mma.getEvent(&event);
float x_accel = event.acceleration.x, y_accel = event.acceleration.y, z_accel = event.acceleration.z;
/* Display the results (acceleration is measured in m/s^2) */
/*
Serial.print("X: \t"); Serial.print(event.acceleration.x); Serial.print("\t");
Serial.print("Y: \t"); Serial.print(event.acceleration.y); Serial.print("\t");
Serial.print("Z: \t"); Serial.print(event.acceleration.z); Serial.print("\t");
Serial.println("m/s^2 ");
*/
/*
// Read the 'raw' data in 14-bit counts
mma.read();
Serial.print("X:\t"); Serial.print(mma.x);
Serial.print("\tY:\t"); Serial.print(mma.y);
Serial.print("\tZ:\t"); Serial.print(mma.z);
Serial.println();
*/
float accelScalar = sqrt(pow(x_accel, 2) + pow(y_accel, 2) + pow(z_accel, 2));
return accelScalar;
}
float tempVal(){
int count = 0;
while (digitalRead(DRDY_PIN)) {
if (count++ > 200) {
count = 0;
//Serial.print(".");
}
}
//Serial.println(maxthermo.readThermocoupleTemperature());
return float(maxthermo.readThermocoupleTemperature());
}
String formatData(float accelerationValue, float temperatureValue){
String accel = String(accelerationValue);
String temperature = String(temperatureValue);
String dataString = accel + "," + temperature;
return dataString;
}
void writeToFile(){
for(int i = 0; i < arraySize; i++){
int beforeTime = micros();
String dataString = formatData(temp_buffer[i], accel_buffer[i]);
dataFile.println(dataString);
dataFile.flush();
int afterTime = micros();
int timeDiff = afterTime - beforeTime;
Serial.println(timeDiff);
}
}