How to change the motor mode in MATLAB

Typically: "How do I... ", "How can I... " questions
baojw
Posts: 13
Joined: 08 Sep 2023, 03:05

How to change the motor mode in MATLAB

Post by baojw »

Hello, I am using zmq communication in MATLAB. I would like to require the
motor to be in position mode from 0 to 1 second and torque mode at other
times in the simulation. My code is:
if t<1
sim.setJointMode(jointHandle(3,2),sim.jointdynctrl_position,1);%Write the
motor handle in front
sim.setJointTargetPosition(jointHandle(3,2),-0.1);
else
sim.setJointMode(jointHandle(3,2),sim.jointdynctrl_force,1)
sim.setJointTargetForce(jointHandle(3,2), 200);
end
>
But there is no significant change in the motor's switching between these
two modes,But use modejoint=sim. getJointMode (jointHandle (3,2)); This
command changes the returned value from 1 to 8, as if it has already been
switched. I don't understand how to write code and how to set the motor.
Can you write a sample program for me if it's convenient. Thank you.
(Translated by the translation software, my English is not very good, and
there are some impolite words. Please forgive me and thank you again.)

coppelia
Site Admin
Posts: 10504
Joined: 14 Dec 2012, 00:25

Re: How to change the motor mode in MATLAB

Post by coppelia »

Hello,

you are using the wrong function. sim.setJointMode allows to set the joint in kinematic, dependent, or dynamic mode. But your joint is already and needs to remain in dynamic mode. Use one of those:

Code: Select all

sim.setObjectInt32Param(jointHandle,sim.jointintparam_dynctrlmode,sim.jointdynctrl_force)

sim.setObjectInt32Param(jointHandle,sim.jointintparam_dynctrlmode,sim.jointdynctrl_position)

...
Cheers

baojw
Posts: 13
Joined: 08 Sep 2023, 03:05

Re: How to change the motor mode in MATLAB

Post by baojw »

thank you very much!

baojw
Posts: 13
Joined: 08 Sep 2023, 03:05

Re: How to change the motor mode in MATLAB

Post by baojw »

I tried your code, and indeed the joint motor state can switch to position mode or torque mode over time. However, a new problem has arisen. When switching to position mode and sending the desired position to the joint motor, I found that the ‘Max.torque’ in the joint state is very small and insufficient to drive the motor to the desired position. This is my code:

if t<1
sim.setObjectInt32Param(jointHandle,sim.jointintparam_dynctrlmode,sim.jointdynctrl_position);
sim.setJointTargetPosition(jointHandle, p_out);
else
sim.setObjectInt32Param(jointHandle,sim.jointintparam_dynctrlmode,sim.jointdynctrl_force);
sim.setJointTargetForce(jointHandle, tao);
end

I tried to add a desired torque in the joint motor position mode, which is the following code:

if t<1
sim.setObjectInt32Param(jointHandle,sim.jointintparam_dynctrlmode,sim.jointdynctrl_position);
sim.setJointTargetPosition(jointHandle, p_out);
sim.setJointTargetForce(jointHandle, 500);
else
sim.setObjectInt32Param(jointHandle,sim.jointintparam_dynctrlmode,sim.jointdynctrl_force);
sim.setJointTargetForce(jointHandle, tao);
end

But it doesn’t seem to work. Can you advise me on how to modify it?Thank you very much

baojw
Posts: 13
Joined: 08 Sep 2023, 03:05

Re: How to change the motor mode in MATLAB

Post by baojw »

I tried your code, and indeed the joint motor state can switch to position mode or torque mode over time. However, a new problem has arisen. When switching to position mode and sending the desired position to the joint motor, I found that the ‘Max.torque’ in the joint state is very small and insufficient to drive the motor to the desired position. This is my code:

if t<1
sim.setObjectInt32Param(jointHandle,sim.jointintparam_dynctrlmode,sim.jointdynctrl_position);
sim.setJointTargetPosition(jointHandle, p_out);
else
sim.setObjectInt32Param(jointHandle,sim.jointintparam_dynctrlmode,sim.jointdynctrl_force);
sim.setJointTargetForce(jointHandle, tao);
end

I tried to add a desired torque in the joint motor position mode, which is the following code:

if t<1
sim.setObjectInt32Param(jointHandle,sim.jointintparam_dynctrlmode,sim.jointdynctrl_position);
sim.setJointTargetPosition(jointHandle, p_out);
sim.setJointTargetForce(jointHandle, 500);
else
sim.setObjectInt32Param(jointHandle,sim.jointintparam_dynctrlmode,sim.jointdynctrl_force);
sim.setJointTargetForce(jointHandle, tao);
end

But it doesn’t seem to work. Can you advise me on how to modify it?

coppelia
Site Admin
Posts: 10504
Joined: 14 Dec 2012, 00:25

Re: How to change the motor mode in MATLAB

Post by coppelia »

Your code looks ok. Can you provide a minimalistic, self-contained scene that illustrates the problem?
Does it also happen when using a child script, or only from MATLAB?

Cheers

baojw
Posts: 13
Joined: 08 Sep 2023, 03:05

Re: How to change the motor mode in MATLAB

Post by baojw »

Hello, I have found the bug and can now run normally. Thank you! I would also like to ask you, I am currently using V-rep+MATLAB to simulate MPC control, and the simulation speed is very slow. My computer is 12400+3060. Is there any way to make the simulation run faster?

coppelia
Site Admin
Posts: 10504
Joined: 14 Dec 2012, 00:25

Re: How to change the motor mode in MATLAB

Post by coppelia »

The next release (out in less than 2 weeks) should display quite some speed improvement, when using a ZeroMQ remote API client. But in general, the MATLAB version is quite slower than its Python, C++ counterparts.

Cheers

baojw
Posts: 13
Joined: 08 Sep 2023, 03:05

Re: How to change the motor mode in MATLAB

Post by baojw »

Hello, I have been working on a project using V-rep for the past few months. I will package each progress into a compressed file as a backup point. Previously, if I wanted to review previous projects, I only needed to open the corresponding compressed file at that time. However, today I suddenly encountered a bug and found that regardless of which compressed file was opened at any time, the simulation environment and content in V-rep are the same. I uninstalled V-rep before installing it, but it still doesn't work, And I found that the compressed package. ttt file for all time points is on the same date as August 25th. I used to save it this way before, and opening it would also correspond to the environment at that time point. Today, it suddenly stopped working. Can I still restore it?

baojw
Posts: 13
Joined: 08 Sep 2023, 03:05

Re: How to change the motor mode in MATLAB

Post by baojw »

已解决了,感谢

Post Reply