Motorshield V2 controlling stepper

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

Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.
Locked
kinetics
 
Posts: 6
Joined: Mon Aug 19, 2013 4:49 pm

Motorshield V2 controlling stepper

Post by kinetics »

Caution: rank newbie here. I do understand basic electronics, but have very limited, and no recent, experience in physical computing.
I have the Adafruit motor shield V2. I have an art object I need to rotate using a stepper motor (Adafruit #324). I have run the stepper using the Uno with the Adafruit shield with the StepperTest sketch from the Adafruit_Motorshield library. Works fine.
However, I don’t think I can get the kind of control of the stepper that I want using the Adafruit Motorshield library sketches as a starting point. I have in mind writing a program that will put the art thingy thru a demo of different kinds of motion – rotate this way, that way, accelerate, decelerate, long time, short time, wait a while, etc. – to show off what it can do, and also allow a user to take over from the program and play with it by controlling direction, speed, duration. RPMs in the range of 20 to 60. I think stepper motors are what I want, what with all the start/stop/reversing, low RPM, need to control revolutions to fractions of a revolution, etc.
1) Is the V2 motor shield a reasonable choice?
2) Any suggestions for libraries that would be handy?
3) Do you think the Uno is up to the task? (I am thinking of how to allow a user to take over from the demo program and control the art thing.)

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

Re: Motorshield V2 controlling stepper

Post by adafruit_support_bill »

Assuming that the #324 motor is capable of moving your piece, an Uno & V2 shield should be up to the task. What you need to do is define how the user will interact with it. Once you have a clear definition of what kind of user input/control devices you need and how they will control the motors, you can start designing the software.

kinetics
 
Posts: 6
Joined: Mon Aug 19, 2013 4:49 pm

Re: Motorshield V2 controlling stepper

Post by kinetics »

The #324 motor has plenty of torque for the job.
1) The Arduino site has a stepper library (http://arduino.cc/en/Reference/Stepper). Am I right in thinking that I can’t use that code with the V2 motor shield I have since I2C is used in the V2?
2) Will the Adafruit AccelStepper library work with the V2 shield? I tried but was not successful.
3) How can I get non-blocking control of a stepper motor (direction, speed, acceleration?)
4) A user will need to be able to take over control at any point in the demo program, and then let the demo continue: how would that be arranged?
5) I have in mind a progression like this:
a) Write code to get the art thingy to show off as indicated in my first post, while ensuring that a user will be able to interrupt the program to play with the thingy.
b) Write sketch to allow user input via switches and potentiometers, e.g.
c) Try some more fanciful input methods for user control, such as sensing hand position to control speed/direction…

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

Re: Motorshield V2 controlling stepper

Post by adafruit_support_bill »

1 - You need to use the Adafruit motor library: https://github.com/ladyada/Adafruit_Mot ... master.zip
The library reference is here:http://learn.adafruit.com/adafruit-moto ... -reference

2. There are AccelStepper example sketches with the Adafruit library

3. Either via AccelStepper or managing your own timing and calling "oneStep()"

4. There are many ways to approach that problem depending on the control interface. For example,
  • If you have a joystick that always returns to neutral, you can let any 'off neutral' input take precedence.
    For a pots, you could monitor for changes and revert to demo-mode if it remains idle for long.
    You could have a capacitive touch sensor on the input device to detect that a user was touching it.
5. For an application of this type, a 'state machine' architecture is a good model to work with.
On every pass through the loop, you:
- check the inputs
- adjust the state (demo mode, or various 'reactive' states based in user input)
- execute the code associated with the current state

kinetics
 
Posts: 6
Joined: Mon Aug 19, 2013 4:49 pm

Re: Motorshield V2 controlling stepper

Post by kinetics »

Still not able to move the stepper using AccelStepper. I can upload the Adafruit_Motorshield StepperTest sketch to the Uno and my stepper motor runs fine. I then upload the AccelStepper ConstantSpeed sketch and: No motion.
1) I see in the sketch this line:
AccelStepper stepper; // Defaults to 4 pins on 2, 3, 4, 5
That leads me to think that AccelStepper is not using I2C communication to the shield, but instead is using Uno pins 2-5. (Sure enough, I see a 5V, 86 msec period, 50% duty cycle square wave on those pins.)
2) I also see:
// Copyright (C) 2009 Mike McCauley
So this is obviously not the correct code for a shield that just came out (the V2 motor shield I am using)
3) Where can I go to get the correct library for the V2 shield? I followed the links on the Adafruit website to get the version I have.
a) http://www.adafruit.com/products/1438 links to

b) http://learn.adafruit.com/adafruit-moto ... per-motors has a link to

c) https://github.com/adafruit/AccelStepper



And thanks for your help…

adafruit
 
Posts: 12151
Joined: Thu Apr 06, 2006 4:21 pm

Re: Motorshield V2 controlling stepper

Post by adafruit »

yes there is no change to accelstepper as long as you install our modified version from github. you just run the Accel_ConstantSpeed in OUR library to use it....and its tested so it could be something with the wiring :)

kinetics
 
Posts: 6
Joined: Mon Aug 19, 2013 4:49 pm

Re: Motorshield V2 controlling stepper

Post by kinetics »

1) Sure enough, if I run Accel_ConstantSpeed the motor turns. Newbie that I am, I tried to use AccelStepper examples. With the V2 motorshield, I can’t simply open an AccelStepper example and have it work, correct?
2) Looking at the Accel_ConstantSpeed example, I have so many questions I hardly know where to begin: can you point me to a document that would give something like a newbie-level explanation of the action?
3) A few of those questions:
a) Where can I find info about forwardstep1 and backwardstep1?
b) What would be some good reasons to use other than the default frequency of 1.6 kHz in AFMS.begin() ?
c) The serial monitor shows:
Stepper test!
Estimated pre-scale: 2.81
Final pre-scale: 3

Where did the last two lines come from?

Here is the Accel_ConstantSpeed code:

Code: Select all

// ConstantSpeed.pde
// -*- mode: C++ -*-
//
// Shows how to run AccelStepper in the simplest,
// fixed speed mode with no accelerations
// Requires the Adafruit_Motorshield v2 library 
//   https://github.com/adafruit/Adafruit_Motor_Shield_V2_Library
// And AccelStepper with AFMotor support 
//   https://github.com/adafruit/AccelStepper

// This tutorial is for Adafruit Motorshield v2 only!
// Will not work with v1 shields

#include <AccelStepper.h>
#include <Wire.h>
#include <Adafruit_MotorShield.h>
#include "utility/Adafruit_PWMServoDriver.h"

// Create the motor shield object with the default I2C address
Adafruit_MotorShield AFMS = Adafruit_MotorShield(); 
// Or, create it with a different I2C address (say for stacking)
// Adafruit_MotorShield AFMS = Adafruit_MotorShield(0x61); 

// Connect a stepper motor with 200 steps per revolution (1.8 degree)
// to motor port #2 (M3 and M4)
Adafruit_StepperMotor *myStepper1 = AFMS.getStepper(200, 2);

// you can change these to DOUBLE or INTERLEAVE or MICROSTEP!
void forwardstep1() {  
  myStepper1->onestep(FORWARD, SINGLE);
}
void backwardstep1() {  
  myStepper1->onestep(BACKWARD, SINGLE);
}

AccelStepper Astepper1(forwardstep1, backwardstep1); // use functions to step

void setup()
{  
   Serial.begin(9600);           // set up Serial library at 9600 bps
   Serial.println("Stepper test!");
  
  AFMS.begin();  // create with the default frequency 1.6KHz
  //AFMS.begin(1000);  // OR with a different frequency, say 1KHz

  Astepper1.setSpeed(250);	
}

void loop()
{  
   Astepper1.runSpeed();
}
Last edited by adafruit_support_bill on Wed Sep 04, 2013 12:23 pm, edited 1 time in total.
Reason: Please use the 'code' button when submitting code

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

Re: Motorshield V2 controlling stepper

Post by adafruit_support_bill »

I can’t simply open an AccelStepper example and have it work, correct?
...
Where can I find info about forwardstep1 and backwardstep1?
The functions: forwardstep1 and backwardstep1 are 'callback' functions that let AccelStepper interface to the Adafruit_Motorshield library. You can't run the standard AccelStepper examples because they do not have these functions defined.
What would be some good reasons to use other than the default frequency of 1.6 kHz in AFMS.begin() ?
You can use this to fine-tune the frequency to match the characteristics of your motors - although the benefits of that at this level are fairly small.
Where did the last two lines come from?
These are from the PWMServoDriver library that is used to control the PWM chip on the shield. The first number is the calculated pre-scale value for the PWM frequency timer. The second is that number rounded to the nearest integer value.

kinetics
 
Posts: 6
Joined: Mon Aug 19, 2013 4:49 pm

Re: Motorshield V2 controlling stepper

Post by kinetics »

Hmm. Where to go from here?
1) Good to know about those ‘callback’ functions, but how can I learn about all the capabilities of the Adafruit_Motorshield library? It seems the examples show the proverbial tip of the iceberg, and that there is much to know beneath the surface! I (and all of you, no doubt) would like to avoid a very lengthy back and forth here as I struggle to figure out what questions to ask first, and then to understand the answers…
2) I have a couple of books that focus on Arduino projects/examples. I’m guessing I should lay hands on books about C and/or C++. What do you recommend?
3) Is there a way I could connect with someone who could serve as a mentor?
4) I see you fixed my inept pasting of code. I tried, honest! Who’d a thunk that the ‘copy for forum’ in the Arduino IDE was for that other forum? And that one could not search for “code button” in the Adafruit forum to find out where it was/how to use it, because ‘code’ is too common! I can laugh about it now, but…Next time I’ll get it right.
5) Thanks for your patience as I get up to speed!

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

Re: Motorshield V2 controlling stepper

Post by adafruit_support_bill »

1) The library itself is relatively simple. As with most physical computing, the complexity of application arises in the interface to the physical world. There are hundreds of thousands of different motors to choose from and countless ways to apply them. The more you can tell us about what you are building, the better we can direct you toward the appropriate solutions.

2) The Arduino environment supports a subset of C++. (Some parts of C and C++ just don't fly on an embedded microprocessor with limited memory). This is a good book for programming on the Arduino platform. http://www.adafruit.com/products/1019

For general C++ programming, I don't have any real favorites. The books by Stroustroup (the creator of C++) are better than most.

3, 4, 5) We are always here on-line and happy to help!

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

Return to “Arduino Shields from Adafruit”