From fea7903aa80b3e289f1121d76f248120016ab35b Mon Sep 17 00:00:00 2001 From: Andrew Tridgell Date: Tue, 11 Oct 2016 22:00:48 +1100 Subject: [PATCH] RC_Channel: added get_radio_out_normalised() --- libraries/RC_Channel/RC_Channel.cpp | 40 +++++++++++++++++++++++++++++ libraries/RC_Channel/RC_Channel.h | 7 +++++ 2 files changed, 47 insertions(+) diff --git a/libraries/RC_Channel/RC_Channel.cpp b/libraries/RC_Channel/RC_Channel.cpp index 9fe2626faa..68ade11a98 100644 --- a/libraries/RC_Channel/RC_Channel.cpp +++ b/libraries/RC_Channel/RC_Channel.cpp @@ -571,3 +571,43 @@ bool RC_Channel::in_trim_dz() { return is_bounded_int32(_radio_in, _radio_trim - _dead_zone, _radio_trim + _dead_zone); } + + +/* + return the current radio_out value normalised as a float with 1.0 + being full output and 0.0 being zero output, taking into account + output type and reversals + + For angle outputs the returned value is from -1 to 1 + + For range outputs the returned value is from 0 to 1 + */ +float RC_Channel::get_radio_out_normalised(void) const +{ + if (_radio_max <= _radio_min) { + return 0; + } + float ret; + if (_type_out == RC_CHANNEL_TYPE_RANGE) { + if (_radio_out <= _radio_min) { + ret = 0; + } else if (_radio_out >= _radio_max) { + ret = 1; + } else { + ret = (_radio_out - _radio_min) / float(_radio_max - _radio_min); + } + if (_reverse == -1) { + ret = 1 - ret; + } + } else { + if (_radio_out < _radio_trim) { + ret = -(_radio_trim - _radio_out) / float(_radio_trim - _radio_min); + } else { + ret = (_radio_out - _radio_trim) / float(_radio_max - _radio_trim); + } + if (_reverse == -1) { + ret = -ret; + } + } + return ret; +} diff --git a/libraries/RC_Channel/RC_Channel.h b/libraries/RC_Channel/RC_Channel.h index 190ac13584..28aed0779c 100644 --- a/libraries/RC_Channel/RC_Channel.h +++ b/libraries/RC_Channel/RC_Channel.h @@ -149,6 +149,13 @@ public: int16_t get_radio_trim() const { return _radio_trim.get();} void set_radio_trim(int16_t val) { _radio_trim.set(val);} void save_radio_trim() { _radio_trim.save();} + + // return output type RC_CHANNEL_TYPE_* + uint8_t get_type_out(void) const { return _type_out; } + + // get the current radio_out value as a floating point number + // normalised so that 1.0 is full output + float get_radio_out_normalised() const; bool min_max_configured() {