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 
00034 
00036 
00037 #include <ros/ros.h>
00038 #include <geometry_msgs/Twist.h>
00039 #include <std_msgs/Float64.h>
00040 
00041 #include <unistd.h>
00042 #include <termios.h>
00043 #include <cstdio>
00044 
00045 namespace offsetter {
00046 
00047   enum ShiftMode { ROTATION=0, TRANSLATION=1, BASELINE=2 };
00048 
00049   void printUsage()
00050   {
00051     printf("\n  Stereo camera offsetter usage:\n\n");
00052                 printf("  t -> switch to translational mode.\n");
00053                 printf("  r -> switch to rotational mode.\n");
00054                 printf("  b -> switch to baseline mode.\n");
00055   }
00056 
00057   void printPrompt(enum ShiftMode mode)
00058   {
00059     printf("\n");
00060     switch (mode)
00061       {
00062       case TRANSLATION:
00063                                 {
00064                                         printf("      translation mode: press x/X y/Y z/Z to translate >> ");
00065                                         break;
00066                                 }
00067       case ROTATION:
00068                                 {
00069                                         printf("          rotational mode: press x/X y/Y z/Z to rotate >> ");
00070                                         break;
00071                                 }
00072       case BASELINE:
00073                                 {
00074                                         printf("baseline mode: press x/X to decrease/increase baseline >> ");
00075                                         break;
00076                                 }
00077       default:
00078                                 printUsage();
00079       }
00080   }
00081 
00082 }
00083 
00084 using namespace offsetter;
00085 using namespace std;
00086 using namespace ros;
00087 
00088 int main(int argc, char** argv)
00089 {
00090   
00091   const int fd = fileno(stdin);
00092   termios prev_flags ;
00093   tcgetattr(fd, &prev_flags) ;
00094   termios flags ;
00095   tcgetattr(fd,&flags);
00096   flags.c_lflag &= ~ICANON;  
00097   flags.c_cc[VMIN]  = 0;     
00098   flags.c_cc[VTIME] = 0;     
00099   tcsetattr(fd,TCSANOW,&flags);
00100 
00101   ros::init(argc, argv, "stereo_offsetter_keyboard_interface");
00102 
00103   ros::NodeHandle nh;
00104 
00105   ros::Publisher pub = nh.advertise<geometry_msgs::Twist>("virtual_twist", 10);
00106 
00107   ros::Publisher baseline_pub = nh.advertise<std_msgs::Float64>("baseline_factor", 10);
00108 
00109   static const double trans = .001;
00110   static const double rot   = .1 * 3.14 / 180.0;
00111   static const double base  = .005;
00112 
00113   ShiftMode cur_shift_mode = offsetter::TRANSLATION;
00114 
00115   printUsage();
00116   printPrompt(cur_shift_mode);
00117 
00118   while (nh.ok())
00119     {
00120       char c = getchar();
00121 
00122       geometry_msgs::Twist twist;
00123       geometry_msgs::Vector3 base_vec;
00124 
00125       geometry_msgs::Vector3* vec = NULL;
00126       double shift_val;
00127       switch (cur_shift_mode)
00128                                 {
00129                                 case ROTATION:
00130                                         vec = &twist.angular;
00131                                         shift_val = rot;
00132                                         break;
00133                                 case TRANSLATION:
00134                                         vec = &twist.linear;
00135                                         shift_val = trans;
00136                                         break;
00137                                 case BASELINE:
00138                                         vec = &base_vec;
00139                                         shift_val = base;
00140                                         break;
00141                                 default:
00142                                         shift_val = 0.0;
00143                                         ROS_FATAL("unknown shift type [%u]", cur_shift_mode);
00144                                         break;
00145                                 }
00146 
00147       bool should_publish = false;
00148 
00149       switch (c)
00150                                 {
00151                                 case 'x': vec->x = -shift_val; printPrompt(cur_shift_mode); should_publish = true; break;
00152                                 case 'X': vec->x =  shift_val; printPrompt(cur_shift_mode); should_publish = true; break;
00153                                 case 'y': vec->y = -shift_val; printPrompt(cur_shift_mode); should_publish = true; break;
00154                                 case 'Y': vec->y =  shift_val; printPrompt(cur_shift_mode); should_publish = true; break;
00155                                 case 'z': vec->z = -shift_val; printPrompt(cur_shift_mode); should_publish = true; break;
00156                                 case 'Z': vec->z =  shift_val; printPrompt(cur_shift_mode); should_publish = true; break;
00157                                 case 't':
00158                                 case 'T':
00159                                         cur_shift_mode = TRANSLATION;
00160                                         printPrompt(cur_shift_mode);
00161                                         break;
00162                                 case 'r':
00163                                 case 'R':
00164                                         cur_shift_mode = ROTATION;
00165                                         printPrompt(cur_shift_mode);
00166                                         break;
00167                                 case 'b':
00168                                 case 'B':
00169                                         cur_shift_mode = BASELINE;
00170                                         printPrompt(cur_shift_mode);
00171                                         break;
00172                                 case EOF:
00173                                         usleep(100);
00174                                         break;
00175                                 default:
00176                                         usleep(100);
00177                                         printUsage();
00178                                         printPrompt(cur_shift_mode);
00179                                         break;
00180                                 }
00181     
00182       if (should_publish)
00183                                 {
00184                                         
00185                                         
00186                                         
00187                                         pub.publish(twist);
00188 
00189                                         std_msgs::Float64 baseline;
00190                                         baseline.data = vec->x;
00191                                         baseline_pub.publish(baseline);
00192 
00193                                 }
00194     }
00195 
00196   tcsetattr(fd,TCSANOW, &prev_flags) ;         
00197 
00198   return 0;
00199 }
00200