⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 batterysimple.cc

📁 基于Oment++的无线传感器网络仿真
💻 CC
字号:
#include "CommonIncludes.h"#include "BatterySimple.h"#include "CoOrdinatorBase.h"Define_Module_Like( BatterySimple , BatteryModule );//Define_Module( BatterySimple );BatterySimple::BatterySimple(const char *name, cModule *parentModule, unsigned stacksize) :	BatteryBase(name, parentModule, stacksize){	//	printf("\n\t batterySimple");}BatterySimple::~BatterySimple(){//	printf("BatterySimple-----I am dieing");}void BatterySimple::initialize(){	//printf("\n\t In BatterySimple: intialize");	//ev << "\n\t In BatterySimple: intialize" ;		m_Energy = (double) par("totalenergy");		m_CurrentEnergy = m_Energy;		m_State = INITIALIZED;    if(SUCCESS==RegisterCoordinator())	{		m_State = INITIALIZED;		MyNodeId= ((CoOrdinatorBase*)pCoOrdinator)->getNodeId();	}	else		m_State=NOT_INITIALIZED;	 batteryOut = new cMessage("BatteryOut",CONTROL);	 batteryOut->addPar("command") = BATTERY_OUT;	 //printf("BatterySimple: initialize state %d",m_State);		return;		 }int BatterySimple::UpdateEnergy(void){//		printf("\n\n\n\n in Energy 0 ............\n\n\n\t");		cConsumer *cons;	double totalPower=0.0;	//	ev<<"BatteryBase UpdateEnergy:"<<now<<"Diff "<<(now-lastTimeOut);			int len=ConsumerList.items();//	printf("length of consumerList %d",len);	if(len>0)	{	simtime_t now=simTime();		for(int i=0;i<len;i++)	{		 cons = (cConsumer *)ConsumerList[i];		 totalPower+=(cons->GetPowerRating())*VOLTAGE;		 m_CurrentEnergy-=totalPower*(now-lastTimeOut);		 	}	if (m_CurrentEnergy<=.001)	{		m_CurrentEnergy=0.0;	}	//printf(" the current energy is  %f for node %d \n", m_CurrentEnergy,MyNodeId);/*		((CoOrdinatorBase*)pCoOrdinator)->UnRegisterFromSensorNetwork();		m_State=DEAD;		return DEAD;	}*/	lastTimeOut=now;	return HAVE_CONSUMER;	}	else	return NO_CONSUMER;}void BatterySimple::handleMessage(cMessage *msg){				//	    printf("\n\t In BatterySimple::handleMessage");//		ev << "\n\t In BatterySimple::handleMessage";		int command=(int)msg->par("command");		switch(command)		{		  case BATTERY_OUT:		  	if (!msg->isSelfMessage())				break;			//m_CurrentEnergy=0;			char s[50];			sprintf(s,"Battery Dead of NodeID  %d CurrentEnergy = %f  \n",MyNodeId,m_CurrentEnergy);			//printf("SURI  THE SIMULATION TIME IS======%f", simTime());			recordScalar(s,simTime());			m_State=DEAD;			((CoOrdinatorBase*)pCoOrdinator)->UnRegisterFromSensorNetwork();			break;		}														 			}void BatterySimple::scheduleBatteryOut(){        double totalPower=0;		cConsumer *cons;		int len=ConsumerList.items();		for(int i=0;i<len;i++)		{		   cons = (cConsumer *)ConsumerList[i];		   totalPower+=(cons->GetPowerRating())*VOLTAGE;		}           double timeToZero=0;          if(totalPower !=0)	       {	            if (m_State!=DEAD)	            {	  					  timeToZero= m_CurrentEnergy/totalPower;					  //printf("Before isScheduled \n");					  //batteryOut = new cMessage("BatteryOut",CONTROL);					  //batteryOut->addPar("command") = BATTERY_OUT;					  if(batteryOut->isScheduled()) 					 {//					 	//printf("in isScheduled" );			//		 	char s[20];                      //  			sprintf(s,"In IsSchedule  at time %f  \n",simTime());					     //	recordScalar(s,simTime());					    	cancelEvent(batteryOut);					 }					 	//				 	//printf("after isScheduled \n " );						batteryOut->setOwner(this);						//printf(" BATTERY TO DIE AT %f for NodeId %d \n",simTime()+timeToZero,MyNodeId);					  // scheduleAt(simTime()+timeToZero,batteryOut);					    //sprintf(s,"In IsSchedule  at time %f  \n",simTime());		  // char s[50];		  // sprintf(s,"The estimated simulation time is %f  for Node Id %d is \n",simTime()+timeToZero,MyNodeId);                    //                            recordScalar(s,simTime());						              }				    	       }		   else		  	cancelEvent(batteryOut);}																																																										  void BatterySimple::UpdateState(const cConsumer* Consumer){	int result;	result =UpdateEnergy();		ConsumerList.set((cConsumer *)Consumer);	if(result==HAVE_CONSUMER)	scheduleBatteryOut();}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -