ardupilot/ArduBoat/ControllerSailboat.h

105 lines
3.3 KiB
C
Raw Normal View History

2011-12-05 13:26:10 -04:00
/*
* ControllerSailboat.h
*
* Created on: Jun 30, 2011
* Author: jgoppert
*/
#ifndef CONTROLLERSAILBOAT_H_
#define CONTROLLERSAILBOAT_H_
#include "../APO/AP_Controller.h"
namespace apo {
class ControllerSailboat: public AP_Controller {
public:
ControllerSailboat(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_sail,ch_str,0.1,-0.9,0.9), ch_mode, k_cntrl),
2011-12-05 13:26:10 -04:00
pidStr(new AP_Var_group(k_pidStr, PSTR("STR_")), 1, steeringP,
steeringI, steeringD, steeringIMax, steeringYMax),
2011-12-07 20:17:29 -04:00
pidSail(new AP_Var_group(k_pidSail, PSTR("SAIL_")), 1, throttleP,
2011-12-05 13:26:10 -04:00
throttleI, throttleD, throttleIMax, throttleYMax,
2011-12-05 13:32:34 -04:00
throttleDFCut), _strCmd(0), _sailCmd(0)
2011-12-05 13:26:10 -04:00
{
2011-12-07 17:31:56 -04:00
_board->debug->println_P(PSTR("initializing sailboat controller"));
2011-12-05 13:26:10 -04: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-12-05 13:26:10 -04: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-12-05 13:26:10 -04:00
1900, RC_MODE_INOUT, false));
2011-12-07 17:31:56 -04:00
_board->rc.push_back(
2011-12-07 21:55:06 -04:00
new AP_RcChannel(k_chSail, PSTR("SAIL_"), board->radio, 2, 1100, 1500,
2011-12-05 13:26:10 -04:00
1900, RC_MODE_INOUT, false));
}
private:
// methods
void manualLoop(const float dt) {
2011-12-07 21:55:06 -04:00
_strCmd = -_board->rc[ch_str]->getRadioPosition();
_sailCmd = _board->rc[ch_sail]->getRadioPosition();
_board->debug->printf_P(PSTR("sail: %f, steering: %f\n"),_sailCmd,_strCmd);
2011-12-05 13:26:10 -04:00
}
void autoLoop(const float dt) {
2011-12-07 21:55:06 -04:00
//_board->debug->printf_P(PSTR("cont: ch1: %f\tch2: %f\n"),_board->rc[ch_sail]->getRadioPosition(), _board->rc[ch_str]->getRadioPosition());
2011-12-07 20:17:29 -04:00
float windDir = -.339373*analogRead(1)+175.999;
2011-12-07 21:55:06 -04:00
2011-12-05 13:26:10 -04:00
// neglects heading command derivative
2011-12-07 21:55:06 -04:00
float steering = -pidStr.update(_guide->getHeadingError(), -_nav->getYawRate(), dt);
2011-12-07 20:17:29 -04:00
float sail = 0.00587302*fabs(windDir) - 0.05;
if (sail < 0.0) sail = 0.0;
2011-12-07 21:55:06 -04:00
//_board->debug->printf_P(PSTR("heading: %f\n"),heading); //Print Heading
2011-12-07 20:17:29 -04:00
2011-12-07 21:55:06 -04:00
//if(fabs(psi)<45) //Tacking Logic
//{
//if(psi<-10)
//alpha = -45;
//else if(psi>10)
//alpha = 45;
//else
//{
//if(psi==10)
//alpha = 45;
//else if(psi==-10)
//alpha = -45;
//else
//alpha = alpha;
//}
//}*/
2011-12-05 13:26:10 -04:00
_strCmd = steering;
2011-12-05 13:32:34 -04:00
_sailCmd = sail;
2011-12-05 13:26:10 -04:00
}
void setMotors() {
2011-12-07 17:31:56 -04:00
_board->rc[ch_str]->setPosition(_strCmd);
2011-12-07 21:55:06 -04:00
_board->rc[ch_sail]->setPosition(_sailCmd);
2011-12-05 13:26:10 -04:00
}
void handleFailsafe() {
// turn off
setMode(MAV_MODE_LOCKED);
}
// attributes
enum {
2011-12-05 13:32:34 -04:00
ch_mode = 0, ch_str, ch_sail
2011-12-05 13:26:10 -04:00
};
enum {
2011-12-07 20:17:29 -04:00
k_chMode = k_radioChannelsStart, k_chStr, k_chSail
2011-12-05 13:26:10 -04:00
};
enum {
2011-12-07 20:17:29 -04:00
k_pidStr = k_controllersStart, k_pidSail
2011-12-05 13:26:10 -04:00
};
BlockPIDDfb pidStr;
2011-12-07 20:17:29 -04:00
BlockPID pidSail;
2011-12-05 13:32:34 -04:00
float _strCmd, _sailCmd;
2011-12-05 13:26:10 -04:00
};
} // namespace apo
#endif /* CONTROLLERSAILBOAT_H_ */
// vim:ts=4:sw=4:expandtab