2010-11-23 15:28:19 -04:00
|
|
|
/*
|
|
|
|
RC_Channel.cpp - Radio library for Arduino
|
|
|
|
Code by Jason Short. DIYDrones.com
|
2011-02-14 00:43:44 -04:00
|
|
|
|
2010-11-23 15:28:19 -04:00
|
|
|
This library is free software; you can redistribute it and / or
|
|
|
|
modify it under the terms of the GNU Lesser General Public
|
|
|
|
License as published by the Free Software Foundation; either
|
|
|
|
version 2.1 of the License, or (at your option) any later version.
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
2010-11-28 03:03:23 -04:00
|
|
|
#include <math.h>
|
|
|
|
#include <avr/eeprom.h>
|
2012-01-27 23:25:47 -04:00
|
|
|
#if defined(ARDUINO) && ARDUINO >= 100
|
|
|
|
#include "Arduino.h"
|
|
|
|
#else
|
|
|
|
#include "WProgram.h"
|
|
|
|
#endif
|
2010-11-23 15:28:19 -04:00
|
|
|
#include "RC_Channel.h"
|
|
|
|
|
2011-05-05 14:46:11 -03:00
|
|
|
#define RC_CHANNEL_ANGLE 0
|
|
|
|
#define RC_CHANNEL_RANGE 1
|
|
|
|
#define RC_CHANNEL_ANGLE_RAW 2
|
2010-11-23 15:28:19 -04:00
|
|
|
|
2011-11-12 23:18:14 -04:00
|
|
|
APM_RC_Class *RC_Channel::_apm_rc;
|
2011-05-06 14:51:26 -03:00
|
|
|
|
2012-02-11 07:54:21 -04:00
|
|
|
const AP_Param::GroupInfo RC_Channel::var_info[] PROGMEM = {
|
2012-07-17 18:32:30 -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.
|
|
|
|
// @Units: ms
|
|
|
|
// @Range: 800 2200
|
|
|
|
// @Increment: 1
|
|
|
|
// @User: Advanced
|
2012-02-12 18:55:13 -04:00
|
|
|
AP_GROUPINFO("MIN", 0, RC_Channel, radio_min),
|
2012-07-17 18:32:30 -03:00
|
|
|
|
|
|
|
// @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.
|
|
|
|
// @Units: ms
|
|
|
|
// @Range: 800 2200
|
|
|
|
// @Increment: 1
|
|
|
|
// @User: Advanced
|
2012-02-12 18:55:13 -04:00
|
|
|
AP_GROUPINFO("TRIM", 1, RC_Channel, radio_trim),
|
2012-07-17 18:32:30 -03:00
|
|
|
|
|
|
|
// @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.
|
|
|
|
// @Units: ms
|
|
|
|
// @Range: 800 2200
|
|
|
|
// @Increment: 1
|
|
|
|
// @User: Advanced
|
2012-02-12 18:55:13 -04:00
|
|
|
AP_GROUPINFO("MAX", 2, RC_Channel, radio_max),
|
2012-07-17 18:32:30 -03:00
|
|
|
|
|
|
|
// @Param: REV
|
|
|
|
// @DisplayName: RC reverse
|
2012-07-18 17:38:41 -03:00
|
|
|
// @Description: Reverse servo operation. Ignored on APM1 unless dip-switches are disabled.
|
2012-07-17 21:11:49 -03:00
|
|
|
// @Values: -1:Reversed,1:Normal
|
2012-07-17 18:32:30 -03:00
|
|
|
// @User: Advanced
|
2012-02-12 18:55:13 -04:00
|
|
|
AP_GROUPINFO("REV", 3, RC_Channel, _reverse),
|
2012-07-17 18:32:30 -03:00
|
|
|
|
|
|
|
// @Param: DZ
|
|
|
|
// @DisplayName: RC dead-zone
|
|
|
|
// @Description: dead zone around trim.
|
|
|
|
// @User: Advanced
|
2012-02-12 18:55:13 -04:00
|
|
|
AP_GROUPINFO("DZ", 4, RC_Channel, _dead_zone),
|
2012-02-12 03:23:19 -04: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-05-31 18:59:03 -03:00
|
|
|
_type = RC_CHANNEL_RANGE;
|
|
|
|
_high = high;
|
|
|
|
_low = low;
|
|
|
|
_high_out = high;
|
|
|
|
_low_out = low;
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
{
|
|
|
|
_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
|
|
|
{
|
2011-05-05 14:46:11 -03:00
|
|
|
_type = RC_CHANNEL_ANGLE;
|
2010-11-23 15:28:19 -04:00
|
|
|
_high = angle;
|
|
|
|
}
|
|
|
|
|
2011-09-27 02:12:39 -03:00
|
|
|
void
|
2012-06-02 02:20:58 -03:00
|
|
|
RC_Channel::set_dead_zone(int16_t dzone)
|
2011-09-27 02:12:39 -03:00
|
|
|
{
|
2011-10-27 15:34:00 -03:00
|
|
|
_dead_zone.set_and_save(abs(dzone >>1));
|
2011-09-27 02:12:39 -03:00
|
|
|
}
|
|
|
|
|
2010-12-20 23:53:26 -04:00
|
|
|
void
|
|
|
|
RC_Channel::set_reverse(bool reverse)
|
|
|
|
{
|
|
|
|
if (reverse) _reverse = -1;
|
|
|
|
else _reverse = 1;
|
|
|
|
}
|
|
|
|
|
2011-01-27 14:16:22 -04:00
|
|
|
bool
|
|
|
|
RC_Channel::get_reverse(void)
|
|
|
|
{
|
|
|
|
if (_reverse==-1) return 1;
|
|
|
|
else return 0;
|
|
|
|
}
|
|
|
|
|
2010-11-28 03:03:23 -04:00
|
|
|
void
|
|
|
|
RC_Channel::set_filter(bool filter)
|
|
|
|
{
|
|
|
|
_filter = filter;
|
|
|
|
}
|
2011-05-06 14:51:26 -03:00
|
|
|
|
2011-05-05 14:46:11 -03:00
|
|
|
void
|
|
|
|
RC_Channel::set_type(uint8_t t)
|
|
|
|
{
|
|
|
|
_type = t;
|
|
|
|
}
|
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
|
|
|
{
|
2010-11-25 03:10:06 -04: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-05-31 15:45:07 -03:00
|
|
|
/*if(_filter){
|
2010-11-28 03:03:23 -04:00
|
|
|
if(radio_in == 0)
|
|
|
|
radio_in = pwm;
|
|
|
|
else
|
2011-09-08 01:58:42 -03:00
|
|
|
radio_in = (pwm + radio_in) >> 1; // Small filtering
|
2010-11-28 03:03:23 -04:00
|
|
|
}else{
|
2010-11-25 03:10:06 -04:00
|
|
|
radio_in = pwm;
|
2012-05-31 15:45:07 -03:00
|
|
|
}*/
|
|
|
|
|
2012-05-31 18:59:03 -03:00
|
|
|
radio_in = pwm;
|
2011-02-14 00:43:44 -04:00
|
|
|
|
2011-05-05 14:46:11 -03:00
|
|
|
if(_type == RC_CHANNEL_RANGE){
|
2010-11-23 15:28:19 -04:00
|
|
|
control_in = pwm_to_range();
|
2012-02-19 01:08:17 -04:00
|
|
|
//control_in = constrain(control_in, _low, _high);
|
2012-05-31 18:59:03 -03:00
|
|
|
//control_in = min(control_in, _high);
|
2011-09-27 02:12:39 -03:00
|
|
|
control_in = (control_in < _dead_zone) ? 0 : control_in;
|
|
|
|
|
|
|
|
if (fabs(scale_output) != 1){
|
|
|
|
control_in *= scale_output;
|
|
|
|
}
|
|
|
|
|
2010-11-23 15:28:19 -04:00
|
|
|
}else{
|
2011-09-27 02:12:39 -03:00
|
|
|
|
|
|
|
//RC_CHANNEL_ANGLE, RC_CHANNEL_ANGLE_RAW
|
2010-11-23 15:28:19 -04:00
|
|
|
control_in = pwm_to_angle();
|
2012-02-11 12:51:02 -04:00
|
|
|
|
2011-09-27 02:12:39 -03:00
|
|
|
|
|
|
|
if (fabs(scale_output) != 1){
|
|
|
|
control_in *= scale_output;
|
|
|
|
}
|
2011-09-21 18:52:20 -03:00
|
|
|
|
2011-09-16 23:06:53 -03:00
|
|
|
/*
|
2011-09-27 02:12:39 -03:00
|
|
|
// coming soon ??
|
2011-09-16 23:06:53 -03:00
|
|
|
if(expo) {
|
|
|
|
long temp = control_in;
|
|
|
|
temp = (temp * temp) / (long)_high;
|
2012-06-02 02:20:58 -03:00
|
|
|
control_in = (int16_t)((control_in >= 0) ? temp : -temp);
|
2011-09-16 23:06:53 -03:00
|
|
|
}*/
|
2010-11-23 15:28:19 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-06-02 02:20:58 -03:00
|
|
|
int16_t
|
2010-11-26 23:04:30 -04:00
|
|
|
RC_Channel::control_mix(float value)
|
|
|
|
{
|
|
|
|
return (1 - abs(control_in / _high)) * value + control_in;
|
|
|
|
}
|
|
|
|
|
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)
|
|
|
|
{
|
2010-11-25 03:10:06 -04: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)
|
|
|
|
{
|
2011-05-05 14:46:11 -03:00
|
|
|
if(_type == RC_CHANNEL_RANGE){
|
2010-12-27 19:03:26 -04:00
|
|
|
pwm_out = range_to_pwm();
|
2011-09-21 18:52:20 -03:00
|
|
|
radio_out = (_reverse >= 0) ? (radio_min + pwm_out) : (radio_max - pwm_out);
|
2011-05-05 14:46:11 -03:00
|
|
|
|
|
|
|
}else if(_type == RC_CHANNEL_ANGLE_RAW){
|
|
|
|
pwm_out = (float)servo_out * .1;
|
2011-09-21 18:52:20 -03:00
|
|
|
radio_out = (pwm_out * _reverse) + radio_trim;
|
2011-05-05 14:46:11 -03:00
|
|
|
|
2011-09-27 02:12:39 -03:00
|
|
|
}else{ // RC_CHANNEL_ANGLE
|
2010-12-27 19:03:26 -04:00
|
|
|
pwm_out = angle_to_pwm();
|
|
|
|
radio_out = pwm_out + radio_trim;
|
2010-11-23 15:28:19 -04:00
|
|
|
}
|
2011-09-21 18:52:20 -03:00
|
|
|
|
2011-02-14 00:43:44 -04:00
|
|
|
radio_out = constrain(radio_out, radio_min.get(), radio_max.get());
|
2010-11-23 15:28:19 -04:00
|
|
|
}
|
|
|
|
|
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-02-11 07:54:21 -04:00
|
|
|
radio_min.load();
|
|
|
|
radio_trim.load();
|
|
|
|
radio_max.load();
|
|
|
|
_reverse.load();
|
2012-02-12 23:19:13 -04:00
|
|
|
_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-02-11 07:54:21 -04:00
|
|
|
radio_min.save();
|
|
|
|
radio_trim.save();
|
|
|
|
radio_max.save();
|
|
|
|
_reverse.save();
|
2012-02-12 23:19:13 -04:00
|
|
|
_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()
|
|
|
|
{
|
2011-02-14 00:43:44 -04: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()
|
|
|
|
{
|
2011-02-14 00:43:44 -04: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
|
|
|
|
2010-11-23 15:28:19 -04:00
|
|
|
// ------------------------------------------
|
|
|
|
|
2010-12-25 21:17:04 -04:00
|
|
|
int16_t
|
2010-11-23 15:28:19 -04:00
|
|
|
RC_Channel::pwm_to_angle()
|
|
|
|
{
|
2012-06-02 02:20:58 -03: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
|
|
|
|
|
|
|
if(radio_in > radio_trim_high){
|
|
|
|
return _reverse * ((long)_high * (long)(radio_in - radio_trim_high)) / (long)(radio_max - radio_trim_high);
|
|
|
|
}else if(radio_in < radio_trim_low){
|
|
|
|
return _reverse * ((long)_high * (long)(radio_in - radio_trim_low)) / (long)(radio_trim_low - radio_min);
|
|
|
|
}else
|
|
|
|
return 0;
|
2010-12-20 23:53:26 -04:00
|
|
|
}
|
|
|
|
|
2010-11-23 15:28:19 -04:00
|
|
|
|
|
|
|
int16_t
|
|
|
|
RC_Channel::angle_to_pwm()
|
|
|
|
{
|
2011-07-13 02:54:40 -03:00
|
|
|
if((servo_out * _reverse) > 0)
|
|
|
|
return _reverse * ((long)servo_out * (long)(radio_max - radio_trim)) / (long)_high;
|
|
|
|
else
|
|
|
|
return _reverse * ((long)servo_out * (long)(radio_trim - radio_min)) / (long)_high;
|
2010-11-23 15:28:19 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// ------------------------------------------
|
|
|
|
|
|
|
|
int16_t
|
|
|
|
RC_Channel::pwm_to_range()
|
|
|
|
{
|
2012-05-31 18:59:03 -03:00
|
|
|
int16_t r_in = constrain(radio_in, radio_min.get(), radio_max.get());
|
|
|
|
|
2012-06-02 02:20:58 -03:00
|
|
|
int16_t radio_trim_low = radio_min + _dead_zone;
|
2011-09-27 02:12:39 -03:00
|
|
|
|
2012-05-31 18:59:03 -03:00
|
|
|
if(r_in > radio_trim_low)
|
|
|
|
return (_low + ((long)(_high - _low) * (long)(r_in - radio_trim_low)) / (long)(radio_max - radio_trim_low));
|
2012-05-31 16:05:47 -03:00
|
|
|
else if(_dead_zone > 0)
|
|
|
|
return 0;
|
2011-09-27 02:12:39 -03:00
|
|
|
else
|
2012-05-31 15:31:48 -03:00
|
|
|
return _low;
|
2010-11-23 15:28:19 -04:00
|
|
|
}
|
|
|
|
|
2011-09-27 02:12:39 -03:00
|
|
|
|
2010-11-23 15:28:19 -04:00
|
|
|
int16_t
|
|
|
|
RC_Channel::range_to_pwm()
|
|
|
|
{
|
2012-05-31 18:59:03 -03:00
|
|
|
return ((long)(servo_out - _low_out) * (long)(radio_max - radio_min)) / (long)(_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()
|
|
|
|
{
|
|
|
|
if(radio_in < radio_trim)
|
|
|
|
return _reverse * (float)(radio_in - radio_trim) / (float)(radio_trim - radio_min);
|
|
|
|
else
|
|
|
|
return _reverse * (float)(radio_in - radio_trim) / (float)(radio_max - radio_trim);
|
|
|
|
}
|
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_output()
|
|
|
|
{
|
2012-02-10 23:35:59 -04:00
|
|
|
int16_t mid = (radio_max + radio_min) / 2;
|
2012-02-19 01:08:17 -04:00
|
|
|
|
2012-02-10 23:35:59 -04:00
|
|
|
if(radio_out < mid)
|
2012-01-20 04:24:55 -04:00
|
|
|
return (float)(radio_out - mid) / (float)(mid - radio_min);
|
2010-12-27 19:03:26 -04:00
|
|
|
else
|
2012-01-20 04:24:55 -04:00
|
|
|
return (float)(radio_out - mid) / (float)(radio_max - mid);
|
2010-12-27 19:03:26 -04:00
|
|
|
}
|
2011-11-12 23:18:14 -04:00
|
|
|
|
|
|
|
void RC_Channel::set_apm_rc( APM_RC_Class * apm_rc )
|
|
|
|
{
|
|
|
|
_apm_rc = apm_rc;
|
|
|
|
}
|