## Controlling velocity with MATLAB

Typically: "How do I... ", "How can I... " questions
NFFUR
Posts: 2
Joined: 29 Nov 2019, 22:12

### Controlling velocity with MATLAB

Hello. I have a 6-DOF robot and I have a set of angular velocities and positions values taken at 10ms intervals of the actual robot joints. I want the robot model in the simulation to follow those values using MATLAB as the control method.

I was able to manipulate the joint positions using the following MATLAB code in a for loop:

Code: Select all

[returnCode]=vrep.simxSetJointTargetPosition(clientID,art_0,(A(i,1))*pi/180,vrep.simx_opmode_oneshot);
Where "A" represents a matrix with joint positions.

I want to be able to manipulate the joint velocities as well but the following function does not work:

Code: Select all

[returnCode]=vrep.simxSetJointTargetVelocity(clientID,art_0,B(i,1),vrep.simx_opmode_oneshot);
Where "B" represents a matrix with joint velocities.

One forum topic said to use child scripts in Coppeliasim and call them using MATLAB. I did that but I also could not control the velocities. I want to also ask if it is even possible to control the velocity. The joints are set to Force/Torque mode with the control loop enabled.

coppelia
Posts: 7605
Joined: 14 Dec 2012, 00:25

### Re: Controlling velocity with MATLAB

Hello,

if your joints are in force/torque mode, the motor enabled, and the control loop enabled, then you should modify the upper velocity limit, i.e. your joint is in position control, and the controller will try to bring the joint via a PID controller to the desired position by only taking into account the upper velocity limit.
That value for a joint can be adjusted programatically via: sim.setObjectFloatParameter(jointHandle,sim.jointfloatparam_upper_limit,upperVelLimit)
Keep in mind that a change will only take effect when simulation is not yet running.

Cheers

NFFUR
Posts: 2
Joined: 29 Nov 2019, 22:12

### Re: Controlling velocity with MATLAB

Thanks, but is there no other way to control the velocity when the simulation is running?

coppelia
Posts: 7605
Joined: 14 Dec 2012, 00:25

### Re: Controlling velocity with MATLAB

Well, yes, you can. But then you need to handle the joint control yourself, via a joint callback function. If you take the example control on that page, simply replace inData.velUpperLimit with another value, e.g.:

Code: Select all

function sysCall_jointCallback(inData)

...

local upperVel=sim.getFloatSignal("upperVelocityLimit")
if not upperVel then
upperVel=90*math.pi/180 -- default value
end

-- 4. Calculate the velocity needed to reach the position in one dynamic time step:
local maxVelocity=ctrl/inData.dynStepSize -- max. velocity allowed.
if (maxVelocity>upperVel) then
maxVelocity=upperVel
end
if (maxVelocity<-upperVel) then
maxVelocity=-upperVel
end

...

return outData
end
Then you can control the upper velocity limit via the float signal upperVelocityLimit.

Cheers