Simulate stepper motor

Typically: "How do I... ", "How can I... " questions
Post Reply
SeveQ
Posts: 34
Joined: 25 Jul 2013, 20:46

Simulate stepper motor

Post by SeveQ » 28 Jul 2013, 09:31

Hi folks,

I'm working on a simulation scene and modelling our own robot. We use steppers for driving motors and I'd like to simulate them the way steppers work in real life: with a step count, a frequency and a direction.

How will I do that, if it's possible?

Thanks a lot.

Cheers,
Hendrik

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

Re: Simulate stepper motor

Post by coppelia » 28 Jul 2013, 14:13

Hello Hendrik,

The easiest is to set the joint into force/torque mode and activate the position control. Then, attach a non-threaded child script to the joint, and add the stepper code. The stepper code will in the end set the desired target position of the joint with simSetJointTargetPosition. And you could communicate with the child script via a signal or tube for instance.

Cheers

SeveQ
Posts: 34
Joined: 25 Jul 2013, 20:46

Re: Simulate stepper motor

Post by SeveQ » 28 Jul 2013, 16:34

Sounds promising, thanks so far.

If I've got it right, it's only possible to let a position controlled joint reach its target position with maximum force/torque or with maximum velocity, right? Does that mean that max. force/torque mode is going exceed the configured maximum velocity of the joint because force/torque remains constant for all velocities from 0 to infinity?

Assuming I'll use max. force/torque mode, is maximum velocity also taken into account? I'd like to build the model as close as possible to the real world robot which means I know the maximum torque the real motor is able to provide. Furthermore I need to be able to set the target position of the joint (in relative steps from its current position) and also, which is even more important, set the maximum velocity with which the joint should turn. It has to be possible to change the maximum velocity even while the joint is still turning.

All this needs to be controllable by my remote API client.

Is that possible?

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

Re: Simulate stepper motor

Post by coppelia » 28 Jul 2013, 22:52

Hello Hendrik,

Make sure to read this post first.

An ideal motor doesn't have a maximum velocity, and it will accelerate as long as the force/torque is higher than the load. A physics engine works a bit differently, and you need to specify a maximum velocity too. If that velocity is reached, then the velocity will be kept.

A joint that is in position control mode will have a modulated velocity (regular position control) or modulated force/torque (often used to simulate springs/dampers). You are of course also free to write your own low-level joint controller.

You can access a joint force/torque and max. velocity via following API functions:
or via the remote API:
But for a joint controller it is recommended to do things from a non-threaded child script or callback script that do not have communication delays and that operate synchronously.

Cheers

SeveQ
Posts: 34
Joined: 25 Jul 2013, 20:46

Re: Simulate stepper motor

Post by SeveQ » 29 Jul 2013, 15:56

Alright, I see.

I've had a look into the default joint controller script... and don't really get my head around it. The documentation of the callback scripts is a little thin in my book. You could go more into detail what the parameters are, where they come from, how to debug such a script and what it can do and what it can't... things like that. I can't figure out how the controller script works in detail and how I could work with it to build some kind of a stepper motor motion controller.

If I'm using the default position controller without a script I cannot set the target position of a joint higher than +180 degrees or lower than -180 since it overflows around the circle by 360 degrees in the opposite direction. But I need the joint to be able to turn to a virtually infinite angle, like, say, for example 19.200 microsteps which is 3 full turns = 1080 degrees into a specified direction. I need that to move the robot along a distance of 3 times the perimeter of the wheel. That's how a motion controller for a stepper motor works in real life, as you probably know. You can tell the motion controller of a stepper motor with 6400 microsteps per turn to move 19.200 steps and it will rotate the motor exactly 3 times.

Thanks for your help! V-REP is really great for simulation purposes. I don't really have access to our robot that often and since I'm the main software developer in our team, it's sometimes hard to make progress without ability to test the latest changes. Keep up the great work!

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

Re: Simulate stepper motor

Post by coppelia » 30 Jul 2013, 10:34

Hello Hendrik,

For your purpose you don't need a custom callback script. Just go with the default PID position controller and attach a child script to your joint, and add your stepper motor code there.

The joint range is limited when the joint is dynamically simulated, because this is a restriction that comes from the physics engine (ODE or Bullet). What you will have to do in your case is set your joint into cyclic mode, and your child script can keep track of the number of turns. So all communications to your joint have to transit via the child script that will handle the details. Here a simple example:

Code: Select all

if (simGetScriptExecutionCount()==0) then
	jointHandle=simGetObjectAssociatedWithScript(sim_handle_self)
	stepsPerTurn=1024
	currentStepPosition=0
	stepsPerSecond=100
end

dt=simGetSimulationTimeStep()
maxStepsNow=stepsPerSecond*dt
s=simGetIntegerSignal("desiredStepPosition")
if s then
	if currentStepPosition~=s then
		dx=s-currentStepPosition
		if (math.abs(dx)>maxStepsNow) then
			dx=maxStepsNow*dx/math.abs(dx)
		end
		currentStepPosition=currentStepPosition+dx
		simSetJointTargetPosition(jointHandle,currentStepPosition*math.pi*2/stepsPerTurn)
	end
end
The above code should run in a non-threaded child script attached to the joint you want to control. The above code is very simple, and just ment as an example.

From a different controller you can now specify a desired stepper motor position with simSetIntegerSignal("desiredStepPosition",desiredStepPosition).

Cheers

Post Reply