User Tools

Site Tools


vrep:socket-com-with-robot

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revision Previous revision
Next revision
Previous revision
Next revision Both sides next revision
vrep:socket-com-with-robot [2020/03/17 17:04]
admin
vrep:socket-com-with-robot [2020/04/16 19:55]
admin
Line 1: Line 1:
-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 :+Back to [[vrep:​main|V-REP Main Page]] 
 + 
 +The 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 :
   * connect to the server   * connect to the server
   * send commands to the robot   * send commands to the robot
Line 7: Line 9:
 On the server side of the socket, we use a [[https://​www.coppeliarobotics.com/​helpFiles/​en/​childScripts.htm|threaded script]] in Lua as we want to listen to the client'​s commands synchronously with the V-REP simulation loop. On the server side of the socket, we use a [[https://​www.coppeliarobotics.com/​helpFiles/​en/​childScripts.htm|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  [[https://​www.ensta-bretagne.fr/​zerr/​filerepo/​vrep/​myLittleBot.ttt|simple 3 wheels robot]] and we load it in V-REP.+We will use a  [[https://​www.ensta-bretagne.fr/​zerr/​filerepo/​vrep/​myLittleBotNoScript.ttt|simple 3 wheels robot]] and we load it in V-REP. You can rename it **myLittleBot.ttt**.
  
 {{:​vrep:​mylittlebot_view.png?​300|}} {{:​vrep:​mylittlebot_view.png?​300|}}
Line 29: Line 31:
 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. ​ 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 :+The [[https://​www.ensta-bretagne.fr/​zerr/​filerepo/​vrep/​simple_robot_control.lua|Lua code]] looks like this :
 <code lua> <code lua>
-function hex(s) +-- load pack library 
-   s=string.gsub(s,"(.)",​function (x) return string.format("​%02X",​string.byte(x)) end) +-- must have been added previoulsy in V-REP folder 
-   ​return s +-- if not done yetsee tutorial ​... 
-end +require ("​pack"​) ​
- +
-require ("​pack"​)+
 bpack=string.pack bpack=string.pack
 bunpack=string.unpack bunpack=string.unpack
  
 +-- define socket
 portNb = 33211 portNb = 33211
 serverOn = false serverOn = false
-connexionTimeout = 0.01 +connexionTimeout = 0.001 -- set 1 ms time out
-cntTimeout = 0+
 socket=require("​socket"​) socket=require("​socket"​)
 srv = nil srv = nil
Line 74: Line 74:
       -- if server is running, accept command and send back status       -- if server is running, accept command and send back status
       if serverOn then       if serverOn then
 + -- to prevent lock on accept, a timeout is set
  srv:​settimeout(connexionTimeout)  srv:​settimeout(connexionTimeout)
  clt1 = srv:​accept()  clt1 = srv:​accept()
- if clt1 == nil then + if clt1 ~= nil then 
-     cntTimeout = cntTimeout + 1 +     -- to prevent ​lock, a timeout is set 
- else +     ​--clt1:​settimeout(connexionTimeout)
-     -- to prevent ​locks, a timeout is set +
-     clt1:​settimeout(connexionTimeout)+
      -- get the data (command) from the client      -- get the data (command) from the client
      dataIn = clt1:​receive(nCharMessage)      dataIn = clt1:​receive(nCharMessage)
Line 165: Line 164:
 </​code>​ </​code>​
  
-The Python3 client code is as follows:+The [[https://​www.ensta-bretagne.fr/​zerr/​filerepo/​vrep/​simple_robot_control.py|Python3 client code]] is as follows:
 <code python> <code python>
 import socket import socket
Line 191: Line 190:
 def interrupt_handler(signal,​ frame): def interrupt_handler(signal,​ frame):
     print ('You pressed CTRL-C, end of communication with V-REP robot'​)     print ('You pressed CTRL-C, end of communication with V-REP robot'​)
 +    time.sleep(0.5)
     try:     try:
         sock.close()         sock.close()
Line 200: Line 200:
 signal.signal(signal.SIGINT,​ interrupt_handler) signal.signal(signal.SIGINT,​ interrupt_handler)
  
-# client ​connection ​every 50 ms+# client ​connects to server ​every 50 ms
 loop_duration = 0.050 loop_duration = 0.050
 while True: while True:
Line 242: Line 242:
                ​(tsock,​vrx[2],​vrx[3]*180.0/​math.pi,​vrx[4]*180.0/​math.pi))                ​(tsock,​vrx[2],​vrx[3]*180.0/​math.pi,​vrx[4]*180.0/​math.pi))
     else:     else:
-        print ("​tsock",​tsoc,"ms, bad data !!")+        print ("​tsock",​tsock,"ms, bad data !!")
  
     tsleep = loop_duration - (time.time()-t0)     tsleep = loop_duration - (time.time()-t0)
     if tsleep>​0:​     if tsleep>​0:​
-        time.sleep (tsleep) +        time.sleep (tsleep)  ​
-  +
 </​code>​ </​code>​
  
 +Go to [[vrep:​socket-com-with-robot|top]]
vrep/socket-com-with-robot.txt · Last modified: 2020/06/09 16:39 by admin