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
|
|
|
/*
|
2017-02-05 22:15:33 -04:00
|
|
|
* RC_Channel.cpp - class for one RC channel input
|
2012-08-17 03:22:48 -03:00
|
|
|
*/
|
2010-11-23 15:28:19 -04:00
|
|
|
|
2012-10-26 20:59:16 -03:00
|
|
|
#include <stdlib.h>
|
2016-03-31 18:43:36 -03:00
|
|
|
#include <cmath>
|
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"
|
|
|
|
|
2018-01-08 23:41:02 -04:00
|
|
|
uint32_t RC_Channel::configured_mask;
|
|
|
|
|
2015-10-25 14:03:46 -03:00
|
|
|
const AP_Param::GroupInfo RC_Channel::var_info[] = {
|
2012-08-17 03:22:48 -03:00
|
|
|
// @Param: MIN
|
|
|
|
// @DisplayName: RC min PWM
|
2017-05-15 20:23:19 -03:00
|
|
|
// @Description: RC minimum PWM pulse width in microseconds. Typically 1000 is lower limit, 1500 is neutral and 2000 is upper limit.
|
2017-05-02 10:49:03 -03:00
|
|
|
// @Units: PWM
|
2012-08-17 03:22:48 -03:00
|
|
|
// @Range: 800 2200
|
|
|
|
// @Increment: 1
|
|
|
|
// @User: Advanced
|
2016-10-22 07:27:40 -03:00
|
|
|
AP_GROUPINFO("MIN", 1, RC_Channel, radio_min, 1100),
|
2012-08-17 03:22:48 -03:00
|
|
|
|
|
|
|
// @Param: TRIM
|
|
|
|
// @DisplayName: RC trim PWM
|
2017-05-15 20:23:19 -03:00
|
|
|
// @Description: RC trim (neutral) PWM pulse width in microseconds. Typically 1000 is lower limit, 1500 is neutral and 2000 is upper limit.
|
2017-05-02 10:49:03 -03:00
|
|
|
// @Units: PWM
|
2012-08-17 03:22:48 -03:00
|
|
|
// @Range: 800 2200
|
|
|
|
// @Increment: 1
|
|
|
|
// @User: Advanced
|
2016-10-22 07:27:40 -03:00
|
|
|
AP_GROUPINFO("TRIM", 2, RC_Channel, radio_trim, 1500),
|
2012-08-17 03:22:48 -03:00
|
|
|
|
|
|
|
// @Param: MAX
|
|
|
|
// @DisplayName: RC max PWM
|
2017-05-15 20:23:19 -03:00
|
|
|
// @Description: RC maximum PWM pulse width in microseconds. Typically 1000 is lower limit, 1500 is neutral and 2000 is upper limit.
|
2017-05-02 10:49:03 -03:00
|
|
|
// @Units: PWM
|
2012-08-17 03:22:48 -03:00
|
|
|
// @Range: 800 2200
|
|
|
|
// @Increment: 1
|
|
|
|
// @User: Advanced
|
2016-10-22 07:27:40 -03:00
|
|
|
AP_GROUPINFO("MAX", 3, RC_Channel, radio_max, 1900),
|
2012-08-17 03:22:48 -03:00
|
|
|
|
2016-10-22 07:27:40 -03:00
|
|
|
// @Param: REVERSED
|
|
|
|
// @DisplayName: RC reversed
|
2017-02-07 01:32:57 -04:00
|
|
|
// @Description: Reverse channel input. Set to 0 for normal operation. Set to 1 to reverse this input channel.
|
2016-10-22 07:27:40 -03:00
|
|
|
// @Values: 0:Normal,1:Reversed
|
2012-08-17 03:22:48 -03:00
|
|
|
// @User: Advanced
|
2016-10-22 07:27:40 -03:00
|
|
|
AP_GROUPINFO("REVERSED", 4, RC_Channel, reversed, 0),
|
2013-07-13 00:18:54 -03:00
|
|
|
|
2012-08-17 03:22:48 -03:00
|
|
|
// @Param: DZ
|
|
|
|
// @DisplayName: RC dead-zone
|
2017-05-15 20:23:19 -03:00
|
|
|
// @Description: PWM dead zone in microseconds around trim or bottom
|
2017-05-02 10:49:03 -03:00
|
|
|
// @Units: PWM
|
2013-11-26 09:34:28 -04:00
|
|
|
// @Range: 0 200
|
2012-08-17 03:22:48 -03:00
|
|
|
// @User: Advanced
|
2016-10-22 07:27:40 -03:00
|
|
|
AP_GROUPINFO("DZ", 5, RC_Channel, dead_zone, 0),
|
2013-07-13 00:18:54 -03:00
|
|
|
|
2012-08-17 03:22:48 -03:00
|
|
|
AP_GROUPEND
|
2012-02-11 07:54:21 -04:00
|
|
|
};
|
|
|
|
|
2012-05-31 18:59:03 -03:00
|
|
|
|
2016-10-22 07:27:40 -03:00
|
|
|
// constructor
|
|
|
|
RC_Channel::RC_Channel(void)
|
2010-12-20 23:53:26 -04:00
|
|
|
{
|
2016-10-22 07:27:40 -03:00
|
|
|
AP_Param::setup_object_defaults(this, var_info);
|
2010-12-20 23:53:26 -04:00
|
|
|
}
|
|
|
|
|
2011-05-05 14:46:11 -03:00
|
|
|
void
|
2016-10-22 07:27:40 -03:00
|
|
|
RC_Channel::set_range(uint16_t high)
|
2011-05-05 14:46:11 -03:00
|
|
|
{
|
2016-10-22 07:27:40 -03:00
|
|
|
type_in = RC_CHANNEL_TYPE_RANGE;
|
|
|
|
high_in = high;
|
2015-12-06 17:25:32 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2016-10-22 07:27:40 -03:00
|
|
|
RC_Channel::set_angle(uint16_t angle)
|
2015-12-06 17:25:32 -04:00
|
|
|
{
|
2016-10-22 07:27:40 -03:00
|
|
|
type_in = RC_CHANNEL_TYPE_ANGLE;
|
|
|
|
high_in = angle;
|
2015-12-06 17:25:32 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2016-10-22 07:27:40 -03:00
|
|
|
RC_Channel::set_default_dead_zone(int16_t dzone)
|
2015-12-06 17:25:32 -04:00
|
|
|
{
|
2016-10-22 07:27:40 -03:00
|
|
|
dead_zone.set_default(abs(dzone));
|
2011-05-05 14:46:11 -03:00
|
|
|
}
|
2010-11-28 03:03:23 -04:00
|
|
|
|
2016-10-22 07:27:40 -03:00
|
|
|
bool
|
|
|
|
RC_Channel::get_reverse(void) const
|
2010-11-23 15:28:19 -04:00
|
|
|
{
|
2016-10-22 07:27:40 -03:00
|
|
|
return bool(reversed.get());
|
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
|
|
|
{
|
2016-10-22 07:27:40 -03:00
|
|
|
radio_in = pwm;
|
2012-08-17 03:22:48 -03:00
|
|
|
|
2016-10-22 07:27:40 -03:00
|
|
|
if (type_in == RC_CHANNEL_TYPE_RANGE) {
|
|
|
|
control_in = pwm_to_range();
|
2012-11-05 05:49:42 -04:00
|
|
|
} else {
|
2016-10-22 07:27:40 -03:00
|
|
|
//RC_CHANNEL_TYPE_ANGLE
|
|
|
|
control_in = pwm_to_angle();
|
2012-08-17 03:22:48 -03:00
|
|
|
}
|
2010-11-23 15:28:19 -04:00
|
|
|
}
|
|
|
|
|
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)
|
|
|
|
{
|
2016-10-22 07:27:40 -03:00
|
|
|
radio_in = pwm;
|
2012-12-04 02:27:18 -04:00
|
|
|
|
2016-10-22 07:27:40 -03:00
|
|
|
if (type_in == RC_CHANNEL_TYPE_RANGE) {
|
|
|
|
control_in = pwm_to_range_dz(0);
|
2012-12-04 02:27:18 -04:00
|
|
|
} else {
|
2016-10-22 07:27:40 -03:00
|
|
|
//RC_CHANNEL_ANGLE
|
|
|
|
control_in = pwm_to_angle_dz(0);
|
2012-12-04 02:27:18 -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
|
|
|
|
*/
|
2016-10-22 07:27:40 -03:00
|
|
|
int16_t RC_Channel::get_control_mid() const
|
|
|
|
{
|
|
|
|
if (type_in == RC_CHANNEL_TYPE_RANGE) {
|
|
|
|
int16_t r_in = (radio_min.get() + radio_max.get())/2;
|
2014-11-17 18:00:31 -04:00
|
|
|
|
2016-10-22 07:27:40 -03:00
|
|
|
if (reversed) {
|
|
|
|
r_in = radio_max.get() - (r_in - radio_min.get());
|
2014-11-17 18:00:31 -04:00
|
|
|
}
|
|
|
|
|
2016-10-22 07:27:40 -03:00
|
|
|
int16_t radio_trim_low = radio_min + dead_zone;
|
2014-11-17 18:00:31 -04:00
|
|
|
|
2016-10-22 07:27:40 -03:00
|
|
|
return (((int32_t)(high_in) * (int32_t)(r_in - radio_trim_low)) / (int32_t)(radio_max - radio_trim_low));
|
2014-11-17 18:00:31 -04:00
|
|
|
} else {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-11-28 21:44:40 -04:00
|
|
|
// ------------------------------------------
|
|
|
|
|
2016-10-22 07:27:40 -03:00
|
|
|
void RC_Channel::load_eeprom(void)
|
2010-12-25 21:17:04 -04:00
|
|
|
{
|
2016-10-22 07:27:40 -03:00
|
|
|
radio_min.load();
|
|
|
|
radio_trim.load();
|
|
|
|
radio_max.load();
|
|
|
|
reversed.load();
|
|
|
|
dead_zone.load();
|
2010-12-25 21:17:04 -04:00
|
|
|
}
|
|
|
|
|
2016-10-22 07:27:40 -03:00
|
|
|
void RC_Channel::save_eeprom(void)
|
2010-11-25 03:10:06 -04:00
|
|
|
{
|
2016-10-22 07:27:40 -03:00
|
|
|
radio_min.save();
|
|
|
|
radio_trim.save();
|
|
|
|
radio_max.save();
|
|
|
|
reversed.save();
|
|
|
|
dead_zone.save();
|
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
|
2017-01-06 21:02:32 -04:00
|
|
|
RC_Channel::pwm_to_angle_dz_trim(uint16_t _dead_zone, uint16_t _trim)
|
2010-11-23 15:28:19 -04:00
|
|
|
{
|
2017-01-06 21:02:32 -04:00
|
|
|
int16_t radio_trim_high = _trim + _dead_zone;
|
|
|
|
int16_t radio_trim_low = _trim - _dead_zone;
|
2012-02-19 01:08:17 -04:00
|
|
|
|
2016-10-22 07:27:40 -03:00
|
|
|
int16_t reverse_mul = (reversed?-1:1);
|
2017-08-23 20:43:41 -03:00
|
|
|
if (radio_in > radio_trim_high && radio_max != radio_trim_high) {
|
2016-10-22 07:27:40 -03:00
|
|
|
return reverse_mul * ((int32_t)high_in * (int32_t)(radio_in - radio_trim_high)) / (int32_t)(radio_max - radio_trim_high);
|
2017-08-23 20:43:41 -03:00
|
|
|
} else if (radio_in < radio_trim_low && radio_trim_low != radio_min) {
|
2016-10-22 07:27:40 -03:00
|
|
|
return reverse_mul * ((int32_t)high_in * (int32_t)(radio_in - radio_trim_low)) / (int32_t)(radio_trim_low - radio_min);
|
|
|
|
} else {
|
2012-08-17 03:22:48 -03:00
|
|
|
return 0;
|
2016-10-22 07:27:40 -03:00
|
|
|
}
|
2010-12-20 23:53:26 -04:00
|
|
|
}
|
|
|
|
|
2015-12-26 06:58:26 -04:00
|
|
|
/*
|
|
|
|
return an "angle in centidegrees" (normally -4500 to 4500) from
|
|
|
|
the current radio_in value using the specified dead_zone
|
|
|
|
*/
|
|
|
|
int16_t
|
2016-10-22 07:27:40 -03:00
|
|
|
RC_Channel::pwm_to_angle_dz(uint16_t _dead_zone)
|
2015-12-26 06:58:26 -04:00
|
|
|
{
|
2016-10-22 07:27:40 -03:00
|
|
|
return pwm_to_angle_dz_trim(_dead_zone, radio_trim);
|
2015-12-26 06:58: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()
|
|
|
|
{
|
2016-10-22 07:27:40 -03:00
|
|
|
return pwm_to_angle_dz(dead_zone);
|
2012-11-05 08:37:25 -04:00
|
|
|
}
|
|
|
|
|
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
|
2016-10-22 07:27:40 -03:00
|
|
|
RC_Channel::pwm_to_range_dz(uint16_t _dead_zone)
|
2010-11-23 15:28:19 -04:00
|
|
|
{
|
2016-10-22 07:27:40 -03:00
|
|
|
int16_t r_in = constrain_int16(radio_in, radio_min.get(), radio_max.get());
|
2012-05-31 18:59:03 -03:00
|
|
|
|
2016-10-22 07:27:40 -03:00
|
|
|
if (reversed) {
|
|
|
|
r_in = radio_max.get() - (r_in - radio_min.get());
|
2012-09-16 02:05:26 -03:00
|
|
|
}
|
|
|
|
|
2016-10-22 07:27:40 -03:00
|
|
|
int16_t radio_trim_low = radio_min + _dead_zone;
|
2011-09-27 02:12:39 -03:00
|
|
|
|
2016-10-22 07:27:40 -03:00
|
|
|
if (r_in > radio_trim_low) {
|
|
|
|
return (((int32_t)(high_in) * (int32_t)(r_in - radio_trim_low)) / (int32_t)(radio_max - radio_trim_low));
|
|
|
|
}
|
|
|
|
return 0;
|
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()
|
|
|
|
{
|
2016-10-22 07:27:40 -03:00
|
|
|
return pwm_to_range_dz(dead_zone);
|
2012-12-04 02:27:18 -04:00
|
|
|
}
|
|
|
|
|
2011-09-27 02:12:39 -03:00
|
|
|
|
2016-10-22 07:27:40 -03:00
|
|
|
int16_t RC_Channel::get_control_in_zero_dz(void)
|
2010-11-23 15:28:19 -04:00
|
|
|
{
|
2016-10-22 07:27:40 -03:00
|
|
|
if (type_in == RC_CHANNEL_TYPE_RANGE) {
|
|
|
|
return pwm_to_range_dz(0);
|
2014-03-26 03:38:27 -03:00
|
|
|
}
|
2016-10-22 07:27:40 -03:00
|
|
|
return pwm_to_angle_dz(0);
|
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;
|
2016-10-22 07:27:40 -03:00
|
|
|
int16_t reverse_mul = (reversed?-1:1);
|
|
|
|
if (radio_in < radio_trim) {
|
|
|
|
if (radio_min >= radio_trim) {
|
2015-11-30 06:53:54 -04:00
|
|
|
return 0.0f;
|
|
|
|
}
|
2016-10-22 07:27:40 -03:00
|
|
|
ret = reverse_mul * (float)(radio_in - radio_trim) / (float)(radio_trim - radio_min);
|
2015-02-17 21:47:56 -04:00
|
|
|
} else {
|
2016-10-22 07:27:40 -03:00
|
|
|
if (radio_max <= radio_trim) {
|
2015-11-30 06:53:54 -04:00
|
|
|
return 0.0f;
|
|
|
|
}
|
2016-10-22 07:27:40 -03:00
|
|
|
ret = reverse_mul * (float)(radio_in - radio_trim) / (float)(radio_max - radio_trim);
|
2015-02-17 21:47:56 -04:00
|
|
|
}
|
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()
|
|
|
|
{
|
2016-10-22 07:27:40 -03:00
|
|
|
int16_t dz_min = radio_trim - dead_zone;
|
|
|
|
int16_t dz_max = radio_trim + dead_zone;
|
2014-11-17 21:44:05 -04:00
|
|
|
float ret;
|
2016-10-22 07:27:40 -03:00
|
|
|
int16_t reverse_mul = (reversed?-1:1);
|
|
|
|
if (radio_in < dz_min && dz_min > radio_min) {
|
|
|
|
ret = reverse_mul * (float)(radio_in - dz_min) / (float)(dz_min - radio_min);
|
|
|
|
} else if (radio_in > dz_max && radio_max > dz_max) {
|
|
|
|
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()
|
|
|
|
{
|
2016-10-22 07:27:40 -03:00
|
|
|
if (radio_in <= radio_min) {
|
|
|
|
return reversed?100:0;
|
2014-03-04 18:16:12 -04:00
|
|
|
}
|
2016-10-22 07:27:40 -03:00
|
|
|
if (radio_in >= radio_max) {
|
|
|
|
return reversed?0:100;
|
2014-03-04 18:16:12 -04:00
|
|
|
}
|
2016-10-22 07:27:40 -03:00
|
|
|
uint8_t ret = 100.0f * (radio_in - radio_min) / (float)(radio_max - radio_min);
|
|
|
|
if (reversed) {
|
2014-03-04 18:16:12 -04:00
|
|
|
ret = 100 - ret;
|
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2012-11-26 02:16:25 -04:00
|
|
|
void
|
|
|
|
RC_Channel::input()
|
|
|
|
{
|
2016-10-22 07:27:40 -03:00
|
|
|
radio_in = hal.rcin->read(ch_in);
|
2012-11-26 02:16:25 -04:00
|
|
|
}
|
|
|
|
|
2013-06-03 03:12:02 -03:00
|
|
|
uint16_t
|
|
|
|
RC_Channel::read() const
|
|
|
|
{
|
2016-10-22 07:27:40 -03:00
|
|
|
return hal.rcin->read(ch_in);
|
2014-04-20 21:34:10 -03:00
|
|
|
}
|
2015-10-30 02:46:03 -03:00
|
|
|
|
|
|
|
/*
|
|
|
|
Return true if the channel is at trim and within the DZ
|
|
|
|
*/
|
|
|
|
bool RC_Channel::in_trim_dz()
|
|
|
|
{
|
2016-10-22 07:27:40 -03:00
|
|
|
return is_bounded_int32(radio_in, radio_trim - dead_zone, radio_trim + dead_zone);
|
2015-10-30 02:46:03 -03:00
|
|
|
}
|
2016-10-11 08:00:48 -03:00
|
|
|
|
2018-01-08 23:41:02 -04:00
|
|
|
|
|
|
|
bool RC_Channel::min_max_configured() const
|
|
|
|
{
|
|
|
|
if (configured_mask & (1U << ch_in)) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
if (radio_min.configured() && radio_max.configured()) {
|
|
|
|
// once a channel is known to be configured it has to stay
|
|
|
|
// configured due to the nature of AP_Param
|
|
|
|
configured_mask |= (1U<<ch_in);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|