Skip to content

Commit

Permalink
multiple_drones: some minor improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
julianoes committed Feb 20, 2019
1 parent decd541 commit dcc2e10
Showing 1 changed file with 13 additions and 18 deletions.
31 changes: 13 additions & 18 deletions example/multiple_drones/multiple_drones.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,28 +8,27 @@
#include <dronecode_sdk/dronecode_sdk.h>
#include <dronecode_sdk/plugins/action/action.h>
#include <dronecode_sdk/plugins/telemetry/telemetry.h>
#include <chrono>
#include <cstdint>
#include <iostream>
#include <thread>
#include <chrono>

using namespace dronecode_sdk;

using namespace std::this_thread;
using namespace std::chrono;

static void takeoff_land(System &system);
static void takeoff_and_land(System &system);

#define ERROR_CONSOLE_TEXT "\033[31m" // Turn text on console red
#define TELEMETRY_CONSOLE_TEXT "\033[34m" // Turn text on console blue
#define NORMAL_CONSOLE_TEXT "\033[0m" // Restore normal console colour

int main(int argc,
char **argv) // The input looks like this ./multiple_drones udp://:14540 udp://:14541
int main(int argc, char *argv[])
{
if (argc == 1) {
std::cout << ERROR_CONSOLE_TEXT << "Please specify connection" << NORMAL_CONSOLE_TEXT
std::cerr << ERROR_CONSOLE_TEXT << "Please specify connection" << NORMAL_CONSOLE_TEXT
<< std::endl;
;
return 1;
}

Expand All @@ -39,10 +38,9 @@ int main(int argc,
for (int i = 1; i < argc; ++i) {
ConnectionResult connection_result = dc.add_any_connection(argv[i]);
if (connection_result != ConnectionResult::SUCCESS) {
std::cout << ERROR_CONSOLE_TEXT
std::cerr << ERROR_CONSOLE_TEXT
<< "Connection error: " << connection_result_str(connection_result)
<< NORMAL_CONSOLE_TEXT << std::endl;
;
return 1;
}
}
Expand All @@ -60,7 +58,7 @@ int main(int argc,
sleep_for(seconds(2));

if (!discovered_system) {
std::cout << ERROR_CONSOLE_TEXT << "No system found, exiting." << NORMAL_CONSOLE_TEXT
std::cerr << ERROR_CONSOLE_TEXT << "No system found, exiting." << NORMAL_CONSOLE_TEXT
<< std::endl;
return 1;
}
Expand All @@ -69,7 +67,7 @@ int main(int argc,

for (auto uuid : dc.system_uuids()) {
System &system = dc.system(uuid);
std::thread t(&takeoff_land, std::ref(system));
std::thread t(&takeoff_and_land, std::ref(system));
threads.push_back(std::move(t));
}

Expand All @@ -79,7 +77,7 @@ int main(int argc,
return 0;
}

void takeoff_land(System &system)
void takeoff_and_land(System &system)
{
auto telemetry = std::make_shared<Telemetry>(system);
auto action = std::make_shared<Action>(system);
Expand All @@ -88,7 +86,7 @@ void takeoff_land(System &system)
const Telemetry::Result set_rate_result = telemetry->set_rate_position(1.0);

if (set_rate_result != Telemetry::Result::SUCCESS) {
std::cout << ERROR_CONSOLE_TEXT
std::cerr << ERROR_CONSOLE_TEXT
<< "Setting rate failed:" << Telemetry::result_str(set_rate_result)
<< NORMAL_CONSOLE_TEXT << std::endl;
return;
Expand All @@ -113,18 +111,16 @@ void takeoff_land(System &system)
const Action::Result arm_result = action->arm();

if (arm_result != Action::Result::SUCCESS) {
std::cout << ERROR_CONSOLE_TEXT << "Arming failed:" << Action::result_str(arm_result)
std::cerr << ERROR_CONSOLE_TEXT << "Arming failed:" << Action::result_str(arm_result)
<< NORMAL_CONSOLE_TEXT << std::endl;
// return ;
}

// Take off
std::cout << "Taking off..." << std::endl;
const Action::Result takeoff_result = action->takeoff();
if (takeoff_result != Action::Result::SUCCESS) {
std::cout << ERROR_CONSOLE_TEXT << "Takeoff failed:" << Action::result_str(takeoff_result)
std::cerr << ERROR_CONSOLE_TEXT << "Takeoff failed:" << Action::result_str(takeoff_result)
<< NORMAL_CONSOLE_TEXT << std::endl;
// return ;
}

// Let it hover for a bit before landing again.
Expand All @@ -133,9 +129,8 @@ void takeoff_land(System &system)
std::cout << "Landing..." << std::endl;
const Action::Result land_result = action->land();
if (land_result != Action::Result::SUCCESS) {
std::cout << ERROR_CONSOLE_TEXT << "Land failed:" << Action::result_str(land_result)
std::cerr << ERROR_CONSOLE_TEXT << "Land failed:" << Action::result_str(land_result)
<< NORMAL_CONSOLE_TEXT << std::endl;
// return ;
}

// Check if vehicle is still in air
Expand Down

0 comments on commit dcc2e10

Please sign in to comment.