Stepper Absolute Position ... how to

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

Do I have to declare also NewHeightZ and LastHeightZ ?

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

can somebody help ?

You're doing fine, so far.

Stepper Absolute Position ... how to

With a position sensor - encoder or a pot.

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.

dhenry:

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.

You're doing fine, so far.

Really ?
Thanks Paul, I'll see what I can do with your advice.

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? ) :slight_smile:
but at the moment I was just thinking to understand how to manage the code.
thanks

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.

No using floats brings rounding errors, you need to use int to keep it accurate. Just let the int stand for a small value.

Humm ... so far ?

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's
lcd.print ("Height mm ");
lcd.print (HeightZ);
delay (5000);
}
}
HeightZ = 0;
if (HeightZ > 0) HeightZ = LastHeightZ;

What do you suppose the chances of this statement evaluating to true?

only at the beginning after the zero is setted

only at the beginning after the zero is setted

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

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.

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.

... 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.

int HeightZ = 0;
int LastHeightZ;
int NewHeightZ;


void ABSheight(){
LastHeightZ = HeightZ;  // previous?? HeightZ variable setted in the menu is stored in LastHeightZ

NewHeightZ = (HeightZ - LastHeightZ); //should be actual HeightZ difference value with previous in the menu
stepper1.moveTo(NewHeightZ * 50); // move Height1 mm's
lcd.print ("Height mm ");
lcd.print (HeightZ);
delay (5000);
}
LastHeightZ == HeightZ;  // previous?? HeightZ variable setted in the menu is stored in LastHeightZ

This is a not an assignment. It is a comparison and does nothing useful.

Presumably the Arduino is going to get powered off or reset at some point, and when it restarts it will have no way to know what the ramp position is.

The most general way (IMO) to deal with that is to move the stepper far enough that you can be certain the hardware assembly has gone as far as it can, and then base all your movements from that position. Assuming that the user doesn't usually want to use the ramp in that position, you could then move it either to a standard initial position, or to the position previously saved in EEPROM as the preferred starting position. For example, you might update that each time the user makes any adjustment, so that the angle always starts off where it was left.

Slightly less general, but also less intrusive to the user, would be to save the current position in EEPROM whenever it was changed, and assume that at startup the ramp would already be in that position. Mostly that would be a valid assumption, but there will be some corner cases where it is not.

Peter,
I know there is a way to do what I want .. the problem is that my programming knowledge is zero, so I try to see what others did and sometimes, like this case, I try to find an hint ...
My idea is to zero the motor at the start and then change the height in two manners: absolute (the case where I need help) and relative (I just tell the motor to move X mm).
When I work with wood sometimes Absolute is the easiest way, somtimes relative is.
But I can solve every problem using just one of the method.
Save to the eeprom is not the solution because when a stepper is turned off you have no guaratee that it will hold the position. A stepper when turned on is blocked to that position unless you tell him to move.

Have you had a look into the AccelStepper library? If I remember correctly they support absolute movement for stepper motors.

http://www.open.com.au/mikem/arduino/AccelStepper/index.html

Thanks aufruf,
I will give an accurate look !

"void AccelStepper::moveTo ( long absolute ) "