117 lines
3 KiB
C++
117 lines
3 KiB
C++
/* This is a accelerometer recorder using a raspi pico
|
|
* and a MPU6050 accelerometer. The accelerometer is connected
|
|
* with its SCL and SDA to GP21 and GP20 respectively. Wires
|
|
* to it should be somewhat short: We are using an i2c clock of
|
|
* 2.5MHz.
|
|
* Data is transmitted over USB-Serial at 8Mbaud in CSV format
|
|
* and can immediately be read by infraconv.
|
|
*
|
|
* Note: This file is to be flashed with the arduino IDE, with
|
|
* the arduino-pico board extension, not with the default
|
|
* Arduino-MbedOS for the pico.
|
|
*
|
|
* All data recorded using this will be at 1kHz sample rate,
|
|
* meaning the maximum possible frequency in the recording is
|
|
* 500Hz.
|
|
*/
|
|
|
|
#include <Wire.h>
|
|
|
|
#define ENABLE_LPF false
|
|
#define addr 0x68
|
|
|
|
#define FIFO_EN 0x23
|
|
#define USER_CTRL 0x6A
|
|
#define FIFO_DATA 0x74
|
|
#define ACCEL_CONFIG 0x1C
|
|
#define SMPRT_DIV 0x19
|
|
#define PWR_MGMT_1 0x6B
|
|
#define FIFO_COUNTH 0x72
|
|
#define FIFO_COUNTL 0x73
|
|
#define CONFIG 0x1A
|
|
#define SETREG(REG, VAL) Wire.beginTransmission(addr); Wire.write(REG); Wire.write(VAL); Wire.endTransmission(true);
|
|
|
|
void setup() {
|
|
pinMode(18, OUTPUT);
|
|
pinMode(19, OUTPUT);
|
|
digitalWrite(19, HIGH);
|
|
Wire.setSCL(21);
|
|
Wire.setSDA(20);
|
|
Wire.begin();
|
|
Wire.setClock(200000);
|
|
|
|
Serial.begin(8000000); // be fast. we are transmitting csv which is inefficient as fuck
|
|
|
|
delay(2000);
|
|
SETREG(PWR_MGMT_1, 0b10000000); // reset
|
|
delay(500);
|
|
SETREG(USER_CTRL, 0b00000100); // reset fifo
|
|
delay(100);
|
|
SETREG(PWR_MGMT_1, 0); // all on
|
|
SETREG(ACCEL_CONFIG, 0b00000000); // +-2g
|
|
if (ENABLE_LPF) {
|
|
SETREG(CONFIG, 6); // DLPF strength 2
|
|
} else {
|
|
SETREG(SMPRT_DIV, 7); // 8kHz / (1 + 7) = 1kHz
|
|
}
|
|
delay(1000);
|
|
SETREG(FIFO_EN, 0b00001000); // only accel bit
|
|
SETREG(USER_CTRL, 0b01000000); // only FIFO_EN bit
|
|
|
|
pinMode(LED_BUILTIN, OUTPUT);
|
|
digitalWrite(LED_BUILTIN, HIGH);
|
|
Wire.setClock(2500000);
|
|
}
|
|
|
|
uint16_t get_fifo_len() {
|
|
Wire.beginTransmission(addr);
|
|
Wire.write(FIFO_COUNTH);
|
|
Wire.endTransmission(false);
|
|
Wire.requestFrom(addr, 2, true);
|
|
return (((uint16_t) Wire.read()) << 8) | Wire.read();
|
|
}
|
|
|
|
int axis = 0;
|
|
int axes = 3;
|
|
|
|
void read_fifo() {
|
|
uint16_t fifolen = get_fifo_len();
|
|
//Serial.println(fifolen);
|
|
if(fifolen < 2 * 3) {
|
|
return;
|
|
}
|
|
fifolen /= 2 * 3;
|
|
Wire.beginTransmission(addr);
|
|
Wire.write(FIFO_DATA);
|
|
Wire.endTransmission(false);
|
|
Wire.requestFrom(addr, min(fifolen * 2 * 3, 256), true);
|
|
for (int i = 0; i < min(fifolen, 128); i++) {
|
|
axis = 0;
|
|
printNext(((uint16_t) Wire.read() << 8) | Wire.read());
|
|
printNext(((uint16_t) Wire.read() << 8) | Wire.read());
|
|
printNext(((uint16_t) Wire.read() << 8) | Wire.read());
|
|
}
|
|
Wire.endTransmission(true);
|
|
//SETREG(USER_CTRL, 0b00000100); // reset fifo
|
|
}
|
|
|
|
void loop() {
|
|
if(Serial.availableForWrite() < 100) {
|
|
return;
|
|
}
|
|
read_fifo();
|
|
Serial.flush();
|
|
}
|
|
|
|
void printNext(int16_t i) {
|
|
//return;
|
|
if(axis != 0) {
|
|
Serial.print(",");
|
|
}
|
|
Serial.print(i);
|
|
if(++axis >= axes) {
|
|
axis = 0;
|
|
Serial.print("\r\n");
|
|
}
|
|
}
|
|
|