Install the Python API of the autonomous driving simulator LGSVL and execute the sample program

Introduction

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.

environment

Python API installation

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 .

Execution of sample program

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". python_api_1.png

The LG SVL Simulator will then display "API ready!". image.png

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. out.gif

Implementation example

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.

in conclusion

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

Install the Python API of the autonomous driving simulator LGSVL and execute the sample program
[AWS] Install node.js on EC2 instance and execute sample program
The story of Python and the story of NaN
Quickly install OpenCV 2.4 (+ python) on OS X and try the sample
Sample code to get the Twitter API oauth_token and oauth_token_secret in Python 2.7
Get the number of articles accessed and likes with Qiita API + Python
Set the process name of the Python program
[Python] A program that calculates the number of updates of the highest and lowest records
Touch the sample v20-python-samples of the OANDA v20 REST API wrapper library for Python
Until you install Caffe and run the sample
Summary of the differences between PHP and Python
The answer of "1/2" is different between python2 and 3
Specifying the range of ruby and python arrays
Compare the speed of Python append and map
About the * (asterisk) argument of python (and itertools.starmap)
A discussion of the strengths and weaknesses of Python
Build API server for checking the operation of front implementation with python3 and Flask
[Python] A program that counts the number of valleys
[Python] Get the text of the law from the e-GOV Law API
The process of installing Atom and getting Python running
List of sample program distribution sites for python books
Python --Explanation and usage summary of the top 24 packages
Visualize the range of interpolation and extrapolation with python
Referencing and changing the upper bound of Python recursion
Until you install Gauge and run the official sample
I checked out the versions of Blender and Python
Send and receive Gmail via the Gmail API using Python
Let's use the Python version of the Confluence API module.
PHP and Python samples that hit the ChatWork API
Install the latest stable Python with pyenv (both 2 and 3)
Specifies the function to execute when the python program ends
[Python] Use the Face API of Microsoft Cognitive Services
Install django on python + anaconda and start the server
Sample of getting module name and class name in Python
[Python] A program that compares the positions of kangaroos.
Call the API of Hatena Blog from Python and save your blog articles individually on your PC
How to start the PC at a fixed time every morning and execute the python program
[Python] A program to find the number of apples and oranges that can be harvested
the zen of Python
Try hitting the Twitter API quickly and easily with Python
Sample program and execution example of ensemble learning (Stacked generalization)
I just changed the sample source of Python a little.
[Python] Heron's formula functionalization and calculation of the maximum area
Measure the execution result of the program in C ++, Java, Python.
Sample image of Python API server for EC2 (public AMI)
[Python] Chapter 01-03 About Python (Write and execute a program using PyCharm)
[python] plot the values ​​before and after the conversion of yeojohnson conversion
Impressions of completing Term 1 of the Udacity autonomous driving engineer course
The process of making Python code object-oriented and improving it
The websocket of toio (nodejs) and python / websocket do not connect.
I want to know the features of Python and pip
[Python] I tried collecting data using the API of wikipedia
[Tips] Problems and solutions in the development of python + kivy
Sample of HTTP GET and JSON parsing with python of pepper
Play with the password mechanism of GitHub Webhook and Python
(One of the solutions) when pyenv install on macOS causes BUILD FAILED and Python installation fails.