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 * SPDX-License-Identifier: GPL-2.0-only
5 *
6 * Author: Andrea Sacco <andrea.sacco85@gmail.com>
7 */
8
9#include "ns3/command-line.h"
10#include "ns3/energy-source-container.h"
11#include "ns3/li-ion-energy-source.h"
12#include "ns3/log.h"
13#include "ns3/simple-device-energy-model.h"
14#include "ns3/simulator.h"
15
16using namespace ns3;
17using namespace ns3::energy;
18
19/**
20 * In this simple example, we show how to create and drain energy from a
21 * LiIonEnergySource.
22 * We make a series of discharge calls to the energy source class with
23 * different current drain and duration until all the energy is depleted
24 * from the cell.
25 *
26 * Every 20 seconds it is printed out the actual cell voltage to verify
27 * that it follows the discharge curve of the datasheet [1].
28 *
29 * At the end of the example it is verified that after the energy depletion
30 * call, the cell voltage is below the threshold voltage.
31 *
32 * References:
33 * [1] Panasonic CGR18650DA Datasheet,
34 * http://www.panasonic.com/industrial/includes/pdf/Panasonic_LiIon_CGR18650DA.pdf
35 */
36
37// NS_DEPRECATED_3_43() - tag for future removal
38// LiIonEnergySource was deprecated in commit
39// https://gitlab.com/nsnam/ns-3-dev/-/commit/086913b0
40//
41// The new battery model is illustrated in
42// `src/energy/examples/generic-battery-discharge-example.cc`
43
45
46static void
48{
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
71
77 esCont->Add(es);
78 es->SetNode(node);
79 sem->SetEnergySource(es);
80 es->AppendDeviceEnergyModel(sem);
81 sem->SetNode(node);
82 node->AggregateObject(esCont);
83
84 Time now = Simulator::Now();
85
86 // discharge at 2.33 A for 1700 seconds
87 sem->SetCurrentA(2.33);
88 now += Seconds(1701);
89
90 // discharge at 4.66 A for 628 seconds
92 now += Seconds(600);
93
94 PrintCellInfo(es);
95
96 Simulator::Stop(now);
99
100 // the cell voltage should be under 3.3v
101 DoubleValue v;
102 es->GetAttribute("ThresholdVoltage", v);
103 NS_ASSERT(es->GetSupplyVoltage() <= v.Get());
104
105 return 0;
106}
Parse command-line arguments.
This class can be used to hold variables of floating point type such as 'double' or 'float'.
Definition double.h:31
double Get() const
Definition double.cc:26
Smart pointer class similar to boost::intrusive_ptr.
static EventId Schedule(const Time &delay, FUNC f, Ts &&... args)
Schedule an event to expire after delay.
Definition simulator.h:560
static void Destroy()
Execute the events scheduled with ScheduleDestroy().
Definition simulator.cc:131
static bool IsFinished()
Check if the simulation should finish.
Definition simulator.cc:160
static Time Now()
Return the current simulation virtual time.
Definition simulator.cc:197
static void Run()
Run the simulation.
Definition simulator.cc:167
static void Stop()
Tell the Simulator the calling event should be the last one executed.
Definition simulator.cc:175
Simulation virtual time values and global simulation resolution.
Definition nstime.h:94
TimeWithUnit As(const Unit unit=Time::AUTO) const
Attach a unit to a Time, to facilitate output in a specific unit.
Definition time.cc:404
@ S
second
Definition nstime.h:105
#define NS_ASSERT(condition)
At runtime, in debugging builds, if this condition is not true, the program prints the source file,...
Definition assert.h:55
Ptr< T > CreateObject(Args &&... args)
Create an object by type, with varying number of constructor parameters.
Definition object.h:619
Time Seconds(double value)
Construct a Time in the indicated unit.
Definition nstime.h:1308
#define NS_WARNING_POP
Pops the diagnostic warning list from the stack, restoring it to the previous state.
Definition warnings.h:102
NS_WARNING_PUSH_DEPRECATED
In this simple example, we show how to create and drain energy from a LiIonEnergySource.
static void PrintCellInfo(Ptr< LiIonEnergySource > es)
Every class exported by the ns3 library is enclosed in the ns3 namespace.