joy.cpp
Go to the documentation of this file.
00001 /*
00002  * Copyright 2015 Fadri Furrer, ASL, ETH Zurich, Switzerland
00003  * Copyright 2015 Michael Burri, ASL, ETH Zurich, Switzerland
00004  * Copyright 2015 Mina Kamel, ASL, ETH Zurich, Switzerland
00005  * Copyright 2015 Janosch Nikolic, ASL, ETH Zurich, Switzerland
00006  * Copyright 2015 Markus Achtelik, ASL, ETH Zurich, Switzerland
00007  *
00008  * Licensed under the Apache License, Version 2.0 (the "License");
00009  * you may not use this file except in compliance with the License.
00010  * You may obtain a copy of the License at
00011  *
00012  *     http://www.apache.org/licenses/LICENSE-2.0
00013 
00014  * Unless required by applicable law or agreed to in writing, software
00015  * distributed under the License is distributed on an "AS IS" BASIS,
00016  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00017  * See the License for the specific language governing permissions and
00018  * limitations under the License.
00019  */
00020 
00021 
00022 #include "rotors_joy_interface/joy.h"
00023 
00024 #include <mav_msgs/default_topics.h>
00025 
00026 Joy::Joy() {
00027   ros::NodeHandle nh;
00028   ros::NodeHandle pnh("~");
00029   ctrl_pub_ = nh_.advertise<mav_msgs::RollPitchYawrateThrust> (
00030     mav_msgs::default_topics::COMMAND_ROLL_PITCH_YAWRATE_THRUST, 10);
00031 
00032   control_msg_.roll = 0;
00033   control_msg_.pitch = 0;
00034   control_msg_.yaw_rate = 0;
00035   control_msg_.thrust.x = 0;
00036   control_msg_.thrust.y = 0;
00037   control_msg_.thrust.z = 0;
00038   current_yaw_vel_ = 0;
00039 
00040   pnh.param("axis_roll_", axes_.roll, 0);
00041   pnh.param("axis_pitch_", axes_.pitch, 1);
00042   pnh.param("axis_thrust_", axes_.thrust, 2);
00043 
00044   pnh.param("axis_direction_roll", axes_.roll_direction, -1);
00045   pnh.param("axis_direction_pitch", axes_.pitch_direction, 1);
00046   pnh.param("axis_direction_thrust", axes_.thrust_direction, 1);
00047 
00048   pnh.param("max_v_xy", max_.v_xy, 1.0);  // [m/s]
00049   pnh.param("max_roll", max_.roll, 10.0 * M_PI / 180.0);  // [rad]
00050   pnh.param("max_pitch", max_.pitch, 10.0 * M_PI / 180.0);  // [rad]
00051   pnh.param("max_yaw_rate", max_.rate_yaw, 45.0 * M_PI / 180.0);  // [rad/s]
00052   pnh.param("max_thrust", max_.thrust, 30.0);  // [N]
00053 
00054   pnh.param("v_yaw_step", v_yaw_step_, 0.05);  // [rad/s]
00055 
00056   pnh.param("is_fixed_wing", is_fixed_wing_, false);
00057 
00058   pnh.param("button_yaw_left_", buttons_.yaw_left, 3);
00059   pnh.param("button_yaw_right_", buttons_.yaw_right, 4);
00060   pnh.param("button_ctrl_enable_", buttons_.ctrl_enable, 5);
00061   pnh.param("button_ctrl_mode_", buttons_.ctrl_mode, 10);
00062   pnh.param("button_takeoff_", buttons_.takeoff, 7);
00063   pnh.param("button_land_", buttons_.land, 8);
00064 
00065   namespace_ = nh_.getNamespace();
00066   joy_sub_ = nh_.subscribe("joy", 10, &Joy::JoyCallback, this);
00067 }
00068 
00069 void Joy::StopMav() {
00070   control_msg_.roll = 0;
00071   control_msg_.pitch = 0;
00072   control_msg_.yaw_rate = 0;
00073   control_msg_.thrust.x = 0;
00074   control_msg_.thrust.y = 0;
00075   control_msg_.thrust.z = 0;
00076 }
00077 
00078 void Joy::JoyCallback(const sensor_msgs::JoyConstPtr& msg) {
00079   current_joy_ = *msg;
00080   control_msg_.roll = msg->axes[axes_.roll] * max_.roll * axes_.roll_direction;
00081   control_msg_.pitch = msg->axes[axes_.pitch] * max_.pitch * axes_.pitch_direction;
00082 
00083   if (msg->buttons[buttons_.yaw_left]) {
00084     current_yaw_vel_ = max_.rate_yaw;
00085   }
00086   else if (msg->buttons[buttons_.yaw_right]) {
00087     current_yaw_vel_ = -max_.rate_yaw;
00088   }
00089   else {
00090     current_yaw_vel_ = 0;
00091   }
00092   control_msg_.yaw_rate = current_yaw_vel_;
00093 
00094   if (is_fixed_wing_) {
00095     double thrust = msg->axes[axes_.thrust] * axes_.thrust_direction;
00096     control_msg_.thrust.x = (thrust >= 0.0) ? thrust : 0.0;
00097   }
00098   else {
00099     control_msg_.thrust.z = (msg->axes[axes_.thrust] + 1) * max_.thrust / 2.0 * axes_.thrust_direction;
00100   }
00101 
00102   ros::Time update_time = ros::Time::now();
00103   control_msg_.header.stamp = update_time;
00104   control_msg_.header.frame_id = "rotors_joy_frame";
00105   Publish();
00106 }
00107 
00108 void Joy::Publish() {
00109   ctrl_pub_.publish(control_msg_);
00110 }
00111 
00112 int main(int argc, char** argv) {
00113   ros::init(argc, argv, "rotors_joy_interface");
00114   Joy joy;
00115 
00116   ros::spin();
00117 
00118   return 0;
00119 }


rotors_joy_interface
Author(s): Fadri Furrer, Michael Burri, Mina Kamel, Janosch Nikolic, Markus Achtelik
autogenerated on Thu Apr 18 2019 02:43:53