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

Committer:
stivending
Date:
Wed May 19 08:25:06 2021 +0000
Revision:
5:c040faf21e07
Child:
7:6e59ed00a6a9
DC_MOTOR works as a library in classes;

Who changed what in which revision?

UserRevisionLine numberNew contents of line
stivending 5:c040faf21e07 1 #include "mbed.h"
stivending 5:c040faf21e07 2 #include "QEI.h"
stivending 5:c040faf21e07 3
stivending 5:c040faf21e07 4 #ifndef DC_MOTOR_CONTROLLER_H
stivending 5:c040faf21e07 5 #define DC_MOTOR_CONTROLLER_H
stivending 5:c040faf21e07 6
stivending 5:c040faf21e07 7 class DC_Motor_Controller {
stivending 5:c040faf21e07 8
stivending 5:c040faf21e07 9 private:
stivending 5:c040faf21e07 10
stivending 5:c040faf21e07 11 DigitalOut out1, out2; // Motor direction control pin 2
stivending 5:c040faf21e07 12 QEI dc_motor; //(D8,D7,NC,792); // Create QEI object for increment encoder
stivending 5:c040faf21e07 13 void goto_angle(int angle){ // Move motor to specific angle from home point
stivending 5:c040faf21e07 14 int tar_pulse = ((double) angle / 360.0)* (2.0 * 792.0); // Calculating target pulse number
stivending 5:c040faf21e07 15 int cur_pulse = dc_motor.getPulses();
stivending 5:c040faf21e07 16
stivending 5:c040faf21e07 17 while ( tar_pulse != cur_pulse ){
stivending 5:c040faf21e07 18
stivending 5:c040faf21e07 19 if (tar_pulse > cur_pulse){ // Rotate motor clockwise
stivending 5:c040faf21e07 20 out1 = 1;
stivending 5:c040faf21e07 21 out2 = 0;
stivending 5:c040faf21e07 22 }
stivending 5:c040faf21e07 23 else { // Rotate motor counter clockwrise
stivending 5:c040faf21e07 24 out1 = 0;
stivending 5:c040faf21e07 25 out2 = 1;
stivending 5:c040faf21e07 26 }
stivending 5:c040faf21e07 27 cur_pulse = dc_motor.getPulses(); // Get increment encoder current pulse number
stivending 5:c040faf21e07 28 }
stivending 5:c040faf21e07 29 out1 = 0; // Stop motor
stivending 5:c040faf21e07 30 out2 = 0;
stivending 5:c040faf21e07 31 };
stivending 5:c040faf21e07 32
stivending 5:c040faf21e07 33 public:
stivending 5:c040faf21e07 34 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)
stivending 5:c040faf21e07 35 {
stivending 5:c040faf21e07 36 }
stivending 5:c040faf21e07 37
stivending 5:c040faf21e07 38 void reset(){ // Setting home point for increment encoder
stivending 5:c040faf21e07 39 /*while (home_button == 0){ // Continue to turn clockwise until home button pressed
stivending 5:c040faf21e07 40 out1 = 1;
stivending 5:c040faf21e07 41 out2 = 0;
stivending 5:c040faf21e07 42 }*/
stivending 5:c040faf21e07 43 out1 = out2 = 0;
stivending 5:c040faf21e07 44 dc_motor.reset(); // Reset pulse_
stivending 5:c040faf21e07 45 //wait(1);
stivending 5:c040faf21e07 46 };
stivending 5:c040faf21e07 47
stivending 5:c040faf21e07 48 void move_angle(int angle){ // move for relative distance
stivending 5:c040faf21e07 49 reset();
stivending 5:c040faf21e07 50 goto_angle(angle);
stivending 5:c040faf21e07 51 reset();
stivending 5:c040faf21e07 52 };
stivending 5:c040faf21e07 53
stivending 5:c040faf21e07 54 };
stivending 5:c040faf21e07 55 #endif