NerfUS / Servomotor

Dependents:   ServomotorTests NerfUSTarget

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers Servomotor.cpp Source File

Servomotor.cpp

00001 #include "Servomotor.hpp"
00002 
00003 Servomotor::Servomotor(PwmOutInterface &pwm_out) : 
00004     up_angle(90),
00005     pwm_out(pwm_out)
00006 {
00007     
00008 }
00009 
00010 //Angle 0 degree = duty cycle 500 ns
00011 //Angle 90 degree = duty cycle 1500 ns
00012 //Angle 180 degree = duty cycle 2500 ns
00013 void Servomotor::set_angle(const float angle_degree)
00014 {
00015     const float duty_cycle_us = (angle_degree / 180.0) * 2000 + 500;
00016     pwm_out.pulsewidth_us(duty_cycle_us);
00017 }
00018 
00019 void Servomotor::set_position_up()
00020 {
00021     set_angle(up_angle);
00022 }
00023 
00024 void Servomotor::set_position_down()
00025 {
00026     set_angle(up_angle - 90);
00027 }