LEFT | RIGHT |
(no file at all) | |
1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ | 1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
2 /* | 2 /* |
3 * Copyright (c) 2010 Network Security Lab, University of Washington, Seattle. | 3 * Copyright (c) 2010 Network Security Lab, University of Washington, Seattle. |
4 * | 4 * |
5 * This program is free software; you can redistribute it and/or modify | 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 | 6 * it under the terms of the GNU General Public License version 2 as |
7 * published by the Free Software Foundation; | 7 * published by the Free Software Foundation; |
8 * | 8 * |
9 * This program is distributed in the hope that it will be useful, | 9 * This program is distributed in the hope that it will be useful, |
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of | 10 * but WITHOUT ANY WARRANTY; without even the implied warranty of |
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
46 .AddAttribute ("BasicEnergySupplyVoltageV", | 46 .AddAttribute ("BasicEnergySupplyVoltageV", |
47 "Initial supply voltage for basic energy source.", | 47 "Initial supply voltage for basic energy source.", |
48 DoubleValue (3.0), // in Volts | 48 DoubleValue (3.0), // in Volts |
49 MakeDoubleAccessor (&BasicEnergySource::SetSupplyVoltage, | 49 MakeDoubleAccessor (&BasicEnergySource::SetSupplyVoltage, |
50 &BasicEnergySource::GetSupplyVoltage), | 50 &BasicEnergySource::GetSupplyVoltage), |
51 MakeDoubleChecker<double> ()) | 51 MakeDoubleChecker<double> ()) |
52 .AddAttribute ("BasicEnergyLowBatteryThreshold", | 52 .AddAttribute ("BasicEnergyLowBatteryThreshold", |
53 "Low battery threshold for basic energy source.", | 53 "Low battery threshold for basic energy source.", |
54 DoubleValue (0.10), // as a fraction of the initial energy | 54 DoubleValue (0.10), // as a fraction of the initial energy |
55 MakeDoubleAccessor (&BasicEnergySource::m_lowBatteryTh), | 55 MakeDoubleAccessor (&BasicEnergySource::m_lowBatteryTh), |
| 56 MakeDoubleChecker<double> ()) |
| 57 .AddAttribute ("BasicEnergyHighBatteryThreshold", |
| 58 "High battery threshold for basic energy source.", |
| 59 DoubleValue (0.15), // as a fraction of the initial energy |
| 60 MakeDoubleAccessor (&BasicEnergySource::m_highBatteryTh), |
56 MakeDoubleChecker<double> ()) | 61 MakeDoubleChecker<double> ()) |
57 .AddAttribute ("PeriodicEnergyUpdateInterval", | 62 .AddAttribute ("PeriodicEnergyUpdateInterval", |
58 "Time between two consecutive periodic energy updates.", | 63 "Time between two consecutive periodic energy updates.", |
59 TimeValue (Seconds (1.0)), | 64 TimeValue (Seconds (1.0)), |
60 MakeTimeAccessor (&BasicEnergySource::SetEnergyUpdateInterval
, | 65 MakeTimeAccessor (&BasicEnergySource::SetEnergyUpdateInterval
, |
61 &BasicEnergySource::GetEnergyUpdateInterval
), | 66 &BasicEnergySource::GetEnergyUpdateInterval
), |
62 MakeTimeChecker ()) | 67 MakeTimeChecker ()) |
63 .AddTraceSource ("RemainingEnergy", | 68 .AddTraceSource ("RemainingEnergy", |
64 "Remaining energy at BasicEnergySource.", | 69 "Remaining energy at BasicEnergySource.", |
65 MakeTraceSourceAccessor (&BasicEnergySource::m_remainingEne
rgyJ)) | 70 MakeTraceSourceAccessor (&BasicEnergySource::m_remainingEne
rgyJ)) |
66 ; | 71 ; |
67 return tid; | 72 return tid; |
68 } | 73 } |
69 | 74 |
70 BasicEnergySource::BasicEnergySource () | 75 BasicEnergySource::BasicEnergySource () |
71 { | 76 { |
72 NS_LOG_FUNCTION (this); | 77 NS_LOG_FUNCTION (this); |
73 m_lastUpdateTime = Seconds (0.0); | 78 m_lastUpdateTime = Seconds (0.0); |
| 79 m_depleted = false; |
74 } | 80 } |
75 | 81 |
76 BasicEnergySource::~BasicEnergySource () | 82 BasicEnergySource::~BasicEnergySource () |
77 { | 83 { |
78 NS_LOG_FUNCTION (this); | 84 NS_LOG_FUNCTION (this); |
79 } | 85 } |
80 | 86 |
81 void | 87 void |
82 BasicEnergySource::SetInitialEnergy (double initialEnergyJ) | 88 BasicEnergySource::SetInitialEnergy (double initialEnergyJ) |
83 { | 89 { |
(...skipping 67 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
151 { | 157 { |
152 return; | 158 return; |
153 } | 159 } |
154 | 160 |
155 m_energyUpdateEvent.Cancel (); | 161 m_energyUpdateEvent.Cancel (); |
156 | 162 |
157 CalculateRemainingEnergy (); | 163 CalculateRemainingEnergy (); |
158 | 164 |
159 m_lastUpdateTime = Simulator::Now (); | 165 m_lastUpdateTime = Simulator::Now (); |
160 | 166 |
161 if (m_remainingEnergyJ <= m_lowBatteryTh * m_initialEnergyJ) | 167 if (!m_depleted && m_remainingEnergyJ <= m_lowBatteryTh * m_initialEnergyJ) |
162 { | 168 { |
| 169 m_depleted = true; |
163 HandleEnergyDrainedEvent (); | 170 HandleEnergyDrainedEvent (); |
164 return; // stop periodic update | 171 // return; // stop periodic update |
| 172 } |
| 173 |
| 174 if (m_depleted && m_remainingEnergyJ >= m_highBatteryTh * m_initialEnergyJ) |
| 175 { |
| 176 m_depleted = false; |
| 177 HandleEnergyRechargedEvent (); |
165 } | 178 } |
166 | 179 |
167 m_energyUpdateEvent = Simulator::Schedule (m_energyUpdateInterval, | 180 m_energyUpdateEvent = Simulator::Schedule (m_energyUpdateInterval, |
168 &BasicEnergySource::UpdateEnergySou
rce, | 181 &BasicEnergySource::UpdateEnergySou
rce, |
169 this); | 182 this); |
170 } | 183 } |
171 | 184 |
172 /* | 185 /* |
173 * Private functions start here. | 186 * Private functions start here. |
174 */ | 187 */ |
(...skipping 15 matching lines...) Expand all Loading... |
190 void | 203 void |
191 BasicEnergySource::HandleEnergyDrainedEvent (void) | 204 BasicEnergySource::HandleEnergyDrainedEvent (void) |
192 { | 205 { |
193 NS_LOG_FUNCTION (this); | 206 NS_LOG_FUNCTION (this); |
194 NS_LOG_DEBUG ("BasicEnergySource:Energy depleted!"); | 207 NS_LOG_DEBUG ("BasicEnergySource:Energy depleted!"); |
195 NotifyEnergyDrained (); // notify DeviceEnergyModel objects | 208 NotifyEnergyDrained (); // notify DeviceEnergyModel objects |
196 if (m_remainingEnergyJ <= 0) | 209 if (m_remainingEnergyJ <= 0) |
197 { | 210 { |
198 m_remainingEnergyJ = 0; // energy never goes below 0 | 211 m_remainingEnergyJ = 0; // energy never goes below 0 |
199 } | 212 } |
| 213 } |
| 214 |
| 215 void |
| 216 BasicEnergySource::HandleEnergyRechargedEvent (void) |
| 217 { |
| 218 NS_LOG_FUNCTION (this); |
| 219 NS_LOG_DEBUG ("BasicEnergySource:Energy recharged!"); |
| 220 NotifyEnergyRecharged (); // notify DeviceEnergyModel objects |
200 } | 221 } |
201 | 222 |
202 void | 223 void |
203 BasicEnergySource::CalculateRemainingEnergy (void) | 224 BasicEnergySource::CalculateRemainingEnergy (void) |
204 { | 225 { |
205 NS_LOG_FUNCTION (this); | 226 NS_LOG_FUNCTION (this); |
206 double totalCurrentA = CalculateTotalCurrent (); | 227 double totalCurrentA = CalculateTotalCurrent (); |
207 Time duration = Simulator::Now () - m_lastUpdateTime; | 228 Time duration = Simulator::Now () - m_lastUpdateTime; |
208 NS_ASSERT (duration.GetSeconds () >= 0); | 229 NS_ASSERT (duration.GetSeconds () >= 0); |
209 // energy = current * voltage * time | 230 // energy = current * voltage * time |
210 double energyToDecreaseJ = totalCurrentA * m_supplyVoltageV * duration.GetSeco
nds (); | 231 double energyToDecreaseJ = totalCurrentA * m_supplyVoltageV * duration.GetSeco
nds (); |
211 m_remainingEnergyJ -= energyToDecreaseJ; | 232 m_remainingEnergyJ -= energyToDecreaseJ; |
212 NS_LOG_DEBUG ("BasicEnergySource:Remaining energy = " << m_remainingEnergyJ); | 233 NS_LOG_DEBUG ("BasicEnergySource:Remaining energy = " << m_remainingEnergyJ); |
213 } | 234 } |
214 | 235 |
215 } // namespace ns3 | 236 } // namespace ns3 |
LEFT | RIGHT |