global.h

Go to the documentation of this file.
00001  /*********************************************************
00002  *
00003  * This source code is part of the Carnegie Mellon Robot
00004  * Navigation Toolkit (CARMEN)
00005  *
00006  * CARMEN Copyright (c) 2002 Michael Montemerlo, Nicholas
00007  * Roy, and Sebastian Thrun
00008  *
00009  * CARMEN is free software; you can redistribute it and/or 
00010  * modify it under the terms of the GNU General Public 
00011  * License as published by the Free Software Foundation; 
00012  * either version 2 of the License, or (at your option)
00013  * any later version.
00014  *
00015  * CARMEN is distributed in the hope that it will be useful,
00016  * but WITHOUT ANY WARRANTY; without even the implied 
00017  * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
00018  * PURPOSE.  See the GNU General Public License for more 
00019  * details.
00020  *
00021  * You should have received a copy of the GNU General 
00022  * Public License along with CARMEN; if not, write to the
00023  * Free Software Foundation, Inc., 59 Temple Place, 
00024  * Suite 330, Boston, MA  02111-1307 USA
00025  *
00026  ********************************************************/
00027 
00029 // @{
00030 
00038 #ifndef CARMEN_GLOBAL_H
00039 #define CARMEN_GLOBAL_H
00040 
00041 #ifdef __cplusplus
00042 extern "C" {
00043 #endif
00044 
00045 #include <carmen/ipc_wrapper.h>
00046 
00047 #define CARMEN_MAJOR_VERSION 0
00048 #define CARMEN_MINOR_VERSION 4
00049 #define CARMEN_REVISION 6
00050 
00051 #ifndef TRUE
00052 #define TRUE 1
00053 #endif
00054 #ifndef FALSE
00055 #define FALSE 0
00056 #endif
00057 
00058 #ifndef M_PI
00059 #define M_PI 3.14159265358979323846  /* pi */
00060 #endif
00061 
00062 
00063 
00064 /* Useful macros */
00065 
00066 typedef struct {
00067   double x;
00068   double y;
00069   double theta;
00070 } carmen_point_t, *carmen_point_p;
00071 
00072 typedef struct {
00073   double x;
00074   double y;
00075   double theta;
00076   double t_vel;
00077   double r_vel;
00078 } carmen_traj_point_t, *carmen_traj_point_p;
00079 
00080 typedef struct {
00081   double max_r_vel;
00082   double max_t_vel;
00083   double acceleration;
00084   double approach_dist;
00085   double side_dist;
00086   double length;
00087   double width;
00088   double reaction_time;
00089   int allow_rear_motion;
00090   int rectangular;
00091   int interpolate_odometry;
00092 } carmen_robot_config_t;
00093 
00094 typedef enum { CARMEN_MOTOR, CARMEN_SERVO } carmen_arm_joint_t;
00095 
00096 typedef struct {
00097   int X1, Y1;
00098   int X2, Y2;
00099   int Increment;
00100   int UsingYIndex;
00101   int DeltaX, DeltaY;
00102   int DTerm;
00103   int IncrE, IncrNE;
00104   int XIndex, YIndex;
00105   int Flipped;
00106 } carmen_bresenham_param_t;
00107 
00108 typedef void (*carmen_usage_func)(char *fmt, ...);
00109 
00110 typedef struct {
00111   int length;
00112   int capacity;
00113   int entry_size;
00114   void *list;
00115 } carmen_list_t;
00116 
00117 typedef struct {
00118   double timestamp;
00119   char *host;
00120 } carmen_default_message;
00121 
00122 #define CARMEN_DEFAULT_MESSAGE_FMT "{double,string}"
00123 
00124 typedef struct {
00125   char *module_name;
00126   int pid;
00127   double timestamp;
00128   char *hostname;
00129 } carmen_heartbeat_message;
00130   
00131 #define CARMEN_HEARTBEAT_NAME "carmen_heartbeat"
00132 #define CARMEN_HEARTBEAT_FMT "{string, int, double, string}"
00133 
00134 #define carmen_red_code ""
00135 #define carmen_blue_code ""
00136 #define carmen_normal_code ""
00137 
00138 #define carmen_time_code(code, str) { double time_code_t1, time_code_t2; time_code_t1 = carmen_get_time(); code; time_code_t2 = carmen_get_time(); fprintf(stderr, "%-20s : %.2f ms.\n", str, (time_code_t2 - time_code_t1) * 1000.0); }
00139 
00140 void carmen_test_ipc(IPC_RETURN_TYPE err, const char *err_msg, const char *ipc_msg);
00141 
00142 #define carmen_test_alloc(X) do {if ((void *)(X) == NULL) carmen_die("Out of memory in %s, (%s, line %d).\n", __FUNCTION__, __FILE__, __LINE__); } while (0)
00143 
00144 #define carmen_test_ipc_return(ERR, ERR_MSG, IPC_MSG) do {carmen_test_ipc((ERR), (ERR_MSG), (IPC_MSG)); if ((ERR) != IPC_OK) return; } while (0)
00145 
00146 #define carmen_test_ipc_return_int(ERR, ERR_MSG, IPC_MSG) do {carmen_test_ipc((ERR), (ERR_MSG), (IPC_MSG)); if ((ERR) != IPC_OK) return -1; } while (0)
00147 
00148 #define carmen_test_ipc_return_null(ERR, ERR_MSG, IPC_MSG) do {carmen_test_ipc((ERR), (ERR_MSG), (IPC_MSG)); if ((ERR) != IPC_OK) return NULL; } while (0)
00149 
00150 #define carmen_test_ipc_exit(ERR, ERR_MSG, IPC_MSG) do {carmen_test_ipc((ERR), (ERR_MSG), (IPC_MSG)); if ((ERR) != IPC_OK) {fprintf(stderr, "This is a fatal error. Exiting.\n"); exit(-1);} } while (0)
00151 
00152 int carmen_find_param(char *lvalue);
00153 
00154 int carmen_find_param_pair(char *lvalue);
00155 
00156 char *carmen_find_robot_name(int argc, char **argv);
00157 
00158 char *carmen_param_pair(char *lvalue);
00159 
00160 char *carmen_param_pair_and_remove(char *lvalue);
00161 
00162 int carmen_num_params(void);
00163 
00164 char *carmen_get_param_by_num(int param_index);
00165 
00166 int carmen_read_commandline_parameters(int argc, char **argv);
00167 
00168 int carmen_process_param_int(char *lvalue, carmen_usage_func usage, int *return_value); 
00169 
00170 double carmen_process_param_double(char *lvalue, carmen_usage_func usage, double *return_value);
00171 
00172 int carmen_process_param_onoff(char *lvalue, carmen_usage_func usage, int *return_value); 
00173 
00174 char *carmen_process_param_string(char *lvalue, carmen_usage_func usage);
00175 
00176 char *carmen_process_param_file(char *lvalue, carmen_usage_func usage);
00177 
00178 char *carmen_process_param_directory(char *lvalue, carmen_usage_func usage);
00179 
00180 char *carmen_extract_filename(char *path);
00181 
00182 void carmen_perror(char* fmt, ...) __attribute__ ((format (printf, 1, 2)));
00183 void carmen_verbose(char *fmt, ...) __attribute__ ((format (printf, 1, 2)));
00184 void carmen_warn(char* fmt, ...) __attribute__ ((format (printf, 1, 2)));
00185 void carmen_die(char* fmt, ...) __attribute__ ((format (printf, 1, 2)));
00186 void carmen_die_syserror(char* fmt, ...) __attribute__ ((format (printf, 1, 2)));
00187 void carmen_carp_set_verbose(int verbosity);
00188 int carmen_carp_get_verbose(void);
00189 void carmen_carp_set_output(FILE *output);
00190 
00191 extern inline double carmen_get_time(void)
00192 {
00193   struct timeval tv;
00194   double t;
00195 
00196   if (gettimeofday(&tv, NULL) < 0) 
00197     carmen_warn("carmen_get_time encountered error in gettimeofday : %s\n",
00198               strerror(errno));
00199   t = tv.tv_sec + tv.tv_usec/1000000.0;
00200   return t;
00201 }
00202 
00203 char *carmen_get_host(void);
00204 
00205 carmen_default_message *carmen_default_message_create(void);
00206 
00207 void carmen_running_average_clear(int which);
00208 void carmen_running_average_add(int which, double x);
00209 double carmen_running_average_report(int which);
00210 
00211 /* This weirdness of extern inline is to allow the function to be inlined
00212    outside the library. Guess what! There's an exact copy of this function in
00213    global.c as well. <sigh> */
00214 
00215 extern inline int carmen_round(double X)
00216 {
00217   if (X >= 0)
00218     return (int)(X + 0.5);
00219   else
00220     return (int)(X - 0.5);
00221 }
00222 
00223 extern inline double carmen_clamp(double X, double Y, double Z) 
00224 {
00225   if (Y < X)
00226     return X;
00227   else if (Y > Z)
00228     return Z;
00229   return Y;
00230 }
00231 
00232 extern inline int carmen_trunc(double X)
00233 {
00234   return (int)(X);
00235 }
00236 
00237 extern inline void carmen_erase_structure(void* ptr, int size_of_struture)
00238 {
00239   memset(ptr, 0, size_of_struture);
00240 }
00241 
00242 
00243 extern inline double carmen_normalize_theta(double theta)
00244 {
00245   int multiplier;
00246   
00247   if (theta >= -M_PI && theta < M_PI)
00248     return theta;
00249   
00250   multiplier = (int)(theta / (2*M_PI));
00251   theta = theta - multiplier*2*M_PI;
00252   if (theta >= M_PI)
00253     theta -= 2*M_PI;
00254   if (theta < -M_PI)
00255     theta += 2*M_PI;
00256 
00257   return theta;
00258 }
00259 
00260 extern inline double carmen_knots_to_meters_per_second(double knots)
00261 {
00263   return (0.5148 * knots);
00264 }
00265 
00266 extern inline double carmen_radians_to_degrees(double theta)
00267 {
00268   return (theta * 180.0 / M_PI);
00269 }
00270 
00271 extern inline double carmen_degrees_to_radians(double theta)
00272 {
00273   return (theta * M_PI / 180.0);
00274 }
00275 
00276 extern inline double carmen_fmin(double val1, double val2)
00277 {
00278   if (val2 < val1)
00279     return val2;
00280   return val1;
00281 }
00282 
00283 extern inline double carmen_fmax(double val1, double val2)
00284 {
00285   if (val2 > val1)
00286     return val2;
00287   return val1;
00288 }
00289 
00290 extern inline double carmen_square(double val)
00291 {
00292         return (val*val);
00293 }
00294 
00295 extern inline double carmen_distance_traj(carmen_traj_point_p p1, carmen_traj_point_p p2)
00296 {
00297   return sqrt((p1->x-p2->x)*(p1->x-p2->x) + (p1->y-p2->y)*(p1->y-p2->y));
00298 }
00299 
00300 extern inline double carmen_angle_between(carmen_traj_point_p p1, carmen_traj_point_p p2)
00301 {
00302   return atan2(p2->y - p1->y, p2->x - p1->x);
00303 }
00304 
00305 extern inline double carmen_distance(carmen_point_p p1, carmen_point_p p2) 
00306 {
00307   return sqrt((p1->x-p2->x)*(p1->x-p2->x) + (p1->y-p2->y)*(p1->y-p2->y));
00308 }
00309 
00310 void carmen_get_bresenham_parameters(int p1x, int p1y, int p2x, int p2y, 
00311                                    carmen_bresenham_param_t *params);
00312 void carmen_get_current_point(carmen_bresenham_param_t *params, int *x, int *y);
00313 int carmen_get_next_point(carmen_bresenham_param_t *params);
00314 int carmen_sign(double num);
00315 
00316 void carmen_rect_to_polar(double x, double y, double *r, double *theta);
00317 
00318 unsigned int carmen_generate_random_seed(void);
00319 unsigned int carmen_randomize(int *argc, char ***argv);
00320 void carmen_set_random_seed(unsigned int seed);
00321 
00322 int carmen_int_random(int max);
00323 double carmen_uniform_random(double min, double max);
00324 double carmen_gaussian_random(double mean, double std);
00325 
00326 int carmen_file_exists(char *filename);
00327 char *carmen_file_extension(char *filename);
00328 char *carmen_file_find(char *filename);
00329 char **carmen_get_search_path(int *num_paths);
00330 
00331 void carmen_global_start_progess_bar(char *label);
00332 void carmen_global_end_progess_bar(void);  
00333 void carmen_global_update_progess_bar(int count, int size);
00334 
00335 int carmen_strcasecmp (const char *s1, const char *s2);
00336 int carmen_strncasecmp (const char *s1, const char *s2, size_t n);
00337 
00338 char *carmen_new_string(const char *fmt, ...);
00339 char *carmen_new_stringv(const char *fmt, va_list ap);
00340 
00341 void carmen_print_version(void);
00342 
00343 int carmen_parse_sonar_offsets(char *offset_string, carmen_point_p offsets,
00344                                int num_sonars);
00345 
00346 int carmen_parse_arm_joint_types(char *joint_string, carmen_arm_joint_t *joint_types,
00347                                  int num_joints);
00348 
00349 int carmen_terminal_cbreak(int blocking);
00350 int carmen_terminal_restore(void);
00351 
00352 carmen_list_t *carmen_list_create(int entry_size, int initial_capacity);
00353 carmen_list_t *carmen_list_create_from_data(int entry_size, int num_elements, 
00354                                             void *data);
00355 carmen_list_t *carmen_list_duplicate(carmen_list_t *list);
00356 void carmen_list_add(carmen_list_t *list, void *entry);
00357 void carmen_list_insert(carmen_list_t *list, void *entry, int i);
00358 void carmen_list_delete(carmen_list_t *list, int entry_num);
00359 void *carmen_list_get(carmen_list_t *list, int entry_num);
00360 void carmen_list_set(carmen_list_t *list, int entry_num, void *entry);
00361 int carmen_list_length(carmen_list_t *list);
00362 void carmen_list_destroy(carmen_list_t **list);
00363 
00364 void carmen_eigs_to_covariance(double theta, double major, double minor,
00365                                double *vx, double *vxy, double *vy);
00366 
00367 extern inline char *carmen_next_word(char *str)
00368 {
00369   char *mark = str;
00370 
00371   if(str == NULL)
00372     return NULL;
00373   while(*mark != '\0' && !(*mark == ' ' || *mark == '\t'))
00374     mark++;
00375   while(*mark != '\0' &&  (*mark == ' ' || *mark == '\t'))
00376     mark++;
00377   return mark;
00378 }
00379 
00380 extern inline char *carmen_next_n_words(char *str, int n)
00381 {
00382   int i;
00383   char *result;
00384 
00385   result = str;
00386   for(i = 0; i < n; i++)
00387     result = carmen_next_word(result);
00388   return result;
00389 }
00390 
00391 void carmen_publish_heartbeat(char *module_name);
00392 
00393 void
00394 carmen_subscribe_heartbeat_message(carmen_heartbeat_message *heartbeat,
00395                                    carmen_handler_t handler,
00396                                    carmen_subscribe_t subscribe_how);
00397 
00398 #ifdef __cplusplus
00399 }
00400 #endif
00401 
00402 #endif
00403 // @}

Generated on Wed Apr 5 18:34:17 2006 for CARMEN by  doxygen 1.4.4