diff --git a/CMakeLists.txt b/CMakeLists.txt index b22aeb7..83b28a5 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -129,6 +129,14 @@ if (BUILD_CPP_EXAMPLES) target_link_libraries(example_broadcast crazyflieLinkCpp ) + + # example_broadcast_and_console + add_executable(example_broadcast_and_console + examples/broadcast_and_console.cpp + ) + target_link_libraries(example_broadcast_and_console + crazyflieLinkCpp + ) endif() if (BUILD_PYTHON_BINDINGS) diff --git a/examples/broadcast_and_console.cpp b/examples/broadcast_and_console.cpp new file mode 100644 index 0000000..ea86eda --- /dev/null +++ b/examples/broadcast_and_console.cpp @@ -0,0 +1,63 @@ +#include +#include + +#include "crazyflieLinkCpp/Connection.h" +#include "PacketUtils.hpp" + +using namespace bitcraze::crazyflieLinkCpp; + +void run1() +{ + Connection con("radio://0/80/2M/E7E7E7E7E7"); + + const auto start = std::chrono::high_resolution_clock::now(); + while (true) { + Packet p = con.recv(0); + if (p.port() == 0 && p.channel() == 0) { + std::string str((const char*)p.payload(), (size_t)p.payloadSize()); + std::cout << str; + } + + const auto end = std::chrono::high_resolution_clock::now(); + const std::chrono::duration elapsed = end - start; + if (elapsed.count() > 5000) { + break; + } + } +} + +void run2() +{ + /* Creates a connection that will broadcast to all + * crazyflies on channel 80. Note that this requires + * updating the Crazyradio firmware from its factory version + */ + Connection broadcastConnection("radiobroadcast://*/80/2M"); + + // Requires high level commander enabled (param commander.enHighLevel == 1) + std::cout << "Taking off..." << std::endl; + broadcastConnection.send(PacketUtils::takeoffCommand(0.5f, 0.0f, 3.0f)); + std::this_thread::sleep_for(std::chrono::milliseconds(3250)); + + std::cout << "Landing..." << std::endl; + broadcastConnection.send(PacketUtils::landCommand(0.0f, 0.0f, 3.0f)); + std::this_thread::sleep_for(std::chrono::milliseconds(3250)); + + std::cout << "Stopping..." << std::endl; + broadcastConnection.send(PacketUtils::stopCommand()); + std::this_thread::sleep_for(std::chrono::milliseconds(500)); + + broadcastConnection.close(); + std::cout << "Done." << std::endl; +} + +int main() +{ + std::thread t1(run1); + std::thread t2(run2); + + t1.join(); + t2.join(); + + return 0; +} \ No newline at end of file