diff --git a/_posts/2021-06-26-using-python-api-in-coppeliasim.md b/_posts/2021-06-26-using-python-api-in-coppeliasim.md new file mode 100644 index 0000000..6f2e413 --- /dev/null +++ b/_posts/2021-06-26-using-python-api-in-coppeliasim.md @@ -0,0 +1,222 @@ +--- +layout: post +title: Using PythonAPI in CoppeliaSim +tags: simulation Python API +description: Basic functions and Usage of PythonAPI in CoppeliaSim +--- +-- [Saad Hashmi](https://github.com/hashmis79) +* We can do many simulation tasks in CoppeliaSim, but the biggest turndown is that we will have to learn a whole new language called Lua. To overcome this CoppeliaSim provides us with an API with can connect CoppeliaSim to many languages including Python, C, C++. In this blog, we will be focusing on Setting up the Python API and some of its basic functions. + +We will be covering the basic functions of Python API and their use in simulation +* [Setting Up the Files](#setting-up-the-files) +* [Establishing Communication with V-Rep](#establishing-communication) +* [Retrieving Object Handles in python](#retrieving-object-handles-in-python) +* [Setting Actuator Velocities](#setting-actuator-velocities) +* [Retrieving Image data from Vrep to python](#retrieving-image-data) +* [Putting it all together](#putting-it-all-together) + + +## Setting Up the Files +* For Setting up the API, we should add the necessary files to our working directory that are needed for the API which can be found in the CoppeliaSim installation Folder + +1) Navigate to `CoppeliaRobotics\CoppeliaSimEdu\programming\remoteApiBindings\python\python` and copy all the .py files into the working directory +2) Navigate to `CoppeliaRobotics\CoppeliaSimEdu\programming\remoteApiBindings\lib\lib` + Depending on the System you are on, you can select the folder (Windows, Ubuntu 16/18, MacOS) + copy the .dll, .so, or the .dylib file respectively to your working directory. + +3) The next thing we want to do is that we have to create a threaded script in any component of the scene where you want to implement API. +
+ +
+4) In the Script we should add the following statement in the sysCall_threadmain() function + +```python +simRemoteApi.start(19990) +``` + +**Note: 19999 is the port that will be used for API communications, this can be defined by you.** + +Now you are all set for using the PythonAPI in CoppeliaSim. +## Establishing Communication +* For Establishing Communication we need to follow the following steps : +1) import the `sim` library in the code +2) Add the below statements to the code + +```python +sim.simxFinish(-1) + +clientID = sim.simxStart('127.0.0.1',19990,True,True,5000,5) +``` +**Note: The port being used in the statement should match the port number specified while setting up.** + +* For Testing the establishment of communication you have to run the following code after starting the simulation in CoppeliaSim : +```python +import sim +import sys + +sim.simxFinish(-1) + +clientID=sim.simxStart('127.0.0.1',19990,True,True,5000,5) + +if clientID != -1: + print("Connected to the remote API server") +else: + print("Connection not successful") + sys.exit("Could not connect") +``` +**Note: You have to run the Simulation before you run the Code or else the Connection would not be established** ++ +
+ +## Retrieving Object Handles in python +* Object handles can be considered as a key or an ID which a component possesses. It is used to provide commands to a component(For eg. joint Velocity to a motor). PythonAPI also has an inbuilt function for that: + +```python +sim.simxGetObjectHandle() +``` + +Parameters | +clientID: the client ID objectName: name of the object. operationMode: a remote API function operation mode. Recommended operation mode for this function is sim.simx_opmode_blocking |
+
Return Values | +returnCode: a remote API function return code handle: the Object handle | +
Parameters | +clientID: the client ID jointHandle: handle of the joint targetVelocity: target velocity of the joint (linear or angular velocity depending on the joint-type) operationMode: a remote API function operation mode. Recommended operation mode for this function is sim.simx_opmode_oneshot or sim.simx_opmode_streaming |
+
Return Values | +returnCode: a remote API function return code | +
Parameters | +clientID: the client ID sensorHandle: handle of the vision sensor options: image options, bit-coded:bit0 set: each image pixel is a byte (greyscale image), otherwise each image pixel is a rgb byte-triplet operationMode: a remote API function operation mode. Recommended operation mode for this function is sim.simx_opmode_streaming |
+
Return Values | +returnCode: a remote API function return code resolution: the resolution of the image (x,y) image: the image data |
+
Parameters | +clientID: the client ID operationMode: a remote API function operation mode. Recommended operation mode for this function is sim.simx_opmode_oneshot |
+
Return Values | +returnCode: a remote API function return code | +
+ +
+ +## Finding the rest of the Functions +* You can find the rest of the equivalent functions from the [PythonAPI Functions list](https://www.coppeliarobotics.com/helpFiles/en/remoteApiFunctionsPython.htm). You just have to search the Lua function's name and you will find the Python equivalent function and its description. + +## References Used +* [A video tutorial via a small project by Nikolai](https://youtu.be/SQont-mTnfM) +* [The official API documentation and Functions List provided by CoppeliaSim](https://www.coppeliarobotics.com/helpFiles/en/remoteApiFunctionsPython.htm) +* Learn more about streaming modes [here](https://www.coppeliarobotics.com/helpFiles/en/remoteApiConstants.htm#operationModes) +* For learning CoppeliaSim from scratch, go [here](https://www.youtube.com/watch?v=PwGY8PxQOXY&list=PLjzuoBhdtaXOoqkJUqhYQletLLnJP8vjZ). diff --git a/assets/posts/using-python-api-in-coppeliasim/PythonAPI_Adding_Script.gif b/assets/posts/using-python-api-in-coppeliasim/PythonAPI_Adding_Script.gif new file mode 100644 index 0000000..ab9bb5e Binary files /dev/null and b/assets/posts/using-python-api-in-coppeliasim/PythonAPI_Adding_Script.gif differ diff --git a/assets/posts/using-python-api-in-coppeliasim/PythonAPI_Final_Code.gif b/assets/posts/using-python-api-in-coppeliasim/PythonAPI_Final_Code.gif new file mode 100644 index 0000000..c46bf2c Binary files /dev/null and b/assets/posts/using-python-api-in-coppeliasim/PythonAPI_Final_Code.gif differ diff --git a/assets/posts/using-python-api-in-coppeliasim/PythonAPI_StartAPI.gif b/assets/posts/using-python-api-in-coppeliasim/PythonAPI_StartAPI.gif new file mode 100644 index 0000000..8c2707d Binary files /dev/null and b/assets/posts/using-python-api-in-coppeliasim/PythonAPI_StartAPI.gif differ