PJLab-ADG / LimSim

LimSim & LimSim++: Integrated traffic and autonomous driving simulators with (M)LLM support
https://pjlab-adg.github.io/LimSim/
GNU General Public License v3.0
391 stars 31 forks source link

How to control ego vehicles #9

Closed AntaiXie closed 9 months ago

AntaiXie commented 10 months ago

Hello, I hope to use my own control algorithm to control the steering wheel angle and throttle of the EGO vehicle. How should I take over the control of the EGO vehicle? Can you tell me where to start modifying the Traci control of ego vehicles? The surrounding vehicles still use their original trajectory control.

wenjie-2000 commented 10 months ago

Hello, I hope to use my own control algorithm to control the steering wheel angle and throttle of the EGO vehicle. How should I take over the control of the EGO vehicle? Can you tell me where to start modifying the Traci control of ego vehicles? The surrounding vehicles still use their original trajectory control.

@AntaiXie I wonder if you just want to change the control algorithm and keep the original planning algorithm. If so, the part of Traci control is on lines 234-264 of the carFactory.py, which is called by the function vehMoveStep in model.py.

def controlSelf(
    self, centerx: float, centery: float,
    yaw: float, speed: float, accel: float
):
    x = centerx + (self.length / 2) * cos(yaw)
    y = centery + (self.length / 2) * sin(yaw)
    # x, y = centerx, centery
    angle = (pi / 2 - yaw) * 180 / pi
    if self._iscontroled:
        traci.vehicle.moveToXY(self.id, '', -1, x, y,
                                angle=angle, keepRoute=2)
        traci.vehicle.setSpeed(self.id, speed)
        if accel >= 0:
            traci.vehicle.setAccel(self.id, accel)
            traci.vehicle.setDecel(self.id, self.maxDecel)
        else:
            traci.vehicle.setAccel(self.id, self.maxAccel)
            traci.vehicle.setDecel(self.id, -accel)
    else:
        traci.vehicle.setLaneChangeMode(self.id, 0)
        traci.vehicle.setSpeedMode(self.id, 0)
        traci.vehicle.moveToXY(self.id, '', -1, x, y,
                                angle=angle, keepRoute=2)
        traci.vehicle.setSpeed(self.id, speed)
        if accel >= 0:
            traci.vehicle.setAccel(self.id, accel)
            traci.vehicle.setDecel(self.id, self.maxDecel)
        else:
            traci.vehicle.setAccel(self.id, self.maxAccel)
            traci.vehicle.setDecel(self.id, -accel)
        self._iscontroled = 1

If you only want to control the EGO vehicle, you can tell what type of vehicle is passed as an argument to the function vehMoveStep, which is called by the function updateVeh, they are on lines 504-521 of the model.py.

def vehMoveStep(self, veh: Vehicle):
    # control vehicles after update its data
    # control happens next timestep
    if veh.plannedTrajectory and veh.plannedTrajectory.xQueue:
        centerx, centery, yaw, speed, accel = veh.plannedTrajectory.pop_last_state(
        )
        try:
            veh.controlSelf(centerx, centery, yaw, speed, accel)
        except:
            return
    else:
        veh.exitControlMode()

def updateVeh(self):
    self.vehMoveStep(self.ego)
    if self.ms.currVehicles:
        for v in self.ms.currVehicles.values():
            self.vehMoveStep(v)

Hope that can help you~

AntaiXie commented 10 months ago

Thank you very much for your answer. I would like to use reinforcement learning algorithms to take over the control of the EGO vehicle, but I want to maintain interaction with surrounding vehicles. If I understand correctly, all vehicles outside of the EGO vehicle within the circle are taken over by the model in your code, right? I also encountered a problem where the traffic flow and ID do not match. When I use traffic flow in my rou.xml file, there will be an ID mismatch problem. How can I solve this problem? Looking forward to your reply.

image Traceback (most recent call last): File "D:\Code\LimSim-master\ModelExample.py", line 89, in run_model(net_file, rou_file, ego_veh_id="Y", carla_cosim=False) File "D:\Code\LimSim-master\ModelExample.py", line 76, in run_model trajectories = planner.plan( File "D:\Code\LimSim-master\trafficManager\traffic_manager.py", line 136, in plan vehicle.update_behaviour(roadgraph, KEY_INPUT) File "D:\Code\LimSim-master\trafficManager\common\vehicle.py", line 184, in update_behaviour f"In available_lanes? {current_lane.id in self.available_lanes}") AttributeError: 'NoneType' object has no attribute 'id'

Fdarco commented 9 months ago

Thank you very much for your answer. I would like to use reinforcement learning algorithms to take over the control of the EGO vehicle, but I want to maintain interaction with surrounding vehicles. If I understand correctly, all vehicles outside of the EGO vehicle within the circle are taken over by the model in your code, right? I also encountered a problem where the traffic flow and ID do not match. When I use traffic flow in my rou.xml file, there will be an ID mismatch problem. How can I solve this problem? Looking forward to your reply. image Traceback (most recent call last): File "D:\Code\LimSim-master\ModelExample.py", line 89, in run_model(net_file, rou_file, ego_veh_id="Y", carla_cosim=False) File "D:\Code\LimSim-master\ModelExample.py", line 76, in run_model trajectories = planner.plan( File "D:\Code\LimSim-master\trafficManager\traffic_manager.py", line 136, in plan vehicle.update_behaviour(roadgraph, KEY_INPUT) File "D:\Code\LimSim-master\trafficManager\common\vehicle.py", line 184, in update_behaviour f"In available_lanes? {current_lane.id in self.available_lanes}") AttributeError: 'NoneType' object has no attribute 'id'

Hello, thank you for using LimSim. first of all, you understand correctly that the vehicles within the AoI, will be controlled by our planner. Secondly, the problem of vehicle ID mismatch can probably be solved by restarting the simulation. If not, please provide us the road network and rou.xml file, we will try to reproduce and debug.