You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
145 lines
6.5 KiB
145 lines
6.5 KiB
# Make sure to have the server side running in CoppeliaSim:
|
|
# in a child script of a CoppeliaSim scene, add following command
|
|
# to be executed just once, at simulation start:
|
|
#
|
|
# simRemoteApi.start(19999)
|
|
#
|
|
# then start simulation, and run this program.
|
|
#
|
|
# IMPORTANT: for each successful call to simxStart, there
|
|
# should be a corresponding call to simxFinish at the end!
|
|
|
|
try:
|
|
import sim
|
|
from sim import *
|
|
except:
|
|
print ('--------------------------------------------------------------')
|
|
print ('"sim.py" could not be imported. This means very probably that')
|
|
print ('either "sim.py" or the remoteApi library could not be found.')
|
|
print ('Make sure both are in the same folder as this file,')
|
|
print ('or appropriately adjust the file "sim.py"')
|
|
print ('--------------------------------------------------------------')
|
|
print ('')
|
|
|
|
import time
|
|
|
|
print('Program started')
|
|
sim.simxFinish(-1) # just in case, close all opened connections
|
|
clientID=sim.simxStart('127.0.0.1',19999,True,True,5000,5) # Connect to CoppeliaSim
|
|
if clientID!=-1:
|
|
print ('Connected to remote API server')
|
|
|
|
# Now try to retrieve data in a blocking fashion (i.e. a service call):
|
|
res,objs=sim.simxGetObjects(clientID,sim.sim_handle_all,sim.simx_opmode_blocking)
|
|
if res==sim.simx_return_ok:
|
|
print ('Number of objects in the scene: ',len(objs))
|
|
else:
|
|
print ('Remote API function call returned with error code: ',res)
|
|
# time.sleep(2)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# # Now retrieve streaming data (i.e. in a non-blocking fashion):
|
|
# startTime=time.time()
|
|
# sim.simxGetIntegerParameter(clientID,sim.sim_intparam_mouse_x,sim.simx_opmode_streaming) # Initialize streaming
|
|
# while time.time()-startTime < 5:
|
|
# returnCode,data=sim.simxGetIntegerParameter(clientID,sim.sim_intparam_mouse_x,sim.simx_opmode_buffer) # Try to retrieve the streamed data
|
|
# if returnCode==sim.simx_return_ok: # After initialization of streaming, it will take a few ms before the first value arrives, so check the return code
|
|
# print ('Mouse position x: ',data) # Mouse position x is actualized when the cursor is over CoppeliaSim's window
|
|
# time.sleep(0.005)
|
|
|
|
# Now send some data to CoppeliaSim in a non-blocking fashion:
|
|
sim.simxAddStatusbarMessage(clientID,'Hello CoppeliaSim!',sim.simx_opmode_oneshot)
|
|
|
|
res,retInts,retFloats,retStrings,retBuffer=sim.simxCallScriptFunction(clientID,'bubbleRob',sim.sim_scripttype_childscript,
|
|
'getRobotPos',[],[],[],'',sim.simx_opmode_blocking)
|
|
if res==sim.simx_return_ok:
|
|
print (retInts)
|
|
print (retFloats)
|
|
print (retStrings)
|
|
print (retBuffer)
|
|
res,retInts,retFloats,retStrings,retBuffer=sim.simxCallScriptFunction(clientID,'bubbleRob',sim.sim_scripttype_childscript,
|
|
'setRobotPos',[],[0,-2,0.12,0,0,0],[],'',sim.simx_opmode_blocking)
|
|
if res==sim.simx_return_ok:
|
|
print (retInts)
|
|
print (retFloats)
|
|
print (retStrings)
|
|
print (retBuffer)
|
|
res,retInts,retFloats,retStrings,retBuffer=sim.simxCallScriptFunction(clientID,'bubbleRob',sim.sim_scripttype_childscript,
|
|
'getRobotPos',[],[],[],'',sim.simx_opmode_blocking)
|
|
if res==sim.simx_return_ok:
|
|
print (retInts)
|
|
print (retFloats)
|
|
print (retStrings)
|
|
print (retBuffer)
|
|
|
|
|
|
|
|
# Retrieve some handles:
|
|
res,bubbleRob=sim.simxGetObjectHandle(clientID,'bubbleRob#',sim.simx_opmode_oneshot_wait)
|
|
res,bubbleRob_leftMotor=sim.simxGetObjectHandle(clientID,'bubbleRob_leftMotor#',sim.simx_opmode_oneshot_wait)
|
|
res,bubbleRob_rightMotor=sim.simxGetObjectHandle(clientID,'bubbleRob_rightMotor#',sim.simx_opmode_oneshot_wait)
|
|
res,bubbleRob_leftWheel=sim.simxGetObjectHandle(clientID,'bubbleRob_leftWheel#',sim.simx_opmode_oneshot_wait)
|
|
res,bubbleRob_rightWheel=sim.simxGetObjectHandle(clientID,'bubbleRob_rightWheel#',sim.simx_opmode_oneshot_wait)
|
|
res,bubbleRob_sensingNose=sim.simxGetObjectHandle(clientID,'bubbleRob_sensingNose#',sim.simx_opmode_oneshot_wait)
|
|
res,bubbleRob_visionSensor=sim.simxGetObjectHandle(clientID,'bubbleRob_visionSensor#',sim.simx_opmode_oneshot_wait)
|
|
res,bubbleRob_slider=sim.simxGetObjectHandle(clientID,'bubbleRob_slider#',sim.simx_opmode_oneshot_wait)
|
|
|
|
|
|
# getResult,position=simxGetObjectPosition(clientID,bubbleRob,-1,sim.simx_opmode_streaming)
|
|
# print(position)
|
|
# getResult,position2=simxGetObjectPosition(clientID,bubbleRob_leftWheel,-1,sim.simx_opmode_buffer)
|
|
# print(position2)
|
|
# getResult,position3=simxGetObjectPosition(clientID,bubbleRob_rightWheel,-1,sim.simx_opmode_buffer)
|
|
# print(position3)
|
|
# getResult,position4=simxGetObjectPosition(clientID,bubbleRob_slider,-1,sim.simx_opmode_buffer)
|
|
# print(position4)
|
|
|
|
|
|
|
|
# getResult,angle=simxGetObjectOrientation(clientID,bubbleRob,-1,sim.simx_opmode_streaming)
|
|
# print(angle)
|
|
# getResult,angle=simxGetObjectOrientation(clientID,bubbleRob_leftWheel,-1,sim.simx_opmode_streaming)
|
|
# print(angle)
|
|
# getResult,angle=simxGetObjectOrientation(clientID,bubbleRob_rightWheel,-1,sim.simx_opmode_streaming)
|
|
# print(angle)
|
|
# getResult,angle=simxGetObjectOrientation(clientID,bubbleRob_slider,-1,sim.simx_opmode_streaming)
|
|
# print(angle)
|
|
|
|
|
|
# setResult=simxSetObjectPosition(clientID,bubbleRob,-1,[0,-2,0.12],sim.simx_opmode_oneshot)
|
|
# print(setResult)
|
|
# setResult=simxSetObjectPosition(clientID,bubbleRob_leftWheel,-1,[0.05,-1.9,0.04],sim.simx_opmode_oneshot)
|
|
# print(setResult)
|
|
# setResult=simxSetObjectPosition(clientID,bubbleRob_rightWheel,-1,[0.05,-2.1,0.04],sim.simx_opmode_oneshot)
|
|
# print(setResult)
|
|
# setResult=simxSetObjectPosition(clientID,bubbleRob_slider,-1,[-0.07,-2,0.025],sim.simx_opmode_oneshot)
|
|
# print(setResult)
|
|
|
|
|
|
# setResult=simxSetObjectOrientation(clientID,bubbleRob,-1,[0,0,90],sim.simx_opmode_oneshot)
|
|
# print(setResult)
|
|
# setResult=simxSetObjectOrientation(clientID,bubbleRob_leftWheel,-1,[-90,90,0],sim.simx_opmode_oneshot)
|
|
# print(setResult)
|
|
# setResult=simxSetObjectOrientation(clientID,bubbleRob_rightWheel,-1,[-90,90,0],sim.simx_opmode_oneshot)
|
|
# print(setResult)
|
|
# setResult=simxSetObjectOrientation(clientID,bubbleRob_slider,-1,[0,0,90],sim.simx_opmode_oneshot)
|
|
# print(setResult)
|
|
|
|
|
|
|
|
|
|
# Before closing the connection to CoppeliaSim, make sure that the last command sent out had time to arrive. You can guarantee this with (for example):
|
|
sim.simxGetPingTime(clientID)
|
|
|
|
# Now close the connection to CoppeliaSim:
|
|
sim.simxFinish(clientID)
|
|
else:
|
|
print ('Failed connecting to remote API server')
|
|
print ('Program ended')
|