client_async_test.cpp
Go to the documentation of this file.
1 #include <Client.hpp>
2 #include <iostream>
3 #include <msp_msg.hpp>
4 
5 struct SubCallbacks {
6  void onIdent(const msp::msg::Ident& ident) { std::cout << ident; }
7 
8  void onStatus(const msp::msg::Status& status) { std::cout << status; }
9 
10  void onImu(const msp::msg::RawImu& imu) {
11  std::cout << msp::msg::ImuSI(
12  imu, 512.0, 1.0 / 4.096, 0.92f / 10.0f, 9.80665f);
13  }
14 
15  void onServo(const msp::msg::Servo& servo) { std::cout << servo; }
16 
17  void onMotor(const msp::msg::Motor& motor) { std::cout << motor; }
18 
19  void onRc(const msp::msg::Rc& rc) { std::cout << rc; }
20 
21  void onAttitude(const msp::msg::Attitude& attitude) {
22  std::cout << attitude;
23  }
24 
25  void onAltitude(const msp::msg::Altitude& altitude) {
26  std::cout << altitude;
27  }
28 
29  void onAnalog(const msp::msg::Analog& analog) { std::cout << analog; }
30 
31  void onRcTuning(const msp::msg::RcTuning& rc_tuning) {
32  std::cout << rc_tuning;
33  }
34 
35  void onPID(const msp::msg::Pid& pid) { std::cout << pid; }
36 
37  void onBox(const msp::msg::ActiveBoxes& box) { std::cout << box; }
38 
39  void onMisc(const msp::msg::Misc& misc) { std::cout << misc; }
40 
41  void onMotorPins(const msp::msg::MotorPins& motor_pins) {
42  std::cout << motor_pins;
43  }
44 
45  void onBoxNames(const msp::msg::BoxNames& box_names) {
46  std::cout << box_names;
47  }
48 
49  void onPidNames(const msp::msg::PidNames& pid_names) {
50  std::cout << pid_names;
51  }
52 
53  void onBoxIds(const msp::msg::BoxIds& box_ids) { std::cout << box_ids; }
54 
55  void onServoConf(const msp::msg::ServoConf& servo_conf) {
56  std::cout << servo_conf;
57  }
58 
59  void onDebugMessage(const msp::msg::DebugMessage& debug_msg) {
60  std::cout << "#Debug message:" << std::endl;
61  std::cout << debug_msg.debug_msg << std::endl;
62  }
63 
64  void onDebug(const msp::msg::Debug& debug) { std::cout << debug; }
65 };
66 
67 static bool running = true;
68 
69 void onExit(int /*signal*/) { running = false; }
70 
71 int main(int argc, char* argv[]) {
72  const std::string device =
73  (argc > 1) ? std::string(argv[1]) : "/dev/ttyUSB0";
74  const size_t baudrate = (argc > 2) ? std::stoul(argv[2]) : 115200;
75 
76  SubCallbacks subs;
77 
78  msp::client::Client client;
79  client.start(device, baudrate);
80 
81  // using lambda callback with stored function object
82  const std::function<void(const msp::msg::RawImu&)> imu_cb2 =
83  [](const msp::msg::RawImu& imu) {
84  std::cout << imu;
85  std::cout << msp::msg::ImuSI(
86  imu, 512.0, 1.0 / 4.096, 0.92f / 10.0f, 9.80665f);
87  };
88  client.subscribe(imu_cb2, 0.1);
89 
90  client.subscribe(&SubCallbacks::onIdent, &subs, 10);
91  client.subscribe(&SubCallbacks::onStatus, &subs, 1);
92  client.subscribe(&SubCallbacks::onServo, &subs, 0.1);
93  client.subscribe(&SubCallbacks::onMotor, &subs, 0.1);
94  client.subscribe(&SubCallbacks::onRc, &subs, 0.1);
95  client.subscribe(&SubCallbacks::onAttitude, &subs, 0.1);
96  client.subscribe(&SubCallbacks::onAltitude, &subs, 0.1);
97  client.subscribe(&SubCallbacks::onAnalog, &subs, 10);
98  client.subscribe(&SubCallbacks::onRcTuning, &subs, 20);
99  client.subscribe(&SubCallbacks::onPID, &subs, 20);
100  client.subscribe(&SubCallbacks::onBox, &subs, 1);
101  client.subscribe(&SubCallbacks::onMisc, &subs, 1);
102  client.subscribe(&SubCallbacks::onMotorPins, &subs, 20);
103  client.subscribe(&SubCallbacks::onBoxNames, &subs, 20);
104  client.subscribe(&SubCallbacks::onPidNames, &subs, 20);
105  client.subscribe(&SubCallbacks::onBoxIds, &subs, 20);
106  client.subscribe(&SubCallbacks::onServoConf, &subs, 20);
107  client.subscribe(&SubCallbacks::onDebugMessage, &subs, 1);
108  client.subscribe(&SubCallbacks::onDebug, &subs, 1);
109 
110  // Ctrl+C to quit
111  std::cin.get();
112 
113  client.stop();
114 
115  std::cout << "DONE" << std::endl;
116 }
Value< std::string > debug_msg
Definition: msp_msg.hpp:4910
bool start(const std::string &device, const size_t baudrate=115200)
Start communications with a flight controller.
Definition: Client.cpp:34
void onServo(const msp::msg::Servo &servo)
void onBox(const msp::msg::ActiveBoxes &box)
void onAltitude(const msp::msg::Altitude &altitude)
void onRcTuning(const msp::msg::RcTuning &rc_tuning)
void onServoConf(const msp::msg::ServoConf &servo_conf)
void onStatus(const msp::msg::Status &status)
void onRc(const msp::msg::Rc &rc)
static bool running
void onPID(const msp::msg::Pid &pid)
void onMisc(const msp::msg::Misc &misc)
void onAttitude(const msp::msg::Attitude &attitude)
bool stop()
Stop communications with a flight controller.
Definition: Client.cpp:39
void onDebug(const msp::msg::Debug &debug)
void onDebugMessage(const msp::msg::DebugMessage &debug_msg)
void onMotor(const msp::msg::Motor &motor)
void onBoxIds(const msp::msg::BoxIds &box_ids)
void onImu(const msp::msg::RawImu &imu)
void onBoxNames(const msp::msg::BoxNames &box_names)
int main(int argc, char *argv[])
std::shared_ptr< SubscriptionBase > subscribe(void(C::*callback)(const T &), C *context, const double &tp)
Register callback function that is called when a message of matching ID is received.
Definition: Client.hpp:131
void onMotorPins(const msp::msg::MotorPins &motor_pins)
void onExit(int)
Requests (1xx)
Definition: msp_msg.hpp:2590
void onPidNames(const msp::msg::PidNames &pid_names)
void onAnalog(const msp::msg::Analog &analog)
void onIdent(const msp::msg::Ident &ident)


msp
Author(s): Christian Rauch
autogenerated on Tue Oct 6 2020 03:38:57