Beispiel #1
0
def update_speed_factor(car):
    """
    handles logic for updating speed according to road curvature and car obstacles

    :param            car: Series
    :return: final_factor: double
    """
    frontview = nav.FrontView(car, stop_distance)
    angles = frontview.angles
    distance_to_node = car['distance-to-node']
    distance_to_car = car['distance-to-car']
    distance_to_red_light = car['distance-to-red-light']
    curvature_factor = road_curvature_factor(car, angles, distance_to_node)

    if distance_to_car and distance_to_red_light:
        if distance_to_car <= distance_to_red_light:
            final_factor = obstacle_factor(distance_to_car)
        else:
            final_factor = obstacle_factor(distance_to_red_light)
    else:
        if distance_to_car and not distance_to_red_light:
            car_factor = obstacle_factor(distance_to_car)
            if distance_to_car > distance_to_node:
                final_factor = models.weigh_factors(
                    car_factor, curvature_factor, distance_to_car, distance_to_node, free_distance
                )
            else:
                final_factor = car_factor
        else:
            if distance_to_red_light:
                final_factor = obstacle_factor(distance_to_red_light)
            else:
                final_factor = curvature_factor
    return abs(final_factor)
Beispiel #2
0
    def find_obstacles(self):
        node_distances, car_distances, light_distances = [], [], []
        for car in self.state.iterrows():
            frontview = nav.FrontView(car[1], stop_distance=self.stop_distance)
            node_distances.append(frontview.distance_to_node())
            car_distances.append(frontview.distance_to_car(self.state))
            light_distances.append(frontview.distance_to_light(self.lights))

        return node_distances, car_distances, light_distances
Beispiel #3
0
def update_cars(cars, dt):
    """
    This function shortens the stored path of a car after determining if the car crossed the next node in the path
    Then calculates the direction and magnitude of the velocity

    :param       cars: dataframe
    :param         dt: double
    :return   package: four Series's suitable for the main dataframe
    """
    new_route = []
    new_xpaths = []
    new_ypaths = []
    new_vx = []
    new_vy = []
    new_times = []

    for car in cars.iterrows():
        xpath, ypath = np.array(car[1]['xpath']), np.array(car[1]['ypath'])
        if xpath.any() and ypath.any():
            # add to route timer
            new_times.append(car[1]['route-time'] + dt)

            frontview = nav.FrontView(car[1], stop_distance)

            # determine if the car has just crossed a node
            if frontview.crossed_node_event():
                new_xpaths.append(car[1]['xpath'][1:])
                new_ypaths.append(car[1]['ypath'][1:])
            else:
                new_xpaths.append(car[1]['xpath'])
                new_ypaths.append(car[1]['ypath'])

            next_node = np.array(frontview.upcoming_node_position())
            position = np.array(frontview.position)

            velocity_direction = models.unit_vector(next_node - position)
            velocity = velocity_direction * speed_limit * update_speed_factor(car[1])

            # if the car has stalled and accelerate() returns True, then give it a push
            if np.isclose(0, velocity, atol=0.1).all() and accelerate(car[1]):
                velocity += default_acceleration

            new_vx.append(velocity[0])
            new_vy.append(velocity[1])
        else:
            # end of route returns the final route time
            return None, None, None, 0, 0, car[1]['route-time']

    package = pd.Series(new_route), pd.Series(new_xpaths), pd.Series(new_ypaths), pd.Series(new_vx), \
        pd.Series(new_vy), pd.Series(new_times)

    return package
Beispiel #4
0
    def simulation_step(self, i):
        """
        make one step in the simulation

        :param         i: simulation step
        :return  arrived: bool
        """
        frontview = nav.FrontView(self.cars_object.state.loc[self.agent])
        end_of_route = frontview.end_of_route()
        if not end_of_route:
            if self.animate:
                self.animator.animate(i)
            else:
                self.lights_object.update(self.dt)
                self.cars_object.update(self.dt, self.lights_object.state)
            arrived = False
        else:
            arrived = True

        return arrived
Beispiel #5
0
def update_cars(cars, graph, dt):
    """
    This function shortens the stored path of a car after determining if the car crossed the next node in the path
    Then calculates the direction and magnitude of the velocity

    :param:       cars: dataframe
    :param:      graph: OGraph object from osm_request
    :param:         dt: double
    :return:     list: four Series's suitable for the main dataframe
    """
    new_route = []
    new_xpaths = []
    new_ypaths = []
    new_vx = []
    new_vy = []
    new_times = []

    for i, car in enumerate(cars.iterrows()):
        xpath, ypath = np.array(car[1]['xpath']), np.array(car[1]['ypath'])
        if xpath.any() and ypath.any():
            # add to route timer
            new_times.append(car[1]['route-time'] + dt)

            # initialize an obstacle scan of the frontal view
            frontview = nav.FrontView(car[1],
                                      graph,
                                      stop_distance=stop_distance)

            # determine if the car has just crossed a node
            if frontview.crossed_node_event():
                new_xpaths.append(car[1]['xpath'][1:])
                new_ypaths.append(car[1]['ypath'][1:])
            else:
                new_xpaths.append(car[1]['xpath'])
                new_ypaths.append(car[1]['ypath'])

            next_node = np.array(frontview.upcoming_node_position())
            position = np.array(frontview.position)

            velocity_direction = models.unit_vector(next_node - position)
            velocity = velocity_direction * speed_limit * update_speed_factor(
                car[1])

            # if the car has stalled and accelerate() returns True, then give it a push
            if np.isclose(0, velocity, atol=0.1).all() and accelerate(car[1]):
                velocity += default_acceleration

            new_vx.append(velocity[0])
            new_vy.append(velocity[1])
        else:
            # set empty paths
            new_xpaths.append([])
            new_ypaths.append([])
            # set null velocity
            new_vx.append(0)
            new_vy.append(0)
            # save final route time
            new_times.append(car[1]['route-time'])

    package = [
        pd.Series(new_route, dtype='float'),
        pd.Series(new_xpaths, dtype='object'),
        pd.Series(new_ypaths, dtype='object'),
        pd.Series(new_vx, dtype='float'),
        pd.Series(new_vy, dtype='float'),
        pd.Series(new_times, dtype='float')
    ]

    return package