A Discrete-Event Network Simulator
API
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
li-ion-energy-source.cc
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/simple-device-energy-model.h"
24 #include "ns3/li-ion-energy-source.h"
25 #include "ns3/energy-source-container.h"
26 
27 using namespace ns3;
28 
46 static void
47 PrintCellInfo (Ptr<LiIonEnergySource> es)
48 {
49  std::cout << "At " << Simulator::Now ().GetSeconds () << " Cell voltage: " << es->GetSupplyVoltage () << " V Remaining Capacity: " <<
50  es->GetRemainingEnergy () / (3.6 * 3600) << " Ah" << std::endl;
51 
52  if (!Simulator::IsFinished ())
53  {
55  &PrintCellInfo,
56  es);
57  }
58 }
59 
60 int
61 main (int argc, char **argv)
62 {
63  // uncomment below to see the energy consumption details
64  // LogComponentEnable ("LiIonEnergySource", LOG_LEVEL_DEBUG);
65 
66  Ptr<Node> node = CreateObject<Node> ();
67 
68  Ptr<SimpleDeviceEnergyModel> sem = CreateObject<SimpleDeviceEnergyModel> ();
69  Ptr<EnergySourceContainer> esCont = CreateObject<EnergySourceContainer> ();
70  Ptr<LiIonEnergySource> es = CreateObject<LiIonEnergySource> ();
71  esCont->Add (es);
72  es->SetNode (node);
73  sem->SetEnergySource (es);
74  es->AppendDeviceEnergyModel (sem);
75  sem->SetNode (node);
76  node->AggregateObject (esCont);
77 
78  Time now = Simulator::Now ();
79 
80  // discharge at 2.33 A for 1700 seconds
81  sem->SetCurrentA (2.33);
82  now += Seconds (1701);
83 
84 
85  // discharge at 4.66 A for 628 seconds
88  sem,
89  4.66);
90  now += Seconds (600);
91 
92  PrintCellInfo (es);
93 
94  Simulator::Stop (now);
95  Simulator::Run ();
97 
98  // the cell voltage should be under 3.3v
99  DoubleValue v;
100  es->GetAttribute ("ThresholdVoltage", v);
101  NS_ASSERT (es->GetSupplyVoltage () <= v.Get ());
102 }
keep track of time unit.
Definition: nstime.h:149
smart pointer class similar to boost::intrusive_ptr
Definition: ptr.h:59
#define NS_ASSERT(condition)
Definition: assert.h:64
static void Run(void)
Definition: simulator.cc:157
static EventId Schedule(Time const &time, MEM mem_ptr, OBJ obj)
Definition: simulator.h:820
virtual double GetRemainingEnergy(void)
double GetSeconds(void) const
Definition: nstime.h:262
void AggregateObject(Ptr< Object > other)
Definition: object.cc:242
static void Destroy(void)
Definition: simulator.cc:121
static Time Now(void)
Definition: simulator.cc:179
Time Seconds(double seconds)
create ns3::Time instances in units of seconds.
Definition: nstime.h:586
static void Stop(void)
Definition: simulator.cc:164
virtual double GetSupplyVoltage(void) const
static bool IsFinished(void)
Definition: simulator.cc:150
Hold an floating point type.
Definition: double.h:41
void SetNode(Ptr< Node > node)
Sets pointer to node containing this EnergySource.