a stepper motor view as a serial midi device, use an interface such ttymidi to generate a serial to 115200 baudrate midi instrument

Dependencies:   MIDI mbed X-NUCLEO-IHM05A1

Files at this revision

API Documentation at this revision

Comitter:
gidiana
Date:
Tue Oct 01 10:42:23 2019 +0000
Parent:
33:c1cefad6d338
Commit message:
first

Changed in this revision

MIDI.lib Show annotated file Show diff for this revision Revisions of this file
main.cpp Show annotated file Show diff for this revision Revisions of this file
pitches.h Show annotated file Show diff for this revision Revisions of this file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/MIDI.lib	Tue Oct 01 10:42:23 2019 +0000
@@ -0,0 +1,1 @@
+https://os.mbed.com/users/gidiana/code/MIDI/#7791cb1cb0b9
--- a/main.cpp	Sun Sep 15 08:47:18 2019 +0000
+++ b/main.cpp	Tue Oct 01 10:42:23 2019 +0000
@@ -1,15 +1,17 @@
 #include "mbed.h"
 #include "L6208.h"
+#include "pitches.h"
+#include "MIDI.h"
 
 #define VREFA_PWM_PIN D3
 #define VREFB_PWM_PIN D9
-#define BAUDRATE 9600
-#define JOINT 2
+
+
 l6208_init_t init =
 {
-  8000,            //Acceleration rate in step/s^2 or (1/16)th step/s^2 for microstep modes
+  65000,            //Acceleration rate in step/s^2 or (1/16)th step/s^2 for microstep modes
   80,              //Acceleration current torque in % (from 0 to 100)
-  8000,            //Deceleration rate in step/s^2 or (1/16)th step/s^2 for microstep modes
+  65000,            //Deceleration rate in step/s^2 or (1/16)th step/s^2 for microstep modes
   80,              //Deceleration current torque in % (from 0 to 100)
   8000,            //Running speed in step/s or (1/16)th step/s for microstep modes
   80,              //Running current torque in % (from 0 to 100)
@@ -21,7 +23,8 @@
   100000           //VREFA and VREFB PWM frequency (Hz)
 };
 
-
+unsigned long motorSpeeds[] = {0, 0, 0, 0, 0};
+int flag=0;
 // Utility
 //InterruptIn button(USER_BUTTON);
 DigitalOut led(LED1);
@@ -31,96 +34,31 @@
 
 InterruptIn end1(USER_BUTTON, PullUp);
 DigitalIn end0(PA_5, PullUp);
-Serial serial(PA_2, PA_3); 
 
-
-
-float pose, current_pose;
-float speed, current_speed;
-void zero()
+    
+MIDI MIDI(PA_2, PA_3);
+  
+void handleNoteOn(byte channel, byte pitch, byte velocity) //MIDI Note ON Command
 {
-  printf("zero");
-  motor->run(StepperMotor::BWD);
-  while(!end0){
-  }
-  motor->hard_stop();
-  motor->set_home();
-  motor->go_to(0);
-  printf("END0: Pressed\n\rPOSITION: %d\n\r", motor->get_position());
-}
-
-void motor_error_handler(uint16_t error)
-{
-  printf("ERROR: Motor Runtime\n\r");
   
-}
-
-void end1_int_handler()
-{
-  // motor->hard_stop();
-  
-  motor->run(StepperMotor::FWD);
-  
-  printf("END1: Pressed\n\rPOSITION: %d\n\r", motor->get_position());
+  motorSpeeds[channel] = pitchVals[pitch]/4; //set the motor speed to specified pitch
+ 
 }
 
-
-void serialrx()
+void handleNoteOff(byte channel, byte pitch, byte velocity) //MIDI Note OFF Command
 {
-
-  int id, speed;
-  
-  
-    if(serial.readable() ) 
-    {
-      serial.scanf("%d %d", &id, &speed );
-      printf("%d %d\n", id, speed);
-      if (id==JOINT)
-      {
-        led=!led;
-        current_speed=speed;
-      }
-    
-    
-  }
+  motorSpeeds[channel] = 0; //set motor speed to zero
+  flag=!flag;
 }
-  
-  void fmotor()
-  {
-  
-      
-      if (current_speed>0)
-      {
-        printf("run FWD\n");
-        motor->set_max_speed(abs(current_speed*80));
-        motor->run(StepperMotor::BWD);
-      }
-      else if (current_speed<0)
-      {
-        printf("run BWD\n");
-        motor->set_max_speed(abs(current_speed*80));
-        motor->run(StepperMotor::FWD);
-      }
-      
-      else
-      {
-        motor->hard_stop();
-        current_pose= motor->get_position();
-        motor->go_to(current_pose);
-      }
-      
-      
-      
-    
-  }
-  
-  
   /* Main ----------------------------------------------------------------------*/
   
   int main()
   {
     led=1;
-    serial.baud(BAUDRATE);
+    
+    MIDI.begin(MIDI_CHANNEL_OMNI); //listen to all MIDI channels
+    MIDI.setHandleNoteOn(handleNoteOn); //execute function when note on message is recieved
+    MIDI.setHandleNoteOff(handleNoteOff); //execute function when note off message is recieved
     
     // Motor Initialization
     motor = new L6208(D2, D8, D7, D4, D5, D6, VREFA_PWM_PIN, VREFB_PWM_PIN);
@@ -130,24 +68,27 @@
       printf("ERROR: vvMotor Init\n\r");
       exit(EXIT_FAILURE);
     }
-    
-    motor->attach_error_handler(&motor_error_handler);
-    
-    
-   // end1.rise(&end1_int_handler);
+
     
-    printf("DONE: Motor Init\n\r");
-    
-    
+    printf("DONE: Motor Init\n\r"); 
     printf("Running!\n\r");
-    
-    //zero();
-    
+     
     while(true)
     {
-      serialrx();
-      //wait (0.001);
-      fmotor();
+      MIDI.read();
+      if(motorSpeeds[0]==0)
+        motor->hard_stop();
+      else
+      {
+        
+        motor->set_max_speed(motorSpeeds[0]);
+        if (flag==1)
+        motor->run(StepperMotor::BWD);
+        else
+        motor->run(StepperMotor::FWD);
+        
+       
       
-    }
-  }
\ No newline at end of file
+      }
+  }
+}
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/pitches.h	Tue Oct 01 10:42:23 2019 +0000
@@ -0,0 +1,132 @@
+const long pitchVals[] = {
+  0, //0, C-1
+  0, //1, C#-1
+  0, //2, D-1
+  0, //3, D#-1
+  0, //4, E-1
+  0, //5, F-1
+  0, //6, F#-1
+  0, //7, G-1
+  0, //8, G#-1
+  0, //9, A-1
+  0, //10, A#-1
+  0, //11, B-1
+  0, //12, C0
+  0, //13, C#0
+  0, //14, D0
+  0, //15, D#0
+  0, //16, E0
+  0, //17, F0
+  0, //18, F#0
+  0, //19, G0
+  0, //20, G#0
+  0, //21, A0
+  0, //22, A#0
+  32000, //  23  B0 31
+  30303,  //  24  C1  33
+  28571, //  25  CS1 35
+  27027, //  26  D1  37
+  25641, //  27  DS1 39
+  24390,  //  28  E1  41
+  22727, //  29  F1  44
+  21739, //  30  FS1 46
+  20408, //  31  G1  49
+  19230, //  32  GS1 52
+  18182, //  33  A1  55
+  17241, //  34  AS1 58
+  16129, //  35  B1  62
+  15385, //  36  C2  65
+  14493, //  37  CS2 69
+  13699, //  38  D2  73
+  12821, //  39  DS2 78
+  12195, //  40  E2  82
+  11494, //  41  F2  87
+  10753, //  42  FS2 93
+  10204, //  43  G2  98
+  9615, //  44  GS2 104
+  9091, //  45  A2  110
+  8547, //  46  AS2 117
+  8130, //  47  B2  123
+  7634, //  48  C3  131
+  7194, //  49  CS3 139
+  6803, //  50  D3  147
+  6410,  //  51  DS3 156
+  6061, //  52  E3  165
+  5714, //  53  F3  175
+  5405, //  54  FS3 185
+  5102, //  55  G3  196
+  4808, //  56  GS3 208
+  4545, //  57  A3  220
+  4292, //  58  AS3 233
+  4049, //  59  B3  247
+  3817, //  60  C4  262
+  3610, //  61  CS4 277
+  3401, //  62  D4  294
+  3215, //  63  DS4 311
+  3030,  //  64  E4  330
+  2865, //  65  F4  349
+  2703, //  66  FS4 370
+  2551, //  67  G4  392
+  2410, //  68  GS4 415
+  2273, //  69  A4  440
+  2146, //  70  AS4 466
+  2024, //  71  B4  494
+  1912, //  72  C5  523
+  1805, //  73  CS5 554
+  1704, //  74  D5  587
+  1608, //  75  DS5 622
+  1517, //  76  E5  659
+  1433, //  77  F5  698
+  1351, //  78  FS5 740
+  1276, //  79  G5  784
+  1203, //  80  GS5 831
+  1136, //  81  A5  880
+  1073, //  82  AS5 932
+  1012, //  83  B5  988
+  955, //  84  C6  1047
+  902, //  85  CS6 1109
+  851, //  86  D6  1175
+  803, //  87  DS6 1245
+  758, //  88  E6  1319
+  716, //  89  F6  1397
+  676, //  90  FS6 1480
+  638,  //  91  G6  1568
+  602, //  92  GS6 1661
+  568, //  93  A6  1760
+  536, //  94  AS6 1865
+  506, //  95  B6  1976
+  478, //  96  C7  2093
+  451,  //  97  CS7 2217
+  426, //  98  D7  2349
+  402, //  99  DS7 2489
+  379, //  100 E7  2637
+  358, //  101 F7  2794
+  338, //  102 FS7 2960
+  315,  //  103 G7  3136
+  301, //  104 GS7 3322
+  284, //  105 A7  3520
+  268, //  106 AS7 3729
+  253, //  107 B7  3951
+  239, //  108 C8  4186
+  225, //  109 CS8 4435
+  213, //  110 D8  4699
+  201, //  111 DS8 4978
+  0, //112, E8
+  0, //113, F8
+  0, //114, F#8
+  0, //115, G8
+  0, //116, G#8
+  0, //117, A8
+  0, //118, A#8
+  0, //119, B8
+  0, //120, C9
+  0, //121, C#9
+  0, //122, D9
+  0, //123, D#9
+  0, //124, E9
+  0, //125, F9
+  0, //126, F#9
+  0, //127, G9
+};
+
+