hil_interface_node.cpp
Go to the documentation of this file.
00001 /*
00002  * Copyright 2016 Pavel Vechersky, ASL, ETH Zurich, Switzerland
00003  *
00004  * Licensed under the Apache License, Version 2.0 (the "License");
00005  * you may not use this file except in compliance with the License.
00006  * You may obtain a copy of the License at
00007  *
00008  *     http://www.apache.org/licenses/LICENSE-2.0
00009 
00010  * Unless required by applicable law or agreed to in writing, software
00011  * distributed under the License is distributed on an "AS IS" BASIS,
00012  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00013  * See the License for the specific language governing permissions and
00014  * limitations under the License.
00015  */
00016 
00017 #include "rotors_hil_interface/hil_interface_node.h"
00018 
00019 namespace rotors_hil {
00020 
00021 HilInterfaceNode::HilInterfaceNode() :
00022     rate_(kDefaultHilFrequency) {
00023   ros::NodeHandle pnh("~");
00024 
00025   bool sensor_level_hil;
00026   double hil_frequency;
00027   double S_B_roll;
00028   double S_B_pitch;
00029   double S_B_yaw;
00030   std::string actuators_pub_topic;
00031   std::string mavlink_pub_topic;
00032   std::string hil_controls_sub_topic;
00033 
00034   pnh.param("sensor_level_hil", sensor_level_hil, kDefaultSensorLevelHil);
00035   pnh.param("hil_frequency", hil_frequency, kDefaultHilFrequency);
00036   pnh.param("body_to_sensor_roll", S_B_roll, kDefaultBodyToSensorsRoll);
00037   pnh.param("body_to_sensor_pitch", S_B_pitch, kDefaultBodyToSensorsPitch);
00038   pnh.param("body_to_sensor_yaw", S_B_yaw, kDefaultBodyToSensorsYaw);
00039   pnh.param("actuators_pub_topic", actuators_pub_topic, std::string(mav_msgs::default_topics::COMMAND_ACTUATORS));
00040   pnh.param("mavlink_pub_topic", mavlink_pub_topic, kDefaultMavlinkPubTopic);
00041   pnh.param("hil_controls_sub_topic", hil_controls_sub_topic, kDefaultHilControlsSubTopic);
00042 
00043   // Create the quaternion and rotation matrix to rotate data into NED frame.
00044   Eigen::AngleAxisd roll_angle(S_B_roll, Eigen::Vector3d::UnitX());
00045   Eigen::AngleAxisd pitch_angle(S_B_pitch, Eigen::Vector3d::UnitY());
00046   Eigen::AngleAxisd yaw_angle(S_B_yaw, Eigen::Vector3d::UnitZ());
00047 
00048   const Eigen::Quaterniond q_S_B = roll_angle * pitch_angle * yaw_angle;
00049 
00050   if (sensor_level_hil)
00051     hil_interface_ = std::auto_ptr<HilSensorLevelInterface>(new HilSensorLevelInterface(q_S_B));
00052   else
00053     hil_interface_ = std::auto_ptr<HilStateLevelInterface>(new HilStateLevelInterface(q_S_B));
00054 
00055   rate_ = ros::Rate(hil_frequency);
00056 
00057   actuators_pub_ = nh_.advertise<mav_msgs::Actuators>(actuators_pub_topic, 1);
00058   mavlink_pub_ = nh_.advertise<mavros_msgs::Mavlink>(mavlink_pub_topic, 5);
00059   hil_controls_sub_ = nh_.subscribe(hil_controls_sub_topic, 1,
00060                                         &HilInterfaceNode::HilControlsCallback, this);
00061 }
00062 
00063 HilInterfaceNode::~HilInterfaceNode() {
00064 }
00065 
00066 void HilInterfaceNode::MainTask() {
00067   while (ros::ok()) {
00068     std::vector<mavros_msgs::Mavlink> hil_msgs = hil_interface_->CollectData();
00069 
00070     while (!hil_msgs.empty()) {
00071       mavlink_pub_.publish(hil_msgs.back());
00072       hil_msgs.pop_back();
00073     }
00074 
00075     ros::spinOnce();
00076     rate_.sleep();
00077   }
00078 }
00079 
00080 void HilInterfaceNode::HilControlsCallback(const mavros_msgs::HilControlsConstPtr& hil_controls_msg) {
00081   mav_msgs::Actuators act_msg;
00082 
00083   ros::Time current_time = ros::Time::now();
00084 
00085   act_msg.normalized.push_back(hil_controls_msg->roll_ailerons);
00086   act_msg.normalized.push_back(hil_controls_msg->pitch_elevator);
00087   act_msg.normalized.push_back(hil_controls_msg->yaw_rudder);
00088   act_msg.normalized.push_back(hil_controls_msg->aux1);
00089   act_msg.normalized.push_back(hil_controls_msg->aux2);
00090   act_msg.normalized.push_back(hil_controls_msg->throttle);
00091 
00092   act_msg.header.stamp.sec = current_time.sec;
00093   act_msg.header.stamp.nsec = current_time.nsec;
00094 
00095   actuators_pub_.publish(act_msg);
00096 }
00097 
00098 }
00099 
00100 int main(int argc, char** argv) {
00101   ros::init(argc, argv, "rotors_hil_interface_node");
00102   rotors_hil::HilInterfaceNode hil_interface_node;
00103 
00104   hil_interface_node.MainTask();
00105 
00106   return 0;
00107 }


rotors_hil_interface
Author(s): Pavel Vechersky
autogenerated on Thu Apr 18 2019 02:43:51