ardupilot/libraries/APO/AP_Controller.cpp

107 lines
3.4 KiB
C++
Raw Normal View History

2011-09-28 21:51:12 -03:00
/*
* AP_Controller.cpp
*
* Created on: Apr 30, 2011
* Author: jgoppert
*/
#include "../FastSerial/FastSerial.h"
#include "AP_ArmingMechanism.h"
#include "AP_BatteryMonitor.h"
2011-12-07 17:31:56 -04:00
#include "AP_Board.h"
#include "AP_RcChannel.h"
#include "../GCS_MAVLink/include/mavlink_types.h"
#include "constants.h"
#include "AP_CommLink.h"
#include "AP_Controller.h"
namespace apo {
AP_Controller::AP_Controller(AP_Navigator * nav, AP_Guide * guide,
2011-12-07 17:31:56 -04:00
AP_Board * board, AP_ArmingMechanism * armingMechanism,
2011-10-26 13:31:11 -03:00
const uint8_t chMode, const uint16_t key, const prog_char_t * name) :
2011-12-07 17:31:56 -04:00
_nav(nav), _guide(guide), _board(board), _armingMechanism(armingMechanism),
2011-10-26 13:31:11 -03:00
_group(key, name ? : PSTR("CNTRL_")),
_chMode(chMode),
_mode(MAV_MODE_LOCKED), _state(MAV_STATE_BOOT) {
2011-10-26 13:31:11 -03:00
setAllRadioChannelsToNeutral();
}
void AP_Controller::setAllRadioChannelsToNeutral() {
2011-12-07 17:31:56 -04:00
for (uint8_t i = 0; i < _board->rc.getSize(); i++) {
_board->rc[i]->setPosition(0.0);
2011-10-26 13:31:11 -03:00
}
}
void AP_Controller::setAllRadioChannelsManually() {
2011-12-07 17:31:56 -04:00
//_board->debug->printf_P(PSTR("\tsize: %d\n"),_board->rc.getSize());
for (uint8_t i = 0; i < _board->rc.getSize(); i++) {
_board->rc[i]->setUsingRadio();
//_board->debug->printf_P(PSTR("\trc %d: %f\n"),i,_board->rc[i]->getPosition());
2011-10-26 13:31:11 -03:00
}
}
void AP_Controller::update(const float dt) {
2011-10-26 13:31:11 -03:00
if (_armingMechanism) _armingMechanism->update(dt);
// handle modes
//
// if in locked mode
if (getMode() == MAV_MODE_LOCKED) {
// if state is not stanby then set it to standby and alert gcs
if (getState()!=MAV_STATE_STANDBY) {
setState(MAV_STATE_STANDBY);
2011-12-07 17:31:56 -04:00
_board->gcs->sendText(SEVERITY_HIGH, PSTR("disarmed"));
}
2011-10-26 13:31:11 -03:00
}
// if not locked
else {
2011-11-28 00:55:28 -04:00
// if state is not active, set it to active and alert gcs
if (getState()!=MAV_STATE_ACTIVE) {
setState(MAV_STATE_ACTIVE);
2011-12-07 17:31:56 -04:00
_board->gcs->sendText(SEVERITY_HIGH, PSTR("armed"));
}
2011-11-28 00:55:28 -04:00
// handle emergencies
//
// check for heartbeat from gcs, if not found go to failsafe
2011-12-07 17:31:56 -04:00
if (_board->gcs->heartBeatLost()) {
2011-11-28 00:55:28 -04:00
setMode(MAV_MODE_FAILSAFE);
2011-12-07 17:31:56 -04:00
_board->gcs->sendText(SEVERITY_HIGH, PSTR("configure gcs to send heartbeat"));
2011-11-28 00:55:28 -04:00
// if battery less than 5%, go to failsafe
2011-12-07 17:31:56 -04:00
} else if (_board->batteryMonitor && _board->batteryMonitor->getPercentage() < 5) {
2011-11-28 00:55:28 -04:00
setMode(MAV_MODE_FAILSAFE);
2011-12-07 17:31:56 -04:00
_board->gcs->sendText(SEVERITY_HIGH, PSTR("recharge battery"));
2011-11-28 00:55:28 -04:00
}
// if in auto mode and manual switch set, change to manual
2011-12-07 17:31:56 -04:00
if (_board->rc[_chMode]->getRadioPosition() > 0) setMode(MAV_MODE_MANUAL);
else setMode(MAV_MODE_AUTO);
// handle all possible modes
if (getMode()==MAV_MODE_MANUAL) {
manualLoop(dt);
} else if (getMode()==MAV_MODE_AUTO) {
autoLoop(dt);
2011-11-28 00:55:28 -04:00
} else if (getMode()==MAV_MODE_FAILSAFE) {
handleFailsafe();
} else {
2011-12-07 17:31:56 -04:00
_board->gcs->sendText(SEVERITY_HIGH, PSTR("unknown mode"));
setMode(MAV_MODE_FAILSAFE);
}
2011-10-26 13:31:11 -03:00
}
2011-10-26 13:31:11 -03:00
// this sends commands to motors
if(getState()==MAV_STATE_ACTIVE) {
2011-12-07 17:31:56 -04:00
digitalWrite(_board->aLedPin, HIGH);
setMotors();
} else {
2011-12-07 17:31:56 -04:00
digitalWrite(_board->aLedPin, LOW);
setAllRadioChannelsToNeutral();
2011-10-26 14:25:06 -03:00
}
}
} // namespace apo
2011-10-26 15:59:40 -03:00
// vim:ts=4:sw=4:expandtab