stage_internal.h
Go to the documentation of this file.00001 #ifndef _STAGE_INTERNAL_H 00002 #define _STAGE_INTERNAL_H 00003 00004 // internal function declarations that are not part of the external 00005 // interface to Stage 00006 00007 00008 #include <assert.h> 00009 #include <pthread.h> 00010 #include <semaphore.h> 00011 #include <math.h> // for lrint() in macros 00012 #include <stdlib.h> 00013 #include <stdio.h> 00014 #include <sys/socket.h> 00015 #include <netdb.h> 00016 #include <string.h> 00017 00018 #include "stage.h" 00019 #include "config.h" // results of autoconf's system configuration tests 00020 #include "replace.h" // Stage's implementations of missing system calls 00021 #include "rtk.h" // and graphics stuff pulled from Andrew Howard's RTK2 library 00022 00023 #if INCLUDE_GNOME 00024 #include "gnome.h" 00025 #endif 00026 00027 00038 #include <glib.h> 00039 00040 #ifdef __cplusplus 00041 extern "C" { 00042 #endif 00043 00045 typedef struct 00046 { 00047 stg_rtk_canvas_t* canvas; 00048 00049 // Gtk stuff 00050 GtkWidget* frame; 00051 GtkWidget *layout; 00052 GtkWidget *menu_bar; 00053 //GtkWidget* scrolled_win; 00054 00055 // The status bar widget 00056 GtkStatusbar *status_bar; 00057 GtkProgressBar *perf_bar; 00058 GtkProgressBar *rt_bar; 00059 GtkLabel *clock_label; 00060 00061 #if INCLUDE_GNOME 00062 // temporary and experimental - will get moved away from here soon - rtv 00063 GnomeCanvas* gcanvas; 00064 double zoom; 00065 #endif 00066 00067 stg_world_t* world; // every window shows a single world 00068 00069 // stg_rtk doesn't support status bars, so we'll use gtk directly 00070 GtkStatusbar* statusbar; 00071 GtkLabel* timelabel; 00072 00073 int wf_section; // worldfile section for load/save 00074 00075 stg_rtk_fig_t* bg; // background 00076 stg_rtk_fig_t* matrix; 00077 stg_rtk_fig_t* matrix_tree; 00078 stg_rtk_fig_t* poses; 00079 00080 gboolean show_matrix; 00081 gboolean fill_polygons; 00082 gboolean show_geom; 00083 gboolean show_polygons; 00084 gboolean show_grid; 00085 00086 int frame_series; 00087 int frame_index; 00088 int frame_callback_tag; 00089 int frame_interval; 00090 int frame_format; 00091 00092 stg_model_t* selection_active; 00093 00094 GList* toggle_list; 00095 00096 } gui_window_t; 00097 00098 void gui_startup( int* argc, char** argv[] ); 00099 void gui_poll( void ); 00100 void gui_shutdown( void ); 00101 00102 void gui_load( gui_window_t* win, int section ); 00103 void gui_save( gui_window_t* win ); 00104 00105 gui_window_t* gui_world_create( stg_world_t* world ); 00106 void gui_world_destroy( stg_world_t* world ); 00107 int gui_world_update( stg_world_t* world ); 00108 void stg_world_add_model( stg_world_t* world, stg_model_t* mod ); 00109 void gui_world_geom( stg_world_t* world ); 00110 00111 void gui_model_create( stg_model_t* model ); 00112 void gui_model_destroy( stg_model_t* model ); 00113 void gui_model_display_pose( stg_model_t* mod, char* verb ); 00114 void gui_model_features( stg_model_t* mod ); 00115 void gui_model_geom( stg_model_t* model ); 00116 void gui_model_mouse(stg_rtk_fig_t *fig, int event, int mode); 00117 void gui_model_nose( stg_model_t* model ); 00118 void gui_window_menus_create( gui_window_t* win ); 00119 void gui_window_menus_destroy( gui_window_t* win ); 00120 00121 // callback functions that handle property changes, mostly for drawing stuff in the GUI 00122 int gui_model_polygons( stg_model_t* mod, void* userp ); 00123 int gui_model_grid( stg_model_t* mod, void* userp ); 00124 int gui_model_move( stg_model_t* mod, void* userp ); 00125 int gui_model_mask( stg_model_t* mod, void* userp ); 00126 int gui_model_lines( stg_model_t* mod, void* userp ); 00127 00128 00129 void gui_add_view_item( const gchar *name, 00130 const gchar *label, 00131 const gchar *tooltip, 00132 GCallback callback, 00133 gboolean is_active, 00134 void* userdata ); 00135 00136 void gui_add_tree_item( stg_model_t* mod ); 00137 00138 00141 typedef struct 00142 { 00143 stg_model_callback_t callback; 00144 void* arg; 00145 } stg_cbarg_t; 00146 00147 // callback functions 00148 //typedef void(*func_init_t)(struct _stg_model*); 00149 typedef int(*func_update_t)(struct _stg_model*); 00150 typedef int(*func_startup_t)(struct _stg_model*); 00151 typedef int(*func_shutdown_t)(struct _stg_model*); 00152 typedef void(*func_load_t)(struct _stg_model*); 00153 typedef void(*func_save_t)(struct _stg_model*); 00154 00155 typedef struct 00156 { 00157 stg_model_t* mod; 00158 void* member; 00159 char* name; 00160 stg_model_callback_t callback_on; 00161 stg_model_callback_t callback_off; 00162 void* arg_on; // argument to callback_on 00163 void* arg_off; // argument to callback_off 00164 //int default_state; // disabled = 0 00165 GtkAction* action; // action associated with this toggle, may be NULL 00166 char* path; 00167 } stg_property_toggle_args_t; 00168 00169 00170 typedef struct { 00171 const char* keyword; 00172 stg_model_initializer_t initializer; 00173 } stg_type_record_t; 00174 00175 struct _stg_model 00176 { 00177 stg_id_t id; // used as hash table key 00178 stg_world_t* world; // pointer to the world in which this model exists 00179 char token[STG_TOKEN_MAX]; // automatically-generated unique ID string 00180 stg_type_record_t* typerec; 00181 00182 struct _stg_model *parent; // the model that owns this one, possibly NULL 00183 00184 GPtrArray* children; // the models owned by this model 00185 00186 // a datalist can contain arbitrary named data items. Can be used 00187 // by derived model types to store properties, and for user code 00188 // to associate arbitrary items with a model. 00189 GData* props; 00190 00191 // a datalist of stg_rtk_figs, indexed by name (string) 00192 GData* figs; 00193 00194 #if INCLUDE_GNOME 00195 GnomeCanvasGroup* grp; 00196 GnomeCanvasGroup* cgrp; 00197 #endif 00198 00199 stg_pose_t pose; 00200 stg_velocity_t velocity; 00201 stg_polygon_t* polygons; 00202 size_t polygons_count; 00203 stg_watts_t watts; //< power consumed by this model 00204 stg_color_t color; 00205 stg_kg_t mass; 00206 stg_geom_t geom; 00207 int laser_return; 00208 int obstacle_return; 00209 int blob_return; 00210 int gripper_return; 00211 int ranger_return; 00212 int fiducial_return; 00213 int fiducial_key; 00214 int boundary; 00215 stg_meters_t map_resolution; 00216 stg_bool_t stall; 00217 00218 int gui_nose; 00219 int gui_grid; 00220 int gui_outline; 00221 int gui_mask; 00222 00223 GHashTable* callbacks; 00224 00225 // the number of children of each type is counted so we can 00226 // automatically generate names for them 00227 int child_type_count[256]; 00228 00229 int subs; // the number of subscriptions to this model 00230 00231 stg_msec_t interval; // time between updates in ms 00232 stg_msec_t interval_elapsed; // time since last update in ms 00233 00234 stg_bool_t disabled; // if non-zero, the model is disabled 00235 00236 // type-dependent functions for this model, implementing simple 00237 // polymorphism 00238 //stg_model_initializer_t initializer; 00239 func_startup_t f_startup; 00240 func_shutdown_t f_shutdown; 00241 func_update_t f_update; 00242 func_load_t f_load; 00243 func_save_t f_save; 00244 00245 void *data, *cmd, *cfg; 00246 size_t data_len, cmd_len, cfg_len; 00247 00248 // XX experimental 00249 stg_polyline_t* lines; 00250 size_t lines_count; 00251 00253 // allow exclusive access to this model 00254 // pthread_mutex_t mutex; 00255 00256 // end experimental 00257 }; 00258 00259 00260 // internal functions 00261 00262 int _model_update( stg_model_t* mod ); 00263 int _model_startup( stg_model_t* mod ); 00264 int _model_shutdown( stg_model_t* mod ); 00265 00266 void model_change( stg_model_t* mod, void* address ); 00267 00268 void stg_model_update_velocity( stg_model_t* model ); 00269 int stg_model_update_pose( stg_model_t* model ); 00270 void stg_model_energy_consume( stg_model_t* mod, stg_watts_t rate ); 00271 void stg_model_map( stg_model_t* mod, gboolean render ); 00272 void stg_model_map_with_children( stg_model_t* mod, gboolean render ); 00273 00274 stg_rtk_fig_t* stg_model_prop_fig_create( stg_model_t* mod, 00275 stg_rtk_fig_t* array[], 00276 stg_id_t propid, 00277 stg_rtk_fig_t* parent, 00278 int layer ); 00279 00280 void stg_model_render_geom( stg_model_t* mod ); 00281 void stg_model_render_pose( stg_model_t* mod ); 00282 void stg_model_render_polygons( stg_model_t* mod ); 00283 00284 int stg_fig_clear_cb( stg_model_t* mod, char* name, 00285 void* data, size_t len, void* userp ); 00286 00287 stg_rtk_fig_t* stg_model_fig_create( stg_model_t* mod, 00288 const char* figname, 00289 const char* parentname, 00290 int layer ); 00291 00295 stg_rtk_fig_t* stg_model_fig_get_or_create( stg_model_t* mod, 00296 const char* figname, 00297 const char* parentname, 00298 int layer ); 00299 00300 stg_rtk_fig_t* stg_model_get_fig( stg_model_t* mod, const char* figname ); 00301 void stg_model_fig_clear( stg_model_t* mod, const char* figname ); 00302 00303 // defines a simulated world 00304 struct _stg_world 00305 { 00306 stg_id_t id; 00307 00308 GHashTable* models; 00309 GHashTable* models_by_name; 00310 00311 stg_meters_t width; 00312 stg_meters_t height; 00313 00317 int child_type_count[256]; 00318 00319 struct _stg_matrix* matrix; 00320 00321 char* token; 00322 00323 stg_msec_t sim_time; 00324 stg_msec_t sim_interval; 00325 00328 stg_msec_t wall_interval; 00329 stg_msec_t wall_last_update; 00330 00331 stg_msec_t gui_interval; 00332 stg_msec_t gui_last_update; 00333 00334 stg_msec_t gui_menu_interval; 00335 stg_msec_t gui_menu_last_update; 00336 00337 00342 stg_msec_t real_interval_measured; 00343 00344 double ppm; 00345 00346 gboolean paused; 00347 00348 gboolean destroy; 00349 00350 gui_window_t* win; 00351 00352 int subs; 00353 }; 00354 00355 // ROTATED RECTANGLES ------------------------------------------------- 00356 00363 typedef struct 00364 { 00365 stg_pose_t pose; 00366 stg_size_t size; 00367 } stg_rotrect_t; // rotated rectangle 00368 00372 void stg_rotrects_normalize( stg_rotrect_t* rects, int num ); 00373 00379 int stg_rotrects_from_image_file( const char* filename, 00380 stg_rotrect_t** rects, 00381 int* rect_count, 00382 int* widthp, int* heightp ); 00383 00384 00388 stg_polygon_t* stg_polygons_from_rotrects( stg_rotrect_t* rects, size_t count, 00389 double width, double height ); 00390 00394 // MATRIX ----------------------------------------------------------------------- 00395 00403 typedef struct stg_cell 00404 { 00405 void* data; 00406 double x, y; 00407 double size; 00408 00409 // bounding box 00410 double xmin,ymin,xmax,ymax; 00411 00412 stg_rtk_fig_t* fig; // for debugging 00413 00414 struct stg_cell* children[4]; 00415 struct stg_cell* parent; 00416 } stg_cell_t; 00417 00420 stg_cell_t* stg_cell_locate( stg_cell_t* cell, double x, double y ); 00421 00422 void stg_cell_unrender( stg_cell_t* cell ); 00423 void stg_cell_render( stg_cell_t* cell ); 00424 void stg_cell_render_tree( stg_cell_t* cell ); 00425 void stg_cell_unrender_tree( stg_cell_t* cell ); 00426 00428 typedef struct _stg_matrix 00429 { 00430 double ppm; // pixels per meter (1/resolution) 00431 double width, height; 00432 00433 // A quad tree of cells. Each leaf node contains a list of 00434 // pointers to objects located at that cell 00435 stg_cell_t* root; 00436 00437 // hash table stores all the pointers to objects rendered in the 00438 // quad tree, each associated with a list of cells in which it is 00439 // rendered. This allows us to remove objects from the tree 00440 // without doing the geometry again 00441 GHashTable* ptable; 00442 00443 /* TODO */ 00444 // lists of cells that have changed recently. This is used by the 00445 // GUI to render cells very quickly, and could also be used by devices 00446 //GSList* cells_changed; 00447 00448 // debug figure. if this is non-NULL, debug info is drawn here 00449 stg_rtk_fig_t* fig; 00450 00451 // todo - record a timestamp for matrix mods so devices can see if 00452 //the matrix has changed since they last peeked into it 00453 // stg_msec_t last_mod_time; 00454 } stg_matrix_t; 00455 00456 00459 stg_matrix_t* stg_matrix_create( double ppm, double width, double height ); 00460 00464 void stg_matrix_destroy( stg_matrix_t* matrix ); 00465 00468 void stg_matrix_clear( stg_matrix_t* matrix ); 00469 00472 void* stg_matrix_cell_get( stg_matrix_t* matrix, int r, double x, double y); 00473 00476 void stg_matrix_cell_append( stg_matrix_t* matrix, 00477 double x, double y, void* object ); 00478 00481 void stg_matrix_remove_obect( stg_matrix_t* matrix, void* object ); 00482 00485 void stg_matrix_cell_remove( stg_matrix_t* matrix, 00486 double x, double y, void* object ); 00487 00491 void stg_matrix_rectangle( stg_matrix_t* matrix, 00492 double px, double py, double pth, 00493 double dx, double dy, 00494 void* object ); 00495 00498 void stg_matrix_line( stg_matrix_t* matrix, 00499 double x1, double y1, 00500 double x2, double y2, 00501 void* object ); 00502 00505 typedef struct 00506 { 00507 stg_meters_t x1, y1, x2, y2; 00508 } stg_line_t; 00509 00510 00513 void stg_matrix_lines( stg_matrix_t* matrix, 00514 stg_line_t* lines, int num_lines, 00515 void* object ); 00516 00519 void stg_matrix_polygons( stg_matrix_t* matrix, 00520 double x, double y, double a, 00521 stg_polygon_t* polys, int num_polys, 00522 void* object ); 00523 00525 void stg_matrix_polylines( stg_matrix_t* matrix, 00526 double x, double y, double a, 00527 stg_polyline_t* polylines, int num_polylines, 00528 void* object ); 00529 00532 void stg_matrix_remove_object( stg_matrix_t* matrix, void* object ); 00533 00534 00535 // RAYTRACE ITERATORS ------------------------------------------------------------- 00536 00537 typedef struct 00538 { 00539 double x, y, a; 00540 double cosa, sina, tana; 00541 double range; 00542 double max_range; 00543 double* incr; 00544 00545 GSList* models; 00546 int index; 00547 stg_matrix_t* matrix; 00548 00549 } itl_t; 00550 00551 typedef enum { PointToPoint=0, PointToBearingRange } itl_mode_t; 00552 00553 typedef int(*stg_itl_test_func_t)(stg_model_t* finder, stg_model_t* found ); 00554 00555 itl_t* itl_create( double x, double y, double a, double b, 00556 stg_matrix_t* matrix, itl_mode_t pmode ); 00557 00558 void itl_destroy( itl_t* itl ); 00559 void itl_raytrace( itl_t* itl ); 00560 00561 stg_model_t* itl_first_matching( itl_t* itl, 00562 stg_itl_test_func_t func, 00563 stg_model_t* finder ); 00564 00572 // C wrappers for C++ worldfile functions 00573 void wf_warn_unused( void ); 00574 int wf_property_exists( int section, char* token ); 00575 int wf_read_int( int section, char* token, int def ); 00576 double wf_read_length( int section, char* token, double def ); 00577 double wf_read_angle( int section, char* token, double def ); 00578 double wf_read_float( int section, char* token, double def ); 00579 int wf_tuple_count( int section, char * token ); 00580 const char* wf_read_tuple_string( int section, char* token, int index, char* def ); 00581 double wf_read_tuple_float( int section, char* token, int index, double def ); 00582 double wf_read_tuple_length( int section, char* token, int index, double def ); 00583 double wf_read_tuple_angle( int section, char* token, int index, double def ); 00584 const char* wf_read_string( int section, char* token, char* def ); 00585 00586 void wf_write_int( int section, char* token, int value ); 00587 void wf_write_length( int section, char* token, double value ); 00588 void wf_write_angle( int section, char* token, double value ); 00589 void wf_write_float( int section, char* token, double value ); 00590 void wf_write_tuple_string( int section, char* token, int index, char* value ); 00591 void wf_write_tuple_float( int section, char* token, int index, double value ); 00592 void wf_write_tuple_length( int section, char* token, int index, double value ); 00593 void wf_write_tuple_angle( int section, char* token, int index, double value ); 00594 void wf_write_string( int section, char* token, char* value ); 00595 00596 void wf_save( void ); 00597 void wf_load( char* path ); 00598 int wf_section_count( void ); 00599 const char* wf_get_section_type( int section ); 00600 int wf_get_parent_section( int section ); 00601 const char* wf_get_filename( void); 00602 00605 // CALLBACK WRAPPERS ------------------------------------------------------------ 00606 00607 // callback wrappers for other functions 00608 void model_update_cb( gpointer key, gpointer value, gpointer user ); 00609 void model_print_cb( gpointer key, gpointer value, gpointer user ); 00610 void model_destroy_cb( gpointer mod ); 00611 00612 00614 // end of libstage_internal documentation 00615 00616 00617 // end documentation group stage 00623 // TODO - some of this needs to be implemented, the rest junked. 00624 00625 /* // -------------------------------------------------------------- */ 00626 00627 /* // standard energy consumption of some devices in W. */ 00628 /* // */ 00629 /* // The MOTIONKG value is a hack to approximate cost of motion, as */ 00630 /* // Stage does not yet have an acceleration model. */ 00631 /* // */ 00632 /* #define STG_ENERGY_COST_LASER 20.0 // 20 Watts! (LMS200 - from SICK web site) */ 00633 /* #define STG_ENERGY_COST_FIDUCIAL 10.0 // 10 Watts */ 00634 /* #define STG_ENERGY_COST_RANGER 0.5 // 500mW (estimate) */ 00635 /* #define STG_ENERGY_COST_MOTIONKG 10.0 // 10 Watts per KG when moving */ 00636 /* #define STG_ENERGY_COST_BLOB 4.0 // 4W (estimate) */ 00637 00638 /* typedef struct */ 00639 /* { */ 00640 /* stg_joules_t joules; // current energy stored in Joules/1000 */ 00641 /* stg_watts_t watts; // current power expenditure in mW (mJoules/sec) */ 00642 /* int charging; // 1 if we are receiving energy, -1 if we are */ 00643 /* // supplying energy, 0 if we are neither charging nor */ 00644 /* // supplying energy. */ 00645 /* stg_meters_t range; // the range that our charging probe hit a charger */ 00646 /* } stg_energy_data_t; */ 00647 00648 /* typedef struct */ 00649 /* { */ 00650 /* stg_joules_t capacity; // maximum energy we can store (we start fully charged) */ 00651 /* stg_meters_t probe_range; // the length of our recharge probe */ 00652 /* //stg_pose_t probe_pose; // TODO - the origin of our probe */ 00653 00654 /* stg_watts_t give_rate; // give this many Watts to a probe that hits me (possibly 0) */ 00655 00656 /* stg_watts_t trickle_rate; // this much energy is consumed or */ 00657 /* // received by this device per second as a */ 00658 /* // baseline trickle. Positive values mean */ 00659 /* // that the device is just burning energy */ 00660 /* // stayting alive, which is appropriate */ 00661 /* // for most devices. Negative values mean */ 00662 /* // that the device is receiving energy */ 00663 /* // from the environment, simulating a */ 00664 /* // solar cell or some other ambient energy */ 00665 /* // collector */ 00666 00667 /* } stg_energy_config_t; */ 00668 00669 00670 /* // BLINKENLIGHT ------------------------------------------------------------ */ 00671 00672 /* // a number of milliseconds, used for example as the blinkenlight interval */ 00673 /* #define STG_LIGHT_ON UINT_MAX */ 00674 /* #define STG_LIGHT_OFF 0 */ 00675 00676 //typedef int stg_interval_ms_t; 00677 00678 00679 /* // TOKEN ----------------------------------------------------------------------- */ 00680 /* // token stuff for parsing worldfiles - this may one day replace 00681 the worldfile c++ code */ 00682 00683 /* #define CFG_OPEN '(' */ 00684 /* #define CFG_CLOSE ')' */ 00685 /* #define STR_OPEN '\"' */ 00686 /* #define STR_CLOSE '\"' */ 00687 /* #define TPL_OPEN '[' */ 00688 /* #define TPL_CLOSE ']' */ 00689 00690 /* typedef enum { */ 00691 /* STG_T_NUM = 0, */ 00692 /* STG_T_BOOLEAN, */ 00693 /* STG_T_MODELPROP, */ 00694 /* STG_T_WORLDPROP, */ 00695 /* STG_T_NAME, */ 00696 /* STG_T_STRING, */ 00697 /* STG_T_KEYWORD, */ 00698 /* STG_T_CFG_OPEN, */ 00699 /* STG_T_CFG_CLOSE, */ 00700 /* STG_T_TPL_OPEN, */ 00701 /* STG_T_TPL_CLOSE, */ 00702 /* } stg_token_type_t; */ 00703 00704 00705 00706 00707 /* typedef struct stg_token */ 00708 /* { */ 00709 /* char* token; ///< the text of the token */ 00710 /* stg_token_type_t type; ///< the type of the token */ 00711 /* unsigned int line; ///< the line on which the token appears */ 00712 00713 /* struct stg_token* next; ///< linked list support */ 00714 /* struct stg_token* child; ///< tree support */ 00715 00716 /* } stg_token_t; */ 00717 00718 /* stg_token_t* stg_token_next( stg_token_t* tokens ); */ 00719 /* /// print [token] formatted for a human reader, with a string [prefix] */ 00720 /* void stg_token_print( char* prefix, stg_token_t* token ); */ 00721 00722 /* /// print a token array suitable for human reader */ 00723 /* void stg_tokens_print( stg_token_t* tokens ); */ 00724 /* void stg_tokens_free( stg_token_t* tokens ); */ 00725 00726 /* /// create a new token structure from the arguments */ 00727 /* stg_token_t* stg_token_create( const char* token, stg_token_type_t type, int line ); */ 00728 00729 /* /// add a token to a list */ 00730 /* stg_token_t* stg_token_append( stg_token_t* head, */ 00731 /* char* token, stg_token_type_t type, int line ); */ 00732 00733 /* const char* stg_token_type_string( stg_token_type_t type ); */ 00734 00735 /* const char* stg_model_type_string( stg_model_type_t type ); */ 00736 00737 /* // functions for parsing worldfiles */ 00738 /* stg_token_t* stg_tokenize( FILE* wf ); */ 00739 /* //stg_world_t* sc_load_worldblock( stg_client_t* cli, stg_token_t** tokensptr ); */ 00740 /* //stg_model_t* sc_load_modelblock( stg_world_t* world, stg_model_t* parent, */ 00741 /* // stg_token_t** tokensptr ); */ 00742 00743 00744 00745 00746 #ifdef __cplusplus 00747 } 00748 #endif 00749 00750 00751 #endif // _STAGE_INTERNAL_H 00752
Generated on Thu Dec 13 14:35:18 2007 for Stage by 1.4.6