Minimal example: asynchronous mode#

It is time to run your first example!

Goals

  1. Test the installation of the DQ Robotics library.

  2. Get familiar with the class DQ_VrepInterface().

  3. Know the method get_object_pose().

  4. Understand the asynchronous mode (default mode).

Open the DQ_Robotics_lab.ttt scene (see One scene to rule them all). Suppose we want to get the position of object Frame_x via code. If you check the scene, you’ll notice that is located in the position [-1,0,0]. Therefore, we expect as result the pure quaternion -i.

For this example, we are going to use the asynchronous mode. This is the default operation mode of CoppeliaSim. This mode means that the simulation on CoppeliaSim will advance without taking into account the progress of your script. Alternatively, CoppeliaSim supports the synchronous mode. (see Minimal example: synchronous mode).

../../_images/frame_x_sc.png

To get the pose (position and orientation) of an object we need to use the method get_object_pose() of the class DQ_VrepInterface().

Roughly speaking, in the default mode (asynchronous mode), you need to do the following steps:

  1. Instantiate an object of the DQ_VrepInterface() class.

  2. Establish the connection to an specific IP and port. If you are running both the script and the simulation in the same computer, the default IP is 127.0.0.1 and the default port is 19997.

  3. Start the simulation.

  4. Do whatever you want to do. For instance, get the object pose.

  5. Stop the simulation.

  6. Disconnect.


Templates:#

The following templates are minimal scripts with the DQ_VrepInterface() class and contain good practices for Matlab, Python, and C++. Those are based on hundreds of feedbacks provided by the Maintainers of the DQ Robotics.

The templates show how to establish communication with a CoppeliaSim scene in asynchronous mode (default mode) using the default port. It’s assumed that both the script and the scene are running in the same computer (default IP).

../../_images/default_mode.png

template.m

 1clear all;
 2close all;
 3clc;
 4
 5
 6include_namespace_dq;
 7vi = DQ_VrepInterface();
 8
 9try
10    vi.connect('127.0.0.1', 19997);
11    vi.start_simulation();
12    pause(0.1);
13    %-----------Your code here-------------------
14
15    %----------------------------------------------
16    vi.stop_simulation();
17    vi.disconnect();
18catch ME
19    vi.stop_simulation();
20    vi.disconnect();
21    rethrow(ME)
22end

template.py

 1#!/bin/python3
 2from dqrobotics.interfaces.vrep import DQ_VrepInterface
 3import time
 4
 5vi = DQ_VrepInterface()
 6
 7
 8def main() -> None:
 9    vi = DQ_VrepInterface()
10    try:
11        if not vi.connect("127.0.0.1", 19997, 100, 10):
12            raise RuntimeError("Unable to connect to CoppeliaSim.")
13
14        vi.start_simulation()
15        time.sleep(0.1)
16        # --------------Your code here----------------------------
17
18        # ---------------------------------------------------------
19
20    except (Exception, KeyboardInterrupt) as e:
21        print(e)
22        pass
23
24    finally:
25        vi.stop_simulation()
26        vi.disconnect()
27
28
29if __name__ == "__main__":
30    main()

template.cpp

 1#include <iostream>
 2#include <dqrobotics/interfaces/vrep/DQ_VrepInterface.h>
 3#include <thread>
 4
 5int main()
 6{
 7    auto vi = DQ_VrepInterface();
 8    try {
 9        if (!vi.connect("127.0.0.1", 19997,100,10))
10            throw std::runtime_error("Unable to connect to CoppeliaSim.");
11        vi.start_simulation();
12        std::this_thread::sleep_for(std::chrono::milliseconds(100));
13        //-----------------Your code here--------------------------
14
15        //---------------------------------------------------------
16        vi.stop_simulation();
17        vi.disconnect();
18    } catch (std::exception& e) {
19        std::cout<<e.what()<<std::endl;
20        vi.stop_simulation();
21        vi.disconnect();
22    }
23    return 0;
24}

See also

CMake examples for Ubuntu, Windows and MacOS dqrobotics/cpp-examples

 1# Example CMAKE project for Ubuntu
 2make_minimum_required(VERSION 3.5)
 3
 4project(template)
 5
 6set(CMAKE_CXX_STANDARD 11)
 7
 8FIND_PACKAGE(Threads REQUIRED)
 9FIND_PACKAGE(Eigen3 REQUIRED)
10INCLUDE_DIRECTORIES(${EIGEN3_INCLUDE_DIR})
11ADD_COMPILE_OPTIONS(-Werror=return-type
12                    -Wall -Wextra -Wmissing-declarations
13                    -Wredundant-decls -Woverloaded-virtual)
14
15add_executable(${PROJECT_NAME}
16               ${PROJECT_NAME}.cpp)
17
18target_link_libraries(${PROJECT_NAME}
19                      dqrobotics
20                      dqrobotics-interface-vrep
21                      Threads::Threads)

Hint

From CoppeliaSim V4.3.0 and up, objects can be accessed with object names (deprecated) and object paths (recommended). See more in Accessing scene objects programmatically.


For instance: the deprecated name of the object Frame_x is Frame_x and its path corresponds to /Frame_x.

Warning

In case you use the object name, you are required to use the deprecated name. The deprecated name does not always coincide with the object name displayed in the scene hierarchy.

../../_images/deprecated_name.png

Example#

communication_test.m

 1clear all;
 2close all;
 3clc;
 4
 5
 6include_namespace_dq;
 7vi = DQ_VrepInterface();
 8
 9try
10    vi.connect('127.0.0.1', 19997);
11    vi.start_simulation();
12    pause(0.1);
13    x = vi.get_object_pose('/Frame_x');
14    disp('Position: ')
15    translation(x)
16    disp('The test was succesful!')
17    vi.stop_simulation();
18    vi.disconnect();
19catch ME
20    vi.stop_simulation();
21    vi.disconnect();
22    rethrow(ME)
23end

communication_test.py

 1#!/bin/python3
 2from dqrobotics.interfaces.vrep import DQ_VrepInterface
 3import time
 4
 5
 6
 7
 8def main() -> None:
 9    vi = DQ_VrepInterface()
10    try:
11        if not vi.connect("127.0.0.1", 19997, 100, 10):
12            raise RuntimeError("Unable to connect to CoppeliaSim.")
13
14        vi.start_simulation()
15        time.sleep(0.1)
16        x = vi.get_object_pose("/Frame_x")
17        print("Position: ", x.translation())
18        print("The test was successful!")
19
20    except (Exception, KeyboardInterrupt) as e:
21        print(e)
22        pass
23
24    finally:
25        vi.stop_simulation()
26        vi.disconnect()
27
28
29if __name__ == "__main__":
30    main()

communication_test.cpp

 1#include <iostream>
 2#include <dqrobotics/interfaces/vrep/DQ_VrepInterface.h>
 3#include <thread>
 4
 5int main()
 6{
 7    auto vi = DQ_VrepInterface();
 8    try {
 9        if (!vi.connect("127.0.0.1", 19997,100,10))
10            throw std::runtime_error("Unable to connect to CoppeliaSim.");
11        vi.start_simulation();
12        std::this_thread::sleep_for(std::chrono::milliseconds(100));
13        DQ x = vi.get_object_pose("/Frame_x");
14        std::cout<<"Position: "<<x.translation()<<std::endl;
15        std::cout<<"the test was successful!"<<std::endl;
16        vi.stop_simulation();
17        vi.disconnect();
18
19    } catch (std::exception& e) {
20        std::cout<<e.what()<<std::endl;
21        vi.stop_simulation();
22        vi.disconnect();
23    }
24    return 0;
25}

You will have the following output:

Position: - 1i
The test was successful!

See also

You can run the script and the simulation on different computers that are on the same local network. To do so, in connect() use the IP of the computer that is running the simulation. For instance, lets say that your simulation is running on a PC with the IP address 10.198.113.159. Then, in the example, you must replace 127.0.0.1 by 10.198.113.159.

vi.connect('10.198.113.159', 19997);
vi.connect("10.198.113.159", 19997, 100, 10)
vi.connect("10.198.113.159", 19997,100,10);
../../_images/requirements.png