2010-12-24 20:06:37 -04:00
|
|
|
/*
|
|
|
|
RC_ChannelB.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>
|
|
|
|
#include "RC_ChannelB.h"
|
2010-12-25 12:51:35 -04:00
|
|
|
#include <AP_Common.h>
|
2010-12-24 20:06:37 -04:00
|
|
|
|
2010-12-25 12:51:35 -04:00
|
|
|
void RC_ChannelB::readRadio(uint16_t pwmRadio) {
|
2010-12-25 03:09:37 -04:00
|
|
|
// apply reverse
|
|
|
|
if(_reverse) _pwmRadio = (_pwmNeutral - pwmRadio) + _pwmNeutral;
|
|
|
|
else _pwmRadio = pwmRadio;
|
2010-12-24 20:06:37 -04:00
|
|
|
|
2010-12-25 03:09:37 -04:00
|
|
|
setPwm(pwmRadio);
|
2010-12-24 20:06:37 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2010-12-25 03:09:37 -04:00
|
|
|
RC_ChannelB::setPwm(uint16_t pwm)
|
2010-12-24 20:06:37 -04:00
|
|
|
{
|
2010-12-25 03:09:37 -04:00
|
|
|
// apply reverse
|
|
|
|
if(_reverse) pwm = (_pwmNeutral-pwm) + _pwmNeutral;
|
2010-12-24 20:06:37 -04:00
|
|
|
|
2010-12-25 03:09:37 -04:00
|
|
|
// apply filter
|
2010-12-24 20:06:37 -04:00
|
|
|
if(_filter){
|
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 03:09:37 -04:00
|
|
|
// apply deadzone
|
|
|
|
_pwm = (abs(_pwm - _pwmNeutral) < _pwmDeadZone) ? _pwmNeutral : _pwm;
|
2010-12-24 20:06:37 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2010-12-25 03:09:37 -04:00
|
|
|
RC_ChannelB::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 03:09:37 -04:00
|
|
|
RC_ChannelB::mixRadio(uint16_t infStart)
|
2010-12-24 20:06:37 -04:00
|
|
|
{
|
2010-12-25 03:09:37 -04:00
|
|
|
float inf = abs(_pwmRadio - _pwmNeutral);
|
|
|
|
inf = min(inf, infStart);
|
|
|
|
inf = ((infStart - inf) /infStart);
|
|
|
|
setPwm(_pwm*inf + _pwmRadio);
|
2010-12-24 20:06:37 -04:00
|
|
|
}
|
|
|
|
|
2010-12-25 03:09:37 -04:00
|
|
|
uint16_t
|
|
|
|
RC_ChannelB::_positionToPwm(float position)
|
2010-12-24 20:06:37 -04:00
|
|
|
{
|
2010-12-25 03:09:37 -04:00
|
|
|
if(position < 0)
|
|
|
|
return (position / _scale) * (_pwmMin - _pwmNeutral);
|
2010-12-24 20:06:37 -04:00
|
|
|
else
|
2010-12-25 03:09:37 -04:00
|
|
|
return (position / _scale) * (_pwmMax - _pwmNeutral);
|
2010-12-24 20:06:37 -04:00
|
|
|
}
|
|
|
|
|
2010-12-25 03:09:37 -04:00
|
|
|
float
|
|
|
|
RC_ChannelB::_pwmToPosition(uint16_t pwm)
|
2010-12-24 20:06:37 -04:00
|
|
|
{
|
2010-12-25 03:09:37 -04:00
|
|
|
if(_pwm < _pwmNeutral)
|
|
|
|
return _scale * (_pwm - _pwmNeutral)/(_pwmNeutral - _pwmMin);
|
2010-12-24 20:06:37 -04:00
|
|
|
else
|
2010-12-25 03:09:37 -04:00
|
|
|
return _scale * (_pwm - _pwmNeutral)/(_pwmMax - _pwmNeutral);
|
2010-12-24 20:06:37 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// ------------------------------------------
|