00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034 #include <ros/ros.h>
00035 #include <sensor_msgs/Joy.h>
00036 #include <geometry_msgs/Twist.h>
00037 #include <robotnik_msgs/ptz.h>
00038
00039
00040 #include <unistd.h>
00041 #include <robotnik_msgs/set_mode.h>
00042 #include <diagnostic_updater/diagnostic_updater.h>
00043 #include <diagnostic_updater/publisher.h>
00044 #include <sensor_msgs/JointState.h>
00045
00046 #define DEFAULT_NUM_OF_BUTTONS 16
00047 #define DEFAULT_AXIS_LINEAR_Z 3
00048 #define DEFAULT_AXIS_PAN 0
00049 #define DEFAULT_AXIS_TILT 1
00050 #define DEFAULT_SCALE_LINEAR_Z 0.1
00051 #define DEFAULT_SCALE_PAN 2.0
00052 #define DEFAULT_SCALE_TILT 2.0
00053
00054 #define ITERATIONS_AFTER_DEADMAN 3.0
00055
00056 class RB1TorsoPad
00057 {
00058 public:
00059 RB1TorsoPad();
00060 void Update();
00061
00062 private:
00063 void padCallback(const sensor_msgs::Joy::ConstPtr& joy);
00064
00065 ros::NodeHandle nh_;
00066
00067 int axis_linear_z_;
00068 double scale_linear_z_;
00069
00070 int axis_pan_;
00071 double scale_pan_;
00072
00073 int axis_tilt_;
00074 double scale_tilt_;
00075
00077 ros::Publisher joint_command_pub_;
00079 ros::Subscriber pad_sub_;
00080
00081 double current_vel;
00082
00084 int dead_man_button_;
00085
00087 int speed_up_button_, speed_down_button_;
00088
00090 int num_of_buttons_;
00091
00093 bool bRegisteredButtonEvent[DEFAULT_NUM_OF_BUTTONS];
00094
00095
00097 diagnostic_updater::HeaderlessTopicDiagnostic *pub_command_freq;
00099 diagnostic_updater::HeaderlessTopicDiagnostic *sus_joy_freq;
00101 diagnostic_updater::Updater updater_pad;
00103 double min_freq_command, min_freq_joy;
00105 double max_freq_command, max_freq_joy;
00107
00108 std::string cmd_topic_;
00109 };
00110
00111
00112
00113
00114
00115
00116 RB1TorsoPad::RB1TorsoPad()
00117 {
00118 current_vel = 0.1;
00119
00120 nh_.param("num_of_buttons", num_of_buttons_, DEFAULT_NUM_OF_BUTTONS);
00121
00122
00123 nh_.param("axis_linear_z", axis_linear_z_, DEFAULT_AXIS_LINEAR_Z);
00124 nh_.param("scale_linear_z", scale_linear_z_, DEFAULT_SCALE_LINEAR_Z);
00125
00126 nh_.param("axis_pan", axis_pan_, DEFAULT_AXIS_PAN);
00127 nh_.param("scale_pan", scale_pan_, DEFAULT_SCALE_PAN);
00128 nh_.param("axis_tilt", axis_tilt_, DEFAULT_AXIS_TILT);
00129 nh_.param("scale_tilt", scale_tilt_, DEFAULT_SCALE_TILT);
00130
00131 nh_.param("button_dead_man_torso", dead_man_button_, dead_man_button_);
00132 nh_.param("button_speed_up", speed_up_button_, speed_up_button_);
00133 nh_.param("button_speed_down", speed_down_button_, speed_down_button_);
00134
00135
00136 ROS_INFO("RB1TorsoPad num_of_buttons_ = %d", num_of_buttons_);
00137 for(int i = 0; i < num_of_buttons_; i++){
00138 bRegisteredButtonEvent[i] = false;
00139 ROS_INFO("bREG %d", i);
00140 }
00141
00142
00143
00144
00145
00146
00147
00148
00149
00150
00151
00152
00153
00154
00155 nh_.param<std::string>("cmd_topic", cmd_topic_, "/joint_commands");
00156 joint_command_pub_ = nh_.advertise<sensor_msgs::JointState>(cmd_topic_, 1);
00157
00158
00159
00160 pad_sub_ = nh_.subscribe<sensor_msgs::Joy>("joy", 10, &RB1TorsoPad::padCallback, this);
00161
00162
00163 updater_pad.setHardwareID("None");
00164
00165 min_freq_command = min_freq_joy = 5.0;
00166 max_freq_command = max_freq_joy = 50.0;
00167 sus_joy_freq = new diagnostic_updater::HeaderlessTopicDiagnostic("/joy", updater_pad,
00168 diagnostic_updater::FrequencyStatusParam(&min_freq_joy, &max_freq_joy, 0.1, 10));
00169
00170 pub_command_freq = new diagnostic_updater::HeaderlessTopicDiagnostic(cmd_topic_.c_str(), updater_pad,
00171 diagnostic_updater::FrequencyStatusParam(&min_freq_command, &max_freq_command, 0.1, 10));
00172 }
00173
00174
00175
00176
00177
00178 void RB1TorsoPad::Update(){
00179 updater_pad.update();
00180 }
00181
00182 void RB1TorsoPad::padCallback(const sensor_msgs::Joy::ConstPtr& joy)
00183 {
00184 static int send_iterations_after_dead_man;
00185
00186 sensor_msgs::JointState cmd;
00187
00188
00189 cmd.name.resize(3);
00190
00191 cmd.name[0] = "torso_slider_joint"; cmd.name[1] = "head_pan_joint"; cmd.name[2] = "head_tilt_joint";
00192 cmd.velocity.resize(3);
00193 cmd.velocity[0] = 0.0; cmd.velocity[1] = 0.0; cmd.velocity[2] = 0.0;
00194
00195
00196
00197 if (joy->buttons[dead_man_button_] == 1) {
00198
00199 if ( joy->buttons[speed_down_button_] == 1 ){
00200 if(!bRegisteredButtonEvent[speed_down_button_])
00201 if(current_vel > 0.1){
00202 current_vel = current_vel - 0.1;
00203 bRegisteredButtonEvent[speed_down_button_] = true;
00204 ROS_INFO("Velocity: %f%%", current_vel*100.0);
00205 char buf[50]="\0";
00206 int percent = (int) (current_vel*100.0);
00207 sprintf(buf," %d percent", percent);
00208
00209 }
00210 }else{
00211 bRegisteredButtonEvent[speed_down_button_] = false;
00212 }
00213
00214 if (joy->buttons[speed_up_button_] == 1){
00215 if(!bRegisteredButtonEvent[speed_up_button_])
00216 if(current_vel < 0.9){
00217 current_vel = current_vel + 0.1;
00218 bRegisteredButtonEvent[speed_up_button_] = true;
00219 ROS_INFO("Velocity: %f%%", current_vel*100.0);
00220 char buf[50]="\0";
00221 int percent = (int) (current_vel*100.0);
00222 sprintf(buf," %d percent", percent);
00223
00224 }
00225
00226 }else{
00227 bRegisteredButtonEvent[speed_up_button_] = false;
00228 }
00229
00230 cmd.velocity[0] = current_vel*scale_linear_z_*joy->axes[axis_linear_z_];
00231 cmd.velocity[1] = current_vel*scale_pan_ * joy->axes[axis_pan_];
00232 cmd.velocity[2] = current_vel*scale_tilt_ * joy->axes[axis_tilt_];
00233
00234
00235 }
00236 else {
00237 cmd.velocity[0] = 0.0; cmd.velocity[1] = 0.0; cmd.velocity[2] = 0.0;
00238 }
00239
00240 sus_joy_freq->tick();
00241
00242
00243 if (joy->buttons[dead_man_button_] == 1) {
00244 send_iterations_after_dead_man = ITERATIONS_AFTER_DEADMAN;
00245 cmd.header.stamp = ros::Time::now();
00246 joint_command_pub_.publish(cmd);
00247 pub_command_freq->tick();
00248 }
00249 else {
00250 if (send_iterations_after_dead_man >0) {
00251 send_iterations_after_dead_man--;
00252 cmd.header.stamp = ros::Time::now();
00253 joint_command_pub_.publish(cmd);
00254 pub_command_freq->tick();
00255 }
00256 }
00257
00258 }
00259
00260
00261 int main(int argc, char** argv)
00262 {
00263 ros::init(argc, argv, "rb1_torso_pad");
00264 RB1TorsoPad rb1_torso_pad;
00265
00266 ros::Rate r(50.0);
00267
00268 while( ros::ok() ){
00269
00270 rb1_torso_pad.Update();
00271 ros::spinOnce();
00272 r.sleep();
00273 }
00274 }
00275