00001
00025 #include "geometry_msgs/Twist.h"
00026 #include "ros/ros.h"
00027 #include "sensor_msgs/Joy.h"
00028 #include "teleop_twist_joy/teleop_twist_joy.h"
00029
00030 #include <map>
00031 #include <string>
00032
00033
00034 namespace teleop_twist_joy
00035 {
00036
00042 struct TeleopTwistJoy::Impl
00043 {
00044 void joyCallback(const sensor_msgs::Joy::ConstPtr& joy);
00045
00046 ros::Subscriber joy_sub;
00047 ros::Publisher cmd_vel_pub;
00048
00049 int enable_button;
00050 int enable_turbo_button;
00051
00052 std::map<std::string, int> axis_linear_map;
00053 std::map<std::string, double> scale_linear_map;
00054 std::map<std::string, double> scale_linear_turbo_map;
00055
00056 std::map<std::string, int> axis_angular_map;
00057 std::map<std::string, double> scale_angular_map;
00058 std::map<std::string, double> scale_angular_turbo_map;
00059
00060 bool sent_disable_msg;
00061 };
00062
00068 TeleopTwistJoy::TeleopTwistJoy(ros::NodeHandle* nh, ros::NodeHandle* nh_param)
00069 {
00070 pimpl_ = new Impl;
00071
00072 pimpl_->cmd_vel_pub = nh->advertise<geometry_msgs::Twist>("cmd_vel", 1, true);
00073 pimpl_->joy_sub = nh->subscribe<sensor_msgs::Joy>("joy", 1, &TeleopTwistJoy::Impl::joyCallback, pimpl_);
00074
00075 nh_param->param<int>("enable_button", pimpl_->enable_button, 0);
00076 nh_param->param<int>("enable_turbo_button", pimpl_->enable_turbo_button, -1);
00077
00078 if (nh_param->getParam("axis_linear", pimpl_->axis_linear_map))
00079 {
00080 nh_param->getParam("axis_linear", pimpl_->axis_linear_map);
00081 nh_param->getParam("scale_linear", pimpl_->scale_linear_map);
00082 nh_param->getParam("scale_linear_turbo", pimpl_->scale_linear_turbo_map);
00083 }
00084 else
00085 {
00086 nh_param->param<int>("axis_linear", pimpl_->axis_linear_map["x"], 1);
00087 nh_param->param<double>("scale_linear", pimpl_->scale_linear_map["x"], 0.5);
00088 nh_param->param<double>("scale_linear_turbo", pimpl_->scale_linear_turbo_map["x"], 1.0);
00089 }
00090
00091 if (nh_param->getParam("axis_angular", pimpl_->axis_angular_map))
00092 {
00093 nh_param->getParam("axis_angular", pimpl_->axis_angular_map);
00094 nh_param->getParam("scale_angular", pimpl_->scale_angular_map);
00095 nh_param->getParam("scale_angular_turbo", pimpl_->scale_angular_turbo_map);
00096 }
00097 else
00098 {
00099 nh_param->param<int>("axis_angular", pimpl_->axis_angular_map["yaw"], 0);
00100 nh_param->param<double>("scale_angular", pimpl_->scale_angular_map["yaw"], 0.5);
00101 nh_param->param<double>("scale_angular_turbo",
00102 pimpl_->scale_angular_turbo_map["yaw"], pimpl_->scale_angular_map["yaw"]);
00103 }
00104
00105 ROS_INFO_NAMED("TeleopTwistJoy", "Teleop enable button %i.", pimpl_->enable_button);
00106 ROS_INFO_COND_NAMED(pimpl_->enable_turbo_button >= 0, "TeleopTwistJoy",
00107 "Turbo on button %i.", pimpl_->enable_turbo_button);
00108
00109 for (std::map<std::string, int>::iterator it = pimpl_->axis_linear_map.begin();
00110 it != pimpl_->axis_linear_map.end(); ++it)
00111 {
00112 ROS_INFO_NAMED("TeleopTwistJoy", "Linear axis %s on %i at scale %f.",
00113 it->first.c_str(), it->second, pimpl_->scale_linear_map[it->first]);
00114 ROS_INFO_COND_NAMED(pimpl_->enable_turbo_button >= 0, "TeleopTwistJoy",
00115 "Turbo for linear axis %s is scale %f.", it->first.c_str(), pimpl_->scale_linear_turbo_map[it->first]);
00116 }
00117
00118 for (std::map<std::string, int>::iterator it = pimpl_->axis_angular_map.begin();
00119 it != pimpl_->axis_angular_map.end(); ++it)
00120 {
00121 ROS_INFO_NAMED("TeleopTwistJoy", "Angular axis %s on %i at scale %f.",
00122 it->first.c_str(), it->second, pimpl_->scale_angular_map[it->first]);
00123 ROS_INFO_COND_NAMED(pimpl_->enable_turbo_button >= 0, "TeleopTwistJoy",
00124 "Turbo for angular axis %s is scale %f.", it->first.c_str(), pimpl_->scale_angular_turbo_map[it->first]);
00125 }
00126
00127 pimpl_->sent_disable_msg = false;
00128 }
00129
00130 void TeleopTwistJoy::Impl::joyCallback(const sensor_msgs::Joy::ConstPtr& joy_msg)
00131 {
00132
00133 geometry_msgs::Twist cmd_vel_msg;
00134
00135 if (enable_turbo_button >= 0 && joy_msg->buttons[enable_turbo_button])
00136 {
00137 if (axis_linear_map.find("x") != axis_linear_map.end())
00138 {
00139 cmd_vel_msg.linear.x = joy_msg->axes[axis_linear_map["x"]] * scale_linear_turbo_map["x"];
00140 }
00141 if (axis_linear_map.find("y") != axis_linear_map.end())
00142 {
00143 cmd_vel_msg.linear.y = joy_msg->axes[axis_linear_map["y"]] * scale_linear_turbo_map["y"];
00144 }
00145 if (axis_linear_map.find("z") != axis_linear_map.end())
00146 {
00147 cmd_vel_msg.linear.z = joy_msg->axes[axis_linear_map["z"]] * scale_linear_turbo_map["z"];
00148 }
00149 if (axis_angular_map.find("yaw") != axis_angular_map.end())
00150 {
00151 cmd_vel_msg.angular.z = joy_msg->axes[axis_angular_map["yaw"]] * scale_angular_turbo_map["yaw"];
00152 }
00153 if (axis_angular_map.find("pitch") != axis_angular_map.end())
00154 {
00155 cmd_vel_msg.angular.y = joy_msg->axes[axis_angular_map["pitch"]] * scale_angular_turbo_map["pitch"];
00156 }
00157 if (axis_angular_map.find("roll") != axis_angular_map.end())
00158 {
00159 cmd_vel_msg.angular.x = joy_msg->axes[axis_angular_map["roll"]] * scale_angular_turbo_map["roll"];
00160 }
00161
00162 cmd_vel_pub.publish(cmd_vel_msg);
00163 sent_disable_msg = false;
00164 }
00165 else if (joy_msg->buttons[enable_button])
00166 {
00167 if (axis_linear_map.find("x") != axis_linear_map.end())
00168 {
00169 cmd_vel_msg.linear.x = joy_msg->axes[axis_linear_map["x"]] * scale_linear_map["x"];
00170 }
00171 if (axis_linear_map.find("y") != axis_linear_map.end())
00172 {
00173 cmd_vel_msg.linear.y = joy_msg->axes[axis_linear_map["y"]] * scale_linear_map["y"];
00174 }
00175 if (axis_linear_map.find("z") != axis_linear_map.end())
00176 {
00177 cmd_vel_msg.linear.z = joy_msg->axes[axis_linear_map["z"]] * scale_linear_map["z"];
00178 }
00179 if (axis_angular_map.find("yaw") != axis_angular_map.end())
00180 {
00181 cmd_vel_msg.angular.z = joy_msg->axes[axis_angular_map["yaw"]] * scale_angular_map["yaw"];
00182 }
00183 if (axis_angular_map.find("pitch") != axis_angular_map.end())
00184 {
00185 cmd_vel_msg.angular.y = joy_msg->axes[axis_angular_map["pitch"]] * scale_angular_map["pitch"];
00186 }
00187 if (axis_angular_map.find("roll") != axis_angular_map.end())
00188 {
00189 cmd_vel_msg.angular.x = joy_msg->axes[axis_angular_map["roll"]] * scale_angular_map["roll"];
00190 }
00191
00192 cmd_vel_pub.publish(cmd_vel_msg);
00193 sent_disable_msg = false;
00194 }
00195 else
00196 {
00197
00198
00199 if (!sent_disable_msg)
00200 {
00201 cmd_vel_pub.publish(cmd_vel_msg);
00202 sent_disable_msg = true;
00203 }
00204 }
00205 }
00206
00207 }