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
Diff: main.cpp
- Revision:
- 34:215d5ee0f434
- Parent:
- 33:c1cefad6d338
--- 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