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


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 Feb 28 2022 23:48:56