Revision: 7280 http://playerstage.svn.sourceforge.net/playerstage/?rev=7280&view=rev Author: rtv Date: 2009-01-18 01:56:16 +0000 (Sun, 18 Jan 2009)
Log Message: ----------- added simple charger and visualizations Modified Paths: -------------- code/stage/trunk/libstage/CMakeLists.txt code/stage/trunk/libstage/canvas.cc code/stage/trunk/libstage/model.cc code/stage/trunk/libstage/model.hh code/stage/trunk/libstage/powerpack.cc code/stage/trunk/libstage/stage.hh code/stage/trunk/libstage/world.cc code/stage/trunk/worlds/fasr.world Added Paths: ----------- code/stage/trunk/libstage/charger.cc Modified: code/stage/trunk/libstage/CMakeLists.txt =================================================================== --- code/stage/trunk/libstage/CMakeLists.txt 2009-01-18 00:10:21 UTC (rev 7279) +++ code/stage/trunk/libstage/CMakeLists.txt 2009-01-18 01:56:16 UTC (rev 7280) @@ -8,6 +8,7 @@ blockgroup.cc camera.cc canvas.cc + charger.cc file_manager.cc file_manager.hh gl.cc Modified: code/stage/trunk/libstage/canvas.cc =================================================================== --- code/stage/trunk/libstage/canvas.cc 2009-01-18 00:10:21 UTC (rev 7279) +++ code/stage/trunk/libstage/canvas.cc 2009-01-18 01:56:16 UTC (rev 7280) @@ -898,7 +898,9 @@ for( GList* it=selected_models; it; it=it->next ) ((Model*)it->data)->DrawSelected(); - + for( GList* it=world->chargers; it; it=it->next ) + ((Charger*)it->data)->Visualize(); + // useful debug - puts a point at the origin of each model //for( GList* it = world->World::children; it; it=it->next ) // ((Model*)it->data)->DrawOriginTree(); Added: code/stage/trunk/libstage/charger.cc =================================================================== --- code/stage/trunk/libstage/charger.cc (rev 0) +++ code/stage/trunk/libstage/charger.cc 2009-01-18 01:56:16 UTC (rev 7280) @@ -0,0 +1,115 @@ +/** charger.cc + Simple model of energy storage + Richard Vaughan + Created 2009.1.15 + SVN: $Id: stage.hh 7279 2009-01-18 00:10:21Z rtv $ +*/ + + +#include "stage_internal.hh" + +Charger::Charger( World* world ) + : world( world ), watts( 1000.0 ) +{ + printf( "Charger constructed" ); +} + +void Charger::ChargeIfContained( PowerPack* pp, Pose pose ) +{ + if( Contains( pose ) ) + Charge( pp ); +} + +bool Charger::Contains( Pose pose ) +{ + return( pose.x >= volume.x.min && + pose.x < volume.x.max && + pose.y >= volume.y.min && + pose.y < volume.y.max && + pose.z >= volume.z.min && + pose.z < volume.z.max ); +} + +void Charger::Charge( PowerPack* pp ) +{ + double given = watts * world->interval_sim * 1e-6; + + pp->stored += given; + + // do not exceed capacity + pp->stored = MIN( pp->stored, pp->capacity ); + pp->charging = true; + /* + printf( "charger %p at [%.2f %.2f] [%.2f %.2f] [%.2f %.2f] gave pack %p %.3f joules\n", + this, + volume.x.min, + volume.x.max, + volume.y.min, + volume.y.max, + volume.z.min, + volume.z.max, + pp, given ); + + pp->Print( "just charged" ); + */ +} + +void Charger::Visualize() +{ + + glPushMatrix(); + glPolygonMode( GL_FRONT, GL_FILL ); + glColor4f( 1, 0.5,0,0.4 ); + glTranslatef( 0,0,volume.z.min); + glRectf( volume.x.min, volume.y.min, volume.x.max, volume.y.max ); + + glTranslatef( 0,0,volume.z.max ); + glRectf( volume.x.min, volume.y.min, volume.x.max, volume.y.max ); + glPopMatrix(); + + glPushMatrix(); + glPolygonMode( GL_FRONT, GL_LINE ); + glColor4f( 1, 0.5,0,0.8 ); + glTranslatef( 0,0,volume.z.min); + glRectf( volume.x.min, volume.y.min, volume.x.max, volume.y.max ); + + glTranslatef( 0,0,volume.z.max ); + glRectf( volume.x.min, volume.y.min, volume.x.max, volume.y.max ); + glPopMatrix(); + + // ? + glPolygonMode( GL_FRONT, GL_FILL ); +} + +void swap( double &a, double &b ) +{ + double keep = a; + a = b; + b = keep; +} + +void Charger::Load( Worldfile* wf, int entity ) +{ + if( wf->PropertyExists( entity, "volume" ) ) + { + volume.x.min = wf->ReadTupleLength( entity, "volume", 0, volume.x.min ); + volume.x.max = wf->ReadTupleLength( entity, "volume", 1, volume.x.max ); + volume.y.min = wf->ReadTupleLength( entity, "volume", 2, volume.y.min ); + volume.y.max = wf->ReadTupleLength( entity, "volume", 3, volume.y.max ); + volume.z.min = wf->ReadTupleLength( entity, "volume", 4, volume.z.min ); + volume.z.max = wf->ReadTupleLength( entity, "volume", 5, volume.z.max ); + + // force the windings for GL's sake + if( volume.x.min > volume.x.max ) + swap( volume.x.min, volume.x.max ); + + if( volume.y.min > volume.y.max ) + swap( volume.y.min, volume.y.max ); + + if( volume.z.min > volume.z.max ) + swap( volume.z.min, volume.z.max ); + + } + + watts = wf->ReadFloat( entity, "watts", watts ); +} Modified: code/stage/trunk/libstage/model.cc =================================================================== --- code/stage/trunk/libstage/model.cc 2009-01-18 00:10:21 UTC (rev 7279) +++ code/stage/trunk/libstage/model.cc 2009-01-18 01:56:16 UTC (rev 7280) @@ -659,22 +659,29 @@ // this->world->sim_time, this->token, this->subs ); // f we're drawing current and a power pack has been installed - if( power_pack && (watts > 0) ) + if( power_pack ) { - // consume energy stored in the power pack - stg_joules_t consumed = watts * (world->interval_sim * 1e-6); - power_pack->stored -= consumed; - - /* - printf ( "%s current %.2f consumed %.6f ppack @ %p [ %.2f/%.2f (%.0f)\n", - token, - watts, - consumed, - power_pack, - power_pack->stored, - power_pack->capacity, - power_pack->stored / power_pack->capacity * 100.0 ); - */ + if( watts > 0 ) + { + // consume energy stored in the power pack + stg_joules_t consumed = watts * (world->interval_sim * 1e-6); + power_pack->stored -= consumed; + + /* + printf ( "%s current %.2f consumed %.6f ppack @ %p [ %.2f/%.2f (%.0f)\n", + token, + watts, + consumed, + power_pack, + power_pack->stored, + power_pack->capacity, + power_pack->stored / power_pack->capacity * 100.0 ); + */ + } + + // I own this power pack, see if the world wants to recharge it */ + if( power_pack->mod == this ) + world->TryCharge( power_pack, GetGlobalPose() ); } CallCallbacks( &hooks.update ); Modified: code/stage/trunk/libstage/model.hh =================================================================== --- code/stage/trunk/libstage/model.hh 2009-01-18 00:10:21 UTC (rev 7279) +++ code/stage/trunk/libstage/model.hh 2009-01-18 01:56:16 UTC (rev 7280) @@ -23,6 +23,10 @@ /** OpenGL visualization of the powerpack state */ void Visualize( Camera* cam ); + + /** Print human-readable status on stdout, prefixed with the + argument string */ + void Print( char* prefix ); }; class Visibility Modified: code/stage/trunk/libstage/powerpack.cc =================================================================== --- code/stage/trunk/libstage/powerpack.cc 2009-01-18 00:10:21 UTC (rev 7279) +++ code/stage/trunk/libstage/powerpack.cc 2009-01-18 01:56:16 UTC (rev 7280) @@ -2,23 +2,28 @@ Simple model of energy storage Richard Vaughan Created 2009.1.15 - $Id$ + SVN: $Id: stage.hh 7279 2009-01-18 00:10:21Z rtv $ */ #include "stage_internal.hh" PowerPack::PowerPack( Model* mod ) : - mod( mod), stored( 0.0 ), capacity( 0.0 ) + mod( mod), stored( 0.0 ), capacity( 0.0 ), charging( false ) { // nothing to do }; +void PowerPack::Print( char* prefix ) +{ + printf( "%s stored %.2f/%.2f joules\n", prefix, stored, capacity ); +} + /** OpenGL visualization of the powerpack state */ void PowerPack::Visualize( Camera* cam ) { const double height = 0.5; - const double width = 0.3; + const double width = 0.2; double percent = stored/capacity * 100.0; @@ -42,13 +47,25 @@ // outline the charge-o-meter glTranslatef( 0,0,0.001 ); glPolygonMode( GL_FRONT_AND_BACK, GL_LINE ); + glColor4f( 0,0,0,0.7 ); + glRectf( 0,0,width, height ); - + glBegin( GL_LINES ); glVertex2f( 0, fullness ); glVertex2f( width, fullness ); glEnd(); + + if( charging ) + { + glLineWidth( 6.0 ); + glColor4f( 1,0,0,0.7 ); + + glRectf( 0,0,width, height ); + + glLineWidth( 1.0 ); + } // draw the percentage //gl_draw_string( -0.2, 0, 0, buf ); Modified: code/stage/trunk/libstage/stage.hh =================================================================== --- code/stage/trunk/libstage/stage.hh 2009-01-18 00:10:21 UTC (rev 7279) +++ code/stage/trunk/libstage/stage.hh 2009-01-18 01:56:16 UTC (rev 7280) @@ -809,7 +809,24 @@ class Region; class SuperRegion; class BlockGroup; + class PowerPack; + /// %Charger class + class Charger + { + World* world; + stg_watts_t watts; + stg_bounds3d_t volume; + + public: + Charger( World* world ); + void ChargeIfContained( PowerPack* pp, Pose pose ); + bool Contains( Pose pose ); + void Charge( PowerPack* pp ); + void Visualize(); + void Load( Worldfile* wf, int entity ); + }; + /// %World class class World : public Ancestor { @@ -817,7 +834,8 @@ friend class Block; //friend class StgTime; friend class Canvas; - + friend class Charger; + private: static GList* world_list; ///< all the worlds that exist @@ -825,6 +843,7 @@ static void UpdateCb( World* world); static unsigned int next_id; ///<initially zero, used to allocate unique sequential world ids + GList* chargers; bool destroy; bool dirty; ///< iff true, a gui redraw would be required GHashTable* models_by_name; ///< the models that make up the world, indexed by name @@ -866,6 +885,7 @@ void LoadModel( Worldfile* wf, int entity, GHashTable* entitytable ); void LoadBlock( Worldfile* wf, int entity, GHashTable* entitytable ); void LoadBlockGroup( Worldfile* wf, int entity, GHashTable* entitytable ); + void LoadCharger( Worldfile* wf, int entity ); SuperRegion* AddSuperRegion( const stg_point_int_t& coord ); SuperRegion* GetSuperRegion( const stg_point_int_t& coord ); @@ -982,6 +1002,8 @@ void CancelQuit(){ quit = false; } void CancelQuitAll(){ quit_all = false; } + void TryCharge( PowerPack* pp, Pose pose ); + /** Get the resolution in pixels-per-metre of the underlying discrete raytracing model */ double Resolution(){ return ppm; }; Modified: code/stage/trunk/libstage/world.cc =================================================================== --- code/stage/trunk/libstage/world.cc 2009-01-18 00:10:21 UTC (rev 7279) +++ code/stage/trunk/libstage/world.cc 2009-01-18 01:56:16 UTC (rev 7280) @@ -69,6 +69,7 @@ double ppm ) : // private + chargers( NULL ), destroy( false ), dirty( true ), models_by_name( g_hash_table_new( g_str_hash, g_str_equal ) ), @@ -310,20 +311,22 @@ for( int entity = 1; entity < wf->GetEntityCount(); entity++ ) { const char *typestr = (char*)wf->GetEntityType(entity); - + // don't load window entries here if( strcmp( typestr, "window" ) == 0 ) - { - /* do nothing here */ - } + { + /* do nothing here */ + } //else if( strcmp( typestr, "blockgroup" ) == 0 ) //LoadBlockGroup( wf, entity, entitytable ); else if( strcmp( typestr, "block" ) == 0 ) - LoadBlock( wf, entity, entitytable ); - else - LoadModel( wf, entity, entitytable ); + LoadBlock( wf, entity, entitytable ); + else if( strcmp( typestr, "charger" ) == 0 ) + LoadCharger( wf, entity ); + else + LoadModel( wf, entity, entitytable ); } - + // warn about unused WF lines wf->WarnUnused(); @@ -342,7 +345,16 @@ putchar( '\n' ); } +void World::LoadCharger( Worldfile* wf, int entity ) +{ + Charger* chg = new Charger( this ); + + chargers = g_list_prepend( chargers, chg ); + chg->Load( wf, entity ); +} + + // delete a model from the hash table static void destroy_model( gpointer dummy1, Model* mod, gpointer dummy2 ) { @@ -916,5 +928,16 @@ extent.z.max = MAX( extent.z.max, pt.z ); } +void World::TryCharge( PowerPack* pack, Pose pose ) +{ + pack->charging = false; + // see if the pose lies within any of the charging rectangles + for( GList* it = chargers; it; it = it->next ) + { + Charger* chg = (Charger*)it->data; + chg->ChargeIfContained( pack, pose ); + } +} + Modified: code/stage/trunk/worlds/fasr.world =================================================================== --- code/stage/trunk/worlds/fasr.world 2009-01-18 00:10:21 UTC (rev 7279) +++ code/stage/trunk/worlds/fasr.world 2009-01-18 01:56:16 UTC (rev 7280) @@ -7,7 +7,7 @@ include "sick.inc" interval_sim 100 # simulation timestep in milliseconds -interval_real 00 # real-time interval between simulation updates in milliseconds +interval_real 0 # real-time interval between simulation updates in milliseconds paused 1 resolution 0.02 @@ -20,9 +20,9 @@ # configure the GUI window window ( - size [ 600.000 600.000 ] + size [ 600.000 599.000 ] - center [ -0.521 -0.218 ] + center [ -0.424 -0.218 ] rotate [ 0 0 ] scale 30.883 @@ -45,6 +45,18 @@ bitmap "bitmaps/cave.png" ) +charger +( + volume [ 5 6 -2 -3 0 0.1 ] + watts 200 +) + +charger +( + volume [ 5 6 -4 -5 0 0.1 ] + watts 200 +) + zone ( color "green" @@ -61,12 +73,6 @@ ctrl "sink" ) -#charger -#( -# pose -# -#) - define autorob pioneer2dx ( sicklaser( samples 32 range_max 5 laser_return 2 watts 30 ) @@ -78,9 +84,9 @@ autorob( pose [6.471 5.304 0 14.941] ) autorob( pose [5.937 4.858 0 -147.503] ) autorob( pose [7.574 6.269 0 -111.715] ) -autorob( pose [5.664 5.938 0 -51.799] ) +autorob( pose [5.664 5.938 0 107.666] ) autorob( pose [7.016 6.428 0 -128.279] ) -autorob( pose [5.911 4.040 0 -97.047] ) +autorob( pose [5.750 4.137 0 -97.047] ) autorob( pose [4.909 6.097 0 -44.366] ) autorob( pose [6.898 4.775 0 -117.576] ) autorob( pose [7.012 5.706 0 129.497] ) @@ -88,13 +94,13 @@ autorob( pose [6.616 6.893 0 170.743] ) autorob( pose [6.451 4.189 0 -61.453] ) autorob( pose [5.098 6.788 0 -61.295] ) -autorob( pose [4.374 5.163 0 -90.417] ) -autorob( pose [4.999 4.230 0 -42.157] ) -autorob( pose [4.331 4.217 0 -95.000] ) +autorob( pose [4.374 5.163 0 -147.713] ) +autorob( pose [4.999 4.230 0 -125.236] ) +autorob( pose [4.007 4.249 0 78.789] ) autorob( pose [5.440 5.317 0 -26.545] ) autorob( pose [7.518 6.973 0 163.239] ) autorob( pose [7.559 4.764 0 -139.066] ) -autorob( pose [4.839 3.595 0 -179.567] ) +autorob( pose [5.940 6.768 0 77.301] ) #autorob( pose [7.122 4.175 0 -31.440] ) #autorob( pose [6.203 6.963 0 2.937] ) This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. ------------------------------------------------------------------------------ This SF.net email is sponsored by: SourcForge Community SourceForge wants to tell your story. http://p.sf.net/sfu/sf-spreadtheword _______________________________________________ Playerstage-commit mailing list Playerstage-commit@lists.sourceforge.net https://lists.sourceforge.net/lists/listinfo/playerstage-commit