Library version for DC_Stepper_Controller_Lib with PWM speed control

Dependencies:   mbed QEI PID

Dependents:   DR-ArmServoTest Auto_DC_pick_class MBed_TR1 ros_button_2021

main.cpp

Committer:
stivending
Date:
2021-05-19
Revision:
4:b242191ed0cf
Parent:
3:1229fe22fdcd
Child:
5:c040faf21e07

File content as of revision 4:b242191ed0cf:

#include "mbed.h"
#include "QEI.h"

DigitalOut out1(D2);    //  Motor direction control pin 1
DigitalOut out2(D4);    //  Motor direction control pin 2
DigitalIn home_button(D13);     // Button for setting the home button
QEI dc_motor (D8,D7,NC,792);    // Create QEI object for increment encoder
int counter = 0;    // Dummy counter for sample program

void reset(){       // Setting home point for increment encoder
    /*while (home_button == 0){       // Continue to turn clockwise until home button pressed
        out1 = 1;
        out2 = 0;
    }*/
    out1 = out2 = 0;
    dc_motor.reset();       //  Reset pulse_
    wait(1);
 };
 
void goto_angle(int angle){      //  Move motor to specific angle from home point
    int tar_pulse = ((double) angle / 360.0)* (2.0 * 792.0);    //  Calculating target pulse number
    int cur_pulse = dc_motor.getPulses();
    
    while ( tar_pulse != cur_pulse ){

        if (tar_pulse > cur_pulse){     //  Rotate motor clockwise
            out1 = 1;
            out2 = 0;
        }
        else {                          //  Rotate motor counter clockwrise 
            out1 = 0;
            out2 = 1;
        }
        cur_pulse = dc_motor.getPulses();   //  Get increment encoder current pulse number
    }
    out1 = 0;       //  Stop motor
    out2 = 0;
};


void move_angle(int angle){      //  move for relative distance
    reset();
    goto_angle(angle);
    reset();
};
 

 
int main() {        //  Sample Testing program
    reset();
    move_angle(90);
    wait(1);
    move_angle(-90);
    wait(1);
    move_angle(-90);
    wait(1);
}