![](/media/cache/group/default_image.jpg.50x50_q85.jpg)
Library version for DC_Stepper_Controller_Lib with PWM speed control
Dependents: DR-ArmServoTest Auto_DC_pick_class MBed_TR1 ros_button_2021
Diff: DC_Motor_Controller.h
- Revision:
- 5:c040faf21e07
- Child:
- 7:6e59ed00a6a9
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/DC_Motor_Controller.h Wed May 19 08:25:06 2021 +0000 @@ -0,0 +1,55 @@ +#include "mbed.h" +#include "QEI.h" + +#ifndef DC_MOTOR_CONTROLLER_H +#define DC_MOTOR_CONTROLLER_H + +class DC_Motor_Controller { + + private: + + DigitalOut out1, out2; // Motor direction control pin 2 + QEI dc_motor; //(D8,D7,NC,792); // Create QEI object for increment encoder + 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; + }; + + public: + DC_Motor_Controller(PinName out1_p, PinName out2_p, PinName in1_p, PinName in2_p, int PPR) : out1(out1_p), out2(out2_p), dc_motor(in1_p, in2_p, NC, PPR) + { + } + + 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 move_angle(int angle){ // move for relative distance + reset(); + goto_angle(angle); + reset(); + }; + +}; +#endif \ No newline at end of file