## Controlling velocity with MATLAB

Typically: "How do I... ", "How can I... " questions
NFFUR
Posts: 3
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: 8446
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: 3
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: 8446
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

codycichosz
Posts: 14
Joined: 06 Dec 2020, 00:39

### Re: Controlling velocity with MATLAB

@NFFUR did you ever implement this solution successfully?

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

### Re: Controlling velocity with MATLAB

nopeee. i had a deadline and couldn't do it before that so i stopped trying honestly

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

### Re: Controlling velocity with MATLAB

Have also a look at the Python examples (the approach is very similar in Matlab) located in programming/remoteApiBindings/python/python, for instance sendMovementSequence-pts.py: that script will basically send a sequence of configurations (together with a sequence of times), that will be executed in CoppeliaSim's scene movementViaRemoteApi.ttt.

Cheers

codycichosz
Posts: 14
Joined: 06 Dec 2020, 00:39

### Re: Controlling velocity with MATLAB

Thanks for the suggestion. I am trying to control 1 joint then replicate the same code for the others. I tried to follow that example and use

Code: Select all

sim.simxCallScriptFunction
to call the joint callback function for Joint 1 and send a float. But I am getting a return code of 8.

I then found an example at the bottom of this page here: https://www.coppeliarobotics.com/helpFi ... iption.htm that sends a double signal for the target position but I tried using my upper velocity and it seems to not change the joint velocity for joint 1. I get a return code of 1. Inside of my non-threaded child script for joint 1, I initialize the double signal with

Code: Select all

setFloatSignal
. Inside of my joint callback function I call the sent double signal using

Code: Select all

getFloatSignal
then return it as out data.

I wonder what I am doing wrong. Below is a link to the scene and MATLAB code.

Cody

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

### Re: Controlling velocity with MATLAB

First question: do the examples I mention work?

It is very difficult to follow what you are trying to explain. Also because you are mixing several things together and it is hard to guess what exactly your problem is. Best would be to solve one elemental problem at a time, and incrementally get your scene working.

Cheers

codycichosz
Posts: 14
Joined: 06 Dec 2020, 00:39

### Re: Controlling velocity with MATLAB

Yes, the examples you mentioned worked. I was able to run the files in Python and the robots in the scene would execute the movements with no issue.