ardupilot/libraries/APO/AP_Autopilot.cpp

274 lines
8.9 KiB
C++
Raw Normal View History

2011-09-28 21:51:12 -03:00
/*
* AP_Autopilot.cpp
*
* Created on: Apr 30, 2011
* Author: jgoppert
*/
2011-10-26 15:59:40 -03:00
#include "../FastSerial/FastSerial.h"
2011-09-28 21:51:12 -03:00
#include "AP_Autopilot.h"
2011-10-26 15:59:40 -03:00
#include "../AP_GPS/AP_GPS.h"
#include "../APM_RC/APM_RC.h"
2011-12-07 17:31:56 -04:00
#include "AP_Board.h"
2011-10-26 15:59:40 -03:00
#include "AP_CommLink.h"
#include "AP_MavlinkCommand.h"
#include "AP_Navigator.h"
#include "AP_Controller.h"
#include "AP_Guide.h"
#include "AP_BatteryMonitor.h"
2011-09-28 21:51:12 -03:00
namespace apo {
2011-12-07 17:31:56 -04:00
class AP_Board;
2011-09-28 21:51:12 -03:00
AP_Autopilot::AP_Autopilot(AP_Navigator * navigator, AP_Guide * guide,
2011-12-07 17:31:56 -04:00
AP_Controller * controller, AP_Board * board,
2011-10-26 13:31:11 -03:00
float loopRate, float loop0Rate, float loop1Rate, float loop2Rate, float loop3Rate) :
Loop(loopRate, callback, this), _navigator(navigator), _guide(guide),
2011-12-07 17:31:56 -04:00
_controller(controller), _board(board),
2011-10-26 13:31:11 -03:00
callbackCalls(0) {
2011-12-07 17:31:56 -04:00
board->debug->printf_P(PSTR("initializing autopilot\n"));
board->debug->printf_P(PSTR("free ram: %d bytes\n"),freeMemory());
2011-10-26 13:31:11 -03:00
/*
2011-12-07 17:31:56 -04:00
* Comm links
2011-10-26 13:31:11 -03:00
*/
2011-12-07 17:31:56 -04:00
board->gcs = new MavlinkComm(board->gcsPort, navigator, guide, controller, board, 3);
if (board->getMode() != AP_Board::MODE_LIVE) {
board->hil = new MavlinkComm(board->hilPort, navigator, guide, controller, board, 3);
}
2011-12-07 20:19:30 -04:00
board->gcsPort->printf_P(PSTR("gcs hello\n"));
2011-12-07 17:31:56 -04:00
board->gcs->sendMessage(MAVLINK_MSG_ID_HEARTBEAT);
board->gcs->sendMessage(MAVLINK_MSG_ID_SYS_STATUS);
2011-10-26 13:31:11 -03:00
/*
* Calibration
*/
controller->setState(MAV_STATE_CALIBRATING);
2011-12-07 17:31:56 -04:00
board->gcs->sendMessage(MAVLINK_MSG_ID_HEARTBEAT);
board->gcs->sendMessage(MAVLINK_MSG_ID_SYS_STATUS);
2011-10-26 13:31:11 -03:00
if (navigator) navigator->calibrate();
/*
* Look for valid initial state
*/
while (_navigator) {
2011-12-06 19:56:16 -04:00
2011-10-26 13:31:11 -03:00
// letc gcs known we are alive
2011-12-07 17:31:56 -04:00
board->gcs->sendMessage(MAVLINK_MSG_ID_HEARTBEAT);
board->gcs->sendMessage(MAVLINK_MSG_ID_SYS_STATUS);
if (board->getMode() == AP_Board::MODE_LIVE) {
2011-10-26 13:31:11 -03:00
_navigator->updateSlow(0);
2011-12-07 17:31:56 -04:00
if (board->gps) {
if (board->gps->fix) {
2011-10-26 13:31:11 -03:00
break;
} else {
2011-12-07 17:31:56 -04:00
board->gps->update();
board->gcs->sendText(SEVERITY_LOW,
2011-10-26 13:31:11 -03:00
PSTR("waiting for gps lock\n"));
2011-12-07 17:31:56 -04:00
board->debug->printf_P(PSTR("waiting for gps lock\n"));
2011-10-26 13:31:11 -03:00
}
} else { // no gps, can skip
break;
}
2011-12-07 17:31:56 -04:00
} else if (board->getMode() == AP_Board::MODE_HIL_CNTL) { // hil
board->hil->sendMessage(MAVLINK_MSG_ID_HEARTBEAT);
board->hil->receive();
2011-10-26 13:31:11 -03:00
if (_navigator->getTimeStamp() != 0) {
// give hil a chance to send some packets
for (int i = 0; i < 5; i++) {
2011-12-07 17:31:56 -04:00
board->debug->println_P(PSTR("reading initial hil packets"));
board->gcs->sendText(SEVERITY_LOW, PSTR("reading initial hil packets"));
2011-10-26 13:31:11 -03:00
delay(1000);
}
break;
}
2011-12-07 17:31:56 -04:00
board->debug->println_P(PSTR("waiting for hil packet"));
board->gcs->sendText(SEVERITY_LOW, PSTR("waiting for hil packets"));
2011-10-26 13:31:11 -03:00
}
delay(500);
}
AP_MavlinkCommand::home.setAlt(_navigator->getAlt());
AP_MavlinkCommand::home.setLat(_navigator->getLat());
AP_MavlinkCommand::home.setLon(_navigator->getLon());
AP_MavlinkCommand::home.setCommand(MAV_CMD_NAV_WAYPOINT);
AP_MavlinkCommand::home.save();
2011-12-07 17:31:56 -04:00
_board->debug->printf_P(PSTR("\nhome before load lat: %f deg, lon: %f deg, cmd: %d\n"),
2011-10-26 13:31:11 -03:00
AP_MavlinkCommand::home.getLat()*rad2Deg,
AP_MavlinkCommand::home.getLon()*rad2Deg,
AP_MavlinkCommand::home.getCommand());
AP_MavlinkCommand::home.load();
2011-12-07 17:31:56 -04:00
_board->debug->printf_P(PSTR("\nhome after load lat: %f deg, lon: %f deg, cmd: %d\n"),
2011-10-26 13:31:11 -03:00
AP_MavlinkCommand::home.getLat()*rad2Deg,
AP_MavlinkCommand::home.getLon()*rad2Deg,
AP_MavlinkCommand::home.getCommand());
2011-12-06 19:56:16 -04:00
2011-10-27 20:42:57 -03:00
guide->setCurrentIndex(0);
controller->setMode(MAV_MODE_LOCKED);
controller->setState(MAV_STATE_STANDBY);
2011-12-06 19:56:16 -04:00
2011-10-26 13:31:11 -03:00
/*
* Attach loops, stacking for priority
*/
2011-12-07 17:31:56 -04:00
board->debug->println_P(PSTR("attaching loops"));
2011-10-26 13:31:11 -03:00
subLoops().push_back(new Loop(loop0Rate, callback0, this));
subLoops().push_back(new Loop(loop1Rate, callback1, this));
subLoops().push_back(new Loop(loop2Rate, callback2, this));
subLoops().push_back(new Loop(loop3Rate, callback3, this));
2011-12-07 17:31:56 -04:00
board->debug->println_P(PSTR("running"));
board->gcs->sendText(SEVERITY_LOW, PSTR("running"));
2011-09-28 21:51:12 -03:00
}
void AP_Autopilot::callback(void * data) {
2011-10-26 13:31:11 -03:00
AP_Autopilot * apo = (AP_Autopilot *) data;
2011-12-07 17:31:56 -04:00
//apo->getBoard()->debug->println_P(PSTR("callback"));
2011-10-26 13:31:11 -03:00
/*
* ahrs update
*/
apo->callbackCalls++;
if (apo->getNavigator())
apo->getNavigator()->updateFast(apo->dt());
2011-09-28 21:51:12 -03:00
}
void AP_Autopilot::callback0(void * data) {
2011-10-26 13:31:11 -03:00
AP_Autopilot * apo = (AP_Autopilot *) data;
2011-12-07 17:31:56 -04:00
//apo->getBoard()->debug->println_P(PSTR("callback 0"));
2011-10-26 13:31:11 -03:00
/*
* hardware in the loop
*/
2011-12-07 17:31:56 -04:00
if (apo->getBoard()->hil && apo->getBoard()->getMode() != AP_Board::MODE_LIVE) {
apo->getBoard()->hil->receive();
apo->getBoard()->hil->sendMessage(MAVLINK_MSG_ID_RC_CHANNELS_SCALED);
2011-10-26 13:31:11 -03:00
}
/*
* update control laws
*/
if (apo->getController()) {
2011-12-07 17:31:56 -04:00
//apo->getBoard()->debug->println_P(PSTR("updating controller"));
2011-10-26 13:31:11 -03:00
apo->getController()->update(apo->subLoops()[0]->dt());
}
/*
char msg[50];
sprintf(msg, "c_hdg: %f, c_thr: %f", apo->guide()->headingCommand, apo->guide()->groundSpeedCommand);
2011-12-07 17:31:56 -04:00
apo->board()->gcs->sendText(AP_CommLink::SEVERITY_LOW, msg);
2011-10-26 13:31:11 -03:00
*/
2011-09-28 21:51:12 -03:00
}
void AP_Autopilot::callback1(void * data) {
2011-10-26 13:31:11 -03:00
AP_Autopilot * apo = (AP_Autopilot *) data;
2011-12-07 17:31:56 -04:00
//apo->getBoard()->debug->println_P(PSTR("callback 1"));
2011-10-26 13:31:11 -03:00
/*
* update guidance laws
*/
if (apo->getGuide())
{
2011-12-07 17:31:56 -04:00
//apo->getBoard()->debug->println_P(PSTR("updating guide"));
2011-10-26 13:31:11 -03:00
apo->getGuide()->update();
}
/*
* slow navigation loop update
*/
if (apo->getNavigator()) {
apo->getNavigator()->updateSlow(apo->subLoops()[1]->dt());
}
/*
* send telemetry
*/
2011-12-07 17:31:56 -04:00
if (apo->getBoard()->gcs) {
apo->getBoard()->gcs->sendMessage(MAVLINK_MSG_ID_ATTITUDE);
apo->getBoard()->gcs->sendMessage(MAVLINK_MSG_ID_GLOBAL_POSITION);
2011-10-26 13:31:11 -03:00
}
/*
* handle ground control station communication
*/
2011-12-07 17:31:56 -04:00
if (apo->getBoard()->gcs) {
2011-10-26 13:31:11 -03:00
// send messages
2011-12-07 17:31:56 -04:00
apo->getBoard()->gcs->requestCmds();
apo->getBoard()->gcs->sendParameters();
2011-10-26 13:31:11 -03:00
// receive messages
2011-12-07 17:31:56 -04:00
apo->getBoard()->gcs->receive();
2011-10-26 13:31:11 -03:00
}
/*
* navigator debug
*/
/*
if (apo->navigator()) {
2011-12-07 17:31:56 -04:00
apo->getBoard()->debug->printf_P(PSTR("roll: %f deg\tpitch: %f deg\tyaw: %f deg\n"),
2011-10-26 13:31:11 -03:00
apo->navigator()->getRoll()*rad2Deg,
apo->navigator()->getPitch()*rad2Deg,
apo->navigator()->getYaw()*rad2Deg);
2011-12-07 17:31:56 -04:00
apo->getBoard()->debug->printf_P(PSTR("lat: %f deg\tlon: %f deg\talt: %f m\n"),
2011-10-26 13:31:11 -03:00
apo->navigator()->getLat()*rad2Deg,
apo->navigator()->getLon()*rad2Deg,
apo->navigator()->getAlt());
}
*/
2011-09-28 21:51:12 -03:00
}
void AP_Autopilot::callback2(void * data) {
2011-10-26 13:31:11 -03:00
AP_Autopilot * apo = (AP_Autopilot *) data;
2011-12-07 17:31:56 -04:00
//apo->getBoard()->debug->println_P(PSTR("callback 2"));
2011-10-26 13:31:11 -03:00
/*
* send telemetry
*/
2011-12-07 17:31:56 -04:00
if (apo->getBoard()->gcs) {
2011-10-26 13:31:11 -03:00
// send messages
2011-12-07 17:31:56 -04:00
//apo->getBoard()->gcs->sendMessage(MAVLINK_MSG_ID_GPS_RAW_INT);
//apo->getBoard()->gcs->sendMessage(MAVLINK_MSG_ID_LOCAL_POSITION);
apo->getBoard()->gcs->sendMessage(MAVLINK_MSG_ID_RC_CHANNELS_SCALED);
apo->getBoard()->gcs->sendMessage(MAVLINK_MSG_ID_RC_CHANNELS_RAW);
apo->getBoard()->gcs->sendMessage(MAVLINK_MSG_ID_SCALED_IMU);
2011-10-26 13:31:11 -03:00
}
/*
* update battery monitor
*/
2011-12-07 17:31:56 -04:00
if (apo->getBoard()->batteryMonitor) apo->getBoard()->batteryMonitor->update();
2011-10-26 13:31:11 -03:00
/*
* send heartbeat
*/
2011-12-07 17:31:56 -04:00
apo->getBoard()->gcs->sendMessage(MAVLINK_MSG_ID_HEARTBEAT);
2011-10-26 13:31:11 -03:00
/*
* load/loop rate/ram debug
*/
2011-12-07 17:31:56 -04:00
apo->getBoard()->load = apo->load();
apo->getBoard()->debug->printf_P(PSTR("callback calls: %d\n"),apo->callbackCalls);
2011-10-26 13:31:11 -03:00
apo->callbackCalls = 0;
2011-12-07 17:31:56 -04:00
apo->getBoard()->debug->printf_P(PSTR("load: %d%%\trate: %f Hz\tfree ram: %d bytes\n"),
2011-10-26 13:31:11 -03:00
apo->load(),1.0/apo->dt(),freeMemory());
2011-12-07 17:31:56 -04:00
apo->getBoard()->gcs->sendMessage(MAVLINK_MSG_ID_SYS_STATUS);
2011-10-26 13:31:11 -03:00
/*
* adc debug
*/
//apo->getDebug().printf_P(PSTR("adc: %d %d %d %d %d %d %d %d\n"),
//apo->adc()->Ch(0), apo->adc()->Ch(1), apo->adc()->Ch(2),
//apo->adc()->Ch(3), apo->adc()->Ch(4), apo->adc()->Ch(5),
//apo->adc()->Ch(6), apo->adc()->Ch(7), apo->adc()->Ch(8));
2011-09-28 21:51:12 -03:00
}
void AP_Autopilot::callback3(void * data) {
2011-10-26 13:31:11 -03:00
//AP_Autopilot * apo = (AP_Autopilot *) data;
2011-12-07 17:31:56 -04:00
//apo->getBoard()->debug->println_P(PSTR("callback 3"));
2011-09-28 21:51:12 -03:00
}
} // apo
2011-10-26 15:59:40 -03:00
// vim:ts=4:sw=4:expandtab