INCALCANDO / Parat+

Parat+ Documentation
Huzzah ESP8266 with MPU6050 accelerometer, Arduino IDE.

The previous post featured a code for the LSM9DSO0 accelerometer in combination with the Huzzah ESP8266. Since the LSM9DSO0 is not produced anymore, here is an alternative: Using the MPU-6050 acceleration and gyroscope sensor. Thanks for Jennifer Sykes for suggesting this alternative and assembling and testing the code.

Fritzing project – Huzzah ESP8266 – MPU-6050

When setting the Target IP in the ESP code to the network settings of Parat+, moving, tilting and rotating the sensor will send the sensor reading as OSC bundles (i.e. packages of separate OSC messages) to Parat+. These will appear as Source Faders:

/esp/accelX
/esp/accelY
/esp/accelZ

/esp/gyroX
/esp/gyroY
/esp/gyroZ

Use the range learn functions of the Parat+ Source faders to calibrate the data streams to the movements you wish to use.

Connect the ‘Value controlled by Source’ in the Fader Edit view of any Parat+ Fader to have that OSC and MIDI controller controlled by your sensor.

[code language=”cpp”]

#include <ESP8266WiFi.h>
#include <WiFiUDP.h>
#include <OSCMessage.h> /// https://github.com/CNMAT/OSC
#include <OSCBundle.h> /// https://github.com/CNMAT/OSC
#include <Wire.h>
// requires I2Cdev library: https://github.com/jrowberg/i2cdevlib
#include “I2Cdev.h”
// requires MPU-6050 part of the I2Cdev lib: https://github.com/jrowberg/i2cdevlib/tree/master/Arduino/MPU6050
#include “MPU6050.h”

///////////////////////
// MPU6050 Setup //
///////////////////////
// class default I2C address is 0x68
// specific I2C addresses may be passed as a parameter here
// AD0 low = 0x68 (default for InvenSense evaluation board)
// AD0 high = 0x69
MPU6050 accelgyro;
//MPU6050 accelgyro(0x69); // <– use for AD0 high
// uncomment “OUTPUT_READABLE_ACCELGYRO” if you want to see a tab-separated
// list of the accel X/Y/Z and then gyro X/Y/Z values in decimal. Easy to read,
// not so easy to parse, and slow(er) over UART.
#define OUTPUT_READABLE_ACCELGYRO

// uncomment “OUTPUT_BINARY_ACCELGYRO” to send all 6 axes of data as 16-bit
// binary, one right after the other. This is very fast (as fast as possible
// without compression or data loss), and easy to parse, but impossible to read
// for a human.
//#define OUTPUT_BINARY_ACCELGYRO
int16_t ax, ay, az;
int16_t gx, gy, gz;

long sendCount = 0;
long frameCount = 0;

/***WIFI NAME AND PASSWORD****/
const char* ssid = “YOUR_SSID”;
const char* password = “0123456789”;
//const char* ssid = “Your SSID name”;
//const char* password = “YourPassword”;

// A UDP instance to let us send and receive packets over UDP
WiFiUDP Udp;
//const IPAddress outIp(192, 168, 1, 95);
const IPAddress outIp(192, 168, 0, 101);
const unsigned int outPort = 10101;

void sendBundleViaOSC();

void getGyro();
void getAccel();

void setup() {

pinMode(0, OUTPUT);
digitalWrite(0, HIGH);
pinMode(2, OUTPUT);
digitalWrite(2, HIGH);
// join I2C bus (I2Cdev library doesn’t do this automatically)
#if I2CDEV_IMPLEMENTATION == I2CDEV_ARDUINO_WIRE
Wire.begin(4, 5);
#elif I2CDEV_IMPLEMENTATION == I2CDEV_BUILTIN_FASTWIRE
Fastwire::setup(400, true);
#endif
// initialize serial communication
// (38400 chosen because it works as well at 8MHz as it does at 16MHz, but
// it’s really up to you depending on your project)
Serial.begin(38400);

// initialize device
Serial.println(“Initializing I2C devices…”);
accelgyro.initialize();

// verify connection
Serial.println(“Testing device connections…”);
Serial.println(accelgyro.testConnection() ? “MPU6050 connection successful” : “MPU6050 connection failed”);
// Connect to WiFi network
Serial.println();
Serial.println();
Serial.print(“Connecting to “);
Serial.println(ssid);

WiFi.begin(ssid, password);

while (WiFi.status() != WL_CONNECTED) {
digitalWrite(0, LOW);
delay(10);
digitalWrite(0, HIGH);
delay(500);
Serial.print(“.”);
}
Serial.println(“”);
Serial.println(“WiFi connected”);
}

void loop() {

sendCount ++;
frameCount++;
if (frameCount < 2) {
digitalWrite(2, LOW); //blue LED on
} else {
digitalWrite(2, HIGH);
}
if (frameCount > 500) {
frameCount = 0;
}
if (sendCount > 1000)
{
getGyro(); // Print “G: gx, gy, gz”
getAccel(); // Print “A: ax, ay, az”

sendViaOSC();
//sendBundleViaOSC();
}
}

void sendViaOSC() {
OSCMessage msg(“/esp/accelX”);
msg.add(ax);
msg.add(“/esp/accelY”);
msg.add(ay);
msg.add(“/esp/accelZ”);
msg.add(az);
Udp.beginPacket(outIp, outPort);
msg.send(Udp);
Udp.endPacket();
msg.empty();
sendCount = 0;
}

void sendBundleViaOSC() {
OSCBundle bndl;

bndl.add(“/esp/accelX”).add(ax);
bndl.add(“/esp/accelY”).add(ay);
bndl.add(“/esp/accelZ”).add(az);
bndl.add(“/esp/gyroX”).add(gx);
bndl.add(“/esp/gyroY”).add(gy);
bndl.add(“/esp/gyroZ”).add(gz);

Udp.beginPacket(outIp, outPort);
bndl.send(Udp); // send the bytes to the SLIP stream
Udp.endPacket(); // mark the end of the OSC Packet
bndl.empty(); // empty the bundle to free room for a new one

// Serial.println(aX);
}

void getGyro()
{
accelgyro.getRotation(&gx, &gy, &gz);
Serial.print(“gx:”);
Serial.println(gx);
Serial.print(“gy:”);
Serial.println(gy);
Serial.print(“gz:”);
Serial.println(gz);

}

void getAccel()
{
accelgyro.getAcceleration(&ax, &ay, &az);
Serial.print(“ax:”);
Serial.println(ax);
Serial.print(“ay:”);
Serial.println(ay);
Serial.print(“az:”);
Serial.println(az);
}

[/code]

Sensor data transmitted as OSC over Wifi – Huzzah ESP8266

This post describes the assembly of a wireless sensor transmitter suitable for music and art performance and installation setups. It has been very reliable for performance. At the time of writing I had to learn that the sensor used here (LSM9DS0 with i2c) is not produced anymore. I will update this post in the near future with a more up-to-date sensor breakout board.

Used Parts:

Huzzah ESP8266 – https://www.adafruit.com/product/2471
*| 9 Degrees of Freedom IMU Breakout – LSM9DS0 – https://www.sparkfun.com/products/retired/1263
but supplied code can be easily updated for the Adafruit 9-DOF Accel/Mag/Gyro+Temp Breakout Board – LSM9DS1 LiPo charger and 3.7 1000mAh LiPo battery.

The LSM9DS0 sensor uses the I2C protocol. It is very straight forward to connect the sensor breakout board and the Huzzah ESP.

The sensor breakout board needs to be powered, so connect Gnd and +3.3V between the two circuits. The digital I2C communication uses two wires, the Data line SDA and a Clock line (SCL).

Below is the Arduino Code that will read the sensor data, format and send the readings as OSC bundles.

Ensure you are setting the Arduino IDE to the Huzzah ESP8266 Board (you might need to add this board in the Arduino Board manager, please see Adafruit overview/tutorials for more info.)

In the code, make sure you are setting the SSID name and password of the network you want to use.

Also ensure that the Wire.begin() contains the pin numbers of the SDA and SCL. Here it is 4 and 5.

 

[code language=”cpp”]
#include <ESP8266WiFi.h>
#include <WiFiUDP.h>
#include <OSCMessage.h> /// https://github.com/CNMAT/OSC
#include <OSCBundle.h> /// https://github.com/CNMAT/OSC
#include <SPI.h> // Included for SFE_LSM9DS0 library
#include <Wire.h>
#include <SFE_LSM9DS0.h> /// https://github.com/sparkfun/SparkFun_LSM9DS0_Arduino_Library/tree/V_1.0.1

#define SET_OFFSET 12

///////////////////////
// LSM9DS0 I2C Setup //
///////////////////////
// Comment out this section if you’re using SPI
// SDO_XM and SDO_G are both grounded, so our addresses are:
#define LSM9DS0_XM 0x1D // Would be 0x1E if SDO_XM is LOW
#define LSM9DS0_G 0x6B // Would be 0x6A if SDO_G is LOW
// Create an instance of the LSM9DS0 library called `dof` the
// parameters for this constructor are:
// [SPI or I2C Mode declaration],[gyro I2C address],[xm I2C add.]
LSM9DS0 dof(MODE_I2C, LSM9DS0_G, LSM9DS0_XM);
long sendCount = 0;
long frameCount = 0;
//const char* ssid = “BTHub3-PQ5N”;
//const char* password = “78cbae358d”;
const char* ssid = “piano+”;
const char* password = “bbbbbbbb”;

// A UDP instance to let us send and receive packets over UDP
WiFiUDP Udp;
//const IPAddress outIp(192, 168, 1, 95);
const IPAddress outIp(192, 168, 5, 111);
const unsigned int outPort = 10101;
float aX = 0.0f;
float aY = 0.0f;
float aZ = 0.0f;

float gX = 0.0f;
float gY = 0.0f;
float gZ = 0.0f;

float mX = 0.0f;
float mY = 0.0f;
float mZ = 0.0f;

void sendBundleViaOSC();
void getMag();
void getGyro();
void getAccel();

void setup() {

Serial.begin(115200);

pinMode(0, OUTPUT);
digitalWrite(0, HIGH);
pinMode(2, OUTPUT);
digitalWrite(2, HIGH);

Wire.begin(4,5); //set i2c SDA and SCL pins

// Use the begin() function to initialize the LSM9DS0 library.
// You can either call it with no parameters (the easy way):
uint16_t status = dof.begin();
// Or call it with declarations for sensor scales and data rates:
//uint16_t status = dof.begin(dof.G_SCALE_2000DPS,
// dof.A_SCALE_6G, dof.M_SCALE_2GS);
// Set output data rates
// Accelerometer output data rate (ODR) can be: A_ODR_3125 (3.225 Hz), A_ODR_625 (6.25 Hz), A_ODR_125 (12.5 Hz), A_ODR_25, A_ODR_50,
// A_ODR_100, A_ODR_200, A_ODR_400, A_ODR_800, A_ODR_1600 (1600 Hz)
dof.setAccelODR(dof.A_ODR_100); // Set accelerometer update rate at 100 Hz
// Accelerometer anti-aliasing filter rate can be 50, 194, 362, or 763 Hz
// Anti-aliasing acts like a low-pass filter allowing oversampling of accelerometer and rejection of high-frequency spurious noise.
// Strategy here is to effectively oversample accelerometer at 100 Hz and use a 50 Hz anti-aliasing (low-pass) filter frequency
// to get a smooth ~150 Hz filter update rate
dof.setAccelABW(dof.A_ABW_50); // Choose lowest filter setting for low noise
// Gyro output data rates can be: 95 Hz (bandwidth 12.5 or 25 Hz), 190 Hz (bandwidth 12.5, 25, 50, or 70 Hz)
// 380 Hz (bandwidth 20, 25, 50, 100 Hz), or 760 Hz (bandwidth 30, 35, 50, 100 Hz)
dof.setGyroODR(dof.G_ODR_95_BW_125); // Set gyro update rate to 190 Hz with the smallest bandwidth for low noise

// Magnetometer output data rate can be: 3.125 (ODR_3125), 6.25 (ODR_625), 12.5 (ODR_125), 25, 50, or 100 Hz
dof.setMagODR(dof.M_ODR_100); // Set magnetometer to update every 80 ms
// begin() returns a 16-bit value which includes both the gyro
// and accelerometers WHO_AM_I response. You can check this to
// make sure communication was successful.

Serial.print(“LSM9DS0 WHO_AM_I’s returned: 0x”);
Serial.println(status, HEX);
Serial.println(“Should be 0x49D4”);
Serial.println();

// Connect to WiFi network
Serial.println();
Serial.println();
Serial.print(“Connecting to “);
Serial.println(ssid);

WiFi.begin(ssid, password);

while (WiFi.status() != WL_CONNECTED) {
digitalWrite(0, LOW);
delay(10);
digitalWrite(0, HIGH);
delay(500);
Serial.print(“.”);
}
Serial.println(“”);
Serial.println(“WiFi connected”);
}

void loop() {

sendCount ++;
frameCount++;
if(frameCount < 2){
digitalWrite(2, LOW); //blue LED on
} else {
digitalWrite(2, HIGH);
}
if(frameCount > 500){
frameCount = 0;
}
if (sendCount > 1000)
{
getGyro(); // Print “G: gx, gy, gz”
getAccel(); // Print “A: ax, ay, az”
getMag(); // Print “M: mx, my, mz”
sendBundleViaOSC();
}
}

void sendViaOSC() {
OSCMessage msg(“/esp/magX”);
msg.add(mX);
msg.add(“/esp/magY”);
msg.add(mY);
msg.add(“/esp/magZ”);
msg.add(mZ);
Udp.beginPacket(outIp, outPort);
msg.send(Udp);
Udp.endPacket();
msg.empty();
sendCount = 0;
}

void sendBundleViaOSC() {
OSCBundle bndl;
bndl.add(“/esp/magX”).add(mX);
bndl.add(“/esp/magY”).add(mY);
bndl.add(“/esp/magZ”).add(mZ);
bndl.add(“/esp/accelX”).add(aX);
bndl.add(“/esp/accelY”).add(aY);
bndl.add(“/esp/accelZ”).add(aZ);
bndl.add(“/esp/gyroX”).add(gX);
bndl.add(“/esp/gyroY”).add(gY);
bndl.add(“/esp/gyroZ”).add(gZ);
Udp.beginPacket(outIp, outPort);
bndl.send(Udp); // send the bytes to the SLIP stream
Udp.endPacket(); // mark the end of the OSC Packet
bndl.empty(); // empty the bundle to free room for a new one

// Serial.println(mX);
}

void getMag()
{
// To read from the magnetometer, you must first call the
// readMag() function. When this exits, it’ll update the
// mx, my, and mz variables with the most current data.
dof.readMag();

// Now we can use the mx, my, and mz variables as we please.
// Either print them as raw ADC values, or calculated in Gauss.
mX = dof.calcMag(dof.mx);
mY = dof.calcMag(dof.my);
mZ = dof.calcMag(dof.mz);
}

void getGyro()
{
// To read from the gyroscope, you must first call the
// readGyro() function. When this exits, it’ll update the
// gx, gy, and gz variables with the most current data.
dof.readGyro();
gX = dof.calcGyro(dof.gx);
gY = dof.calcGyro(dof.gy);
gZ = dof.calcGyro(dof.gz);

}

void getAccel()
{
// To read from the accelerometer, you must first call the
// readAccel() function. When this exits, it’ll update the
// ax, ay, and az variables with the most current data.
dof.readAccel();

// If you want to print calculated values, you can use the
// calcAccel helper function to convert a raw ADC value to
// g’s. Give the function the value that you want to convert.
aX = dof.calcAccel(dof.ax);
aY = dof.calcAccel(dof.ay);
aZ = dof.calcAccel(dof.az);

}

[/code]

Angharad Davies’ Solo Violin and Four Bass Amps at Borealis Festival, Bergen, Norway

Following images were taken during the sound check for Angharad Davies’ Solo Violin and Four Bass Amps.

Angharad Davies @Borealis Festival, Bergen, 2017

Angharad Davies @Borealis Festival, Bergen, 2017

Live processing of the violin with an adaption of the Max/MSP patches of the performance system piano+ by Sebastian Lexer.

The processing parameters and diffusion of the sounds to the four bass amps was controlled using the OSC and MIDI controller app Parat+.

Angharad Davies @Borealis Festival, Bergen, 2017

Angharad Davies @Borealis Festival, Bergen, 2017

Max/MSP patch and Parat+

Max/MSP patch and Parat+

Max/MSP patch and Parat+

Setup: Max/MSP patch and Parat+

Images: Sebastian Lexer, 10.03.2017