![](/media/cache/img/default_profile.jpg.50x50_q85.jpg)
my new gear...
actuator/DCmotor.cpp@2:e7b09385d197, 2022-03-27 (annotated)
- Committer:
- yootee
- Date:
- Sun Mar 27 04:39:16 2022 +0000
- Revision:
- 2:e7b09385d197
arrc?
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
yootee | 2:e7b09385d197 | 1 | #include<DCmotor.hpp> |
yootee | 2:e7b09385d197 | 2 | |
yootee | 2:e7b09385d197 | 3 | DCmotor::DCmotor(Port motor, float max_pwm, float min_pwm, bool CW_flag):motor_(motor),max_pwm_(max_pwm),min_pwm_(min_pwm),CW_flag_(CW_flag){ |
yootee | 2:e7b09385d197 | 4 | } |
yootee | 2:e7b09385d197 | 5 | |
yootee | 2:e7b09385d197 | 6 | DCmotor::DCmotor(PinName pin_a, PinName pin_b, float max_pwm, float min_pwm, bool CW_flag):max_pwm_(max_pwm),min_pwm_(min_pwm),CW_flag_(CW_flag){ |
yootee | 2:e7b09385d197 | 7 | motor_.pin[0] = pin_a; |
yootee | 2:e7b09385d197 | 8 | motor_.pin[1] = pin_b; |
yootee | 2:e7b09385d197 | 9 | } |
yootee | 2:e7b09385d197 | 10 | |
yootee | 2:e7b09385d197 | 11 | void DCmotor::drive(float pwm){ |
yootee | 2:e7b09385d197 | 12 | float output_pwm = (abs(pwm) < min_pwm_ ? min_pwm_ : abs(pwm) > max_pwm_ ? max_pwm_ : abs(pwm)); |
yootee | 2:e7b09385d197 | 13 | pwm = (CW_flag_ == false ? -pwm : pwm); |
yootee | 2:e7b09385d197 | 14 | if(!pwm) { |
yootee | 2:e7b09385d197 | 15 | DigitalOut Moter1(motor_.pin[0],0); |
yootee | 2:e7b09385d197 | 16 | DigitalOut Moter2(motor_.pin[1],0); |
yootee | 2:e7b09385d197 | 17 | } else if(pwm < 0) { |
yootee | 2:e7b09385d197 | 18 | PwmOut Moter1(motor_.pin[0]); |
yootee | 2:e7b09385d197 | 19 | Moter1.period_us(256); |
yootee | 2:e7b09385d197 | 20 | Moter1.write(output_pwm); |
yootee | 2:e7b09385d197 | 21 | DigitalOut Moter2(motor_.pin[1],0); |
yootee | 2:e7b09385d197 | 22 | } else { |
yootee | 2:e7b09385d197 | 23 | DigitalOut Moter1(motor_.pin[0],0); |
yootee | 2:e7b09385d197 | 24 | PwmOut Moter2(motor_.pin[1]); |
yootee | 2:e7b09385d197 | 25 | Moter2.period_us(256); |
yootee | 2:e7b09385d197 | 26 | Moter2.write(output_pwm); |
yootee | 2:e7b09385d197 | 27 | } |
yootee | 2:e7b09385d197 | 28 | } |