Ejemplo n.º 1
0
 def communicate(self):
     """
     Performs data transfer between vehicles, i.e., fetching data from
     leading and front vehicles to feed the CACC algorithm. This function is
     an adaptation of the original that can be found in the Michele Segata's
     GitHub repository and in the utils.py file
     """
     for vid, links in self._topology.iteritems():
         # get data about platoon leader
         leader_data = utils.get_par(links["leader"], cc.PAR_SPEED_AND_ACCELERATION)
         (l_v, l_a, l_u, l_x, l_y, l_t) = cc.unpack(leader_data)
         leader_data = cc.pack(l_v, l_u, l_x, l_y, l_t)
         # get data about front vehicle
         front_data = utils.get_par(links["front"], cc.PAR_SPEED_AND_ACCELERATION)
         (f_v, f_a, f_u, f_x, f_y, f_t) = cc.unpack(front_data)
         front_data = cc.pack(f_v, f_u, f_x, f_y, f_t)
         # pass leader and front vehicle data to CACC
         if (l_a < 0 and f_a < 0) or (l_a > 0 and f_a > 0):
             utils.set_par(vid, cc.PAR_LEADER_SPEED_AND_ACCELERATION, leader_data)
         else:
             utils.set_par(vid, cc.PAR_LEADER_SPEED_AND_ACCELERATION, front_data)
         utils.set_par(vid, cc.PAR_PRECEDING_SPEED_AND_ACCELERATION, front_data)
         # compute GPS distance and pass it to the fake CACC
         f_d = gap_between_vehicles(vid, links["front"])[0]
         utils.set_par(vid, cc.PAR_LEADER_FAKE_DATA, cc.pack(l_v, l_u))
         utils.set_par(vid, cc.PAR_FRONT_FAKE_DATA, cc.pack(f_v, f_u, f_d))
Ejemplo n.º 2
0
def communicate(topology):
    """
    Performs data transfer between vehicles, i.e., fetching data from
    leading and front vehicles to feed the CACC algorithm
    :param topology: a dictionary pointing each vehicle id to its front
    vehicle and platoon leader. Each entry of the dictionary is a dictionary
    which includes the keys "leader" and "front"
    :type topology: dict[str, dict[str, str]]
    """
    for vid, links in topology.iteritems():
        # get data about platoon leader
        leader_data = get_par(links["leader"], cc.PAR_SPEED_AND_ACCELERATION)
        (l_v, l_a, l_u, l_x, l_y, l_t) = cc.unpack(leader_data)
        leader_data = cc.pack(l_v, l_u, l_x, l_y, l_t)
        # get data about front vehicle
        front_data = get_par(links["front"], cc.PAR_SPEED_AND_ACCELERATION)
        (f_v, f_a, f_u, f_x, f_y, f_t) = cc.unpack(front_data)
        front_data = cc.pack(f_v, f_u, f_x, f_y, f_t)
        # pass leader and front vehicle data to CACC
        set_par(vid, cc.PAR_LEADER_SPEED_AND_ACCELERATION, leader_data)
        set_par(vid, cc.PAR_PRECEDING_SPEED_AND_ACCELERATION, front_data)
        # compute GPS distance and pass it to the fake CACC
        f_d = get_distance(vid, links["front"])
        set_par(vid, cc.PAR_LEADER_FAKE_DATA, cc.pack(l_v, l_u))
        set_par(vid, cc.PAR_FRONT_FAKE_DATA, cc.pack(f_v, f_u, f_d))
Ejemplo n.º 3
0
def add_vehicles():
    """
    Adds the vehicles to the simulation
    """
    for i in range(len(VEHICLES)):
        vid = VEHICLES[i]
        add_vehicle(vid, 10, i, 0, 5)
        change_lane(vid, i)
        set_par(vid, cc.PAR_ACTIVE_CONTROLLER, cc.ACC)
        set_par(vid, cc.CC_PAR_VEHICLE_ENGINE_MODEL,
                cc.CC_ENGINE_MODEL_REALISTIC)
        set_par(vid, cc.CC_PAR_VEHICLES_FILE, "vehicles.xml")
        set_par(vid, cc.CC_PAR_VEHICLE_MODEL, vid)
        set_par(vid, cc.PAR_FIXED_ACCELERATION, cc.pack(1, 0))
Ejemplo n.º 4
0
def main(demo_mode, real_engine=True, setter=None):
    random.seed(1)
    start_sumo("cfg/freeway.sumo.cfg", False)
    step = 0

    while running(demo_mode, step, 4000):

        # when reaching 60 seconds, reset the simulation when in demo_mode
        if demo_mode and step == 4000:
            start_sumo("cfg/freeway.sumo.cfg", True)
            step = 0
            random.seed(1)

        traci.simulationStep()
        if step == 0:
            # create vehicles and track one of them
            add_vehicles()
            traci.gui.trackVehicle("View #0", VEHICLES[TRACK])
            traci.gui.setZoom("View #0", 20000)
        if step == 100:
            # at 1 second let them accelerate as much as they can
            for vid in VEHICLES:
                set_par(vid, cc.PAR_FIXED_ACCELERATION, cc.pack(1, 20))

        # in the dashboard, show the engine information about the vehicle
        # being tracked
        tracked_id = traci.gui.getTrackedVehicle("View #0")
        if setter is not None and tracked_id != "":
            (g, rpm) = cc.unpack(get_par(tracked_id, cc.PAR_ENGINE_DATA))
            data = get_par(tracked_id, cc.PAR_SPEED_AND_ACCELERATION)
            (v, a, u, x, y, t) = cc.unpack(data)
            setter(rpm, g, v, a)

        step += 1

    traci.close()