VREP and ROS Communication (First program) - lavz-upiita/CoppeliaSim-V-REP- GitHub Wiki
This tutorial show how to write a simple script on V-Rep to communicate through ROSInterface.
Considerations
For this tutorial, ROS must be already installed, and V-Rep must have the plugin libv_repExtRosInteface.so.
For this tutorial, the V-Rep root directory (where the file vrep.sh is located) will be represented as VREP_ROOT.
Open V-REP
To communicate through ROS messages, V-Rep must load libv_repExtRosInteface.so at launch, for this to happen, the ROS master node must be running before launch V-Rep. Follow the next instructions:
- Open a terminal and start the ROS master node
$ roscore
- Open a new terminal open the V-REP application
$ cd VREP_ROOT && ./vrepsh
Create a child script
A child script can be added to any element on the simulation. For this example, select and right click on the DefaultCamera. Add a non-threaded child script form the menu: Add >> Associated child script >> Non threaded
The script can be accessed by double-click on the icon in front of the DefaultCamera.
Writing the script
A new child scrip have already the basic prototype function for the simulation to works.
Depending on the simulation elements not all the functions are necessary. For this example only four functions are needed, the callback function, the system init function (sysCall_init) , the actuation call function (sysCall_actuation), and the cleanup function (sysCall_cleanup).
The callback function is called every time a new message is published in the subscriber topic, a child script can have more than one callback functions (one for every subscriber), and all of them must be defined before the other functions. The system init function is called when the simulation starts, all the publishers ans subscribers must be defined on this function. The actuation call function is called every simulation cicle. Finally the cleanup function is called when the simulations stops, depending on the source, most script doesn't need the cleanup function.
The function for the child script are as follows:
Callback Function
function subscriber_callback(msg)
-- This is the subscriber callback function
sim.addStatusbarMessage('subscriber receiver following Float32: '..msg.data)
end
System Init Function
function sysCall_init()
-- The child script initialization
objectHandle=sim.getObjectAssociatedWithScript(sim.handle_self)
objectName=sim.getObjectName(objectHandle)
-- Check if the required RosInterface is there:
moduleName=0
index=0
rosInterfacePresent=false
while moduleName do
moduleName=sim.getModuleName(index)
if (moduleName=='RosInterface') then
rosInterfacePresent=true
end
index=index+1
end
-- Prepare the float32 publisher and subscriber (we subscribe to the topic we advertise):
if rosInterfacePresent then
publisher=simROS.advertise('/simulationTime','std_msgs/Float32')
subscriber=simROS.subscribe('/simulationTime','std_msgs/Float32','subscriber_callback')
end
end
Actuation Call Function
function sysCall_actuation()
-- Send an updated simulation time message:
if rosInterfacePresent then
simROS.publish(publisher,{data=sim.getSimulationTime()})
end
end
Cleanup Function
function sysCall_cleanup()
-- Following not really needed in a simulation script (i.e. automatically shut down at simulation end):
if rosInterfacePresent then
simROS.shutdownPublisher(publisher)
simROS.shutdownSubscriber(subscriber)
end
end
Testing the script
Start the simulation by clicking the start button
Now open a new terminal and list the ROS topics:
$ rostopic list
If the node "simulationTime" appears, the script works correctly, and you can echo the topic information:
$ rostopic echo /simulationTime