From d98f46ce647846b0aa30b2e16a30fd4e152a1bf5 Mon Sep 17 00:00:00 2001 From: Carlos Maiolino Date: Thu, 10 Jul 2025 22:55:07 +0200 Subject: Add new code Signed-off-by: Carlos Maiolino --- .../AFMotor_ConstantSpeed.pde | 40 ++++++++ .../AFMotor_MultiStepper/AFMotor_MultiStepper.pde | 57 ++++++++++++ .../AccelStepper/examples/Blocking/Blocking.pde | 28 ++++++ .../AccelStepper/examples/Bounce/Bounce.pde | 29 ++++++ .../examples/ConstantSpeed/ConstantSpeed.pde | 23 +++++ .../examples/DualMotorShield/DualMotorShield.pde | 49 ++++++++++ .../examples/MotorShield/MotorShield.pde | 103 +++++++++++++++++++++ .../examples/MultiStepper/MultiStepper.pde | 44 +++++++++ .../examples/MultipleSteppers/MultipleSteppers.pde | 41 ++++++++ .../AccelStepper/examples/Overshoot/Overshoot.pde | 28 ++++++ .../ProportionalControl/ProportionalControl.pde | 32 +++++++ .../AccelStepper/examples/Quickstop/Quickstop.pde | 40 ++++++++ .../AccelStepper/examples/Random/Random.pde | 30 ++++++ 13 files changed, 544 insertions(+) create mode 100644 Arduino/libraries/AccelStepper/examples/AFMotor_ConstantSpeed/AFMotor_ConstantSpeed.pde create mode 100644 Arduino/libraries/AccelStepper/examples/AFMotor_MultiStepper/AFMotor_MultiStepper.pde create mode 100644 Arduino/libraries/AccelStepper/examples/Blocking/Blocking.pde create mode 100644 Arduino/libraries/AccelStepper/examples/Bounce/Bounce.pde create mode 100644 Arduino/libraries/AccelStepper/examples/ConstantSpeed/ConstantSpeed.pde create mode 100644 Arduino/libraries/AccelStepper/examples/DualMotorShield/DualMotorShield.pde create mode 100644 Arduino/libraries/AccelStepper/examples/MotorShield/MotorShield.pde create mode 100644 Arduino/libraries/AccelStepper/examples/MultiStepper/MultiStepper.pde create mode 100644 Arduino/libraries/AccelStepper/examples/MultipleSteppers/MultipleSteppers.pde create mode 100644 Arduino/libraries/AccelStepper/examples/Overshoot/Overshoot.pde create mode 100644 Arduino/libraries/AccelStepper/examples/ProportionalControl/ProportionalControl.pde create mode 100644 Arduino/libraries/AccelStepper/examples/Quickstop/Quickstop.pde create mode 100644 Arduino/libraries/AccelStepper/examples/Random/Random.pde (limited to 'Arduino/libraries/AccelStepper/examples') diff --git a/Arduino/libraries/AccelStepper/examples/AFMotor_ConstantSpeed/AFMotor_ConstantSpeed.pde b/Arduino/libraries/AccelStepper/examples/AFMotor_ConstantSpeed/AFMotor_ConstantSpeed.pde new file mode 100644 index 0000000..d755e18 --- /dev/null +++ b/Arduino/libraries/AccelStepper/examples/AFMotor_ConstantSpeed/AFMotor_ConstantSpeed.pde @@ -0,0 +1,40 @@ +// AFMotor_ConstantSpeed.pde +// -*- mode: C++ -*- +// +// Shows how to run AccelStepper in the simplest, +// fixed speed mode with no accelerations +// Requires the AFMotor library +// (https://github.com/adafruit/Adafruit-Motor-Shield-library) +// Caution, does not work with Adafruit Motor Shield V2 +// See https://github.com/adafruit/Adafruit_Motor_Shield_V2_Library +// for examples that work with Adafruit Motor Shield V2. + +#include +#include + +AF_Stepper motor1(200, 1); + + +// you can change these to DOUBLE or INTERLEAVE or MICROSTEP! +void forwardstep() { + motor1.onestep(FORWARD, SINGLE); +} +void backwardstep() { + motor1.onestep(BACKWARD, SINGLE); +} + +AccelStepper stepper(forwardstep, backwardstep); // use functions to step + +void setup() +{ + Serial.begin(9600); // set up Serial library at 9600 bps + Serial.println("Stepper test!"); + + stepper.setMaxSpeed(50); + stepper.setSpeed(50); +} + +void loop() +{ + stepper.runSpeed(); +} diff --git a/Arduino/libraries/AccelStepper/examples/AFMotor_MultiStepper/AFMotor_MultiStepper.pde b/Arduino/libraries/AccelStepper/examples/AFMotor_MultiStepper/AFMotor_MultiStepper.pde new file mode 100644 index 0000000..db0fd2a --- /dev/null +++ b/Arduino/libraries/AccelStepper/examples/AFMotor_MultiStepper/AFMotor_MultiStepper.pde @@ -0,0 +1,57 @@ +// AFMotor_MultiStepper.pde +// -*- mode: C++ -*- +// +// Control both Stepper motors at the same time with different speeds +// and accelerations. +// Requires the AFMotor library (https://github.com/adafruit/Adafruit-Motor-Shield-library) +// Caution, does not work with Adafruit Motor Shield V2 +// See https://github.com/adafruit/Adafruit_Motor_Shield_V2_Library +// for examples that work with Adafruit Motor Shield V2. + +#include +#include + +// two stepper motors one on each port +AF_Stepper motor1(200, 1); +AF_Stepper motor2(200, 2); + +// you can change these to DOUBLE or INTERLEAVE or MICROSTEP! +// wrappers for the first motor! +void forwardstep1() { + motor1.onestep(FORWARD, SINGLE); +} +void backwardstep1() { + motor1.onestep(BACKWARD, SINGLE); +} +// wrappers for the second motor! +void forwardstep2() { + motor2.onestep(FORWARD, SINGLE); +} +void backwardstep2() { + motor2.onestep(BACKWARD, SINGLE); +} + +// Motor shield has two motor ports, now we'll wrap them in an AccelStepper object +AccelStepper stepper1(forwardstep1, backwardstep1); +AccelStepper stepper2(forwardstep2, backwardstep2); + +void setup() +{ + stepper1.setMaxSpeed(200.0); + stepper1.setAcceleration(100.0); + stepper1.moveTo(24); + + stepper2.setMaxSpeed(300.0); + stepper2.setAcceleration(100.0); + stepper2.moveTo(1000000); + +} + +void loop() +{ + // Change direction at the limits + if (stepper1.distanceToGo() == 0) + stepper1.moveTo(-stepper1.currentPosition()); + stepper1.run(); + stepper2.run(); +} diff --git a/Arduino/libraries/AccelStepper/examples/Blocking/Blocking.pde b/Arduino/libraries/AccelStepper/examples/Blocking/Blocking.pde new file mode 100644 index 0000000..f91b34e --- /dev/null +++ b/Arduino/libraries/AccelStepper/examples/Blocking/Blocking.pde @@ -0,0 +1,28 @@ +// Blocking.pde +// -*- mode: C++ -*- +// +// Shows how to use the blocking call runToNewPosition +// Which sets a new target position and then waits until the stepper has +// achieved it. +// +// Copyright (C) 2009 Mike McCauley +// $Id: Blocking.pde,v 1.1 2011/01/05 01:51:01 mikem Exp mikem $ + +#include + +// Define a stepper and the pins it will use +AccelStepper stepper; // Defaults to AccelStepper::FULL4WIRE (4 pins) on 2, 3, 4, 5 + +void setup() +{ + stepper.setMaxSpeed(200.0); + stepper.setAcceleration(100.0); +} + +void loop() +{ + stepper.runToNewPosition(0); + stepper.runToNewPosition(500); + stepper.runToNewPosition(100); + stepper.runToNewPosition(120); +} diff --git a/Arduino/libraries/AccelStepper/examples/Bounce/Bounce.pde b/Arduino/libraries/AccelStepper/examples/Bounce/Bounce.pde new file mode 100644 index 0000000..6073c53 --- /dev/null +++ b/Arduino/libraries/AccelStepper/examples/Bounce/Bounce.pde @@ -0,0 +1,29 @@ +// Bounce.pde +// -*- mode: C++ -*- +// +// Make a single stepper bounce from one limit to another +// +// Copyright (C) 2012 Mike McCauley +// $Id: Random.pde,v 1.1 2011/01/05 01:51:01 mikem Exp mikem $ + +#include + +// Define a stepper and the pins it will use +AccelStepper stepper; // Defaults to AccelStepper::FULL4WIRE (4 pins) on 2, 3, 4, 5 + +void setup() +{ + // Change these to suit your stepper if you want + stepper.setMaxSpeed(100); + stepper.setAcceleration(20); + stepper.moveTo(500); +} + +void loop() +{ + // If at the end of travel go to the other end + if (stepper.distanceToGo() == 0) + stepper.moveTo(-stepper.currentPosition()); + + stepper.run(); +} diff --git a/Arduino/libraries/AccelStepper/examples/ConstantSpeed/ConstantSpeed.pde b/Arduino/libraries/AccelStepper/examples/ConstantSpeed/ConstantSpeed.pde new file mode 100644 index 0000000..8aef26d --- /dev/null +++ b/Arduino/libraries/AccelStepper/examples/ConstantSpeed/ConstantSpeed.pde @@ -0,0 +1,23 @@ +// ConstantSpeed.pde +// -*- mode: C++ -*- +// +// Shows how to run AccelStepper in the simplest, +// fixed speed mode with no accelerations +/// \author Mike McCauley (mikem@airspayce.com) +// Copyright (C) 2009 Mike McCauley +// $Id: ConstantSpeed.pde,v 1.1 2011/01/05 01:51:01 mikem Exp mikem $ + +#include + +AccelStepper stepper; // Defaults to AccelStepper::FULL4WIRE (4 pins) on 2, 3, 4, 5 + +void setup() +{ + stepper.setMaxSpeed(1000); + stepper.setSpeed(50); +} + +void loop() +{ + stepper.runSpeed(); +} diff --git a/Arduino/libraries/AccelStepper/examples/DualMotorShield/DualMotorShield.pde b/Arduino/libraries/AccelStepper/examples/DualMotorShield/DualMotorShield.pde new file mode 100644 index 0000000..1ca70b1 --- /dev/null +++ b/Arduino/libraries/AccelStepper/examples/DualMotorShield/DualMotorShield.pde @@ -0,0 +1,49 @@ +// DualMotorShield.pde +// -*- mode: C++ -*- +// +// Shows how to run 2 simultaneous steppers +// using the Itead Studio Arduino Dual Stepper Motor Driver Shield +// model IM120417015 +// This shield is capable of driving 2 steppers at +// currents of up to 750mA +// and voltages up to 30V +// Runs both steppers forwards and backwards, accelerating and decelerating +// at the limits. +// +// Copyright (C) 2014 Mike McCauley +// $Id: $ + +#include + +// The X Stepper pins +#define STEPPER1_DIR_PIN 3 +#define STEPPER1_STEP_PIN 2 +// The Y stepper pins +#define STEPPER2_DIR_PIN 7 +#define STEPPER2_STEP_PIN 6 + +// Define some steppers and the pins the will use +AccelStepper stepper1(AccelStepper::DRIVER, STEPPER1_STEP_PIN, STEPPER1_DIR_PIN); +AccelStepper stepper2(AccelStepper::DRIVER, STEPPER2_STEP_PIN, STEPPER2_DIR_PIN); + +void setup() +{ + stepper1.setMaxSpeed(200.0); + stepper1.setAcceleration(200.0); + stepper1.moveTo(100); + + stepper2.setMaxSpeed(100.0); + stepper2.setAcceleration(100.0); + stepper2.moveTo(100); +} + +void loop() +{ + // Change direction at the limits + if (stepper1.distanceToGo() == 0) + stepper1.moveTo(-stepper1.currentPosition()); + if (stepper2.distanceToGo() == 0) + stepper2.moveTo(-stepper2.currentPosition()); + stepper1.run(); + stepper2.run(); +} diff --git a/Arduino/libraries/AccelStepper/examples/MotorShield/MotorShield.pde b/Arduino/libraries/AccelStepper/examples/MotorShield/MotorShield.pde new file mode 100644 index 0000000..8018ed1 --- /dev/null +++ b/Arduino/libraries/AccelStepper/examples/MotorShield/MotorShield.pde @@ -0,0 +1,103 @@ +// AFMotor_ConstantSpeed.pde +// -*- mode: C++ -*- +// +// Shows how to use AccelStepper to control a 3-phase motor, such as a HDD spindle motor +// using the Adafruit Motor Shield +// http://www.ladyada.net/make/mshield/index.html. +// Create a subclass of AccelStepper which controls the motor pins via the +// Motor Shield serial-to-parallel interface + +#include + +// Arduino pin names for interface to 74HCT595 latch +// on Adafruit Motor Shield +#define MOTORLATCH 12 +#define MOTORCLK 4 +#define MOTORENABLE 7 +#define MOTORDATA 8 + +// PWM pins, also used to enable motor outputs +#define PWM0A 5 +#define PWM0B 6 +#define PWM1A 9 +#define PWM1B 10 +#define PWM2A 11 +#define PWM2B 3 + + +// The main purpose of this class is to override setOutputPins to work with Adafruit Motor Shield +class AFMotorShield : public AccelStepper +{ + public: + AFMotorShield(uint8_t interface = AccelStepper::FULL4WIRE, uint8_t pin1 = 2, uint8_t pin2 = 3, uint8_t pin3 = 4, uint8_t pin4 = 5); + + virtual void setOutputPins(uint8_t mask); +}; + + +AFMotorShield::AFMotorShield(uint8_t interface, uint8_t pin1, uint8_t pin2, uint8_t pin3, uint8_t pin4) + : AccelStepper(interface, pin1, pin2, pin3, pin4) +{ + // Enable motor control serial to parallel latch + pinMode(MOTORLATCH, OUTPUT); + pinMode(MOTORENABLE, OUTPUT); + pinMode(MOTORDATA, OUTPUT); + pinMode(MOTORCLK, OUTPUT); + digitalWrite(MOTORENABLE, LOW); + + // enable both H bridges on motor 1 + pinMode(PWM2A, OUTPUT); + pinMode(PWM2B, OUTPUT); + pinMode(PWM0A, OUTPUT); + pinMode(PWM0B, OUTPUT); + digitalWrite(PWM2A, HIGH); + digitalWrite(PWM2B, HIGH); + digitalWrite(PWM0A, HIGH); + digitalWrite(PWM0B, HIGH); + + setOutputPins(0); // Reset +}; + +// Use the AF Motor Shield serial-to-parallel to set the state of the motor pins +// Caution: the mapping of AccelStepper pins to AF motor outputs is not +// obvious: +// AccelStepper Motor Shield output +// pin1 M4A +// pin2 M1A +// pin3 M2A +// pin4 M3A +// Caution this is pretty slow and limits the max speed of the motor to about 500/3 rpm +void AFMotorShield::setOutputPins(uint8_t mask) +{ + uint8_t i; + + digitalWrite(MOTORLATCH, LOW); + digitalWrite(MOTORDATA, LOW); + + for (i=0; i<8; i++) + { + digitalWrite(MOTORCLK, LOW); + + if (mask & _BV(7-i)) + digitalWrite(MOTORDATA, HIGH); + else + digitalWrite(MOTORDATA, LOW); + + digitalWrite(MOTORCLK, HIGH); + } + digitalWrite(MOTORLATCH, HIGH); +} + +AFMotorShield stepper(AccelStepper::HALF3WIRE, 0, 0, 0, 0); // 3 phase HDD spindle drive + +void setup() +{ + stepper.setMaxSpeed(500); // divide by 3 to get rpm + stepper.setAcceleration(80); + stepper.moveTo(10000000); +} + +void loop() +{ + stepper.run(); +} diff --git a/Arduino/libraries/AccelStepper/examples/MultiStepper/MultiStepper.pde b/Arduino/libraries/AccelStepper/examples/MultiStepper/MultiStepper.pde new file mode 100644 index 0000000..386c784 --- /dev/null +++ b/Arduino/libraries/AccelStepper/examples/MultiStepper/MultiStepper.pde @@ -0,0 +1,44 @@ +// MultiStepper.pde +// -*- mode: C++ -*- +// Use MultiStepper class to manage multiple steppers and make them all move to +// the same position at the same time for linear 2d (or 3d) motion. + +#include +#include + +// EG X-Y position bed driven by 2 steppers +// Alas its not possible to build an array of these with different pins for each :-( +AccelStepper stepper1(AccelStepper::FULL4WIRE, 2, 3, 4, 5); +AccelStepper stepper2(AccelStepper::FULL4WIRE, 8, 9, 10, 11); + +// Up to 10 steppers can be handled as a group by MultiStepper +MultiStepper steppers; + +void setup() { + Serial.begin(9600); + + // Configure each stepper + stepper1.setMaxSpeed(100); + stepper2.setMaxSpeed(100); + + // Then give them to MultiStepper to manage + steppers.addStepper(stepper1); + steppers.addStepper(stepper2); +} + +void loop() { + long positions[2]; // Array of desired stepper positions + + positions[0] = 1000; + positions[1] = 50; + steppers.moveTo(positions); + steppers.runSpeedToPosition(); // Blocks until all are in position + delay(1000); + + // Move to a different coordinate + positions[0] = -100; + positions[1] = 100; + steppers.moveTo(positions); + steppers.runSpeedToPosition(); // Blocks until all are in position + delay(1000); +} diff --git a/Arduino/libraries/AccelStepper/examples/MultipleSteppers/MultipleSteppers.pde b/Arduino/libraries/AccelStepper/examples/MultipleSteppers/MultipleSteppers.pde new file mode 100644 index 0000000..242e9f5 --- /dev/null +++ b/Arduino/libraries/AccelStepper/examples/MultipleSteppers/MultipleSteppers.pde @@ -0,0 +1,41 @@ +// MultiStepper.pde +// -*- mode: C++ -*- +// +// Shows how to multiple simultaneous steppers +// Runs one stepper forwards and backwards, accelerating and decelerating +// at the limits. Runs other steppers at the same time +// +// Copyright (C) 2009 Mike McCauley +// $Id: MultiStepper.pde,v 1.1 2011/01/05 01:51:01 mikem Exp mikem $ + +#include + +// Define some steppers and the pins the will use +AccelStepper stepper1; // Defaults to AccelStepper::FULL4WIRE (4 pins) on 2, 3, 4, 5 +AccelStepper stepper2(AccelStepper::FULL4WIRE, 6, 7, 8, 9); +AccelStepper stepper3(AccelStepper::FULL2WIRE, 10, 11); + +void setup() +{ + stepper1.setMaxSpeed(200.0); + stepper1.setAcceleration(100.0); + stepper1.moveTo(24); + + stepper2.setMaxSpeed(300.0); + stepper2.setAcceleration(100.0); + stepper2.moveTo(1000000); + + stepper3.setMaxSpeed(300.0); + stepper3.setAcceleration(100.0); + stepper3.moveTo(1000000); +} + +void loop() +{ + // Change direction at the limits + if (stepper1.distanceToGo() == 0) + stepper1.moveTo(-stepper1.currentPosition()); + stepper1.run(); + stepper2.run(); + stepper3.run(); +} diff --git a/Arduino/libraries/AccelStepper/examples/Overshoot/Overshoot.pde b/Arduino/libraries/AccelStepper/examples/Overshoot/Overshoot.pde new file mode 100644 index 0000000..7e16baf --- /dev/null +++ b/Arduino/libraries/AccelStepper/examples/Overshoot/Overshoot.pde @@ -0,0 +1,28 @@ +// Overshoot.pde +// -*- mode: C++ -*- +// +// Check overshoot handling +// which sets a new target position and then waits until the stepper has +// achieved it. This is used for testing the handling of overshoots +// +// Copyright (C) 2009 Mike McCauley +// $Id: Overshoot.pde,v 1.1 2011/01/05 01:51:01 mikem Exp mikem $ + +#include + +// Define a stepper and the pins it will use +AccelStepper stepper; // Defaults to AccelStepper::FULL4WIRE (4 pins) on 2, 3, 4, 5 + +void setup() +{ + stepper.setMaxSpeed(150); + stepper.setAcceleration(100); +} + +void loop() +{ + stepper.moveTo(500); + while (stepper.currentPosition() != 300) // Full speed up to 300 + stepper.run(); + stepper.runToNewPosition(0); // Cause an overshoot then back to 0 +} diff --git a/Arduino/libraries/AccelStepper/examples/ProportionalControl/ProportionalControl.pde b/Arduino/libraries/AccelStepper/examples/ProportionalControl/ProportionalControl.pde new file mode 100644 index 0000000..2afe444 --- /dev/null +++ b/Arduino/libraries/AccelStepper/examples/ProportionalControl/ProportionalControl.pde @@ -0,0 +1,32 @@ +// ProportionalControl.pde +// -*- mode: C++ -*- +// +// Make a single stepper follow the analog value read from a pot or whatever +// The stepper will move at a constant speed to each newly set posiiton, +// depending on the value of the pot. +// +// Copyright (C) 2012 Mike McCauley +// $Id: ProportionalControl.pde,v 1.1 2011/01/05 01:51:01 mikem Exp mikem $ + +#include + +// Define a stepper and the pins it will use +AccelStepper stepper; // Defaults to AccelStepper::FULL4WIRE (4 pins) on 2, 3, 4, 5 + +// This defines the analog input pin for reading the control voltage +// Tested with a 10k linear pot between 5v and GND +#define ANALOG_IN A0 + +void setup() +{ + stepper.setMaxSpeed(1000); +} + +void loop() +{ + // Read new position + int analog_in = analogRead(ANALOG_IN); + stepper.moveTo(analog_in); + stepper.setSpeed(100); + stepper.runSpeedToPosition(); +} diff --git a/Arduino/libraries/AccelStepper/examples/Quickstop/Quickstop.pde b/Arduino/libraries/AccelStepper/examples/Quickstop/Quickstop.pde new file mode 100644 index 0000000..e6cfd44 --- /dev/null +++ b/Arduino/libraries/AccelStepper/examples/Quickstop/Quickstop.pde @@ -0,0 +1,40 @@ +// Quickstop.pde +// -*- mode: C++ -*- +// +// Check stop handling. +// Calls stop() while the stepper is travelling at full speed, causing +// the stepper to stop as quickly as possible, within the constraints of the +// current acceleration. +// +// Copyright (C) 2012 Mike McCauley +// $Id: $ + +#include + +// Define a stepper and the pins it will use +AccelStepper stepper; // Defaults to AccelStepper::FULL4WIRE (4 pins) on 2, 3, 4, 5 + +void setup() +{ + stepper.setMaxSpeed(150); + stepper.setAcceleration(100); +} + +void loop() +{ + stepper.moveTo(500); + while (stepper.currentPosition() != 300) // Full speed up to 300 + stepper.run(); + stepper.stop(); // Stop as fast as possible: sets new target + stepper.runToPosition(); + // Now stopped after quickstop + + // Now go backwards + stepper.moveTo(-500); + while (stepper.currentPosition() != 0) // Full speed basck to 0 + stepper.run(); + stepper.stop(); // Stop as fast as possible: sets new target + stepper.runToPosition(); + // Now stopped after quickstop + +} diff --git a/Arduino/libraries/AccelStepper/examples/Random/Random.pde b/Arduino/libraries/AccelStepper/examples/Random/Random.pde new file mode 100644 index 0000000..871d361 --- /dev/null +++ b/Arduino/libraries/AccelStepper/examples/Random/Random.pde @@ -0,0 +1,30 @@ +// Random.pde +// -*- mode: C++ -*- +// +// Make a single stepper perform random changes in speed, position and acceleration +// +// Copyright (C) 2009 Mike McCauley +// $Id: Random.pde,v 1.1 2011/01/05 01:51:01 mikem Exp mikem $ + +#include + +// Define a stepper and the pins it will use +AccelStepper stepper; // Defaults to AccelStepper::FULL4WIRE (4 pins) on 2, 3, 4, 5 + +void setup() +{ +} + +void loop() +{ + if (stepper.distanceToGo() == 0) + { + // Random change to speed, position and acceleration + // Make sure we dont get 0 speed or accelerations + delay(1000); + stepper.moveTo(rand() % 200); + stepper.setMaxSpeed((rand() % 200) + 1); + stepper.setAcceleration((rand() % 200) + 1); + } + stepper.run(); +} -- cgit v1.2.3