def do_max_speed(self, _): """ Gets 2 arguments: 1. A number *. Vehicle ids Sets the maximum speed given to the vehicle ids given. If none were given, sets the maximum speed given to all vehicles in the simulation. """ mandatory = 1 args = self.get_args(mandatory=mandatory) try: speed = float(args[0]) except ValueError: raise ClientError( f"Cannot change maximum speed to '{args[0]}', it's not a number!" ) vehicles = sg.VEHICLES if len( args) == mandatory else self.get_vehicles(args[mandatory:]) for v in vehicles: v.set_max_speed(speed) sg.LOG( f"Updated maximum speed to {speed} to {[v.id for v in vehicles]}")
def do_rmp(self, _): """ Removes the platoon from th simulation. """ sg.set_platoon(None) sg.LOG("Platoon removed")
def do_resume(self, _): """ Resumes the simulation if it was paused """ self.block.set() self.pause = False sg.LOG("Resumed")
def do_pause(self, _): """ Stops the simulation until the user allows to continue """ self.block.clear() self.pause = True sg.LOG("Paused")
def set_target(self, edge): """ Gets the id of a target edge, assumes the edge is legal. Sets the course of the vehicle to target edge. """ traci.vehicle.changeTarget(self.id, edge) self.target_edge = edge sg.LOG(f"Setting {self.id} target edge to {edge}")
def propagate(self): """ Activates the propagation algorithm of the platoon. """ platoon.size = 0 platoon.members = [self.focal_point.v] direction = self.focal_point.v.get_remaining_edges() self.focal_point.v.highlight() platoon_member.search_vehicles = sl.get_vehicles_in_direction( direction, exclude=[self.focal_point.v]) sg.LOG( f"Members for propagation: {[v.id for v in platoon_member.search_vehicles]}" ) self.focal_point.propagate(self.radius) sg.LOG(f"Platoon size: {platoon.size}")
def do_setp(self, _): """ Gets a single argument to be the a vehicle id. Creates a platoon object and sets the vehicle id given as the focal point. """ args = self.get_args(mandatory=1) vehicle_id = args[0] v = sl.get_vehicle(vehicle_id) sg.set_platoon(to.platoon(v, self.focal_direction)) sg.LOG(f"Set platoon on vehicle {v.id}")
def update_current_edge(self): """ Retrieves the value of the current edge of the vehicle and updates the self.curr_edge attribute """ updated_curr_edge = traci.vehicle.getRoadID(self.id) if updated_curr_edge[0] != ":" and self.curr_edge != updated_curr_edge: sg.LOG( f"Updating {self.id} current edge from {self.curr_edge} to {updated_curr_edge}" ) self.curr_edge = updated_curr_edge
def set_max_speed(self, max_speed): """ Sets the maximum speed the vehicle can drive. """ speed = self.get_speed() if max_speed < speed: raise TrafficObjectsError( f"Cannot change maximum speed of {self.id} to {max_speed}, speed is {speed}!" ) traci.vehicle.setMaxSpeed(self.id, max_speed) sg.LOG(f"Setting {self.id} maximum speed to {max_speed}")
def remove_vehicles(): """ Goes over every registered vehicle, removes vehicles that left the simulation """ left_vehicle_ids = [] for i, v in enumerate(sg.VEHICLES): if not is_vehicle(v.id): left_vehicle_ids.append(i) for i in reversed(left_vehicle_ids): sg.LOG(f"Removing vehicle: {sg.VEHICLES[i]}") sg.ID_TO_VEHICLE.pop(sg.VEHICLES[i].id) sg.VEHICLES.pop(i)
def set_speed(self, speed): """ Sets the speed of the vehicle to a given number """ max_speed = self.get_max_speed() if max_speed < speed: raise TrafficObjectsError( f"Cannot change speed of {self.id} to {speed}, max speed is {max_speed}!" ) traci.vehicle.setSpeed(self.id, speed) self.target_speed = speed sg.LOG(f"Setting {self.id} speed to {speed}")
def get_args(self, mandatory=0): """ Returns the arguments given to the command, according the the number of args expected. Can be given the the number of mandatory arguments expected. If mandatory is 0, assumes all arguments are optional. """ args = self.last_line.split(" ") sg.LOG(f"Got command: {args}") if mandatory > 0 and len(args) - 1 < mandatory: raise ClientError( f"Not enough arguments provided! Expected at least {mandatory}, got {len(args) - 1}" ) if len(args) == 1: return [] else: return args[1:]
def propagate(self, radius): """ Goes over the vehicles in the smulation and checks if they are in the radius of the current platoon member, if so, adds them to its member's list. Removes any member that was already acquired by another member. """ self.members_in_radius = [] vir = self.get_vehicles_in_radius(radius) sg.LOG(f"Vehicles in radius of {self.v.id}: {[v.id for v in vir]}") for v in vir: if v.id == self.v.id: continue platoon.increment() # debug purpose platoon.add_vehicle(v) # debug purpose v.highlight(color=(0, 255, 0, 255)) self.members_in_radius.append(platoon_member(v)) for m in self.members_in_radius: m.propagate(radius)