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
00032 #include <miscellaneous.h>
00033
00034
00035
00036
00037 void print_help(void)
00038 {
00039 const char help[]={"***************************************************************************+\n\
00040 ------------------------------ |\n\
00041 | PHUA HAPTIC INTERFACE HELP | |\n\
00042 ------------------------------ |\n\
00043 |\n\
00044 This program uses RS232 comunication over an USB adapter to send |\n\
00045 commands to the humanoid robot. |\n\
00046 If you're having trouble connecting, use the path to your USB port |\n\
00047 as an argument when you call the application. |\n\
00048 |\n\
00049 example: >> ./phua_haptic_interface /dev/ttyUSB* |\n\
00050 [where * is the number of your port] |\n\
00051 |\n\
00052 ---------------------------------------------------------------------------+\n\
00053 |\n\
00054 The PHANToM OMNI joystick functions require a FireWire port active |\n\
00055 and superuser permissions to access it. |\n\
00056 If you are having trouble with this, open a terminal and run: |\n\
00057 |\n\
00058 >> sudo modprobe raw1394 |\n\
00059 |\n\
00060 This will probe the FireWire port the joystick connection. If it is |\n\
00061 sucessful, module raw1394 should appear in your ´/dev/´ system folder. |\n\
00062 |\n\
00063 For the program to have permission to communicate with the device, call |\n\
00064 this application with superuser rights using ´sudo´ or run within |\n\
00065 a terminal: |\n\
00066 |\n\
00067 >> sudo chmod 777 /dev/raw1394 |\n\
00068 |\n\
00069 This should allow normal execution of the program. |\n\
00070 |\n\
00071 ---------------------------------------------------------------------------+\n\
00072 |\n\
00073 To run aplication in debug mode, use argument ´--debug´. |\n\
00074 In this mode, application will execute even if connections are |\n\
00075 not established. Keep in mind that the application may not |\n\
00076 properly control the robot or retrieve data from the PHANToM OMNI |\n\
00077 joystick in this mode, and a restart is required in order to |\n\
00078 use the program's full funcionalities. |\n\
00079 |\n\
00080 example: >> ./phua_haptic_interface --debug |\n\
00081 |\n\
00082 ---------------------------------------------------------------------------+\n\
00083 |\n\
00084 Pedro Cruz 2012 |\n\
00085 Happy Haptics! :-) |\n\
00086 ***************************************************************************+"
00087 };
00088 printf("%s\n",help);
00089 }
00090
00091 void print_greeting(shared_vars_t*RobotVars)
00092 {
00093 char servo_comm_on[15]=" ";
00094 if(RobotVars->servo->IsActive())
00095 strcat(servo_comm_on,"ONLINE");
00096 else
00097 strcat(servo_comm_on,"OFFLINE");
00098
00099 char phant_on[15]=" ";
00100 if(RobotVars->phantom_data.phantom_on)
00101 strcat(phant_on,"ONLINE");
00102 else
00103 strcat(phant_on,"OFFLINE");
00104
00105 printf("***************************************************************************\n\
00106 ------------------------------------- \n\
00107 | PHUA HAPTIC INTERFACE APPLICATION | \n\
00108 ------------------------------------- \n\
00109 \n\
00110 Pedro Cruz 2012 \n\
00111 ***************************************************************************\n\
00112 \n\
00113 REQUIRED COMUNICATION STATUS \n\
00114 ---------------------------------- \n\
00115 > Servomotor COMM: |%s | \n\
00116 > PHANToM OMNI COMM: |%s | \n\
00117 ---------------------------------- \n",
00118 servo_comm_on,phant_on);
00119 }
00120
00121 void CalculateAverageCartesianSpeed(double diffX, double diffY, double diffZ, long double time_interval, double *return_vector)
00122 {
00123 static const uint array_size=20;
00124
00125 static std::vector<double> accumulationXdiff;
00126 static std::vector<double> accumulationYdiff;
00127 static std::vector<double> accumulationZdiff;
00128 static std::vector<long double> accumulation_time;
00129
00130 accumulationXdiff.push_back(diffX);
00131 accumulationYdiff.push_back(diffY);
00132 accumulationZdiff.push_back(diffZ);
00133 accumulation_time.push_back(time_interval);
00134
00135 if(accumulationXdiff.size()>array_size)
00136 accumulationXdiff.erase(accumulationXdiff.begin());
00137
00138 if(accumulationYdiff.size()>array_size)
00139 accumulationYdiff.erase(accumulationYdiff.begin());
00140
00141 if(accumulationZdiff.size()>array_size)
00142 accumulationZdiff.erase(accumulationZdiff.begin());
00143
00144 if(accumulation_time.size()>array_size)
00145 accumulation_time.erase(accumulation_time.begin());
00146
00147 double x_mean=pc_mean<double>(accumulationXdiff);
00148 double y_mean=pc_mean<double>(accumulationYdiff);
00149 double z_mean=pc_mean<double>(accumulationZdiff);
00150 long double time_mean=pc_mean<long double>(accumulation_time);
00151
00152 return_vector[0]=round(x_mean/time_mean);
00153 return_vector[1]=round(y_mean/time_mean);
00154 return_vector[2]=round(z_mean/time_mean);
00155 }
00156
00157 Eigen::Matrix3d rotx(double angle_in_degrees)
00158 {
00159 static Eigen::Matrix3d rotation_matrix;
00160 static double angle_in_radians;
00161
00162 angle_in_radians = DegToRad(angle_in_degrees);
00163
00164 for(int i=0; i<3; i++)
00165 {
00166 for(int j=0; j<3; j++)
00167 {
00168 if((i==0 && j==0))
00169 rotation_matrix(i,j) = 1;
00170 else if((i==1 && j==1) || (i==2 && j==2))
00171 rotation_matrix(i,j) = cos(angle_in_radians);
00172 else if((i==1 && j==2))
00173 rotation_matrix(i,j) = -1 * sin(angle_in_radians);
00174 else if((i==2 && j==1))
00175 rotation_matrix(i,j) = sin(angle_in_radians);
00176 else
00177 rotation_matrix(i,j) = 0;
00178 }
00179 }
00180 return rotation_matrix;
00181 }
00182
00183 Eigen::Matrix3d roty(double angle_in_degrees)
00184 {
00185 static Eigen::Matrix3d rotation_matrix;
00186 static double angle_in_radians;
00187
00188 angle_in_radians = DegToRad(angle_in_degrees);
00189
00190 for(int i=0; i<3; i++)
00191 {
00192 for(int j=0; j<3; j++)
00193 {
00194 if(i==1 && j==1)
00195 rotation_matrix(i,j) = 1;
00196 else if((i==0 && j==0) || (i==2 && j==2))
00197 rotation_matrix(i,j) = cos(angle_in_radians);
00198 else if((i==0 && j==2))
00199 rotation_matrix(i,j) = sin(angle_in_radians);
00200 else if((i==2 && j==0))
00201 rotation_matrix(i,j) = -1. * sin(angle_in_radians);
00202 else
00203 rotation_matrix(i,j) = 0;
00204 }
00205 }
00206 return rotation_matrix;
00207 }
00208
00209 Eigen::Matrix3d rotz(double angle_in_degrees)
00210 {
00211 static Eigen::Matrix3d rotation_matrix;
00212 static double angle_in_radians;
00213
00214 angle_in_radians = DegToRad(angle_in_degrees);
00215
00216 for(int i=0; i<3; i++)
00217 {
00218 for(int j=0; j<3; j++)
00219 {
00220 if(i==2 && j==2)
00221 rotation_matrix(i,j) = 1;
00222 else if((i==0 && j==0) || (i==1 && j==1))
00223 rotation_matrix(i,j) = cos(angle_in_radians);
00224 else if((i==1 && j==0))
00225 rotation_matrix(i,j) = sin(angle_in_radians);
00226 else if((i==0 && j==1))
00227 rotation_matrix(i,j) = -1. * sin(angle_in_radians);
00228 else
00229 rotation_matrix(i,j) = 0;
00230 }
00231 }
00232 return rotation_matrix;
00233 }
00234
00235 double pc_mean(double *v, int len)
00236 {
00237 double sum = 0;
00238 int i;
00239 for (i = 0; i < len; i++)
00240 sum += v[i];
00241 return sum / len;
00242 }
00243
00244 int isNumeric (const char * s)
00245 {
00246 if (s == NULL || *s == '\0' || isspace(*s))
00247 return 0;
00248 char * p;
00249 int r=strtod (s, &p);
00250 if(r){}
00251 return *p == '\0';
00252 }
00253
00254 double get_sign(double x)
00255 {
00256 return ((x >= 0.) ? 1. : -1.);
00257 }
00258
00259 int GetNbrFromKeyboard(void)
00260 {
00261 int s;
00262 int ret=scanf("%d",&s);
00263 if(ret){}
00264 return s;
00265 }
00266
00267 char GetStrFromKeyboard(void)
00268 {
00269 char s;
00270 int ret=scanf("%s",&s);
00271 if(ret){}
00272 return s;
00273 }
00274
00275 double GetDblFromKeyboard(void)
00276 {
00277 float s;
00278 int ret=scanf("%f",&s);
00279 if(ret){}
00280 return s;
00281 }
00282
00283 void ScreenClear(void)
00284 {
00285 printf("\033[2J");
00286 printf("\033[0;0f");
00287 }
00288
00289 void textcolor(int attr, int fg, int bg)
00290 {
00291 printf("%c[%d;%d;%dm", 0x1B, attr, fg + 30, bg + 40);
00292 }
00293
00294 void PrintRedLine(void)
00295 {
00296 int n;
00297 textcolor(RESET, RED, WHITE);
00298 for(n=1;n<41;n++)
00299 printf("=");
00300 printf("\n");
00301 }
00302
00303 void ResetTextColors(void)
00304 {
00305 textcolor(RESET, WHITE, WHITE);
00306 }
00307
00308 void HighLightText(void)
00309 {
00310 textcolor(BRIGHT, WHITE, BLACK);
00311 }