我有一个代码通过AccelStepper libary运行14个StepperMotors。 但我想要同时运行3个StepperMotors。 如果应该使用4(更多)StepperMotor,那么第4个应该等到其他3个中的一个完成。 意思是:当“空闲插槽”可用时启动StepperMotor。 我怎么能这样做?
这是我的代码:
#include <Max3421e.h>
#include <Usb.h>
#include <AndroidAccessory.h>
#include <AccelStepper.h>
#define VALUE_OFF 0x0
#define VALUE_ON 0x1
#define COMMAND_DRINK 0x2
#define COMMAND_LED 0x3
AndroidAccessory acc("Manufacturer",
"Model",
"Description",
"1.0",
"http://nichtsda.com",
"0000000012345678");
//pin definitions. must be PWM-capable pins!
const int redPin = 2;
const int greenPin = 3;
const int bluePin = 4;
AccelStepper stepper1 (AccelStepper::DRIVER, 23, 12);
AccelStepper stepper2 (AccelStepper::DRIVER, 27, 11);
AccelStepper stepper3 (AccelStepper::DRIVER, 31, 11);
AccelStepper stepper4 (AccelStepper::DRIVER, 35, 11);
AccelStepper stepper5 (AccelStepper::DRIVER, 39, 11);
AccelStepper stepper6 (AccelStepper::DRIVER, 43, 11);
AccelStepper stepper7 (AccelStepper::DRIVER, 47, 11);
AccelStepper stepper8 (AccelStepper::DRIVER, 51, 11);
AccelStepper stepper9 (AccelStepper::DRIVER, 24, 11);
AccelStepper stepper10 (AccelStepper::DRIVER, 28, 11);
AccelStepper stepper11 (AccelStepper::DRIVER, 32, 11);
AccelStepper stepper12 (AccelStepper::DRIVER, 36, 11);
AccelStepper stepper13 (AccelStepper::DRIVER, 40, 11);
AccelStepper stepper14 (AccelStepper::DRIVER, 44, 11);
//int stepPin[16]={23,27,31,35,39,43,47,51,24,28,32,36,40,44,48,52};
int multiplikator = 60000;
int MengeZutat[16];
const int max_red = 255;
const int max_green = 255;
const int max_blue = 200;
byte colors[3] = {0, 0, 0}; //array to store led brightness values
byte rcvmsg[16];
void setup()
{
//set all three of our led pins to output
pinMode(redPin, OUTPUT);
pinMode(greenPin, OUTPUT);
pinMode(bluePin, OUTPUT);
Serial.begin(115200);
acc.powerOn();
stepper1.setMaxSpeed(1800.0);
stepper1.setAcceleration(1000.0);
stepper2.setMaxSpeed(1100.0);
stepper2.setAcceleration(1000.0);
stepper3.setMaxSpeed(1800.0);
stepper3.setAcceleration(1000.0);
stepper4.setMaxSpeed(1800.0);
stepper4.setAcceleration(1000.0);
stepper5.setMaxSpeed(1800.0);
stepper5.setAcceleration(1000.0);
stepper6.setMaxSpeed(1800.0);
stepper6.setAcceleration(1000.0);
stepper7.setMaxSpeed(1800.0);
stepper7.setAcceleration(1000.0);
stepper8.setMaxSpeed(1800.0);
stepper8.setAcceleration(1000.0);
stepper9.setMaxSpeed(1800.0);
stepper9.setAcceleration(1000.0);
stepper10.setMaxSpeed(1800.0);
stepper10.setAcceleration(1000.0);
stepper11.setMaxSpeed(1800.0);
stepper11.setAcceleration(1000.0);
stepper12.setMaxSpeed(1800.0);
stepper12.setAcceleration(1000.0);
stepper13.setMaxSpeed(1800.0);
stepper13.setAcceleration(1000.0);
stepper14.setMaxSpeed(1800.0);
stepper14.setAcceleration(1000.0);
}
void loop()
{
if (acc.isConnected()) {
acc.read(rcvmsg, sizeof(rcvmsg), 1);
if (rcvmsg[14] == COMMAND_LED ) {
byte value = rcvmsg[15];
if (value == VALUE_ON) {
colors[0] = rcvmsg[0] + 128;
colors[1] = rcvmsg[1] + 128;
colors[2] = rcvmsg[2] + 128;
analogWrite(redPin, map(colors[0], 0, 255, 0, max_red));
analogWrite(greenPin, map(colors[1], 0, 255, 0, max_green));
analogWrite(bluePin, map(colors[2], 0, 255, 0, max_blue));
}
else if (value = VALUE_OFF) {
colors[0] = 0;
colors[1] = 0;
colors[2] = 0;
analogWrite(redPin, map(colors[0], 0, 255, 0, max_red));
analogWrite(greenPin, map(colors[1], 0, 255, 0, max_green));
analogWrite(bluePin, map(colors[2], 0, 255, 0, max_blue));
}
}
if (rcvmsg[14] == COMMAND_DRINK ) {
byte value = rcvmsg[15];
if (value == VALUE_ON) {
//int sleepPin[16]={25,29,33,37,41,45,49,53,22,26,30,34,38,42,46,50};
stepper1.move(rcvmsg[0]*multiplikator);
stepper1.setEnablePin(25);
stepper2.move(rcvmsg[1]*multiplikator);
stepper2.setEnablePin(29);
stepper3.move(rcvmsg[2]*multiplikator);
stepper3.setEnablePin(33);
stepper4.move(rcvmsg[3]*multiplikator);
stepper4.setEnablePin(25);
stepper5.move(rcvmsg[4]*multiplikator);
stepper5.setEnablePin(37);
stepper6.move(rcvmsg[5]*multiplikator);
stepper6.setEnablePin(41);
stepper7.move(rcvmsg[6]*multiplikator);
stepper7.setEnablePin(45);
stepper8.move(rcvmsg[7]*multiplikator);
stepper8.setEnablePin(49);
stepper9.move(rcvmsg[8]*multiplikator);
stepper9.setEnablePin(53);
stepper10.move(rcvmsg[9]*multiplikator);
stepper10.setEnablePin(22);
stepper11.move(rcvmsg[10]*multiplikator);
stepper11.setEnablePin(26);
stepper12.move(rcvmsg[11]*multiplikator);
stepper12.setEnablePin(30);
stepper13.move(rcvmsg[12]*multiplikator);
stepper13.setEnablePin(34);
stepper14.move(rcvmsg[13]*multiplikator);
stepper14.setEnablePin(38);
}
else if (value == VALUE_OFF ) {
stepper1.run();
if (stepper1.isRunning() == false) {
stepper1.disableOutputs();
}
stepper2.run();
if (stepper2.isRunning() == false) {
stepper2.disableOutputs();
}
stepper3.run();
if (stepper3.isRunning() == false) {
stepper3.disableOutputs();
}
stepper4.run();
if (stepper4.isRunning() == false) {
stepper4.disableOutputs();
}
stepper5.run();
if (stepper5.isRunning() == false) {
stepper5.disableOutputs();
}
stepper6.run();
if (stepper6.isRunning() == false) {
stepper6.disableOutputs();
}
stepper7.run();
if (stepper7.isRunning() == false) {
stepper7.disableOutputs();
}
stepper8.run();
if (stepper8.isRunning() == false) {
stepper8.disableOutputs();
}
stepper9.run();
if (stepper9.isRunning() == false) {
stepper9.disableOutputs();
}
stepper10.run();
if (stepper10.isRunning() == false) {
stepper10.disableOutputs();
}
stepper11.run();
if (stepper11.isRunning() == false) {
stepper11.disableOutputs();
}
stepper12.run();
if (stepper12.isRunning() == false) {
stepper12.disableOutputs();
}
stepper13.run();
if (stepper13.isRunning() == false) {
stepper13.disableOutputs();
}
stepper14.run();
if (stepper14.isRunning() == false) {
stepper14.disableOutputs();
}
}
}
}
}
答案 0 :(得分:0)
这取决于你的图书馆。
主要有两种方法:使用数组或使用指针。
对于数组,您可以创建一个操作队列并仅执行前三个操作,只要操作完成就将队列向下移动。 但你怎么知道它是完整的,这完全是另一个故事。
使用指针,您可以创建三个用于执行的插槽:
AccelStepper *slot1;
AccelStepper *slot2;
Accelstepper *slot3;
然后使用指向正确电机的指针加载它们。
slot1 = &stepper1;
现在slot1是stepper1,直到你改变它。 你仍然必须知道电机何时完成并更新所有这些,所以你可能最终得到一个队列阵列。
我的建议是采取完全不同的方法: 使用其中一个模拟输入来测量伺服板的平均电流供应,如果高于某个值则延迟执行。 所以你的程序运行并且你有过载保护。
实际上在写完所有这些之后我意识到你可以只有一个计数器,它在电机启动时递增,在结束时递减,避免在一定数量以上启动。 但是这个简单的方法再次要求您知道电机何时完成并且可能导致程序挂断,然后应该通过超时来解决。
希望这一切都很有用。