powerpack.cc
Go to the documentation of this file.
1 
8 #include "stage.hh"
9 #include "texture_manager.hh"
10 using namespace Stg;
11 
12 
17 
19  event_vis( 2.0 * std::max( fabs(ceil(mod->GetWorld()->GetExtent().x.max)),
20  fabs(floor(mod->GetWorld()->GetExtent().x.min))),
21  2.0 * std::max( fabs(ceil(mod->GetWorld()->GetExtent().y.max)),
22  fabs(floor(mod->GetWorld()->GetExtent().y.min))),
23  1.0 ),
24  output_vis( 0,100,200,40, 1200, Color(1,0,0), Color(0,0,0,0.5), "energy output", "energy_input" ),
25  stored_vis( 0,142,200,40, 1200, Color(0,1,0), Color(0,0,0,0.5), "energy stored", "energy_stored" ),
26  mod( mod),
27  stored( 0.0 ),
28  capacity( 0.0 ),
29  charging( false ),
30  dissipated( 0.0 ),
31  last_time(0),
32  last_joules(0.0),
33  last_watts(0.0)
34 {
35  // tell the world about this new pp
36  mod->world->AddPowerPack( this );
37 
38  mod->AddVisualizer( &event_vis, false );
39  mod->AddVisualizer( &output_vis, false );
40  mod->AddVisualizer( &stored_vis, false );
41 }
42 
44 {
45  mod->world->RemovePowerPack( this );
49 }
50 
53 {
54  (void)cam; // avoid warning about unused var
55 
56  const double height = 0.5;
57  const double width = 0.2;
58 
59  double percent = stored/capacity * 100.0;
60 
61  const double alpha = 0.5;
62 
63  if( percent > 50 )
64  glColor4f( 0,1,0, alpha ); // green
65  else if( percent > 25 )
66  glColor4f( 1,0,1, alpha ); // magenta
67  else
68  glColor4f( 1,0,0, alpha ); // red
69 
70  // snprintf( buf, 32, "%.0f", percent );
71 
72  glTranslatef( -width, 0.0, 0.0 );
73 
74  glPolygonMode( GL_FRONT_AND_BACK, GL_FILL );
75 
76  GLfloat fullness = height * (percent * 0.01);
77  glRectf( 0,0,width, fullness);
78 
79  // outline the charge-o-meter
80  glTranslatef( 0,0,0.1 );
81  glPolygonMode( GL_FRONT_AND_BACK, GL_LINE );
82 
83  glColor4f( 0,0,0,0.7 );
84 
85  glRectf( 0,0,width, height );
86 
87  glBegin( GL_LINES );
88  glVertex2f( 0, fullness );
89  glVertex2f( width, fullness );
90  glEnd();
91 
92  if( stored < 0.0 ) // inifinite supply!
93  {
94  // draw an arrow toward the top
95  glBegin( GL_LINES );
96  glVertex2f( width/3.0, height/3.0 );
97  glVertex2f( 2.0 * width/3, height/3.0 );
98 
99  glVertex2f( width/3.0, height/3.0 );
100  glVertex2f( width/3.0, height - height/5.0 );
101 
102  glVertex2f( width/3.0, height - height/5.0 );
103  glVertex2f( 0, height - height/5.0 );
104 
105  glVertex2f( 0, height - height/5.0 );
106  glVertex2f( width/2.0, height );
107 
108  glVertex2f( width/2.0, height );
109  glVertex2f( width, height - height/5.0 );
110 
111  glVertex2f( width, height - height/5.0 );
112  glVertex2f( 2.0 * width/3.0, height - height/5.0 );
113 
114  glVertex2f( 2.0 * width/3.0, height - height/5.0 );
115  glVertex2f( 2.0 * width/3, height/3.0 );
116 
117  glEnd();
118  }
119 
120 
121  if( charging )
122  {
123  glLineWidth( 6.0 );
124  glColor4f( 1,0,0,0.7 );
125 
126  glRectf( 0,0,width, height );
127 
128  glLineWidth( 1.0 );
129  }
130 
131 
132  // compute the instantaneous power output
133  usec_t time_now = mod->world->SimTimeNow();
134  usec_t delta_t = time_now - last_time;
135  watts_t watts = last_watts;
136 
137  if( delta_t > 0 ) // some sim time elapsed
138  {
139  joules_t delta_j = stored - last_joules;
140  watts_t watts = (-1e6 * delta_j) / (double)delta_t;
141 
142  last_joules = stored;
143  last_time = time_now;
144  last_watts = watts;
145  }
146 
147  if( fabs(watts) > 1e-5 ) // any current
148  {
149  glColor4f( 1,0,0,0.8 ); // red
150  char buf[32];
151  snprintf( buf, 32, "%.1fW", watts );
152  Gl::draw_string( -0.05,height+0.05,0, buf );
153  }
154 }
155 
156 
158 {
159  return( capacity - stored );
160 }
161 
163 {
164  joules_t amount = std::min( RemainingCapacity(), j );
165  stored += amount;
166  global_stored += amount;
167 
168  if( amount > 0 ) charging = true;
169 }
170 
172 {
173  if( stored < 0 ) // infinte supply!
174  {
175  global_input += j; // record energy entering the system
176  return;
177  }
178 
179  joules_t amount = std::min( stored, j );
180 
181  stored -= amount;
182  global_stored -= amount;
183 }
184 
186 {
187  //printf( "amount %.2f stored %.2f dest capacity %.2f\n",
188  // amount, stored, dest->RemainingCapacity() );
189 
190  // if stored is non-negative we can't transfer more than the stored
191  // amount. If it is negative, we have infinite energy stored
192  if( stored >= 0.0 )
193  amount = std::min( stored, amount );
194 
195  // we can't transfer more than he can take
196  amount = std::min( amount, dest->RemainingCapacity() );
197 
198  //printf( "%s gives %.3f J to %s\n",
199  // mod->Token(), amount, dest->mod->Token() );
200 
201  Subtract( amount );
202  dest->Add( amount );
203 
204  mod->NeedRedraw();
205 }
206 
207 
209 {
211  capacity = cap;
213 
214  if( stored > cap )
215  {
217  stored = cap;
218  global_stored += stored;
219  }
220 }
221 
223 {
224  return capacity;
225 }
226 
228 {
229  return stored;
230 }
231 
233 {
234  return dissipated;
235 }
236 
238 {
240  stored = j;
241  global_stored += stored;
242 }
243 
245 {
246  joules_t amount = (stored < 0) ? j : std::min( stored, j );
247 
248  Subtract( amount );
249  dissipated += amount;
250  global_dissipated += amount;
251 
252  output_vis.AppendValue( amount );
254 }
255 
256 void PowerPack::Dissipate( joules_t j, const Pose& p )
257 {
258  Dissipate( j );
259  event_vis.Accumulate( p.x, p.y, j );
260 }
261 
262 //------------------------------------------------------------------------------
263 // Dissipation Visualizer class
264 
266 
268  meters_t height,
269  meters_t cellsize )
270  : Visualizer( "energy dissipation", "energy_dissipation" ),
271  columns(width/cellsize),
272  rows(height/cellsize),
273  width(width),
274  height(height),
275  cells( columns*rows ),
276  peak_value(0),
277  cellsize(cellsize)
278 { /* nothing to do */ }
279 
281 {
282 }
283 
285 {
286  (void)cam; // avoid warning about unused var
287 
288  // go into world coordinates
289 
290  glPushMatrix();
291 
293 
294  glTranslatef( -width/2.0, -height/2.0, 0.01 );
295  glScalef( cellsize, cellsize, 1 );
296 
297  for( unsigned int y=0; y<rows; y++ )
298  for( unsigned int x=0; x<columns; x++ )
299  {
300  joules_t j = cells[ y*columns + x ];
301 
302  //printf( "%d %d %.2f\n", x, y, j );
303 
304  if( j > 0 )
305  {
306  glColor4f( 1.0, 0, 0, j/global_peak_value );
307  glRectf( x,y,x+1,y+1 );
308  }
309  }
310 
311  glPopMatrix();
312 }
313 
314 
315 
317  meters_t y,
318  joules_t amount )
319 {
320  //printf( "accumulate %.2f %.2f %.2f\n", x, y, amount );
321 
322  int ix = (x+width/2.0)/cellsize;
323  int iy = (y+height/2.0)/cellsize;
324 
325  // don't accumulate if we're outside the grid
326  if( ix < 0 || ix >= int(columns) || iy < 0 || iy >= int(rows) )
327  return;
328 
329  joules_t& j = cells[ ix + (iy*columns) ];
330 
331  j += amount;
332  if( j > peak_value )
333  {
334  peak_value = j;
335 
338  }
339 }
Model class
Definition: stage.hh:1742
joules_t stored
Definition: stage.hh:1667
static joules_t global_input
Definition: stage.hh:1687
Model * mod
Definition: stage.hh:1664
usec_t SimTimeNow(void) const
Definition: stage.hh:1119
The Stage library uses its own namespace.
Definition: canvas.hh:8
StripPlotVis output_vis
Definition: stage.hh:1660
void Accumulate(meters_t x, meters_t y, joules_t amount)
Definition: powerpack.cc:316
void RemovePowerPack(PowerPack *pp)
Definition: world.cc:1154
static joules_t global_capacity
Definition: stage.hh:1685
void Add(joules_t j)
Definition: powerpack.cc:162
StripPlotVis stored_vis
Definition: stage.hh:1661
static joules_t global_stored
Definition: stage.hh:1684
void Visualize(Camera *cam)
Definition: powerpack.cc:52
void Dissipate(joules_t j)
Definition: powerpack.cc:244
usec_t last_time
Definition: stage.hh:1679
meters_t y
Definition: stage.hh:251
bool charging
Definition: stage.hh:1673
double watts_t
Definition: stage.hh:195
void RemoveVisualizer(Visualizer *custom_visual)
Definition: model_draw.cc:348
joules_t last_joules
Definition: stage.hh:1680
void pose_inverse_shift(const Pose &pose)
Definition: gl.cc:18
std::vector< joules_t > cells
Definition: stage.hh:1641
joules_t GetCapacity() const
Definition: powerpack.cc:222
void Subtract(joules_t j)
Definition: powerpack.cc:171
Pose GetGlobalPose() const
Definition: model.cc:1379
uint64_t usec_t
Definition: stage.hh:186
double meters_t
Definition: stage.hh:174
joules_t dissipated
Definition: stage.hh:1676
virtual void Visualize(Model *mod, Camera *cam)
Definition: powerpack.cc:284
void AddVisualizer(Visualizer *custom_visual, bool on_by_default)
Definition: model_draw.cc:322
void draw_string(float x, float y, float z, const char *string)
Definition: gl.cc:65
void NeedRedraw()
Definition: model.cc:899
void TransferTo(PowerPack *dest, joules_t amount)
Definition: powerpack.cc:185
PowerPack(Model *mod)
Definition: powerpack.cc:18
joules_t GetStored() const
Definition: powerpack.cc:227
static joules_t global_peak_value
Definition: stage.hh:1646
World * world
Definition: stage.hh:2017
void SetCapacity(joules_t j)
Definition: powerpack.cc:208
joules_t RemainingCapacity() const
Definition: powerpack.cc:157
void SetStored(joules_t j)
Definition: powerpack.cc:237
joules_t capacity
Definition: stage.hh:1670
DissipationVis(meters_t width, meters_t height, meters_t cellsize)
Definition: powerpack.cc:267
void AppendValue(float value)
Definition: vis_strip.cc:56
watts_t last_watts
Definition: stage.hh:1681
static joules_t global_dissipated
Definition: stage.hh:1686
double joules_t
Definition: stage.hh:192
joules_t GetDissipated() const
Definition: powerpack.cc:232
meters_t x
Definition: stage.hh:251
Stg::PowerPack::DissipationVis event_vis
void AddPowerPack(PowerPack *pp)
Definition: world.cc:1149


stage
Author(s): Richard Vaughan , Brian Gerkey , Reed Hedges , Andrew Howard , Toby Collett , Pooya Karimian , Jeremy Asher , Alex Couture-Beil , Geoff Biggs , Rich Mattes , Abbas Sadat
autogenerated on Mon Jun 10 2019 15:06:09