Troxid / vrep-api-python

Simple for use Python binding for Coppelia Robotics V-REP simulator (remote API)
GNU General Public License v2.0
29 stars 10 forks source link

Resetting the connection #2

Closed psychemedia closed 7 years ago

psychemedia commented 7 years ago

(Thanks for posting this example...)

I can get the Pioneer example running by executing the example code in a code cell in a Jupyter notebook, and stopping the cell execution stops the simulator. But if I then try to re-run the code cell, to rerun the demo, I get an error:

---------------------------------------------------------------------------
ReturnCommandError                        Traceback (most recent call last)
<ipython-input-2-10d78785080c> in <module>()
     38         return self._left_sensor.read()[1].distance()
     39 
---> 40 with VRep.connect("127.0.0.1", 19997) as api:
     41     r = PioneerP3DX(api)
     42     while True:

/usr/local/lib/python3.6/site-packages/pyrep/api.py in connect(ip, port)
     26             return VRepApi(res)
     27         else:
---> 28             raise ReturnCommandError(res)
     29 
     30     def __enter__(self):

ReturnCommandError: Undefined return code: -1

The only way I can reconnect from the notebook is to restart the notebook kernel and then re-run the cell?

Troxid commented 7 years ago

Hello @psychemedia. I think, that exception has been happen because the last connection was not closed. I will create closeConnection (simxFinish) method and add it to RAII method. If you want to avoid this error right now, you can open the connection in separate cell and don't stop them. Example: Cell [1] (don't touch this cell anymore):

# class definitions, imports, etc...
api = VRep.connect("127.0.0.1", 19997)

Cell [2]:

api.simulation.start() # the same thing that you manually press `Start` button in v-rep
r = PioneerP3DX(api)
for _ in range(100):
    rl = r.right_length()
    ll = r.left_length()
    if rl > 0.01 and rl < 10:
        r.rotate_left()
    elif ll > 0.01 and ll < 10:
        r.rotate_right()
    else:
        r.move_forward()
    time.sleep(0.1)
api.simulation.stop() # the same thing that you manually press `Stop` button in v-rep
Troxid commented 7 years ago

@psychemedia i had fixed this and now a connection is automatically close. Update the library by pip install 'git+https://github.com/Troxid/vrep-api-python' --upgrade and try again.

psychemedia commented 7 years ago

Great, thanks... that works far more cleanly:-)