I'm trying to work with "AccelStepper" library to control 50 uni-polar step-motors using this library.
I'm using Arduino DUE for sufficient computing capability but because of lack of pins I use shift registers (74hc595) to control step motors.
The library itself can work with custom "backward function" and "forward function" so I managed to update an array for state of each coils of each stepper and custom function to change the coil state.
Currently everything works fine, however I should define 2 different "backward" and "forward" function for each stepper (so 100 function definition!!!).
I'm wondering if there is some way to define these function using preprocessor!
My working example is something like this (only for 4 steppers! for more steppers I need more dummy lines!):
Thanks for your reply. I know that I can use arguments for my function, but the accelStepper object only accept function pointers without arguments!
My "sharedForwardAndBackward" function needs to know which motor calls it! So I defined different function for each motor!
AccelStepper is a standard library of Arduino for controlling stepper motors!
I've already put them in array, the problem is the definition of 50 object, not to access them throw array.
If you really need a working code, here you are, but I think it will make other confused!
You're using the AccelStepper's alternate constructor which accepts function pointers as callbacks. Instead try to subclass AccelStepper and define the protected virtual functions. Step0, Step1, ... Step8. This way you can define one class for all your motors, then specify exactly which motor you want to control when you instance your Subclass.
bnetbutter:
You're using the AccelStepper's alternate constructor which accepts function pointers as callbacks. Instead try to subclass AccelStepper and define the protected virtual functions. Step0, Step1, ... Step8. This way you can define one class for all your motors, then specify exactly which motor you want to control when you instance your Subclass.
johnwasser:
Maybe this will help. At least it only requires one line per stepper and produces the expected output.
#include <AccelStepper.h>
// This macro expands into two Lambda expressions, basically functions with no name. #define StepFunctions(n) {sharedForwardAndBackward(n, true);}, {sharedForwardAndBackward(n, false);}
#define NUMBER_OF_STEPPERS 5
// Prototype must be seen before macro is expanded
void sharedForwardAndBackward(uint8_t motorNumber, bool goForward);
}
Thanks a lot. At least it makes code half size!
bnetbutter:
You're using the AccelStepper's alternate constructor which accepts function pointers as callbacks. Instead try to subclass AccelStepper and define the protected virtual functions. Step0, Step1, ... Step8. This way you can define one class for all your motors, then specify exactly which motor you want to control when you instance your Subclass.
It's sounds great, but I've no idea how to do that! I was planned to duplicate main file and edit it directly but I don't know how to subclass "AccelStepper" library!
I think this is how you would subclass the AccelStepper class to create your own setOutputPins() function that updates your 'registerPins' array instead of setting output pins.
#include <AccelStepper.h>
class SRStepper : public AccelStepper
{
public:
SRStepper(byte mn) : AccelStepper(FULL4WIRE)
{
motorNumber = mn;
};
// You might want to override this to implement eg serial output
// bit 0 of the mask corresponds to _pin[0]
// bit 1 of the mask corresponds to _pin[1]
// ....
void setOutputPins(uint8_t mask)
{
const uint8_t numpins = 4;
registersUpdated = true;
for (uint8_t i = 0; i < numpins; i++)
{
registerPins[numpins * motorNumber + i] = !!(mask & (1 << i));
}
}
private:
byte motorNumber;
byte registerPins[200];
boolean registersUpdated;
};
void setup() {}
void loop() {}
alitava:
Thank you Johnwasser, I got your general idea but I didn't understand your code, maybe because I'm not so familiar with process of "subclass"ing!
alitava:
Thank you Johnwasser, I got your general idea but I didn't understand your code, maybe because I'm not so familiar with process of "subclass"ing!
I try to read more about this and implement the code using this manner.
Thank you and thanks to every one else how shared their idea.
class SRStepper : public AccelStepper
This means "the new class SRStepper (Shift Register Stepper) is a kind of 'AccelStepper'". It will do anything an AccelStepper will do but will do some things differently.
AccelStepper has a method declared "virtual void setOutputPins(uint8_t mask);". The 'virtual' keyword says "if a derived class (like SRStepper) defines this function then that one will be called instead of this one.
This means "the new class SRStepper (Shift Register Stepper) is a kind of 'AccelStepper'". It will do anything an AccelStepper will do but will do some things differently.
AccelStepper has a method declared "virtual void setOutputPins(uint8_t mask);". The 'virtual' keyword says "if a derived class (like SRStepper) defines this function then that one will be called instead of this one.
Thanks a lot. I'm currently working on my project using your single line macro. As soon as I complete this project I will try for creating child class. I'm some how familiar with "parent-child" inheritance but I didn't have enough experiment and courage to start it, but I will do soon.