In this article, I will introduce how to install the Python API of LGSVL, which is an autonomous driving simulator, how to execute a sample program, and a simple implementation example. The installation method and basic usage of LGSVL are introduced in this article in an easy-to-understand manner. Please refer to it. https://qiita.com/zacc/items/13c4e04bc04fb29c9aec
2020/05/21 Added a simple implementation example using Python API. Throttle control is performed so that the target speed is followed by proportional control.
Official Python API documentation: https://www.lgsvlsimulator.com/docs/python-api/ Python API repository: https://github.com/lgsvl/PythonAPI
Clone and install the Python API repository. LGSVL's Python API supports Python 3.5> =.
git clone https://github.com/lgsvl/PythonAPI.git
cd PythonAPI
pip install .
Try running the sample program in the Python API repository. The sample program is in a folder named "quickstart".
First, from the LGSVL browser, click the "Simulations tab"-> "API Only"-> "Play Button".
The LG SVL Simulator will then display "API ready!".
In this state, execute the sample program in the Python API repository.
python ./quickstart/05-ego-drive-in-circle.py
The following will be displayed, so press Enter.
Current time = 0
Current frame = 0
Press Enter to start driving
Then the vehicle keeps spinning around.
Here, we will use proportional control to throttle control the vehicle so that it follows the target speed. The code is below.
import lgsvl
import os
import numpy as np
def p_control(target_speed, current_speed):
"""
Proportional control
:param target_speed:Target speed
:param current_speed:Current speed
:return:Throttle value
"""
k_p = 0.05
error = target_speed - current_speed
throttle = np.clip(error * k_p, 0.0, 1.0)
return throttle
if __name__ == '__main__':
sim = lgsvl.Simulator(os.environ.get("SIMULATOR_HOST", "127.0.0.1"), 8181)
#Load map
#Specifies a map with no obstacles
map_name = "WideFlatMap"
if sim.current_scene == map_name:
sim.reset()
else:
sim.load(map_name)
#Get a list of transforms where you can spawn a vehicle
spawns = sim.get_spawn()
#Determine the initial state of the ego car
state = lgsvl.AgentState()
state.transform = spawns[0]
#Add an ego car to the simulator
ego = sim.add_agent("Lincoln2017MKZ (Apollo 5.0)", lgsvl.AgentType.EGO, state)
#Target speed[km/h]
target_speed = 50.0
#Control cycle[sec]
time_step = 0.05
while True:
#Current speed[km/h]Get
#Ignore the y-axis (perpendicular to the ground) velocity here
state = ego.state
current_speed = np.linalg.norm([state.velocity.x, state.velocity.z]) * 3.6
#Get throttle value
throttle = p_control(target_speed, current_speed)
#Apply control values to ego cars
control = lgsvl.VehicleControl()
control.throttle = throttle
ego.apply_control(control, True)
#Simulator time time_Step only
sim.run(time_limit=time_step)
#Output current speed to console
print("Current speed: {:.1f} [km/h]".format(current_speed))
If you execute this code in the same way as the execution procedure of the sample program above, it will go straight at the specified target speed.
You can use the Python API to control the vehicle, get sensor information, and much more. LGSVL seems to be able to work with self-driving software such as Autoware, but I think it is possible to create an original self-driving car using the Python API. I would like to write an article about using LGSVL's Python API. That was a brief explanation, but that's it. There are many other sample programs, so please give it a try!
Recommended Posts