2010-12-24 20:06:37 -04:00
|
|
|
/*
|
2010-12-25 15:14:47 -04:00
|
|
|
AP_RcChannel.cpp - Radio library for Arduino
|
2010-12-25 03:09:37 -04:00
|
|
|
Code by Jason Short, James Goppert. DIYDrones.com
|
2010-12-24 20:06:37 -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.
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <math.h>
|
|
|
|
#include <avr/eeprom.h>
|
2010-12-25 15:14:47 -04:00
|
|
|
#include "AP_RcChannel.h"
|
2010-12-25 12:51:35 -04:00
|
|
|
#include <AP_Common.h>
|
2010-12-24 20:06:37 -04:00
|
|
|
|
2010-12-26 14:07:08 -04:00
|
|
|
void AP_RcChannel::readRadio() {
|
2010-12-25 03:09:37 -04:00
|
|
|
// apply reverse
|
2010-12-29 04:26:21 -04:00
|
|
|
uint16_t pwmRadio = APM_RC.InputCh(getCh());
|
2010-12-25 03:09:37 -04:00
|
|
|
setPwm(pwmRadio);
|
2010-12-24 20:06:37 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2010-12-25 15:14:47 -04:00
|
|
|
AP_RcChannel::setPwm(uint16_t pwm)
|
2010-12-24 20:06:37 -04:00
|
|
|
{
|
2010-12-29 04:26:21 -04:00
|
|
|
//Serial.printf("reverse: %s\n", (getReverse())?"true":"false");
|
2010-12-25 15:03:38 -04:00
|
|
|
|
2010-12-25 03:09:37 -04:00
|
|
|
// apply reverse
|
2010-12-29 04:26:21 -04:00
|
|
|
if(getReverse()) pwm = int16_t(getPwmNeutral()-pwm) + getPwmNeutral();
|
2010-12-25 15:03:38 -04:00
|
|
|
|
|
|
|
//Serial.printf("pwm after reverse: %d\n", pwm);
|
2010-12-24 20:06:37 -04:00
|
|
|
|
2010-12-25 03:09:37 -04:00
|
|
|
// apply filter
|
2010-12-29 04:26:21 -04:00
|
|
|
if(getFilter()){
|
2010-12-25 03:09:37 -04:00
|
|
|
if(_pwm == 0)
|
|
|
|
_pwm = pwm;
|
2010-12-24 20:06:37 -04:00
|
|
|
else
|
2010-12-25 03:09:37 -04:00
|
|
|
_pwm = ((pwm + _pwm) >> 1); // Small filtering
|
2010-12-24 20:06:37 -04:00
|
|
|
}else{
|
2010-12-25 03:09:37 -04:00
|
|
|
_pwm = pwm;
|
2010-12-24 20:06:37 -04:00
|
|
|
}
|
|
|
|
|
2010-12-25 15:03:38 -04:00
|
|
|
//Serial.printf("pwm after filter: %d\n", _pwm);
|
|
|
|
|
2010-12-25 03:09:37 -04:00
|
|
|
// apply deadzone
|
2010-12-29 04:26:21 -04:00
|
|
|
_pwm = (abs(_pwm - getPwmNeutral()) < getPwmDeadZone()) ? getPwmNeutral() : _pwm;
|
2010-12-25 15:03:38 -04:00
|
|
|
|
|
|
|
//Serial.printf("pwm after deadzone: %d\n", _pwm);
|
2010-12-29 04:26:21 -04:00
|
|
|
APM_RC.OutputCh(getCh(),_pwm);
|
2010-12-24 20:06:37 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2010-12-25 15:14:47 -04:00
|
|
|
AP_RcChannel::setPosition(float position)
|
2010-12-24 20:06:37 -04:00
|
|
|
{
|
2010-12-25 03:09:37 -04:00
|
|
|
setPwm(_positionToPwm(position));
|
2010-12-24 20:06:37 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2010-12-25 15:14:47 -04:00
|
|
|
AP_RcChannel::mixRadio(uint16_t infStart)
|
2010-12-24 20:06:37 -04:00
|
|
|
{
|
2010-12-29 04:26:21 -04:00
|
|
|
uint16_t pwmRadio = APM_RC.InputCh(getCh());
|
|
|
|
float inf = abs( int16_t(pwmRadio - getPwmNeutral()) );
|
2010-12-25 03:09:37 -04:00
|
|
|
inf = min(inf, infStart);
|
|
|
|
inf = ((infStart - inf) /infStart);
|
2010-12-26 14:07:08 -04:00
|
|
|
setPwm(_pwm*inf + pwmRadio);
|
2010-12-24 20:06:37 -04:00
|
|
|
}
|
|
|
|
|
2010-12-25 03:09:37 -04:00
|
|
|
uint16_t
|
2010-12-25 15:14:47 -04:00
|
|
|
AP_RcChannel::_positionToPwm(const float & position)
|
2010-12-24 20:06:37 -04:00
|
|
|
{
|
2010-12-25 15:03:38 -04:00
|
|
|
uint16_t pwm;
|
2010-12-29 04:26:21 -04:00
|
|
|
float p = position - getCenter();
|
2010-12-25 15:03:38 -04:00
|
|
|
//Serial.printf("position: %f\n", position);
|
2010-12-29 04:26:21 -04:00
|
|
|
if(p < 0)
|
|
|
|
pwm = p * int16_t(getPwmNeutral() - getPwmMin()) /
|
|
|
|
getScale() + getPwmNeutral();
|
2010-12-24 20:06:37 -04:00
|
|
|
else
|
2010-12-29 04:26:21 -04:00
|
|
|
pwm = p * int16_t(getPwmMax() - getPwmNeutral()) /
|
|
|
|
getScale() + getPwmNeutral();
|
|
|
|
constrain(pwm,getPwmMin(),getPwmMax());
|
2010-12-25 15:03:38 -04:00
|
|
|
return pwm;
|
2010-12-24 20:06:37 -04:00
|
|
|
}
|
|
|
|
|
2010-12-25 03:09:37 -04:00
|
|
|
float
|
2010-12-25 15:14:47 -04:00
|
|
|
AP_RcChannel::_pwmToPosition(const uint16_t & pwm)
|
2010-12-24 20:06:37 -04:00
|
|
|
{
|
2010-12-25 15:03:38 -04:00
|
|
|
float position;
|
2010-12-29 04:26:21 -04:00
|
|
|
if(pwm < getPwmNeutral())
|
|
|
|
position = getScale() * int16_t(pwm - getPwmNeutral())/
|
|
|
|
int16_t(getPwmNeutral() - getPwmMin()) + getCenter();
|
2010-12-24 20:06:37 -04:00
|
|
|
else
|
2010-12-29 04:26:21 -04:00
|
|
|
position = getScale() * int16_t(pwm -getPwmNeutral())/
|
|
|
|
int16_t(getPwmMax() - getPwmNeutral()) + getCenter();
|
|
|
|
constrain(position,-getScale()+getCenter(),
|
|
|
|
getScale()+getCenter());
|
2010-12-25 15:03:38 -04:00
|
|
|
return position;
|
2010-12-24 20:06:37 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// ------------------------------------------
|