teleop_head.cpp
Go to the documentation of this file.
00001 /*
00002  * teleop_head
00003  * Copyright (c) 2008, Willow Garage, Inc.
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 <ORGANIZATION> 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 
00031 // Author: Tully Foote
00032 
00033 #include <cstdio>
00034 #include <cstdlib>
00035 #include <unistd.h>
00036 #include <math.h>
00037 #include "ros/ros.h"
00038 #include "joy/Joy.h"
00039 
00040 #include "sensor_msgs/JointState.h"
00041 
00042 class TeleopHead 
00043 {
00044    public:
00045   double req_pan, req_tilt, max_pan, max_tilt, min_tilt;
00046   int axis_pan, axis_tilt;
00047   int deadman_button, passthrough_button;
00048   double pan_step, tilt_step;
00049   bool deadman_no_publish_;
00050   bool deadman_;
00051 
00052   ros::NodeHandle n_;
00053   ros::Publisher head_pub_;
00054   ros::Subscriber joy_sub_;
00055 
00056   TeleopHead(bool deadman_no_publish = false) : max_pan(0.6), max_tilt(1.4), min_tilt(0.4), pan_step(0.1), tilt_step(0.1), deadman_no_publish_(deadman_no_publish), deadman_(false) {}
00057 
00058   void init()
00059   {
00060     req_pan = req_tilt = 0;
00061     
00062     // Head pan/tilt parameters
00063     n_.param("max_pan", max_pan, max_pan);
00064     n_.param("max_tilt", max_tilt, max_tilt);
00065     n_.param("min_tilt", min_tilt, min_tilt);
00066     
00067     n_.param("tilt_step", tilt_step, tilt_step);
00068     n_.param("pan_step", pan_step, pan_step);
00069     
00070     n_.param("axis_pan", axis_pan, 0);
00071     n_.param("axis_tilt", axis_tilt, 2);
00072 
00073     n_.param("deadman_button", deadman_button, 0);
00074     
00075     ROS_DEBUG("tilt step: %.3f rad\n", tilt_step);
00076     ROS_DEBUG("pan step: %.3f rad\n", pan_step);
00077     
00078     ROS_DEBUG("axis_pan: %d\n", axis_pan);
00079     ROS_DEBUG("axis_tilt: %d\n", axis_tilt);
00080     
00081     ROS_DEBUG("deadman_button: %d\n", deadman_button);
00082     
00083     head_pub_ = n_.advertise<sensor_msgs::JointState>("head_traj_controller/command", 1);
00084 
00085     joy_sub_ = n_.subscribe("joy", 10, &TeleopHead::joy_cb, this);
00086   }
00087 
00088 
00089   ~TeleopHead() {  }
00090 
00091 
00092   void joy_cb(const joy::Joy::ConstPtr& joy_msg)
00093   {
00094     deadman_ = (((unsigned int)deadman_button < joy_msg->get_buttons_size()) && joy_msg->buttons[deadman_button]);
00095 
00096     if (!deadman_)
00097       return;
00098 
00099     if((axis_pan >= 0) && (((unsigned int)axis_pan) < joy_msg->get_axes_size()))
00100     {
00101       req_pan += joy_msg->axes[axis_pan] * pan_step;
00102       req_pan = std::max(std::min(req_pan, max_pan), -max_pan);
00103     }
00104     
00105     if ((axis_tilt >= 0) && (((unsigned int)axis_tilt) < joy_msg->get_axes_size()))
00106     {
00107       req_tilt += joy_msg->axes[axis_tilt] * tilt_step;
00108       req_tilt = std::max(std::min(req_tilt, max_tilt), -max_tilt);
00109     }
00110   }
00111 
00112   void send_cmd()
00113   {
00114     if (deadman_)
00115     { 
00116       sensor_msgs::JointState joint_cmds;
00117       joint_cmds.set_name_size(2);
00118       joint_cmds.set_position_size(2);
00119       joint_cmds.name[0] ="head_pan_joint";
00120       joint_cmds.position[0] = req_pan;
00121       joint_cmds.name[1] ="head_tilt_joint";
00122       joint_cmds.position[1] = req_tilt;
00123       head_pub_.publish(joint_cmds);
00124       fprintf(stdout,"teleop_head:: %f, %f\n", req_pan, req_tilt);  
00125     }
00126     else if (!deadman_no_publish_)
00127     {
00128       sensor_msgs::JointState joint_cmds;
00129       joint_cmds.set_name_size(2);
00130       joint_cmds.set_position_size(2);
00131       joint_cmds.name[0] ="head_pan_joint";
00132       joint_cmds.position[0] = req_pan;
00133       joint_cmds.name[1] ="head_tilt_joint";
00134       joint_cmds.position[1] = req_tilt;
00135       head_pub_.publish(joint_cmds);
00136       fprintf(stdout,"teleop_head:: %f, %f\n", req_pan, req_tilt);  
00137     }
00138   }
00139 
00140 };
00141 
00142 int main(int argc, char **argv)
00143 {
00144   ros::init(argc, argv, "teleop_head");
00145    const char* opt_no_publish    = "--deadman_no_publish";
00146 
00147    bool no_publish = false;
00148    for(int i=1;i<argc;i++)
00149    {
00150      if(!strncmp(argv[i], opt_no_publish, strlen(opt_no_publish)))
00151        no_publish = true;
00152    }
00153 
00154    TeleopHead teleop_head(no_publish);
00155    teleop_head.init();
00156 
00157    ros::Rate pub_rate(20);
00158 
00159    while (teleop_head.n_.ok())
00160    {
00161      ros::spinOnce(); 
00162      teleop_head.send_cmd();
00163      pub_rate.sleep();
00164    }
00165    
00166    exit(0);
00167    return 0;
00168 }
00169 


teleop_head
Author(s): Kevin Watts
autogenerated on Fri Jan 3 2014 11:38:59