Reputation: 11
I am new to NS3; can you please explain how the RandomWalk2dMobilityModel
works?
I am trying to model an environment where a user will be given a destination, and he is to go there with a randomly generated speed. Is this possible to design with this model?
Thank You.
static void
CourseChange (std::string foo, Ptr<const MobilityModel> mobility)
{
Vector pos = mobility->GetPosition ();
Vector vel = mobility->GetVelocity ();
std::cout << Simulator::Now () << ", model=" << mobility << ", POS: x=" << pos.x << ", y=" << pos.y
<< ", z=" << pos.z << "; VEL:" << vel.x << ", y=" << vel.y
<< ", z=" << vel.z << std::endl;
}
int main (int argc, char *argv[])
{
Config::SetDefault ("ns3::RandomWalk2dMobilityModel::Mode", StringValue ("Time"));
Config::SetDefault ("ns3::RandomWalk2dMobilityModel::Time", StringValue ("2s"));
Config::SetDefault ("ns3::RandomWalk2dMobilityModel::Speed", StringValue ("ns3::ConstantRandomVariable[Constant=1.0]"));
Config::SetDefault ("ns3::RandomWalk2dMobilityModel::Bounds", StringValue ("0|200|0|200"));
CommandLine cmd;
cmd.Parse (argc, argv);
NodeContainer c;
c.Create (100);
MobilityHelper mobility;
mobility.SetPositionAllocator ("ns3::RandomDiscPositionAllocator",
"X", StringValue ("100.0"),
"Y", StringValue ("100.0"),
"Rho", StringValue ("ns3::UniformRandomVariable[Min=0|Max=30]"));
mobility.SetMobilityModel ("ns3::RandomWalk2dMobilityModel",
"Mode", StringValue ("Time"),
"Time", StringValue ("2s"),
"Speed", StringValue ("ns3::ConstantRandomVariable[Constant=1.0]"),
"Bounds", StringValue ("0|200|0|200"));
mobility.InstallAll ();
Config::Connect ("/NodeList/*/$ns3::MobilityModel/CourseChange",
MakeCallback (&CourseChange));
Upvotes: 1
Views: 1043
Reputation: 81
Basically the answer is No, RandomWalk2dMobilityModel is used to generate random directions with random speed, if you want specific direction with random speed then you have to create your mode.
I created a function to create movement between to positions where I pass node id, startPosition, endPosition and current simulation time. This function return the simulation time taken to move between the two position
double GenerateMovementBetweenTwoPosition(Ptr<Node> node, Vector startPosition,Vector newPosition, double time)
{
float x,y;
Ptr<MobilityModel> mobilityModel = node->GetObject<MobilityModel> ();
//Vector startPosition = mobilityModel->GetPosition();
double distance = sqrt( pow(startPosition.x - newPosition.x, 2)
+ pow(startPosition.y - newPosition.y, 2) );
double tagSpeed = UniformVariable ().GetValue(0.7,1.3);
double MovementTime = distance/tagSpeed; // time is in seconds as distance in meter and 1.5 m/s
double distanceStep = distance/ceil(MovementTime);
for ( int i=1; i <= (int)ceil(MovementTime); i++)
{
if (time+i >= simTime)
return time;
x = startPosition.x + ( distanceStep * i / distance )*(newPosition.x - startPosition.x)+ NormalVariable (-2,2).GetValue(); //path width
y = startPosition.y + ( distanceStep * i / distance )*(newPosition.y - startPosition.y)+ NormalVariable (-2,2).GetValue();
// std::cout << "x: " << x << " y: "<< y<<std::endl;
Simulator::Schedule (Seconds (time+i), &MobilityModel::SetPosition, mobilityModel,
Vector( x, y, 0.0));
}
return time+ceil(MovementTime);
}
For example Node 10 moved from point A to point B at simulation time 100 and this movement took 50 seconds thus return simulation time 150. However if simulation times ends at 140 seconds, then this function will return 140 as simulation ended in the middle of the trajectory.
Upvotes: 1