2022-02-16 22:06:07 +00:00
|
|
|
#include "servo_state.hpp"
|
|
|
|
|
|
|
|
namespace servo {
|
2022-03-30 10:49:47 +00:00
|
|
|
ServoState::ServoState()
|
|
|
|
: servo_value(0.0f), last_enabled_pulse(0.0f), enabled(false) {
|
2022-03-21 23:51:15 +00:00
|
|
|
}
|
|
|
|
|
2022-02-17 22:38:59 +00:00
|
|
|
ServoState::ServoState(CalibrationType default_type)
|
2022-03-30 10:49:47 +00:00
|
|
|
: servo_value(0.0f), last_enabled_pulse(0.0f), enabled(false), calib(default_type) {
|
2022-03-21 23:51:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
ServoState::ServoState(const Calibration& calibration)
|
2022-03-30 10:49:47 +00:00
|
|
|
: servo_value(0.0f), last_enabled_pulse(0.0f), enabled(false), calib(calibration) {
|
2022-02-16 22:06:07 +00:00
|
|
|
}
|
|
|
|
|
2022-03-30 10:49:47 +00:00
|
|
|
float ServoState::enable_with_return() {
|
2022-02-19 19:25:15 +00:00
|
|
|
// Has the servo not had a pulse value set before being enabled?
|
2022-02-17 22:38:59 +00:00
|
|
|
if(last_enabled_pulse < MIN_VALID_PULSE) {
|
2022-03-22 14:21:10 +00:00
|
|
|
// Set the servo to its middle
|
2022-03-27 12:53:10 +00:00
|
|
|
return to_mid_with_return();
|
2022-02-16 22:06:07 +00:00
|
|
|
}
|
2022-03-30 10:49:47 +00:00
|
|
|
return _enable_with_return();
|
2022-02-16 22:06:07 +00:00
|
|
|
}
|
|
|
|
|
2022-03-30 10:49:47 +00:00
|
|
|
float ServoState::disable_with_return() {
|
2022-02-16 22:06:07 +00:00
|
|
|
enabled = false;
|
|
|
|
return 0.0f; // A zero pulse
|
|
|
|
}
|
|
|
|
|
2022-03-30 10:49:47 +00:00
|
|
|
float ServoState::_enable_with_return() {
|
2022-02-19 19:25:15 +00:00
|
|
|
enabled = true;
|
|
|
|
return last_enabled_pulse;
|
|
|
|
}
|
|
|
|
|
2022-02-17 22:38:59 +00:00
|
|
|
bool ServoState::is_enabled() const {
|
2022-02-17 17:59:09 +00:00
|
|
|
return enabled;
|
|
|
|
}
|
|
|
|
|
2022-02-17 22:38:59 +00:00
|
|
|
float ServoState::get_pulse() const {
|
2022-02-16 22:06:07 +00:00
|
|
|
return last_enabled_pulse;
|
|
|
|
}
|
|
|
|
|
2022-03-27 12:53:10 +00:00
|
|
|
float ServoState::set_pulse_with_return(float pulse) {
|
2022-02-17 22:38:59 +00:00
|
|
|
if(pulse >= MIN_VALID_PULSE) {
|
2022-02-19 19:25:15 +00:00
|
|
|
float value_out, pulse_out;
|
2022-03-21 23:51:15 +00:00
|
|
|
if(calib.pulse_to_value(pulse, value_out, pulse_out)) {
|
2022-02-19 19:25:15 +00:00
|
|
|
servo_value = value_out;
|
|
|
|
last_enabled_pulse = pulse_out;
|
2022-03-30 10:49:47 +00:00
|
|
|
return _enable_with_return();
|
2022-02-19 19:25:15 +00:00
|
|
|
}
|
2022-02-16 22:06:07 +00:00
|
|
|
}
|
2022-03-30 10:49:47 +00:00
|
|
|
return disable_with_return();
|
2022-02-16 22:06:07 +00:00
|
|
|
}
|
|
|
|
|
2022-03-08 14:26:57 +00:00
|
|
|
float ServoState::get_value() const {
|
|
|
|
return servo_value;
|
|
|
|
}
|
|
|
|
|
2022-03-27 12:53:10 +00:00
|
|
|
float ServoState::set_value_with_return(float value) {
|
2022-03-08 14:26:57 +00:00
|
|
|
float pulse_out, value_out;
|
2022-03-21 23:51:15 +00:00
|
|
|
if(calib.value_to_pulse(value, pulse_out, value_out)) {
|
2022-03-08 14:26:57 +00:00
|
|
|
last_enabled_pulse = pulse_out;
|
|
|
|
servo_value = value_out;
|
2022-03-30 10:49:47 +00:00
|
|
|
return _enable_with_return();
|
2022-03-08 14:26:57 +00:00
|
|
|
}
|
2022-03-30 10:49:47 +00:00
|
|
|
return disable_with_return();
|
2022-03-08 14:26:57 +00:00
|
|
|
}
|
|
|
|
|
2022-02-17 22:38:59 +00:00
|
|
|
float ServoState::get_min_value() const {
|
|
|
|
float value = 0.0f;
|
2022-03-22 14:21:10 +00:00
|
|
|
if(calib.size() > 0) {
|
|
|
|
value = calib.first().value;
|
2022-02-17 22:38:59 +00:00
|
|
|
}
|
|
|
|
return value;
|
|
|
|
}
|
|
|
|
|
|
|
|
float ServoState::get_mid_value() const {
|
|
|
|
float value = 0.0f;
|
2022-03-22 14:21:10 +00:00
|
|
|
if(calib.size() > 0) {
|
|
|
|
const Calibration::Pair &first = calib.first();
|
|
|
|
const Calibration::Pair &last = calib.last();
|
|
|
|
value = (first.value + last.value) / 2.0f;
|
2022-02-17 22:38:59 +00:00
|
|
|
}
|
|
|
|
return value;
|
|
|
|
}
|
|
|
|
|
|
|
|
float ServoState::get_max_value() const {
|
|
|
|
float value = 0.0f;
|
2022-03-22 14:21:10 +00:00
|
|
|
if(calib.size() > 0) {
|
|
|
|
value = calib.last().value;
|
2022-02-17 22:38:59 +00:00
|
|
|
}
|
|
|
|
return value;
|
|
|
|
}
|
|
|
|
|
2022-03-27 12:53:10 +00:00
|
|
|
float ServoState::to_min_with_return() {
|
|
|
|
return set_value_with_return(get_min_value());
|
2022-02-16 22:06:07 +00:00
|
|
|
}
|
|
|
|
|
2022-03-27 12:53:10 +00:00
|
|
|
float ServoState::to_mid_with_return() {
|
|
|
|
return set_value_with_return(get_mid_value());
|
2022-02-16 22:06:07 +00:00
|
|
|
}
|
|
|
|
|
2022-03-27 12:53:10 +00:00
|
|
|
float ServoState::to_max_with_return() {
|
|
|
|
return set_value_with_return(get_max_value());
|
2022-02-16 22:06:07 +00:00
|
|
|
}
|
|
|
|
|
2022-03-27 12:53:10 +00:00
|
|
|
float ServoState::to_percent_with_return(float in, float in_min, float in_max) {
|
2022-02-17 22:38:59 +00:00
|
|
|
float value = Calibration::map_float(in, in_min, in_max, get_min_value(), get_max_value());
|
2022-03-27 12:53:10 +00:00
|
|
|
return set_value_with_return(value);
|
2022-02-16 22:06:07 +00:00
|
|
|
}
|
|
|
|
|
2022-03-27 12:53:10 +00:00
|
|
|
float ServoState::to_percent_with_return(float in, float in_min, float in_max, float value_min, float value_max) {
|
2022-02-17 22:38:59 +00:00
|
|
|
float value = Calibration::map_float(in, in_min, in_max, value_min, value_max);
|
2022-03-27 12:53:10 +00:00
|
|
|
return set_value_with_return(value);
|
2022-02-16 22:06:07 +00:00
|
|
|
}
|
|
|
|
|
2022-02-17 22:38:59 +00:00
|
|
|
Calibration& ServoState::calibration() {
|
2022-03-21 23:51:15 +00:00
|
|
|
return calib;
|
2022-02-17 17:59:09 +00:00
|
|
|
}
|
|
|
|
|
2022-02-17 22:38:59 +00:00
|
|
|
const Calibration& ServoState::calibration() const {
|
2022-03-21 23:51:15 +00:00
|
|
|
return calib;
|
2022-02-17 17:59:09 +00:00
|
|
|
}
|
|
|
|
|
2022-02-19 01:07:19 +00:00
|
|
|
uint32_t ServoState::pulse_to_level(float pulse, uint32_t resolution, float freq) {
|
2022-02-17 22:38:59 +00:00
|
|
|
uint32_t level = 0;
|
|
|
|
if(pulse >= MIN_VALID_PULSE) {
|
2022-02-19 01:07:19 +00:00
|
|
|
level = (uint32_t)((pulse * (float)resolution * freq) / 1000000);
|
2022-02-17 22:38:59 +00:00
|
|
|
}
|
|
|
|
return level;
|
2022-02-17 17:59:09 +00:00
|
|
|
}
|
|
|
|
|
2022-02-16 22:06:07 +00:00
|
|
|
};
|