-
Notifications
You must be signed in to change notification settings - Fork 0
StepperController By Example
In the following example, the motor being controlled by stepper driver with direction pin 4, step pin 3, and sleep pin 5 will rotate for 5.125 revolutions are a speed of 200 RPM. It's important to keep in mind that although the controller behavior is defined by the methods being called in setup(), the same methods can be used in loop() and other functions that have access to the StepperController instance motor0. This allows the controller to change the speed, position, and state of the motor dynamically and in response to things like sensors or data coming from a system bus.
#include <StepperController.h>
#define DIR 4
#define STEP 3
#define SLEEP 5
unsigned long lastTime;
unsigned long dt;
StepperController motor0(STEP, DIR, SLEEP); // instantiate motor0 as a StepperController with the given pins
void setup() {
motor0.setPositionMode(); // set the motor to operate in position mode.
motor0.setSpeed(200); // set the speed of the motor to 200 RPM, sign doesn't matter for position control.
motor0.setPosition( 5.125 ); // set the motor to turn to 5.125 revolutions.
}
void loop() {
lastTime = micros();
motor0.update(dt); // update StepperController instance each loop
// other arduino code goes here
dt = micros() - lastTime;
}The following example will set the speed of the motor to 100 RPM in the positive direction. The motor will keep turning continuously until either the motor or MCU lose power.
#include <StepperController.h>
#define DIR 4
#define STEP 3
#define SLEEP 5
unsigned long lastTime;
unsigned long dt;
StepperController motor0(STEP, DIR, SLEEP); // instantiate motor0 as a StepperController with the given pins
void setup() {
motor0.setSpeedMode(); // set the motor to operate in position mode.
motor0.setSpeed(100); // set the speed of the motor to 100 RPM in the positive direction.
}
void loop() {
lastTime = micros();
motor0.update(dt); // update StepperController instance each loop
// other arduino code goes here
dt = micros() - lastTime;
}This example compares the Jog mode to the Position mode. motor0 is commanded to jog 12 revolutions in the positive direction and then 2 revolutions in the negative direction. This results in a final position of 10 revolutions in the positive direction, whereas motor1 is configured to position mode and is commanded to go to a position of 12 revolutions in the positive direction and then commanded to go to a position of 2 revolutions in the negative direction. This results in the final position of motor1 to be 2 revolutions in the negative direction.
#include <StepperController.h>
unsigned long lastTime;
unsigned long dt;
StepperController motor0(2, 3, 4);
StepperController motor1(5, 6, 7);
void setup() {
// motor0 configured to demonstrate jogging mode
motor0.setJogMode(); // set the motor to operate in jog mode.
motor0.setSpeed(100); // set the speed of the motor to 100 RPM.
// the following code will result in the motor0's position setpoint to be set to 10 revolutions in the positive direction.
motor0.setJog(12); // set the motor to jog 12 revolutions in the positive direction.
motor0.setJog(-2); // set the motor to jog 2 revolutions in the negative direction.
// motor1 configured to demonstrate position mode compared to jogging mode.
motor1.setPositionMode(); // set the motor to operate in position mode.
motor1.setSpeed(100); // set the speed of the motor to 100 RPM.
// the following will result in motor1's position setpoint to be -2 revolutions.
motor1.setPosition(12);
motor1.setPosition(-2);
}
void loop() {
lastTime = micros();
motor0.update(dt);
motor1.update(dt);
// other arduino code goes here
dt = micros() - lastTime;
}This example illustrates how to use the common Wire library to use bytes read from the bus to set the motor speed. This would allow a master on the bus to write bytes to the MCU's address in order to adjust the speed of the motor. It can be easily imagined that more complex packet structures could control the other aspects of the motor state over the bus.
#include <Wire.h>
#include <StepperController.h>
#define DIR 4
#define STEP 3
#define SLEEP 5
unsigned long lastTime;
unsigned long dt;
StepperController motor0(STEP, DIR, SLEEP); // instantiate motor0 as a StepperController with the given pins
void setup() {
Wire.begin(0x8); // connect to i2c bus with address 0x8.
Wire.onReceive(receiveEvent); // register receive event function
motor0.setSpeedMode(); // set the motor to operate in position mode.
motor0.setSpeed(0); // set the speed of the motor to zero so the rotor is stationary.
}
void loop() {
lastTime = micros();
motor0.update(dt); // update StepperController instance each loop
// other arduino code goes here
dt = micros() - lastTime;
}
void receiveEvent(int howMany) {
while (Wire.available()) { // loop through all but the last
char c = Wire.read(); // receive byte as a character
motor0.setSpeed((int)c); // set the motor speed to char cast as int
}
}In this example, motor0 is set to be a slave of motor1. This means that motor0 gets disabled and all of the configuration of motor0 in setup() will not have any effect on motor0 unless the master (motor1) gets it's slave cleared. This means that the final behavior of both of the motors is that they will follow the exact same trajectory which is defined in setup() as motor1's configuration. Both motors will turn 18.33 revolutions traveling at 200 RPM.
#include <StepperController.h>
unsigned long lastTime;
unsigned long dt;
StepperController motor0(2, 3, 4);
StepperController motor1(5, 6, 7);
void setup() {
motor0.setPositionMode();
motor0.setSpeed(200);
motor0.setPosition( 5.125 );
motor1.setPositionMode();
motor1.setSpeed(200);
motor1.setPosition(18.33);
motor1.setSlave(motor0);
}
void loop() {
lastTime = micros();
motor0.update(dt);
motor1.update(dt);
// other arduino code goes here
dt = micros() - lastTime;
}This example will demonstrates a sketch that will servo a stepper motor between two positions back and fourth while adjusting speed between setpoints.
#include <StepperController.h>
unsigned long lastTime;
unsigned long dt;
StepperController motor0(2, 3, 4);
// define positions for motor to rock between.
float positionOne = 8.f;
float positionTwo = -2.f;
// define travel speeds between positions.
float speedOne = 200;
float speedTwo = 50;
void setup() {
motor0.setPositionMode();
motor0.setPosition(positionOne);
motor0.setSpeed(speedOne);
}
void loop() {
lastTime = micros();
motor0.update(dt);
// Rocker Logic
if(motor0.isInPosition()) {
if (motor0.getPositionSetpoint() == positionOne){
motor0.setPosition(positionTwo);
motor0.setSpeed(speedTwo);
} else if (motor0.getPositionSetpoint() == positionTwo){
motor0.setPosition(positionOne);
motor0.setSpeed(speedOne);
}
}
dt = micros() - lastTime;
}In this example, the motor is given a positional operation range withing [-1, 1] revolutions. This means that even though the motor position is set to 99 revolutions, it will halt after completing 1 revolution.
#include <StepperController.h>
unsigned long lastTime;
unsigned long dt;
StepperController motor0(2, 3, 4);
void setup() {
motor0.setPositionMode();
motor0.setPosition(99); // set motor position to 99 revolutions
motor0.setSpeed(10);
motor0.setRange(-1, 1); // the motor may not rotate to a position less than -1 revolution or a position greater than 1 revolution
}
void loop() {
lastTime = micros();
motor0.update(dt);
// other arduino code goes here
dt = micros() - lastTime;
}This example demonstrates micro-stepping functionality. Assuming the micro-step pins on the stepper driver are correct, this will pull the pins according to a standard A4988 truth table for configuring micro-stepping.
#include <StepperController.h>
unsigned long lastTime;
unsigned long dt;
StepperController motor0(2, 3, 4);
void setup() {
motor0.setPositionMode();
motor0.setPosition(1.0625);
motor0.setSpeed(10);
// Configure microstepping with pins:
// MS1 = 5
// MS2 = 6
// MS3 = 7
motor0.configureMicroSteppingPins(5, 6, 7);
motor0.setStepControlMode(4); // configure motor to quarter step mode, 4 microsteps per step.
}
void loop() {
lastTime = micros();
motor0.update(dt);
// other arduino code goes here
dt = micros() - lastTime;
}