Stepper motor motion for pendular motion

Hello all-

I would like to mimic pendular motion with a stepper motor.

I have wired up and am successfully controlling a NEMA17 bipolar stepper, suncor SC42STH38-1304AF
http://www.suncormotor.com/Products/1468325853.html
with this motor shield: http://www.seeedstudio.com/wiki/Motor_Shield_V1.0 using the rudimentary code contained in aforementioned wiki.

 SEEED Studio Stepper Motor Control - one revolution
 
 Adapts the Stepper example for use with the SEEED STUDIO motor shield.
 This program drives a unipolar or bipolar stepper motor
 by using the included Stepper library of the Arduino.
 The motor is attached to the Seeed Studio motor shield and an Arduino.
 
 The digital pins 8,11,12,13 drive the L298N and are used when creating the stepper object
 Digital pins 9 and 10 must be high to enable the chip.
 
 The motor should revolves one revolution in one direction, then
 one revolution in the other direction. 
 
 
 Created 11 Mar. 2007
 Modified 30 Nov. 2009
 by Tom Igoe
 Modified 4 July 2012
 By R. Dumouchelle
 
 */
 
#include <Stepper.h>
 
const int stepsPerRevolution = 200;  // change this to fit the number of steps per revolution
// for your motor
 
// initialize the stepper library on pins 8 through 11:
Stepper myStepper(stepsPerRevolution, 8,11,12,13);           
 
void setup() {
  // set the speed at 60 rpm:
  myStepper.setSpeed(60);
  // initialize the serial port:
  Serial.begin(9600);
  pinMode(9,OUTPUT);
  pinMode(10,OUTPUT);
  digitalWrite(9,HIGH);
  digitalWrite(10,HIGH);
}
 
void loop() {
  // step one revolution  in one direction:
  Serial.println("clockwise");
  myStepper.step(stepsPerRevolution);
  delay(500);
 
//step one revolution in the other direction:
  Serial.println("counterclockwise");
  myStepper.step(-stepsPerRevolution);
  delay(500);
}

I would like to use the stepper motor to control the motion of a swinging pendulum about 4’ (125cm) long with a weight of around 5lb.

I have no problem with the mechanical aspects of the project (I run a fabrication shop).

My current approach is to measure the period and angle of the swing, and then use that as a reference for controlling the motion- the stepper’s motion will mimic the systems natural tendency within some reasonable degree of accuracy.

Here is some code that contains an angular momentum algorithm. Is there a way to use this algorithm or something like it to inform the stepper’s motion?

Pendulum p;
 
void setup() {
  size(640,360);
We make a new Pendulum object with an origin location and arm length.
  p = new Pendulum(new PVector(width/2,10),125);
}
 
void draw() {
  background(255);
  p.go();
}
 
class Pendulum  {
Many, many variables to keep track of the Pendulum’s various properties
  PVector location;    // Location of bob
  PVector origin;      // Location of arm origin
  float r;             // Length of arm
  float angle;         // Pendulum arm angle
  float aVelocity;     // Angle velocity
  float aAcceleration; // Angle acceleration
  float damping;       // Arbitrary damping amount
 
  Pendulum(PVector origin_, float r_) {
    origin = origin_.get();
    location = new PVector();
    r = r_;
    angle = PI/4;
 
    aVelocity = 0.0;
    aAcceleration = 0.0;
An arbitrary damping so that the Pendulum slows over time
    damping = 0.995;
  }
 
  void go() {
    update();
    display();
  }
 
  void update() {
    float gravity = 0.4;
Formula we worked out for angular acceleration
    aAcceleration = (-1 * gravity / r) * sin(angle);
 
Standard angular motion algorithm
    aVelocity += aAcceleration;
    angle += aVelocity;
 
Apply some damping.
    aVelocity *= damping;
  }
 
  void display() {
Where is the bob relative to the origin? Polar to Cartesian coordinates will tell us!
    location.set(r*sin(angle),r*cos(angle),0);
    location.add(origin);
 
    stroke(0);
The arm
    line(origin.x,origin.y,location.x,location.y);
    fill(175);
The bob
    ellipse(location.x,location.y,16,16);
  }
}

So, we need to control the rate of change of velocity with a variable…
something like this?
aAcceleration = (-1 * gravity / r) * sin(angle);
aVelocity += aAcceleration;
angle += aVelocity;

There then need to be two variables associated with velocity: V and △V (acceleration).

als, these:
PVector location; // Location of bob
PVector origin; // Location of arm origin
float r; // Length of arm
float angle; // Pendulum arm angle
float aVelocity; // Angle velocity

thats all I got.
I can’t think in c++ abstractly or succinctly enough to make this work. But its possible and easy, right?
Please give me a pointer,

-R

I can't understand what you want to do. It seems that you have a regular swinging pendulum which will exhibit the normal pendulum motion at a frequency determined by its length.

What is the role of the stepper motor? What is it going to drive?

How many steps will the stepper make for a single sweep of the pendulum?

Is the stepper intended to produce sinusoidal fore and aft movement? If so I suspect you just need an array of values for the intervals between each step. That could be calculated on a spreadhseet and then built into your program.

A pendulums is very predictable. I don't see any need to measure its motion. Mabe you could have a slotted optical switch to detect the period of the pendulum if you want to synchronize the motor with it.

...R Stepper Motor Basics

The math may work, but a stepper motor follows its own laws, affecting the pendulum in a different way. If the motor is strong enough, it can hold or make the pendulum swing at (almost) any speed or frequency, overriding gravity.

What do you want to achieve with the motor?

Hi, Do you want the pendulum to swing at its natural frequency and use some device to keep the pendulum swinging, instead of slowly decaying?

Tom.... :)

@TomGeorge Yes, that is exactly what I want to do.

@Robin2 I thought about that, but I would like a way to adjust the movement with a variable or two rather than have to re populate a spreadsheet and manually enter a bunch of different velocity variables. You are talking about calculating a bunch id different static velocities and intervals to approximate pendular motion, correct? How would you go about generating those values?

What this is, is a kinetic sculpture with pendulum like movement. It is a figure on a swing with rigid 'ropes' attached to an axle which is driven by the stepper. It needs to look like pendular motion, but does not have to be mathematically exact, though that would be a good thing to aim for.

Normally, the way they do that is to have an electromagnet at the bottom of the swing and it adds a small kick of energy to the pendulum as it goes by - just enough to compensate for friction (air and pivot) losses.

revel: @TomGeorge Yes, that is exactly what I want to do.

I confess I thought of what @TomGeorge had in mind and I dismissed it because I could not relate it to the text of your Original Post

A stepper motor seems overkill for something intended to add a little energy to a pendulum to offset friction. Think of the tiny amount of energy used by a clock mechanism.

In any case I see no reason why the stepper (or any other motor) has to mimic the motion of the pendulum. All it needs to do is give it a little push at the correct point in the cycle.

I have seen pendulum systems where the added energy was provided through an electromagnet sited below the centre of the pendulum's arc - no moving parts required (apart from the pendulum).

...R

@gpsmikey that sound like potentially a good solution. I assume the electromagnet would be triggered mechanically by the pendulum itself right before it gets perpendicular to the ground plane, possibly bypassing the need for an arduino? Or would you manually set the interval of the electromagnet with the arduino? The main issue I see with this is that this assembly will be in a window display and it is not a pendulum proper but rather a seated sculpture on a swing. So I see an issue getting the electromagnet close enough to the swing seat (which is where the corresponding ferrous metal would be embedded). Also potentially an issue with hiding the electromagnet underneath the swinging sculpture. Do you know where to source an electromagnet like this? There would be, say, 14" between the electromagnet and the swing seat.

@Robin2 Yes my original post was a tad convoluted. If it is necessary to drive the pendulum with a stepper at the point of pivot

Robin2: All it needs to do is give it a little push at the correct point in the cycle.

how would that look mechanically? a ratcheting drive of some sort?

And, for arguments sake, say I wanted to impart a 'slow motion' pendulum movement. Then it would become necessary for the stepper to drive the whole thing. How would I make that stepper motion in arduino code?

Why not be really cute and make the doll seated on the swing move his legs. Maybe conceal a small servo inside the doll?

Then, just like a real swing, you could change the amplitude of the movement by kicking the legs higher. And get the swing to start and stop.

If I was using a servo stepper to force the swing to move in a pendulum-like way (but not necessarily at its natural frequency) I would (as I said earlier) save the intervals between steps in an array and just iterate through the array to cause the stepper to move according to the "plan".

...R Edit - just realized I typed "servo" when I had intended "stepper". Sorry for any confusion. It is doubly confusing because you could possibly use a servo in the same role - but with different code.

14 inches is a bit much for an electromagnet to work against the swing itself. You could add something in the pivot though to add some energy - it doesn't take much although you are talking a swing with a doll on it and a distance of 4 feet - I think you are going to have a problem with air resistance unless you make the doll heavier (ever tried to swing on a swing that had really long lines? You can't get the height you would expect because of the air resistance at the bottom where you are moving the fastest. That is why many pendulums are disk shaped so that they present the minimum air resistance. You could have a motor in the pivot assembly that you provide current limited power to for a portion of the swing cycle that would add the needed energy.

Robin2: Why not be really cute and make the doll seated on the swing move his legs. Maybe conceal a small servo inside the doll?

Then, just like a real swing, you could change the amplitude of the movement by kicking the legs higher. And get the swing to start and stop.

If I was using a servo to force the swing to move in a pendulum-like way (but not necessarily at its natural frequency) I would (as I said earlier) save the intervals between steps in an array and just iterate through the array to cause the stepper to move according to the "plan".

...R

That would be cool (and cute)!... but its a static, rigid sculpture, so i guess its option 2. Can you point to an example of a servo control using velocity values stored in an array? As for the basic math... so, if I remember correctly the velocity of the 'bob' relative to the ground is equivalent to what it would be dropped from a dead height. the velocity v at the bottom of the swing is v = √2g * L * (1-cos(a)), and, well i should probably be able to figure out the rest, and convert it to rotational speed / position. but, as for getting that translated to stepper movement, I'm coming up short.

I still don't quite understand why it is not possible to use a function within the code to generate velocity values. or is it?

Also, are there stepper motor libraries that would handle the acceleration and deceleration like AccelStepper?

gpsmikey: 14 inches is a bit much for an electromagnet to work against the swing itself. You could add something in the pivot though to add some energy - it doesn't take much although you are talking a swing with a doll on it and a distance of 4 feet - I think you are going to have a problem with air resistance unless you make the doll heavier (ever tried to swing on a swing that had really long lines? You can't get the height you would expect because of the air resistance at the bottom where you are moving the fastest. That is why many pendulums are disk shaped so that they present the minimum air resistance. You could have a motor in the pivot assembly that you provide current limited power to for a portion of the swing cycle that would add the needed energy.

The sculpture weighs about 5 pounds and will certainly provide some air resistance, the swing rods are covered in a faux twine rope which will add to that resistance.

Can you elaborate on what you mean by current limited power? powering the system for a portion of the swing cycle is still a viable path for this project, too. soon i'll have to choose which way to go, always energized or partially.

What I was thinking of was either a motor directly connected or even some sort of electro magnet in the pivot assembly. By current limited power was for example if you take a DC motor that is designed to run on 12v at 5000 rpm and feed it the full 12v with the armature effectively locked, you will burn it out, but if you use a current limiting resistor in series with the motor, then it will produce some energy, but not be working against a full supply voltage with the locked armature. Just one way of approaching the problem.

revel: Can you point to an example of a servo control using velocity values stored in an array?

Sorry. In Reply #8 I typed "servo" when I intended "stepper" (I have now corrected it).

In fact you could probably control a servo in a similar fashion. Look at the servo-sweep example. It uses the same increment for every step, but there is no reason not to use a different increment drawn from an array.

For long-term operation a stepper would probably be more robust than a servo.

What @gspmikey has suggested seems to be another interesting idea. If one of the pivots for the swing was the shaft of a small DC motor then if the motor is energised for a short period during each swing it will add a little energy. All that would require may be a £2 motor (or free from a broken toy) and a transistor between the Arduino and the motor. Adjust the voltage of the motor power supply to get the proper amount of force. A simple adjustable voltage supply can be built cheaply with an LM317 voltage regulator.

You need to keep in mind that steppers move in steps and swings don't.

...R

Thanks guys, thats very interesting. Can you show me a diagram on the transistor between the motor and the arduino? Would the arduino itself drive said motor or would it use a driver?

It sounds like if I were to use a stepper, it would have to be geared down to increase step resolution rather than direct drive.

revel: Thanks guys, thats very interesting. Can you show me a diagram on the transistor between the motor and the arduino? Would the arduino itself drive said motor or would it use a driver?

The transistor would be the "driver". I'm assuming you don't need bi-directional control. Have a look at the connections here

By the way I forgot, when writing Reply #12, that you could use the Arduino's analogWrite() to control the power going to the motor so there would probably be no need to have a variable voltage power source.

...R

You would need a sensor to tell it when it was at the bottom where it could then add some energy to the swing. The advantage to that is if the Arduino were to be triggered at the bottom by an opto pickup and then it added say 1 second of energy to the motor (via the control transistor), it could be self starting. If it was stationary, it would see it was at the bottom and add the burst of energy to start it moving and each time through zero, it would add another burst until it was up to the normal swing - you would have to fiddle (precise engineering term) with the time / energy of the burst you add to get the swing where you wanted it.

The scope of the project has changed, I would like to allow for the possibility of operating the swing with a more slow motion, laconic movement if desired, and to generally be able to adjust the apparent speed, angle, and acceleration of the piece with software. This is as opposed to reinforce the system's natural tendency. I suppose I could help achieve this by extending a counterweight past the fulcrum, but this is not possible because it will be mounted on a ceiling. So I am currently going forward with the stepper motor drive concept, which I plan to gear down 10 or 20, maybe 50:1 to increase step resolution, and / or use micro stepping.

I think I'll just see if I can mimic a realistic, if not somewhat slow motion, swing movement by 'bouncing' the stepper between two fixed points with AccelStepper:

stepper.setMaxSpeed(1500);
stepper.setAcceleration(1000);
stepper.moveTo(1000);

The system will naturally come to rest in the same position when de energized, even so there will be a routine at the beginning that will back up the swing till it trips a limit switch to establish a reference, and another one at the other end of the swing for safety. This will be in a window display for 2 months, so I supppose I need to look into torques and duty cycles as well. Maybe the program will sleep at night or an R Pi can turn off the arduino.

gpsmikey: You would need a sensor to tell it when it was at the bottom where it could then add some energy to the swing. The advantage to that is if the Arduino were to be triggered at the bottom by an opto pickup and then it added say 1 second of energy to the motor (via the control transistor), it could be self starting. If it was stationary, it would see it was at the bottom and add the burst of energy to start it moving and each time through zero, it would add another burst until it was up to the normal swing - you would have to fiddle (precise engineering term) with the time / energy of the burst you add to get the swing where you wanted it.

Its a great idea, and I would still like to try it if this stepper approach fails.

Hi all-

A few more questions about this project:

If I want to use a geared stepper motor such as this one and provided it is under minimal load, properly powered and cooled, what would the duty cycle be, and / or what would be a good way to calculate that?

Would I be better off building a gt2 timing belt / pulley or gear assembly and achieving the reduction that way?

If I go with the 'add energy to the swing' approach with a current limited dc motor and opto sensor: how far away can that sensor be from the swing? keep in mind the swinging motion is to be pretty subtle, maybe a foot from beginning to end, or around 10 degrees of angular position change.

Thank you,

-R

If you are near the original frequency (of 1 second) you only need a fast motor to go from left to right every 1 second. how many steps is not important. same like a real clock it move only a few mills to each side to keep the pendulum going. it will be hard to move a weight of 5 lbs on a long stick. probably one step is enoughn to keep it moving. so put a step every 500millis() then go back again. use the height to get a correct swing time.

for an animation you will need another approach: make a table with times for each step. on the ends ie 20 millis and on the bottom 1 millis() this is depending on the total angle the swing is. and the swingtime. the speed is changing every step.