Using <stepper.h>

Is it possible to create multiple instances of stepper? I would like to create one-each for X-, Y- and Z-axes.

Geoff

I guess so, why not?

eg

Stepper myStepperX = Stepper(100, 5, 6);

Stepper myStepperY = Stepper(100, 7, 8 );

Stepper myStepperZ = Stepper(100, 9, 10);

T.

You can but these are blocking functions. That is if you tell a motor to turn a specific amount it will not return until the motor has finished. Therefore you can't move more than one motor at a time, which I think is what you want to do.

Hmmm, G-M, that might not be so bad. I seem to remember snippets of code from the old BBC Micro that did interpolation between axes, one-step-at-a-time.

The important thing is that I can create multiple instances. Many thanks, both of you.

Geoff

There are several stepper libraries, some specialized. The standard (included in the Arduino download) stepper works fine but is blocking and only does full steps. You can write your code to do one step at a time to reduce the blocking effect.

I have also used the AccelStepper library. (AccelStepper: AccelStepper library for Arduino) This is written so you can use it nonblocking. You simply call the routine to check if a step needs to made as often as possible, and it will make the step if needed. So you can have more than one stepper motor run at the same time, or anything else you may need to happen (watch switches, blink LEDs...). You have to master the skills to write code that is nonblocking, ie whatever you other code does, it must not use delay() (or Serial-output, it will block when sending more than one character) when you need the motors to turn.

Thanks for that Msquare. What I'm trying to do is an Arduino version of Mach3, massively cut-down and reduced to handling only a few of the many G- and M-codes.

What I have achieved so far is to be able to send a text-file from a Terminal.exe application, line-by-line, and decode it into usable routines, which move my stepper-motor(s).

My first version uses <AccelStepper.h>, which is rather nice - full praise to MikeMc - and I thought it would make sense to compare it with using <stepper.h>. When you get down to it, both of these are effectively 'blocking' for the duration of one-step.

Last night I ran some comparisons: AccelStepper was way out in front on smoothness and step-speed, due mostly to its acceleration feature. My set-up for testing is: Arduino -> L298 -> 200oz-in motor, running off about 12V. NB No micro-stepping, full-steps, 200 steps/rev.

As an interesting aside, I put an ammeter in the 12V line. At idle (no steps, end of routine etc.) it was drawing 1.01A but while running, this fell to about 1/4 A - which is what the motor is rated-for - returning to 1A when all moves were finished. With AccelStepper, I simply added a disableOutputs to cut quiescent current to 60mA or so.

So far, I am inclined to use Mike's routines while developing the rest of my code. I'll gladly share it - just tell me how.

Geoff

At idle (no steps, end of routine etc.) it was drawing 1.01A but while running, this fell to about 1/4 A - which is what the motor is rated-for - returning to 1A when all moves were finished.

No surprise, you are chopping current and the current rise time in an inductor is limiting the total amount of current it will take.

However, if you motor is only rated for 250mA then you are giving it too much current because the maximum rated current of a stepping motor is drawn when it is standing still.

Thanks for that, Mike. I've taken your advice and reduced the supply-voltage.

I have actually (nearly) completed a Sketch, using <stepper.h>, which 'waits' for a command (G1 X42 Y10 etc), decodes it, and runs two motors as appropriate. The commands come from external text files, via Terminal.exe, and using XON, XOFF to control the flow.

If anyone is interested, I will be pleased to share the Sketch. Just tell me how?

And now for two more questions:

A/ How can I create-and-#include an external file?
I've used an array of sin()s and cos()s and would like to make it available to other Sketches - I tried the obvious of just make a file containing float myCos[] = {1,.98, etc....} and called it myCos.h but I can't persuade the main Sketch to compile with this as #include <myCos.h> I hope that this way I don't bloat my sketch with values that are not always needed.

B/ For some CNC routines
I would like to repeat sub-sections, which should be defined within the text file e.g.

M98 P101 L8 (Calls the subroutine O101, eight times)
G0 Z5
......
O101 (subroutine 101)
G01 Z-2.5 F100
....
M99 (Return from subroutine 101)

I wondered about making the subroutine a separate file, and 'calling' it when/if required. Is it possible for Arduino to 'open' a named file from over a Com1-connection?

Thanks again,

Geoff

How can I create-and-#include an external file?

Put it in the same folder as the sketch. Restart the Arduino IDE and it will appear as a tab next to your sketch.

I wondered about making the subroutine a separate file, and 'calling' it when/if required. Is it possible for Arduino to 'open' a named file from over a Com1-connection?

Not quite sure what you mean because the arduino contains compiled code and you want to "open" a file with text. That sort of thing is possible but needs a helper application at the PC end.
One popular way is to store the gcode file on an SD card and have an SD interface that the arduino reads.

Thanks, Mike.
I did a #include <SinAndCos.h> and got complaints from the compiler, despite that the code had been cut-and-pasted from the main file. Much head-scratching later, I happened across an example that did a #include "blahblah.h". Tried that and Presto!

On the other point, I think we are talking at cross-purposes. Some pseudo-code might help:

Somewhere in the CNC-file that is currently running
.....
M98 P101 L8 (Calls the subroutine O101, eight times)
.....
.....
M98 P102 L4 (calls a different subroutine (O102) four times)
.....
.....
M03 (End program, which, BTW, I use to shut-off power to the drive motors.)

(Subroutines are normally defined outside/at the end of the main string of commands
  for the clarity of the main flow and of the subroutines)
O101
"openFile o101.txt"  (which will contain more G-code stuff, as routines that are complete in themselves)
The main Arduino program decodes and actions this code, just as it would the main CNC file, and using XON, XOFF to read a line at a time.
Then returns to where it left-off
M99 (Return from subroutine)
.....
O102
"openFile o102.txt"  (which will contain more G-code stuff, as routines that are complete in themselves)
The main Arduino program decodes and actions this code, just as it would the main CNC file
Then returns to where it left-off
M99 (Return from subroutine)

I'm not sure how storing the O-files on an SD-card would make it any simpler - I would still have to call file.open(.....) (or whatever) and would lose the benefit of being able to just edit-and-save at the PC when refining the G-codes (also known, to me anyway, as getting-rid of my mistakes!) Beside which, I don't have an SD-card reader (and just now, can't afford one.)

I visualise the my M98-handling function would do the file.open() bit as well as an XOFF to the main terminal.
Then the M99 handler would re-enable the terminal with an XON.

Go on, point out how daft my whole idea is - most neighbours already know I'm crackers.

Geoff

"So far, I am inclined to use Mike's routines while developing the rest of my code. I'll gladly share it - just tell me how."

Just below the text window, lower left corner, there's a gadget,
?Additional Options...
Click on that and then you'll presented with the attachment manager, then you can U/L everything as a ZIP or PDE, etc.

Thanks pancake. Have done.

mySerialStepper_h.pde
SinAndCos.h
PistonScallopB.txt, example of mods to a Honda motorbike piston, which often crops-up.

I use Terminal.exe http://sites.google.com/site/terminalbpp/, to which I was directed by another member of this forum.

Enjoy,

Geoff

mySerial_stepper_h.pde (14.1 KB)

SinAndCos.h (1.04 KB)

PistonScallopB.txt (635 Bytes)

When you are "running" a gcode file you are taking text and deciding what routines to call. Now if that text calls for a subroutine you then have to find the text that defines the subroutine. Normally this would be in the computer's memory, but the arduino has so little memory that it can't. Therefore the application on the PC that is feeding the arduino has to find this text and feed it to the arduino.
If you had the whole file on an SD card you could let the arduino do this.

I think what you are asking is can this text file cause another text file ( containing the subroutine )to be opened? There is no way the arduino can do this directly with out the help of another application on the PC.

Mike, you have put it in a nutshell.

OK, I'll use your SD-card route, I just have to order the gadget from flooded Bangkok. It's great living 'in the sticks' away from the flooding but everything has to come from the city anyways ....

Thanks for all your help so far,

Geoff