hil_state_level_interface.cpp
Go to the documentation of this file.
1 /*
2  * Copyright 2016 Pavel Vechersky, ASL, ETH Zurich, Switzerland
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9 
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
18 
19 namespace rotors_hil {
20 
21 HilStateLevelInterface::HilStateLevelInterface(const Eigen::Quaterniond& q_S_B) {
22  ros::NodeHandle pnh("~");
23 
24  // Retrieve the necessary parameters.
25  std::string air_speed_sub_topic;
26  std::string gps_sub_topic;
27  std::string ground_speed_sub_topic;
28  std::string imu_sub_topic;
29 
30  pnh.param("air_speed_topic", air_speed_sub_topic, std::string(mav_msgs::default_topics::AIR_SPEED));
31  pnh.param("gps_topic", gps_sub_topic, std::string(mav_msgs::default_topics::GPS));
32  pnh.param("ground_speed_topic", ground_speed_sub_topic, std::string(mav_msgs::default_topics::GROUND_SPEED));
33  pnh.param("imu_topic", imu_sub_topic, std::string(mav_msgs::default_topics::IMU));
34 
35  // Compute the rotation matrix to rotate data into NED frame
36  q_S_B_ = q_S_B;
37  R_S_B_ = q_S_B_.matrix().cast<float>();
38 
39  // Initialize the subscribers.
41  nh_.subscribe<geometry_msgs::TwistStamped>(
42  air_speed_sub_topic, 1, boost::bind(
44 
45  gps_sub_ =
46  nh_.subscribe<sensor_msgs::NavSatFix>(
47  gps_sub_topic, 1, boost::bind(
49 
51  nh_.subscribe<geometry_msgs::TwistStamped>(
52  ground_speed_sub_topic, 1, boost::bind(
54 
55  imu_sub_ =
56  nh_.subscribe<sensor_msgs::Imu>(
57  imu_sub_topic, 1, boost::bind(
59 }
60 
62 }
63 
64 std::vector<mavros_msgs::Mavlink> HilStateLevelInterface::CollectData() {
65  boost::mutex::scoped_lock lock(mtx_);
66 
67  ros::Time current_time = ros::Time::now();
68  uint64_t time_usec = RosTimeToMicroseconds(current_time);
69 
70  mavlink_message_t mmsg;
71  std::vector<mavros_msgs::Mavlink> hil_msgs;
72 
73  // Rotate the attitude into NED frame
74  Eigen::Quaterniond att = q_S_B_ * hil_data_.att;
75 
76  // Rotate gyroscope, accelerometer, and ground speed data into NED frame
77  Eigen::Vector3f gyro = R_S_B_ * hil_data_.gyro_rad_per_s;
78  Eigen::Vector3f acc = R_S_B_ * hil_data_.acc_m_per_s2;
79  Eigen::Vector3i gps_vel = (R_S_B_ * hil_data_.gps_vel_cm_per_s.cast<float>()).cast<int>();
80 
81  // Fill in a MAVLINK HIL_STATE_QUATERNION message and convert it to MAVROS format.
82  hil_state_qtrn_msg_.time_usec = time_usec;
83  hil_state_qtrn_msg_.attitude_quaternion[0] = att.w();
84  hil_state_qtrn_msg_.attitude_quaternion[1] = att.x();
85  hil_state_qtrn_msg_.attitude_quaternion[2] = att.y();
86  hil_state_qtrn_msg_.attitude_quaternion[3] = att.z();
87  hil_state_qtrn_msg_.rollspeed = gyro.x();
88  hil_state_qtrn_msg_.pitchspeed = gyro.y();
89  hil_state_qtrn_msg_.yawspeed = gyro.z();
93  hil_state_qtrn_msg_.vx = gps_vel.x();
94  hil_state_qtrn_msg_.vy = gps_vel.y();
95  hil_state_qtrn_msg_.vz = gps_vel.z();
101 
102  mavlink_hil_state_quaternion_t* hil_state_qtrn_msg_ptr = &hil_state_qtrn_msg_;
103  mavlink_msg_hil_state_quaternion_encode(1, 0, &mmsg, hil_state_qtrn_msg_ptr);
104 
105  mavros_msgs::MavlinkPtr rmsg_hil_state_qtrn = boost::make_shared<mavros_msgs::Mavlink>();
106  rmsg_hil_state_qtrn->header.stamp.sec = current_time.sec;
107  rmsg_hil_state_qtrn->header.stamp.nsec = current_time.nsec;
108  mavros_msgs::mavlink::convert(mmsg, *rmsg_hil_state_qtrn);
109 
110  hil_msgs.push_back(*rmsg_hil_state_qtrn);
111 
112  return hil_msgs;
113 }
114 
115 }
std::vector< mavros_msgs::Mavlink > CollectData()
Gather data collected from ROS messages into MAVLINK messages.
boost::mutex mtx_
Mutex lock for thread safety of reading hil data.
Definition: hil_interface.h:94
void GroundSpeedCallback(const geometry_msgs::TwistStampedConstPtr &ground_speed_msg, HilData *hil_data)
Callback for handling Ground Speed messages.
static constexpr char AIR_SPEED[]
Subscriber subscribe(const std::string &topic, uint32_t queue_size, void(T::*fp)(M), T *obj, const TransportHints &transport_hints=TransportHints())
ros::Subscriber ground_speed_sub_
ROS ground speed subscriber.
Definition: hil_interface.h:70
Eigen::Matrix3f R_S_B_
Rotation, in matrix form, from body into sensor (NED) frame.
Definition: hil_interface.h:85
static constexpr float kGravityMagnitude_m_per_s2
Definition: hil_listeners.h:32
Eigen::Vector3f acc_m_per_s2
Definition: hil_listeners.h:63
static constexpr float kMetersToMm
Definition: hil_listeners.h:46
HilData hil_data_
Object for storing the latest data.
Definition: hil_interface.h:88
ros::Subscriber imu_sub_
ROS IMU subscriber.
Definition: hil_interface.h:73
Eigen::Quaterniond att
Definition: hil_listeners.h:62
uint16_t true_airspeed_1e2m_per_s
Definition: hil_listeners.h:79
ros::NodeHandle nh_
ROS node handle.
Definition: hil_interface.h:61
uint16_t ind_airspeed_1e2m_per_s
Definition: hil_listeners.h:78
mavlink_hil_state_quaternion_t hil_state_qtrn_msg_
MAVLINK HIL_STATE_QUATERNION message.
bool param(const std::string &param_name, T &param_val, const T &default_val) const
HilStateLevelInterface(const Eigen::Quaterniond &q_S_B)
Constructor.
static constexpr char GPS[]
void AirSpeedCallback(const geometry_msgs::TwistStampedConstPtr &air_speed_msg, HilData *hil_data)
Callback for handling Air Speed messages.
Definition: hil_listeners.h:92
static constexpr char GROUND_SPEED[]
Eigen::Quaterniond q_S_B_
Rotation, in quaternion form, from body into sensor (NED) frame.
Definition: hil_interface.h:82
void GpsCallback(const sensor_msgs::NavSatFixConstPtr &gps_msg, HilData *hil_data)
Callback for handling GPS messages.
u_int64_t RosTimeToMicroseconds(const ros::Time &rostime)
Convert ros::Time into single value in microseconds.
Definition: hil_interface.h:43
static constexpr char IMU[]
ros::Subscriber air_speed_sub_
ROS air speed subscriber.
Definition: hil_interface.h:64
HilListeners hil_listeners_
Object with callbacks for receiving data.
Definition: hil_interface.h:91
static Time now()
Eigen::Vector3f gyro_rad_per_s
Definition: hil_listeners.h:64
Eigen::Vector3i gps_vel_cm_per_s
Definition: hil_listeners.h:66
ros::Subscriber gps_sub_
ROS GPS subscriber.
Definition: hil_interface.h:67
void ImuCallback(const sensor_msgs::ImuConstPtr &imu_msg, HilData *hil_data)
Callback for handling IMU messages.


rotors_hil_interface
Author(s): Pavel Vechersky
autogenerated on Mon Feb 28 2022 23:39:15