rb1_torso_pad.cpp
Go to the documentation of this file.
00001 /*
00002  * rb1_torso_pad
00003  * Copyright (c) 2012, Robotnik Automation, SLL
00004  * All rights reserved.
00005  *
00006  * Redistribution and use in source and binary forms, with or without
00007  * modification, are permitted provided that the following conditions are met:
00008  *
00009  *     * Redistributions of source code must retain the above copyright
00010  *       notice, this list of conditions and the following disclaimer.
00011  *     * Redistributions in binary form must reproduce the above copyright
00012  *       notice, this list of conditions and the following disclaimer in the
00013  *       documentation and/or other materials provided with the distribution.
00014  *     * Neither the name of the Robotnik Automation, SLL. nor the names of its
00015  *       contributors may be used to endorse or promote products derived from
00016  *       this software without specific prior written permission.
00017  *
00018  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
00019  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00020  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
00021  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
00022  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
00023  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
00024  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
00025  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
00026  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
00027  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
00028  * POSSIBILITY OF SUCH DAMAGE.
00029  *
00030  * \author Robotnik Automation, SLL
00031  * \brief Allows to use a pad with the robot controller, sending the messages received from the joystick device
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 // Not yet catkinized 9/2013
00039 // #include <sound_play/sound_play.h>
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         // DIAGNOSTICS
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     //  sound_play::SoundClient sc;
00108     std::string cmd_topic_;
00109 };
00110 
00111 
00112 //RB1TorsoPad::RB1TorsoPad():
00113 //  axis_pan_(0),
00114 //  axis_tilt_(1),
00115 //  linear_z_(3)
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     // MOTION CONF
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         /*ROS_INFO("Service I/O = [%s]", cmd_service_io_.c_str());
00143         ROS_INFO("Topic PTZ = [%s]", cmd_topic_ptz_.c_str());
00144         ROS_INFO("Service I/O = [%s]", cmd_topic_vel_.c_str());
00145         ROS_INFO("Axis linear = %d", linear_);
00146         ROS_INFO("Axis angular = %d", angular_);
00147         ROS_INFO("Scale angular = %d", a_scale_);
00148         ROS_INFO("Deadman button = %d", dead_man_button_);
00149         ROS_INFO("OUTPUT1 button %d", button_output_1_);
00150         ROS_INFO("OUTPUT2 button %d", button_output_2_);
00151         ROS_INFO("OUTPUT1 button %d", button_output_1_);
00152         ROS_INFO("OUTPUT2 button %d", button_output_2_);*/      
00153 
00154         // Publish through the node handle Twist type messages to the guardian_controller/command topic
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         // Listen through the node handle sensor_msgs::Joy messages from joystick 
00159     // (these are the references that we will sent to cmd_vel)
00160         pad_sub_ = nh_.subscribe<sensor_msgs::Joy>("joy", 10, &RB1TorsoPad::padCallback, this);
00161         
00162         // Diagnostics
00163         updater_pad.setHardwareID("None");
00164         // Topics freq control 
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  *      \brief Updates the diagnostic component. Diagnostics
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         // Joints: j1_torso (linear in m/s) j1_head (pan in rad/s), j2_head (tilt in rad/s)
00189         cmd.name.resize(3);
00190     //cmd.name[0] = "j1_torso"; cmd.name[1] = "j1_head"; cmd.name[2] = "j2_head";
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     // Actions dependant on dead-man button
00197     if (joy->buttons[dead_man_button_] == 1) {
00198                 // Set the current velocity level
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                     // sc.say(buf);
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                     // sc.say(buf);
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();   // Ticks the reception of joy events
00241 
00242     // Publish only with deadman button pushed
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 { // send some 0 if deadman is released
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                 // UPDATING DIAGNOSTICS
00270                 rb1_torso_pad.Update();
00271                 ros::spinOnce();
00272                 r.sleep();
00273                 }
00274 }
00275 


rb1_torso_pad
Author(s):
autogenerated on Thu Jun 6 2019 21:17:27