2011-09-28 21:51:12 -03:00
|
|
|
/*
|
|
|
|
* ControllerCar.h
|
|
|
|
*
|
|
|
|
* Created on: Jun 30, 2011
|
|
|
|
* Author: jgoppert
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef CONTROLLERCAR_H_
|
|
|
|
#define CONTROLLERCAR_H_
|
|
|
|
|
|
|
|
#include "../APO/AP_Controller.h"
|
|
|
|
|
|
|
|
namespace apo {
|
|
|
|
|
|
|
|
class ControllerCar: public AP_Controller {
|
2011-10-25 20:40:07 -03:00
|
|
|
public:
|
2011-10-26 14:25:06 -03:00
|
|
|
ControllerCar(AP_Navigator * nav, AP_Guide * guide,
|
2011-12-07 17:31:56 -04:00
|
|
|
AP_Board * board) :
|
|
|
|
AP_Controller(nav, guide, board,new AP_ArmingMechanism(board,this,ch_thrust,ch_str,0.1,-0.9,0.9), ch_mode, k_cntrl),
|
2011-10-26 14:25:06 -03:00
|
|
|
pidStr(new AP_Var_group(k_pidStr, PSTR("STR_")), 1, steeringP,
|
2011-12-03 13:13:11 -04:00
|
|
|
steeringI, steeringD, steeringIMax, steeringYMax),
|
2011-10-26 14:25:06 -03:00
|
|
|
pidThrust(new AP_Var_group(k_pidThrust, PSTR("THR_")), 1, throttleP,
|
|
|
|
throttleI, throttleD, throttleIMax, throttleYMax,
|
2011-11-01 15:09:57 -03:00
|
|
|
throttleDFCut), _strCmd(0), _thrustCmd(0),
|
|
|
|
_rangeFinderFront()
|
2011-10-26 14:25:06 -03:00
|
|
|
{
|
2011-12-07 17:31:56 -04:00
|
|
|
_board->debug->println_P(PSTR("initializing car controller"));
|
2011-10-26 14:25:06 -03:00
|
|
|
|
2011-12-07 17:31:56 -04:00
|
|
|
_board->rc.push_back(
|
|
|
|
new AP_RcChannel(k_chMode, PSTR("MODE_"), board->radio, 5, 1100,
|
2011-10-26 14:25:06 -03:00
|
|
|
1500, 1900, RC_MODE_IN, false));
|
2011-12-07 17:31:56 -04:00
|
|
|
_board->rc.push_back(
|
|
|
|
new AP_RcChannel(k_chStr, PSTR("STR_"), board->radio, 3, 1100, 1500,
|
2011-10-26 14:25:06 -03:00
|
|
|
1900, RC_MODE_INOUT, false));
|
2011-12-07 17:31:56 -04:00
|
|
|
_board->rc.push_back(
|
|
|
|
new AP_RcChannel(k_chThrust, PSTR("THR_"), board->radio, 2, 1100, 1500,
|
2011-10-26 14:25:06 -03:00
|
|
|
1900, RC_MODE_INOUT, false));
|
2011-12-07 17:31:56 -04:00
|
|
|
_board->rc.push_back(
|
|
|
|
new AP_RcChannel(k_chFwdRev, PSTR("FWDREV_"), board->radio, 4, 1100, 1500,
|
2011-11-01 15:09:57 -03:00
|
|
|
1900, RC_MODE_IN, false));
|
|
|
|
|
2011-12-07 17:31:56 -04:00
|
|
|
for (uint8_t i = 0; i < _board->rangeFinders.getSize(); i++) {
|
|
|
|
RangeFinder * rF = _board->rangeFinders[i];
|
2011-11-01 15:09:57 -03:00
|
|
|
if (rF == NULL)
|
|
|
|
continue;
|
|
|
|
if (rF->orientation_x == 1 && rF->orientation_y == 0
|
|
|
|
&& rF->orientation_z == 0)
|
|
|
|
_rangeFinderFront = rF;
|
|
|
|
}
|
2011-10-26 14:25:06 -03:00
|
|
|
}
|
2011-10-25 20:40:07 -03:00
|
|
|
|
|
|
|
private:
|
2011-10-26 14:25:06 -03:00
|
|
|
// methods
|
|
|
|
void manualLoop(const float dt) {
|
2011-12-07 17:31:56 -04:00
|
|
|
_strCmd = _board->rc[ch_str]->getRadioPosition();
|
|
|
|
_thrustCmd = _board->rc[ch_thrust]->getRadioPosition();
|
|
|
|
if (useForwardReverseSwitch && _board->rc[ch_FwdRev]->getRadioPosition() < 0.0)
|
2011-11-01 15:09:57 -03:00
|
|
|
_thrustCmd = -_thrustCmd;
|
2011-10-26 14:25:06 -03:00
|
|
|
}
|
|
|
|
void autoLoop(const float dt) {
|
2011-12-07 17:31:56 -04:00
|
|
|
//_board->debug->printf_P(PSTR("cont: ch1: %f\tch2: %f\n"),_board->rc[ch_thrust]->getRadioPosition(), _board->rc[ch_str]->getRadioPosition());
|
2011-12-03 13:13:11 -04:00
|
|
|
// neglects heading command derivative
|
|
|
|
float steering = pidStr.update(_guide->getHeadingError(), -_nav->getYawRate(), dt);
|
2011-11-01 15:09:57 -03:00
|
|
|
float thrust = pidThrust.update(
|
2011-10-26 14:25:06 -03:00
|
|
|
_guide->getGroundSpeedCommand()
|
|
|
|
- _nav->getGroundSpeed(), dt);
|
2011-11-01 15:09:57 -03:00
|
|
|
|
|
|
|
// obstacle avoidance overrides
|
|
|
|
// try to drive around the obstacle in front. if this fails, stop
|
|
|
|
if (_rangeFinderFront) {
|
|
|
|
_rangeFinderFront->read();
|
|
|
|
|
|
|
|
int distanceToObstacle = _rangeFinderFront->distance;
|
|
|
|
if (distanceToObstacle < 100) {
|
|
|
|
thrust = 0; // Avoidance didn't work out. Stopping
|
|
|
|
}
|
|
|
|
else if (distanceToObstacle < 650) {
|
|
|
|
// Deviating from the course. Deviation angle is inverse proportional
|
|
|
|
// to the distance to the obstacle, with 15 degrees min angle, 180 - max
|
|
|
|
steering += (15 + 165 *
|
|
|
|
(1 - ((float)(distanceToObstacle - 100)) / 550)) * deg2Rad;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
_strCmd = steering;
|
|
|
|
_thrustCmd = thrust;
|
2011-10-26 14:25:06 -03:00
|
|
|
}
|
2011-11-22 17:42:51 -04:00
|
|
|
void setMotors() {
|
2011-12-07 17:31:56 -04:00
|
|
|
_board->rc[ch_str]->setPosition(_strCmd);
|
|
|
|
_board->rc[ch_thrust]->setPosition(fabs(_thrustCmd) < 0.1 ? 0 : _thrustCmd);
|
2011-10-26 14:25:06 -03:00
|
|
|
}
|
2011-11-22 17:42:51 -04:00
|
|
|
void handleFailsafe() {
|
|
|
|
// turn off
|
|
|
|
setMode(MAV_MODE_LOCKED);
|
|
|
|
}
|
2011-10-26 14:25:06 -03:00
|
|
|
|
|
|
|
// attributes
|
|
|
|
enum {
|
2011-11-01 15:09:57 -03:00
|
|
|
ch_mode = 0, ch_str, ch_thrust, ch_FwdRev
|
2011-10-26 14:25:06 -03:00
|
|
|
};
|
|
|
|
enum {
|
2011-11-01 15:09:57 -03:00
|
|
|
k_chMode = k_radioChannelsStart, k_chStr, k_chThrust, k_chFwdRev
|
2011-10-26 14:25:06 -03:00
|
|
|
};
|
|
|
|
enum {
|
|
|
|
k_pidStr = k_controllersStart, k_pidThrust
|
|
|
|
};
|
|
|
|
BlockPIDDfb pidStr;
|
|
|
|
BlockPID pidThrust;
|
|
|
|
float _strCmd, _thrustCmd;
|
2011-11-01 15:09:57 -03:00
|
|
|
|
|
|
|
RangeFinder * _rangeFinderFront;
|
2011-09-28 21:51:12 -03:00
|
|
|
};
|
|
|
|
|
|
|
|
} // namespace apo
|
|
|
|
|
|
|
|
#endif /* CONTROLLERCAR_H_ */
|
2011-10-26 14:25:06 -03:00
|
|
|
// vim:ts=4:sw=4:expandtab
|