first
Dependents: 17robo_fuzi 17robo_tokyo_kaede
motor_drive.cpp@0:aefa71eaf32a, 2017-09-24 (annotated)
- Committer:
- echo_piyo
- Date:
- Sun Sep 24 05:24:19 2017 +0000
- Revision:
- 0:aefa71eaf32a
????2??
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
echo_piyo | 0:aefa71eaf32a | 1 | #include "motor_drive.h" |
echo_piyo | 0:aefa71eaf32a | 2 | |
echo_piyo | 0:aefa71eaf32a | 3 | MotorDrive::MotorDrive (PinName Direction,PinName Pwm) : direction(Direction), pwm(Pwm){ |
echo_piyo | 0:aefa71eaf32a | 4 | direction = 0; |
echo_piyo | 0:aefa71eaf32a | 5 | pwm = 0; |
echo_piyo | 0:aefa71eaf32a | 6 | } |
echo_piyo | 0:aefa71eaf32a | 7 | |
echo_piyo | 0:aefa71eaf32a | 8 | void MotorDrive::setup(float frequency, float acceleration, float timerCycle){ |
echo_piyo | 0:aefa71eaf32a | 9 | float period = 1/frequency; |
echo_piyo | 0:aefa71eaf32a | 10 | pwm.period(period); |
echo_piyo | 0:aefa71eaf32a | 11 | a = acceleration*timerCycle; |
echo_piyo | 0:aefa71eaf32a | 12 | } |
echo_piyo | 0:aefa71eaf32a | 13 | |
echo_piyo | 0:aefa71eaf32a | 14 | void MotorDrive::output(float targetDuty){ |
echo_piyo | 0:aefa71eaf32a | 15 | if (fabs(targetDuty-duty) <= a) { |
echo_piyo | 0:aefa71eaf32a | 16 | duty = targetDuty; |
echo_piyo | 0:aefa71eaf32a | 17 | } else if (duty < targetDuty) { |
echo_piyo | 0:aefa71eaf32a | 18 | duty = duty + a; |
echo_piyo | 0:aefa71eaf32a | 19 | } else if (duty > targetDuty) { |
echo_piyo | 0:aefa71eaf32a | 20 | duty = duty - a; |
echo_piyo | 0:aefa71eaf32a | 21 | } |
echo_piyo | 0:aefa71eaf32a | 22 | |
echo_piyo | 0:aefa71eaf32a | 23 | if (duty < 0) { |
echo_piyo | 0:aefa71eaf32a | 24 | direction = 1; |
echo_piyo | 0:aefa71eaf32a | 25 | if (duty > DUTY_LIMIT) { |
echo_piyo | 0:aefa71eaf32a | 26 | duty = DUTY_LIMIT; |
echo_piyo | 0:aefa71eaf32a | 27 | } |
echo_piyo | 0:aefa71eaf32a | 28 | } else { |
echo_piyo | 0:aefa71eaf32a | 29 | direction = 0; |
echo_piyo | 0:aefa71eaf32a | 30 | if (duty < -DUTY_LIMIT) { |
echo_piyo | 0:aefa71eaf32a | 31 | duty = -DUTY_LIMIT; |
echo_piyo | 0:aefa71eaf32a | 32 | } |
echo_piyo | 0:aefa71eaf32a | 33 | } |
echo_piyo | 0:aefa71eaf32a | 34 | |
echo_piyo | 0:aefa71eaf32a | 35 | pwm = fabs(duty); |
echo_piyo | 0:aefa71eaf32a | 36 | } |