mag_calibration_status.cpp
Go to the documentation of this file.
1 
11 #include <mavros/mavros_plugin.h>
12 #include <std_msgs/UInt8.h>
13 #include <mavros_msgs/MagnetometerReporter.h>
14 namespace mavros {
15 namespace std_plugins {
22 public:
24  mcs_nh("~mag_calibration")
25  { }
26 
30  void initialize(UAS &uas_)
31  {
33  mcs_pub = mcs_nh.advertise<std_msgs::UInt8>("status", 2, true);
34  mcr_pub = mcs_nh.advertise<mavros_msgs::MagnetometerReporter>("report", 2, true);
35  }
36 
46  return {
47  /* automatic message deduction by second argument */
50  };
51  }
52 
53 private:
57  std::array<bool, 8> calibration_show;
58  std::array<uint8_t, 8> _rg_compass_cal_progress;
59  //Send progress of magnetometer calibration
60  void handle_status(const mavlink::mavlink_message_t *, mavlink::ardupilotmega::msg::MAG_CAL_PROGRESS &mp) {
61  auto mcs = boost::make_shared<std_msgs::UInt8>();
62 
63  // How many compasses are we calibrating?
64  std::bitset<8> compass_calibrating = mp.cal_mask;
65 
66  if (compass_calibrating[mp.compass_id]) {
67  // Each compass gets a portion of the overall progress
68  if (mp.completion_pct < 95) {
69  calibration_show[mp.compass_id] = true;
70  }
71  _rg_compass_cal_progress[mp.compass_id] = mp.completion_pct;
72  }
73 
74  // Prevent data over 100% after cal_mask reset bit assigned to compass_id
75  uint16_t total_percentage = 0;
76  for (size_t i = 0; i < 8 && (compass_calibrating >> i).any(); i++) {
77  if (compass_calibrating[i]) {
78  total_percentage += static_cast<uint8_t>(_rg_compass_cal_progress[i]);
79  }
80  }
81 
82  mcs->data = total_percentage / compass_calibrating.count();
83 
84  mcs_pub.publish(mcs);
85  }
86 
87  //Send report after calibration is done
88  void handle_report(const mavlink::mavlink_message_t *, mavlink::common::msg::MAG_CAL_REPORT &mr) {
89  if (calibration_show[mr.compass_id]) {
90  auto mcr = boost::make_shared<mavros_msgs::MagnetometerReporter>();
91  mcr->header.stamp = ros::Time::now();
92  mcr->header.frame_id = std::to_string(mr.compass_id);
93  mcr->report = mr.cal_status;
94  mcr->confidence = mr.orientation_confidence;
95  mcr_pub.publish(mcr);
96  calibration_show[mr.compass_id] = false;
97  }
98  }
99 };
100 } // namespace std_plugins
101 } // namespace mavros
102 
HandlerInfo make_handler(const mavlink::msgid_t id, void(_C::*fn)(const mavlink::mavlink_message_t *msg, const mavconn::Framing framing))
std::string to_string(MAV_SENSOR_ORIENTATION orientation)
void publish(const boost::shared_ptr< M > &message) const
Publisher advertise(const std::string &topic, uint32_t queue_size, bool latch=false)
void handle_report(const mavlink::mavlink_message_t *, mavlink::common::msg::MAG_CAL_REPORT &mr)
void handle_status(const mavlink::mavlink_message_t *, mavlink::ardupilotmega::msg::MAG_CAL_PROGRESS &mp)
std::vector< HandlerInfo > Subscriptions
static Time now()
void initialize(UAS &uas_) override
#define PLUGINLIB_EXPORT_CLASS(class_type, base_class_type)


mavros_extras
Author(s): Vladimir Ermakov , Amilcar Lucas
autogenerated on Tue Jun 13 2023 02:17:59