Controling a Joint (Part 1: VREP Interface) - lavz-upiita/CoppeliaSim-V-REP- GitHub Wiki
This is the first of a two part tutorial to learn how to control an actuated joint trough external signals using the ROS communication. This first part will show how to generate a single joint mechanism on the VREP interface, and how to prepare it for am external ROS signal.
Primitive Shapes
Primitive shapes are simple geometric shapes that can be used to create complex bodies. VREP supports 5 primitive shapes: Plane, Disc, Cuboid, Sphere and Cylinder.
Creating a model
For this tutorial, we will construct a single prismatic joint mechanism using primitive cuboids.
1.- Select "Add" on the right click menu, then select "Primitive Shape", and "Cuboid". This can be done using the "Add" menu on the menu bar.
Then select the XYZ dimensions of the cuboid; for this tutorial we will use a 0.1 meters cube. This body will be the base of the mechanism.
Note: All shapes are added on the middle of the scene by default.
2.- Add a new cube of 0.05 meters. As mentioned before, all shapes are added on the middle of the scene, meaning the two cuboid will be superposed, and one most be moved. Select the second cuboid using the hierarchy list, and click on the move button.
Then introduce the new XYZ coordinates on the position tab. For this tutorial we change the Z coordinate to 0.125 meters, so the second cuboid will be above the first one. Note: The position coordinates are of the centroid of the shape.
3.- Select "Add" on the right click menu, then select "Joint", and "Prismatic". This can be done using the "Add" menu on the menu bar.
Note: All the joints are added on the middle of the scene with the direction of action along the positive Z axis.
First we move the joint so its centroid is positioned between both cuboids.
Then we rotate the joint so the axis of action (local Z axis) runs along the world X axis. Select the joint using the hierarchy list and click on the rotate button.
And rotate the joint 90 degrees along the XZ plane (BETA angle).
With this, all the pieces are accommodated. Finally we change the name of the pieces by double clicking the name of the piece in the hierarchy list. For this tutorial we use the names Base, Link, and q1 for the first, second cuboids and the prismatic joint respectively, this names will help to differentiate the pieces of the mechanism.
4.- All the pieces are on the scene, but is necessary to define the hierarchy of the mechanism. For this tutorial, the Base cube will be the parent body, and will be unite with the Link cube trough the joint. Select the joint q1 on the hierarchy list and drag it above the Base cube icon.
Now drag the Link cube above the q1 joint.
Now the mechanism is complete, and we are ready to add the script.
Making the script and enable motor
Add a non-threated child script to the Base body (See the tutorial). Copy the nest code to the script.
-- ROS Callback function
function subCallback(msg)
-- Each time a new message is publish, assign the new data as the new target velocity of the joint
sim.setJointTargetVelocity(hJointq1,msg.data)
end
-- Initialization function
function sysCall_init()
hJointq1=sim.getObjectHandle('q1') -- Object handler for the prismatic joint
-- ROS elements declarations
publisher=simROS.advertise('/q1pos','std_msgs/Float32') -- ROS publisher('topic','message type')
subscriber=simROS.subscribe('/ctrl','std_msgs/Float32','subCallback') -- ROS subscriber('topic','message type','callback function name')
end
function sysCall_actuation()
-- Publish the q1 joint position each actuation fase
simROS.publish(publisher,{data=sim.getJointPosition(hJointq1)})
end
For the model to work, we need to enable the motor of the prismatic joint. Select the joint on the hierarchy list and double click the joint icon (not the name) to open the properties window.
Click Show dynamic properties dialog button, and check Motor enable.
Testing the Model
It's time to check the program. Close VREP and type the next command on a linux terminal
$ roscore
Then restart VREP (See the tutorial). Start the simulation by clicking the Start button
Open a new terminal and type the next command
$ rostopic list
If the topics "/ctrl" and "/q1pos" are shown then the script works correctly and is ready to receive a ROS message.