A Discrete-Event Network Simulator
API
Loading...
Searching...
No Matches
generic-battery-discharge-example.cc
Go to the documentation of this file.
1/*
2 * Copyright (c) 2023 Tokushima University
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License version 2 as
6 * published by the Free Software Foundation;
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License
14 * along with this program; if not, write to the Free Software
15 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
16 *
17 * Author: Alberto Gallegos Ramonet <alramonet@is.tokushima-u.ac.jp>
18 */
19
20#include <ns3/core-module.h>
21#include <ns3/energy-module.h>
22#include <ns3/gnuplot.h>
23
24#include <fstream>
25#include <sstream>
26#include <string>
27
28using namespace ns3;
29
51Gnuplot battDischPlot1 = Gnuplot("BattDisch1.eps");
53std::ofstream battDischFile1("BattDischCurve1.plt");
54
55Gnuplot battDischPlot2 = Gnuplot("BattDisch2.eps");
57std::ofstream battDischFile2("BattDischCurve2.plt");
58
59Gnuplot battDischPlot3 = Gnuplot("BattDisch3.eps");
61std::ofstream battDischFile3("BattDischCurve3.plt");
62
63Gnuplot battDischPlot4 = Gnuplot("BattDisch4.eps");
65std::ofstream battDischFile4("BattDischCurve4.plt");
66
67Gnuplot battDischPlot5 = Gnuplot("BattDisch5.eps");
69std::ofstream battDischFile5("BattDischCurve5.plt");
70
71void
73{
74 // NiMh battery Panasonic HHR650D NiMH
75 double cellVoltage = es->GetSupplyVoltage();
76 Time currentTime = Simulator::Now();
77 battDischDataset1.Add(currentTime.GetMinutes(), cellVoltage);
78 // battDischDataset1.Add(currentTime.GetHours(), cellVoltage);
79
81 {
83 }
84}
85
86void
88{
89 // CSB GP1272 Lead Acid
90 double cellVoltage = es->GetSupplyVoltage();
91 Time currentTime = Simulator::Now();
92 battDischDataset2.Add(currentTime.GetMinutes(), cellVoltage);
93 // battDischDataset2.Add(currentTime.GetHours(), cellVoltage);
94
96 {
98 }
99}
100
101void
103{
104 // Panasonic CGR18650DA Li-on
105 double cellVoltage = es->GetSupplyVoltage();
106 double dischargeCapacityAh = es->GetDrainedCapacity();
107 battDischDataset3.Add(dischargeCapacityAh * 1000, cellVoltage);
108
110 {
112 }
113}
114
115void
117{
118 // Rs Pro LGP12100 Lead Acid
119 double cellVoltage = es->GetSupplyVoltage();
120 Time currentTime = Simulator::Now();
121 battDischDataset4.Add(currentTime.GetMinutes(), cellVoltage);
122 // battDischDataset4.Add(currentTime.GetHours(), cellVoltage);
123
125 {
127 }
128}
129
130void
132{
133 // Panasonic N-700AAC NiCd
134 double cellVoltage = es->GetSupplyVoltage();
135 Time currentTime = Simulator::Now();
136 // battDischDataset5.Add(currentTime.GetMinutes(), cellVoltage);
137 battDischDataset5.Add(currentTime.GetHours(), cellVoltage);
138
140 {
142 }
143}
144
145int
146main(int argc, char** argv)
147{
148 CommandLine cmd(__FILE__);
149 cmd.Parse(argc, argv);
150
151 LogComponentEnable("GenericBatteryModel", LOG_LEVEL_DEBUG);
152
153 Ptr<Node> node;
154 GenericBatteryModelHelper batteryHelper;
155 Ptr<GenericBatteryModel> batteryModel;
156 Ptr<SimpleDeviceEnergyModel> devicesEnergyModel;
157
159
160 // Discharge 6.5A (1C)
161 battDischDataset1 = Gnuplot2dDataset("Panasonic NiMH HHR650D 6.5 A (1C)");
162
163 node = CreateObject<Node>();
164 devicesEnergyModel = CreateObject<SimpleDeviceEnergyModel>();
165 batteryModel = CreateObject<GenericBatteryModel>();
166
167 batteryModel->SetAttribute("FullVoltage", DoubleValue(1.39)); // Vfull
168 batteryModel->SetAttribute("MaxCapacity", DoubleValue(7.0)); // Q
169
170 batteryModel->SetAttribute("NominalVoltage", DoubleValue(1.18)); // Vnom
171 batteryModel->SetAttribute("NominalCapacity", DoubleValue(6.25)); // QNom
172
173 batteryModel->SetAttribute("ExponentialVoltage", DoubleValue(1.28)); // Vexp
174 batteryModel->SetAttribute("ExponentialCapacity", DoubleValue(1.3)); // Qexp
175
176 batteryModel->SetAttribute("InternalResistance", DoubleValue(0.0046)); // R
177 batteryModel->SetAttribute("TypicalDischargeCurrent", DoubleValue(1.3)); // i typical
178 batteryModel->SetAttribute("CutoffVoltage", DoubleValue(1.0)); // End of charge.
179
180 // Capacity Ah(qMax) * (Vfull) voltage * 3600 = (7 * 1.39 * 3.6) = 35028
181 batteryModel->SetAttribute("BatteryType", EnumValue(NIMH_NICD)); // Battery type
182
183 // The Generic battery model allow users to simulate different types of
184 // batteries based on some parameters. However, presets of batteries are
185 // included in ns-3, for example, the previous battery values can be
186 // configured using a helper to set a NiMh battery preset:
187
188 // batteryModel = DynamicCast<GenericBatteryModel>
189 // (batteryHelper.Install(node,PANASONIC_HHR650D_NIMH));
190
191 devicesEnergyModel->SetEnergySource(batteryModel);
192 batteryModel->AppendDeviceEnergyModel(devicesEnergyModel);
193 devicesEnergyModel->SetNode(node);
194
195 devicesEnergyModel->SetCurrentA(6.5);
196
197 GraphBattery1(batteryModel);
198
200 // 18717 secs around 5.3hrs, 750secs for 32.5 current, or (4200 70 mins)
204
205 // Discharge 13A (2C)
206 battDischDataset1 = Gnuplot2dDataset("Panasonic NiMH HHR650D 13 A (2C)");
207 node = CreateObject<Node>();
208 batteryModel =
209 DynamicCast<GenericBatteryModel>(batteryHelper.Install(node, PANASONIC_HHR650D_NIMH));
210
211 devicesEnergyModel = CreateObject<SimpleDeviceEnergyModel>();
212 devicesEnergyModel->SetEnergySource(batteryModel);
213 batteryModel->AppendDeviceEnergyModel(devicesEnergyModel);
214 devicesEnergyModel->SetNode(node);
215
216 devicesEnergyModel->SetCurrentA(13);
217
218 GraphBattery1(batteryModel);
219
224
225 // Discharge 32.5A (5C)
226 battDischDataset1 = Gnuplot2dDataset("Panasonic NiMH HHR650D 32.5 A (5C)");
227 node = CreateObject<Node>();
228 batteryModel = CreateObject<GenericBatteryModel>();
229 batteryModel =
230 DynamicCast<GenericBatteryModel>(batteryHelper.Install(node, PANASONIC_HHR650D_NIMH));
231
232 devicesEnergyModel = CreateObject<SimpleDeviceEnergyModel>();
233 devicesEnergyModel->SetEnergySource(batteryModel);
234 batteryModel->AppendDeviceEnergyModel(devicesEnergyModel);
235 devicesEnergyModel->SetNode(node);
236
237 devicesEnergyModel->SetCurrentA(32.5);
238
239 GraphBattery1(batteryModel);
241
245
246 battDischPlot1.SetTerminal("postscript eps color enh \"Times-BoldItalic\"");
247 battDischPlot1.SetLegend(" Time (minutes)", "Voltage (V)");
248 battDischPlot1.SetExtra("set xrange[0:70]\n\
249 set yrange [0.8:1.8]\n\
250 set xtics 10\n\
251 set ytics 0.1\n\
252 set grid\n\
253 set style line 1 linewidth 5\n\
254 set style line 2 linewidth 5\n\
255 set style line 3 linewidth 5\n\
256 set style line 4 linewidth 5\n\
257 set style line 5 linewidth 5\n\
258 set style line 6 linewidth 5\n\
259 set style line 7 linewidth 5\n\
260 set style line 8 linewidth 5\n\
261 set style increment user\n\
262 set key reverse Left");
263
265 battDischFile1.close();
266 std::cout << "The end, plotting now\n";
267
269
270 // Discharge 0.36A (0.05C)
271 battDischDataset2 = Gnuplot2dDataset("CSB GP1272 0.36 A (0.05C)");
272 node = CreateObject<Node>();
273 batteryModel =
274 DynamicCast<GenericBatteryModel>(batteryHelper.Install(node, CSB_GP1272_LEADACID));
275
276 devicesEnergyModel = CreateObject<SimpleDeviceEnergyModel>();
277 devicesEnergyModel->SetEnergySource(batteryModel);
278 batteryModel->AppendDeviceEnergyModel(devicesEnergyModel);
279 devicesEnergyModel->SetNode(node);
280
281 devicesEnergyModel->SetCurrentA(0.36);
282
283 GraphBattery2(batteryModel);
285
286 Simulator::Stop(Seconds(55000));
289
290 // Discharge 0.648A (0.09C)
291 battDischDataset2 = Gnuplot2dDataset("CSB GP1272 0.648 A (0.09C)");
292 node = CreateObject<Node>();
293 batteryModel =
294 DynamicCast<GenericBatteryModel>(batteryHelper.Install(node, CSB_GP1272_LEADACID));
295
296 devicesEnergyModel = CreateObject<SimpleDeviceEnergyModel>();
297 devicesEnergyModel->SetEnergySource(batteryModel);
298 batteryModel->AppendDeviceEnergyModel(devicesEnergyModel);
299 devicesEnergyModel->SetNode(node);
300
301 devicesEnergyModel->SetCurrentA(0.648);
302
303 GraphBattery2(batteryModel);
305
306 Simulator::Stop(Seconds(30000));
309
310 battDischPlot2.SetTerminal("postscript eps color enh \"Times-BoldItalic\"");
311 battDischPlot2.SetLegend(" Time (Minutes)", "Voltage (V)");
312 battDischPlot2.SetExtra("set xrange[1:1800]\n\
313 set yrange [7:14]\n\
314 set logscale x \n\
315 set tics scale 3\n\
316 set xtics (1,2,3,5,10,20,30,60,120,180,300,600,1200,1800)\n\
317 set ytics (0,8,9,10,11,12,13,14)\n\
318 set grid\n\
319 set style line 1 linewidth 5\n\
320 set style line 2 linewidth 5\n\
321 set style line 3 linewidth 5\n\
322 set style line 4 linewidth 5\n\
323 set style line 5 linewidth 5\n\
324 set style line 6 linewidth 5\n\
325 set style line 7 linewidth 5\n\
326 set style line 8 linewidth 5\n\
327 set style increment user\n\
328 set key reverse Left");
330 battDischFile2.close();
331 std::cout << "The end, plotting now\n";
332
334
335 // Discharge 0.466A (0.2C)
336 battDischDataset3 = Gnuplot2dDataset("Panasonic Li-on CGR18650DA 0.466 A (0.2C)");
337 node = CreateObject<Node>();
338 batteryModel =
339 DynamicCast<GenericBatteryModel>(batteryHelper.Install(node, PANASONIC_CGR18650DA_LION));
340
341 devicesEnergyModel = CreateObject<SimpleDeviceEnergyModel>();
342 devicesEnergyModel->SetEnergySource(batteryModel);
343 batteryModel->AppendDeviceEnergyModel(devicesEnergyModel);
344 devicesEnergyModel->SetNode(node);
345
346 devicesEnergyModel->SetCurrentA(0.466);
347
348 GraphBattery3(batteryModel);
350
351 Simulator::Stop(Seconds(17720));
354
355 // Discharge 2.33A (1C)
356 battDischDataset3 = Gnuplot2dDataset("Panasonic Li-on CGR18650DA 2.33 A (1C)");
357 node = CreateObject<Node>();
358 batteryModel =
359 DynamicCast<GenericBatteryModel>(batteryHelper.Install(node, PANASONIC_CGR18650DA_LION));
360
361 devicesEnergyModel = CreateObject<SimpleDeviceEnergyModel>();
362 devicesEnergyModel->SetEnergySource(batteryModel);
363 batteryModel->AppendDeviceEnergyModel(devicesEnergyModel);
364 devicesEnergyModel->SetNode(node);
365
366 devicesEnergyModel->SetCurrentA(2.33);
367
368 GraphBattery3(batteryModel);
370
374
375 // Discharge 4.66A (2C)
376 battDischDataset3 = Gnuplot2dDataset("Panasonic Li-on CGR18650DA 4.66 A (2C)");
377 node = CreateObject<Node>();
378 batteryModel =
379 DynamicCast<GenericBatteryModel>(batteryHelper.Install(node, PANASONIC_CGR18650DA_LION));
380
381 devicesEnergyModel = CreateObject<SimpleDeviceEnergyModel>();
382 devicesEnergyModel->SetEnergySource(batteryModel);
383 batteryModel->AppendDeviceEnergyModel(devicesEnergyModel);
384 devicesEnergyModel->SetNode(node);
385
386 devicesEnergyModel->SetCurrentA(4.66);
387
388 GraphBattery3(batteryModel);
390
394
395 battDischPlot3.SetTerminal("postscript eps color enh \"Times-BoldItalic\"");
396 battDischPlot3.SetLegend(" Discharge Capacity (mAh)", "Voltage (V)");
397 battDischPlot3.SetExtra("set xrange[0:2400]\n\
398 set yrange [2.6:4.4]\n\
399 set xtics 400\n\
400 set ytics 0.2\n\
401 set grid\n\
402 set style line 1 linewidth 5\n\
403 set style line 2 linewidth 5\n\
404 set style line 3 linewidth 5\n\
405 set style line 4 linewidth 5\n\
406 set style line 5 linewidth 5\n\
407 set style line 6 linewidth 5\n\
408 set style line 7 linewidth 5\n\
409 set style line 8 linewidth 5\n\
410 set style increment user\n\
411 set key reverse Left");
413 battDischFile3.close();
414 std::cout << "The end, plotting now\n";
415
417
418 // Discharge 0.36A (0.05C)
419 battDischDataset4 = Gnuplot2dDataset("Rs PRO LGP12100 5A (0.05C)");
420 node = CreateObject<Node>();
421 batteryModel =
422 DynamicCast<GenericBatteryModel>(batteryHelper.Install(node, RSPRO_LGP12100_LEADACID));
423
424 devicesEnergyModel = CreateObject<SimpleDeviceEnergyModel>();
425 devicesEnergyModel->SetEnergySource(batteryModel);
426 batteryModel->AppendDeviceEnergyModel(devicesEnergyModel);
427 devicesEnergyModel->SetNode(node);
428
429 devicesEnergyModel->SetCurrentA(5);
430
431 GraphBattery4(batteryModel);
433
434 Simulator::Stop(Seconds(65000));
437
438 // Discharge 100A (1C)
439 battDischDataset4 = Gnuplot2dDataset("Rs PRO LGP12100 100A (1C)");
440 node = CreateObject<Node>();
441 batteryModel =
442 DynamicCast<GenericBatteryModel>(batteryHelper.Install(node, RSPRO_LGP12100_LEADACID));
443
444 devicesEnergyModel = CreateObject<SimpleDeviceEnergyModel>();
445 devicesEnergyModel->SetEnergySource(batteryModel);
446 batteryModel->AppendDeviceEnergyModel(devicesEnergyModel);
447 devicesEnergyModel->SetNode(node);
448
449 devicesEnergyModel->SetCurrentA(100);
450
451 GraphBattery4(batteryModel);
453
457
458 battDischPlot4.SetTerminal("postscript eps color enh \"Times-BoldItalic\"");
459 battDischPlot4.SetLegend(" Time (Minutes)", "Voltage (V)");
460 battDischPlot4.SetExtra("set xrange[1:1800]\n\
461 set yrange [7:13]\n\
462 set logscale \n\
463 set tics scale 3\n\
464 set xtics (1,2,4,6,8,10,20,40,60,120,240,360,480,600,1200)\n\
465 set ytics (7,8,9,10,11,12,13)\n\
466 set grid\n\
467 set style line 1 linewidth 5\n\
468 set style line 2 linewidth 5\n\
469 set style line 3 linewidth 5\n\
470 set style line 4 linewidth 5\n\
471 set style line 5 linewidth 5\n\
472 set style line 6 linewidth 5\n\
473 set style line 7 linewidth 5\n\
474 set style line 8 linewidth 5\n\
475 set style increment user\n\
476 set key reverse Left");
478 battDischFile4.close();
479 std::cout << "The end, plotting now\n";
480
482
483 // Discharge 0.7A (0.1C)
484 battDischDataset5 = Gnuplot2dDataset("Panasonic N-700AAC 0.7A (0.01C)");
485 node = CreateObject<Node>();
486 batteryModel =
487 DynamicCast<GenericBatteryModel>(batteryHelper.Install(node, PANASONIC_N700AAC_NICD));
488
489 devicesEnergyModel = CreateObject<SimpleDeviceEnergyModel>();
490 devicesEnergyModel->SetEnergySource(batteryModel);
491 batteryModel->AppendDeviceEnergyModel(devicesEnergyModel);
492 devicesEnergyModel->SetNode(node);
493
494 devicesEnergyModel->SetCurrentA(0.07);
495
496 GraphBattery5(batteryModel);
498
499 Simulator::Stop(Seconds(38500));
502
503 // Discharge 0.14A (0.2C)
504 battDischDataset5 = Gnuplot2dDataset("Panasonic N-700AAC 0.14A (0.2C)");
505 node = CreateObject<Node>();
506 batteryModel =
507 DynamicCast<GenericBatteryModel>(batteryHelper.Install(node, PANASONIC_N700AAC_NICD));
508
509 devicesEnergyModel = CreateObject<SimpleDeviceEnergyModel>();
510 devicesEnergyModel->SetEnergySource(batteryModel);
511 batteryModel->AppendDeviceEnergyModel(devicesEnergyModel);
512 devicesEnergyModel->SetNode(node);
513
514 devicesEnergyModel->SetCurrentA(0.14);
515
516 GraphBattery5(batteryModel);
518
519 Simulator::Stop(Seconds(19200));
522
523 // Discharge 0.35A (0.5C)
524 battDischDataset5 = Gnuplot2dDataset("Panasonic N-700AAC 0.35A (0.5C)");
525 node = CreateObject<Node>();
526 batteryModel =
527 DynamicCast<GenericBatteryModel>(batteryHelper.Install(node, PANASONIC_N700AAC_NICD));
528
529 devicesEnergyModel = CreateObject<SimpleDeviceEnergyModel>();
530 devicesEnergyModel->SetEnergySource(batteryModel);
531 batteryModel->AppendDeviceEnergyModel(devicesEnergyModel);
532 devicesEnergyModel->SetNode(node);
533
534 devicesEnergyModel->SetCurrentA(0.35);
535
536 GraphBattery5(batteryModel);
538
542
543 battDischPlot5.SetTerminal("postscript eps color enh \"Times-BoldItalic\"");
544 battDischPlot5.SetLegend(" Time (Hours)", "Voltage (V)");
545 battDischPlot5.SetExtra("set xrange[0:16]\n\
546 set yrange [0.7:1.5]\n\
547 set tics scale 3\n\
548 set xtics 2\n\
549 set ytics 0.1\n\
550 set grid\n\
551 set style line 1 linewidth 5\n\
552 set style line 2 linewidth 5\n\
553 set style line 3 linewidth 5\n\
554 set style line 4 linewidth 5\n\
555 set style line 5 linewidth 5\n\
556 set style line 6 linewidth 5\n\
557 set style line 7 linewidth 5\n\
558 set style line 8 linewidth 5\n\
559 set style increment user\n\
560 set key reverse Left");
562 battDischFile5.close();
563 std::cout << "The end, plotting now\n";
564 return 0;
565}
Parse command-line arguments.
Definition: command-line.h:232
This class can be used to hold variables of floating point type such as 'double' or 'float'.
Definition: double.h:42
Hold variables of type enum.
Definition: enum.h:56
Creates and assign an assortment of BatteryModels to Nodes.
Ptr< EnergySourceContainer > Install(NodeContainer c) const
This function installs energy sources in a group of nodes in a node container.
Class to represent a 2D points plot.
Definition: gnuplot.h:116
void Add(double x, double y)
Definition: gnuplot.cc:377
a simple class to generate gnuplot-ready plotting commands from a set of datasets.
Definition: gnuplot.h:370
void AddDataset(const GnuplotDataset &dataset)
Definition: gnuplot.cc:796
void SetLegend(const std::string &xLegend, const std::string &yLegend)
Definition: gnuplot.cc:776
void SetTerminal(const std::string &terminal)
Definition: gnuplot.cc:764
void GenerateOutput(std::ostream &os)
Writes gnuplot commands and data values to a single output stream.
Definition: gnuplot.cc:802
void SetExtra(const std::string &extra)
Definition: gnuplot.cc:783
Smart pointer class similar to boost::intrusive_ptr.
Definition: ptr.h:78
static EventId Schedule(const Time &delay, FUNC f, Ts &&... args)
Schedule an event to expire after delay.
Definition: simulator.h:558
static void Destroy()
Execute the events scheduled with ScheduleDestroy().
Definition: simulator.cc:140
static bool IsFinished()
Check if the simulation should finish.
Definition: simulator.cc:169
static Time Now()
Return the current simulation virtual time.
Definition: simulator.cc:199
static void Run()
Run the simulation.
Definition: simulator.cc:176
static void Stop()
Tell the Simulator the calling event should be the last one executed.
Definition: simulator.cc:184
Simulation virtual time values and global simulation resolution.
Definition: nstime.h:105
double GetMinutes() const
Get an approximation of the time stored in this instance in the indicated unit.
Definition: nstime.h:397
double GetHours() const
Get an approximation of the time stored in this instance in the indicated unit.
Definition: nstime.h:392
std::ofstream battDischFile1("BattDischCurve1.plt")
Gnuplot2dDataset battDischDataset2
void GraphBattery1(Ptr< GenericBatteryModel > es)
std::ofstream battDischFile3("BattDischCurve3.plt")
void GraphBattery4(Ptr< GenericBatteryModel > es)
std::ofstream battDischFile5("BattDischCurve5.plt")
void GraphBattery2(Ptr< GenericBatteryModel > es)
std::ofstream battDischFile4("BattDischCurve4.plt")
Gnuplot battDischPlot1
This example shows the use of batteries in ns-3.
Gnuplot2dDataset battDischDataset5
void GraphBattery5(Ptr< GenericBatteryModel > es)
Gnuplot2dDataset battDischDataset1
void GraphBattery3(Ptr< GenericBatteryModel > es)
Gnuplot2dDataset battDischDataset3
Gnuplot2dDataset battDischDataset4
std::ofstream battDischFile2("BattDischCurve2.plt")
@ NIMH_NICD
Nickel-metal hydride and Nickel cadmium batteries.
@ PANASONIC_CGR18650DA_LION
Panasonic CGR18650DA Li-Ion battery.
@ CSB_GP1272_LEADACID
CSB GP1272 Lead acid battery.
@ PANASONIC_HHR650D_NIMH
Panasonic HHR650D NiMh battery.
@ RSPRO_LGP12100_LEADACID
RS Pro LGP12100 Lead acid battery.
@ PANASONIC_N700AAC_NICD
Panasonic N700AAC NiCd battery.
Time Seconds(double value)
Construct a Time in the indicated unit.
Definition: nstime.h:1325
Every class exported by the ns3 library is enclosed in the ns3 namespace.
void LogComponentEnable(const std::string &name, LogLevel level)
Enable the logging output associated with that log component.
Definition: log.cc:302
@ LOG_LEVEL_DEBUG
LOG_DEBUG and above.
Definition: log.h:113
ns cmd
Definition: second.py:33