pifcamp-2021/ArduinoNano33BLE/MidiBle-tilt/MidiBle-tilt.ino

233 lines
6.5 KiB
Arduino
Raw Normal View History

2022-03-02 22:31:56 +01:00
#include <Arduino_LSM9DS1.h>
#include <BLEMIDI_Transport.h>
2022-03-04 02:39:17 +01:00
#include "SensorFusion.h" //SF
2022-03-02 22:31:56 +01:00
//#include <hardware/BLEMIDI_ESP32_NimBLE.h>
//#include <hardware/BLEMIDI_ESP32.h>
//#include <hardware/BLEMIDI_nRF52.h>
#include <hardware/BLEMIDI_ArduinoBLE.h>
BLEMIDI_CREATE_DEFAULT_INSTANCE()
unsigned long t0 = millis();
bool isConnected = false;
2022-03-04 00:14:31 +01:00
// midi
// offsets CC:
// 0-63: left hand
// 64-127: right hand
enum side {LEFT, RIGHT=64};
int hand = LEFT;
int midichannel = 1;
2022-03-02 22:31:56 +01:00
// accelerometer
2022-03-04 00:14:31 +01:00
float accx, accy, accz;
float maccx, maccy, maccz; // midi mapped
float accmag; // magnitude
// gyroscope
float gyrox, gyroy, gyroz;
float mgyrox, mgyroy, mgyroz;
// magnetometer
float magx, magy, magz;
float mmagx, mmagy, mmagz;
2022-03-04 02:39:17 +01:00
// sensor fusion
float mroll, mpitch, myaw;
2022-03-04 00:14:31 +01:00
float maxx, maxy, maxz;
2022-03-02 22:31:56 +01:00
2022-03-04 02:39:17 +01:00
//SF -
SF fusion;
float gx, gy, gz, ax, ay, az, mx, my, mz;
float pitch, roll, yaw;
float deltat;
//Gyro Offset....: -605622 -24028 -338386
float goffx = -605622.0 / 1000000.0;
float goffy = -24028.0 / 1000000.0;
float goffz = -338386.0 / 1000000.0;
2022-03-04 00:14:31 +01:00
float fmap(float x, float in_min, float in_max, float out_min, float out_max)
{
return (x - in_min) * (out_max - out_min) / (in_max - in_min) + out_min;
}
2022-03-02 22:31:56 +01:00
// -----------------------------------------------------------------------------
// When BLE connected, LED will turn on (indication that connection was successful)
// When receiving a NoteOn, LED will go out, on NoteOff, light comes back on.
// This is an easy and conveniant way to show that the connection is alive and working.
// -----------------------------------------------------------------------------
void setup()
{
Serial.begin(115200);
Serial.println("MIDI-BLE TEST 1");
Serial.println("2022-03-02");
MIDI.begin();
pinMode(LED_BUILTIN, OUTPUT);
digitalWrite(LED_BUILTIN, LOW);
BLEMIDI.setHandleConnected([]() {
isConnected = true;
digitalWrite(LED_BUILTIN, HIGH);
Serial.println("MIDI-BLE CONNECTED");
});
BLEMIDI.setHandleDisconnected([]() {
isConnected = false;
digitalWrite(LED_BUILTIN, LOW);
Serial.println("MIDI-BLE DISCONNECTED");
});
MIDI.setHandleNoteOn([](byte channel, byte note, byte velocity) {
digitalWrite(LED_BUILTIN, LOW);
});
MIDI.setHandleNoteOff([](byte channel, byte note, byte velocity) {
digitalWrite(LED_BUILTIN, HIGH);
});
// accelerometer
if (!IMU.begin()) {
Serial.println("Failed to initialize IMU!");
while (1);
}
Serial.print("Accelerometer sample rate = ");
Serial.print(IMU.accelerationSampleRate());
Serial.println(" Hz");
Serial.println();
Serial.println("Acceleration in G's");
Serial.println("X\tY\tZ");
2022-03-04 00:14:31 +01:00
Serial.print("Gyroscope sample rate = ");
Serial.print(IMU.gyroscopeSampleRate());
Serial.println(" Hz");
Serial.println();
Serial.println("Gyroscope in degrees/second");
Serial.println("X\tY\tZ");
Serial.print("Magnetic field sample rate = ");
Serial.print(IMU.magneticFieldSampleRate());
Serial.println(" uT");
Serial.println();
Serial.println("Magnetic Field in uT");
Serial.println("X\tY\tZ");
2022-03-02 22:31:56 +01:00
}
// -----------------------------------------------------------------------------
//
// -----------------------------------------------------------------------------
void loop()
{
2022-03-04 02:39:17 +01:00
// now you should read the gyroscope, accelerometer (and magnetometer if you have it also)
// NOTE: the gyroscope data have to be in radians
// if you have them in degree convert them with: DEG_TO_RAD example: gx * DEG_TO_RAD
if (IMU.magneticFieldAvailable()) {
IMU.readMagneticField(mx, my, mz);
}
2022-03-04 00:14:31 +01:00
2022-03-04 02:39:17 +01:00
if (IMU.accelerationAvailable()&&IMU.gyroscopeAvailable()) {
IMU.readAcceleration(ax, ay, az);
/*
ax *= 9.81;
ay *= 9.81;
az *= 9.81;
*/
IMU.readGyroscope(gx, gy, gz);
gx -= goffx;
gy -= goffy;
gz -= goffz;
gx *= DEG_TO_RAD;
gy *= DEG_TO_RAD;
gz *= DEG_TO_RAD;
deltat = fusion.deltatUpdate(); //this have to be done before calling the fusion update
//choose only one of these two:
//fusion.MahonyUpdate(gx, gy, gz, ax, ay, az, deltat); //mahony is suggested if there isn't the mag and the mcu is slow
fusion.MadgwickUpdate(gx, gy, gz, ax, ay, az, mx, my, mz, deltat); //else use the magwick, it is slower but more accurate
pitch = fusion.getPitch();
roll = fusion.getRoll(); //you could also use getRollRadians() ecc
yaw = fusion.getYaw();
accmag = sqrt(ax*ax+ay*ay+az*az);
2022-03-04 00:14:31 +01:00
// accmag = map(accmag, 0, 1000, 0,127);
2022-03-04 02:39:17 +01:00
mroll = fmap(roll,-180,180,0,127);
mpitch = fmap(pitch,-90,90,0,127);
myaw = fmap(yaw,0,360,0,127);
maccx = fmap(ax,-4.0,4.0, 0.0,127.0);
maccy = fmap(ay,-4.0,4.0, 0.0,127.0);
maccz = fmap(az,-4.0,4.0, 0.0,127.0);
2022-03-04 00:14:31 +01:00
//mgyrox = gyrox/2000 * 64 + 64;
//mgyroy = gyroy/2000 * 64 + 64;
//mgyroz = gyroz/2000 * 64 + 64;
2022-03-04 02:39:17 +01:00
mgyrox = map(gx, -2000,2000, 0.0, 127.0);
mgyroy = map(gy, -2000,2000, 0.0, 127.0);
mgyroz = map(gz, -2000,2000, 0.0, 127.0);
2022-03-04 00:14:31 +01:00
// mz = max(10, abs(z) * 1000);
2022-03-04 02:39:17 +01:00
mmagx = fmap(mx, 0,60, 0,127);
mmagy = fmap(my, 0,60, 0,127);
mmagz = fmap(mz, 0,60, 0,127);
2022-03-04 00:14:31 +01:00
maxx = max(magx, maxx);
maxy = max(magy, maxy);
maxz = max(magz, maxz);
// Serial.print(mx);
// Serial.print('\t');
// Serial.print(my);
2022-03-02 22:31:56 +01:00
// Serial.print(y);
// Serial.print('\t');
// Serial.println(z);
2022-03-04 00:14:31 +01:00
// Serial.println();
2022-03-02 22:31:56 +01:00
}
MIDI.read();
2022-03-04 00:14:31 +01:00
if (isConnected && (millis() - t0) > 10)
2022-03-02 22:31:56 +01:00
{
t0 = millis();
2022-03-04 00:14:31 +01:00
// MIDI.sendNoteOn (my, mx, 1); // note 60, velocity 100 on channel 1
MIDI.sendControlChange(0 + hand, maccx, midichannel);
MIDI.sendControlChange(1 + hand, maccy, midichannel);
MIDI.sendControlChange(2 + hand, maccz, midichannel);
MIDI.sendControlChange(3 + hand, mgyrox, midichannel);
MIDI.sendControlChange(4 + hand, mgyroy, midichannel);
MIDI.sendControlChange(5 + hand, mgyroz, midichannel);
MIDI.sendControlChange(6, accmag * 100, midichannel);
MIDI.sendControlChange(7 + hand, mmagx, midichannel);
MIDI.sendControlChange(8 + hand, mmagy, midichannel);
MIDI.sendControlChange(9 + hand, mmagz, midichannel);
2022-03-04 02:39:17 +01:00
MIDI.sendControlChange(10 + hand, mroll, midichannel);
MIDI.sendControlChange(11 + hand, mpitch, midichannel);
MIDI.sendControlChange(12 + hand, myaw, midichannel);
// Serial.print(mmagx);
// Serial.print('\t');
// Serial.print(mmagy);
// Serial.print('\t');
// Serial.println(mmagz);
2022-03-04 00:14:31 +01:00
// Serial.print('\t');
// Serial.println(accmag * 100);
// Serial.println("ping");
// delay(mz);
// MIDI.sendNoteOff(my, mx, 1);
2022-03-02 22:31:56 +01:00
}
}