Go Down

### Topic: Stepper Absolute Position ... how to (Read 6769 times)previous topic - next topic

#### Silverdog63

##### Jan 27, 2013, 11:59 pm
I am bulding an actuator with a stepper motor, to control the height of a router /shaper.
I need to choose and execute two tipe of movement: Absolute and Relative.
Relative is easy for me (beginner):

stepper1.moveTo(MoveX * 50); // move Height1 mm's
where MoveX is the variable I set (es. 30mm) 50 is the number of step to make 1mm
Each time I set the MoveX variable I move of X mm.
Pretty straightforward.
But with the absolute height I am in trouble.
The first thing to do is to set the Zero point each time the sistem start (that will be done with a limit switch)
So I have my starting point, then I set my variable that I call HeightZ, the fist step is easy, if I set HeightZ=20mm the stepper will go there with 20*50=1000 steps. From now all the new movement have to consider as a starting point the last HeightZ. So if I want to set my Absolute height at 75mm I have to do (New.HeightZ - Last.HeightZ)*50steps .... (75-20)*50 ... 2750 steps.
I suppose that the fist thing is to  declare var HeightZ:
Int HeightZ = 0;
Do I have to declare also NewHeightZ and LastHeightZ ?
can somebody help ?
or can somebody point me on a similar sketch ?
thanks

#### PaulS

#1
##### Jan 28, 2013, 12:01 am
Quote
Do I have to declare also NewHeightZ and LastHeightZ ?

Yes, you do. And value them appropriately (and at appropriate times).

Quote
can somebody help ?

You're doing fine, so far.

#### dhenry

#2
##### Jan 28, 2013, 12:02 am
Quote
Stepper Absolute Position ... how to

With a position sensor - encoder or a pot.

#### Grumpy_Mike

#3
##### Jan 28, 2013, 12:03 am
Quote
Int HeightZ = 0;

Are you dealing in units of mm?
That is quite coarse for a miller, your motors are capable of finer control. If you use ints then you have to use some arbitrary units like 1/1000 th of a mm so you can cope with fractional movement.

#### Grumpy_Mike

#4
##### Jan 28, 2013, 12:05 am

Quote
Stepper Absolute Position ... how to

With a position sensor - encoder or a pot.

This might come as a surprise to you dear henry but there is some text in the post as well as the title. That needs to be read before a sensible answer can be given.

#### Silverdog63

#5
##### Jan 28, 2013, 12:18 am
Quote
You're doing fine, so far.

Really ?

Quote
With a position sensor - encoder or a pot.

none of the above,I don't need a feedback, I just need to set the Zero (starting Point) then I just have to know where I am and where I want to go.

yes Mike, you are right I would much better use float (am I right? ) :-)
but at the moment I was just thinking to understand how to manage the code.
thanks

#### nickgammon

#6
##### Jan 28, 2013, 12:27 am
I find it easier to write code than to try to describe it.

Try putting your thoughts into a small sketch. See if it compiles. With luck you can fix any compile errors yourself. If you get that far, test it.

If the results are not what you expect, or if you get stuck on some point, post what you have and describe the problem.
Please post technical questions on the forum, not by personal message. Thanks!

#### Grumpy_Mike

#7
##### Jan 28, 2013, 12:28 am
No using floats brings rounding errors, you need to use int to keep it accurate. Just let the int stand for a small value.

#### Silverdog63

#8
##### Jan 28, 2013, 01:05 am
Humm ... so far ?

Code: [Select]
`int HeightZ = 0;int LastHeightZ = 0;//int NewHeightZ = 0;void ABSheight(){HeightZ = 0;if (HeightZ > 0) HeightZ = LastHeightZ;stepper1.moveTo((LastHeightZ - HeightZ) * 50); // move Height1 mm'slcd.print ("Height mm ");lcd.print (HeightZ);delay (5000);}}`

#### PaulS

#9
##### Jan 28, 2013, 01:44 am
Code: [Select]
`HeightZ = 0;if (HeightZ > 0) HeightZ = LastHeightZ;`
What do you suppose the chances of this statement evaluating to true?

#### Silverdog63

#10
##### Jan 28, 2013, 01:56 am
only at the beginning after the zero is setted

#### PaulS

#11
##### Jan 28, 2013, 01:56 am
Quote
only at the beginning after the zero is setted

You set HeightZ at the start of that function, every time it is called.

#### dhenry

#12
##### Jan 28, 2013, 02:05 am
Quote
I just need to set the Zero (starting Point) then I just have to know where I am and where I want to go.

Without a position sensor, you wouldn't know where you are: all you know is where you have told the stepper to go. Whether the motor is there or not is an unknown, unless you have a position sensor.

#### Silverdog63

#13
##### Jan 28, 2013, 03:38 pm
Paul, there is somthing I don't catch sorry, I have to do a better research on other project to see something similar at work.

Henry, with all respect, but a position sensor is not needed. It's the code that know where the stepper is. With the steppers the position is calculeted by the software, and they work perfecly this way, most of the CNC work like that.

#### Silverdog63

#14
##### Jan 28, 2013, 08:18 pmLast Edit: Jan 28, 2013, 10:28 pm by Silverdog63 Reason: 1
... well I spent some hours trying understand, but I cannot clear my idea on how I can store the previous HeightZ value to LastHeightZ
This code is obviously not working, I miss the way to store my variable HeightZ and convert to LastHeightZ before changing the value.

Code: [Select]
`int HeightZ = 0;int LastHeightZ;int NewHeightZ;void ABSheight(){LastHeightZ = HeightZ;  // previous?? HeightZ variable setted in the menu is stored in LastHeightZNewHeightZ = (HeightZ - LastHeightZ); //should be actual HeightZ difference value with previous in the menustepper1.moveTo(NewHeightZ * 50); // move Height1 mm'slcd.print ("Height mm ");lcd.print (HeightZ);delay (5000);}`

Go Up