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 "WProgram.h"
|
|
|
|
#include "RC_ChannelB.h"
|
|
|
|
|
|
|
|
void
|
2010-12-25 03:09:37 -04:00
|
|
|
RC_ChannelB::readRadio(uint16_t pwmRadio)
|
2010-12-24 20:06:37 -04:00
|
|
|
{
|
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
|
|
|
}
|
|
|
|
|
2010-12-25 03:09:37 -04:00
|
|
|
void
|
|
|
|
RC_ChannelB::loadEEProm()
|
|
|
|
{
|
|
|
|
if (_storageType == STORE_EEPROM)
|
|
|
|
{
|
|
|
|
eeprom_read_block((void*)&_scale,(const void*)(_address),sizeof(_scale));
|
|
|
|
eeprom_read_block((void*)&_pwmMin,(const void*)(_address + 4),sizeof(_pwmMin));
|
|
|
|
eeprom_read_block((void*)&_pwmMax,(const void*)(_address + 6),sizeof(_pwmMax));
|
|
|
|
eeprom_read_block((void*)&_pwmNeutral,(const void*)(_address + 8),sizeof(_pwmNeutral));
|
|
|
|
eeprom_read_block((void*)&_pwmDeadZone,(const void*)(_address + 10),sizeof(_pwmDeadZone));
|
|
|
|
eeprom_read_block((void*)&_filter,(const void*)(_address+12),sizeof(_filter));
|
|
|
|
eeprom_read_block((void*)&_pwmDeadZone,(const void*)(_address+13),sizeof(_pwmDeadZone));
|
|
|
|
}
|
2010-12-24 20:06:37 -04:00
|
|
|
}
|
|
|
|
|
2010-12-25 03:09:37 -04:00
|
|
|
void
|
|
|
|
RC_ChannelB::saveEEProm()
|
|
|
|
{
|
|
|
|
if (_storageType == STORE_EEPROM)
|
|
|
|
{
|
|
|
|
eeprom_write_block((const void*)&_scale,(void*)(_address),sizeof(_scale));
|
|
|
|
eeprom_write_block((const void*)&_pwmMin,(void*)(_address + 4),sizeof(_pwmMin));
|
|
|
|
eeprom_write_block((const void*)&_pwmMax,(void*)(_address + 6),sizeof(_pwmMax));
|
|
|
|
eeprom_write_block((const void*)&_pwmNeutral,(void*)(_address + 8),sizeof(_pwmNeutral));
|
|
|
|
eeprom_write_block((const void*)&_pwmDeadZone,(void*)(_address + 10),sizeof(_pwmDeadZone));
|
|
|
|
eeprom_write_block((const void*)&_filter,(void*)(_address+12),sizeof(_filter));
|
|
|
|
eeprom_write_block((const void*)&_pwmDeadZone,(void*)(_address+13),sizeof(_pwmDeadZone));
|
|
|
|
}
|
2010-12-24 20:06:37 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// ------------------------------------------
|