This is an old revision of the document!
Th socket communication allows for an external program to control the robot inside V-REP. The external program is a Python script. We will use for the socket a server/client communication. The server is written in Lua inside a threaded script in V-REP. The client is written in Python, It will :
On the server side of the socket, we use a threaded script in Lua as we want to listen to the client's commands synchronously with the V-REP simulation loop.
We will use a simple 3 wheels robot and we load it in V-REP.
The first thing to do is to add a threaded script to a object. We can choose to add it to the MainBody object but it can be any other object in the scene :
Right-Clic on the small icon on the left of MainBody and select Add→Associated child script→Threaded. After this a blue icon showing a text file should appear on the right of MainBody :
To edit, double-clic on this icon. You can edit directely in the V-REP script window. However, if you prefer using you preferred editor, you can remove all the Lua text code in the script and just type one line :
Then you will have to create a file called simple_robot_control.lua in the directory where your scene file (here myLittleBot.ttt) is stored.
The Lua code looks like this :
function hex(s) s=string.gsub(s,"(.)",function (x) return string.format("%02X",string.byte(x)) end) return s end require ("pack") bpack=string.pack bunpack=string.unpack portNb = 33211 serverOn = false connexionTimeout = 0.01 cntTimeout = 0 socket=require("socket") srv = nil clt1 = nil nCharMessage = 10 function sysCall_threadmain() -- initialization -- get left and right motors handles leftMotor = sim.getObjectHandle("MotorLeft") rightMotor = sim.getObjectHandle("MotorRight") -- control loop while true do -- get simulation time at beginning of the loop simTime = sim.getSimulationTime() -- if server is not running, start it if not serverOn then printToConsole ("not connected") srv = assert(socket.bind('127.0.0.1',portNb)) if (srv==nil) then printToConsole ("bad connect") else printToConsole ("get socket") ip, port = srv:getsockname() printToConsole ("server ok at "..ip.." on port "..port) serverOn = true printToConsole ("connexion granted !!! ") end end -- if server is running, accept command and send back status if serverOn then srv:settimeout(connexionTimeout) clt1 = srv:accept() if clt1 == nil then cntTimeout = cntTimeout + 1 else -- to prevent locks, a timeout is set clt1:settimeout(connexionTimeout) -- get the data (command) from the client dataIn = clt1:receive(nCharMessage) if dataIn ~= nil then -- unpack the received data with following format : -- char : first synchro char 'A' -- char : second synchro char 'Z' -- float : speed left -- float : speed right nrd1,ch1,ch2,speedLeft,speedRight = bunpack(dataIn,"AAff") if ch1 ~= 'A' or ch2 ~= 'Z' then print ("bad data from py client ...") else -- apply the command sim.setJointTargetVelocity(leftMotor,speedLeft) sim.setJointTargetVelocity(rightMotor,speedRight) end -- prepare the status to send back the client : -- simulation time and wheel angular position (radians) wheelAngleLeft = sim.getJointPosition(leftMotor) wheelAngleRight = sim.getJointPosition(rightMotor) ch1 = 'A' -- sync code 1 ch2 = 'Z' -- sync code 2 -- pack the status data dataPacked = bpack("AAfff",ch1,ch2,simTime,wheelAngleLeft,wheelAngleRight) -- Send the status data back to the client: clt1:send(dataPacked) else printToConsole ("no data") end clt1:close() end end end end function sysCall_cleanup() -- Put some clean-up code here end
The socket transfers binary data, so we need to pack the data before sending them and unpack them at the reception. For Lua we use the lpack libary]. You can either download the code and recompile **the pack.so** shared library or get it [[https://www.ensta-bretagne.fr/zerr/filerepo/vrep/pack.so|already compiled here if you work on x86_64 linux computer. The file pack.so should be added in the main V-REP directory.
The socket is between processes on the same computer so the IP address is localhost or 127.0.0.1. The communication port is set to 33211. The port number is arbitrary, if this port is already used, we will get an error message. To get a list of used port, you can use lsof. Here we print only the ports used by V-REP:
sudo lsof -i -P -n | grep LISTEN | grep vrep [sudo] password for user: vrep 13871 newubu 29u IPv4 17679486 0t0 TCP *:19997 (LISTEN) vrep 13871 newubu 38u IPv4 20187130 0t0 TCP 127.0.0.1:33211 (LISTEN)
The Lua server expects to receive a command from the Python client with 4 values :
It has first to unpack these data with bunpack() function. Then it checks if the two first values are 'A' and 'Z'. This is just to ensures that the connection is synchronized properly. In principle, TCP/IP sockets already check if all is OK, so may no used it and transmit only the usefull values.
If the message received is OK (starting with 'A' and 'Z'), the Lua server applies the command to the left and right motors.
Finally it reads the status data. Here the status has 3 values : the simulation time and the angular position of the two wheels. The angular position of the wheels is very useful if we want to simulate odometers. The status message is made of five values :
The 5 values are packed with bpack() function and sent back to the python client.
The Python3 code of the client controls the robot by sending the motor speed for the two wheels. It can be executed via a terminal. For example to set the left motor speed to 0.5 and the right to -0.5, type:
python3 simple_robot_control.py 0.5 -0.5 sockect time=6.8 ms, simTime=4.85 s, left angle=-0.0, right angle=0.0 sockect time=3.9 ms, simTime=4.95 s, left angle=2.4, right angle=-2.4 sockect time=6.7 ms, simTime=4.95 s, left angle=3.8, right angle=-3.8 sockect time=7.0 ms, simTime=5.05 s, left angle=5.2, right angle=-5.2 sockect time=0.4 ms, simTime=5.10 s, left angle=8.0, right angle=-8.0
The robot will turn in place to the right.
Do not forget to start the simulation in V-REP before executing the Python client code. Otherwise, the communication socket will not work :
python3 simple_robot_control.py 0.5 -0.5 Cannot connect to server