A Discrete-Event Network Simulator
API
Loading...
Searching...
No Matches
li-ion-energy-source-example.cc
Go to the documentation of this file.
1/*
2 * Copyright (c) 2010 Andrea Sacco
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: Andrea Sacco <andrea.sacco85@gmail.com>
18 */
19
20#include "ns3/command-line.h"
21#include "ns3/energy-source-container.h"
22#include "ns3/li-ion-energy-source.h"
23#include "ns3/log.h"
24#include "ns3/simple-device-energy-model.h"
25#include "ns3/simulator.h"
26
27using namespace ns3;
28
29/**
30 * In this simple example, we show how to create and drain energy from a
31 * LiIonEnergySource.
32 * We make a series of discharge calls to the energy source class with
33 * different current drain and duration until all the energy is depleted
34 * from the cell.
35 *
36 * Every 20 seconds it is printed out the actual cell voltage to verify
37 * that it follows the discharge curve of the datasheet [1].
38 *
39 * At the end of the example it is verified that after the energy depletion
40 * call, the cell voltage is below the threshold voltage.
41 *
42 * References:
43 * [1] Panasonic CGR18650DA Datasheet,
44 * http://www.panasonic.com/industrial/includes/pdf/Panasonic_LiIon_CGR18650DA.pdf
45 */
46
47static void
49{
50 std::cout << "At " << Simulator::Now().As(Time::S)
51 << " Cell voltage: " << es->GetSupplyVoltage()
52 << " V Remaining Capacity: " << es->GetRemainingEnergy() / (3.6 * 3600) << " Ah"
53 << std::endl;
54
56 {
58 }
59}
60
61int
62main(int argc, char** argv)
63{
64 CommandLine cmd(__FILE__);
65 cmd.Parse(argc, argv);
66
67 // uncomment below to see the energy consumption details
68 // LogComponentEnable ("LiIonEnergySource", LOG_LEVEL_DEBUG);
69
70 Ptr<Node> node = CreateObject<Node>();
71
72 Ptr<SimpleDeviceEnergyModel> sem = CreateObject<SimpleDeviceEnergyModel>();
73 Ptr<EnergySourceContainer> esCont = CreateObject<EnergySourceContainer>();
74 Ptr<LiIonEnergySource> es = CreateObject<LiIonEnergySource>();
75 esCont->Add(es);
76 es->SetNode(node);
77 sem->SetEnergySource(es);
78 es->AppendDeviceEnergyModel(sem);
79 sem->SetNode(node);
80 node->AggregateObject(esCont);
81
82 Time now = Simulator::Now();
83
84 // discharge at 2.33 A for 1700 seconds
85 sem->SetCurrentA(2.33);
86 now += Seconds(1701);
87
88 // discharge at 4.66 A for 628 seconds
90 now += Seconds(600);
91
92 PrintCellInfo(es);
93
94 Simulator::Stop(now);
97
98 // the cell voltage should be under 3.3v
100 es->GetAttribute("ThresholdVoltage", v);
101 NS_ASSERT(es->GetSupplyVoltage() <= v.Get());
102
103 return 0;
104}
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
double Get() const
Definition: double.cc:37
Smart pointer class similar to boost::intrusive_ptr.
Definition: ptr.h:77
static EventId Schedule(const Time &delay, FUNC f, Ts &&... args)
Schedule an event to expire after delay.
Definition: simulator.h:571
static void Destroy()
Execute the events scheduled with ScheduleDestroy().
Definition: simulator.cc:142
static bool IsFinished()
Check if the simulation should finish.
Definition: simulator.cc:171
static Time Now()
Return the current simulation virtual time.
Definition: simulator.cc:208
static void Run()
Run the simulation.
Definition: simulator.cc:178
static void Stop()
Tell the Simulator the calling event should be the last one executed.
Definition: simulator.cc:186
Simulation virtual time values and global simulation resolution.
Definition: nstime.h:105
TimeWithUnit As(const Unit unit=Time::AUTO) const
Attach a unit to a Time, to facilitate output in a specific unit.
Definition: time.cc:415
@ S
second
Definition: nstime.h:116
#define NS_ASSERT(condition)
At runtime, in debugging builds, if this condition is not true, the program prints the source file,...
Definition: assert.h:66
Time Seconds(double value)
Construct a Time in the indicated unit.
Definition: nstime.h:1326
static void PrintCellInfo(Ptr< LiIonEnergySource > es)
In this simple example, we show how to create and drain energy from a LiIonEnergySource.
Every class exported by the ns3 library is enclosed in the ns3 namespace.
ns cmd
Definition: second.py:40