gps_rtk.cpp
Go to the documentation of this file.
1 
9 /*
10  * Copyright 2018 Alexis Paques.
11  *
12  * This file is part of the mavros package and subject to the license terms
13  * in the top-level LICENSE file of the mavros repository.
14  * https://github.com/mavlink/mavros/tree/master/LICENSE.md
15  */
16 
17 #include <mavros/mavros_plugin.h>
18 #include <mavros_msgs/RTCM.h>
19 #include <mavros_msgs/RTKBaseline.h>
20 #include <algorithm>
21 
22 namespace mavros {
23 namespace extra_plugins {
31 public:
33  gps_rtk_nh("~gps_rtk")
34  { }
35 
36  void initialize(UAS &uas_) override
37  {
39  gps_rtk_sub = gps_rtk_nh.subscribe("send_rtcm", 10, &GpsRtkPlugin::rtcm_cb, this);
40  rtk_baseline_pub_ = gps_rtk_nh.advertise<mavros_msgs::RTKBaseline>("rtk_baseline", 1, true);
41  }
42 
44  {
45  return {
47  };
48  }
49 
50 private:
53 
55  mavros_msgs::RTKBaseline rtk_baseline_;
56 
57  /* -*- callbacks -*- */
65  {
66  mavlink::common::msg::GPS_RTCM_DATA rtcm_data = {};
67  const size_t max_frag_len = rtcm_data.data.size();
68 
69  uint8_t seq_u5 = uint8_t(msg->header.seq & 0x1F) << 3;
70 
71  if (msg->data.size() > 4 * max_frag_len) {
72  ROS_FATAL("gps_rtk: RTCM message received is bigger than the maximal possible size.");
73  return;
74  }
75 
76  auto data_it = msg->data.begin();
77  auto end_it = msg->data.end();
78 
79  if (msg->data.size() <= max_frag_len) {
80  rtcm_data.len = msg->data.size();
81  rtcm_data.flags = seq_u5;
82  std::copy(data_it, end_it, rtcm_data.data.begin());
83  std::fill(rtcm_data.data.begin() + rtcm_data.len, rtcm_data.data.end(), 0);
84  UAS_FCU(m_uas)->send_message(rtcm_data);
85  } else {
86  for (uint8_t fragment_id = 0; fragment_id < 4 && data_it < end_it; fragment_id++) {
87  uint8_t len = std::min((size_t) std::distance(data_it, end_it), max_frag_len);
88  rtcm_data.flags = 1; // LSB set indicates message is fragmented
89  rtcm_data.flags |= fragment_id << 1; // Next 2 bits are fragment id
90  rtcm_data.flags |= seq_u5; // Next 5 bits are sequence id
91  rtcm_data.len = len;
92 
93  std::copy(data_it, data_it + len, rtcm_data.data.begin());
94  std::fill(rtcm_data.data.begin() + len, rtcm_data.data.end(), 0);
95  UAS_FCU(m_uas)->send_message(rtcm_data);
96  std::advance(data_it, len);
97  }
98  }
99  }
100 
101  /* MAvlink msg handlers */
108  void handle_baseline_msg( const mavlink::mavlink_message_t *msg, mavlink::common::msg::GPS_RTK &rtk_bsln )
109  {
110  /* Received a decoded packet containing mavlink's msg #127,#128 in Common.
111  Simply convert to ROS and publish.
112  */
113  rtk_baseline_.time_last_baseline_ms = rtk_bsln.time_last_baseline_ms;
114  rtk_baseline_.rtk_receiver_id = rtk_bsln.rtk_receiver_id;
115  rtk_baseline_.wn = rtk_bsln.wn; // week num.
116  rtk_baseline_.tow = rtk_bsln.tow; // ms
117  rtk_baseline_.rtk_health = rtk_bsln.rtk_health;
118  rtk_baseline_.rtk_rate = rtk_bsln.rtk_rate;
119  rtk_baseline_.nsats = rtk_bsln.nsats;
120  rtk_baseline_.baseline_coords_type = rtk_bsln.baseline_coords_type; // 0: ECEF, 1: NED
121  rtk_baseline_.baseline_a_mm = rtk_bsln.baseline_a_mm;
122  rtk_baseline_.baseline_b_mm = rtk_bsln.baseline_b_mm;
123  rtk_baseline_.baseline_c_mm = rtk_bsln.baseline_c_mm;
124  rtk_baseline_.accuracy = rtk_bsln.accuracy;
125  rtk_baseline_.iar_num_hypotheses = rtk_bsln.iar_num_hypotheses;
126 
127  rtk_baseline_.header.stamp = ros::Time::now();
128  rtk_baseline_pub_.publish( rtk_baseline_ );
129  }
130 };
131 } // namespace extra_plugins
132 } // namespace mavros
133 
std::shared_ptr< MAVConnInterface const > ConstPtr
#define ROS_FATAL(...)
void handle_baseline_msg(const mavlink::mavlink_message_t *msg, mavlink::common::msg::GPS_RTK &rtk_bsln)
Publish GPS_RTK message (MAvlink Common) received from FCU. The message is already decoded by Mavlink...
Definition: gps_rtk.cpp:108
void publish(const boost::shared_ptr< M > &message) const
mavros_msgs::RTKBaseline rtk_baseline_
Definition: gps_rtk.cpp:55
Subscriber subscribe(const std::string &topic, uint32_t queue_size, void(T::*fp)(M), T *obj, const TransportHints &transport_hints=TransportHints())
HandlerInfo make_handler(const mavlink::msgid_t id, void(_C::*fn)(const mavlink::mavlink_message_t *msg, const mavconn::Framing framing))
ssize_t len
void rtcm_cb(const mavros_msgs::RTCM::ConstPtr &msg)
Handle mavros_msgs::RTCM message It converts the message to the MAVLink GPS_RTCM_DATA message for GPS...
Definition: gps_rtk.cpp:64
#define UAS_FCU(uasobjptr)
Publisher advertise(const std::string &topic, uint32_t queue_size, bool latch=false)
Subscriptions get_subscriptions() override
Definition: gps_rtk.cpp:43
void initialize(UAS &uas_) override
Definition: gps_rtk.cpp:36
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 1 2021 02:36:36