Servomotor
Dependents: ServomotorTests NerfUSTarget
Servomotor.cpp@0:46b4e7d76586, 2017-02-25 (annotated)
- Committer:
- dupm2216
- Date:
- Sat Feb 25 22:57:27 2017 +0000
- Revision:
- 0:46b4e7d76586
Initial commit
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
dupm2216 | 0:46b4e7d76586 | 1 | #include "Servomotor.hpp" |
dupm2216 | 0:46b4e7d76586 | 2 | |
dupm2216 | 0:46b4e7d76586 | 3 | Servomotor::Servomotor(PwmOutInterface &pwm_out) : |
dupm2216 | 0:46b4e7d76586 | 4 | pwm_out(pwm_out) |
dupm2216 | 0:46b4e7d76586 | 5 | { |
dupm2216 | 0:46b4e7d76586 | 6 | |
dupm2216 | 0:46b4e7d76586 | 7 | } |
dupm2216 | 0:46b4e7d76586 | 8 | |
dupm2216 | 0:46b4e7d76586 | 9 | //Angle 0 degree = duty cycle 1 ms |
dupm2216 | 0:46b4e7d76586 | 10 | //Angle 90 degree = duty cycle 1.5 ms |
dupm2216 | 0:46b4e7d76586 | 11 | //Angle 180 degree = duty cycle 2 ms |
dupm2216 | 0:46b4e7d76586 | 12 | void Servomotor::set_angle(const float angle_degree) |
dupm2216 | 0:46b4e7d76586 | 13 | { |
dupm2216 | 0:46b4e7d76586 | 14 | const float duty_cycle_ms = angle_degree / 180.0 + 1.0; |
dupm2216 | 0:46b4e7d76586 | 15 | pwm_out.pulsewidth_ms(duty_cycle_ms); |
dupm2216 | 0:46b4e7d76586 | 16 | } |
dupm2216 | 0:46b4e7d76586 | 17 | |
dupm2216 | 0:46b4e7d76586 | 18 | void Servomotor::set_position_up() |
dupm2216 | 0:46b4e7d76586 | 19 | { |
dupm2216 | 0:46b4e7d76586 | 20 | set_angle(0); |
dupm2216 | 0:46b4e7d76586 | 21 | } |
dupm2216 | 0:46b4e7d76586 | 22 | |
dupm2216 | 0:46b4e7d76586 | 23 | void Servomotor::set_position_down() |
dupm2216 | 0:46b4e7d76586 | 24 | { |
dupm2216 | 0:46b4e7d76586 | 25 | set_angle(90); |
dupm2216 | 0:46b4e7d76586 | 26 | } |