A Discrete-Event Network Simulator
API
li-ion-energy-source.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/li-ion-energy-source.h"
21
22#include "ns3/command-line.h"
23#include "ns3/energy-source-container.h"
24#include "ns3/log.h"
25#include "ns3/simple-device-energy-model.h"
26#include "ns3/simulator.h"
27
28using namespace ns3;
29
48static void
50{
51 std::cout << "At " << Simulator::Now().As(Time::S)
52 << " Cell voltage: " << es->GetSupplyVoltage()
53 << " V Remaining Capacity: " << es->GetRemainingEnergy() / (3.6 * 3600) << " Ah"
54 << std::endl;
55
56 if (!Simulator::IsFinished())
57 {
58 Simulator::Schedule(Seconds(20), &PrintCellInfo, es);
59 }
60}
61
62int
63main(int argc, char** argv)
64{
65 CommandLine cmd(__FILE__);
66 cmd.Parse(argc, argv);
67
68 // uncomment below to see the energy consumption details
69 // LogComponentEnable ("LiIonEnergySource", LOG_LEVEL_DEBUG);
70
71 Ptr<Node> node = CreateObject<Node>();
72
73 Ptr<SimpleDeviceEnergyModel> sem = CreateObject<SimpleDeviceEnergyModel>();
74 Ptr<EnergySourceContainer> esCont = CreateObject<EnergySourceContainer>();
75 Ptr<LiIonEnergySource> es = CreateObject<LiIonEnergySource>();
76 esCont->Add(es);
77 es->SetNode(node);
78 sem->SetEnergySource(es);
79 es->AppendDeviceEnergyModel(sem);
80 sem->SetNode(node);
81 node->AggregateObject(esCont);
82
83 Time now = Simulator::Now();
84
85 // discharge at 2.33 A for 1700 seconds
86 sem->SetCurrentA(2.33);
87 now += Seconds(1701);
88
89 // discharge at 4.66 A for 628 seconds
90 Simulator::Schedule(now, &SimpleDeviceEnergyModel::SetCurrentA, sem, 4.66);
91 now += Seconds(600);
92
93 PrintCellInfo(es);
94
95 Simulator::Stop(now);
96 Simulator::Run();
97 Simulator::Destroy();
98
99 // the cell voltage should be under 3.3v
100 DoubleValue v;
101 es->GetAttribute("ThresholdVoltage", v);
102 NS_ASSERT(es->GetSupplyVoltage() <= v.Get());
103}
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
void AggregateObject(Ptr< Object > other)
Aggregate two Objects together.
Definition: object.cc:259
Smart pointer class similar to boost::intrusive_ptr.
Definition: ptr.h:78
virtual void SetNode(Ptr< Node > node)
Sets pointer to node.
void SetEnergySource(Ptr< EnergySource > source) override
Sets pointer to EnergySouce installed on node.
Simulation virtual time values and global simulation resolution.
Definition: nstime.h:105
TimeWithUnit As(const enum Unit unit=Time::AUTO) const
Attach a unit to a Time, to facilitate output in a specific unit.
Definition: time.cc:417
static void PrintCellInfo(Ptr< LiIonEnergySource > es)
In this simple example, we show how to create and drain energy from a LiIonEnergySource.
#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 Now()
create an ns3::Time instance which contains the current simulation time.
Definition: simulator.cc:296
Time Seconds(double value)
Construct a Time in the indicated unit.
Definition: nstime.h:1338
Every class exported by the ns3 library is enclosed in the ns3 namespace.
cmd
Definition: second.py:33