## controlling motor from python

Typically: "How do I... ", "How can I... " questions
Usama1551
Posts: 11
Joined: 16 Feb 2021, 21:47

### controlling motor from python

I was previously controlling the motor from within CoppeliaSim, I am moving to python now. however I can not seem to make it work. this just an example I am trying to use on the pioneer_p3dx. I also tried on my own robot but it would not work. it worked for a few times however the motor only runs for a few seconds then stops. however now it does not move at all. the script already written in the pioneer is disabled. i tried both vrep.simx_opmode_blocking and vrep.simx_opmode_oneshot_wait

Code: Select all

import sim as vrep
import sys

vrep.simxFinish(-1)
clientID = vrep.simxStart('127.0.0.1', 19999, True, True, 5000, 5)

if clientID != -1:
print('Connected to remote API server')
print('Vision Sensor object handling')

else:
print('Not Connected to remote API server')
sys.exit('Could not Connect')

errorCode,L_wheel1 = vrep.simxGetObjectHandle(clientID, 'Pioneer_p3dx_leftMotor"', vrep.simx_opmode_blocking)
errorCode,R_wheel1 = vrep.simxGetObjectHandle(clientID, 'Pioneer_p3dx_rightMotor', vrep.simx_opmode_blocking)

vrep.simxSetJointTargetVelocity(clientID,R_wheel1,0.2,vrep.simx_opmode_streaming)
vrep.simxSetJointTargetVelocity(clientID,L_wheel1,0.2,vrep.simx_opmode_streaming)

Usama1551
Posts: 11
Joined: 16 Feb 2021, 21:47

### Re: controlling motor from python

Update I got it moving, however it stops, it does not continues moving. I added an infinite while loop to make it move

Usama1551
Posts: 11
Joined: 16 Feb 2021, 21:47

### Re: controlling motor from python

got it working

Usama1551
Posts: 11
Joined: 16 Feb 2021, 21:47

### Re: controlling motor from python

hello again, yeah I am having the issue were the motor just stops moving after a certain distance. I did not figure that one out.

sorry for the confusion

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

### Re: controlling motor from python

Hello,

could it be that the motors are limited in range?
If you do the same in a self-contained scene, i.e. without the python client, does it work correctly?

Also, you should use the simx_opmode_oneshot operation mode, when simply sending out a function/data, without expecting something is return.

Cheers

Usama1551
Posts: 11
Joined: 16 Feb 2021, 21:47

### Re: controlling motor from python

yeah without using python client there is no issue, I think the motors might have a limited to a range or running time. when the motor reach a certain distance the tick next the joint in coppelaism disappears . however every time the target velocity is increased the robot goes further. I thought maybe CoppeliaSim cuts off however the camera feed from the vision sensor is still broadcasting. this is the code I am using in a self-contained scene

Code: Select all

function sysCall_init()
L_wheel1 = sim.getObjectHandle('L_motor1')
L_wheel2 = sim.getObjectHandle('L_motor2')
L_wheel3 = sim.getObjectHandle('L_motor3')
L_wheel4 = sim.getObjectHandle('L_motor4')
L_wheel5 = sim.getObjectHandle('L_motor5')
L_wheel6 = sim.getObjectHandle('L_motor6')
L_wheel7 = sim.getObjectHandle('L_motor7')
L_wheel8 = sim.getObjectHandle('L_motor8')
L_wheel9 = sim.getObjectHandle('L_motor9')
L_wheel10 = sim.getObjectHandle('L_motor10')
L_wheel11 = sim.getObjectHandle('L_motor11')
L_wheel12 = sim.getObjectHandle('L_motor12')
L_wheel13 = sim.getObjectHandle('L_motor13')
R_wheel1 = sim.getObjectHandle('R_motor1')
R_wheel2 = sim.getObjectHandle('R_motor2')
R_wheel3 = sim.getObjectHandle('R_motor3')
R_wheel4 = sim.getObjectHandle('R_motor4')
R_wheel5 = sim.getObjectHandle('R_motor5')
R_wheel6 = sim.getObjectHandle('R_motor6')
R_wheel7 = sim.getObjectHandle('R_motor7')
R_wheel8 = sim.getObjectHandle('R_motor8')
R_wheel9 = sim.getObjectHandle('R_motor9')
R_wheel10 = sim.getObjectHandle('R_motor10')
R_wheel11 = sim.getObjectHandle('R_motor11')
R_wheel12 = sim.getObjectHandle('R_motor12')
R_wheel13 = sim.getObjectHandle('R_motor13')
RobotBase=sim.getObjectAssociatedWithScript(sim.handle_self)

speed = 5

end

function sysCall_actuation()

sim.setJointTargetVelocity(L_wheel1,-speed)
sim.setJointTargetVelocity(L_wheel2,-speed)
sim.setJointTargetVelocity(L_wheel3,-speed)
sim.setJointTargetVelocity(L_wheel4,-speed)
sim.setJointTargetVelocity(L_wheel5,-speed)
sim.setJointTargetVelocity(L_wheel6,-speed)
sim.setJointTargetVelocity(L_wheel7,-speed)
sim.setJointTargetVelocity(L_wheel8,-speed)
sim.setJointTargetVelocity(L_wheel9,-speed)
sim.setJointTargetVelocity(L_wheel10,-speed)
sim.setJointTargetVelocity(L_wheel11,-speed)
sim.setJointTargetVelocity(L_wheel12,-speed)
sim.setJointTargetVelocity(L_wheel13,-speed)
sim.setJointTargetVelocity(R_wheel1,-speed)
sim.setJointTargetVelocity(R_wheel2,-speed)
sim.setJointTargetVelocity(R_wheel3,-speed)
sim.setJointTargetVelocity(R_wheel4,-speed)
sim.setJointTargetVelocity(R_wheel5,-speed)
sim.setJointTargetVelocity(R_wheel6,-speed)
sim.setJointTargetVelocity(R_wheel7,-speed)
sim.setJointTargetVelocity(R_wheel8,-speed)
sim.setJointTargetVelocity(R_wheel9,-speed)
sim.setJointTargetVelocity(R_wheel10,-speed)
sim.setJointTargetVelocity(R_wheel11,-speed)
sim.setJointTargetVelocity(R_wheel12,-speed)
sim.setJointTargetVelocity(R_wheel13,-speed)
end

when using python on python pioneer_3x I got to turn constantly without it stopping. I checked the motor settings between pioneer_3x and my robot and they are the same. so I don't know how to change the motor limit if there is one.

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

### Re: controlling motor from python

When you say: "...when the motor reach a certain distance the tick next the joint in coppelaism disappears..." makes me think that the joints are not dynamically enabled anymore, after a certain time (i.e. they are not handled by the physics engine anymore). One of the reason could be that it is too far away from the origin and CoppeliaSim decides that it should not be simulated by the physics engine. Have a look at the setting dynamicActivityRange in system/usrset.txt, and try to increase it.

But there must be a good reason why the bouncing ball icon next to the joints are not displayed anymore after a certain time. Is there anything specific you do in-between?

Cheers

Usama1551
Posts: 11
Joined: 16 Feb 2021, 21:47

### Re: controlling motor from python

I mean the robot does not move that far my environment is only 10x10.

anyways when I changed the dynamicAcitivityRange it increased the range. I just have one qs is there a limit number for how large this number can be. since my robot will perform many tasks in a single simulation it might need a lot of motor work.

from running the simulation I do not do anything in between, so yeah I don't understand why it does that too. is it because I am using large amount of motors?

thanks for ur help anyways the issue is fixed

coppelia