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


msp
Author(s): Christian Rauch
autogenerated on Mon Oct 9 2017 03:02:13