2022-02-16 22:06:07 +00:00
|
|
|
#include "calibration.hpp"
|
|
|
|
|
|
|
|
namespace servo {
|
2022-03-22 14:21:10 +00:00
|
|
|
Calibration::Pair::Pair()
|
2022-03-21 23:51:15 +00:00
|
|
|
: pulse(0.0f), value(0.0f) {
|
2022-02-16 22:06:07 +00:00
|
|
|
}
|
|
|
|
|
2022-03-22 14:21:10 +00:00
|
|
|
Calibration::Pair::Pair(float pulse, float value)
|
2022-03-21 23:51:15 +00:00
|
|
|
: pulse(pulse), value(value) {
|
2022-02-16 22:06:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Calibration::Calibration()
|
2022-02-17 22:38:59 +00:00
|
|
|
: calibration(nullptr), calibration_size(0), limit_lower(true), limit_upper(true) {
|
2022-02-16 22:06:07 +00:00
|
|
|
}
|
|
|
|
|
2022-02-17 22:38:59 +00:00
|
|
|
Calibration::Calibration(CalibrationType default_type)
|
2022-03-21 23:51:15 +00:00
|
|
|
: Calibration() {
|
2022-03-22 14:21:10 +00:00
|
|
|
apply_default_pairs(default_type);
|
2022-02-16 22:06:07 +00:00
|
|
|
}
|
|
|
|
|
2022-03-08 16:54:20 +00:00
|
|
|
Calibration::Calibration(const Calibration &other)
|
|
|
|
: calibration(nullptr), calibration_size(0), limit_lower(other.limit_lower), limit_upper(other.limit_upper) {
|
|
|
|
uint size = other.size();
|
2022-03-22 14:21:10 +00:00
|
|
|
apply_blank_pairs(size);
|
2022-03-08 16:54:20 +00:00
|
|
|
for(uint i = 0; i < size; i++) {
|
|
|
|
calibration[i] = other.calibration[i];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-02-16 22:06:07 +00:00
|
|
|
Calibration::~Calibration() {
|
|
|
|
if(calibration != nullptr) {
|
|
|
|
delete[] calibration;
|
|
|
|
calibration = nullptr;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-03-22 14:21:10 +00:00
|
|
|
Calibration &Calibration::operator=(const Calibration &other) {
|
2022-03-08 16:54:20 +00:00
|
|
|
uint size = other.size();
|
2022-03-22 14:21:10 +00:00
|
|
|
apply_blank_pairs(size);
|
2022-03-08 16:54:20 +00:00
|
|
|
for(uint i = 0; i < size; i++) {
|
|
|
|
calibration[i] = other.calibration[i];
|
|
|
|
}
|
|
|
|
limit_lower = other.limit_lower;
|
|
|
|
limit_upper = other.limit_upper;
|
|
|
|
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2022-03-22 14:21:10 +00:00
|
|
|
Calibration::Pair &Calibration::operator[](uint8_t index) {
|
|
|
|
assert(index < calibration_size);
|
2022-03-21 23:51:15 +00:00
|
|
|
return calibration[index];
|
|
|
|
}
|
2022-03-08 16:54:20 +00:00
|
|
|
|
2022-03-22 14:21:10 +00:00
|
|
|
const Calibration::Pair &Calibration::operator[](uint8_t index) const {
|
|
|
|
assert(index < calibration_size);
|
|
|
|
return calibration[index];
|
|
|
|
}
|
|
|
|
|
|
|
|
void Calibration::apply_blank_pairs(uint size) {
|
2022-02-17 22:38:59 +00:00
|
|
|
if(calibration != nullptr) {
|
|
|
|
delete[] calibration;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(size > 0) {
|
2022-03-22 14:21:10 +00:00
|
|
|
calibration = new Pair[size];
|
2022-02-17 22:38:59 +00:00
|
|
|
calibration_size = size;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
calibration = nullptr;
|
|
|
|
calibration_size = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-03-22 14:21:10 +00:00
|
|
|
void Calibration::apply_two_pairs(float min_pulse, float max_pulse, float min_value, float max_value) {
|
|
|
|
apply_blank_pairs(2);
|
|
|
|
calibration[0] = Pair(min_pulse, min_value);
|
|
|
|
calibration[1] = Pair(max_pulse, max_value);
|
2022-02-17 22:38:59 +00:00
|
|
|
}
|
|
|
|
|
2022-03-22 14:21:10 +00:00
|
|
|
void Calibration::apply_three_pairs(float min_pulse, float mid_pulse, float max_pulse, float min_value, float mid_value, float max_value) {
|
|
|
|
apply_blank_pairs(3);
|
|
|
|
calibration[0] = Pair(min_pulse, min_value);
|
|
|
|
calibration[1] = Pair(mid_pulse, mid_value);
|
|
|
|
calibration[2] = Pair(max_pulse, max_value);
|
2022-02-17 22:38:59 +00:00
|
|
|
}
|
|
|
|
|
2022-03-22 14:21:10 +00:00
|
|
|
void Calibration::apply_uniform_pairs(uint size, float min_pulse, float max_pulse, float min_value, float max_value) {
|
|
|
|
apply_blank_pairs(size);
|
2022-02-17 22:38:59 +00:00
|
|
|
if(size > 0) {
|
|
|
|
float size_minus_one = (float)(size - 1);
|
|
|
|
for(uint i = 0; i < size; i++) {
|
2022-02-18 18:15:15 +00:00
|
|
|
float pulse = Calibration::map_float((float)i, 0.0f, size_minus_one, min_pulse, max_pulse);
|
|
|
|
float value = Calibration::map_float((float)i, 0.0f, size_minus_one, min_value, max_value);
|
2022-03-22 14:21:10 +00:00
|
|
|
calibration[i] = Pair(pulse, value);
|
2022-02-17 22:38:59 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-03-22 14:21:10 +00:00
|
|
|
void Calibration::apply_default_pairs(CalibrationType default_type) {
|
2022-02-17 22:38:59 +00:00
|
|
|
switch(default_type) {
|
2022-02-16 22:06:07 +00:00
|
|
|
default:
|
|
|
|
case ANGULAR:
|
2022-03-22 14:21:10 +00:00
|
|
|
apply_three_pairs(DEFAULT_MIN_PULSE, DEFAULT_MID_PULSE, DEFAULT_MAX_PULSE,
|
|
|
|
-90.0f, 0.0f, +90.0f);
|
2022-02-16 22:06:07 +00:00
|
|
|
break;
|
|
|
|
case LINEAR:
|
2022-03-22 14:21:10 +00:00
|
|
|
apply_two_pairs(DEFAULT_MIN_PULSE, DEFAULT_MAX_PULSE,
|
|
|
|
0.0f, 1.0f);
|
2022-02-16 22:06:07 +00:00
|
|
|
break;
|
|
|
|
case CONTINUOUS:
|
2022-03-22 14:21:10 +00:00
|
|
|
apply_three_pairs(DEFAULT_MIN_PULSE, DEFAULT_MID_PULSE, DEFAULT_MAX_PULSE,
|
|
|
|
-1.0f, 0.0f, +1.0f);
|
2022-02-16 22:06:07 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-02-17 22:38:59 +00:00
|
|
|
uint Calibration::size() const {
|
|
|
|
return calibration_size;
|
2022-02-16 22:06:07 +00:00
|
|
|
}
|
|
|
|
|
2022-03-22 14:21:10 +00:00
|
|
|
Calibration::Pair &Calibration::pair(uint8_t index) {
|
|
|
|
assert(index < calibration_size);
|
|
|
|
return calibration[index];
|
2022-02-16 22:06:07 +00:00
|
|
|
}
|
|
|
|
|
2022-03-27 12:48:58 +01:00
|
|
|
const Calibration::Pair &Calibration::pair(uint8_t index) const {
|
|
|
|
assert(index < calibration_size);
|
|
|
|
return calibration[index];
|
|
|
|
}
|
|
|
|
|
|
|
|
float Calibration::pulse(uint8_t index) const {
|
|
|
|
return pair(index).pulse;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Calibration::pulse(uint8_t index, float pulse) {
|
|
|
|
pair(index).pulse = pulse;
|
|
|
|
}
|
|
|
|
|
|
|
|
float Calibration::value(uint8_t index) const {
|
|
|
|
return pair(index).value;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Calibration::value(uint8_t index, float value) {
|
|
|
|
pair(index).value = value;
|
|
|
|
}
|
|
|
|
|
2022-03-22 14:21:10 +00:00
|
|
|
Calibration::Pair &Calibration::first() {
|
|
|
|
assert(calibration_size > 0);
|
|
|
|
return calibration[0];
|
2022-02-16 22:06:07 +00:00
|
|
|
}
|
|
|
|
|
2022-03-27 12:48:58 +01:00
|
|
|
const Calibration::Pair &Calibration::first() const {
|
2022-03-22 14:21:10 +00:00
|
|
|
assert(calibration_size > 0);
|
2022-03-27 12:48:58 +01:00
|
|
|
return calibration[0];
|
2022-03-22 14:21:10 +00:00
|
|
|
}
|
|
|
|
|
2022-03-27 12:48:58 +01:00
|
|
|
float Calibration::first_pulse() const {
|
|
|
|
return first().pulse;
|
2022-03-22 14:21:10 +00:00
|
|
|
}
|
|
|
|
|
2022-03-27 12:48:58 +01:00
|
|
|
void Calibration::first_pulse(float pulse) {
|
|
|
|
first().pulse = pulse;
|
|
|
|
}
|
|
|
|
|
|
|
|
float Calibration::first_value() const {
|
|
|
|
return first().value;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Calibration::first_value(float value) {
|
|
|
|
first().value = value;
|
|
|
|
}
|
|
|
|
|
|
|
|
Calibration::Pair &Calibration::last() {
|
2022-03-22 14:21:10 +00:00
|
|
|
assert(calibration_size > 0);
|
2022-03-27 12:48:58 +01:00
|
|
|
return calibration[calibration_size - 1];
|
2022-03-22 14:21:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
const Calibration::Pair &Calibration::last() const {
|
|
|
|
assert(calibration_size > 0);
|
|
|
|
return calibration[calibration_size - 1];
|
2022-02-16 22:06:07 +00:00
|
|
|
}
|
|
|
|
|
2022-03-27 12:48:58 +01:00
|
|
|
float Calibration::last_pulse() const {
|
|
|
|
return last().pulse;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Calibration::last_pulse(float pulse) {
|
|
|
|
last().pulse = pulse;
|
|
|
|
}
|
|
|
|
|
|
|
|
float Calibration::last_value() const {
|
|
|
|
return last().value;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Calibration::last_value(float value) {
|
|
|
|
last().value = value;
|
|
|
|
}
|
|
|
|
|
2022-03-08 14:26:57 +00:00
|
|
|
bool Calibration::has_lower_limit() const {
|
|
|
|
return limit_lower;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Calibration::has_upper_limit() const {
|
|
|
|
return limit_upper;
|
|
|
|
}
|
|
|
|
|
2022-02-16 22:06:07 +00:00
|
|
|
void Calibration::limit_to_calibration(bool lower, bool upper) {
|
|
|
|
limit_lower = lower;
|
|
|
|
limit_upper = upper;
|
|
|
|
}
|
|
|
|
|
2022-02-19 19:25:15 +00:00
|
|
|
bool Calibration::value_to_pulse(float value, float &pulse_out, float &value_out) const {
|
|
|
|
bool success = false;
|
2022-02-17 22:38:59 +00:00
|
|
|
if(calibration_size >= 2) {
|
|
|
|
uint8_t last = calibration_size - 1;
|
2022-02-16 22:06:07 +00:00
|
|
|
|
2022-02-19 20:00:50 +00:00
|
|
|
value_out = value;
|
|
|
|
|
2022-03-22 14:21:10 +00:00
|
|
|
// Is the value below the bottom most calibration pair?
|
2022-02-16 22:06:07 +00:00
|
|
|
if(value < calibration[0].value) {
|
|
|
|
// Should the value be limited to the calibration or projected below it?
|
2022-02-19 19:25:15 +00:00
|
|
|
if(limit_lower) {
|
|
|
|
pulse_out = calibration[0].pulse;
|
|
|
|
value_out = calibration[0].value;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
pulse_out = map_float(value, calibration[0].value, calibration[1].value,
|
|
|
|
calibration[0].pulse, calibration[1].pulse);
|
|
|
|
}
|
2022-02-16 22:06:07 +00:00
|
|
|
}
|
2022-03-22 14:21:10 +00:00
|
|
|
// Is the value above the top most calibration pair?
|
2022-02-16 22:06:07 +00:00
|
|
|
else if(value > calibration[last].value) {
|
|
|
|
// Should the value be limited to the calibration or projected above it?
|
2022-02-19 19:25:15 +00:00
|
|
|
if(limit_upper) {
|
|
|
|
pulse_out = calibration[last].pulse;
|
|
|
|
value_out = calibration[last].value;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
pulse_out = map_float(value, calibration[last - 1].value, calibration[last].value,
|
|
|
|
calibration[last - 1].pulse, calibration[last].pulse);
|
|
|
|
}
|
2022-02-16 22:06:07 +00:00
|
|
|
}
|
|
|
|
else {
|
2022-03-22 14:21:10 +00:00
|
|
|
// The value must between two calibration pairs, so iterate through them to find which ones
|
2022-02-16 22:06:07 +00:00
|
|
|
for(uint8_t i = 0; i < last; i++) {
|
|
|
|
if(value <= calibration[i + 1].value) {
|
2022-02-19 19:25:15 +00:00
|
|
|
pulse_out = map_float(value, calibration[i].value, calibration[i + 1].value,
|
|
|
|
calibration[i].pulse, calibration[i + 1].pulse);
|
2022-02-16 22:06:07 +00:00
|
|
|
break; // No need to continue checking so break out of the loop
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2022-02-19 19:25:15 +00:00
|
|
|
|
|
|
|
// Clamp the pulse between the hard limits
|
2022-02-19 20:00:50 +00:00
|
|
|
if(pulse_out < LOWER_HARD_LIMIT || pulse_out > UPPER_HARD_LIMIT) {
|
|
|
|
pulse_out = MIN(MAX(pulse_out, LOWER_HARD_LIMIT), UPPER_HARD_LIMIT);
|
2022-02-19 19:25:15 +00:00
|
|
|
|
2022-03-22 14:21:10 +00:00
|
|
|
// Is the pulse below the bottom most calibration pair?
|
2022-02-19 20:00:50 +00:00
|
|
|
if(pulse_out < calibration[0].pulse) {
|
|
|
|
value_out = map_float(pulse_out, calibration[0].pulse, calibration[1].pulse,
|
|
|
|
calibration[0].value, calibration[1].value);
|
|
|
|
}
|
2022-03-22 14:21:10 +00:00
|
|
|
// Is the pulse above the top most calibration pair?
|
2022-02-19 20:00:50 +00:00
|
|
|
else if(pulse_out > calibration[last].pulse) {
|
|
|
|
value_out = map_float(pulse_out, calibration[last - 1].pulse, calibration[last].pulse,
|
|
|
|
calibration[last - 1].value, calibration[last].value);
|
|
|
|
}
|
|
|
|
else {
|
2022-03-22 14:21:10 +00:00
|
|
|
// The pulse must between two calibration pairs, so iterate through them to find which ones
|
2022-02-19 20:00:50 +00:00
|
|
|
for(uint8_t i = 0; i < last; i++) {
|
|
|
|
if(pulse_out <= calibration[i + 1].pulse) {
|
|
|
|
value_out = map_float(pulse_out, calibration[i].pulse, calibration[i + 1].pulse,
|
|
|
|
calibration[i].value, calibration[i + 1].value);
|
|
|
|
break; // No need to continue checking so break out of the loop
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2022-02-19 19:25:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
success = true;
|
2022-02-16 22:06:07 +00:00
|
|
|
}
|
|
|
|
|
2022-02-19 19:25:15 +00:00
|
|
|
return success;
|
2022-02-16 22:06:07 +00:00
|
|
|
}
|
|
|
|
|
2022-02-19 20:13:18 +00:00
|
|
|
bool Calibration::pulse_to_value(float pulse, float &value_out, float &pulse_out) const {
|
2022-02-19 19:25:15 +00:00
|
|
|
bool success = false;
|
2022-02-17 22:38:59 +00:00
|
|
|
if(calibration_size >= 2) {
|
|
|
|
uint8_t last = calibration_size - 1;
|
2022-02-16 22:06:07 +00:00
|
|
|
|
2022-02-19 19:25:15 +00:00
|
|
|
// Clamp the pulse between the hard limits
|
2022-02-19 20:00:50 +00:00
|
|
|
pulse_out = MIN(MAX(pulse, LOWER_HARD_LIMIT), UPPER_HARD_LIMIT);
|
2022-02-19 19:25:15 +00:00
|
|
|
|
2022-03-22 14:21:10 +00:00
|
|
|
// Is the pulse below the bottom most calibration pair?
|
2022-02-19 20:00:50 +00:00
|
|
|
if(pulse_out < calibration[0].pulse) {
|
2022-02-16 22:06:07 +00:00
|
|
|
// Should the pulse be limited to the calibration or projected below it?
|
2022-02-19 19:25:15 +00:00
|
|
|
if(limit_lower) {
|
|
|
|
value_out = calibration[0].value;
|
|
|
|
pulse_out = calibration[0].pulse;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
value_out = map_float(pulse, calibration[0].pulse, calibration[1].pulse,
|
|
|
|
calibration[0].value, calibration[1].value);
|
|
|
|
}
|
2022-02-16 22:06:07 +00:00
|
|
|
}
|
2022-03-22 14:21:10 +00:00
|
|
|
// Is the pulse above the top most calibration pair?
|
2022-02-16 22:06:07 +00:00
|
|
|
else if(pulse > calibration[last].pulse) {
|
|
|
|
// Should the pulse be limited to the calibration or projected above it?
|
2022-02-19 19:25:15 +00:00
|
|
|
if(limit_upper) {
|
|
|
|
value_out = calibration[last].value;
|
|
|
|
pulse_out = calibration[last].pulse;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
value_out = map_float(pulse, calibration[last - 1].pulse, calibration[last].pulse,
|
|
|
|
calibration[last - 1].value, calibration[last].value);
|
|
|
|
}
|
2022-02-16 22:06:07 +00:00
|
|
|
}
|
|
|
|
else {
|
2022-03-22 14:21:10 +00:00
|
|
|
// The pulse must between two calibration pairs, so iterate through them to find which ones
|
2022-02-16 22:06:07 +00:00
|
|
|
for(uint8_t i = 0; i < last; i++) {
|
|
|
|
if(pulse <= calibration[i + 1].pulse) {
|
2022-02-19 19:25:15 +00:00
|
|
|
value_out = map_float(pulse, calibration[i].pulse, calibration[i + 1].pulse,
|
|
|
|
calibration[i].value, calibration[i + 1].value);
|
2022-02-16 22:06:07 +00:00
|
|
|
break; // No need to continue checking so break out of the loop
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2022-02-19 19:25:15 +00:00
|
|
|
|
|
|
|
success = true;
|
2022-02-16 22:06:07 +00:00
|
|
|
}
|
|
|
|
|
2022-02-19 19:25:15 +00:00
|
|
|
return success;
|
2022-02-16 22:06:07 +00:00
|
|
|
}
|
|
|
|
|
2022-02-17 22:38:59 +00:00
|
|
|
float Calibration::map_float(float in, float in_min, float in_max, float out_min, float out_max) {
|
2022-02-16 22:06:07 +00:00
|
|
|
return (((in - in_min) * (out_max - out_min)) / (in_max - in_min)) + out_min;
|
|
|
|
}
|
|
|
|
};
|