|
|
| 1 |
/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| 2 |
/* |
| 3 |
* This program is free software; you can redistribute it and/or modify |
| 4 |
* it under the terms of the GNU General Public License version 2 as |
| 5 |
* published by the Free Software Foundation; |
| 6 |
* |
| 7 |
* This program is distributed in the hope that it will be useful, |
| 8 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 9 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 10 |
* GNU General Public License for more details. |
| 11 |
* |
| 12 |
* You should have received a copy of the GNU General Public License |
| 13 |
* along with this program; if not, write to the Free Software |
| 14 |
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
| 15 |
* |
| 16 |
* Author: Hossam Khader <hossamkhader@gmail.com> |
| 17 |
*/ |
| 18 |
#include "resonant-charger-energy-model.h" |
| 19 |
#include "ns3/mobility-model.h" |
| 20 |
#include "ns3/energy-harvester-container.h" |
| 21 |
#include "ns3/resonant-energy-harvester.h" |
| 22 |
#include "src/core/model/callback.h" |
| 23 |
|
| 24 |
NS_LOG_COMPONENT_DEFINE ("ResonantChargerEnergyModel"); |
| 25 |
|
| 26 |
namespace ns3 { |
| 27 |
|
| 28 |
NS_OBJECT_ENSURE_REGISTERED (ResonantChargerEnergyModel); |
| 29 |
|
| 30 |
TypeId |
| 31 |
ResonantChargerEnergyModel::GetTypeId (void) |
| 32 |
{ |
| 33 |
static TypeId tid = TypeId ("ns3::ResonantChargerEnergyModel") |
| 34 |
.SetParent<DeviceEnergyModel> () |
| 35 |
.AddConstructor<ResonantChargerEnergyModel> () |
| 36 |
.AddAttribute ("ResonantCharger Power Consumption", |
| 37 |
"Consumed power by the Resonant Charger", |
| 38 |
DoubleValue (51.0), |
| 39 |
MakeDoubleAccessor (&ResonantChargerEnergyModel::m_resonatorPowerRate), |
| 40 |
MakeDoubleChecker<double> ()) |
| 41 |
.AddAttribute ("Power Loss Per Distance", |
| 42 |
"The power loss per unit distance", |
| 43 |
DoubleValue (20.4), |
| 44 |
MakeDoubleAccessor (&ResonantChargerEnergyModel::m_powerLossPerDistance), |
| 45 |
MakeDoubleChecker<double> ()) |
| 46 |
|
| 47 |
//The energy loss per unit distance |
| 48 |
; |
| 49 |
return tid; |
| 50 |
} |
| 51 |
|
| 52 |
ResonantChargerEnergyModel::ResonantChargerEnergyModel () |
| 53 |
: m_node (NULL), |
| 54 |
m_source (NULL), |
| 55 |
m_lastUpdateTime (Simulator::Now ()), |
| 56 |
m_state (OFF) |
| 57 |
{ |
| 58 |
NS_LOG_FUNCTION (this); |
| 59 |
m_energyDepletionCallback.Nullify (); |
| 60 |
m_energyRechargeCallback.Nullify (); |
| 61 |
} |
| 62 |
|
| 63 |
ResonantChargerEnergyModel::~ResonantChargerEnergyModel () |
| 64 |
{ |
| 65 |
m_energyDepletionCallback.Nullify (); |
| 66 |
m_energyRechargeCallback.Nullify (); |
| 67 |
m_node = NULL; |
| 68 |
m_source = NULL; |
| 69 |
} |
| 70 |
void |
| 71 |
ResonantChargerEnergyModel::SetNode (Ptr<Node> node) |
| 72 |
{ |
| 73 |
NS_LOG_FUNCTION (this << node); |
| 74 |
NS_ASSERT (node != NULL); |
| 75 |
m_node = node; |
| 76 |
Charge (); |
| 77 |
} |
| 78 |
|
| 79 |
Ptr<Node> |
| 80 |
ResonantChargerEnergyModel::GetNode (void) const |
| 81 |
{ |
| 82 |
return m_node; |
| 83 |
} |
| 84 |
|
| 85 |
void |
| 86 |
ResonantChargerEnergyModel::SetEnergySource (Ptr<EnergySource> source) |
| 87 |
{ |
| 88 |
NS_LOG_FUNCTION (this << source); |
| 89 |
NS_ASSERT (source != NULL); |
| 90 |
m_source = source; |
| 91 |
} |
| 92 |
|
| 93 |
double |
| 94 |
ResonantChargerEnergyModel::GetTotalEnergyConsumption (void) const |
| 95 |
{ |
| 96 |
NS_LOG_FUNCTION (this); |
| 97 |
return m_totalEnergyConsumption; |
| 98 |
} |
| 99 |
|
| 100 |
void |
| 101 |
ResonantChargerEnergyModel::ChangeState (int newState) |
| 102 |
{ |
| 103 |
|
| 104 |
} |
| 105 |
|
| 106 |
void |
| 107 |
ResonantChargerEnergyModel::SetState (State state) |
| 108 |
{ |
| 109 |
NS_LOG_FUNCTION (this); |
| 110 |
|
| 111 |
if (m_state != DISABLED) |
| 112 |
{ |
| 113 |
m_state = state; |
| 114 |
} |
| 115 |
|
| 116 |
} |
| 117 |
|
| 118 |
ResonantChargerEnergyModel::State |
| 119 |
ResonantChargerEnergyModel::GetState () |
| 120 |
{ |
| 121 |
NS_LOG_FUNCTION (this); |
| 122 |
|
| 123 |
return m_state; |
| 124 |
} |
| 125 |
|
| 126 |
void |
| 127 |
ResonantChargerEnergyModel::ChangeEnergyConsumption () |
| 128 |
{ |
| 129 |
Time duration = Simulator::Now () - m_lastUpdateTime; |
| 130 |
NS_ASSERT (duration.GetNanoSeconds () >= 0); // check if duration is valid |
| 131 |
|
| 132 |
double energyToDecrease = duration.GetSeconds () * m_resonatorPowerRate; |
| 133 |
|
| 134 |
// charger is not active |
| 135 |
if(m_state == OFF || m_state == DISABLED) |
| 136 |
{ |
| 137 |
energyToDecrease = 0; |
| 138 |
} |
| 139 |
|
| 140 |
// update total energy consumption |
| 141 |
m_totalEnergyConsumption += energyToDecrease; |
| 142 |
|
| 143 |
// update current drain |
| 144 |
double supplyVoltage = m_source->GetSupplyVoltage (); |
| 145 |
m_actualCurrentDrain = m_resonatorPowerRate / supplyVoltage; |
| 146 |
|
| 147 |
// update last update time stamp |
| 148 |
m_lastUpdateTime = Simulator::Now (); |
| 149 |
|
| 150 |
// notify energy source |
| 151 |
m_source->UpdateEnergySource (); |
| 152 |
|
| 153 |
// some debug message |
| 154 |
NS_LOG_DEBUG ("ResonantChargerEnergyModel:Total energy consumption at node #" << |
| 155 |
m_node->GetId () << " is " << m_totalEnergyConsumption << "J"); |
| 156 |
} |
| 157 |
|
| 158 |
void |
| 159 |
ResonantChargerEnergyModel::SetEnergyDepletionCallback (ResonantChargerEnergyDepletionCallback callback) |
| 160 |
{ |
| 161 |
NS_LOG_FUNCTION (this); |
| 162 |
if (callback.IsNull ()) |
| 163 |
{ |
| 164 |
NS_LOG_DEBUG ("ResonantChargerEnergyModel:Setting NULL energy depletion callback!"); |
| 165 |
} |
| 166 |
m_energyDepletionCallback = callback; |
| 167 |
} |
| 168 |
|
| 169 |
void |
| 170 |
ResonantChargerEnergyModel::SetEnergyRechargeCallback (ResonantChargerEnergyRechargeCallback callback) |
| 171 |
{ |
| 172 |
NS_LOG_FUNCTION (this); |
| 173 |
if (callback.IsNull ()) |
| 174 |
{ |
| 175 |
NS_LOG_DEBUG ("ResonantChargerEnergyModel:Setting NULL energy recharge callback!"); |
| 176 |
} |
| 177 |
m_energyRechargeCallback = callback; |
| 178 |
} |
| 179 |
|
| 180 |
void |
| 181 |
ResonantChargerEnergyModel::HandleEnergyDepletion (void) |
| 182 |
{ |
| 183 |
NS_LOG_FUNCTION (this); |
| 184 |
NS_LOG_DEBUG ("ResonantChargerEnergyModel:Energy is depleted at node #" << |
| 185 |
m_node->GetId ()); |
| 186 |
m_state = DISABLED; |
| 187 |
// invoke energy depletion callback, if set. |
| 188 |
if (!m_energyDepletionCallback.IsNull ()) |
| 189 |
{ |
| 190 |
m_energyDepletionCallback (); |
| 191 |
} |
| 192 |
} |
| 193 |
|
| 194 |
void |
| 195 |
ResonantChargerEnergyModel::HandleEnergyRecharged (void) |
| 196 |
{ |
| 197 |
NS_LOG_FUNCTION (this); |
| 198 |
NS_LOG_DEBUG ("ResonantChargerEnergyModel:Energy is recharged at node #" << |
| 199 |
m_node->GetId ()); |
| 200 |
m_state = OFF; |
| 201 |
// invoke energy recharge callback, if set. |
| 202 |
if (!m_energyRechargeCallback.IsNull ()) |
| 203 |
{ |
| 204 |
m_energyRechargeCallback (); |
| 205 |
} |
| 206 |
} |
| 207 |
|
| 208 |
/* |
| 209 |
* Private functions start here. |
| 210 |
*/ |
| 211 |
|
| 212 |
void |
| 213 |
ResonantChargerEnergyModel::DoDispose (void) |
| 214 |
{ |
| 215 |
NS_LOG_FUNCTION (this); |
| 216 |
} |
| 217 |
|
| 218 |
double |
| 219 |
ResonantChargerEnergyModel::DoGetCurrentA (void) const |
| 220 |
{ |
| 221 |
NS_LOG_FUNCTION (this); |
| 222 |
|
| 223 |
return m_actualCurrentDrain; |
| 224 |
} |
| 225 |
|
| 226 |
double |
| 227 |
ResonantChargerEnergyModel::GetChargingPowerRate () |
| 228 |
{ |
| 229 |
NS_LOG_FUNCTION (this); |
| 230 |
|
| 231 |
return m_resonatorPowerRate; |
| 232 |
} |
| 233 |
|
| 234 |
void |
| 235 |
ResonantChargerEnergyModel::SetChargingPowerRate (double power) |
| 236 |
{ |
| 237 |
NS_LOG_FUNCTION (this); |
| 238 |
|
| 239 |
m_resonatorPowerRate = power; |
| 240 |
} |
| 241 |
|
| 242 |
void |
| 243 |
ResonantChargerEnergyModel::AddRechargableNodes (const NodeContainer nodeContainer) |
| 244 |
{ |
| 245 |
m_nodeContainer.Add (nodeContainer); |
| 246 |
} |
| 247 |
|
| 248 |
void |
| 249 |
ResonantChargerEnergyModel::AddRechargableNode (const Ptr<Node> node) |
| 250 |
{ |
| 251 |
NS_ASSERT (node != NULL); |
| 252 |
m_nodeContainer.Add (node); |
| 253 |
} |
| 254 |
|
| 255 |
double |
| 256 |
ResonantChargerEnergyModel::CalculateDistance (const Ptr<Node> node) |
| 257 |
{ |
| 258 |
NS_ASSERT (GetNode ()->GetObject<MobilityModel> () != NULL); |
| 259 |
NS_ASSERT (node->GetObject<MobilityModel> () != NULL); |
| 260 |
|
| 261 |
double distance = GetNode ()->GetObject<MobilityModel> () |
| 262 |
->GetDistanceFrom (node->GetObject<MobilityModel> ()); |
| 263 |
|
| 264 |
return distance; |
| 265 |
} |
| 266 |
|
| 267 |
void |
| 268 |
ResonantChargerEnergyModel::Charge () |
| 269 |
{ |
| 270 |
NodeContainer::Iterator i = m_nodeContainer.Begin (); |
| 271 |
while (i != m_nodeContainer.End ()) |
| 272 |
{ |
| 273 |
DoCharge (*i); |
| 274 |
i++; |
| 275 |
} |
| 276 |
Simulator::Schedule (Seconds (1), &ResonantChargerEnergyModel::Charge, this); |
| 277 |
} |
| 278 |
|
| 279 |
void |
| 280 |
ResonantChargerEnergyModel::DoCharge (const Ptr<Node> node) |
| 281 |
{ |
| 282 |
double power = m_resonatorPowerRate - (CalculateDistance (node) * m_powerLossPerDistance); |
| 283 |
if (power < 0 || m_state == OFF || m_state == DISABLED) |
| 284 |
{ |
| 285 |
power = 0; |
| 286 |
} |
| 287 |
Ptr<ResonantEnergyHarvester> resonantHarvester = node->GetObject<EnergyHarvesterContainer> ()->Get (0) |
| 288 |
->GetObject<ResonantEnergyHarvester> (); |
| 289 |
|
| 290 |
if(resonantHarvester != NULL) |
| 291 |
{ |
| 292 |
resonantHarvester->SetHarvestablePower (power); |
| 293 |
} |
| 294 |
} |
| 295 |
|
| 296 |
} // namespace ns3 |