Luigi di rado / Mbed 2 deprecated Arm_stepper_MIDI

Dependencies:   MIDI mbed X-NUCLEO-IHM05A1

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 #include "mbed.h"
00002 #include "L6208.h"
00003 #include "pitches.h"
00004 #include "MIDI.h"
00005 
00006 #define VREFA_PWM_PIN D3
00007 #define VREFB_PWM_PIN D9
00008 
00009 
00010 l6208_init_t init =
00011 {
00012   65000,            //Acceleration rate in step/s^2 or (1/16)th step/s^2 for microstep modes
00013   80,              //Acceleration current torque in % (from 0 to 100)
00014   65000,            //Deceleration rate in step/s^2 or (1/16)th step/s^2 for microstep modes
00015   80,              //Deceleration current torque in % (from 0 to 100)
00016   8000,            //Running speed in step/s or (1/16)th step/s for microstep modes
00017   80,              //Running current torque in % (from 0 to 100)
00018   40,              //Holding current torque in % (from 0 to 100)
00019   STEP_MODE_1_16,  //Step mode via enum motorStepMode_t
00020   FAST_DECAY,      //Decay mode via enum motorDecayMode_t
00021   0,               //Dwelling time in ms
00022   FALSE,           //Automatic HIZ STOP
00023   100000           //VREFA and VREFB PWM frequency (Hz)
00024 };
00025 
00026 unsigned long motorSpeeds[] = {0, 0, 0, 0, 0};
00027 int flag=0;
00028 // Utility
00029 //InterruptIn button(USER_BUTTON);
00030 DigitalOut led(LED1);
00031 
00032 // Motor Control
00033 L6208 *motor;
00034 
00035 InterruptIn end1(USER_BUTTON, PullUp);
00036 DigitalIn end0(PA_5, PullUp);
00037 
00038     
00039 MIDI MIDI(PA_2, PA_3);
00040   
00041 void handleNoteOn(byte channel, byte pitch, byte velocity) //MIDI Note ON Command
00042 {
00043   
00044   motorSpeeds[channel] = pitchVals[pitch]/4; //set the motor speed to specified pitch
00045  
00046 }
00047 
00048 void handleNoteOff(byte channel, byte pitch, byte velocity) //MIDI Note OFF Command
00049 {
00050   motorSpeeds[channel] = 0; //set motor speed to zero
00051   flag=!flag;
00052 }
00053   /* Main ----------------------------------------------------------------------*/
00054   
00055   int main()
00056   {
00057     led=1;
00058     
00059     MIDI.begin(MIDI_CHANNEL_OMNI); //listen to all MIDI channels
00060     MIDI.setHandleNoteOn(handleNoteOn); //execute function when note on message is recieved
00061     MIDI.setHandleNoteOff(handleNoteOff); //execute function when note off message is recieved
00062     
00063     // Motor Initialization
00064     motor = new L6208(D2, D8, D7, D4, D5, D6, VREFA_PWM_PIN, VREFB_PWM_PIN);
00065     motor->set_step_mode(StepperMotor::STEP_MODE_1_16);
00066     if (motor->init(&init) != COMPONENT_OK)
00067     {
00068       printf("ERROR: vvMotor Init\n\r");
00069       exit(EXIT_FAILURE);
00070     }
00071 
00072     
00073     printf("DONE: Motor Init\n\r"); 
00074     printf("Running!\n\r");
00075      
00076     while(true)
00077     {
00078       MIDI.read();
00079       if(motorSpeeds[0]==0)
00080         motor->hard_stop();
00081       else
00082       {
00083         
00084         motor->set_max_speed(motorSpeeds[0]);
00085         if (flag==1)
00086         motor->run(StepperMotor::BWD);
00087         else
00088         motor->run(StepperMotor::FWD);
00089         
00090        
00091       
00092       }
00093   }
00094 }