본문 바로가기
AI/python or ros_Webot

Webots - Python ( PyCharm ), SSH, External Controller

by 몽돌리스트 2020. 7. 13.
반응형

PyCharm

PyCharm is a cross-platform integrated development environment (IDE), specifically for the Python language. It provides code analysis, a graphical debugger, an integrated unit tester and integration with version control systems (VCSes).

 

PyCharm is a possible alternative to using Webots built-in editor for Python. This chapter explains step-by-step how to configure PyCharm to edit a Python controller and run it. Although this chapter focuses on PyCharm, you should be able to configure any other Python IDE in a similar way.

Creation of the PyCharm Project

Once PyCharm is started, click on Open and then select the directory of the Webots robot controller that you want to modify. As an example, the driver sample controller is used here.

Open controller in PyCharm

In order to use the Webots Python API, it should be added to the project. This can be done from the File / Settings menu. In the Settings window, select the Project / Project Structure tab, then, the Add Content Root button can be used to add a new folder to the path, select the WEBOTS_HOME/lib/controller/python37 folder (or any other Python version).

Addition of the Webots controller library

The Webots Python API depends on the Webots CPP API, therefore, the path need to be modifed to include the Webots lib directory. This can be done from the Run / Edit Configurations menu. In the Run Configurations windows, press the + button and then select Python, then set the Script path to point to your python file and in the Environment variables define the path variable (i.e. PATH on Windows, LD_LIBRARY_PATH on Linux or DYLD_LIBRARY_PATH on macOS) to point to WEBOTS_HOME/lib/controller. On Windows, you should also add WEBOTS_HOME/msys64/mingw64/bin to your PATH environment variable. If you are using other libraries (e.g., the vehicle libraries, DARwIn-OP library, etc.), the path to the corresponding shared libraries should be added as well.

Addition of the Webots libraries to the pathRun the Controller

Once the PyCharm project configured, you can start Webots and open the desired world. To allow PyCharm to start the controller instead of Webots, set the controller of the robot to <extern> (see the Running Extern Robot Controllers chapter for more information about external controller).

Robot controller to external

The controller can now be started from PyCharm from the Run menu (if not already done, start the simulation in Webots).

Run controller from PyCharm

 

 

파이 참

PyCharm 은 특히 ​​Python 언어를위한 크로스 플랫폼 통합 개발 환경 (IDE)입니다. 코드 분석, 그래픽 디버거, 통합 단위 테스터 및 버전 제어 시스템 (VCSe)과의 통합을 제공합니다.

PyCharm 은 Python 용 Webots 내장 편집기를 사용하는 것의 대안입니다. 이 장에서는 Python 컨트롤러를 편집하고 실행 하도록 PyCharm  구성하는 방법을 단계별로 설명 합니다. 이 장에서는 PyCharm에 중점을두고 있지만 , 다른 방식으로 다른 Python IDE를 구성 할 수 있어야합니다.

PyCharm 프로젝트의 생성

PyCharm이 시작되면를 클릭 Open한 다음 수정할 Webots 로봇 컨트롤러의 디렉토리를 선택하십시오. 예를 들어, driver샘플 컨트롤러가 여기에 사용됩니다.

PyCharm에서 컨트롤러 열기

Webots Python API를 사용하려면 프로젝트에 추가해야합니다. 이것은 File/ Settings메뉴 에서 수행 할 수 있습니다 . 에서 Settings창에서 선택 Project/ Project Structure다음, 탭, Add Content Root버튼, 경로에 새 폴더를 추가 선택할 수 있습니다 WEBOTS_HOME/lib/controller/python37폴더 (또는 다른 파이썬 버전).

Webots 컨트롤러 라이브러리 추가

Webots Python API는 Webots CPP API에 의존하므로 Webots lib디렉토리 를 포함하도록 경로를 수정해야합니다 . 이것은 Run/ Edit Configurations메뉴 에서 수행 할 수 있습니다 . 에서 Run Configurations창, 언론 +버튼을 누른 다음 선택 Python후 설정 Script path은 파이썬 파일을 가리 키도록하고의 Environment variables(즉, 경로 변수 정의 PATHWindows에서, LD_LIBRARY_PATH리눅스 나 DYLD_LIBRARY_PATH맥 OS에) 지점까지를 WEBOTS_HOME/lib/controller. Windows에서, 당신은 또한 추가해야 WEBOTS_HOME/msys64/mingw64/bin당신에 PATH환경 변수. 다른 라이브러리를 사용하는 경우 (예를 들어,은 vehicle libraries, DARwIn-OP library등), 해당 공유 라이브러리에 대한 경로뿐만 아니라 추가해야합니다.

경로에 Webots 라이브러리 추가컨트롤러 실행

PyCharm 프로젝트가 구성 되면 Webots를 시작하고 원하는 세계를 열 수 있습니다. PyCharm 이 Webot 대신 컨트롤러를 시작 하도록하려면 로봇 컨트롤러를로 설정하십시오 <extern>( 외부 컨트롤러에 대한 자세한 내용은 Extern 로봇 컨트롤러 실행 장을 참조하십시오).

로봇 컨트롤러 외부

컨트롤러는 이제 메뉴의 PyCharm 에서 시작할 수 있습니다 Run(아직 수행하지 않은 경우 Webots에서 시뮬레이션을 시작하십시오).

PyCharm에서 컨트롤러 실행

 

 



Webots User Guide 

R2020a revision 2

Starting Webots Remotely ("ssh")

Webots can be started on a remote computer, by using ssh (or a similar) command. However, Webots will work only if it can get a X11 connection to a X-server running locally (on the same computer). It is currently not possible to redirect Webots graphical output to another computer.

Using the "ssh" Command

Here is the usual way to start from computer A, a Webots instance that will run on computer B:

$ ssh myname@computerB.org
$ export DISPLAY=:0.0
$ webots --mode=fast --stdout --stderr myworld.wbt

The first line logs onto computer B. The 2nd line sets the DISPLAY variable to the display 0 (and screen 0) of computer B. This will indicate to all X11 applications (including Webots) that they need to connect to the X-server running on the local computer: computer B in this case. This step is necessary because the DISPLAY variable is usually not set in an ssh session.

The last line starts Webots: the --mode=fast option enables the Fast simulation mode. The --mode=fast option makes the simulation run as fast as possible, without graphical rendering, which is fine because the graphical output won't be visible anyway from computer A. Options --stdout and --stderr are used to redirect Webots' output to the standard streams instead of Webots console, otherwise the output would not be visible on computer A.

At this point, Webots will start only if a X-server with proper authorizations is running on computer B. To ensure that this is the case, the simplest solution is to have an open login session on computer B, i.e., to have logged in using the login screen of computer B, and not having logged out. Unless configured differently, the ssh login and the screen login session must belong to the same user, otherwise the X-server will reject the connection. Note that the xhost + command can be used to grant access to the X-server to another user. For security reasons, the screen of the open session on computer B can be locked (e.g. with a screen-saver): this won't affect the running X-server.

Terminating the "ssh" Session

A little problem with the above approach is that closing the ssh session will kill the remote jobs, including Webots. Fortunately it is easy to overcome this problem by starting the Webots as a background job and redirecting its output to a file:

$ ssh myname@computerB.org
$ export DISPLAY=:0.0
$ webots --mode=fast --stdout --stderr myworld.wbt &> out.txt &
$ exit

The &> sign redirects into a text file the output that would otherwise appear in the ssh terminal. The & sign starts Webots as a background job: so the user can safely exit the ssh session, while Webots keeps running.

In this case the decision to terminate the job is usually made in the Supervisor controller code according to simulation specific criteria. The wb_supervisor_simulation_quit function can be used to automatically terminate Webots when the job is over.

 

Webots 사용자 안내서 

R2020a 개정 2

Webots를 원격으로 시작하기 ( "ssh")

Webot은 원격 컴퓨터에서 ssh(또는 유사한) 명령 을 사용하여 시작할 수 있습니다 . 그러나 Webot은 동일한 컴퓨터에서 로컬로 실행되는 X 서버에 X11 연결을 얻을 수있는 경우에만 작동합니다. 현재 Webots 그래픽 출력을 다른 컴퓨터로 리디렉션 할 수 없습니다.

"ssh"명령 사용

다음은 컴퓨터 B에서 실행되는 Webots 인스턴스 인 컴퓨터 A에서 시작하는 일반적인 방법입니다.

$ ssh myname@computerB.org
$ export DISPLAY=:0.0
$ webots --mode=fast --stdout --stderr myworld.wbt

첫 번째 줄은 컴퓨터 B에 로그온합니다. 두 번째 줄은 DISPLAY 변수를 컴퓨터 B의 표시 0 (및 화면 0)으로 설정합니다. 이는 실행중인 X 서버에 연결해야하는 모든 X11 응용 프로그램 (Webot 포함)에 표시됩니다. 로컬 컴퓨터 :이 경우 컴퓨터 B. 이 단계는 일반적으로 DISPLAY 변수가 ssh세션 에서 설정되지 않기 때문에 필요합니다 .

마지막 줄은 Webots를 시작합니다. --mode = fast 옵션은 빠른 시뮬레이션 모드를 활성화 합니다. --mode는 = 빠른 옵션은 그래픽 출력이 컴퓨터 A. 옵션에서 볼 어쨌든 수 없기 때문에 괜찮 그래픽 렌더링없이 가능한 한 빨리와 시뮬레이션, 실행, 수 --stdout  --stderr가 리디렉션 Webots에 사용됩니다 'Webots 콘솔 대신 표준 스트림으로 출력합니다. 그렇지 않으면 컴퓨터 A에서 출력을 볼 수 없습니다.

이때 Webots는 컴퓨터 B에서 적절한 권한을 가진 X 서버가 실행중인 경우에만 시작됩니다.이 경우를 위해 가장 간단한 해결책은 컴퓨터 B에서 열린 로그인 세션을 갖는 것입니다. 컴퓨터 B의 로그인 화면을 사용하고 로그 아웃하지 않았습니다. 다르게 구성되지 않는 한, ssh로그인 및 화면 로그인 세션은 동일한 사용자에 속해야합니다. 그렇지 않으면 X 서버가 연결을 거부합니다.  xhost +명령을 사용하여 다른 사용자에게 X 서버에 대한 액세스 권한을 부여 할 수 있습니다. 보안상의 이유로 컴퓨터 B의 열린 세션 화면을 잠글 수 있습니다 (예 : 화면 보호기 사용). 이는 실행중인 X 서버에 영향을 미치지 않습니다.

"ssh"세션 종료

위의 접근 방식의 약간의 문제는 ssh세션 을 닫으면 Webots를 포함한 원격 작업이 종료된다는 것입니다. 다행히도 Webots를 백그라운드 작업으로 시작하고 출력을 파일로 리디렉션하여이 문제를 쉽게 극복 할 수 있습니다.

$ ssh myname@computerB.org
$ export DISPLAY=:0.0
$ webots --mode=fast --stdout --stderr myworld.wbt &> out.txt &
$ exit

&> 기호는 다른 방법으로 ssh터미널에 표시되는 출력을 텍스트 파일로 리디렉션합니다 . & 기호는 Webots를 백그라운드 작업으로 시작하므로 sshWebots가 계속 실행되는 동안 사용자가 안전하게 세션을 종료 할 수 있습니다 .

이 경우 작업 종료 결정은 일반적으로 시뮬레이션 특정 기준에 따라 Supervisor 컨트롤러 코드 에서 이루어집니다 .  wb_supervisor_simulation_quit기능은 작업이 끝나면 Webot을 자동으로 종료하는 데 사용할 수 있습니다.

 



Running Extern Robot Controllers

This chapter describes extern robot controllers and how to use them.

Index

Introduction

Normally, Webots launches automatically the robot controller specified in the controller field of each Robot node. However, if this field is set to <extern>, no controller is launched and the robot will behave like if its controller field was an empty string, that is, the robot will not be controlled. But as soon as a Webots controller is launched manually on the same computer, it will attempt to connect to this <extern> robot controller in order to control this robot.

Usefulness

Running an extern robot controller requires that the controller is launched manually. This may seem inconvenient, but in several cases, it turns out to be very useful, because the user has full control over the controller process. For example, it may run it within a debugging environment, like gdb, a command line tool like Python shell, or within some Integrated Development Environment (IDE), such as Visual C++, Eclipse or PyCharm. Also, the standard output and error streams (stdout and stderr) remain under the user control and are not sent to the Webots console. It is even possible to read the standard input stream (stdin) like with any standard program.

Note: If the robot.synchronization field is set to TRUE Webots will wait for the extern controller to be launched, otherwise the simulation will run whether the controller is started or not.

Environment Variables

In order to be able to run an extern Webots controller, a number of environment variables should be set or extended. Please refer to the documentation of your operating system to set environment variables.

Generic Webots environment variables needed for all the controller languages:

 

Environment VariableTypical Value

Specific setup depending on the controller language:

C / C++ -> No specific setup is needed.

Setup

Different use cases are detailed here from the most simple to the most complex:

Single Simulation and Single Extern Robot Controller

You are running a single Webots simulation simultaneously on the same machine and this simulation has only one robot that you want to control from an extern controller. In this case, you simply need to set the controller field of this robot to <extern> and to launch the controller program from a console or from your favorite IDE.

Single Simulation and Multiple Extern Robot Controllers

You are running a single Webots simulation simultaneously on the same machine and this simulation has several robots that you want to control from extern controllers. In this case, for each robot that you want to control externally, you should set their controller field to <extern>. Then, in the environment from which you are going to launch the extern controller, you should define an environment variable named WEBOTS_ROBOT_NAME and set it to match the name field of the Robot node you want to control. Once this environment variable is set, you can launch your controller and it will connect to the extern robot whose name matches the one provided in the environment variable. You can repeat this for the other controllers, e.g., set a different value to the WEBOTS_ROBOT_NAME environment variable before starting a new controller, so that it will connect to a different robot.

Note: if the WEBOTS_ROBOT_NAME is not set, the controller will connect to the first extern robot found which is not already connected to an extern controller.

Multiple Concurrent Simulations

If you are running multiple simulations simultaneously on the same machine, then you need to indicate to your controller to which instance of Webots it should try to connect. This can be achieved by setting an environment variable named WEBOTS_PID with the PID (Process ID) of the running Webots instance to which you want to connect your controller. If that simulation has more than one extern controller, you may also set the WEBOTS_ROBOT_NAME environment variable to specify the robot to which your controller should connect.

Note: the environment variables can be set inside the controller program, before calling the wb_robot_init() function.

Running Extern Robot Controller with the Snap Version of Webots

In order to compile and execute extern controllers, the following environment variables should be set:

export WEBOTS_HOME=/snap/webots/current/usr/share/webots export LD_LIBRARY_PATH=$WEBOTS_HOME/lib/controller

Additionally, on Ubuntu 16.04 the following environment variables should be set:

export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/snap/webots/current/usr/lib/x86_64-linux-gnu export LD_PRELOAD=/snap/webots/current/usr/lib/x86_64-linux-gnu/libz.so

Because of the snap sand-boxing system, Webots has to use a special temporary folder to share information with robot controllers. When you launch the snap version of Webots, the launcher computes the WEBOTS_TMPDIR environment variable if it is not already set. This variable is computed from the SNAP_USER_COMMON environment variable which typically points to /home/username/snap/webots/common, a folder accessible by both Webots and your own programs. Similarly, the libController will automatically check this folder and its contents to determine if it should use it to communicate with Webots. It is recommended that you do not override this WEBOTS_TMPDIR environment variable, unless you want to experiment a different mechanism.

Example Usage

  1. Open for example the "WEBOTS_HOME/projects/robots/softbank/nao/worlds/nao_demo.wbt" world file.
  2. If the simulation was running, stop it and revert it.
  3. Then, open the Nao node in the scene tree and change its controller field from nao_demo to <extern>.
  4. Save the simulation, restart it and run it.
  5. Open the "WEBOTS_HOME/projects/robots/softbank/nao/controllers/nao_demo" folder from a terminal.
  6. Setup environment variables needed for a C/C++ controller as explained in the above section.
  7. Start the nao_demo controller manually from the terminal.
  8. You should see the Nao robot moving in the simulation, controlled by the nao_demo program you just started.


Extern 로봇 컨트롤러 실행

이 장에서는 외부 로봇 컨트롤러 및 사용 방법에 대해 설명합니다.

인덱스

소개

일반적으로 Webots controller는 각 로봇 노드  필드에 지정된 로봇 컨트롤러를 자동으로 시작 합니다. 그러나이 필드를로 설정하면 <extern>컨트롤러가 시작되지 않고 controller필드가 빈 문자열 인 것처럼 로봇이 작동 합니다. 즉, 로봇이 제어되지 않습니다. 그러나 동일한 컴퓨터에서 Webots 컨트롤러를 수동으로 시작하면이 <extern>로봇 컨트롤러에 연결하여이 로봇을 제어 하려고 시도합니다 .

유용성

외부 로봇 컨트롤러를 실행하려면 컨트롤러를 수동으로 시작해야합니다. 이것은 불편 해 보일 수 있지만, 사용자가 컨트롤러 프로세스를 완전히 제어 할 수 있기 때문에 매우 유용한 것으로 판명되었습니다. 예를 들어, gdb 와 같은 디버깅 환경, Python shell 과 같은 명령 줄 도구 또는 Visual C ++ , Eclipse 또는 PyCharm 과 같은 일부 IDE (Integrated Development Environment) 내에서 실행할 수 있습니다 . 또한 표준 출력 및 오류 스트림 ( stdout및 stderr)은 사용자 정의 컨트롤로 유지되며 Webots 콘솔로 전송되지 않습니다. stdin표준 프로그램과 마찬가지로 표준 입력 스트림 ( ) 을 읽을 수도 있습니다 .

참고 : robot.synchronization필드가 TRUEWebots 로 설정 되면 extern 컨트롤러가 시작될 때까지 기다립니다. 그렇지 않으면 컨트롤러가 시작되었는지 여부에 관계없이 시뮬레이션이 실행됩니다.

환경 변수

extern Webots 컨트롤러를 실행할 수 있으려면 많은 환경 변수를 설정하거나 확장해야합니다. 환경 변수를 설정하려면 운영 체제 설명서를 참조하십시오.

설정

가장 간단한 것에서 가장 복잡한 것까지 다양한 사용 사례가 여기에 자세히 설명되어 있습니다.

단일 시뮬레이션 및 단일 Extern 로봇 컨트롤러

동일한 컴퓨터에서 단일 Webots 시뮬레이션을 동시에 실행하고 있으며이 시뮬레이션에는 외부 컨트롤러에서 제어하려는 로봇이 하나만 있습니다. 이 경우, controller이 로봇  필드 를 설정 <extern>하거나 콘솔 또는 선호하는 IDE에서 컨트롤러 프로그램을 시작 하기 만하면 됩니다.

단일 시뮬레이션 및 다중 Extern 로봇 컨트롤러

동일한 머신에서 단일 Webots 시뮬레이션을 동시에 실행하고 있으며이 시뮬레이션에는 외부 컨트롤러에서 제어하려는 여러 로봇이 있습니다. 이 경우 외부에서 제어하려는 각 로봇에 대해 해당 controller필드를로 설정해야 합니다 <extern>. 그런 다음 extern 컨트롤러를 시작할 환경에서 이름이 지정된 환경 변수를 정의하고 제어하려는 로봇 노드 WEBOTS_ROBOT_NAME의 name필드 와 일치하도록 설정해야합니다 . 이 환경 변수가 설정되면 컨트롤러를 시작할 수 있으며 환경 변수에 제공된 것과 일치하는 외부 로봇에 연결됩니다 . 다른 컨트롤러에 대해이 작업을 반복 할 수 있습니다 (예 : 다른 값을nameWEBOTS_ROBOT_NAME 다른 컨트롤러에 연결되도록 새 컨트롤러를 시작하기 전에 환경 변수.

참고 : WEBOTS_ROBOT_NAME이 설정되어 있지 않으면 컨트롤러가 외부 컨트롤러에 아직 연결되지 않은 첫 번째 외부 로봇에 연결됩니다.

다중 동시 시뮬레이션

동일한 컴퓨터에서 여러 시뮬레이션을 동시에 실행하는 경우 컨트롤러에 연결하려는 Webot의 인스턴스를 표시해야합니다. WEBOTS_PID컨트롤러를 연결하려는 실행중인 Webots 인스턴스의 PID (프로세스 ID)로 명명 된 환경 변수를 설정하면됩니다 . 해당 시뮬레이션에 둘 이상의 extern 컨트롤러가있는 경우 WEBOTS_ROBOT_NAME환경 변수를 설정 하여 컨트롤러를 연결해야하는 로봇을 지정할 수도 있습니다.

참고 : 환경 변수는 wb_robot_init()함수 를 호출하기 전에 컨트롤러 프로그램에서 설정할 수 있습니다 .

Webot의 Snap 버전으로 Extern Robot Controller 실행

extern 컨트롤러를 컴파일하고 실행하려면 다음 환경 변수를 설정해야합니다.

export WEBOTS_HOME=/snap/webots/current/usr/share/webots export LD_LIBRARY_PATH=$WEBOTS_HOME/lib/controller

또한 Ubuntu 16.04에서 다음 환경 변수를 설정해야합니다.

export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/snap/webots/current/usr/lib/x86_64-linux-gnu export LD_PRELOAD=/snap/webots/current/usr/lib/x86_64-linux-gnu/libz.so

스냅 샌드 복싱 시스템으로 인해 Webots는 로봇 컨트롤러와 정보를 공유하기 위해 특수한 임시 폴더를 사용해야합니다. Webots의 스냅 버전을 시작할 때 실행기는 WEBOTS_TMPDIR환경 변수가 설정되어 있지 않은 경우 환경 변수를 계산합니다 . 이 변수는 Webots와 자신의 프로그램에서 모두 액세스 할 수있는 폴더 인 SNAP_USER_COMMON환경 변수 에서 계산됩니다 /home/username/snap/webots/common. 마찬가지로 libController는이 폴더와 그 내용을 자동으로 확인하여 Webots와 통신하는 데 사용해야하는지 결정합니다. WEBOTS_TMPDIR다른 메커니즘을 실험하지 않는 한이 환경 변수를 대체하지 않는 것이 좋습니다 .

사용법 예

  1. "WEBOTS_HOME / projects / robots / softbank / nao / worlds / nao_demo.wbt"월드 파일을여십시오.
  2. 시뮬레이션이 실행 중이면 중지하고 되돌립니다.
  3. 그런 다음 장면 트리에서 Nao 노드를 열고 컨트롤러 필드를에서 nao_demo로 변경하십시오 <extern>.
  4. 시뮬레이션을 저장하고 다시 시작한 후 실행하십시오.
  5. 터미널에서 "WEBOTS_HOME / projects / robots / softbank / nao / controllers / nao_demo"폴더를여십시오.
  6. 위 섹션에서 설명한대로 C / C ++ 컨트롤러에 필요한 설정 환경 변수입니다.
  7. nao_demo터미널에서 컨트롤러를 수동으로 시작하십시오 .
  8. nao_demo방금 시작한 프로그램에 의해 제어되는 Nao 로봇이 시뮬레이션에서 움직이는 것을 볼 수 있습니다.

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

반응형

댓글