quad_joy_teleop.cpp
Go to the documentation of this file.
00001 #include "quad_joy_teleop/quad_joy_teleop.h"
00002 
00003 QuadJoyTeleop::QuadJoyTeleop(ros::NodeHandle nh, ros::NodeHandle nh_private):
00004   nh_(nh), 
00005   nh_private_(nh_private)
00006 {
00007   ROS_INFO("Starting QuadJoyTeleop"); 
00008 
00009   takeoff_btn_pressed_  = false;
00010   land_btn_pressed_     = false;
00011   engage_btn_pressed_   = false;
00012   estop_btn_pressed_    = false;
00013   pos_hold_btn_pressed_ = false;
00014   vel_hold_btn_pressed_ = false;
00015 
00016   last_joy_event_ = ros::Time::now();
00017 
00018   cmd_vel_msg_ = boost::make_shared<geometry_msgs::Twist>();
00019   cmd_vel_msg_->linear.x  = 0.0;
00020   cmd_vel_msg_->linear.y  = 0.0;
00021   cmd_vel_msg_->linear.z  = 0.0;
00022   cmd_vel_msg_->angular.x  = 0.0;
00023   cmd_vel_msg_->angular.y  = 0.0;
00024   cmd_vel_msg_->angular.z  = 0.0;
00025 
00026   initParams();
00027 
00028   ros::NodeHandle nh_mav (nh_, "mav");
00029 
00030   // **** publishers
00031 
00032   // TODO: have all cmd_joy all here?
00033 
00034   cmd_roll_publisher_ = nh_mav.advertise<mav_msgs::Roll>(
00035     "cmd/roll", 1);
00036   cmd_pitch_publisher_ = nh_mav.advertise<mav_msgs::Pitch>(
00037     "cmd/pitch", 1);
00038   cmd_yaw_rate_publisher_ = nh_mav.advertise<mav_msgs::YawRate>(
00039     "cmd/yaw_rate", 1);
00040   cmd_thrust_publisher_ = nh_mav.advertise<mav_msgs::Thrust>(
00041     "cmd/thrust", 1);
00042   cmd_vel_publisher_ = nh_mav.advertise<geometry_msgs::TwistStamped>(
00043     "cmd_joy/vel", 1);
00044 
00045   // **** subscribers
00046 
00047   joy_subscriber_ = nh_.subscribe(
00048     "joy", 1, &QuadJoyTeleop::joyCallback, this);
00049 
00050   // **** services
00051 
00052   estop_client_ = nh_mav.serviceClient<mav_srvs::ESTOP>(
00053     "estop");
00054   takeoff_client_ = nh_mav.serviceClient<mav_srvs::Takeoff>(
00055     "takeoff");
00056   land_client_ = nh_mav.serviceClient<mav_srvs::Land>(
00057     "land");
00058   change_des_pose_client_ = nh_mav.serviceClient<mav_srvs::ChangeDesPose>(
00059     "changeDesPose");
00060   toggle_engage_client_ = nh_mav.serviceClient<mav_srvs::ToggleEngage>(
00061     "toggleEngage");
00062   pos_hold_client_ = nh_mav.serviceClient<mav_srvs::PositionHold>(
00063     "positionHold");
00064   vel_hold_client_ = nh_mav.serviceClient<mav_srvs::VelocityHold>(
00065     "velocityHold");
00066 }
00067 
00068 QuadJoyTeleop::~QuadJoyTeleop()
00069 {
00070   ROS_INFO("Destroying QuadJoyTeleop"); 
00071 }
00072 
00073 void QuadJoyTeleop::initParams()
00074 {
00075   // for position commands
00076   x_step_size_ = 0.25;
00077   y_step_size_ = 0.25;
00078   z_step_size_ = 0.25;
00079   yaw_step_size_ = 0.7853975; // 45 degrees
00080 
00081   // for velocity commands
00082   cmd_vel_linear_scale_ = 1.0;
00083   cmd_vel_angular_scale_ = 1.0; // rad
00084   
00085   // for direct commands
00086   cmd_roll_scale_     = 0.5;       
00087   cmd_pitch_scale_    = 0.5;
00088   cmd_yaw_rate_scale_ = 1.0;   
00089   cmd_thrust_scale_   = 100.0;  
00090 
00091   estop_button_    = 999;   
00092   takeoff_button_  = 12;    // triangle
00093   land_button_     = 14;    // X
00094   engage_button_   = 15;    // square
00095   pos_hold_button_ = 11;    // R1 (right trigger)
00096   vel_hold_button_ = 10;    // L1 (left trigger)
00097 
00098   roll_axis_   = 2;
00099   pitch_axis_  = 3;
00100   yaw_axis_    = 0;
00101   thrust_axis_ = 1;
00102 
00103   vy_axis_   = 2;
00104   vx_axis_   = 3;
00105   vz_axis_   = 1;
00106   vyaw_axis_ = 0;
00107 }
00108 
00109 void QuadJoyTeleop::joyCallback (const sensor_msgs::JoyPtr& joy_msg)
00110 {
00111 /*
00112   // **** E-STOP
00113   if (joy_msg->buttons[estop_button_] == 1)
00114   {
00115     ROS_WARN("E-STOP button pressed!");
00116 
00117     mav_srvs::ESTOP estop_srv;
00118     estop_client_.call(estop_srv);
00119   }
00120 */
00121   // ****  takeoff
00122   if (joy_msg->buttons[takeoff_button_] == 1)
00123   {
00124     if (!takeoff_btn_pressed_)
00125     {
00126       ROS_INFO("Takeoff button pressed.");
00127 
00128       mav_srvs::Takeoff takeoff_srv;
00129       takeoff_client_.call(takeoff_srv);
00130 
00131       takeoff_btn_pressed_ = true;
00132     }
00133   }
00134   else
00135     takeoff_btn_pressed_ = false;
00136 
00137   // **** land
00138   if (joy_msg->buttons[land_button_] == 1)
00139   {
00140     if (!land_btn_pressed_)
00141     {
00142       ROS_INFO("Land button pressed.");
00143 
00144       mav_srvs::Land land_srv;
00145       land_client_.call(land_srv);
00146 
00147       land_btn_pressed_ = true;
00148     }
00149   }
00150   else
00151     land_btn_pressed_ = false;
00152 
00153   // **** engage / disengage
00154   if (joy_msg->buttons[engage_button_ ] == 1)
00155   {
00156     if (!engage_btn_pressed_)
00157     {
00158       ROS_INFO("Toggle-Engage button pressed.");
00159 
00160       mav_srvs::ToggleEngage toggle_engage_srv; 
00161       toggle_engage_client_.call(toggle_engage_srv);
00162 
00163       engage_btn_pressed_ = true;
00164     }
00165   }
00166   else
00167     engage_btn_pressed_ = false;
00168 
00169   // **** HOLD
00170   if (joy_msg->buttons[pos_hold_button_ ] == 1)
00171   {
00172     if (!pos_hold_btn_pressed_)
00173     {
00174       ROS_INFO("Pos. Hold button pressed.");
00175 
00176       mav_srvs::PositionHold pos_hold_srv; 
00177       pos_hold_client_.call(pos_hold_srv);
00178 
00179       pos_hold_btn_pressed_ = true;
00180     }
00181   }
00182   else
00183     pos_hold_btn_pressed_ = false;
00184 
00185   // **** RELEASE
00186   if (joy_msg->buttons[vel_hold_button_ ] == 1)
00187   {
00188     if (!vel_hold_btn_pressed_)
00189     {
00190       ROS_INFO("Vel. Hold button pressed.");
00191 
00192       mav_srvs::VelocityHold vel_hold_srv; 
00193       vel_hold_client_.call(vel_hold_srv);
00194 
00195       vel_hold_btn_pressed_ = true;
00196     }
00197   }
00198   else
00199     vel_hold_btn_pressed_ = false;
00200 
00201   // restrict to one press per .20 seconds
00202   if ((ros::Time::now() - last_joy_event_).toSec() > 0.20) 
00203   {
00204     mav_srvs::ChangeDesPose change_des_pose_srv;
00205     change_des_pose_srv.request.delta_x   = 0.0;
00206     change_des_pose_srv.request.delta_y   = 0.0;
00207     change_des_pose_srv.request.delta_z   = 0.0;
00208     change_des_pose_srv.request.delta_yaw = 0.0;
00209 
00210     // **** z: axis 8 
00211 
00212     if (joy_msg->buttons[6] == 1)
00213     {
00214       ROS_INFO("Z-down button pressed.");
00215       change_des_pose_srv.request.delta_z = -z_step_size_;
00216       change_des_pose_client_.call(change_des_pose_srv);
00217       last_joy_event_ = ros::Time::now();
00218     }
00219     else if (joy_msg->buttons[4] == 1)
00220     {
00221       ROS_INFO("Z-up button pressed.");
00222       change_des_pose_srv.request.delta_z =  z_step_size_;
00223       change_des_pose_client_.call(change_des_pose_srv);
00224       last_joy_event_ = ros::Time::now();
00225     }
00226   }
00227 
00228   // **** direct yaw/pitch/roll/thrust commands
00229 
00230   mav_msgs::Roll    cmd_roll_msg;
00231   mav_msgs::Pitch   cmd_pitch_msg;
00232   mav_msgs::YawRate cmd_yaw_rate_msg;
00233   mav_msgs::Thrust  cmd_thrust_msg;
00234 
00235   cmd_roll_msg.roll = 
00236     joy_msg->axes[roll_axis_]   * cmd_roll_scale_ * (-1);
00237   cmd_pitch_msg.pitch = 
00238     joy_msg->axes[pitch_axis_]  * cmd_pitch_scale_;
00239   cmd_yaw_rate_msg.yaw_rate = 
00240     joy_msg->axes[yaw_axis_]    * cmd_yaw_rate_scale_;
00241   cmd_thrust_msg.thrust = 
00242     joy_msg->axes[thrust_axis_] * cmd_thrust_scale_;
00243 
00244   cmd_thrust_msg.thrust = std::max(cmd_thrust_msg.thrust, 0.0);
00245 
00246   cmd_roll_publisher_.publish(cmd_roll_msg);
00247   cmd_pitch_publisher_.publish(cmd_pitch_msg);
00248   cmd_yaw_rate_publisher_.publish(cmd_yaw_rate_msg);
00249   cmd_thrust_publisher_.publish(cmd_thrust_msg);
00250 
00251   // **** velocity commands
00252 
00253   geometry_msgs::TwistStamped twist_msg;
00254   twist_msg.header.stamp = ros::Time::now();
00255 
00256   double vx_j   = joy_msg->axes[vx_axis_];
00257   double vy_j   = joy_msg->axes[vy_axis_];
00258   double vz_j   = joy_msg->axes[vz_axis_];
00259   double vyaw_j = joy_msg->axes[vyaw_axis_];
00260 
00261   double vx, vy, vz, vyaw;
00262 
00263   // linear x
00264   if      (std::abs(vx_j) <= 0.1) vx = 0.0;
00265   else if (std::abs(vx_j) <= 1.0) vx = vx_j * cmd_vel_linear_scale_;
00266   else                            vx = 0.0;
00267 
00268   // linear y
00269   if      (std::abs(vy_j) <= 0.1) vy = 0.0;
00270   else if (std::abs(vy_j) <= 1.0) vy = vy_j * cmd_vel_linear_scale_;
00271   else                            vy = 0.0;
00272 
00273   // linear z
00274   if      (std::abs(vz_j) <= 0.1) vz = 0.0;
00275   else if (std::abs(vz_j) <= 1.0) vz = vz_j * cmd_vel_linear_scale_;
00276   else                            vz = 0.0;
00277 
00278   // angular z
00279   if      (std::abs(vyaw_j) <= 0.1) vyaw = 0.0;
00280   else if (std::abs(vyaw_j) <= 1.0) vyaw = vyaw_j * cmd_vel_angular_scale_;
00281   else                              vyaw = 0.0;
00282 
00283   twist_msg.twist.linear.x = vx;
00284   twist_msg.twist.linear.y = vy;
00285   twist_msg.twist.linear.z = vz;
00286 
00287   twist_msg.twist.angular.x = 0.0;
00288   twist_msg.twist.angular.y = 0.0;
00289   twist_msg.twist.angular.z = vyaw;
00290 
00291   cmd_vel_publisher_.publish(twist_msg);
00292 }


quad_joy_teleop
Author(s): Ivan Dryanovski
autogenerated on Thu Jan 2 2014 11:28:44