hil_interface_node.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 
22  rate_(kDefaultHilFrequency) {
23  ros::NodeHandle pnh("~");
24 
25  bool sensor_level_hil;
26  double hil_frequency;
27  double S_B_roll;
28  double S_B_pitch;
29  double S_B_yaw;
30  std::string actuators_pub_topic;
31  std::string mavlink_pub_topic;
32  std::string hil_controls_sub_topic;
33 
34  pnh.param("sensor_level_hil", sensor_level_hil, kDefaultSensorLevelHil);
35  pnh.param("hil_frequency", hil_frequency, kDefaultHilFrequency);
36  pnh.param("body_to_sensor_roll", S_B_roll, kDefaultBodyToSensorsRoll);
37  pnh.param("body_to_sensor_pitch", S_B_pitch, kDefaultBodyToSensorsPitch);
38  pnh.param("body_to_sensor_yaw", S_B_yaw, kDefaultBodyToSensorsYaw);
39  pnh.param("actuators_pub_topic", actuators_pub_topic, std::string(mav_msgs::default_topics::COMMAND_ACTUATORS));
40  pnh.param("mavlink_pub_topic", mavlink_pub_topic, kDefaultMavlinkPubTopic);
41  pnh.param("hil_controls_sub_topic", hil_controls_sub_topic, kDefaultHilControlsSubTopic);
42 
43  // Create the quaternion and rotation matrix to rotate data into NED frame.
44  Eigen::AngleAxisd roll_angle(S_B_roll, Eigen::Vector3d::UnitX());
45  Eigen::AngleAxisd pitch_angle(S_B_pitch, Eigen::Vector3d::UnitY());
46  Eigen::AngleAxisd yaw_angle(S_B_yaw, Eigen::Vector3d::UnitZ());
47 
48  const Eigen::Quaterniond q_S_B = roll_angle * pitch_angle * yaw_angle;
49 
50  if (sensor_level_hil)
51  hil_interface_ = std::auto_ptr<HilSensorLevelInterface>(new HilSensorLevelInterface(q_S_B));
52  else
53  hil_interface_ = std::auto_ptr<HilStateLevelInterface>(new HilStateLevelInterface(q_S_B));
54 
55  rate_ = ros::Rate(hil_frequency);
56 
57  actuators_pub_ = nh_.advertise<mav_msgs::Actuators>(actuators_pub_topic, 1);
58  mavlink_pub_ = nh_.advertise<mavros_msgs::Mavlink>(mavlink_pub_topic, 5);
59  hil_controls_sub_ = nh_.subscribe(hil_controls_sub_topic, 1,
61 }
62 
64 }
65 
67  while (ros::ok()) {
68  std::vector<mavros_msgs::Mavlink> hil_msgs = hil_interface_->CollectData();
69 
70  while (!hil_msgs.empty()) {
71  mavlink_pub_.publish(hil_msgs.back());
72  hil_msgs.pop_back();
73  }
74 
75  ros::spinOnce();
76  rate_.sleep();
77  }
78 }
79 
80 void HilInterfaceNode::HilControlsCallback(const mavros_msgs::HilControlsConstPtr& hil_controls_msg) {
81  mav_msgs::Actuators act_msg;
82 
83  ros::Time current_time = ros::Time::now();
84 
85  act_msg.normalized.push_back(hil_controls_msg->roll_ailerons);
86  act_msg.normalized.push_back(hil_controls_msg->pitch_elevator);
87  act_msg.normalized.push_back(hil_controls_msg->yaw_rudder);
88  act_msg.normalized.push_back(hil_controls_msg->aux1);
89  act_msg.normalized.push_back(hil_controls_msg->aux2);
90  act_msg.normalized.push_back(hil_controls_msg->throttle);
91 
92  act_msg.header.stamp.sec = current_time.sec;
93  act_msg.header.stamp.nsec = current_time.nsec;
94 
95  actuators_pub_.publish(act_msg);
96 }
97 
98 }
99 
100 int main(int argc, char** argv) {
101  ros::init(argc, argv, "rotors_hil_interface_node");
102  rotors_hil::HilInterfaceNode hil_interface_node;
103 
104  hil_interface_node.MainTask();
105 
106  return 0;
107 }
int main(int argc, char **argv)
Subscriber subscribe(const std::string &topic, uint32_t queue_size, void(T::*fp)(M), T *obj, const TransportHints &transport_hints=TransportHints())
ros::Rate rate_
Object for spinning.
std::unique_ptr< HilInterface > hil_interface_
Pointer to the HIL interface object.
ROSCPP_DECL void init(int &argc, char **argv, const std::string &name, uint32_t options=0)
ros::Publisher actuators_pub_
ROS publisher for sending actuator commands.
void MainTask()
Main execution loop.
static constexpr char COMMAND_ACTUATORS[]
bool param(const std::string &param_name, T &param_val, const T &default_val) const
void publish(const boost::shared_ptr< M > &message) const
static constexpr double kDefaultBodyToSensorsRoll
static const std::string kDefaultMavlinkPubTopic
static constexpr bool kDefaultSensorLevelHil
ROSCPP_DECL bool ok()
static const std::string kDefaultHilControlsSubTopic
Publisher advertise(const std::string &topic, uint32_t queue_size, bool latch=false)
void HilControlsCallback(const mavros_msgs::HilControlsConstPtr &hil_controls_msg)
Callback for handling HilControls messages.
bool sleep()
ros::Subscriber hil_controls_sub_
ROS subscriber for handling HilControls messages.
static constexpr double kDefaultBodyToSensorsYaw
static constexpr double kDefaultBodyToSensorsPitch
static Time now()
static constexpr double kDefaultHilFrequency
ROSCPP_DECL void spinOnce()
ros::NodeHandle nh_
ROS node handle.
ros::Publisher mavlink_pub_
ROS publisher for sending MAVLINK messages.


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