我尝试记录用电量的值。为此,我要像这样构建NED文件:
simple EConsumerRSF
{
parameters:
volatile xml ElectricityConsumptionRSFLoadProfile;
volatile double sendIaTime @unit(s);
@signal[electricityConsumption](type="double");
@statistics[electricityConsumption](title="Electricity Consumption RSF Unit 1";source="electricityConsumption"; record=vector,stats; interpolationmode=none);
gates:
output outElectricityConsumptionRSF;
}
我的c ++类和头文件看起来像这样:
#ifndef __VISIONSIMULATIONOFPED_ECONSUMERRSF_H_
#define __VISIONSIMULATIONOFPED_ECONSUMERRSF_H_
#include <omnetpp.h>
#include <stdio.h>
#include <string.h>
//#include "EConsumptionRSFMessage_m.h"
using namespace omnetpp;
namespace network {
/**
* Network EConsumerRSF; see NED file for more info.
*/
//class EConsumerRSF : public EConsumptionRSFMessage
class EConsumerRSF : public cSimpleModule
{
public:
EConsumerRSF();
virtual ~EConsumerRSF();
protected:
virtual void initialize() override;
virtual void handleMessage(cMessage *msg) override;
// virtual void setValEConsumption(double valEConsumption);
private:
int counterSend = 1;
int counterMessage = 1;
double valEConsumption1;
double valEConsumption2;
double valEConsumption3;
double valEConsumption4;
simsignal_t electricityConsumptionSingnalId;
//double electricityConsumption;
double valEConsumption;
double aggregationOfElectricityConsumptionRSF = 0;
cMessage *endTxEvent;
cMessage *EConsumptionRSFMessage;
};
}; //namespace
#endif
#include "EConsumerRSF.h"
#include <stdio.h>
#include <string.h>
//#include "EConsumptionRSFMessage_m.h"
using namespace omnetpp;
namespace network {
Define_Module(EConsumerRSF);
EConsumerRSF::EConsumerRSF()
{
endTxEvent = nullptr;
}
EConsumerRSF::~EConsumerRSF()
{
cancelAndDelete(endTxEvent);
}
void EConsumerRSF::initialize()
{
electricityConsumptionSingnalId = registerSignal("electricityConsumption");
//Get param XMLElectricity file from simple module
cXMLElement *rootelementEConsumption = par("ElectricityConsumptionRSFLoadProfile");
cXMLElementList paramsEConsumption = rootelementEConsumption->getChildrenByTagName("param");
//Loop to iterate through all elements called param
for (const auto & element : paramsEConsumption) {
const char* valueEConsumption = element->getNodeValue();
valEConsumption = atof(valueEConsumption);
EV << "Read value: " << valEConsumption << std::endl;
if (valEConsumption > 0.0) {
//Calculation of electricity supply through utility
aggregationOfElectricityConsumptionRSF = aggregationOfElectricityConsumptionRSF + valEConsumption;
}
switch(counterMessage)
{
case 1:
valEConsumption1 = valEConsumption;
counterMessage++;
break;
case 2:
valEConsumption2 = valEConsumption;
counterMessage++;
break;
case 3:
valEConsumption3 = valEConsumption;
counterMessage++;
break;
case 4:
valEConsumption4 = valEConsumption;
counterMessage++;
break;
}
}
EConsumptionRSFMessage = new cMessage("valEConsumption");
endTxEvent = new cMessage("send/endTx");
scheduleAt(simTime(), endTxEvent);
}
void EConsumerRSF::handleMessage(cMessage *msg)
{
// Check if self message is received
if(endTxEvent->isSelfMessage()){
if(counterSend <= 4){
switch(counterSend)
{
case 1:
if(valEConsumption1 > 0.0){
double electricityConsumption = valEConsumption1;
emit(electricityConsumptionSingnalId, electricityConsumption);
send(EConsumptionRSFMessage->dup(), "outElectricityConsumptionRSF");
}else{
double electricityConsumption = 0.0000000;
emit(electricityConsumptionSingnalId, electricityConsumption);
}
counterSend++;
scheduleAt(simTime()+par("sendIaTime").doubleValue(), endTxEvent);
break;
case 2:
if(valEConsumption2 > 0.0){
double electricityConsumption = valEConsumption2;
emit(electricityConsumptionSingnalId, electricityConsumption);
send(EConsumptionRSFMessage->dup(), "outElectricityConsumptionRSF");
}else{
double electricityConsumption = 0.0000000;
emit(electricityConsumptionSingnalId, electricityConsumption);
}
counterSend++;
scheduleAt(simTime()+par("sendIaTime").doubleValue(), endTxEvent);
break;
case 3:
if(valEConsumption3 > 0.0){
double electricityConsumption = valEConsumption3;
emit(electricityConsumptionSingnalId, electricityConsumption);
send(EConsumptionRSFMessage->dup(), "outElectricityConsumptionRSF");
}else{
double electricityConsumption = 0.0000000;
emit(electricityConsumptionSingnalId, electricityConsumption);
}
counterSend++;
scheduleAt(simTime()+par("sendIaTime").doubleValue(), endTxEvent);
break;
case 4:
if(valEConsumption4 > 0.0){
double electricityConsumption = valEConsumption4;
emit(electricityConsumptionSingnalId, electricityConsumption);
send(EConsumptionRSFMessage->dup(), "outElectricityConsumptionRSF");
}else{
double electricityConsumption = 0.0000000;
emit(electricityConsumptionSingnalId, electricityConsumption);
}
counterSend++;
//cancelEvent(endTxEvent);
scheduleAt(simTime()+par("sendIaTime").doubleValue(), endTxEvent);
break;
default:
cancelEvent(endTxEvent);
break;
}
}else {
throw cRuntimeError("self-message not delivered");
cancelEvent(endTxEvent);
}
}
}
}; //namespace
并且我像这样启用了.ini文件中的记录:
[Config EConsumerRSF]
**.serviceTime = 1s
**.electricityConsumption.result-recording-modes = -vector
当我运行模拟时,它甚至不生成.vec文件。我究竟做错了什么?无法在handleMessage()中记录结果吗?还是我想念什么?
可以请人帮我解决这个问题吗?
提前感谢!
显示的代码中有两个错误:
@statistic
(而不是@statistics
)。 实际上在omnetpp.ini
行中:
**.electricityConsumption.result-recording-modes = -vector
您已关闭矢量记录(减号表示禁用)。要打开矢量记录,将其更改为+vector
或简单地删除此行。