Now you can download a copy of these docs so you can use them offline! Download now
BaeUtilities.cpp
00001 /******************************************************************************** 00002 * Project : FIRST Motor Controller 00003 * File Name : BaeUtilities.cpp 00004 * Contributors : JDG, ELF, EMF 00005 * Creation Date : July 20, 2008 00006 * Revision History : Source code & revision history maintained at sourceforge.WPI.edu 00007 * File Description : Open source utility extensions for FIRST Vision API. 00008 */ 00009 /*----------------------------------------------------------------------------*/ 00010 /* Copyright (c) FIRST 2008. All Rights Reserved. */ 00011 /* Open Source Software - may be modified and shared by FRC teams. The code */ 00012 /* must be accompanied by the FIRST BSD license file in $(WIND_BASE)/WPILib. */ 00013 /*----------------------------------------------------------------------------*/ 00014 #include <stdio.h> 00015 #include <sys/types.h> 00016 #include <sys/stat.h> 00017 #include <unistd.h> 00018 #include <string.h> 00019 #include <math.h> 00020 #include <stdioLib.h> 00021 00022 #include "BaeUtilities.h" 00023 #include "Servo.h" 00024 #include "Timer.h" 00025 00034 static DebugOutputType dprintfFlag = DEBUG_OFF; 00035 00040 void SetDebugFlag ( DebugOutputType flag ) 00041 { dprintfFlag = flag; } 00042 00050 void dprintf ( char * tempString, ... ) /* Variable argument list */ 00051 { 00052 va_list args; /* Input argument list */ 00053 int line_number; /* Line number passed in argument */ 00054 int type; 00055 char *functionName; /* Format passed in argument */ 00056 char *fmt; /* Format passed in argument */ 00057 char text[512]; /* Text string */ 00058 char outtext[512]; /* Text string */ 00059 FILE *outfile_fd; /* Output file pointer */ 00060 char filepath[128]; /* Text string */ 00061 int fatalFlag=0; 00062 char *filename; 00063 int index; 00064 int tempStringLen; 00065 00066 if (dprintfFlag == DEBUG_OFF) { return; } 00067 00068 va_start (args, tempString); 00069 00070 tempStringLen = strlen(tempString); 00071 filename = tempString; 00072 for (index=0;index<tempStringLen;index++){ 00073 if (tempString[index] == ' ') { 00074 printf( "ERROR in dprintf: malformed calling sequence (%s)\n",tempString);return; 00075 } 00076 if (tempString[index] == '\\' || tempString[index] == '/') 00077 filename = tempString + index + 1; 00078 } 00079 00080 /* Extract function name */ 00081 functionName = va_arg (args, char *); 00082 00083 /* Extract line number from argument list */ 00084 line_number = va_arg (args, int); 00085 00086 /* Extract information type from argument list */ 00087 type = va_arg (args, int); 00088 00089 /* Extract format from argument list */ 00090 fmt = va_arg (args, char *); 00091 00092 vsprintf (text, fmt, args); 00093 00094 va_end (args); 00095 00096 /* Format output statement */ 00097 switch (type) 00098 { 00099 case DEBUG_TYPE: 00100 sprintf (outtext, "[%s:%s@%04d] DEBUG %s\n", 00101 filename, functionName, line_number, text); 00102 break; 00103 case INFO_TYPE: 00104 sprintf (outtext, "[%s:%s@%04d] INFO %s\n", 00105 filename, functionName, line_number, text); 00106 break; 00107 case ERROR_TYPE: 00108 sprintf (outtext, "[%s:%s@%04d] ERROR %s\n", 00109 filename, functionName, line_number, text); 00110 break; 00111 case CRITICAL_TYPE: 00112 sprintf (outtext, "[%s:%s@%04d] CRITICAL %s\n", 00113 filename, functionName, line_number, text); 00114 break; 00115 case FATAL_TYPE: 00116 fatalFlag = 1; 00117 sprintf (outtext, "[%s:%s@%04d] FATAL %s\n", 00118 filename, functionName, line_number, text); 00119 break; 00120 default: 00121 printf( "ERROR in dprintf: malformed calling sequence\n"); 00122 return; 00123 break; 00124 } 00125 00126 sprintf (filepath, "%s.debug", filename); 00127 00128 /* Write output statement */ 00129 switch (dprintfFlag) 00130 { 00131 default: 00132 case DEBUG_OFF: 00133 break; 00134 case DEBUG_MOSTLY_OFF: 00135 if (fatalFlag) { 00136 if ((outfile_fd = fopen (filepath, "a+")) != NULL) { 00137 fwrite (outtext, sizeof (char), strlen (outtext), outfile_fd); 00138 fclose (outfile_fd); 00139 } 00140 } 00141 break; 00142 case DEBUG_SCREEN_ONLY: 00143 printf ("%s", outtext); 00144 break; 00145 case DEBUG_FILE_ONLY: 00146 if ((outfile_fd = fopen (filepath, "a+")) != NULL) { 00147 fwrite (outtext, sizeof (char), strlen (outtext), outfile_fd); 00148 fclose (outfile_fd); 00149 } 00150 break; 00151 case DEBUG_SCREEN_AND_FILE: // BOTH 00152 printf ("%s", outtext); 00153 if ((outfile_fd = fopen (filepath, "a+")) != NULL) { 00154 fwrite (outtext, sizeof (char), strlen (outtext), outfile_fd); 00155 fclose (outfile_fd); 00156 } 00157 break; 00158 } 00159 } 00160 00167 double RangeToNormalized(double position, int range){ 00168 return(((position*2.0)/(double)range)-1.0); 00169 } 00170 00179 float NormalizeToRange(float normalizedValue, float minRange, float maxRange) { 00180 float range = maxRange-minRange; 00181 float temp = (float)((normalizedValue / 2.0)+ 0.5)*range; 00182 return (temp + minRange); 00183 } 00184 float NormalizeToRange(float normalizedValue) { 00185 return (float)((normalizedValue / 2.0) + 0.5); 00186 } 00187 00193 void ShowActivity (char *fmt, ...) 00194 { 00195 static char activity_indication_string[] = "|/-\\"; 00196 static int ai = 3; 00197 va_list args; 00198 char text[1024]; 00199 00200 va_start (args, fmt); 00201 00202 vsprintf (text, fmt, args); 00203 00204 ai = ai == 3 ? 0 : ai + 1; 00205 00206 printf ("%c %s \r", activity_indication_string[ai], text); 00207 fflush (stdout); 00208 00209 va_end (args); 00210 } 00211 00212 #define PI 3.14159265358979 00213 00222 double SinPosition (double *period, double sinStart) 00223 { 00224 double rtnVal; 00225 static double sinePeriod=0.0; 00226 static double timestamp; 00227 double sinArg; 00228 00229 //1st call 00230 if (period != NULL) { 00231 sinePeriod = *period; 00232 timestamp = GetTime(); 00233 return 0.0; 00234 } 00235 00236 //Multiplying by 2*pi to the time difference makes sinePeriod work if it's measured in seconds. 00237 //Adding sinStart to the part multiplied by PI, but not by 2, allows it to work as described in the comments. 00238 sinArg = PI *((2.0 * (GetTime() - timestamp)) + sinStart) / sinePeriod; 00239 rtnVal = sin (sinArg); 00240 return (rtnVal); 00241 } 00242 00243 00249 double ElapsedTime ( double startTime ) 00250 { 00251 double realTime = GetTime(); 00252 return (realTime-startTime); 00253 } 00254 00259 void panInit() { 00260 double period = 3.0; // number of seconds for one complete pan 00261 SinPosition(&period, 0.0); // initial call to set up time 00262 } 00263 00264 void panInit(double period) { 00265 if (period < 0.0) period=3.0; 00266 SinPosition(&period, 0.0); // initial call to set up time 00267 } 00268 00274 void panForTarget(Servo *panServo) { panForTarget(panServo, 0.0); } 00275 00276 void panForTarget(Servo *panServo, double sinStart) { 00277 float normalizedSinPosition = (float)SinPosition(NULL, sinStart); 00278 float newServoPosition = NormalizeToRange(normalizedSinPosition); 00279 panServo->Set( newServoPosition ); 00280 //ShowActivity ("pan x: normalized %f newServoPosition = %f" , 00281 // normalizedSinPosition, newServoPosition ); 00282 } 00283 00284 00296 int processFile(char *inputFile, char *outputString, int lineNumber) 00297 { 00298 FILE *infile; 00299 int stringSize = 80; // max size of one line in file 00300 char inputStr[stringSize]; 00301 struct stat fileStatus; 00302 int fileSize=0; 00303 int lineCount=0; 00304 00305 if (lineNumber < 0) 00306 return (-1); 00307 00308 if ((infile = fopen (inputFile, "r")) == NULL) { 00309 printf ("Fatal error opening file %s\n",inputFile); 00310 return (0); 00311 } 00312 memset (&fileStatus, 0, sizeof(fileStatus)); 00313 if (!stat(inputFile, &fileStatus)) { 00314 if (S_ISREG(fileStatus.st_mode)) { 00315 fileSize = fileStatus.st_size; 00316 } 00317 } 00318 00319 while (!feof(infile)) { 00320 if (fgets (inputStr, stringSize, infile) != NULL) { 00321 // Skip empty lines 00322 if (emptyString(inputStr)) 00323 continue; 00324 // Skip comment lines 00325 if (inputStr[0] == '#' || inputStr[0] == '!') 00326 continue; 00327 00328 lineCount++; 00329 if (lineNumber == 0) 00330 continue; 00331 else 00332 { 00333 if (lineCount == lineNumber) 00334 break; 00335 } 00336 } 00337 } 00338 00339 // close file 00340 fclose (infile); 00341 // if number lines requested return the count 00342 if (lineNumber == 0) 00343 return (lineCount); 00344 // check for input out of range 00345 if (lineNumber > lineCount) 00346 return (-1); 00347 // return the line selected 00348 if (lineCount) { 00349 stripString(inputStr); 00350 strcpy(outputString, inputStr); 00351 return(lineCount); 00352 } 00353 else { 00354 return(-1); 00355 } 00356 } 00357 00361 int emptyString(char *string) 00362 { 00363 int i,len; 00364 00365 if(string == NULL) 00366 return(1); 00367 00368 len = strlen(string); 00369 for(i=0; i<len; i++) { 00370 // Ignore the following characters 00371 if (string[i] == '\n' || string[i] == '\r' || 00372 string[i] == '\t' || string[i] == ' ') 00373 continue; 00374 return(0); 00375 } 00376 return(1); 00377 } 00378 00382 void stripString(char *string) 00383 { 00384 int i,j,len; 00385 00386 if(string == NULL) 00387 return; 00388 00389 len = strlen(string); 00390 for(i=0,j=0; i<len; i++) { 00391 // Remove the following characters from the string 00392 if (string[i] == '\n' || string[i] == '\r' || string[i] == '\"') 00393 continue; 00394 // Copy anything else 00395 string[j++] = string[i]; 00396 } 00397 string[j] = '\0'; 00398 } 00399 00400
Generated on Thu Jan 12 2012 22:35:17 for WPILibC++ by
1.7.1