Go to the documentation of this file.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 #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
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
00114 tcgetattr(kfd, &cooked);
00115 memcpy(&raw, &cooked, sizeof(struct termios));
00116 raw.c_lflag &=~ (ICANON | ECHO);
00117
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
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 }