2011-09-28 21:51:12 -03:00
|
|
|
/*
|
|
|
|
* ControllerTank.h
|
|
|
|
*
|
|
|
|
* Created on: Jun 30, 2011
|
|
|
|
* Author: jgoppert
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef CONTROLLERTANK_H_
|
|
|
|
#define CONTROLLERTANK_H_
|
|
|
|
|
|
|
|
#include "../APO/AP_Controller.h"
|
|
|
|
|
|
|
|
namespace apo {
|
|
|
|
|
|
|
|
class ControllerTank: public AP_Controller {
|
2011-10-25 20:40:07 -03:00
|
|
|
public:
|
2011-09-28 21:51:12 -03:00
|
|
|
enum {
|
|
|
|
k_chMode = k_radioChannelsStart, k_chLeft, k_chRight, k_chStr, k_chThr
|
|
|
|
};
|
|
|
|
enum {
|
|
|
|
k_pidStr = k_controllersStart, k_pidThr
|
|
|
|
};
|
|
|
|
enum {
|
2011-10-25 20:40:07 -03:00
|
|
|
ch_mode = 0, ch_left, ch_right, ch_str, ch_thrust
|
2011-09-28 21:51:12 -03:00
|
|
|
};
|
2011-10-25 20:40:07 -03:00
|
|
|
|
2011-09-28 21:51:12 -03:00
|
|
|
ControllerTank(AP_Navigator * nav, AP_Guide * guide,
|
2011-10-25 20:40:07 -03:00
|
|
|
AP_HardwareAbstractionLayer * hal) :
|
|
|
|
AP_Controller(nav, guide, hal, new AP_ArmingMechanism(hal,ch_thrust,ch_str,0.1,-0.9,0.9),ch_mode),
|
|
|
|
pidStr(new AP_Var_group(k_pidStr, PSTR("STR_")), 1, steeringP,
|
|
|
|
steeringI, steeringD, steeringIMax, steeringYMax, steeringDFCut),
|
|
|
|
pidThr(new AP_Var_group(k_pidThr, PSTR("THR_")), 1, throttleP,
|
|
|
|
throttleI, throttleD, throttleIMax, throttleYMax,
|
|
|
|
throttleDFCut), _headingOutput(0), _throttleOutput(0) {
|
2011-09-28 21:51:12 -03:00
|
|
|
_hal->debug->println_P(PSTR("initializing tank controller"));
|
|
|
|
|
|
|
|
_hal->rc.push_back(
|
|
|
|
new AP_RcChannel(k_chMode, PSTR("MODE_"), APM_RC, 5, 1100,
|
2011-10-03 13:42:27 -03:00
|
|
|
1500, 1900, RC_MODE_IN, false));
|
2011-09-28 21:51:12 -03:00
|
|
|
_hal->rc.push_back(
|
|
|
|
new AP_RcChannel(k_chLeft, PSTR("LEFT_"), APM_RC, 0, 1100, 1500,
|
2011-10-03 13:42:27 -03:00
|
|
|
1900, RC_MODE_OUT, false));
|
2011-09-28 21:51:12 -03:00
|
|
|
_hal->rc.push_back(
|
|
|
|
new AP_RcChannel(k_chRight, PSTR("RIGHT_"), APM_RC, 1, 1100, 1500,
|
2011-10-03 13:42:27 -03:00
|
|
|
1900, RC_MODE_OUT, false));
|
2011-09-28 21:51:12 -03:00
|
|
|
_hal->rc.push_back(
|
|
|
|
new AP_RcChannel(k_chStr, PSTR("STR_"), APM_RC, 0, 1100, 1500,
|
2011-10-03 13:42:27 -03:00
|
|
|
1900, RC_MODE_IN, false));
|
2011-09-28 21:51:12 -03:00
|
|
|
_hal->rc.push_back(
|
|
|
|
new AP_RcChannel(k_chThr, PSTR("THR_"), APM_RC, 1, 1100, 1500,
|
2011-10-03 13:42:27 -03:00
|
|
|
1900, RC_MODE_IN, false));
|
2011-09-28 21:51:12 -03:00
|
|
|
}
|
2011-10-25 20:40:07 -03:00
|
|
|
|
|
|
|
void manualLoop(const float dt) {
|
|
|
|
setAllRadioChannelsManually();
|
|
|
|
_headingOutput = _hal->rc[ch_str]->getPosition();
|
|
|
|
_throttleOutput = _hal->rc[ch_thrust]->getPosition();
|
2011-09-28 21:51:12 -03:00
|
|
|
}
|
|
|
|
|
2011-10-25 20:40:07 -03:00
|
|
|
void autoLoop(const float dt) {
|
|
|
|
float headingError = _guide->getHeadingCommand()
|
|
|
|
- _nav->getYaw();
|
|
|
|
if (headingError > 180 * deg2Rad)
|
|
|
|
headingError -= 360 * deg2Rad;
|
|
|
|
if (headingError < -180 * deg2Rad)
|
|
|
|
headingError += 360 * deg2Rad;
|
|
|
|
_headingOutput = pidStr.update(headingError, _nav->getYawRate(), dt);
|
|
|
|
_throttleOutput = pidThr.update(_guide->getGroundSpeedCommand()
|
|
|
|
- _nav->getGroundSpeed(), dt);
|
|
|
|
}
|
2011-09-28 21:51:12 -03:00
|
|
|
|
2011-10-25 20:40:07 -03:00
|
|
|
void setMotors() {
|
2011-09-28 21:51:12 -03:00
|
|
|
|
2011-10-25 20:40:07 -03:00
|
|
|
switch (_hal->getState()) {
|
2011-09-28 21:51:12 -03:00
|
|
|
|
2011-10-25 20:40:07 -03:00
|
|
|
case MAV_STATE_ACTIVE: {
|
|
|
|
digitalWrite(_hal->aLedPin, HIGH);
|
|
|
|
// turn all motors off if below 0.1 throttle
|
|
|
|
if (fabs(_hal->rc[ch_thrust]->getRadioPosition()) < 0.1) {
|
|
|
|
setAllRadioChannelsToNeutral();
|
|
|
|
} else {
|
|
|
|
_hal->rc[ch_left]->setPosition(_throttleOutput + _headingOutput);
|
|
|
|
_hal->rc[ch_right]->setPosition(_throttleOutput - _headingOutput);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case MAV_STATE_EMERGENCY: {
|
|
|
|
digitalWrite(_hal->aLedPin, LOW);
|
|
|
|
setAllRadioChannelsToNeutral();
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case MAV_STATE_STANDBY: {
|
|
|
|
digitalWrite(_hal->aLedPin,LOW);
|
|
|
|
setAllRadioChannelsToNeutral();
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
default: {
|
|
|
|
digitalWrite(_hal->aLedPin, LOW);
|
|
|
|
setAllRadioChannelsToNeutral();
|
|
|
|
}
|
2011-09-28 21:51:12 -03:00
|
|
|
|
|
|
|
}
|
|
|
|
}
|
2011-10-25 20:40:07 -03:00
|
|
|
|
|
|
|
private:
|
|
|
|
BlockPIDDfb pidStr;
|
|
|
|
BlockPID pidThr;
|
|
|
|
float _headingOutput;
|
|
|
|
float _throttleOutput;
|
2011-09-28 21:51:12 -03:00
|
|
|
};
|
|
|
|
|
|
|
|
} // namespace apo
|
|
|
|
|
|
|
|
#endif /* CONTROLLERTANK_H_ */
|