#! /usr/bin/perl # # create-map.pl: creates a circular road map for the freeway model. # The map is then read by the mobility generator found at # http://nile.usc.edu/important/software.htm, to create the mobility # for nodes in an ns2 simulation. # # Nathan Balon # University of Michigan - Dearborn # use strict; use warnings; use constant PI => 3.1415926535; use constant LANE_DIST => 5; # distance between lane # parameter used to generate a circular road my $nodes = 24; # the number of points on the circle my $inner_edge_dist = 300; # the distance to the inner lane my $traffic_direction = 2; # direction of traffic (either 1 or 2) my $lanes = 8; # number of lanes my $lane_dist = 0; # distance between the inner lane my @center = (500,500); # the center point of the circular road my $min_velocity = 20.0; # minimum velocity of the nodes my $max_velocity = 30.0; # maximum velocity of the nodes my $theta_const = 2 * PI / $nodes; # angle between nodes my $theta = 0; # theta value for the current node # Check that an even number of lanes exist if traffic # flows in both directions on the road. if($lanes % 2 != 0 && $traffic_direction == 2){ die "ERROR: if traffic flows in both direction, you must " . "have an even number of lanes\n"; } # display the information about the freeway print "FREEWAY\n"; print "FREEWAY_NUM 1\n"; print "LANE_NUM $lanes\n"; for(my $i = 0; $i < $lanes; $i++,$lane_dist += LANE_DIST){ $theta = 0; my $direction = 1; # set the phase of the traffic for traffic to # flow in the opposite direction for half the lanes if($i/$lanes >= 0.5 && $traffic_direction == 2){ $direction = -1; } # create a new lane in the map print "LANE_BEGIN 0 $i $i $direction $nodes\n"; # determine the position for two nodes connected by an edge # and display the results for(my $j = 0; $j < $nodes; $j++){ my $next_x = 0; my $next_y = 0; my $initial_x = $center[0] + cos($theta) * ($inner_edge_dist + $lane_dist); my $initial_y = $center[1] + sin($theta) * ($inner_edge_dist + $lane_dist); if($direction == 1){ $next_x = $center[0] + cos($theta + $theta_const) * ($inner_edge_dist + $lane_dist); $next_y = $center[1] + sin($theta + $theta_const) * ($inner_edge_dist + $lane_dist); $theta += $theta_const; }else{ # traffic going in the opposite direction $next_x = $center[0] + cos($theta - $theta_const) * ($inner_edge_dist + $lane_dist); $next_y = $center[1] + sin($theta - $theta_const) * ($inner_edge_dist + $lane_dist); $theta -= $theta_const; } printf("PHASE %d \(%4.5f\, %4.5f\) \(%4.5f\, %4.5f\) %3.3f %3.3f\n", $j, $initial_x, $initial_y, $next_x, $next_y, $min_velocity, $max_velocity); } }