Skip to content
Snippets Groups Projects
OSCIMU.ino 3.36 KiB
Newer Older
// OSC IMU 
// Nathan Adams / Atau Tanaka - Goldsmiths, University of London
// 2020

// OSCIMU.ino
// Main code for sensing orientation and sending an OSC message

// Code is based on OSC_propShield, which was designed to work on a Teensy 3.1, Teensy PropShield and Adafruit Airlift Wifi (which uses the ESP32 Wifi coprocessor)
// for this project we are using the Adafruit HUZZAH32 Feather board, with the Adafruit 9DoF Fusion Breakout based on the Bosch BNO055. So this code is
// slightly reworked to use the appropriate libaries (e.g. Wifi instead of the Adafruit WifiNINA port / Adafruits Sensor library) 

// Build History
// Jan 2020 -> 1-prototype-build - get basics working across wifi

// ESP32 WiFi
#include <WiFi.h>

// Secrets file
#include "secrets.h"

// BNO055 / Adafruit Sensor library
#include <Adafruit_Sensor.h>
#include <Adafruit_BNO055.h>

#define BNO055_ADDRESS 0x28
//BNO055 is by standard for the fusion board on 0x28
Adafruit_BNO055 bno = Adafruit_BNO055(55, BNO055_ADDRESS);

// OSC Messaging
#include <OSCMessage.h>  /// https://github.com/CNMAT/OSC
#include <OSCBundle.h>  /// https://github.com/CNMAT/OSC

unsigned int localPort = 8888;
int sendCount;
const IPAddress outIp(SECRET_IP); 
WiFiUDP udp;

#define SERIAL_SPEED 115200
#define VERSION 0.1

void setup() {
  Serial.begin(SERIAL_SPEED);
  while (!Serial) {
    //wait for Serial to start up
  }
  //Try to connect to the Wifi netowrk provided in secrets
  Serial.print("OSC IMU ");
  Serial.println(VERSION);
  Serial.println();
  Serial.print("Connecting to ");
  Serial.println(SECRET_SSID);

  WiFi.begin(SECRET_SSID, SECRET_PASSWORD);

  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print('.');
  }

  Serial.println();
  Serial.print("Success! Connected, IP Address is ");
  Serial.println(WiFi.localIP());

  Serial.println("\nStarting connection to server...");
  // if you get a connection, report back via serial:
  udp.begin(localPort);


  //Now try to find the BNO055 sensor
  if(bno.begin()) {
    Serial.print("Found BNO055 on I2C address 0x");
    Serial.println(BNO055_ADDRESS, HEX);
  } else {
    /* There was a problem detecting the BNO055 ... check your connections */
    Serial.print("Ooops, no BNO055 detected ... Check your wiring or I2C ADDR!");
    while(1);
   }

  bno.setExtCrystalUse(true);

}


void loop() {

  sensors_event_t event;

  sendCount++;
  
  if (sendCount > 1000) {
 sensors_event_t orientationData , angVelocityData , linearAccelData;
  imu::Vector<3> magnetometer = bno.getVector(Adafruit_BNO055::VECTOR_MAGNETOMETER);
  imu::Vector<3> gyroscope = bno.getVector(Adafruit_BNO055::VECTOR_EULER);
  imu::Vector<3> linearAccel = bno.getVector(Adafruit_BNO055::VECTOR_LINEARACCEL);
  sendViaOSC(gyroscope.x(), gyroscope.y(), gyroscope.z(), linearAccel.x(), linearAccel.y(), linearAccel.z(), magnetometer.x(), magnetometer.y(), magnetometer.z());
  sendCount = 0; 
void sendViaOSC(float gyroX, float gyroY, float gyroZ, float acX, float acY, float acZ, float oX, float oY, float oZ) {
  OSCMessage msg("/oscimu");
  msg.add((int32_t)gyroX);
  msg.add((int32_t)gyroY);
  msg.add((int32_t)gyroZ);
  msg.add((int32_t)acX);
  msg.add((int32_t)acY);
  msg.add((int32_t)acZ);
  msg.add((int32_t)oX);
  msg.add((int32_t)oY);
  msg.add((int32_t)oZ);
   //send out
  udp.beginPacket(outIp, SECRET_OUTPORT);
  msg.send(udp);
  udp.endPacket();
  msg.empty();
//  sendCount = 0;