Go to the documentation of this file.00001 #ifndef FLIGHTCONTROLLER_HPP
00002 #define FLIGHTCONTROLLER_HPP
00003
00004 #include "Client.hpp"
00005 #include "FlightMode.hpp"
00006 #include "PeriodicTimer.hpp"
00007 #include "msp_msg.hpp"
00008
00009 namespace fcu {
00010
00011 enum class ControlSource { NONE, SBUS, MSP, OTHER };
00012
00013 class FlightController {
00014 public:
00018 FlightController();
00019
00023 ~FlightController();
00024
00033 bool connect(const std::string &device, const size_t baudrate = 115200,
00034 const double &timeout = 0.0, const bool print_info = false);
00035
00041 bool disconnect();
00042
00047 bool isConnected() const;
00048
00053 void setFlightMode(const FlightMode mode);
00054
00059 FlightMode getFlightMode() const;
00060
00066 void setControlSource(const ControlSource source);
00067
00072 ControlSource getControlSource();
00073
00081 void setRPYT(std::array<double, 4> &&rpyt);
00082
00087 void generateMSP();
00088
00092 bool saveSettings();
00093
00099 bool reboot();
00100
00106 msp::FirmwareVariant getFwVariant() const;
00107
00112 int getProtocolVersion() const;
00113
00118 std::string getBoardName() const;
00119
00125 void setLoggingLevel(const msp::client::LoggingLevel &level);
00126
00141 bool setRc(const uint16_t roll, const uint16_t pitch, const uint16_t yaw,
00142 const uint16_t throttle, const uint16_t aux1 = 1000,
00143 const uint16_t aux2 = 1000, const uint16_t aux3 = 1000,
00144 const uint16_t aux4 = 1000,
00145 const std::vector<uint16_t> auxs = std::vector<uint16_t>());
00146
00152 bool setRc(const std::vector<uint16_t> channels);
00153
00162 template <typename T, typename C,
00163 class = typename std::enable_if<
00164 std::is_base_of<msp::Message, T>::value>::type>
00165 std::shared_ptr<msp::client::SubscriptionBase> subscribe(
00166 void (C::*callback)(const T &), C *context, const double tp = 0.0) {
00167 return client_.subscribe(callback, context, tp);
00168 }
00169
00178 template <typename T, class = typename std::enable_if<
00179 std::is_base_of<msp::Message, T>::value>::type>
00180 std::shared_ptr<msp::client::SubscriptionBase> subscribe(
00181 const std::function<void(const T &)> &callback, const double tp = 0.0) {
00182 return client_.subscribe(callback, tp);
00183 }
00184
00190 bool hasSubscription(const msp::ID &id) const {
00191 return client_.hasSubscription(id);
00192 }
00193
00199 std::shared_ptr<msp::client::SubscriptionBase> getSubscription(
00200 const msp::ID &id) {
00201 return client_.getSubscription(id);
00202 }
00203
00211 bool sendMessage(msp::Message &message, const double timeout = 0) {
00212 return client_.sendMessage(message, timeout);
00213 }
00214
00218 void initBoxes();
00219
00224 const std::map<std::string, size_t> &getBoxNames() const {
00225 return box_name_ids_;
00226 }
00227
00233 bool hasCapability(const msp::msg::Capability &cap) const {
00234 return capabilities_.count(cap);
00235 }
00236
00241 bool hasBind() const { return hasCapability(msp::msg::Capability::BIND); }
00242
00247 bool hasDynBal() const {
00248 return hasCapability(msp::msg::Capability::DYNBAL);
00249 }
00250
00255 bool hasFlap() const { return hasCapability(msp::msg::Capability::FLAP); }
00256
00262 bool hasSensor(const msp::msg::Sensor &sensor) const {
00263 return sensors_.count(sensor);
00264 }
00265
00270 bool hasAccelerometer() const {
00271 return hasSensor(msp::msg::Sensor::Accelerometer);
00272 }
00273
00278 bool hasBarometer() const { return hasSensor(msp::msg::Sensor::Barometer); }
00279
00284 bool hasMagnetometer() const {
00285 return hasSensor(msp::msg::Sensor::Magnetometer);
00286 }
00287
00292 bool hasGPS() const { return hasSensor(msp::msg::Sensor::GPS); }
00293
00298 bool hasSonar() const { return hasSensor(msp::msg::Sensor::Sonar); }
00299
00304 bool isStatusActive(const std::string &status_name);
00305
00312 bool isArmed() { return isStatusActive("ARM"); }
00313
00319 bool isStatusFailsafe() { return isStatusActive("FAILSAFE"); }
00320
00325 bool setMotors(const std::array<uint16_t, msp::msg::N_MOTOR> &motor_values);
00326
00337 int updateFeatures(
00338 const std::set<std::string> &add = std::set<std::string>(),
00339 const std::set<std::string> &remove = std::set<std::string>());
00340
00341 private:
00342
00343 msp::client::Client client_;
00344
00345
00346 std::string board_name_;
00347 msp::FirmwareVariant fw_variant_;
00348 int msp_version_;
00349 std::map<std::string, size_t> box_name_ids_;
00350 std::set<msp::msg::Sensor> sensors_;
00351 std::array<uint8_t, msp::msg::MAX_MAPPABLE_RX_INPUTS> channel_map_;
00352 std::set<msp::msg::Capability> capabilities_;
00353
00354
00355 std::array<double, 4> rpyt_;
00356 FlightMode flight_mode_;
00357
00358 std::mutex msp_updates_mutex;
00359
00360 ControlSource control_source_;
00361
00362 msp::PeriodicTimer msp_timer_;
00363 };
00364
00365 }
00366
00367 #endif // FLIGHTCONTROLLER_HPP