A Discrete-Event Network Simulator
API
li-ion-energy-source.cc
Go to the documentation of this file.
1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2 /*
3  * Copyright (c) 2010 Andrea Sacco
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License version 2 as
7  * published by the Free Software Foundation;
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17  *
18  * Author: Andrea Sacco <andrea.sacco85@gmail.com>
19  */
20 
21 #include "ns3/log.h"
22 #include "ns3/simulator.h"
23 #include "ns3/command-line.h"
24 #include "ns3/simple-device-energy-model.h"
25 #include "ns3/li-ion-energy-source.h"
26 #include "ns3/energy-source-container.h"
27 
28 using namespace ns3;
29 
47 static void
49 {
50  std::cout << "At " << Simulator::Now ().As (Time::S) << " Cell voltage: " << es->GetSupplyVoltage () << " V Remaining Capacity: " <<
51  es->GetRemainingEnergy () / (3.6 * 3600) << " Ah" << std::endl;
52 
53  if (!Simulator::IsFinished ())
54  {
57  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  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 
89  // discharge at 4.66 A for 628 seconds
92  sem,
93  4.66);
94  now += Seconds (600);
95 
96  PrintCellInfo (es);
97 
98  Simulator::Stop (now);
99  Simulator::Run ();
101 
102  // the cell voltage should be under 3.3v
103  DoubleValue v;
104  es->GetAttribute ("ThresholdVoltage", v);
105  NS_ASSERT (es->GetSupplyVoltage () <= v.Get ());
106 }
ns3::CommandLine
Parse command-line arguments.
Definition: command-line.h:228
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:67
ns3::Simulator::Now
static Time Now(void)
Return the current simulation virtual time.
Definition: simulator.cc:195
ns3
Every class exported by the ns3 library is enclosed in the ns3 namespace.
ns3::Time::As
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:429
ns3::Simulator::Schedule
static EventId Schedule(Time const &delay, FUNC f, Ts &&... args)
Schedule an event to expire after delay.
Definition: simulator.h:556
ns3::DoubleValue
This class can be used to hold variables of floating point type such as 'double' or 'float'.
Definition: double.h:41
ns3::SimpleDeviceEnergyModel::SetCurrentA
void SetCurrentA(double current)
Definition: simple-device-energy-model.cc:100
ns3::Ptr
Smart pointer class similar to boost::intrusive_ptr.
Definition: ptr.h:74
ns3::Simulator::Stop
static void Stop(void)
Tell the Simulator the calling event should be the last one executed.
Definition: simulator.cc:180
ns3::ObjectBase::GetAttribute
void GetAttribute(std::string name, AttributeValue &value) const
Get the value of an attribute, raising fatal errors if unsuccessful.
Definition: object-base.cc:223
ns3::Object::AggregateObject
void AggregateObject(Ptr< Object > other)
Aggregate two Objects together.
Definition: object.cc:252
ns3::EnergySource::SetNode
void SetNode(Ptr< Node > node)
Sets pointer to node containing this EnergySource.
Definition: energy-source.cc:56
ns3::Time
Simulation virtual time values and global simulation resolution.
Definition: nstime.h:104
ns3::SimpleDeviceEnergyModel::SetNode
virtual void SetNode(Ptr< Node > node)
Sets pointer to node.
Definition: simple-device-energy-model.cc:70
second.cmd
cmd
Definition: second.py:35
ns3::DoubleValue::Get
double Get(void) const
Definition: double.cc:35
ns3::Simulator::Run
static void Run(void)
Run the simulation.
Definition: simulator.cc:172
ns3::Seconds
Time Seconds(double value)
Construct a Time in the indicated unit.
Definition: nstime.h:1289
ns3::Time::S
@ S
second
Definition: nstime.h:115
ns3::Simulator::Destroy
static void Destroy(void)
Execute the events scheduled with ScheduleDestroy().
Definition: simulator.cc:136
ns3::LiIonEnergySource::GetSupplyVoltage
virtual double GetSupplyVoltage(void) const
Definition: li-ion-energy-source.cc:152
ns3::Simulator::IsFinished
static bool IsFinished(void)
Check if the simulation should finish.
Definition: simulator.cc:165
PrintCellInfo
static void PrintCellInfo(Ptr< LiIonEnergySource > es)
In this simple example, we show how to create and drain energy from a LiIonEnergySource.
Definition: li-ion-energy-source.cc:48
ns3::SimpleDeviceEnergyModel::SetEnergySource
virtual void SetEnergySource(Ptr< EnergySource > source)
Sets pointer to EnergySouce installed on node.
Definition: simple-device-energy-model.cc:62
ns3::EnergySourceContainer::Add
void Add(EnergySourceContainer container)
Definition: energy-source-container.cc:94
ns3::LiIonEnergySource::GetRemainingEnergy
virtual double GetRemainingEnergy(void)
Definition: li-ion-energy-source.cc:173
ns3::EnergySource::AppendDeviceEnergyModel
void AppendDeviceEnergyModel(Ptr< DeviceEnergyModel > deviceEnergyModelPtr)
Definition: energy-source.cc:70