Control project for the Lift-arm. Works with ROS Melodic
Dependencies: mbed Servo ros_lib_melodic ULN2003_StepperDriver Async_4pin_Stepper
src/motor.cpp@4:9edb248c6431, 2021-05-31 (annotated)
- Committer:
- krogedal
- Date:
- Mon May 31 16:47:02 2021 +0000
- Revision:
- 4:9edb248c6431
- Parent:
- 0:441289ea4e29
Updated to work with ROS
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
krogedal | 0:441289ea4e29 | 1 | /* Karbot motor class |
krogedal | 0:441289ea4e29 | 2 | * Written by Simon Krogedal |
krogedal | 0:441289ea4e29 | 3 | * 27/05/21 |
krogedal | 0:441289ea4e29 | 4 | * Team 9 4th Year project |
krogedal | 0:441289ea4e29 | 5 | * |
krogedal | 0:441289ea4e29 | 6 | * for NUCLEO-F401RE |
krogedal | 0:441289ea4e29 | 7 | * |
krogedal | 0:441289ea4e29 | 8 | */ |
krogedal | 0:441289ea4e29 | 9 | |
krogedal | 0:441289ea4e29 | 10 | |
krogedal | 0:441289ea4e29 | 11 | #include "mbed.h" |
krogedal | 0:441289ea4e29 | 12 | #include "motor.h" |
krogedal | 0:441289ea4e29 | 13 | |
krogedal | 0:441289ea4e29 | 14 | motor::motor(PinName pwm_pin, PinName dir_pin, double period) : output(pwm_pin), dir(dir_pin), T(period) { |
krogedal | 0:441289ea4e29 | 15 | output.period(T); //this period is not going to change during the run |
krogedal | 0:441289ea4e29 | 16 | dir = 1; //forward is active high |
krogedal | 0:441289ea4e29 | 17 | dutCyc = 0.1; //just need a default value to avoid bugs |
krogedal | 0:441289ea4e29 | 18 | stop(); //starts off |
krogedal | 0:441289ea4e29 | 19 | } |
krogedal | 0:441289ea4e29 | 20 | |
krogedal | 0:441289ea4e29 | 21 | void motor::drive(void) { |
krogedal | 0:441289ea4e29 | 22 | driving = 1; |
krogedal | 0:441289ea4e29 | 23 | output.write(dutCyc); |
krogedal | 0:441289ea4e29 | 24 | } |
krogedal | 0:441289ea4e29 | 25 | |
krogedal | 0:441289ea4e29 | 26 | void motor::stop(void) { |
krogedal | 0:441289ea4e29 | 27 | driving = 0; |
krogedal | 0:441289ea4e29 | 28 | output.write(0.0); //just constant low |
krogedal | 0:441289ea4e29 | 29 | } |
krogedal | 0:441289ea4e29 | 30 | |
krogedal | 0:441289ea4e29 | 31 | void motor::setOut(double dc) { //set duty cycle as a number between -1 and 1, where 1 is full force forward, -1 is backwards and 0 is still |
krogedal | 0:441289ea4e29 | 32 | if(dc < 0.0) { |
krogedal | 0:441289ea4e29 | 33 | dir = 0; |
krogedal | 0:441289ea4e29 | 34 | if(dc < -1.0) |
krogedal | 0:441289ea4e29 | 35 | dutCyc = 1.0; |
krogedal | 0:441289ea4e29 | 36 | else |
krogedal | 0:441289ea4e29 | 37 | dutCyc = -dc; |
krogedal | 0:441289ea4e29 | 38 | } |
krogedal | 0:441289ea4e29 | 39 | else { |
krogedal | 0:441289ea4e29 | 40 | dir = 1; |
krogedal | 0:441289ea4e29 | 41 | if(dc > 1.0) |
krogedal | 0:441289ea4e29 | 42 | dutCyc = 1.0; |
krogedal | 0:441289ea4e29 | 43 | else |
krogedal | 0:441289ea4e29 | 44 | dutCyc = dc; |
krogedal | 0:441289ea4e29 | 45 | } |
krogedal | 0:441289ea4e29 | 46 | if(driving) |
krogedal | 0:441289ea4e29 | 47 | output.write(dutCyc); |
krogedal | 0:441289ea4e29 | 48 | } |
krogedal | 0:441289ea4e29 | 49 | |
krogedal | 0:441289ea4e29 | 50 | double motor::getPeriod(void) {return T;} |
krogedal | 0:441289ea4e29 | 51 | |
krogedal | 0:441289ea4e29 | 52 | double motor::getDuty(void) {return dutCyc;} |
krogedal | 0:441289ea4e29 | 53 |