teleop_head_keyboard.cpp
Go to the documentation of this file.
00001 /*
00002  * teleop_head_keyboard
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: Kevin Watts
00032 
00033 #include <termios.h>
00034 #include <signal.h>
00035 #include <math.h>
00036 #include <stdlib.h>
00037 #include <stdio.h>
00038 
00039 #include <ros/ros.h>
00040 
00041 #include <trajectory_msgs/JointTrajectory.h>
00042 
00043 #define HEAD_TOPIC "/head_traj_controller/command"
00044 
00045 #define KEYCODE_A 0x61
00046 #define KEYCODE_D 0x64
00047 #define KEYCODE_S 0x73
00048 #define KEYCODE_W 0x77 
00049 
00050 #define KEYCODE_A_CAP 0x41
00051 #define KEYCODE_D_CAP 0x44
00052 #define KEYCODE_S_CAP 0x53
00053 #define KEYCODE_W_CAP 0x57
00054 
00055 class TeleopHeadKeyboard
00056 {
00057   private:
00058   double req_tilt, req_pan;
00059   double max_pan, max_tilt, min_tilt, tilt_step, pan_step;
00060 
00061   ros::NodeHandle n_;
00062   ros::Publisher head_pub_;
00063 
00064   public:
00065   void init()
00066   {
00067     req_tilt = 0.0;
00068     req_pan = 0.0;
00069  
00070     head_pub_ = n_.advertise<trajectory_msgs::JointTrajectory>(HEAD_TOPIC, 1);
00071     
00072     n_.param("max_pan", max_pan, 2.7);
00073     n_.param("max_tilt", max_tilt, 1.4);
00074     n_.param("min_tilt", min_tilt, -0.4);
00075     n_.param("tilt_step", tilt_step, 0.02);
00076     n_.param("pan_step", pan_step, 0.03);
00077     }
00078   
00079   ~TeleopHeadKeyboard()   { }
00080   void keyboardLoop();
00081 
00082 };
00083 
00084 //THK_Node* thk;
00085 int kfd = 0;
00086 struct termios cooked, raw;
00087 
00088 void quit(int sig)
00089 {
00090   tcsetattr(kfd, TCSANOW, &cooked);
00091   exit(0);
00092 }
00093 
00094 int main(int argc, char** argv)
00095 {
00096   ros::init(argc, argv, "head_keyboard");
00097 
00098   TeleopHeadKeyboard thk;
00099   thk.init();
00100 
00101   signal(SIGINT,quit);
00102 
00103   thk.keyboardLoop();
00104 
00105   return(0);
00106 }
00107 
00108 void TeleopHeadKeyboard::keyboardLoop()
00109 {
00110   char c;
00111   bool dirty=false;
00112 
00113   // get the console in raw mode
00114   tcgetattr(kfd, &cooked);
00115   memcpy(&raw, &cooked, sizeof(struct termios));
00116   raw.c_lflag &=~ (ICANON | ECHO);
00117   // Setting a new line, then end of file
00118   raw.c_cc[VEOL] = 1;
00119   raw.c_cc[VEOF] = 2;
00120   tcsetattr(kfd, TCSANOW, &raw);
00121 
00122   puts("Reading from keyboard");
00123   puts("---------------------------");
00124   puts("Use 'WASD' to pan and tilt");
00125   puts("Press 'Shift' to move faster");
00126 
00127 
00128   for(;;)
00129   {
00130     // get the next event from the keyboard
00131     if(read(kfd, &c, 1) < 0)
00132     {
00133       perror("read():");
00134       exit(-1);
00135     }
00136 
00137     switch(c)
00138     {
00139     case KEYCODE_W:
00140       req_tilt += tilt_step;
00141       dirty = true;
00142       break;
00143     case KEYCODE_S:
00144       req_tilt -= tilt_step;
00145       dirty = true;
00146       break;
00147     case KEYCODE_A:
00148       req_pan += pan_step;
00149       dirty = true;
00150       break;
00151     case KEYCODE_D:
00152       req_pan -= pan_step;
00153       dirty = true;
00154       break;
00155     case KEYCODE_W_CAP:
00156       req_tilt += tilt_step * 3;
00157       dirty = true;
00158       break;
00159     case KEYCODE_S_CAP:
00160       req_tilt -= tilt_step * 3;
00161       dirty = true;
00162       break;
00163     case KEYCODE_A_CAP:
00164       req_pan += pan_step * 3;
00165       dirty = true;
00166       break;
00167     case KEYCODE_D_CAP:
00168       req_pan -= pan_step * 3;
00169       dirty = true;
00170       break;
00171     }
00172 
00173     req_tilt = std::max(std::min(req_tilt, max_tilt), min_tilt);
00174     req_pan = std::max(std::min(req_pan, max_pan), -max_pan);
00175 
00176     if (dirty == true)
00177     {
00178       
00179       trajectory_msgs::JointTrajectory joint_cmds;
00180       joint_cmds.joint_names.push_back ("head_pan_joint");
00181       joint_cmds.joint_names.push_back ("head_tilt_joint");
00182 
00183       joint_cmds.points.resize (1);
00184       joint_cmds.points[0].positions.push_back (req_pan);
00185       joint_cmds.points[0].positions.push_back (req_tilt);
00186       joint_cmds.points[0].time_from_start = ros::Duration (1.0);
00187       head_pub_.publish(joint_cmds);
00188 
00189 
00190     }
00191   }
00192 }


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