Drift issue

Hello -

I am using a stepmotor driver to drive a motor to move a plate in a clockwise and counterclockwise direction.

http://www.anaheimautomation.com/products/stepper/stepper-driver-item.php?sID=57&serID=12&pt=i&tID=87&cID=20

http://www.anaheimautomation.com/products/stepper/stepper-motor-item.php?sID=15&pt=i&tID=75&cID=19

I was able to run this properly using the code below but have been experiencing a drift - that is, the plate doesn't end up at the correct position every time (except for the first few trials). The amount of drift is variable and most frequently in the same direction. It seems that it doesn't stop when it's supposed to, thus the larger displacements than coded.

Is there a problem with the timer function? How can I correct the drift?

Thank you for your help!
Maria

void setup(void)
{

Timer1.initialize(100);
Timer1.pwm(9, 512); // 9 bit setup to get the clock speed to be approximately 10 kHz
// - Yellow wire

pinMode(8, OUTPUT); // Direction output - Green wire
pinMode(11, OUTPUT); // Control Output - Brown wire

pinMode(2, INPUT_PULLUP); // Direction of rotation - Blue wire
pinMode(3, INPUT_PULLUP); // Motor on/off - Orange wire
pinMode(4, INPUT_PULLUP); // open 0% gape (gape1) - Yellow wire
pinMode(5, INPUT_PULLUP); // open 50% gape (gape2) - Green wire
pinMode(6, INPUT_PULLUP); // open 100% gape (gape3) - Red wire
}

void loop() {
int rotation_direction = digitalRead(2); // Read motor rotational direction from pin 2
// P2.1 HIGH = CW, LOW = CCW
int onoff = digitalRead(3); // Read motor on/off signals from pin 3
// P2.2 HIGH = NonDriving, LOW = DRIVING
int gape1 = digitalRead(4); // Read gape size from pin 4
int gape2 = digitalRead(5); // Read gape size from pin 5
int gape3 = digitalRead(6); // Read gape size from pin 6

digitalWrite(8,rotation_direction); // Write motor rotation direction to pin 8

if (onoff == LOW)
{
if (gape2==HIGH)
{digitalWrite(11, LOW);

delay(400);
digitalWrite(11, HIGH);

}
else if (gape3==HIGH)
{
digitalWrite(11, LOW);

delay(4800);
digitalWrite(11, HIGH);

}
}

}

  1. Use [ code ] tags. Also put [ url ] tags around your datasheet links.

  2. Name your pins. It is difficult to see what digitalWrite(11,...) does when you don't use a name for that pin.

  3. That does not look like a stepper motor. You just send one pin high for 400 or 4800ms, which looks like a DC motor control scheme. That will never be accurate, as you have found.

Thank you!

Pin 11 turns the motor on at LOW and turns it off at HIGH.

The delay(ms) moves the plate for that amount of time and is stopped by turning the motor off.

This Simple Stepper Code should work with your stepper driver and should allow you to explore whether the motor is missing steps. Experiment with driving the motor at different speeds.

You have not told us what motor power supply you are using (volts and amps) - maybe it is not sufficient?

You have not put your code in code tags as requested in Reply #1.

Code should look like this

Use the code button </>

…R
Stepper Motor Basics

Is there a problem with the timer function? How can I correct the drift?

I’m not entirely certain what you are doing.

Timer1.initialize(100);
  Timer1.pwm(9, 512);

It looks like you have set up the Timer1 library(although there is no #include <TimerOne.h>in the posted clode) to generate a 50% duty cycle at 10Khz on Pin 9 which I assume you are using as input for the steps.

It looks like you are using a delay period to gate the steps into the controller. That is, if pin 11 enables the controller to respond to the pulses on pin 9 then the motor should move.

You are handling the entire system incorrectly, and for position control, you should be counting steps, rather than running the motor for a fixed period of time.

Take a look at Robin2’s tutorial on stepper motor basics and Simple Stepper Program.

Edit: I see that Robin2 has just posted the links to his example and tutorial

Robin2:
This Simple Stepper Code should work with your stepper driver and should allow you to explore whether the motor is missing steps. Experiment with driving the motor at different speeds.

You have not told us what motor power supply you are using (volts and amps) - maybe it is not sufficient?

You have not put your code in code tags as requested in Reply #1.

Code should look like this

Use the code button </>

…R
Stepper Motor Basics

He clearly does not have a stepper motor…

Regards,
Ray L.

He clearly does not have a stepper motor....

MBC05641 - 2.6-7.0A Current Range - Stepper Drivers with DC Input

http://www.anaheimautomation.com/products/stepper/stepper-motor-item.php?sID=15&pt=i&tID=75&cID=19

The posted links are clearly to a stepper motor and driver.

cattledog:
The posted links are clearly to a stepper motor and driver.

Hmmmm.... Then his current software is completely mis-guided and will never work reliably..... He must be generating continuous step pulses, and moving by turning on Enable for a period of time. In other words, not even close to a reasonable way to use a stepper motor.

Regards,
Ray L.

Thank you all!

I did have #include <TimerOne.h> on the first line of my code but missed copying it.

With my current code, I am able to move the plate clockwise and counterclockwise at the specified delay, which corresponds to a particular angle based on how long it takes for the gear to complete a full revolution (the frequency of the step motor driver is set to 6400 steps/revolution and it takes 4.8 s to complete a revolution). I am not sure how I should do this with steps even after reading Simple Stepper program of Robin2.

Can you please show me how to approach this correctly so I can have reliable displacements?

How does counting steps solve the problem of drift?

hpfsfia2013:
Thank you all!

I did have #include <TimerOne.h> on the first line of my code but missed copying it.

With my current code, I am able to move the plate clockwise and counterclockwise at the specified delay, which corresponds to a particular angle based on how long it takes for the gear to complete a full revolution (the frequency of the step motor driver is set to 6400 steps/revolution and it takes 4.8 s to complete a revolution). I am not sure how I should do this with steps even after reading Simple Stepper program of Robin2.

Can you please show me how to approach this correctly so I can have reliable displacements?

How does counting steps solve the problem of drift?

Your approach will NEVER be accurate, for several reasons. First, with an (apparently) 10kHz step rate, your timing would have to be very precise to get it to stop at an exact position. Second, stepper motors do NOT like suddenly being enabled with a high step rate - they REQUIRE a smooth acceleration, and deceleration, or you will “lose steps”, which means you issue step pulses that the motor does not react properly to. Proper stepper driver will move an exact number of steps, taking it to an exact position, and it will gradually increase speed from a stop, reach a specified maximum speed for long distances, then gradually decelerate back to a stop at the exact position specified. Without smooth acceleration and decleration ramps, you will be limited to only very slow speed moves.

Find a good stepper driver library, and use it. The one I’m familiar with is called “AccelStepper”, but I’m sure there are others.

Regards,
Ray L.

You need to look at the AccelStepper library and use that.

hpfsfia2013:
I am not sure how I should do this with steps even after reading Simple Stepper program of Robin2.

Have you tried my code?

That would be a good starting point. It will get us all on the same page.

...R

What is the travel distance from A to B exactly?
Does A travelling to B change?
Does A and B travel change?
Can A(x) and B(x) be calculated?
Does A and B have any other axis C,D,W,Z included?