client_async_test.cpp
Go to the documentation of this file.
00001 #include <Client.hpp>
00002 #include <iostream>
00003 #include <msp_msg.hpp>
00004 
00005 struct SubCallbacks {
00006     void onIdent(const msp::msg::Ident& ident) { std::cout << ident; }
00007 
00008     void onStatus(const msp::msg::Status& status) { std::cout << status; }
00009 
00010     void onImu(const msp::msg::RawImu& imu) {
00011         std::cout << msp::msg::ImuSI(
00012             imu, 512.0, 1.0 / 4.096, 0.92f / 10.0f, 9.80665f);
00013     }
00014 
00015     void onServo(const msp::msg::Servo& servo) { std::cout << servo; }
00016 
00017     void onMotor(const msp::msg::Motor& motor) { std::cout << motor; }
00018 
00019     void onRc(const msp::msg::Rc& rc) { std::cout << rc; }
00020 
00021     void onAttitude(const msp::msg::Attitude& attitude) {
00022         std::cout << attitude;
00023     }
00024 
00025     void onAltitude(const msp::msg::Altitude& altitude) {
00026         std::cout << altitude;
00027     }
00028 
00029     void onAnalog(const msp::msg::Analog& analog) { std::cout << analog; }
00030 
00031     void onRcTuning(const msp::msg::RcTuning& rc_tuning) {
00032         std::cout << rc_tuning;
00033     }
00034 
00035     void onPID(const msp::msg::Pid& pid) { std::cout << pid; }
00036 
00037     void onBox(const msp::msg::ActiveBoxes& box) { std::cout << box; }
00038 
00039     void onMisc(const msp::msg::Misc& misc) { std::cout << misc; }
00040 
00041     void onMotorPins(const msp::msg::MotorPins& motor_pins) {
00042         std::cout << motor_pins;
00043     }
00044 
00045     void onBoxNames(const msp::msg::BoxNames& box_names) {
00046         std::cout << box_names;
00047     }
00048 
00049     void onPidNames(const msp::msg::PidNames& pid_names) {
00050         std::cout << pid_names;
00051     }
00052 
00053     void onBoxIds(const msp::msg::BoxIds& box_ids) { std::cout << box_ids; }
00054 
00055     void onServoConf(const msp::msg::ServoConf& servo_conf) {
00056         std::cout << servo_conf;
00057     }
00058 
00059     void onDebugMessage(const msp::msg::DebugMessage& debug_msg) {
00060         std::cout << "#Debug message:" << std::endl;
00061         std::cout << debug_msg.debug_msg << std::endl;
00062     }
00063 
00064     void onDebug(const msp::msg::Debug& debug) { std::cout << debug; }
00065 };
00066 
00067 static bool running = true;
00068 
00069 void onExit(int /*signal*/) { running = false; }
00070 
00071 int main(int argc, char* argv[]) {
00072     const std::string device =
00073         (argc > 1) ? std::string(argv[1]) : "/dev/ttyUSB0";
00074     const size_t baudrate = (argc > 2) ? std::stoul(argv[2]) : 115200;
00075 
00076     SubCallbacks subs;
00077 
00078     msp::client::Client client;
00079     client.start(device, baudrate);
00080 
00081     // using lambda callback with stored function object
00082     const std::function<void(const msp::msg::RawImu&)> imu_cb2 =
00083         [](const msp::msg::RawImu& imu) {
00084             std::cout << imu;
00085             std::cout << msp::msg::ImuSI(
00086                 imu, 512.0, 1.0 / 4.096, 0.92f / 10.0f, 9.80665f);
00087         };
00088     client.subscribe(imu_cb2, 0.1);
00089 
00090     client.subscribe(&SubCallbacks::onIdent, &subs, 10);
00091     client.subscribe(&SubCallbacks::onStatus, &subs, 1);
00092     client.subscribe(&SubCallbacks::onServo, &subs, 0.1);
00093     client.subscribe(&SubCallbacks::onMotor, &subs, 0.1);
00094     client.subscribe(&SubCallbacks::onRc, &subs, 0.1);
00095     client.subscribe(&SubCallbacks::onAttitude, &subs, 0.1);
00096     client.subscribe(&SubCallbacks::onAltitude, &subs, 0.1);
00097     client.subscribe(&SubCallbacks::onAnalog, &subs, 10);
00098     client.subscribe(&SubCallbacks::onRcTuning, &subs, 20);
00099     client.subscribe(&SubCallbacks::onPID, &subs, 20);
00100     client.subscribe(&SubCallbacks::onBox, &subs, 1);
00101     client.subscribe(&SubCallbacks::onMisc, &subs, 1);
00102     client.subscribe(&SubCallbacks::onMotorPins, &subs, 20);
00103     client.subscribe(&SubCallbacks::onBoxNames, &subs, 20);
00104     client.subscribe(&SubCallbacks::onPidNames, &subs, 20);
00105     client.subscribe(&SubCallbacks::onBoxIds, &subs, 20);
00106     client.subscribe(&SubCallbacks::onServoConf, &subs, 20);
00107     client.subscribe(&SubCallbacks::onDebugMessage, &subs, 1);
00108     client.subscribe(&SubCallbacks::onDebug, &subs, 1);
00109 
00110     // Ctrl+C to quit
00111     std::cin.get();
00112 
00113     client.stop();
00114 
00115     std::cout << "DONE" << std::endl;
00116 }


msp
Author(s): Christian Rauch
autogenerated on Thu Jun 20 2019 19:40:38