Max Msp Arduino Mpu6050 Accelerometer Maxuino
Hey Max/Msp Community,
I am searching for a way to read out the accelerometer and gyroscope data from my MPU6050 in Max/Msp.
My Hardware:
MPU6050
Lattepanda with included Arduino Leonardo
The problem is. Which arduino code should i use and how do i read out the data in max then. I tried various ways so far.
1. I tried to use this description. http://geps.synack.ch/documentation.html
I installed the I2c and the mpu9150 library and run this arduino code. There is an additional puredata code in this example, but unfortunately i get some serial status error while trying to connect in puredata. I was not able to translate the pure data code to max/msp. How do I get the data into max/msp and how do i interpret them?
#include "I2Cdev.h"
#include "MPU9150.h"
#if I2CDEV_IMPLEMENTATION == I2CDEV_ARDUINO_WIRE
#include "Wire.h"
#endif
///// INITIALIZE //////
// MPU
MPU9150 mpu;
// the 6 values of the MPU
int16_t ax, ay, az;
int16_t gx, gy, gz;
// bit startpoint for parsing the serial data
uint8_t start = 255;
// MSB/LSB variables for encapsulation
uint8_t msbax, lsbax, msbay, lsbay, msbaz, lsbaz;
uint8_t msbgx, lsbgx, msbgy, lsbgy, msbgz, lsbgz;
///// XBEE SERIAL CONNECTION //////
// Include the software serial port library
#include
// DIO used to communicate with the Bluetooth module's TXD pin
#define BT_SERIAL_TX_DIO 10
// DIO used to communicate with the Bluetooth module's RXD pin
#define BT_SERIAL_RX_DIO 11
// Initialise the software serial port
SoftwareSerial XBeeSerial(BT_SERIAL_TX_DIO, BT_SERIAL_RX_DIO);
void setup()
{
// join I2C bus (I2Cdev library doesn't do this automatically)
#if I2CDEV_IMPLEMENTATION == I2CDEV_ARDUINO_WIRE
Wire.begin();
#elif I2CDEV_IMPLEMENTATION == I2CDEV_BUILTIN_FASTWIRE
Fastwire::setup(400, true);
#endif
// begin Software Serial Port
XBeeSerial.begin(38400);
// initialize device
mpu.initialize();
mpu.setFullScaleGyroRange(MPU9150_GYRO_FS_2000); // default: MPU6050_GYRO_FS_250
mpu.setFullScaleAccelRange(MPU9150_ACCEL_FS_8);
}
void loop()
{
// read raw accel/gyro measurements from device
mpu.getMotion6(&ax, &ay, &az, &gx, &gy, &gz);
// convert sensor values from int to uint (0...65535)
ax += 32768;
ay += 32768;
az += 32768;
gx += 32768;
gy += 32768;
gz += 32768;
// cut off highest values (2^16-1, reserved for the "start" bytes)
if (ax == 65535) ax = 65534;
if (ay == 65535) ay = 65534;
if (az == 65535) az = 65534;
if (gx == 65535) gx = 65534;
if (gy == 65535) gy = 65534;
if (gz == 65535) gz = 65534;
// send int16_t as 2 int8_ts for transmission and inhibit consecutive 255 bytes
msbax = (uint8_t)(ax >> 8); lsbax = (uint8_t)(ax & 255);
msbay = (uint8_t)(ay >> 8); lsbay = (uint8_t)(ay & 255);
if(lsbax == 255 && msbay == 255) {
lsbax = 254;
}
msbaz = (uint8_t)(az >> 8); lsbaz = (uint8_t)(az & 255);
if(lsbay == 255 && msbaz == 255) {
lsbay = 254;
}
msbgx = (uint8_t)(gx >> 8); lsbgx = (uint8_t)(gx & 255);
if(lsbaz == 255 && msbgx == 255) {
lsbaz = 254;
}
msbgy = (uint8_t)(gy >> 8); lsbgy = (uint8_t)(gy & 255);
if(lsbgx == 255 && msbgy == 255) {
lsbgx = 254;
}
msbgz = (uint8_t)(gz >> 8); lsbgz = (uint8_t)(gz & 255);
if(lsbgy == 255 && msbgz == 255) {
lsbgy = 254;
}
if(lsbgz == 255) {
lsbgz = 254;
}
// transmit values
XBeeSerial.write(start); XBeeSerial.write(start);
XBeeSerial.write(msbax); XBeeSerial.write(lsbax);
XBeeSerial.write(msbay); XBeeSerial.write(lsbay);
XBeeSerial.write(msbaz); XBeeSerial.write(lsbaz);
XBeeSerial.write(msbgx); XBeeSerial.write(lsbgx);
XBeeSerial.write(msbgy); XBeeSerial.write(lsbgy);
XBeeSerial.write(msbgz); XBeeSerial.write(lsbgz);
}
2. My second try was to get it going with Maxuino. I installed everything like described at maxuino site. http://www.maxuino.org/video
Maxiuno uses the Standard Fimata Arduino Code. I am able to get the connection going between max/msp and arduino. I connected the scl and sda pins of the MPU6050 to analog pins of the arduino and i get some values into max... But they are not useable. How do I interpret this data?
The aim is to get the accelerometer and gyroscope data in a handable way going in max msp to control sound .
Hope somebody can help me with some concret arduino code with fitting max/msp code.
Hello Denim,
I stop to use Maxuino because it doesn't work properly, especially for the I2C connection. I made a code (copy and paste from the net) and a patch to read out the MPU6050 values and to control some leds. I'm sure there is not the best way, but for my projects it enough for me. Sometimes I need to restart the serial connection at beginning (in the patch the big "bang") to have a properly connection between Arduino and MAX.
If someone uses another way it could be nice to share it !
Enjoy-it.
Claudio
As I known, firmata is a standard communication protocol for communication between Arduino and interaction dev software like processing and Max. Is it possible to program Arduino with firmata standard software and use firmata function in MaxMSP to build the digital, analog and i2c sensor reading easily?
I am not sure if it works but find this link from github. Hope it's helpful.
https://github.com/firmata/maxduino
maxuino is not that bad afaik and the big advantage is that you don't really need to learn how to code for arduino, it's a big firmata script that you use with the max object/patcher and this script exposes most of (if not all) the board connections
as for what you do wtih the values... it's up to you ! should be your job now, inside max.
Hello,
i have a sketch that works very well. You can send values directly to Max.
MPU6050 library is required.
Unfortunately this sketch works for only sensor but is possible edit the code to use two or more sensors but i don't know how to do.
Does anyone to be able to edit this sketch to use two sensors?
Thank you in advances
Connections:
MPU6050 ARDUINO UNO
VCC -> +3.3V
GND -> GND
SDA -> A4
SCL -> A5
Here the code:
#include
#include
MPU6050 mpu;
// Pitch, Roll and Yaw values
float pitch = 0;
float roll = 0;
float yaw = 0;
float acX = 0, acY = 0, acZ = 0;
void setup()
{
Serial.begin(115200);
Serial.println("Initialize MPU6050");
while (!mpu.begin(MPU6050_SCALE_2000DPS, MPU6050_RANGE_2G))
{
Serial.println("Could not find a valid MPU6050 sensor, check wiring!");
delay(500);
}
// Calibrate gyroscope. The calibration must be at rest.
// If you don't want calibrate, comment this line.
mpu.calibrateGyro();
// Set threshold sensivty. Default 3.
// If you don't want use threshold, comment this line or set 0.
mpu.setThreshold(1);
}
void loop()
{
// Read normalized values
Vector normAccel = mpu.readNormalizeAccel();
Vector normGyro = mpu.readNormalizeGyro();
acX = normAccel.XAxis;
acY = normAccel.YAxis;
acZ = normAccel.ZAxis;
// Calculate Pitch & Roll
pitch = -(atan2(normAccel.XAxis, sqrt(normAccel.YAxis * normAccel.YAxis + normAccel.ZAxis * normAccel.ZAxis)) * 180.0) / M_PI;
roll = (atan2(normAccel.YAxis, normAccel.ZAxis) * 180.0) / M_PI;
//Ignore the gyro if our angular velocity does not meet our threshold
if (normGyro.ZAxis > 1 || normGyro.ZAxis < -1) {
normGyro.ZAxis /= 100;
yaw += normGyro.ZAxis;
}
//Keep our angle between 0-359 degrees
if (yaw < 0)
yaw += 1;
else if (yaw > 359)
yaw -= 1;
// Output
Serial.print(" Pitch = ");
Serial.print(pitch);
Serial.print("\tRoll = ");
Serial.print(roll);
Serial.print("\tYaw = ");
Serial.print(yaw);
Serial.print("\tAcX = ");
Serial.print(acX);
Serial.print("\tAcY = ");
Serial.print(acY);
Serial.print("\tAcZ = ");
Serial.print(acZ);
Serial.println();
delay(10);
}