AF_Motor +AccelStepper: How to go backwards?

Adafruit Ethernet, Motor, Proto, Wave, Datalogger, GPS Shields - etc!

Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.
protonumerique
 
Posts: 10
Joined: Wed Dec 05, 2012 3:14 pm

AF_Motor +AccelStepper: How to go backwards?

Post by protonumerique »

Hi,

I'm working with an Arduino Mega 2560, using a AF_MotorShield and a small 28BYJ-48 Stepper.
I'm interested in using AF_Motor + AccelStepper together, and I need to control the motor via Serial. The motor should go forward if the value is positive and backwards if negative. Or that is what I get from the move() function in the AccelStepper lib. Anyhow, the motor moves nicely but always reaches the value going FORWARD. Is that easy to achive given this CODE?:

Code: Select all


#include <AccelStepper.h>
#include <AFMotor.h>

AF_Stepper motor1(64, 2);
float azimuth;
long pos= 0, lastPos = pos;
int REVOLUTION = 2048;///Somehow found out that this is the value that works for this motor
boolean control = true;
String inString = "";

void forwardstep() {  
  motor1.onestep(FORWARD, DOUBLE);
}
void backwardstep() {  
  motor1.onestep(BACKWARD, DOUBLE);
}

AccelStepper stepper(forwardstep, backwardstep);

void setup()
{  
  Serial.begin(9600);           // set up Serial library at 9600 bps
  Serial.println("Serial to Stepper!");
  stepper.setMaxSpeed(320.0);
  stepper.setAcceleration(100.0);
  //stepper.moveTo(pos);
}

void loop()
{  
  if(control){
  stepper.move(mapToRange());
  control = false;
  }

  while (Serial.available() > 0) {
    float inChar = Serial.read();
    //if (isDigit(inChar)) {
    // convert the incoming byte to a char 
    // and add it to the string:
    inString += (char)inChar; 
    //}
    // if you get a newline, print the string,
    // then the string's value:
    if (inChar == '\n') {
      control = true;
      azimuth=inString.toFloat();
      Serial.print("Value:");
      Serial.println(mapToRange());
      Serial.print("String: ");
      Serial.println(inString);
      // clear the string for new input:
      inString = ""; 
    }
  }
  stepper.run();
  lastPos = pos;
}

long mapToRange(){
  
  pos = map(azimuth, 0, 360, 0, REVOLUTION);
  return pos-lastPos;
    
}

I haven't been able to and am getting a bit clueless.The ideal would be to work with absolute positioning (moveTo(pos)), but that also doesn't work. It will keep on moving FORWARD. Any hints?

User avatar
adafruit_support_bill
 
Posts: 88086
Joined: Sat Feb 07, 2009 10:11 am

Re: AF_Motor +AccelStepper: How to go backwards?

Post by adafruit_support_bill »

The motor should go forward if the value is positive and backwards if negative.
The parameter you are passing to Move is modified by MapToRange. Your MapToRange() function always returns a positive value.

protonumerique
 
Posts: 10
Joined: Wed Dec 05, 2012 3:14 pm

Re: AF_Motor +AccelStepper: How to go backwards?

Post by protonumerique »

Hi and Thanks,

actually the map() function returns a positive value but the mapToRange() function returns the substraction of the new position-last Position, . If the new position is less than the last one, it returns a negative value. I've tested this on the serial monitor and I get negative values, but it will still go forward. That's why I'm confused.

Are there any other possible reasons or am I overlooking something?

User avatar
adafruit_support_bill
 
Posts: 88086
Joined: Sat Feb 07, 2009 10:11 am

Re: AF_Motor +AccelStepper: How to go backwards?

Post by adafruit_support_bill »

I will be doing some motor testing this afternoon. I'll see if I can reproduce it here.

User avatar
adafruit_support_bill
 
Posts: 88086
Joined: Sat Feb 07, 2009 10:11 am

Re: AF_Motor +AccelStepper: How to go backwards?

Post by adafruit_support_bill »

The code doesn't compile as-posted. Where are you getting toFloat() from?

But looking closer at the code, the statement " lastPos = pos;" will be executed on every pass of the loop. Although your the call to mapToRange() inside serial.println() will give you the expected number, the call to mapToRange() inside move() will not have the expected result.

Instead of using a global variable like 'lastPos ', you would be better off using the "currentPosition()" function of accelStepper.

protonumerique
 
Posts: 10
Joined: Wed Dec 05, 2012 3:14 pm

Re: AF_Motor +AccelStepper: How to go backwards?

Post by protonumerique »

Thanks for trying it!
The toFloat() function comes from a modified Arduino String class. Sorry about not mentioning it.
http://www.timewasters-place.com/arduin ... and-float/
This works for my needs so I decided to use it, but toInt() will do the same for testing.
Now I did something else following your lead, even forcing it to pass negative values. It will always go FORWARD. I've tested the Motor with just the AF_Motor lib and I know it works properly.

Here's the version:

Code: Select all

#include <AccelStepper.h>
#include <AFMotor.h>

AF_Stepper motor1(64, 2);
float azimuth;
long pos= 0, lastPos = pos;
int REVOLUTION = 2048;
boolean control = true;
String inString = "";
//int elevation;
// you can change these to DOUBLE or INTERLEAVE or MICROSTEP!
void forwardstep() {  
  motor1.onestep(FORWARD, DOUBLE);
}
void backwardstep() {  
  motor1.onestep(BACKWARD, DOUBLE);
}

AccelStepper stepper(forwardstep, backwardstep);

void setup()
{  
  Serial.begin(9600);           // set up Serial library at 9600 bps
  Serial.println("Stepper block!");
  stepper.setMaxSpeed(320.0);
  stepper.setAcceleration(100.0);
  //stepper.moveTo(pos);
}

void loop()
{  
  if(control){
    Serial.print("3.Values:");
      Serial.println(mapToRange());
      Serial.println(pos);
      Serial.println(stepper.currentPosition());
  stepper.move(mapToRange());
  control = false;
  }

  while (Serial.available() > 0) {
    float inChar = Serial.read();
    //if (isDigit(inChar)) {
    // convert the incoming byte to a char 
    // and add it to the string:
    inString += (char)inChar; 
    //}
    // if you get a newline, print the string,
    // then the string's value:
    if (inChar == '\n') {
      control = true;
      azimuth=inString.toInt();
      Serial.print("1.Value:");
      Serial.println(mapToRange());
      Serial.print("2.String: ");
      Serial.println(inString);
      // clear the string for new input:
      inString = ""; 
    }
  }
  stepper.run();

}

long mapToRange(){
  
  pos = map(azimuth, -360, 360, -REVOLUTION, REVOLUTION);
  //return pos-stepper.currentPosition();
    
}


User avatar
adafruit_support_bill
 
Posts: 88086
Joined: Sat Feb 07, 2009 10:11 am

Re: AF_Motor +AccelStepper: How to go backwards?

Post by adafruit_support_bill »

Now I did something else following your lead, even forcing it to pass negative values. It will always go FORWARD.
Have you tried the AccelStepper MultiStepper example? In my tests with the current version of the library it goes both forward and backward.

protonumerique
 
Posts: 10
Joined: Wed Dec 05, 2012 3:14 pm

Re: AF_Motor +AccelStepper: How to go backwards?

Post by protonumerique »

Oh oh...I tried it and it did the same unidirectional movement with my setup. However, I changed things and made it work strangely. Perhaps you have some Idea about that?:

I got another motor of the same kind and plugged it to the M1, M2 ports of the shield. Changed the code to match the motor, and most importantly, changed my power source to send 9v instead of 5v... it works now, but the motor vibrates much more. The motor is a 5v motor, so I don't know if I'm frying it or something, but I read that most motor bizarre stuff is about voltage so I tried it. the direction also changes with 7.5v but the movement's jagged, so 9 is better. The motor will also just move properly if set to DOUBLE steps :-S

Btw. The source is an adapter, plugged to the arduino board and using the power jumper of the shield.

I got the uneasy feeling that my shield must be badly built. Can be that because of some bad soldering some parts get crazy? I realized that my shield never lights its LED even if I plug a constant power source. But basic examples work though...

Any further advice on how to test this or about dealing with the voltage uncertainty would be mostly welcome. The code is working in the end.

User avatar
adafruit_support_bill
 
Posts: 88086
Joined: Sat Feb 07, 2009 10:11 am

Re: AF_Motor +AccelStepper: How to go backwards?

Post by adafruit_support_bill »

If you post photos of the front & back of the board we'll take a look at your assembly. A bad connections can result in a 'missed phase' which will act unpredictably. Higher voltages will give you more torque, and possibly enough inertia to skip over the missing phase.

protonumerique
 
Posts: 10
Joined: Wed Dec 05, 2012 3:14 pm

Re: AF_Motor +AccelStepper: How to go backwards?

Post by protonumerique »

Sure. I think I checked all the positions very well. All you need to see are those 2 major F*** U*s in the soldering on the backpart. I had some strange soldering that never snapped... I could resume assembly after getting a new one, but, it was too late. Servos are working on the side of the biggest burn, but I don't know anymore...

Thanks again
front
front
_MG_8630.JPG (194.69 KiB) Viewed 3475 times
back
back
_MG_8629.JPG (174.72 KiB) Viewed 3475 times

User avatar
adafruit_support_bill
 
Posts: 88086
Joined: Sat Feb 07, 2009 10:11 am

Re: AF_Motor +AccelStepper: How to go backwards?

Post by adafruit_support_bill »

Is it possible to get closer? I can't see much detail in the joints themselves. Some look like they may have insufficient wetting of the pad: http://learn.adafruit.com/adafruit-guid ... n-problems.

I hope you didn't power it up with those bare wires hanging out. That could short a leg on the H-bridge. A partially blown H-bridge would cause irregular stepper operation too.

protonumerique
 
Posts: 10
Joined: Wed Dec 05, 2012 3:14 pm

Re: AF_Motor +AccelStepper: How to go backwards?

Post by protonumerique »

Hi. I wanted to try and repair the thing myself. I'd think it is allright now. My LED finally turns on.

Anyway, the thing is, that now I'm back at the beginning. The motor will always go FORWARD, even with the source set ot 9v. Kind of frustrating, because now I can't reproduce the settings with which things were working. May be it's that phase problem you said. I don't know, I attach a pictures of the update. Perhaps there is something there...
_MG_8650.JPG
_MG_8650.JPG (688.44 KiB) Viewed 3390 times
_MG_8634.JPG
_MG_8634.JPG (195.35 KiB) Viewed 3390 times

User avatar
adafruit_support_bill
 
Posts: 88086
Joined: Sat Feb 07, 2009 10:11 am

Re: AF_Motor +AccelStepper: How to go backwards?

Post by adafruit_support_bill »

Is the forward-only problem on just one motor channel, or on both? If only on one: when you swap the L293D chips, does the problem follow the chip?

protonumerique
 
Posts: 10
Joined: Wed Dec 05, 2012 3:14 pm

Re: AF_Motor +AccelStepper: How to go backwards?

Post by protonumerique »

First of all: you're the fastest, most dedicated of all supports I've ever found on a Forum. Great to work with your stuff.

Second: The problem replicates on both sides. I just checked using the Accel-stepper lib and a LN2003AN to control the same motor. It works perfectly, and that helped confirming that connection of the coils is right. Something on the shield is not doing its job maybe?

User avatar
adafruit_support_bill
 
Posts: 88086
Joined: Sat Feb 07, 2009 10:11 am

Re: AF_Motor +AccelStepper: How to go backwards?

Post by adafruit_support_bill »

It is strange that the problem appears on both sides. If it were on one side only, we could swap the chips and narrow it down to a chip problem or a soldering problem.

Is this the first time you connected anything to the other side?

Locked
Please be positive and constructive with your questions and comments.

Return to “Arduino Shields from Adafruit”