Controlling Your Robot With Python: 1 Preparing The Environment
Controlling Your Robot With Python: 1 Preparing The Environment
October 9, 2018
1
1.3 Setting up a Server
Now, on VREP your scene needs to start a server so that the python scripts can connect
to it. To start the server you have to add a threaded script to an object in your scene
that will start the server.
• Open your bubbleRob scene
• Click on any of the cylinders you used as obstacles
• Open the scripts window (click on the “page” on the left hand side icons)
• Select the script on bubbleRob
• Click on “Disable”
• Close the scripts window. You will see an “x” next to the script for bubbleRob
And execute the program. You should see “Connected to remote API server” on
the screen.
You have effectively established communication with the scene.
2
2 Controling the Robot
We will make the robot move by accessing its motors. For this we need a handle to
control each individual motor.
The function vrep.simxGetObjectHandle will return an error code and a
handle to an object. Add the following to the code to obtain a handle to both motors of
the robot.
err_code,l_motor_handle = vrep.simxGetObjectHandle(clientID,"
bubbleRob_leftMotor", vrep.simx_opmode_blocking)
err_code,r_motor_handle = vrep.simxGetObjectHandle(clientID,"
bubbleRob_rightMotor", vrep.simx_opmode_blocking)
The parameters to the functions are specified in the Python remote API for VREP
at http://www.coppeliarobotics.com/helpFiles/en/remoteApiFunctionsPython.htm. In
this case, the vrep.simxGetObjectHandle, it needs a handle to the server clientID,
the name of the object, and a mode (in this case simx_opmode_blocking)
Now that we have handles to the motors, we can make them go with the following
code:
err_code = vrep.simxSetJointTargetVelocity(clientID,l_motor_handle,1.0,
vrep.simx_opmode_streaming)
err_code = vrep.simxSetJointTargetVelocity(clientID,r_motor_handle,1.0,
vrep.simx_opmode_streaming)
Switch to VREP and watch bubbleRob go. You can stop your simulation if bub-
bleRob goes out of the scene or gets trapped.
3 Sensing
Sensors work by detecting objects at a given point. They provide the information to
check the distance and coordinates of the detected object with respect to the sensor.
Sensors need to be initialized (first detection) and then used over and over again.
To initialize a sensor we call the simxReadProximitySensor function. Notice
the simx_opmode_streaming at the end. This function returns several variables.
err_code,ps_handle = vrep.simxGetObjectHandle(clientID,"
bubbleRob_sensingNose", vrep.simx_opmode_blocking)
err_code,detectionState,detectedPoint,detectedObjectHandle,
detectedSurfaceNormalVector=vrep.simxReadProximitySensor(clientID,
ps_handle,vrep.simx_opmode_streaming)
3
err_code,detectionState,detectedPoint,detectedObjectHandle,
detectedSurfaceNormalVector=vrep.simxReadProximitySensor(clientID,
ps_handle,vrep.simx_opmode_buffer)
Lastly, an important piece of information with proximity sensors is: How far is the
object I detect?
One of the returned values of simxReadProximitySensor is the detected
point relative to the sensor’s origin. The norm of that vector tells us the distance from
the sensor to the object. To obtain the norm we need to import numpy and then apply
the norm function like so:
import numpy as np #do this at the top of the program.
np.linalg.norm(detectedPoint)
err_code,l_motor_handle = vrep.simxGetObjectHandle(clientID,"
bubbleRob_leftMotor", vrep.simx_opmode_blocking)
err_code,r_motor_handle = vrep.simxGetObjectHandle(clientID,"
bubbleRob_rightMotor", vrep.simx_opmode_blocking)
err_code,ps_handle = vrep.simxGetObjectHandle(clientID,"
bubbleRob_sensingNose", vrep.simx_opmode_blocking)
err_code,detectionState,detectedPoint,detectedObjectHandle,
detectedSurfaceNormalVector=vrep.simxReadProximitySensor(clientID,
ps_handle,vrep.simx_opmode_streaming)
4
err_code = vrep.simxSetJointTargetVelocity(clientID,r_motor_handle
,1.0,vrep.simx_opmode_streaming)
time.sleep(0.2)
err_code,detectionState,detectedPoint,detectedObjectHandle,
detectedSurfaceNormalVector=vrep.simxReadProximitySensor(clientID,
ps_handle,vrep.simx_opmode_buffer)
print (sensor_val,detectedPoint)
vrep.simxStopSimulation(clientID,vrep.simx_opmode_oneshot)
print("Done")
Then, as with the proximity sensor, we need to do a first read by calling simxGetVisionSensorImage.
This function takes in the clientID, the handle to the vision sensor, a 0 or a 1 de-
pending on whether the picture is grayscale or color, and a mode.
As with the proximity sensor, the only difference between the first read and the
subsequent ones is the mode.
err_code,resolution,image = vrep.simxGetVisionSensorImage(clientID,
camera,0,vrep.simx_opmode_streaming)
The image returned is an array of values. Each pixel is represented by three values
(RGB for color and grayscale for non-color).
Therefore, the next step is to try to convert it to something readable, say a three
dimensional array where for every pixel (i, j) there’s a 3-value array with information
about its color.
To do this we need numpy. First, to convert the python array (image) into a numpy
array (or matrix) which is generally a more efficient array for numpy to work with.
Second, we tell numpy to group this array in columns, rows and triplets. That is,
we tell numpy that our representation of an image is of ixjx3 where i and j are the
resolution of the screen.
5
img = np.array(image, dtype = np.uint8)
img.resize([resolution[0],resolution[1],3])
5 Conclusion
Now you know how to connect to V-REP with python, move motors and read sensors
and images.
6 API Reference
To understand the python API reference, please follow Coppelia Python’s reference
API. Here you will find all the function calls you can do with your robot. Experiment
away!