ai_car1
Dependencies: mbed ai_car ros_lib_melodic
Actuator/Motor.cpp@0:a35213e1e14e, 2021-05-03 (annotated)
- Committer:
- wngudwls000
- Date:
- Mon May 03 07:22:52 2021 +0000
- Revision:
- 0:a35213e1e14e
45
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
wngudwls000 | 0:a35213e1e14e | 1 | #include "Motor.h" |
wngudwls000 | 0:a35213e1e14e | 2 | |
wngudwls000 | 0:a35213e1e14e | 3 | |
wngudwls000 | 0:a35213e1e14e | 4 | Motor::Motor(PinName IN1, PinName IN2, PinName INH1, PinName INH2) |
wngudwls000 | 0:a35213e1e14e | 5 | : IN1_(IN1), IN2_(IN2), INH1_(INH1), INH2_(INH2) |
wngudwls000 | 0:a35213e1e14e | 6 | { |
wngudwls000 | 0:a35213e1e14e | 7 | INH1_=1; |
wngudwls000 | 0:a35213e1e14e | 8 | INH2_=1; |
wngudwls000 | 0:a35213e1e14e | 9 | IN1_.period_us(50); |
wngudwls000 | 0:a35213e1e14e | 10 | IN2_.period_us(50); |
wngudwls000 | 0:a35213e1e14e | 11 | } |
wngudwls000 | 0:a35213e1e14e | 12 | |
wngudwls000 | 0:a35213e1e14e | 13 | void Motor::setSpeed_percent(float percent, char direction) |
wngudwls000 | 0:a35213e1e14e | 14 | { |
wngudwls000 | 0:a35213e1e14e | 15 | float duty = percent/100.0; |
wngudwls000 | 0:a35213e1e14e | 16 | if(duty>1.0) { |
wngudwls000 | 0:a35213e1e14e | 17 | duty=1.0; |
wngudwls000 | 0:a35213e1e14e | 18 | } else if(duty<0.0) { |
wngudwls000 | 0:a35213e1e14e | 19 | duty=0.0; |
wngudwls000 | 0:a35213e1e14e | 20 | } |
wngudwls000 | 0:a35213e1e14e | 21 | |
wngudwls000 | 0:a35213e1e14e | 22 | if(direction==FORWARD) |
wngudwls000 | 0:a35213e1e14e | 23 | { |
wngudwls000 | 0:a35213e1e14e | 24 | IN1_=duty; |
wngudwls000 | 0:a35213e1e14e | 25 | IN2_=0; |
wngudwls000 | 0:a35213e1e14e | 26 | } |
wngudwls000 | 0:a35213e1e14e | 27 | else if (direction == BACKWARD){ |
wngudwls000 | 0:a35213e1e14e | 28 | IN1_=0; |
wngudwls000 | 0:a35213e1e14e | 29 | IN2_=duty; |
wngudwls000 | 0:a35213e1e14e | 30 | } |
wngudwls000 | 0:a35213e1e14e | 31 | else if (direction == BREAK) |
wngudwls000 | 0:a35213e1e14e | 32 | { |
wngudwls000 | 0:a35213e1e14e | 33 | IN1_=1; |
wngudwls000 | 0:a35213e1e14e | 34 | IN2_=1; |
wngudwls000 | 0:a35213e1e14e | 35 | } |
wngudwls000 | 0:a35213e1e14e | 36 | else |
wngudwls000 | 0:a35213e1e14e | 37 | { |
wngudwls000 | 0:a35213e1e14e | 38 | IN1_=0; |
wngudwls000 | 0:a35213e1e14e | 39 | IN2_=0; |
wngudwls000 | 0:a35213e1e14e | 40 | } |
wngudwls000 | 0:a35213e1e14e | 41 | } |