Servomotor

Dependents:   ServomotorTests NerfUSTarget

Committer:
GaiSensei
Date:
Sun Apr 09 23:15:03 2017 +0000
Revision:
6:8d8268e5a561
Parent:
5:003d3d682097
Child:
7:a5b6e6d9ad3d
Nano seconds to micro seconds

Who changed what in which revision?

UserRevisionLine numberNew 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
GaiSensei 5:003d3d682097 9 //Angle 0 degree = duty cycle 500 ns
GaiSensei 5:003d3d682097 10 //Angle 90 degree = duty cycle 1500 ns
GaiSensei 5:003d3d682097 11 //Angle 180 degree = duty cycle 2500 ns
dupm2216 0:46b4e7d76586 12 void Servomotor::set_angle(const float angle_degree)
dupm2216 0:46b4e7d76586 13 {
GaiSensei 6:8d8268e5a561 14 const float duty_cycle_us = (angle_degree / 180.0) * 2000 + 500;
GaiSensei 6:8d8268e5a561 15 pwm_out.pulsewidth_us(duty_cycle_us);
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 }