snowskijunky
Active member
So my goal here is to replace the electronics of a model train. I'm using a teensy 3.6+prop shield so that later on I can hook up a motor controller and RFID reader that will make use of the teensy 3.6's extra I2c and/or SPI ports (plus it already has an on board sd card).
I'd like to be able to calculate velocity and distance traveled so that I can play a chuffing sound every "x amount" distance traveled. That amount will be determined by the circumference of the locomotive's wheels so that I can play 4 chuffs per revolution of the driving wheel. I am wondering if it is possible to calculate velocity and distance from the accelerometer and what the best way to go about doing it would be, if there are any examples, etc. The calibration seems to have worked well (it came out rather spherical) but the acceleration values I'm reading are jumpy, and the heading reading from the orientation is constantly changing despite the device being still.
Any help or guidance here would be much appreciated. Thanks in advance!
I'd like to be able to calculate velocity and distance traveled so that I can play a chuffing sound every "x amount" distance traveled. That amount will be determined by the circumference of the locomotive's wheels so that I can play 4 chuffs per revolution of the driving wheel. I am wondering if it is possible to calculate velocity and distance from the accelerometer and what the best way to go about doing it would be, if there are any examples, etc. The calibration seems to have worked well (it came out rather spherical) but the acceleration values I'm reading are jumpy, and the heading reading from the orientation is constantly changing despite the device being still.
Code:
// Full orientation sensing using NXP's advanced sensor fusion algorithm.
//
// You *must* perform a magnetic calibration before this code will work.
//
// To view this data, use the Arduino Serial Monitor to watch the
// scrolling angles, or run the OrientationVisualiser example in Processing.
#include <NXPMotionSense.h>
#include <Wire.h>
#include <EEPROM.h>
NXPMotionSense imu;
NXPSensorFusion filter;
void setup() {
Serial.begin(9600);
imu.begin();
filter.begin(100);
}
void loop() {
float ax, ay, az;
float gx, gy, gz;
float mx, my, mz;
float roll, pitch, heading;
if (imu.available()) {
// Read the motion sensors
imu.readMotionSensor(ax, ay, az, gx, gy, gz, mx, my, mz);
// Update the SensorFusion filter
filter.update(gx, gy, gz, ax, ay, az, mx, my, mz);
// print the heading, pitch and roll
roll = filter.getRoll();
pitch = filter.getPitch();
heading = filter.getYaw();
Serial.print("Orientation: ");
Serial.print(ax);
Serial.print(" ");
Serial.print(ay);
Serial.print(" ");
Serial.println(az);
}
}
Any help or guidance here would be much appreciated. Thanks in advance!
Last edited: