Управление двумя шаговыми двигателями с помощью управления одним двигателем Библиотека Arduino (Uno)
Я успешно управляю шаговым двигателем, используя следующий бесплатный код:
/*
* Simple demo, should work with any driver board
*
* Connect STEP, DIR as indicated
*
* Copyright (C)2015-2017 Laurentiu Badea
*
* This file may be redistributed under the terms of the MIT license.
* A copy of this license has been included with this distribution in the file LICENSE.
*/
#include <Arduino.h>
#include "BasicStepperDriver.h"
// Motor steps per revolution. Most steppers are 200 steps or 1.8 degrees/step
#define MOTOR_STEPS 200
#define RPM 120
// Since microstepping is set externally, make sure this matches the selected mode
// If it doesn't, the motor will move at a different RPM than chosen
// 1=full step, 2=half step etc.
#define MICROSTEPS 1
// All the wires needed for full functionality
#define DIR 4
#define STEP 3
//Uncomment line to use enable/disable functionality
//#define SLEEP 13
// 2-wire basic config, microstepping is hardwired on the driver
BasicStepperDriver stepper(MOTOR_STEPS, DIR, STEP);
//Uncomment line to use enable/disable functionality
//BasicStepperDriver stepper(MOTOR_STEPS, DIR, STEP, SLEEP);
void setup() {
stepper.begin(RPM, MICROSTEPS);
// if using enable/disable on ENABLE pin (active LOW) instead of SLEEP uncomment next line
// stepper.setEnableActiveState(LOW);
}
void loop() {
// energize coils - the motor will hold position
// stepper.enable();
/*
* Moving motor one full revolution using the degree notation
*/
stepper.rotate(360);
/*
* Moving motor to original position using steps
*/
stepper.move(-MOTOR_STEPS*MICROSTEPS);
// pause and allow the motor to be moved by hand
// stepper.disable();
delay(5000);
}
Тем не менее, я бы хотел использовать два двигателя одновременно, используя один и тот же Arduino-Uno, и немного изменить код. Что я могу сделать? Я супер новичок в кодировании, так что будь проще. Я понимаю, что есть нечто, называемое функцией, и оно получает значения. Однако мне нужно использовать несколько управляющих штырьков: два для каждого двигателя - "шаг" (который каждый раз двигает двигатель на один шаг вперед) и "dir" (для направления вращения каждого двигателя). Я также понимаю, что функция:
BasicStepperDriver stepper(MOTOR_STEPS, DIR, STEP);
не может принимать 4 значения (DIR1, DIR2, STEP1 и STEP2) одновременно. Я хотел бы сделать возможным использование вышеуказанного кода (с небольшими изменениями), чтобы управлять 2 двигателями одновременно, используя 4 разных вывода ввода / вывода (2 для первого привода мотора (мое arduino подключено к драйверу мотора, который У меня есть 2 из них, и я хочу использовать другой для управления другим двигателем) контакты "step" и "dir", и 2 для контактов "step" и "dir" водителя 2-го двигателя).
Thanx
1 ответ
Я сделал следующее, и это сработало!
/*
* Simple demo, should work with any driver board
*
* Connect STEP, DIR as indicated
*
* Copyright (C)2015-2017 Laurentiu Badea
*
* This file may be redistributed under the terms of the MIT license.
* A copy of this license has been included with this distribution in the file LICENSE.
*/
#include <Arduino.h>
#include "BasicStepperDriver.h"
// Motor steps per revolution. Most steppers are 200 steps or 1.8 degrees/step
#define MOTOR_STEPS 200
#define RPM1 60
#define RPM2 60
#define RPM3 60
// Since microstepping is set externally, make sure this matches the selected mode
// If it doesn't, the motor will move at a different RPM than chosen
// 1=full step, 2=half step etc.
#define MICROSTEPS1 4
#define MICROSTEPS2 4
#define MICROSTEPS3 4
// All the wires needed for full functionality
#define DIR1 3
#define STEP1 4
#define DIR2 5
#define STEP2 6
#define DIR3 7
#define STEP3 8
//Uncomment line to use enable/disable functionality
//#define SLEEPx 13
// 2-wire basic config, microstepping is hardwired on the driver
// BasicStepperDriver stepper(MOTOR_STEPS, DIR, STEP);
BasicStepperDriver stepper1(MOTOR_STEPS, DIR1, STEP1); //DIR1 = ,GPIO 3, STEP1 = GPIO 4
BasicStepperDriver stepper2(MOTOR_STEPS, DIR2, STEP2); //DIR2 = ,GPIO 5, STEP2 = GPIO 6
BasicStepperDriver stepper3(MOTOR_STEPS, DIR3, STEP3); //DIR3 = ,GPIO 7, STEP8 = GPIO 8
//Uncomment line to use enable/disable functionality
//BasicStepperDriver stepper(MOTOR_STEPS, DIRx, STEPx, SLEEPx);
void setup() {
stepper1.begin(RPM1, MICROSTEPS1);
stepper2.begin(RPM2, MICROSTEPS2);
stepper3.begin(RPM3, MICROSTEPS3);
// if using enable/disable on ENABLE pin (active LOW) instead of SLEEP uncomment next line
// stepper.setEnableActiveState(LOW);
}
void loop() {
// energize coils - the motor will hold position
// stepper.enable();
/*
* Moving motor one full revolution using the degree notation
*/
stepper1.rotate(360);
stepper2.rotate(360);
stepper3.rotate(360);
/*
* Moving motor to original position using steps
*/
stepper1.move(-MOTOR_STEPS*MICROSTEPS1);
stepper2.move(-MOTOR_STEPS*MICROSTEPS2);
stepper2.move(-MOTOR_STEPS*MICROSTEPS3);
// pause and allow the motor to be moved by hand
// stepper.disable();
delay(1000);
}