在仿真中我必须每秒计算汽车之间的距离,我的代码仅在时间= 0时显示
simulator :: schedule
double
GetDistance_From (Ptr<Node> node1, Ptr<Node> node2)
{
Ptr<MobilityModel> model1 = node1->GetObject<MobilityModel>();
Ptr<MobilityModel> model2 = node2->GetObject<MobilityModel>();
double distance = model1->GetDistanceFrom (model2);
return distance;
}
并调用主函数
for (int i=0; i < 5; i++)
{
for (int k=i+1; k < 5; k++)
{
double dist= GetDistance_From(c.Get(i),c.Get(k));
// Simulator::Schedule(Seconds(10), GetDistance_From,c.Get(i),c.Get(k), dist);
int t = (Simulator::Now ()).GetSeconds ();
std::ostringstream oss;
oss << "distance between Source Node(" << i << ") and Node(" <<k<< ") est= " << dist<<" at time="<< t ;
NS_LOG_UNCOND (oss.str());
}
}
结果仅一次
答案 0 :(得分:0)
您必须将代码放入运行1000ms的循环中以模拟时间流逝。
答案 1 :(得分:0)
只需重新组织代码并使用Simulator::Schedule()
函数
void
GetDistance_From (Ptr<Node> node1, Ptr<Node> node2, int i, int k)
{
Ptr<MobilityModel> model1 = node1->GetObject<MobilityModel>();
Ptr<MobilityModel> model2 = node2->GetObject<MobilityModel>();
double distance = model1->GetDistanceFrom (model2);
std::ostringstream oss;
oss << "distance between Source Node(" << i << ") and Node(" <<k<< ") est= " << distance<<" at time="<< Simulator::Now().GetSeconds() ;
NS_LOG_UNCOND (oss.str());
}
和main()内-
for (int i=0; i < 5; i++)
{
for (int k=i+1; k < 5; k++)
{
double dist= GetDistance_From(c.Get(i),c.Get(k));
Simulator::Schedule(Seconds(10), GetDistance_From,c.Get(i),c.Get(k),i,k);
}
}