2012-11-26 02:16:25 -04:00
|
|
|
// -*- tab-width: 4; Mode: C++; c-basic-offset: 4; indent-tabs-mode: nil -*-
|
2013-08-29 02:34:34 -03:00
|
|
|
/*
|
|
|
|
This program is free software: you can redistribute it and/or modify
|
|
|
|
it under the terms of the GNU General Public License as published by
|
|
|
|
the Free Software Foundation, either version 3 of the License, or
|
|
|
|
(at your option) any later version.
|
|
|
|
|
|
|
|
This program is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
GNU General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
2010-11-23 15:28:19 -04:00
|
|
|
/*
|
2012-08-17 03:22:48 -03:00
|
|
|
* RC_Channel.cpp - Radio library for Arduino
|
|
|
|
* Code by Jason Short. DIYDrones.com
|
|
|
|
*
|
|
|
|
*/
|
2010-11-23 15:28:19 -04:00
|
|
|
|
2012-10-26 20:59:16 -03:00
|
|
|
#include <stdlib.h>
|
2010-11-28 03:03:23 -04:00
|
|
|
#include <math.h>
|
2012-10-26 20:59:16 -03:00
|
|
|
|
2015-08-11 03:28:46 -03:00
|
|
|
#include <AP_HAL/AP_HAL.h>
|
2012-10-26 20:59:16 -03:00
|
|
|
extern const AP_HAL::HAL& hal;
|
|
|
|
|
2015-08-11 03:28:46 -03:00
|
|
|
#include <AP_Math/AP_Math.h>
|
2012-10-26 20:59:16 -03:00
|
|
|
|
2010-11-23 15:28:19 -04:00
|
|
|
#include "RC_Channel.h"
|
|
|
|
|
2012-10-26 20:59:16 -03:00
|
|
|
/// global array with pointers to all APM RC channels, will be used by AP_Mount
|
|
|
|
/// and AP_Camera classes / It points to RC input channels, both APM1 and APM2
|
|
|
|
/// only have 8 input channels.
|
2013-08-17 22:37:42 -03:00
|
|
|
RC_Channel *RC_Channel::rc_ch[RC_MAX_CHANNELS];
|
2012-08-04 13:39:20 -03:00
|
|
|
|
2012-02-11 07:54:21 -04:00
|
|
|
const AP_Param::GroupInfo RC_Channel::var_info[] PROGMEM = {
|
2012-08-17 03:22:48 -03:00
|
|
|
// @Param: MIN
|
|
|
|
// @DisplayName: RC min PWM
|
|
|
|
// @Description: RC minimum PWM pulse width. Typically 1000 is lower limit, 1500 is neutral and 2000 is upper limit.
|
2013-11-26 09:34:28 -04:00
|
|
|
// @Units: pwm
|
2012-08-17 03:22:48 -03:00
|
|
|
// @Range: 800 2200
|
|
|
|
// @Increment: 1
|
|
|
|
// @User: Advanced
|
|
|
|
AP_GROUPINFO("MIN", 0, RC_Channel, radio_min, 1100),
|
|
|
|
|
|
|
|
// @Param: TRIM
|
|
|
|
// @DisplayName: RC trim PWM
|
|
|
|
// @Description: RC trim (neutral) PWM pulse width. Typically 1000 is lower limit, 1500 is neutral and 2000 is upper limit.
|
2013-11-26 09:34:28 -04:00
|
|
|
// @Units: pwm
|
2012-08-17 03:22:48 -03:00
|
|
|
// @Range: 800 2200
|
|
|
|
// @Increment: 1
|
|
|
|
// @User: Advanced
|
|
|
|
AP_GROUPINFO("TRIM", 1, RC_Channel, radio_trim, 1500),
|
|
|
|
|
|
|
|
// @Param: MAX
|
|
|
|
// @DisplayName: RC max PWM
|
|
|
|
// @Description: RC maximum PWM pulse width. Typically 1000 is lower limit, 1500 is neutral and 2000 is upper limit.
|
2013-11-26 09:34:28 -04:00
|
|
|
// @Units: pwm
|
2012-08-17 03:22:48 -03:00
|
|
|
// @Range: 800 2200
|
|
|
|
// @Increment: 1
|
|
|
|
// @User: Advanced
|
|
|
|
AP_GROUPINFO("MAX", 2, RC_Channel, radio_max, 1900),
|
|
|
|
|
|
|
|
// @Param: REV
|
|
|
|
// @DisplayName: RC reverse
|
2013-06-03 04:49:56 -03:00
|
|
|
// @Description: Reverse servo operation. Set to 1 for normal (forward) operation. Set to -1 to reverse this channel.
|
2012-08-17 03:22:48 -03:00
|
|
|
// @Values: -1:Reversed,1:Normal
|
|
|
|
// @User: Advanced
|
|
|
|
AP_GROUPINFO("REV", 3, RC_Channel, _reverse, 1),
|
|
|
|
|
2013-07-13 00:18:54 -03:00
|
|
|
// Note: index 4 was used by the previous _dead_zone value. We
|
|
|
|
// changed it to 5 as dead zone values had previously been
|
|
|
|
// incorrectly saved, overriding user values. They were also
|
|
|
|
// incorrectly interpreted for the throttle on APM:Plane
|
|
|
|
|
2012-08-17 03:22:48 -03:00
|
|
|
// @Param: DZ
|
|
|
|
// @DisplayName: RC dead-zone
|
2015-05-13 22:32:47 -03:00
|
|
|
// @Description: dead zone around trim or bottom
|
2013-11-26 09:34:28 -04:00
|
|
|
// @Units: pwm
|
|
|
|
// @Range: 0 200
|
2012-08-17 03:22:48 -03:00
|
|
|
// @User: Advanced
|
2013-07-13 00:18:54 -03:00
|
|
|
AP_GROUPINFO("DZ", 5, RC_Channel, _dead_zone, 0),
|
|
|
|
|
2012-08-17 03:22:48 -03:00
|
|
|
AP_GROUPEND
|
2012-02-11 07:54:21 -04:00
|
|
|
};
|
|
|
|
|
2010-11-23 15:28:19 -04:00
|
|
|
// setup the control preferences
|
2011-02-14 00:43:44 -04:00
|
|
|
void
|
2012-06-02 02:20:58 -03:00
|
|
|
RC_Channel::set_range(int16_t low, int16_t high)
|
2010-11-23 15:28:19 -04:00
|
|
|
{
|
2012-12-09 03:42:58 -04:00
|
|
|
_type = RC_CHANNEL_TYPE_RANGE;
|
2012-08-17 03:22:48 -03:00
|
|
|
_high = high;
|
|
|
|
_low = low;
|
|
|
|
_high_out = high;
|
|
|
|
_low_out = low;
|
2012-05-31 18:59:03 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2012-06-02 02:20:58 -03:00
|
|
|
RC_Channel::set_range_out(int16_t low, int16_t high)
|
2012-05-31 18:59:03 -03:00
|
|
|
{
|
2012-08-17 03:22:48 -03:00
|
|
|
_high_out = high;
|
|
|
|
_low_out = low;
|
2010-11-23 15:28:19 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2012-06-02 02:20:58 -03:00
|
|
|
RC_Channel::set_angle(int16_t angle)
|
2010-11-23 15:28:19 -04:00
|
|
|
{
|
2012-12-09 03:42:58 -04:00
|
|
|
_type = RC_CHANNEL_TYPE_ANGLE;
|
2012-08-17 03:22:48 -03:00
|
|
|
_high = angle;
|
2010-11-23 15:28:19 -04:00
|
|
|
}
|
|
|
|
|
2011-09-27 02:12:39 -03:00
|
|
|
void
|
2013-07-11 11:08:16 -03:00
|
|
|
RC_Channel::set_default_dead_zone(int16_t dzone)
|
2011-09-27 02:12:39 -03:00
|
|
|
{
|
2013-07-11 11:08:16 -03:00
|
|
|
if (!_dead_zone.load()) {
|
|
|
|
_dead_zone.set(abs(dzone));
|
|
|
|
}
|
2011-09-27 02:12:39 -03:00
|
|
|
}
|
|
|
|
|
2010-12-20 23:53:26 -04:00
|
|
|
void
|
|
|
|
RC_Channel::set_reverse(bool reverse)
|
|
|
|
{
|
2012-08-17 03:22:48 -03:00
|
|
|
if (reverse) _reverse = -1;
|
|
|
|
else _reverse = 1;
|
2010-12-20 23:53:26 -04:00
|
|
|
}
|
|
|
|
|
2011-01-27 14:16:22 -04:00
|
|
|
bool
|
2014-04-20 21:34:10 -03:00
|
|
|
RC_Channel::get_reverse(void) const
|
2011-01-27 14:16:22 -04:00
|
|
|
{
|
2014-04-20 21:34:10 -03:00
|
|
|
if (_reverse == -1) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
2011-01-27 14:16:22 -04:00
|
|
|
}
|
|
|
|
|
2011-05-05 14:46:11 -03:00
|
|
|
void
|
|
|
|
RC_Channel::set_type(uint8_t t)
|
|
|
|
{
|
2012-08-17 03:22:48 -03:00
|
|
|
_type = t;
|
2011-05-05 14:46:11 -03:00
|
|
|
}
|
2010-11-28 03:03:23 -04:00
|
|
|
|
2010-11-23 15:28:19 -04:00
|
|
|
// call after first read
|
|
|
|
void
|
2010-11-25 03:10:06 -04:00
|
|
|
RC_Channel::trim()
|
2010-11-23 15:28:19 -04:00
|
|
|
{
|
2012-08-17 03:22:48 -03:00
|
|
|
radio_trim = radio_in;
|
2010-11-23 15:28:19 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// read input from APM_RC - create a control_in value
|
|
|
|
void
|
2012-05-31 18:59:03 -03:00
|
|
|
RC_Channel::set_pwm(int16_t pwm)
|
2010-11-23 17:20:29 -04:00
|
|
|
{
|
2012-08-17 03:22:48 -03:00
|
|
|
radio_in = pwm;
|
|
|
|
|
2013-07-13 00:18:54 -03:00
|
|
|
if (_type == RC_CHANNEL_TYPE_RANGE) {
|
2012-08-17 03:22:48 -03:00
|
|
|
control_in = pwm_to_range();
|
2012-11-05 05:49:42 -04:00
|
|
|
} else {
|
2012-12-09 03:42:58 -04:00
|
|
|
//RC_CHANNEL_TYPE_ANGLE, RC_CHANNEL_TYPE_ANGLE_RAW
|
2012-08-17 03:22:48 -03:00
|
|
|
control_in = pwm_to_angle();
|
|
|
|
}
|
2010-11-23 15:28:19 -04:00
|
|
|
}
|
|
|
|
|
2014-04-02 22:18:56 -03:00
|
|
|
/*
|
|
|
|
call read() and set_pwm() on all channels
|
|
|
|
*/
|
|
|
|
void
|
|
|
|
RC_Channel::set_pwm_all(void)
|
|
|
|
{
|
|
|
|
for (uint8_t i=0; i<RC_MAX_CHANNELS; i++) {
|
|
|
|
if (rc_ch[i] != NULL) {
|
|
|
|
rc_ch[i]->set_pwm(rc_ch[i]->read());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-12-04 02:27:18 -04:00
|
|
|
// read input from APM_RC - create a control_in value, but use a
|
|
|
|
// zero value for the dead zone. When done this way the control_in
|
|
|
|
// value can be used as servo_out to give the same output as input
|
|
|
|
void
|
|
|
|
RC_Channel::set_pwm_no_deadzone(int16_t pwm)
|
|
|
|
{
|
|
|
|
radio_in = pwm;
|
|
|
|
|
|
|
|
if (_type == RC_CHANNEL_TYPE_RANGE) {
|
|
|
|
control_in = pwm_to_range_dz(0);
|
|
|
|
} else {
|
|
|
|
//RC_CHANNEL_ANGLE, RC_CHANNEL_ANGLE_RAW
|
|
|
|
control_in = pwm_to_angle_dz(0);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-06-02 02:20:58 -03:00
|
|
|
int16_t
|
2010-11-26 23:04:30 -04:00
|
|
|
RC_Channel::control_mix(float value)
|
|
|
|
{
|
2012-08-17 03:22:48 -03:00
|
|
|
return (1 - abs(control_in / _high)) * value + control_in;
|
2010-11-26 23:04:30 -04:00
|
|
|
}
|
|
|
|
|
2010-11-23 15:28:19 -04:00
|
|
|
// are we below a threshold?
|
2010-11-28 03:03:23 -04:00
|
|
|
bool
|
2010-11-23 15:28:19 -04:00
|
|
|
RC_Channel::get_failsafe(void)
|
|
|
|
{
|
2012-08-17 03:22:48 -03:00
|
|
|
return (radio_in < (radio_min - 50));
|
2010-11-23 15:28:19 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// returns just the PWM without the offset from radio_min
|
|
|
|
void
|
|
|
|
RC_Channel::calc_pwm(void)
|
|
|
|
{
|
2012-12-09 03:42:58 -04:00
|
|
|
if(_type == RC_CHANNEL_TYPE_RANGE) {
|
2013-03-29 08:36:29 -03:00
|
|
|
pwm_out = range_to_pwm();
|
2012-08-17 03:22:48 -03:00
|
|
|
radio_out = (_reverse >= 0) ? (radio_min + pwm_out) : (radio_max - pwm_out);
|
2011-05-05 14:46:11 -03:00
|
|
|
|
2012-12-09 03:42:58 -04:00
|
|
|
}else if(_type == RC_CHANNEL_TYPE_ANGLE_RAW) {
|
2013-03-29 08:36:29 -03:00
|
|
|
pwm_out = (float)servo_out * 0.1f;
|
2015-02-17 21:47:56 -04:00
|
|
|
int16_t reverse_mul = (_reverse==-1?-1:1);
|
|
|
|
radio_out = (pwm_out * reverse_mul) + radio_trim;
|
2011-05-05 14:46:11 -03:00
|
|
|
|
2012-12-09 03:42:58 -04:00
|
|
|
}else{ // RC_CHANNEL_TYPE_ANGLE
|
2013-03-29 08:36:29 -03:00
|
|
|
pwm_out = angle_to_pwm();
|
2012-08-17 03:22:48 -03:00
|
|
|
radio_out = pwm_out + radio_trim;
|
|
|
|
}
|
2011-09-21 18:52:20 -03:00
|
|
|
|
2012-12-18 22:36:35 -04:00
|
|
|
radio_out = constrain_int16(radio_out, radio_min.get(), radio_max.get());
|
2010-11-23 15:28:19 -04:00
|
|
|
}
|
|
|
|
|
2014-11-17 18:00:31 -04:00
|
|
|
|
|
|
|
/*
|
|
|
|
return the center stick position expressed as a control_in value
|
|
|
|
used for thr_mid in copter
|
|
|
|
*/
|
|
|
|
int16_t
|
2014-11-17 20:18:51 -04:00
|
|
|
RC_Channel::get_control_mid() const {
|
2014-11-17 18:00:31 -04:00
|
|
|
if (_type == RC_CHANNEL_TYPE_RANGE) {
|
|
|
|
int16_t r_in = (radio_min.get()+radio_max.get())/2;
|
|
|
|
|
|
|
|
if (_reverse == -1) {
|
|
|
|
r_in = radio_max.get() - (r_in - radio_min.get());
|
|
|
|
}
|
|
|
|
|
|
|
|
int16_t radio_trim_low = radio_min + _dead_zone;
|
|
|
|
|
|
|
|
return (_low + ((int32_t)(_high - _low) * (int32_t)(r_in - radio_trim_low)) / (int32_t)(radio_max - radio_trim_low));
|
|
|
|
} else {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-11-28 21:44:40 -04:00
|
|
|
// ------------------------------------------
|
|
|
|
|
2010-11-23 15:28:19 -04:00
|
|
|
void
|
2010-11-28 03:03:23 -04:00
|
|
|
RC_Channel::load_eeprom(void)
|
2010-11-23 15:28:19 -04:00
|
|
|
{
|
2012-08-17 03:22:48 -03:00
|
|
|
radio_min.load();
|
|
|
|
radio_trim.load();
|
|
|
|
radio_max.load();
|
|
|
|
_reverse.load();
|
|
|
|
_dead_zone.load();
|
2010-11-23 15:28:19 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2010-11-28 03:03:23 -04:00
|
|
|
RC_Channel::save_eeprom(void)
|
2010-11-23 15:28:19 -04:00
|
|
|
{
|
2012-08-17 03:22:48 -03:00
|
|
|
radio_min.save();
|
|
|
|
radio_trim.save();
|
|
|
|
radio_max.save();
|
|
|
|
_reverse.save();
|
|
|
|
_dead_zone.save();
|
2010-11-28 21:44:40 -04:00
|
|
|
}
|
|
|
|
|
2010-11-25 03:10:06 -04:00
|
|
|
// ------------------------------------------
|
2010-11-28 03:03:23 -04:00
|
|
|
|
2010-12-25 21:17:04 -04:00
|
|
|
void
|
|
|
|
RC_Channel::zero_min_max()
|
|
|
|
{
|
2012-08-17 03:22:48 -03:00
|
|
|
radio_min = radio_max = radio_in;
|
2010-12-25 21:17:04 -04:00
|
|
|
}
|
|
|
|
|
2010-11-25 03:10:06 -04:00
|
|
|
void
|
|
|
|
RC_Channel::update_min_max()
|
|
|
|
{
|
2012-08-17 03:22:48 -03:00
|
|
|
radio_min = min(radio_min.get(), radio_in);
|
|
|
|
radio_max = max(radio_max.get(), radio_in);
|
2010-11-25 03:10:06 -04:00
|
|
|
}
|
2010-11-28 03:03:23 -04:00
|
|
|
|
2012-11-05 08:37:25 -04:00
|
|
|
/*
|
|
|
|
return an "angle in centidegrees" (normally -4500 to 4500) from
|
|
|
|
the current radio_in value using the specified dead_zone
|
|
|
|
*/
|
2010-12-25 21:17:04 -04:00
|
|
|
int16_t
|
2012-12-04 02:27:18 -04:00
|
|
|
RC_Channel::pwm_to_angle_dz(uint16_t dead_zone)
|
2010-11-23 15:28:19 -04:00
|
|
|
{
|
2012-11-05 08:37:25 -04:00
|
|
|
int16_t radio_trim_high = radio_trim + dead_zone;
|
|
|
|
int16_t radio_trim_low = radio_trim - dead_zone;
|
2012-02-19 01:08:17 -04:00
|
|
|
|
2011-10-30 10:28:39 -03:00
|
|
|
// prevent div by 0
|
2012-02-19 01:08:17 -04:00
|
|
|
if ((radio_trim_low - radio_min) == 0 || (radio_max - radio_trim_high) == 0)
|
2011-10-30 10:28:39 -03:00
|
|
|
return 0;
|
2011-09-27 02:12:39 -03:00
|
|
|
|
2015-02-17 21:47:56 -04:00
|
|
|
int16_t reverse_mul = (_reverse==-1?-1:1);
|
2012-08-17 03:22:48 -03:00
|
|
|
if(radio_in > radio_trim_high) {
|
2015-02-17 21:47:56 -04:00
|
|
|
return reverse_mul * ((int32_t)_high * (int32_t)(radio_in - radio_trim_high)) / (int32_t)(radio_max - radio_trim_high);
|
2012-08-17 03:22:48 -03:00
|
|
|
}else if(radio_in < radio_trim_low) {
|
2015-02-17 21:47:56 -04:00
|
|
|
return reverse_mul * ((int32_t)_high * (int32_t)(radio_in - radio_trim_low)) / (int32_t)(radio_trim_low - radio_min);
|
2012-08-17 03:22:48 -03:00
|
|
|
}else
|
|
|
|
return 0;
|
2010-12-20 23:53:26 -04:00
|
|
|
}
|
|
|
|
|
2012-11-05 08:37:25 -04:00
|
|
|
/*
|
|
|
|
return an "angle in centidegrees" (normally -4500 to 4500) from
|
|
|
|
the current radio_in value
|
|
|
|
*/
|
|
|
|
int16_t
|
|
|
|
RC_Channel::pwm_to_angle()
|
|
|
|
{
|
|
|
|
return pwm_to_angle_dz(_dead_zone);
|
|
|
|
}
|
|
|
|
|
2010-11-23 15:28:19 -04:00
|
|
|
|
|
|
|
int16_t
|
|
|
|
RC_Channel::angle_to_pwm()
|
|
|
|
{
|
2015-02-17 21:47:56 -04:00
|
|
|
int16_t reverse_mul = (_reverse==-1?-1:1);
|
|
|
|
if((servo_out * reverse_mul) > 0) {
|
|
|
|
return reverse_mul * ((int32_t)servo_out * (int32_t)(radio_max - radio_trim)) / (int32_t)_high;
|
|
|
|
} else {
|
|
|
|
return reverse_mul * ((int32_t)servo_out * (int32_t)(radio_trim - radio_min)) / (int32_t)_high;
|
|
|
|
}
|
2010-11-23 15:28:19 -04:00
|
|
|
}
|
|
|
|
|
2012-12-04 02:27:18 -04:00
|
|
|
/*
|
|
|
|
convert a pulse width modulation value to a value in the configured
|
|
|
|
range, using the specified deadzone
|
|
|
|
*/
|
2010-11-23 15:28:19 -04:00
|
|
|
int16_t
|
2012-12-04 02:27:18 -04:00
|
|
|
RC_Channel::pwm_to_range_dz(uint16_t dead_zone)
|
2010-11-23 15:28:19 -04:00
|
|
|
{
|
2012-12-18 22:36:35 -04:00
|
|
|
int16_t r_in = constrain_int16(radio_in, radio_min.get(), radio_max.get());
|
2012-05-31 18:59:03 -03:00
|
|
|
|
2012-09-16 02:05:26 -03:00
|
|
|
if (_reverse == -1) {
|
|
|
|
r_in = radio_max.get() - (r_in - radio_min.get());
|
|
|
|
}
|
|
|
|
|
2012-12-04 02:27:18 -04:00
|
|
|
int16_t radio_trim_low = radio_min + dead_zone;
|
2011-09-27 02:12:39 -03:00
|
|
|
|
2012-12-04 02:27:18 -04:00
|
|
|
if (r_in > radio_trim_low)
|
2014-11-17 17:53:10 -04:00
|
|
|
return (_low + ((int32_t)(_high - _low) * (int32_t)(r_in - radio_trim_low)) / (int32_t)(radio_max - radio_trim_low));
|
2012-12-04 02:27:18 -04:00
|
|
|
else if (dead_zone > 0)
|
2012-08-17 03:22:48 -03:00
|
|
|
return 0;
|
|
|
|
else
|
|
|
|
return _low;
|
2010-11-23 15:28:19 -04:00
|
|
|
}
|
|
|
|
|
2012-12-04 02:27:18 -04:00
|
|
|
/*
|
|
|
|
convert a pulse width modulation value to a value in the configured
|
|
|
|
range
|
|
|
|
*/
|
|
|
|
int16_t
|
|
|
|
RC_Channel::pwm_to_range()
|
|
|
|
{
|
|
|
|
return pwm_to_range_dz(_dead_zone);
|
|
|
|
}
|
|
|
|
|
2011-09-27 02:12:39 -03:00
|
|
|
|
2010-11-23 15:28:19 -04:00
|
|
|
int16_t
|
|
|
|
RC_Channel::range_to_pwm()
|
|
|
|
{
|
2014-03-26 03:38:27 -03:00
|
|
|
if (_high_out == _low_out) {
|
|
|
|
return radio_trim;
|
|
|
|
}
|
2014-11-17 17:53:10 -04:00
|
|
|
return ((int32_t)(servo_out - _low_out) * (int32_t)(radio_max - radio_min)) / (int32_t)(_high_out - _low_out);
|
2010-11-23 15:28:19 -04:00
|
|
|
}
|
|
|
|
|
2010-12-27 19:03:26 -04:00
|
|
|
// ------------------------------------------
|
2010-12-25 21:17:04 -04:00
|
|
|
|
2011-02-14 00:43:44 -04:00
|
|
|
float
|
2010-12-27 19:03:26 -04:00
|
|
|
RC_Channel::norm_input()
|
|
|
|
{
|
2014-03-15 06:43:13 -03:00
|
|
|
float ret;
|
2015-02-17 21:47:56 -04:00
|
|
|
int16_t reverse_mul = (_reverse==-1?-1:1);
|
|
|
|
if (radio_in < radio_trim) {
|
|
|
|
ret = reverse_mul * (float)(radio_in - radio_trim) / (float)(radio_trim - radio_min);
|
|
|
|
} else {
|
|
|
|
ret = reverse_mul * (float)(radio_in - radio_trim) / (float)(radio_max - radio_trim);
|
|
|
|
}
|
2014-03-15 06:43:13 -03:00
|
|
|
return constrain_float(ret, -1.0f, 1.0f);
|
2010-12-27 19:03:26 -04:00
|
|
|
}
|
2010-12-25 21:17:04 -04:00
|
|
|
|
2014-11-17 21:44:05 -04:00
|
|
|
float
|
|
|
|
RC_Channel::norm_input_dz()
|
|
|
|
{
|
|
|
|
int16_t dz_min = radio_trim - _dead_zone;
|
|
|
|
int16_t dz_max = radio_trim + _dead_zone;
|
|
|
|
float ret;
|
2015-02-17 21:47:56 -04:00
|
|
|
int16_t reverse_mul = (_reverse==-1?-1:1);
|
2014-11-17 21:44:05 -04:00
|
|
|
if (radio_in < dz_min && dz_min > radio_min) {
|
2015-02-17 21:47:56 -04:00
|
|
|
ret = reverse_mul * (float)(radio_in - dz_min) / (float)(dz_min - radio_min);
|
2014-11-17 21:44:05 -04:00
|
|
|
} else if (radio_in > dz_max && radio_max > dz_max) {
|
2015-02-17 21:47:56 -04:00
|
|
|
ret = reverse_mul * (float)(radio_in - dz_max) / (float)(radio_max - dz_max);
|
2014-11-17 21:44:05 -04:00
|
|
|
} else {
|
|
|
|
ret = 0;
|
|
|
|
}
|
|
|
|
return constrain_float(ret, -1.0f, 1.0f);
|
|
|
|
}
|
|
|
|
|
2014-03-04 18:16:12 -04:00
|
|
|
/*
|
|
|
|
get percentage input from 0 to 100. This ignores the trim value.
|
|
|
|
*/
|
|
|
|
uint8_t
|
|
|
|
RC_Channel::percent_input()
|
|
|
|
{
|
|
|
|
if (radio_in <= radio_min) {
|
|
|
|
return _reverse==-1?100:0;
|
|
|
|
}
|
|
|
|
if (radio_in >= radio_max) {
|
|
|
|
return _reverse==-1?0:100;
|
|
|
|
}
|
|
|
|
uint8_t ret = 100.0f * (radio_in - radio_min) / (float)(radio_max - radio_min);
|
|
|
|
if (_reverse == -1) {
|
|
|
|
ret = 100 - ret;
|
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2011-02-14 00:43:44 -04:00
|
|
|
float
|
2010-12-27 19:03:26 -04:00
|
|
|
RC_Channel::norm_output()
|
|
|
|
{
|
2012-02-10 23:35:59 -04:00
|
|
|
int16_t mid = (radio_max + radio_min) / 2;
|
2012-09-16 02:05:26 -03:00
|
|
|
float ret;
|
2015-07-26 21:05:33 -03:00
|
|
|
if (mid <= radio_min) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
if (radio_out < mid) {
|
2012-09-16 02:05:26 -03:00
|
|
|
ret = (float)(radio_out - mid) / (float)(mid - radio_min);
|
2015-07-26 21:05:33 -03:00
|
|
|
} else if (radio_out > mid) {
|
2012-09-16 02:05:26 -03:00
|
|
|
ret = (float)(radio_out - mid) / (float)(radio_max - mid);
|
2015-07-26 21:05:33 -03:00
|
|
|
} else {
|
|
|
|
ret = 0;
|
|
|
|
}
|
2012-09-16 02:05:26 -03:00
|
|
|
if (_reverse == -1) {
|
|
|
|
ret = -ret;
|
|
|
|
}
|
|
|
|
return ret;
|
2010-12-27 19:03:26 -04:00
|
|
|
}
|
2011-11-12 23:18:14 -04:00
|
|
|
|
2013-06-03 03:12:02 -03:00
|
|
|
void RC_Channel::output() const
|
2012-08-05 18:08:31 -03:00
|
|
|
{
|
2012-10-26 20:59:16 -03:00
|
|
|
hal.rcout->write(_ch_out, radio_out);
|
2012-08-05 18:08:31 -03:00
|
|
|
}
|
|
|
|
|
2013-06-03 03:12:02 -03:00
|
|
|
void RC_Channel::output_trim() const
|
|
|
|
{
|
|
|
|
hal.rcout->write(_ch_out, radio_trim);
|
|
|
|
}
|
|
|
|
|
2014-04-02 22:18:56 -03:00
|
|
|
void RC_Channel::output_trim_all()
|
|
|
|
{
|
|
|
|
for (uint8_t i=0; i<RC_MAX_CHANNELS; i++) {
|
|
|
|
if (rc_ch[i] != NULL) {
|
|
|
|
rc_ch[i]->output_trim();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-04-20 19:37:24 -03:00
|
|
|
/*
|
|
|
|
setup the failsafe value to the trim value for all channels
|
|
|
|
*/
|
|
|
|
void RC_Channel::setup_failsafe_trim_all()
|
|
|
|
{
|
|
|
|
for (uint8_t i=0; i<RC_MAX_CHANNELS; i++) {
|
|
|
|
if (rc_ch[i] != NULL) {
|
|
|
|
hal.rcout->set_failsafe_pwm(1U<<i, rc_ch[i]->radio_trim);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-11-26 02:16:25 -04:00
|
|
|
void
|
|
|
|
RC_Channel::input()
|
|
|
|
{
|
2012-10-26 20:59:16 -03:00
|
|
|
radio_in = hal.rcin->read(_ch_out);
|
2012-11-26 02:16:25 -04:00
|
|
|
}
|
|
|
|
|
2013-06-03 03:12:02 -03:00
|
|
|
uint16_t
|
|
|
|
RC_Channel::read() const
|
|
|
|
{
|
|
|
|
return hal.rcin->read(_ch_out);
|
|
|
|
}
|
|
|
|
|
2012-08-05 18:08:31 -03:00
|
|
|
void
|
|
|
|
RC_Channel::enable_out()
|
|
|
|
{
|
2012-10-26 20:59:16 -03:00
|
|
|
hal.rcout->enable_ch(_ch_out);
|
2012-08-05 18:08:31 -03:00
|
|
|
}
|
2013-08-17 22:37:42 -03:00
|
|
|
|
2013-12-19 20:58:53 -04:00
|
|
|
void
|
|
|
|
RC_Channel::disable_out()
|
|
|
|
{
|
|
|
|
hal.rcout->disable_ch(_ch_out);
|
|
|
|
}
|
|
|
|
|
2013-08-17 22:37:42 -03:00
|
|
|
RC_Channel *RC_Channel::rc_channel(uint8_t i)
|
|
|
|
{
|
|
|
|
if (i >= RC_MAX_CHANNELS) {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
return rc_ch[i];
|
|
|
|
}
|
2014-04-20 21:34:10 -03:00
|
|
|
|
|
|
|
// return a limit PWM value
|
|
|
|
uint16_t RC_Channel::get_limit_pwm(LimitValue limit) const
|
|
|
|
{
|
|
|
|
switch (limit) {
|
|
|
|
case RC_CHANNEL_LIMIT_TRIM:
|
|
|
|
return radio_trim;
|
|
|
|
case RC_CHANNEL_LIMIT_MAX:
|
|
|
|
return get_reverse() ? radio_min : radio_max;
|
|
|
|
case RC_CHANNEL_LIMIT_MIN:
|
|
|
|
return get_reverse() ? radio_max : radio_min;
|
|
|
|
}
|
|
|
|
// invalid limit value, return trim
|
|
|
|
return radio_trim;
|
|
|
|
}
|