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"
#include "AP_HardwareAbstractionLayer.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-10-26 13:31:11 -03:00
AP_HardwareAbstractionLayer * hal, AP_ArmingMechanism * armingMechanism,
const uint8_t chMode, const uint16_t key, const prog_char_t * name) :
_nav(nav), _guide(guide), _hal(hal), _armingMechanism(armingMechanism),
_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-10-26 13:31:11 -03:00
for (uint8_t i = 0; i < _hal->rc.getSize(); i++) {
_hal->rc[i]->setPosition(0.0);
}
}
void AP_Controller::setAllRadioChannelsManually() {
//_hal->debug->printf_P(PSTR("\tsize: %d\n"),_hal->rc.getSize());
2011-10-26 13:31:11 -03:00
for (uint8_t i = 0; i < _hal->rc.getSize(); i++) {
_hal->rc[i]->setUsingRadio();
//_hal->debug->printf_P(PSTR("\trc %d: %f\n"),i,_hal->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);
_hal->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);
_hal->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-11-29 14:59:44 -04:00
if (_hal->gcs->heartBeatLost()) {
2011-11-28 00:55:28 -04:00
setMode(MAV_MODE_FAILSAFE);
_hal->gcs->sendText(SEVERITY_HIGH, PSTR("configure gcs to send heartbeat"));
// if battery less than 5%, go to failsafe
} else if (_hal->batteryMonitor && _hal->batteryMonitor->getPercentage() < 5) {
setMode(MAV_MODE_FAILSAFE);
_hal->gcs->sendText(SEVERITY_HIGH, PSTR("recharge battery"));
}
// if in auto mode and manual switch set, change to manual
if (_hal->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 {
_hal->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-10-26 14:25:06 -03:00
digitalWrite(_hal->aLedPin, HIGH);
setMotors();
} else {
2011-10-26 14:25:06 -03:00
digitalWrite(_hal->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