AllegroHand_keyboard.cpp
Go to the documentation of this file.
00001 #include <ros/ros.h>
00002 
00003 #include "std_msgs/String.h"
00004 //#include "std_msgs/Float32.h"
00005 //#include "std_msgs/Int16.h"
00006 #include "sensor_msgs/JointState.h"
00007 
00008 #include <iostream>
00009 #include <signal.h>
00010 #include <termios.h>
00011 #include <stdio.h>
00012 #include <unistd.h>
00013 
00014 
00015 using namespace std;
00016 
00017 #define DOF_JOINTS 16
00018 
00019 // http://nehe.gamedev.net/article/msdn_virtualkey_codes/15009/
00020 #define VK_SPACE 0x20
00021 #define VK_LSHIFT 0xA0
00022  
00023 
00024 #define KEYCODE_RIGHT 0x43 
00025 #define KEYCODE_LEFT 0x44
00026 #define KEYCODE_UP 0x41
00027 #define KEYCODE_DOWN 0x42
00028 
00029 #define KEYCODE_0       0x30    
00030 #define KEYCODE_1       0x31
00031 #define KEYCODE_2       0x32
00032 #define KEYCODE_3       0x33
00033 #define KEYCODE_4       0x34
00034 #define KEYCODE_5       0x35
00035 #define KEYCODE_6       0x36
00036 #define KEYCODE_7       0x37
00037 #define KEYCODE_8       0x38
00038 #define KEYCODE_9       0x39
00039 
00040 #define KEYCODE_NUMPAD0 0x60
00041 #define KEYCODE_NUMPAD1 0x61
00042 #define KEYCODE_NUMPAD2 0x62
00043 #define KEYCODE_NUMPAD3 0x63
00044 #define KEYCODE_NUMPAD4 0x64
00045 #define KEYCODE_NUMPAD5 0x65
00046 #define KEYCODE_NUMPAD6 0x66
00047 #define KEYCODE_NUMPAD7 0x67
00048 #define KEYCODE_NUMPAD8 0x68
00049 #define KEYCODE_NUMPAD9 0x69
00050 
00051 #define KEYCODE_A       0x41
00052 #define KEYCODE_B       0x42
00053 #define KEYCODE_C       0x43
00054 #define KEYCODE_D       0x44
00055 #define KEYCODE_E       0x45
00056 #define KEYCODE_F       0x46
00057 #define KEYCODE_G       0x47
00058 #define KEYCODE_H       0x48
00059 #define KEYCODE_I       0x49
00060 #define KEYCODE_J       0x4A
00061 #define KEYCODE_K       0x4B
00062 #define KEYCODE_L       0x4C
00063 #define KEYCODE_M       0x4D
00064 #define KEYCODE_N       0x4E
00065 #define KEYCODE_O       0x4F
00066 #define KEYCODE_P       0x50
00067 #define KEYCODE_Q       0x51
00068 #define KEYCODE_R       0x52
00069 #define KEYCODE_S       0x53
00070 #define KEYCODE_T       0x54
00071 #define KEYCODE_U       0x55
00072 #define KEYCODE_V       0x56
00073 #define KEYCODE_W       0x57
00074 #define KEYCODE_X       0x58
00075 #define KEYCODE_Y       0x59
00076 #define KEYCODE_Z       0x5A
00077 
00078 #define KEYCODE_a       0x61
00079 #define KEYCODE_b       0x62
00080 #define KEYCODE_c       0x63
00081 #define KEYCODE_d       0x64
00082 #define KEYCODE_e       0x65
00083 #define KEYCODE_f       0x66
00084 #define KEYCODE_g       0x67
00085 #define KEYCODE_h       0x68
00086 #define KEYCODE_i       0x69
00087 #define KEYCODE_j       0x6A
00088 #define KEYCODE_k       0x6B
00089 #define KEYCODE_l       0x6C
00090 #define KEYCODE_m       0x6D
00091 #define KEYCODE_n       0x6E
00092 #define KEYCODE_o       0x6F
00093 #define KEYCODE_p       0x70
00094 #define KEYCODE_q       0x71
00095 #define KEYCODE_r       0x72
00096 #define KEYCODE_s       0x73
00097 #define KEYCODE_t       0x74
00098 #define KEYCODE_u       0x75
00099 #define KEYCODE_v       0x76
00100 #define KEYCODE_w       0x77
00101 #define KEYCODE_x       0x78
00102 #define KEYCODE_y       0x79
00103 #define KEYCODE_z       0x7A
00104 
00105 class AHKeyboard
00106 {
00107 public:
00108         AHKeyboard();
00109         void keyLoop();
00110 
00111 private:
00112 
00113   
00114         ros::NodeHandle nh_;
00115         int count_;
00116         float edit_;
00117         ros::Publisher cmd_pub_;
00118   
00119 };
00120 
00121         AHKeyboard::AHKeyboard():
00122         count_(0),
00123         edit_(0.0)
00124 {
00125         cmd_pub_ = nh_.advertise<std_msgs::String>("/allegroHand/lib_cmd", 10);
00126 }
00127 
00128 
00129 
00130 int finger_num;
00131 int knuckle_num;
00132 
00133 
00134 int kfd = 0;
00135 struct termios cooked, raw;
00136 
00137 void quit(int sig)
00138 {
00139         tcsetattr(kfd, TCSANOW, &cooked);
00140         ros::shutdown();
00141         exit(0);
00142 }
00143 
00144 
00145 int main(int argc, char** argv)
00146 {
00147         ros::init(argc, argv, "allegro_hand_keyboard_cmd");
00148         AHKeyboard allegro_hand_keyboard_cmd;
00149 
00150         signal(SIGINT,quit);
00151 
00152         allegro_hand_keyboard_cmd.keyLoop();
00153   
00154         return(0);
00155 }
00156 
00157 
00158 void AHKeyboard::keyLoop()
00159 {
00160         char c;
00161         bool dirty=false;
00162         bool jointUpdate=false;
00163 
00164 
00165         // get the console in raw mode                                                              
00166         tcgetattr(kfd, &cooked);
00167         memcpy(&raw, &cooked, sizeof(struct termios));
00168         raw.c_lflag &=~ (ICANON | ECHO);
00169         // Setting a new line, then end of file                         
00170         raw.c_cc[VEOL] = 1;
00171         raw.c_cc[VEOF] = 2;
00172         tcsetattr(kfd, TCSANOW, &raw);
00173 
00174         sleep(2);
00175         std::cout << std::endl;
00176         std::cout << " -----------------------------------------------------------------------------" << std::endl;
00177         std::cout << "  Use the keyboard to send Allegro Hand grasp & motion commands" << std::endl;
00178         std::cout << " -----------------------------------------------------------------------------" << std::endl;
00179         
00180         std::cout << "\tHome Pose:\t\t\t'H'" << std::endl;  
00181         std::cout << "\tReady Pose:\t\t\t'R'" << std::endl; 
00182         std::cout << "\tPinch (index+thumb):\t\t'I'" << std::endl; 
00183         std::cout << "\tPinch (middle+thumb):\t\t'M'" << std::endl; 
00184         std::cout << "\tGrasp (3 fingers):\t\t'G'" << std::endl; 
00185         std::cout << "\tGrasp (4 fingers):\t\t'F'" << std::endl;  
00186         std::cout << "\tGrasp (envelop):\t\t'E'" << std::endl;  
00187         std::cout << "\tMotors Off (free motion):\t'O'" << std::endl;   
00188         std::cout << "\tSave Current Pose:\t\t'S'" << std::endl;  
00189         std::cout << "\tPD Control (last saved):\t'Space'" << std::endl;                
00190         std::cout << " -----------------------------------------------------------------------------" << std::endl;             
00191         //std::cout << "  Note: Unless elsewhere implemented, these keyboard commands only work with " << std::endl;    
00192         //std::cout << "  the 'allegro_hand_core_grasp' and 'allegro_hand_core_grasp_slp' packages." << std::endl;              
00193         std::cout << "  Subscriber code for reading these messages is included in '~core_template'." << std::endl;              
00194         std::cout << " -----------------------------------------------------------------------------\n" << std::endl;                   
00195 
00196   for(;;)
00197   {
00198   
00199         std_msgs::String msg;
00200     std::stringstream ss;
00201   
00202     // get the next event from the keyboard  
00203     if(read(kfd, &c, 1) < 0)
00204     {
00205       perror("read():");
00206       exit(-1);
00207     }
00208 
00209     //linear_=angular_=0;
00210     ROS_DEBUG("value: 0x%02X\n", c);
00211     printf("value: 0x%02X\n", c);
00212 
00213         switch(c)
00214     {
00215                 case VK_SPACE:
00216                 ROS_DEBUG("space bar: PD Control");
00217                 //cmd_ = 4;
00218                 ss << "pdControl";
00219                 dirty = true;
00220                 break;
00221         case KEYCODE_h:
00222                 ROS_DEBUG("h_key: Home");
00223                 //cmd_ = 1;
00224                 ss << "home";
00225                 dirty = true;
00226                 break;
00227         case KEYCODE_r:
00228                 ROS_DEBUG("r_key: Ready");
00229                 //cmd_ = 2;
00230                 ss << "ready";
00231                 dirty = true;
00232                 break;
00233         case KEYCODE_g:
00234                 ROS_DEBUG("g_key: Grasp (3 finger)");
00235                 //cmd_ = 3;
00236                 ss << "grasp_3";
00237                 dirty = true;
00238                 break;  
00239         case KEYCODE_f:
00240                 ROS_DEBUG("f_key: Grasp (4 finger)");
00241                 //cmd_ = 3;
00242                 ss << "grasp_4";
00243                 dirty = true;
00244                 break;                  
00245         case KEYCODE_p:
00246                 ROS_DEBUG("p_key: Pinch (index)");
00247                 //cmd_ = 4;
00248                 ss << "pinch_it";
00249                 dirty = true;
00250                 break;  
00251         case KEYCODE_m:
00252                 ROS_DEBUG("m_key: Pinch (middle)");
00253                 //cmd_ = 4;
00254                 ss << "pinch_mt";
00255                 dirty = true;
00256                 break;  
00257                 case KEYCODE_e:
00258                 ROS_DEBUG("e_key: Envelop");
00259                 //cmd_ = 4;
00260                 ss << "envelop";
00261                 dirty = true;
00262                 break;  
00263                 case KEYCODE_o:
00264                 ROS_DEBUG("o_key: Servos Off");
00265                 //cmd_ = 4;
00266                 ss << "off";
00267                 dirty = true;
00268                 break; 
00269                 case KEYCODE_s:
00270                 ROS_DEBUG("s_key: Save joint pos. for PD control");
00271                 //cmd_ = 4;
00272                 ss << "save";
00273                 dirty = true;
00274                 break;                                  
00275     }
00276    
00277 
00278     if(dirty ==true)
00279     {
00280       msg.data = ss.str();
00281       ROS_INFO("%s", msg.data.c_str());
00282       cmd_pub_.publish(msg);    
00283       dirty=false;
00284     }
00285     
00286   }
00287 
00288 
00289   return;
00290 }
00291 
00292 
00293 


allegro_hand_keyboard
Author(s): Alex
autogenerated on Sun Oct 5 2014 22:09:38