MAV'RIC
/home/travis/build/lis-epfl/MAVRIC_Library/drivers/servo.hpp
00001 /*******************************************************************************
00002  * Copyright (c) 2009-2016, MAV'RIC Development Team
00003  * All rights reserved.
00004  *
00005  * Redistribution and use in source and binary forms, with or without
00006  * modification, are permitted provided that the following conditions are met:
00007  *
00008  * 1. Redistributions of source code must retain the above copyright notice,
00009  * this list of conditions and the following disclaimer.
00010  *
00011  * 2. Redistributions in binary form must reproduce the above copyright notice,
00012  * this list of conditions and the following disclaimer in the documentation
00013  * and/or other materials provided with the distribution.
00014  *
00015  * 3. Neither the name of the copyright holder nor the names of its contributors
00016  * may be used to endorse or promote products derived from this software without
00017  * specific prior written permission.
00018  *
00019  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
00020  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00021  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
00022  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
00023  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
00024  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
00025  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
00026  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
00027  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
00028  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
00029  * POSSIBILITY OF SUCH DAMAGE.
00030  ******************************************************************************/
00031 
00032 /*******************************************************************************
00033  * \file servo.hpp
00034  *
00035  * \author MAV'RIC Team
00036  * \author Julien Lecoeur
00037  *
00038  * \brief Driver for servomotors using PWM
00039  *
00040  ******************************************************************************/
00041 
00042 
00043 #ifndef SERVO_HPP_
00044 #define SERVO_HPP_
00045 
00046 #include <cstdint>
00047 #include <cstdbool>
00048 
00049 #include "hal/common/pwm.hpp"
00050 
00051 
00055 typedef struct
00056 {
00057     float    trim;                  
00058     float    min;                   
00059     float    max;                   
00060     float    failsafe;              
00061     uint32_t repeat_freq;           
00062     uint16_t pulse_center_us;       
00063     uint16_t pulse_magnitude_us;    
00064 } servo_conf_t;
00065 
00066 
00072 static inline servo_conf_t servo_default_config_standard();
00073 
00074 
00080 static inline servo_conf_t servo_default_config_esc();
00081 
00082 
00086 class Servo
00087 {
00088 public:
00094     Servo(Pwm& pwm, const servo_conf_t config = servo_default_config_standard());
00095 
00096 
00102     bool init(void);
00103 
00104 
00110     float read(void) const;
00111 
00112 
00121     bool write(float value, bool to_hardware = true);
00122 
00123 
00131     bool failsafe(bool to_hardware = true);
00132 
00133 
00139     bool write_to_hardware(void);
00140 
00141 
00148     void calibrate_esc(void);
00149 
00153     void set_servo_max(void);
00154 
00158     void set_servo_min(void);
00159 
00163     float servo_max(void);
00164 
00168     float servo_min(void);
00169 
00170 private:
00171     Pwm&            pwm_;       
00172     servo_conf_t    config_;    
00173     float           value_;     
00174 };
00175 
00176 
00182 static inline servo_conf_t servo_default_config_standard()
00183 {
00184     servo_conf_t conf       = {};
00185 
00186     conf.trim               = 0.0f;
00187     conf.min                = -1.0f;
00188     conf.max                = 1.0f;
00189     conf.failsafe           = 0.0f;
00190     conf.repeat_freq        = 50;
00191     conf.pulse_center_us    = 1500;
00192     conf.pulse_magnitude_us = 500;
00193 
00194     return conf;
00195 };
00196 
00197 
00203 static inline servo_conf_t servo_default_config_esc()
00204 {
00205     servo_conf_t conf       = {};
00206 
00207     conf.trim               = 0.0f;
00208     conf.min                = -0.9f;
00209     conf.max                = 1.0f;
00210     conf.failsafe           = -1.1f;
00211     conf.repeat_freq        = 200;
00212     conf.pulse_center_us    = 1500;
00213     conf.pulse_magnitude_us = 500;
00214 
00215     return conf;
00216 };
00217 
00218 
00224 static inline servo_conf_t servo_default_config_brush_motor()
00225 {
00226     servo_conf_t conf       = {};
00227 
00228     conf.trim               = 0.0f;
00229     conf.min                = -1.0f;
00230     conf.max                = 1.0f;
00231     conf.failsafe           = -1.0f;
00232     conf.repeat_freq        = 4000; // period = 250us
00233     conf.pulse_center_us    = 125;
00234     conf.pulse_magnitude_us = 125;  // duty cycle: 0 to 100%
00235 
00236     return conf;
00237 };
00238 
00239 #endif /* SERVO_HPP_ */
 All Classes Files Functions Variables Typedefs Enumerations Enumerator Friends Defines