Main Page | Namespace List | Class Hierarchy | Alphabetical List | Class List | File List | Namespace Members | Class Members | File Members | Related Pages

robot/control/oldikor/IKOR/useful_UTILS.c

Go to the documentation of this file.
00001 /* ________________________________________________________
00002   |                                                        |
00003   | Program Name:   useful_UTILS.c                         |
00004   |________________________________________________________|
00005   |                                                        |
00006   | description: This file contains stuff that I'm not sure|
00007   |    where to put it in the system.  Some of them like   |
00008   |    Solutions_init really don't belong here, others     |
00009   |    might. I recommend that if you too have procedures  |
00010   |    to add and not quite sure where they should go,     |
00011   |    leave them here_(8^>).                              |
00012   |                                                        |
00013   | Procedures:                                            |
00014   |    IKerror          :                                  |
00015   |    Solutions_init   :                                  |
00016   |    Solutions_free   :                                  |
00017   |    GetData          :                                  |
00018   |    spheres          :                                  |
00019   |    fmat_pr/fmat_prf :                                  |
00020   |    fprint_norm      :                                  |
00021   |________________________________________________________| */
00022 
00023 
00024 #include <IKOR/general.h>       /* Header File to link all others */
00025 
00026 /* ________________________________________________________
00027   | name:    IKerror                                       |
00028   | description: Prints out error message and exits to     |
00029   |          to the system, if desired.                    |
00030   |                                                        |
00031   | inputs:   error_vector and fatal flag.                 |
00032   | outputs:  error message displayed to screen.           |
00033   |________________________________________________________|
00034   | Authors:     Date:     Modifications:                  |
00035   | c hacker     3/95      Created                         |
00036   |________________________________________________________| */
00037 
00038 void IKerror(int error_vector, int fatal, char *mesg)
00039 {
00040   fprintf (stderr, "\nERROR %d: ", error_vector);
00041   switch  (error_vector) {
00042       case 10: fprintf (stderr, "FSP didn't complete!\n");             break;
00043       case 11: fprintf (stderr, "Unable to Open '%s'\n", mesg);        break;
00044       case 12: fprintf (stderr, "General File Error in '%s'", mesg);   break;
00045       case 15: fprintf (stderr, "Too Many Points in Trajectory. '%s'\n",mesg);
00046                                                                        break;
00047       case 16: fprintf (stderr, "Request Joint Space %d is > %d in %s.\n",
00048                                                  M, Robot->NA,mesg);   break;
00049       case 17: fprintf (stderr, "Requested Task Space %d is > 6 in %s.\n",
00050                                                               mesg);   break;
00051       case 18: fprintf (stderr, "Memory allocation error in 's'\n",mesg);
00052                                                                        break;
00053       case 19: fprintf (stderr, "Manipulator File Integrity Lost \n Bad data",
00054                                 " file: ROBOT.dat in '%s'\n", mesg);   break;
00055       case 21: fprintf (stderr, "SORRY!...more constraints %s %s\n",
00056                                 "than D.O.R. in procedure: ", mesg);   break;
00057       case 23: fprintf (stderr, "Defaulting to using one-shot in %s.\n",
00058                                                               mesg);   break;
00059       case 24: fprintf (stderr, "Platform does not exist, see %s.\n", mesg);
00060                                                                        break;
00061       case 25: fprintf (stderr, "System did not complete.\n");         break;
00062       case 26: fprintf (stderr, "Requested dx[%s] too large.\n",mesg); break;
00063       case 29: fprintf (stderr, "%s%s%s\n", "System Error: Did not Execute One",
00064                                 " Step or Two Step in ",mesg);         break;
00065       case 30: fprintf (stderr, "Division by zero in Analytical.c\n"); break;
00066       case 31: fprintf (stderr, "Option UnImplemented: %s\n",mesg);    break;
00067       default: fprintf (stderr, "No such Error. error_vector doesn't exist.\n");
00068   }
00069 
00070   if (fatal == FATAL)
00071      { fprintf(stderr, "FATAL Error.  aborting... \n"); abort(); }
00072   else fprintf(stderr, "Error not Fatal.  Continuing execution...\n");
00073 }
00074 
00075 
00076 /* ________________________________________________________
00077   | name:    Solutions_init                                |
00078   | description: The Solutions structure is explained in   |
00079   |     structures.h, this procedure is used to declare    |
00080   |     and allocate memory for the structure.             |
00081   |                                                        |
00082   | inputs:  M (joint space) and N (Task space)            |
00083   | outputs: Allocates memory for the three Matrixes as    |
00084   |     well as the structure itself. Also initializes     |
00085   |     M, N, Mred, Nred (Mred and Nred are the current    |
00086   |     delienators of the space, they are changed in      |
00087   |     the file FSP.c in the function ReduceA.            |
00088   |________________________________________________________|
00089   | Authors:     Date:     Modifications:                  |
00090   | c hacker     3/95      Created                         |
00091   |________________________________________________________| */
00092 
00093 Solutions *Solutions_init(int M, int N)
00094 {
00095   int i, j;
00096   Solutions *Temp;
00097 
00098   Temp = (Solutions *) malloc( sizeof( Solutions ) );
00099   Temp->g       = mat_malloc( M, M);         /* Soln   */
00100   Temp->Qarray  = mat_malloc( M, 1);         /* Angles */
00101   Temp->Xelim   = mat_malloc( M, 1);         /* Angles */
00102   Temp->betall  = mat_malloc( M - N + 15, M); /* Betas  */
00103 
00104   for (i=0; i< Temp->betall->rows; i++)
00105         for (j=0; j<Temp->betall->cols; j++)
00106                 Temp->betall->p[i][j]=0.0;
00107 
00108   if(!Temp) IKerror(18, FATAL, "Solutions_init");
00109 
00110   Temp->M = Temp->Mred = M;    /* init  space size */
00111   Temp->N = Temp->Nred = N;
00112   Temp->cn = 0;                /* init # constrnts */
00113 
00114   return (Temp);
00115 }
00116 
00117 /* ________________________________________________________
00118   | name:    Solutions_free                                |
00119   | description: The Solutions structure is explained in   |
00120   |     structures.h, this procedure is used to free       |
00121   |     any allocated memory for the structure.            |
00122   |                                                        |
00123   | inputs:  M (joint space) and N (Task space)            |
00124   | outputs: Deallocates memory for the three Matrixes as  |
00125   |     well as the structure itself.                      |
00126   |________________________________________________________|
00127   | Authors:     Date:     Modifications:                  |
00128   | c hacker     3/95      Created                         |
00129   |________________________________________________________| */
00130 
00131 void Solutions_free( Solutions *Temp )
00132 {
00133   mat_free(Temp->betall);
00134   mat_free(Temp->Qarray);
00135   mat_free(Temp->Xelim);
00136   mat_free(Temp->g);
00137   free ( (char *) Temp);
00138 }
00139 
00140 
00141 /* ________________________________________________________
00142   | name:    GetData                                       |
00143   | description:    read in A and b vector to be used for  |
00144   |     testing the code with a single jacobian and dx. The|
00145   |     macro DEBUG_FSP must be defined in the file:       |
00146   |                    general.h.                          |
00147   |                                                        |
00148   | inputs:  A file called : FSP_data                      |
00149   | outputs: Aorig, borig                                  |
00150   |________________________________________________________|
00151   | Authors:     Date:     Modifications:                  |
00152   |  K Morgansn  6/94      Created                         |
00153   |  c hacker    3/95      Removed struct from header      |
00154   |________________________________________________________| */
00155 
00156 GetData ( MATRIX *A,  MATRIX *b)
00157 {
00158   FILE *fpin;
00159   int i, j;
00160 
00161   fpin = fopen("FSP_data", "r");
00162 
00163   for (i=0; i<N; i++)
00164     for (j=0; j<M; j++)
00165       fscanf(fpin, "%f", &A->p[i][j]);
00166 
00167   for (i=0; i<N; i++)
00168     fscanf(fpin, "%f", &b->p[i][0]);
00169 
00170   fclose(fpin);
00171 }
00172 
00173 
00174 /* ________________________________________________________
00175   | name:    spheres                                       |
00176   | description: spheres reads a datafile, or user's input |
00177   |     for data concerning spherical objects.  However, I |
00178   |     rarely use this in an windows interface becuase it |
00179   |     is easier to just change the sphere_data file it-  |
00180   |     self, but to each his own.                         |
00181   | inputs:  spheredata a 2D array                         |
00182   | outputs: spheredata contains spherical obstacles.      |
00183   |________________________________________________________|
00184   | Authors:     Date:     Modifications:                  |
00185   | F Tulloch    2/93      Created                         |
00186   | c hacker    10/94      removed spheredata as global    |
00187   |________________________________________________________| */
00188 
00189 int spheres(spheredata, datafp)
00190 FILE  *datafp;
00191 double spheredata[4][4];
00192 {
00193   int   ns, sphere_no, i;
00194   FILE *sphere_file;
00195   char  question='n';
00196 
00197   if (CHANGE_SPHERES)
00198   {
00199     fprintf(stdout, "\nDo you want to change the spheres? ");
00200     scanf(" %c", &question);
00201   }
00202 
00203   if ((sphere_file = fopen("sphere_data", "r+")) == NULL)
00204   { IKerror(11, OK, "sphere_data"); return 0; }
00205   
00206   if ((question != 'Y') && (question != 'y'))
00207   {
00208     fscanf(sphere_file, "%d", &ns);
00209     for (sphere_no = 0; sphere_no < ns; sphere_no++)
00210       for (i = 0; i < 4; i++)
00211         fscanf(sphere_file, "%lf", &spheredata[sphere_no][i]);
00212   }
00213   else
00214   {
00215     fprintf(stdout, "How many spheres? (4 max): ");
00216 
00217     /*IF FILE EXISTS IT MUST  define how        */
00218     /*  many spheres to read even if 0 spheres  */
00219     /* (don't just have an empty file.)         */
00220     scanf("%d", &ns);
00221     if (ns > 4)  ns = 4; /*LIMITATION*/
00222 
00223     for (i = 0; i < ns; i++)
00224     {
00225       fprintf(stdout, "xcenter of sphere[%d]: ", i);
00226       fscanf(stdin, "%f", &spheredata[i][0]);
00227       fprintf(stdout, "ycenter of sphere[%d]: ", i);
00228       fscanf(stdin, "%f", &spheredata[i][1]);
00229       fprintf(stdout, "zcenter of sphere[%d]: ", i);
00230       fscanf(stdin, "%f", &spheredata[i][2]);
00231       fprintf(stdout, " radius of sphere[%d]: ", i);
00232       fscanf(stdin, "%f", &spheredata[i][3]);
00233     }
00234 
00235     fprintf(sphere_file, "%i\n", ns);
00236     for (sphere_no = 0; sphere_no < ns; sphere_no++)
00237     {
00238       for (i = 0; i < 4; i++)
00239         fprintf(sphere_file, "%f ", spheredata[sphere_no][i]);
00240       fprintf(sphere_file, "\n");
00241     }
00242   }                          /******* end else ********/
00243 
00244   fprintf(datafp,"\n Spheres read into the system...");
00245   for (i=0; i< ns; i++)        /* confirm proper initialization  */
00246     fprintf(datafp,"\n%i: <%f, %f, %f> %f",i,
00247         spheredata[i][0], spheredata[i][1], spheredata[i][2], spheredata[i][3]);
00248 
00249   fclose(sphere_file);
00250   return (ns);
00251 }                            /* end spheres... */
00252 
00253 
00254 /* ________________________________________________________
00255   | name:    fmat_pr/ fmat_prf                             |
00256   | description: Disqusted with the inability to print the |
00257   |     MATRIX to a file which is currently the way IKOR   |
00258   |     communicates with the outside world, I have alter- |
00259   |     ed the mat_pr function in matrix.c. The function   |
00260   |     that ends with an 'f' means more decimal places    |
00261   |     will be printed out.                               |
00262   | inputs:  Datafile, description string, and a MATRIX    |
00263   | outputs: The checkfile contains the input string and   |
00264   |     MATRIX.                                            |
00265   |________________________________________________________|
00266   | Authors:     Date:     Modifications:                  |
00267   | c hacker    10/94      Created                         |
00268   |              3/95      took struct from "struct MATRIX"|
00269   |________________________________________________________| */
00270 
00271 void fmat_pr (FILE *checkfile, char *string, MATRIX *a)
00272 { 
00273   int i, j;
00274 
00275   fprintf(checkfile, "\n   %s ROWS: %d, COLS: %d\n", 
00276         string, a->rows, a->cols);
00277   for (i = 0; i < a->rows; i++)
00278   {
00279     for (j = 0; j < a->cols; j++)
00280       fprintf(checkfile, "%8.4f ", a->p[i][j]);
00281     fprintf(checkfile, "\n");
00282   }
00283   fprintf(checkfile, "\n");
00284 }
00285 
00286 void fmat_prf(FILE *checkfile, char *string, MATRIX *a)
00287 {
00288   int i, j;
00289 
00290   fprintf(checkfile, "\n   %s Rows: %d, Cols: %d\n",
00291          string, a->rows, a->cols);
00292   for (i = 0; i < a->rows; i++)
00293   {
00294     for (j = 0; j < a->cols; j++)
00295     {
00296       fprintf(checkfile,"   %16.12f", a->p[i][j]);
00297       }
00298     fprintf(checkfile, "\n");
00299   }
00300   fprintf(checkfile, "\n");
00301 }
00302 
00303 
00304 /* ________________________________________________________
00305   | name:       fprint_norm                                |
00306   | description: Prints out the Norm of the change in      |
00307   |     angles.  This has been used mostly to compare the  |
00308   |     psuedo inverse against the FSP.                    |
00309   | inputs:  Datafile, change in angles                    |
00310   | outputs: The datafile contains the least norm.         |
00311   |________________________________________________________|
00312   | Authors:     Date:     Modifications:                  |
00313   | D Carlson    2/92      Created                         |
00314   | c hacker    10/94      Removed Squarroot function      |
00315   |              3/95      took struct from "struct MATRIX"|
00316   |________________________________________________________| */
00317 void fprint_norm(outfile,dq,datafp)
00318 MATRIX *dq;
00319 FILE   *outfile, *datafp;
00320 {
00321   int i;
00322   double temp, result;
00323 
00324 /*
00325 fprintf(stderr, "\ntemp: %8.4f  ", temp);
00326 fprintf(stderr, "temp^2: %8.4f   ", temp);  
00327 fprintf(stderr, "result: %8.4f", result);  
00328 */
00329 
00330   for (i = 0, result = 0.0; i < dq->rows; i++)
00331   {
00332     temp    = (dq->p[i][0]);
00333     temp   *= temp;
00334     result += sqrt((temp * temp));
00335   }
00336 
00337   if (DEBUG) fprintf(datafp, "\n NORM value: %lf", result);
00338 
00339   fprintf (outfile, " %14.10f\n", result);
00340 }
00341 
00342 
00343 /* ________________________________________________________
00344   | name:       fprint_hist                                |
00345   | description: Prints out the the contents of the history|
00346   | inputs:  history                                       |
00347   | outputs:                                               |
00348   |________________________________________________________|
00349   | Authors:     Date:     Modifications:                  |
00350   | K Morgansen   8/95      Created                        |
00351   |________________________________________________________| */
00352 
00353 void fprint_hist(outfile, Old_DQs)
00354 FILE *outfile;
00355 History *Old_DQs;
00356 {
00357   int i, j;
00358 
00359   fprintf(outfile, "index pointer at:  %d\n", Old_DQs->whereami);
00360   fprintf(outfile, "times:\n");
00361   for (i=0; i<HIST_SIZE; i++)
00362     fprintf(outfile, "  %5.6lf   ", Old_DQs->dq[i].time);
00363   fprintf(outfile, "\n\n");
00364   for (i=0; i<HIST_SIZE; i++)
00365     {
00366     for (j=0; j<Old_DQs->dq[i].DQ->rows; j++)
00367       fprintf(outfile, "%8.6lf  ", Old_DQs->dq[i].DQ->p[j][0]);
00368     fprintf(outfile, "\n");
00369     }
00370   fprintf(outfile, "\t==========================================\n\n");
00371 
00372 }
00373 
00374 
00375 /* ________________________________________________________
00376   | name:       fdq_pr                                     |
00377   | description: Prints out the the contents of dq         |
00378   | inputs:  dq                                            |
00379   | outputs:                                               |
00380   |________________________________________________________|
00381   | Authors:     Date:     Modifications:                  |
00382   | K Morgansen   8/95      Created                        |
00383   |________________________________________________________| */
00384 
00385 void fdq_pr (DQFile, Old_DQs)
00386 FILE *DQFile;
00387 History Old_DQs;
00388 {
00389   int now,
00390       i;
00391 
00392   now = Old_DQs.whereami-1;
00393   if (now == -1) now = HIST_SIZE - 1;
00394   fprintf(DQFile, "%8.6lf  ", Old_DQs.dq[now].time);
00395   for (i=0; i<M; i++)
00396     fprintf(DQFile, "%8.6lf  ", Old_DQs.dq[now].DQ->p[i][0]);
00397   fprintf(DQFile, "\n");
00398 }
00399 

Generated on Thu Jul 29 15:56:35 2004 for OpenSim by doxygen 1.3.6