Final version of project
Dependencies: FSR LSM9DS1_Library_cal USBMIDI mbed
Fork of LSM9DS1_Demo_wCal by
Diff: main.cpp
- Revision:
- 2:82b2a1e84586
- Parent:
- 1:a81deeb5ba58
- Child:
- 3:de36d456f684
--- a/main.cpp Fri Apr 29 01:47:18 2016 +0000
+++ b/main.cpp Fri Apr 29 13:21:09 2016 +0000
@@ -23,7 +23,8 @@
LSM9DS1 IMU(p9, p10, 0xD6, 0x3C);
LSM9DS1 IMU2(p28, p27, 0xD6, 0x3C);
-//uLCD_4DGL uLCD(p28,p27,p30); // serial tx, serial rx, reset pin;
+USBMIDI midi;
+
Serial pc(USBTX, USBRX);
DigitalOut led1(LED1);
DigitalOut led2(LED2);
@@ -41,72 +42,51 @@
float t_gyroCurr2 = 0;
float delta_t = 0;
-float x_accel = 0;
float y_accel = 0;
float y_accel2 = 0;
-float x_vel_prev = 0;
-float x_vel_curr = 0;
-float y_vel_prev = 0;
-float y_vel_curr = 0;
-
-float x_pos_prev = 0;
-float x_pos_curr = 0;
-float y_pos_prev = 0;
-float y_pos_curr = 0;
int resetIndex = BUFFERSIZE - 2;
float average[BUFFERSIZE] = {0};
int avg_index = 0;
float total = 0;
-float avg_thresh;
+float avg_thresh = 20;
float average2[BUFFERSIZE] = {0};
int avg_index2 = 0;
float total2 = 0;
float avg_thresh2;
-float z_gyro = 0;
-float gyroAverage[3] = {0};
-int avg_gyroIndex = 0;
-float gyroTotal = 0;
-float running_gyroAvg = 0;
-
-float avg_gyroThresh = 0;
-float avg_gyroThresh2 = 0;
-
-float gyroAverage2[3] = {0};
-int avg_gyroIndex2 = 0;
-float gyroTotal2 = 0;
-float running_gyroAvg2 = 0;
-
-float gyroInterval = 1.00;
-
float prev_y_accel = 0;
float curr_y_accel = 0;
-float prev_x_accel = 0;
-float curr_x_accel = 0;
float y_accel_threshold = 0.8;
-float x_accel_threshold = 0.1;
bool check_y_accel = false;
-bool check_x_accel = false;
float t_prev_y_accel = 0;
float prev_y_accel2 = 0;
float curr_y_accel2 = 0;
-float prev_x_accel2 = 0;
-float curr_x_accel2 = 0;
bool check_y_accel2 = false;
-bool check_x_accel2 = false;
float t_prev_y_accel2 = 0;
+
int count2 = 0;
+int count = 0;
+
+bool detectHit = 0;
+bool detectHit2 = 0;
+bool detectUp = 0;
+bool detectUp2 = 0;
+
+float runningAvg = 0;
+float runningAvg2 = 0;
+
+float interval = 0.20;
+float hit_volume = 0;
+float hit_volume2 = 0;
-// enum InputType {STILL, ACCEL_POS, ACCEL_NEG};
enum StateType {FRONT, SIDE, HIT};
enum StateType2 {FRONT2, SIDE2, HIT2};
-// InputType input = STILL; // Initial input is STILL
StateType state = FRONT; // Initial state is FRONT
StateType2 state2 = FRONT2;
@@ -159,23 +139,11 @@
}
}
- int count = 0;
- USBMIDI midi;
- bool detectHit = 0;
- bool detectHit2 = 0;
- bool detectUp = 0;
- bool detectUp2 = 0;
- float runningAvg = 0;
- float runningAvg2 = 0;
- float interval;
- float hit_volume = 0;
- float hit_volume2 = 0;
-
int main()
{
- midi.attach(show_message); // call back for messages received
+ midi.attach(show_message); // call back for messages received
pc.baud(9600);
pc.printf("Hello world!\n");
IMU.begin();
@@ -194,6 +162,7 @@
while(1) {
+ // Initialize acceleration and gyroscope data for both IMUs
while(!IMU.accelAvailable());
IMU.readAccel();
while(!IMU.gyroAvailable());
@@ -204,52 +173,42 @@
while(!IMU2.gyroAvailable());
IMU2.readGyro();
- /**
- * FSR
- */
-
- if(fsr_kick.readRaw()>0.3){
- if(kicked == false){
- midi.write(MIDIMessage::NoteOn(45, fsr_kick.readRaw()*127 + 30, 10));
- }
- kicked = true;
- }
- else{kicked = false;}
-
- if(fsr_hh.readRaw()>0.3){
- if(hh_close == false){
- midi.write(MIDIMessage::NoteOn(42, fsr_hh.readRaw()*127, 10));
- }
- hh_close = true;
- }
- else{hh_close = false;}
-
- /**
- End FSR
- **/
-
-// pc.printf("\nIMU Temperature = %f C\n\r",25.0 + IMU.temperature/16.0);
-// pc.printf(" X axis Y axis Z axis\n\r");
- //pc.printf("gyro: %9f %9f %9f in deg/s\n\r", IMU.calcGyro(IMU.gx), IMU.calcGyro(IMU.gy), IMU.calcGyro(IMU.gz));
- //pc.printf("gyro: %9f in deg/s\n\r", IMU.calcGyro(IMU.gy));
- //pc.printf("accel: %9f %9f %9f in Gs\n\r", IMU.calcAccel(IMU.ax), IMU.calcAccel(IMU.ay), IMU.calcAccel(IMU.az));
-// pc.printf("mag: %9f %9f %9f in gauss\n\r", IMU.calcMag(IMU.mx), IMU.calcMag(IMU.my), IMU.calcMag(IMU.mz));
-// printAltitude(IMU.calcAccel(IMU.ax), IMU.calcAccel(IMU.ay), IMU.calcAccel(IMU.az), IMU.calcMag(IMU.mx),
-// IMU.calcMag(IMU.my), IMU.calcMag(IMU.mz));
+ // Create variables for ease of use for acceleration
y_accel = IMU.calcAccel(IMU.ay);
y_accel2 = IMU2.calcAccel(IMU2.ay);
- x_accel = IMU.calcAccel(IMU.ax);
+ // Initialize timer
t_curr = t.read();
t_curr2 = t_curr;
+ /**
+ * FSR detection
+ */
+
+ if (fsr_kick.readRaw() > 0.3){
+ if (kicked == false){
+ midi.write(MIDIMessage::NoteOn(45, fsr_kick.readRaw() * 127 + 30, 10)); // Play a kick, map the volume and boost the amplitude, channel 10
+ }
+ kicked = true;
+ }
+ else {kicked = false;}
+
+ if (fsr_hh.readRaw() > 0.3){
+ if (hh_close == false){
+ midi.write(MIDIMessage::NoteOn(42, fsr_hh.readRaw()*127, 10)); // Play a hi-hat pedal, map the volume, channel 10
+ }
+ hh_close = true;
+ }
+ else {hh_close = false;}
+
/**
- * Averaging for hit detection
+ * Running average for hit detection
*/
// First IMU
+
total -= average[avg_index];
average[avg_index] = IMU.calcGyro(IMU.gy);
total += average[avg_index];
@@ -270,7 +229,7 @@
}
/**
- * Detect hit
+ * Detect downward hit
*/
if (IMU.calcGyro(IMU.gy) > 35) {
detectHit = 1;
@@ -280,6 +239,7 @@
detectHit2 = 1;
}
+
/**
* Check all conditions for hit
*/
@@ -291,19 +251,21 @@
// First IMU
detectUp = IMU.calcGyro(IMU.gy) <= 0;
runningAvg = total / BUFFERSIZE;
- interval = 0.20;
- avg_thresh = 20;
-
- if (detectHit && detectUp && runningAvg > avg_thresh && (t_curr - t_prev) > interval) {
+
+ // Check if drumstick is brought down and then brought back up (eliminates continous hit detection if drumstick is just held tilted down)
+ // Then check if running average is greater than a threshold (to elimate noise)
+ // Elimate debouncing by only allowing a hit to play if the time interval has passed
+ if (detectHit && detectUp && (runningAvg > avg_thresh) && (t_curr - t_prev) > interval) {
+ // Depending on the state, play the corresponding instrument
switch (state) {
case (FRONT):
- midi.write(MIDIMessage::NoteOn(46, runningAvg, 10));
+ midi.write(MIDIMessage::NoteOn(46, hit_volume, 10)); // Play ride sound
break;
case (SIDE):
if (hh_close) {
- midi.write(MIDIMessage::NoteOn(40, hit_volume, 10));
+ midi.write(MIDIMessage::NoteOn(40, hit_volume, 10)); // Play closed hi-hat sound
} else {
- midi.write(MIDIMessage::NoteOn(41, hit_volume, 10));
+ midi.write(MIDIMessage::NoteOn(41, hit_volume, 10)); // Play open hi-hat sound
}
break;
}
@@ -319,10 +281,10 @@
if (detectHit2 && detectUp2 && runningAvg2 > avg_thresh2 && (t_curr2 - t_prev2) > interval) {
switch (state2) {
case (FRONT2):
- midi.write(MIDIMessage::NoteOn(47, hit_volume2, 10));
+ midi.write(MIDIMessage::NoteOn(47, hit_volume2, 10)); // Play snare sound
break;
case (SIDE2):
- midi.write(MIDIMessage::NoteOn(51, hit_volume2, 10));
+ midi.write(MIDIMessage::NoteOn(51, hit_volume2, 10)); // Play clap sound
break;
}
detectHit2 = 0;
@@ -331,19 +293,16 @@
/**
- * Switching instruments detection
+ * Instrument switching detection
*/
curr_y_accel = y_accel;
- curr_x_accel = x_accel;
-
curr_y_accel2 = y_accel2;
check_y_accel = abs(curr_y_accel - prev_y_accel) > y_accel_threshold;
check_y_accel2 = abs(curr_y_accel2 - prev_y_accel2) > y_accel_threshold;
- check_x_accel = abs(curr_x_accel - prev_x_accel) > x_accel_threshold;
-
+ // Check that y accleration is above threshold; if it is, increase the count for the number of cycles it is above this threshold
if (check_y_accel) {
count++;
}
@@ -355,6 +314,8 @@
// First IMU
switch (state) {
case (FRONT):
+ // Check that y_accleration is above the threshold for at least 3 cycles
+ // Elimnate debouncing by only switching if a time interval has passed
if (check_y_accel && (count >= 3) && (t_curr - t_prev_y_accel) > 0.3) {
count = 0;
state = SIDE;
@@ -374,7 +335,6 @@
break;
}
prev_y_accel = curr_y_accel;
- prev_x_accel = curr_x_accel;
//Second IMU
switch (state2) {
@@ -399,5 +359,4 @@
}
prev_y_accel2 = curr_y_accel2;
}
-}
-
+}
\ No newline at end of file
