A Discrete-Event Network Simulator
Home
Tutorials ▼
English
Documentation ▼
Installation
Manual
Models
Contributing
Wiki
Development ▼
API Docs
Issue Tracker
Merge Requests
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
16
using namespace
ns3
;
17
using 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
44
NS_WARNING_PUSH_DEPRECATED
;
45
46
static
void
47
PrintCellInfo
(
Ptr<LiIonEnergySource>
es
)
48
{
49
NS_WARNING_POP
;
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
55
if
(!
Simulator::IsFinished
())
56
{
57
Simulator::Schedule
(
Seconds
(20), &
PrintCellInfo
,
es
);
58
}
59
}
60
61
int
62
main(
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
NS_WARNING_PUSH_DEPRECATED
;
75
Ptr<LiIonEnergySource>
es
=
CreateObject<LiIonEnergySource>
();
76
NS_WARNING_POP
;
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
91
Simulator::Schedule
(now, &
SimpleDeviceEnergyModel::SetCurrentA
,
sem
, 4.66);
92
now +=
Seconds
(600);
93
94
PrintCellInfo
(
es
);
95
96
Simulator::Stop
(now);
97
Simulator::Run
();
98
Simulator::Destroy
();
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
}
ns3::CommandLine
Parse command-line arguments.
Definition
command-line.h:221
ns3::DoubleValue
This class can be used to hold variables of floating point type such as 'double' or 'float'.
Definition
double.h:31
ns3::DoubleValue::Get
double Get() const
Definition
double.cc:26
ns3::Ptr
Smart pointer class similar to boost::intrusive_ptr.
Definition
ptr.h:66
ns3::Simulator::Schedule
static EventId Schedule(const Time &delay, FUNC f, Ts &&... args)
Schedule an event to expire after delay.
Definition
simulator.h:560
ns3::Simulator::Destroy
static void Destroy()
Execute the events scheduled with ScheduleDestroy().
Definition
simulator.cc:131
ns3::Simulator::IsFinished
static bool IsFinished()
Check if the simulation should finish.
Definition
simulator.cc:160
ns3::Simulator::Now
static Time Now()
Return the current simulation virtual time.
Definition
simulator.cc:197
ns3::Simulator::Run
static void Run()
Run the simulation.
Definition
simulator.cc:167
ns3::Simulator::Stop
static void Stop()
Tell the Simulator the calling event should be the last one executed.
Definition
simulator.cc:175
ns3::Time
Simulation virtual time values and global simulation resolution.
Definition
nstime.h:94
ns3::Time::As
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
ns3::Time::S
@ S
second
Definition
nstime.h:105
ns3::energy::SimpleDeviceEnergyModel::SetCurrentA
void SetCurrentA(double current)
Definition
simple-device-energy-model.cc:93
NS_ASSERT
#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
ns3::Create
Ptr< T > Create(Ts &&... args)
Create class instances by constructors with varying numbers of arguments and return them by Ptr.
Definition
ptr.h:436
ns3::Seconds
Time Seconds(double value)
Construct a Time in the indicated unit.
Definition
nstime.h:1344
NS_WARNING_POP
#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
NS_WARNING_PUSH_DEPRECATED
In this simple example, we show how to create and drain energy from a LiIonEnergySource.
Definition
li-ion-energy-source-example.cc:44
PrintCellInfo
static void PrintCellInfo(Ptr< LiIonEnergySource > es)
Definition
li-ion-energy-source-example.cc:47
ns3::energy
Definition
energy-harvester-container.cc:18
ns3
Every class exported by the ns3 library is enclosed in the ns3 namespace.
second.cmd
cmd
Definition
second.py:29
src
energy
examples
li-ion-energy-source-example.cc
Generated on Mon Dec 15 2025 15:21:52 for ns-3 by
1.9.8