motor does only want to go forward....

735 views
Skip to first unread message

Jan Bos

unread,
Jan 21, 2013, 2:31:52 AM1/21/13
to accels...@googlegroups.com
I am using an Arduino Mega with an AC Servo Drive + AC Servo motor and I am trying to send the positioning command over the serial port to the Mega who sends the pulses to the drive. Drive takes step/direction input but for some reason I can make it go only in one direction. I do not need accel/decel because the drive has that build in. I am able to make the motor do what I need when I use acc/dec in the Arduino code but without it I can get higher speeds. The Mega is only used as slave to drive the motor. and will receive the updated positioning commands from another Mega where the main program runs.
 
Here is what I have:
 

#include <AccelStepper.h>
int positiontogo =0; // set position to go to
String inString = "";    // string to hold input
int targetposition=0; // target position
// Define a stepper and the pins it will use
AccelStepper stepper(1,27,26); // step = 27 direction =26
void setup()

  Serial.begin(57600); // set baud rate
  stepper.setMaxSpeed(8000);
  stepper.setAcceleration(48000);
}
void loop()
{
  // read serial input and convert string into integer for position data
  // Read serial input:
    while (Serial.available() > 0) {
    int inChar = Serial.read();
    if (isDigit(inChar)) {
      // convert the incoming byte to a char
      // and add it to the string:
      inString += (char)inChar;
      //Serial.println(inString);
    }
    // if you get a newline, print the string,
    // then the string's value:
    if (inChar == '\n') {
      Serial.print("Moving to:");
      //Serial.println(inString.toInt());
      //stepper.moveTo(inString.toInt());
      // clear the string for new input:
      targetposition=(inString.toInt());
      Serial.println(targetposition);
     inString = ""; // clears the string
    }
  }
  stepper.moveTo(targetposition);
  stepper.setSpeed(4000);
  stepper.runSpeedToPosition();
}
// end of program
 
 
I can enter for eaxmple 10000 in the serial monitor and it runs nicely to the correct position but when I enter 0 to make it go back it goes the same distance forward again. If I enter 250 only to make it go back to 10000-250 = 9750 it goes another 250 steps forward but not back.
The serial monitor shows the correct position but it only goes in one direction.
 
What am I missing here? Anybody can help me to sort this out?
 
Thanks in advance,
 
JB.

Mike McCauley

unread,
Jan 21, 2013, 2:44:01 AM1/21/13
to accels...@googlegroups.com
Hello,

sounds a bit like the direction pin is not connected properly.
Or maybe step/dir are back to front?

Cheers.
--
Mike McCauley mi...@open.com.au
Open System Consultants Pty. Ltd
9 Bulbul Place Currumbin Waters QLD 4223 Australia http://www.open.com.au
Phone +61 7 5598-7474 Fax +61 7 5598-7070

Radiator: the most portable, flexible and configurable RADIUS server
anywhere. SQL, proxy, DBM, files, LDAP, NIS+, password, NT, Emerald,
Platypus, Freeside, TACACS+, PAM, external, Active Directory, EAP, TLS,
TTLS, PEAP, TNC, WiMAX, RSA, Vasco, Yubikey, MOTP, HOTP, TOTP,
DIAMETER etc. Full source on Unix, Windows, MacOSX, Solaris, VMS, NetWare etc.

Jan Bos

unread,
Jan 21, 2013, 2:53:53 AM1/21/13
to accels...@googlegroups.com
Hello Mike,
 
Thanks for your quick reply but I am positive this is not the case. the code below does work fine but uses acc/dec which I do not need.
 
// servo drive V1.1 with serial position input with acc/decel
#include <AccelStepper.h>

// Define a stepper / servo drive and the pins it will use

AccelStepper stepper(1,27,26); // step = 27 direction =26
int positiontogo =0; // set position to go to
String inString = "";    // string to hold input
void setup()

  Serial.begin(57600); // used to see what is going on
  stepper.setMaxSpeed(1000);
  stepper.setAcceleration(48000);
  stepper.moveTo(positiontogo);
}
void loop()
{

  // Read serial input:
    while (Serial.available() > 0) {
    int inChar = Serial.read();
    if (isDigit(inChar)) {
      // convert the incoming byte to a char
      // and add it to the string:
      inString += (char)inChar;
      //Serial.println(inString);
    }
    // if you get a newline, print the string,
    // then the string's value:
    if (inChar == '\n') {
      Serial.print("Moving to:");
      Serial.println(inString.toInt());

      stepper.moveTo(inString.toInt());
      // clear the string for new input:
     inString = ""; // clears the string
    }
  }
  stepper.run();
  }
// end of program.
 
When I use this code, I can enter the target position in the serial monitor and it runs perfectly to where it needs to be. When I enter 10000 for example it runs to 10000 and when I enter then 0, it runs backwards to 0. Perfectly but since it uses acc/dec I cannot get a very high speeds. In the code below without acc/dec I can get speeds up to 4k without problem but it only goes one way....
 
Any thoughts?
 
Thanks in advance,
 
RR

Mike McCauley

unread,
Jan 21, 2013, 3:01:22 AM1/21/13
to accels...@googlegroups.com
Hello,

looks like you are are always setting the speed to be positive. To make it go
backwards at a fixed speed you will need

stepper.setSpeed(-4000);

Cheers.

Jan Bos

unread,
Jan 21, 2013, 3:34:43 AM1/21/13
to accels...@googlegroups.com
Hello Mike,
 
Aha, that is what I did not know. Makes sense to me. I will give it a shot and let you know what happens. What confuses me is that in the 'proportional sample' the speed does not change from + to -? does the motor follow the potmeter cw and ccw?
 
Is there any reason why in the second piece of code I included with acc/dec there is no need to change the sign of the speed? Is much easier to work with acc/dec since we do not have the change the sign for the speed. I might have to port this on my new Due to see what speeds I can get. The servo is 10.000 steps per revolution and I scaled it down now to 500 but lose some resolution of course.
 
Thanks again,
 
RR
 

On Monday, January 21, 2013 3:31:52 PM UTC+8, Jan Bos wrote:

Mike McCauley

unread,
Jan 21, 2013, 3:44:52 AM1/21/13
to accels...@googlegroups.com
Hi,

When you use accel and decel, the code works out tyhe spped based on the
current position, the desired position and the (absolute) max speed and
acceleration.

If you set the speed yourself (ie for constant speed) all bets are off and you
have to set the speed appropriately yourself.

Cheers.

Jan Bos

unread,
Jan 21, 2013, 3:54:51 AM1/21/13
to accels...@googlegroups.com
Hello Mike,
 
Got it, I will stick with acc/dec and set them to inh in the drive for the motor.
 
One more little thing: is there a trick to send a serial command when the motor reached the target? If I use:  if (stepper.distanceToGo() == 0)  to send out my serial.print("at target position"); it will continue to send this while the motor sits at this position but I only want it once when the motor reaches the target position.
 
Thanks,
 
RR
 

On Monday, January 21, 2013 3:31:52 PM UTC+8, Jan Bos wrote:

Mike McCauley

unread,
Jan 21, 2013, 4:17:34 AM1/21/13
to accels...@googlegroups.com
Hi,


On Monday, January 21, 2013 12:54:51 AM Jan Bos wrote:
> Hello Mike,
>
> Got it, I will stick with acc/dec and set them to inh in the drive for the
> motor.
>
> One more little thing: is there a trick to send a serial command when the
> motor reached the target? If I use: if
> (stepper.*distanceToGo*<http://www.open.com.au/mikem/arduino/AccelStepper/c
> lassAccelStepper.html#a748665c3962e66fbc0e9373eb14c69c1>() == 0) to send
> out my serial.print("at target position"); it will continue to send this
> while the motor sits at this position but I only want it once when the
> motor reaches the target position.

The library wont give you any help with that.

Cheers.
//Serial.println(inString);http://img.photobucket.com/albums/v682/KeithLeiter/100_1740.jpg

Cloe Gibbons

unread,
Jun 7, 2018, 7:38:13 AM6/7/18
to accelstepper

Hi JB,

I am trying to use your code as a starter to control my AC Motor and Driver using an Arduino Uno. At the beginning of your code where you are declaring your motor you have "AccelStepper stepper(1,27,26); // step = 27 direction =26", I was wondering what you have in pin 1? 

Thank you,

Cloe

gregor

unread,
Jun 7, 2018, 7:45:35 AM6/7/18
to accelstepper
Hi,

the '1' is not a pin number, but tells AccelStepper that stepper driver uses a step and enable pin only. see http://www.airspayce.com/mikem/arduino/AccelStepper/classAccelStepper.html#a73bdecf1273d98d8c5fbcb764cabeea5

Cloe Gibbons

unread,
Jun 7, 2018, 10:15:25 AM6/7/18
to accelstepper
Oh of course yes! Thank you for the quick response!

Ralph Hulslander

unread,
Jun 7, 2018, 11:25:38 AM6/7/18
to accels...@googlegroups.com
Cloe let us know how using a AC motor works with stepper code.

I am new to this so am probably missing something.

Ralph

--
You received this message because you are subscribed to the Google Groups "accelstepper" group.
To unsubscribe from this group and stop receiving emails from it, send an email to accelstepper+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Message has been deleted

Cloe Gibbons

unread,
Jun 8, 2018, 7:47:26 AM6/8/18
to accelstepper
Hi Ralph

I could not get this code to work, I think it is a wiring problem rather than a code problem.

I used the following code that I wrote for controlling a small stepper motor.

const int stepPin = 8; 
const int dirPin = 10; 
const int stepPin2 = 9; 
const int dirPin2 = 11; 

void setup() {
  // put your setup code here, to run once:
  pinMode(stepPin,OUTPUT); 
  pinMode(dirPin,OUTPUT);
  pinMode(stepPin2,OUTPUT); 
  pinMode(dirPin2,OUTPUT);
}

void loop() {
  // put your main code here, to run repeatedly:
  digitalWrite(dirPin,HIGH);
  digitalWrite(dirPin2,HIGH);
  for(int x = 0; x < 10000; x++) {
    digitalWrite(stepPin,HIGH);
    digitalWrite(stepPin2,HIGH); 
    //delayMicroseconds(500); 
    digitalWrite(stepPin,LOW);
    digitalWrite(stepPin2,LOW); 
    //delayMicroseconds(500); 
  }
  delay(2000);
}

This code works though I cannot control the speed/rpm of my motor.

Cloe

On Thursday, 7 June 2018 16:25:38 UTC+1, Ralph Hulslander wrote:
Cloe let us know how using a AC motor works with stepper code.

I am new to this so am probably missing something.

Ralph
On Thu, Jun 7, 2018 at 10:15 AM, Cloe Gibbons <cloeg...@gmail.com> wrote:
Oh of course yes! Thank you for the quick response!


On Thursday, 7 June 2018 12:45:35 UTC+1, gregor wrote:
Hi,

the '1' is not a pin number, but tells AccelStepper that stepper driver uses a step and enable pin only. see http://www.airspayce.com/mikem/arduino/AccelStepper/classAccelStepper.html#a73bdecf1273d98d8c5fbcb764cabeea5

--
You received this message because you are subscribed to the Google Groups "accelstepper" group.
To unsubscribe from this group and stop receiving emails from it, send an email to accelstepper...@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages