// %flair:license{ // This file is part of the Flair framework distributed under the // CECILL-C License, Version 1.0. // %flair:license} // created: 2016/09/02 // filename: GuiGcs.h // // authors: Thomas Fuhrmann // Copyright Heudiasyc UMR UTC/CNRS 7253 // // version: $Id: $ // // purpose: Implementation of GuiInterface, using sockets // // /*********************************************************************/ #include "GuiGcs.h" #include "GuiInterface.h" #include #include #include "MavlinkUDP.h" #include //todo remove for tests #include using namespace std; using namespace flair::core; using namespace flair::gui; GuiGcs::GuiGcs(const FrameworkManager *parent, string name, std::string &outputAddress, int outputPort): GuiInterface(parent, name) { cout << "MavPlanner GuiGcs constructor" << endl; //outputSocket = new Socket((Thread *)this, "output socket", outputAddress + ":" + to_string(outputPort)); mavCom = new MavlinkUDP(outputAddress, outputPort); if (mavCom) { // mavCom->startThreads(); } else { //TODO handle the error } } GuiGcs::~GuiGcs() { if (mavCom) { mavCom->stopThreads(); delete mavCom; } } void GuiGcs::MissionStart() { cout << "MavPlanner GuiGcs MissionStart" << endl; outputSocket->SendMessage("MavPlanner GuiGcs MissionStart"); } void GuiGcs::MissionStop() { cout << "MavPlanner GuiGcs MissionStop" << endl; outputSocket->SendMessage("MavPlanner GuiGcs MissionStop"); } void GuiGcs::MissionResume() { cout << "MavPlanner GuiGcs MissionResume" << endl; outputSocket->SendMessage("MavPlanner GuiGcs MissionResume"); } void GuiGcs::MissionSend() { cout << "MavPlanner GuiGcs MissionSend" << endl; // upload a flight plan cout << "---------------\n"; cout << "Flight planning \n"; cout << "---------------\n"; // get the list of commands to send std::vector missionCommandList = listMissionItems->GetItemList(); // reset all the previous waypoints mavCom->sendMissionClearAll(mavCom->target.getSysID(), mavCom->target.getCompID()); cout << "[INFO] Mission write partial list.\n"; // write partial list, takeoff + 4 waypoints + land mavCom->sendMissionWritePartialList(mavCom->target.getSysID(),mavCom->target.getCompID(), 0, missionCommandList.size()); mavCom->waitMissionAck(ACK_TIMEOUT); // sending mission items cout << "[INFO] Mission items.\n"; std::string delimiter = "|"; for (auto &missionCommand : missionCommandList) { cout << "[INFO] Mission item : " << missionCommand << "\n"; // Parse command std::string command = missionCommand.substr(0, missionCommand.find(delimiter)); missionCommand.erase(0, missionCommand.find(delimiter) + delimiter.length()); // Parse param1 float param1 = std::stof(missionCommand.substr(0, missionCommand.find(delimiter))); missionCommand.erase(0, missionCommand.find(delimiter) + delimiter.length()); // Parse param2 float param2 = std::stof(missionCommand.substr(0, missionCommand.find(delimiter))); missionCommand.erase(0, missionCommand.find(delimiter) + delimiter.length()); // Parse param3 float param3 = std::stof(missionCommand.substr(0, missionCommand.find(delimiter))); // cout << "[INFO] Mission item commands : " << command << ", " << param1 << ", " << param2 << ", " << param3 << std::endl; mavCom->sendMissionItem(mavCom->target.getSysID(), mavCom->target.getCompID(), 0, MAV_FRAME_GLOBAL_RELATIVE_ALT, MavCommandGet(command), 0, 1, 0, 0, 0, 0, param1, param2, param3); mavCom->waitMissionAck(ACK_TIMEOUT); } cout << "[INFO] Check mission item list.\n"; // mission request list (to check) mavCom->sendMissionRequestList(mavCom->target.getSysID(), mavCom->target.getCompID()); //mavCom.waitMissionCount(ACK_TIMEOUT); cout << "----------------\n"; cout << "Flight plan sent \n"; cout << "----------------\n"; } uint16_t GuiGcs::MavCommandGet(const std::string& command) const { uint16_t returnValue = -1; if (command == "WAYPOINT") { returnValue = MAV_CMD_NAV_WAYPOINT; } if (command == "TAKEOFF") { returnValue = MAV_CMD_NAV_TAKEOFF; } if (command == "LAND") { returnValue = MAV_CMD_NAV_LAND; } if (command == "RETURN") { returnValue = MAV_CMD_NAV_RETURN_TO_LAUNCH; } if (command == "JUMP") { returnValue = MAV_CMD_DO_JUMP; } return returnValue; }