Skip to content

Commit

Permalink
Merge pull request #105 from DCC-EX:next-release
Browse files Browse the repository at this point in the history
v0.7.0-Devel
  • Loading branch information
peteGSX committed Feb 26, 2024
2 parents 8a48fe7 + ec07e28 commit 50b0f90
Show file tree
Hide file tree
Showing 14 changed files with 560 additions and 452 deletions.
52 changes: 33 additions & 19 deletions AccelStepper.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// AccelStepper.cpp
//
// Copyright (C) 2009-2013 Mike McCauley
// Copyright (C) 2009-2020 Mike McCauley
// $Id: AccelStepper.cpp,v 1.24 2020/04/20 00:15:03 mikem Exp mikem $

#include "AccelStepper.h"
Expand Down Expand Up @@ -94,7 +94,8 @@ void AccelStepper::setCurrentPosition(long position)
_speed = 0.0;
}

void AccelStepper::computeNewSpeed()
// Subclasses can override
unsigned long AccelStepper::computeNewSpeed()
{
long distanceTo = distanceToGo(); // +ve is clockwise from curent location

Expand All @@ -106,7 +107,7 @@ void AccelStepper::computeNewSpeed()
_stepInterval = 0;
_speed = 0.0;
_n = 0;
return;
return _stepInterval;
}

if (distanceTo > 0)
Expand Down Expand Up @@ -174,6 +175,7 @@ void AccelStepper::computeNewSpeed()
Serial.println(stepsToStop);
Serial.println("-----");
#endif
return _stepInterval;
}

// Run the motor to implement speed and acceleration in order to proceed to the target position
Expand All @@ -187,17 +189,13 @@ boolean AccelStepper::run()
return _speed != 0.0 || distanceToGo() != 0;
}

// Line commented out below is the original AccelStepper line.
// AccelStepper::AccelStepper(uint8_t interface, uint8_t pin1, uint8_t pin2, uint8_t pin3, uint8_t pin4, bool enable)

// EX-Turntable modification: add extra flag to enable automatic inversion of two wire drivers at instantiation.
AccelStepper::AccelStepper(uint8_t interface, uint8_t pin1, uint8_t pin2, uint8_t pin3, uint8_t pin4, bool enable, bool invert)
AccelStepper::AccelStepper(uint8_t interface, uint8_t pin1, uint8_t pin2, uint8_t pin3, uint8_t pin4, bool enable)
{
_interface = interface;
_currentPos = 0;
_targetPos = 0;
_speed = 0.0;
_maxSpeed = 1.0;
_maxSpeed = 0.0;
_acceleration = 0.0;
_sqrt_twoa = 1.0;
_stepInterval = 0;
Expand All @@ -209,7 +207,6 @@ AccelStepper::AccelStepper(uint8_t interface, uint8_t pin1, uint8_t pin2, uint8_
_pin[2] = pin3;
_pin[3] = pin4;
_enableInverted = false;
_invert = invert;

// NEW
_n = 0;
Expand All @@ -225,14 +222,7 @@ AccelStepper::AccelStepper(uint8_t interface, uint8_t pin1, uint8_t pin2, uint8_
enableOutputs();
// Some reasonable default
setAcceleration(1);

// Turntable-EX modification: add extra flag to enable automatic inversion of two wire drivers at instantiation.
if (_interface == AccelStepper::DRIVER) {
setEnablePin(_pin[2]);
if (_invert) {
setPinsInverted(false, false, true);
}
}
setMaxSpeed(1);
}

AccelStepper::AccelStepper(void (*forward)(), void (*backward)())
Expand All @@ -241,7 +231,7 @@ AccelStepper::AccelStepper(void (*forward)(), void (*backward)())
_currentPos = 0;
_targetPos = 0;
_speed = 0.0;
_maxSpeed = 1.0;
_maxSpeed = 0.0;
_acceleration = 0.0;
_sqrt_twoa = 1.0;
_stepInterval = 0;
Expand All @@ -267,6 +257,7 @@ AccelStepper::AccelStepper(void (*forward)(), void (*backward)())
_pinInverted[i] = 0;
// Some reasonable default
setAcceleration(1);
setMaxSpeed(1);
}

void AccelStepper::setMaxSpeed(float speed)
Expand Down Expand Up @@ -308,6 +299,11 @@ void AccelStepper::setAcceleration(float acceleration)
}
}

float AccelStepper::acceleration()
{
return _acceleration;
}

void AccelStepper::setSpeed(float speed)
{
if (speed == _speed)
Expand Down Expand Up @@ -363,6 +359,24 @@ void AccelStepper::step(long step)
}
}

long AccelStepper::stepForward()
{
// Clockwise
_currentPos += 1;
step(_currentPos);
_lastStepTime = micros();
return _currentPos;
}

long AccelStepper::stepBackward()
{
// Counter-clockwise
_currentPos -= 1;
step(_currentPos);
_lastStepTime = micros();
return _currentPos;
}

// 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]
Expand Down
66 changes: 46 additions & 20 deletions AccelStepper.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
/// The latest version of this documentation can be downloaded from
/// http://www.airspayce.com/mikem/arduino/AccelStepper
/// The version of the package that this documentation refers to can be downloaded
/// from http://www.airspayce.com/mikem/arduino/AccelStepper/AccelStepper-1.61.zip
/// from http://www.airspayce.com/mikem/arduino/AccelStepper/AccelStepper-1.64.zip
///
/// Example Arduino programs are included to show the main modes of use.
///
Expand All @@ -35,6 +35,10 @@
/// - http://www.catb.org/esr/faqs/smart-questions.html
/// - http://www.chiark.greenend.org.uk/~shgtatham/bugs.html
///
/// Beginners to C++ and stepper motors in general may find this helpful:
/// - https://hackaday.io/project/183279-accelstepper-the-missing-manual
/// - https://hackaday.io/project/183713-using-the-arduino-accelstepper-library
///
/// Tested on Arduino Diecimila and Mega with arduino-0018 & arduino-0021
/// on OpenSuSE 11.1 and avr-libc-1.6.1-1.15,
/// cross-avr-binutils-2.19-9.1, cross-avr-gcc-4.1.3_20080612-26.5.
Expand Down Expand Up @@ -81,7 +85,7 @@
///
/// \par Copyright
///
/// This software is Copyright (C) 2010-2018 Mike McCauley. Use is subject to license
/// This software is Copyright (C) 2010-2021 Mike McCauley. Use is subject to license
/// conditions. The main licensing options available are GPL V3 or Commercial:
///
/// \par Open Source Licensing GPL V3
Expand Down Expand Up @@ -249,9 +253,18 @@
/// \version 1.61 2020-04-20
/// Added yield() call in runToPosition(), so that platforms like esp8266 dont hang/crash
/// during long runs.
/// \version 1.62 2022-05-22
/// Added link to AccelStepper - The Missing Manual.<br>
/// Fixed a problem when setting the maxSpeed to 1.0 due to incomplete initialisation.
/// Reported by Olivier Pécheux. <br>
/// \version 1.63 2022-06-30
/// Added virtual destructor at the request of Jan.<br>
/// \version 1.64 2022-10-31
/// Patch courtesy acwest: Changes to make AccelStepper more subclassable. These changes are
/// largely oriented to implementing new step-scheduling algorithms.
///
/// \author Mike McCauley (mikem@airspayce.com) DO NOT CONTACT THE AUTHOR DIRECTLY: USE THE LISTS
// Copyright (C) 2009-2013 Mike McCauley
/// \author Mike McCauley (mikem@airspayce.com) DO NOT CONTACT THE AUTHOR DIRECTLY: USE THE GOOGLE GROUP
// Copyright (C) 2009-2020 Mike McCauley
// $Id: AccelStepper.h,v 1.28 2020/04/20 00:15:03 mikem Exp mikem $

#ifndef AccelStepper_h
Expand Down Expand Up @@ -370,12 +383,7 @@ class AccelStepper
/// to pin 5.
/// \param[in] enable If this is true (the default), enableOutputs() will be called to enable
/// the output pins at construction time.

// Line below commented out is the original AccelStepper line.
// AccelStepper(uint8_t interface = AccelStepper::FULL4WIRE, uint8_t pin1 = 2, uint8_t pin2 = 3, uint8_t pin3 = 4, uint8_t pin4 = 5, bool enable = true);

// EX-Turntable modification: add extra flag to enable automatic inversion of two wire drivers at instantiation.
AccelStepper(uint8_t interface = AccelStepper::FULL4WIRE, uint8_t pin1 = 2, uint8_t pin2 = 3, uint8_t pin3 = 4, uint8_t pin4 = 5, bool enable = true, bool invert = false);
AccelStepper(uint8_t interface = AccelStepper::FULL4WIRE, uint8_t pin1 = 2, uint8_t pin2 = 3, uint8_t pin3 = 4, uint8_t pin4 = 5, bool enable = true);

/// Alternate Constructor which will call your own functions for forward and backward steps.
/// You can have multiple simultaneous steppers, all moving
Expand Down Expand Up @@ -434,6 +442,11 @@ class AccelStepper
/// root to be calculated. Dont call more ofthen than needed
void setAcceleration(float acceleration);

/// Returns the acceleration/deceleration rate configured for this stepper
/// that was previously set by setAcceleration();
/// \return The currently configured acceleration/deceleration
float acceleration();

/// Sets the desired constant speed for use with runSpeed().
/// \param[in] speed The desired constant speed in steps per
/// second. Positive is clockwise. Speeds of more than 1000 steps per
Expand Down Expand Up @@ -476,7 +489,10 @@ class AccelStepper
/// position. Dont use this in event loops, since it blocks.
void runToPosition();

/// Runs at the currently selected speed until the target position is reached.
/// Executes runSpeed() unless the targetPosition is reached.
/// This function needs to be called often just like runSpeed() or run().
/// Will step the motor if a step is required at the currently selected
/// speed unless the target position has been reached.
/// Does not implement accelerations.
/// \return true if it stepped
boolean runSpeedToPosition();
Expand Down Expand Up @@ -538,6 +554,8 @@ class AccelStepper
/// \return true if the speed is not zero or not at the target position
bool isRunning();

/// Virtual destructor to prevent warnings during delete
virtual ~AccelStepper() {};
protected:

/// \brief Direction indicator
Expand All @@ -556,7 +574,8 @@ class AccelStepper
/// \li after change to acceleration through setAcceleration()
/// \li after change to target position (relative or absolute) through
/// move() or moveTo()
void computeNewSpeed();
/// \return the new step interval
virtual unsigned long computeNewSpeed();

/// Low level function to set the motor output pins
/// bit 0 of the mask corresponds to _pin[0]
Expand All @@ -571,6 +590,16 @@ class AccelStepper
/// number of pins defined for the stepper.
/// \param[in] step The current step phase number (0 to 7)
virtual void step(long step);

/// Called to execute a clockwise(+) step. Only called when a new step is
/// required. This increments the _currentPos and calls step()
/// \return the updated current position
long stepForward();

/// Called to execute a counter-clockwise(-) step. Only called when a new step is
/// required. This decrements the _currentPos and calls step()
/// \return the updated current position
long stepBackward();

/// Called to execute a step using stepper functions (pins = 0) Only called when a new step is
/// required. Calls _forward() or _backward() to perform the step
Expand Down Expand Up @@ -612,7 +641,7 @@ class AccelStepper
/// \param[in] step The current step phase number (0 to 7)
virtual void step6(long step);

/// Called to execute a step on a 4 pin half-steper motor. Only called when a new step is
/// Called to execute a step on a 4 pin half-stepper motor. Only called when a new step is
/// required. Subclasses may override to implement new stepping
/// interfaces. The default sets or clears the outputs of pin1, pin2,
/// pin3, pin4.
Expand All @@ -623,6 +652,10 @@ class AccelStepper
/// Protected because some peoples subclasses need it to be so
boolean _direction; // 1 == CW

/// The current interval between steps in microseconds.
/// 0 means the motor is currently stopped with _speed == 0
unsigned long _stepInterval;

private:
/// Number of pins on the stepper motor. Permits 2 or 4. 2 pins is a
/// bipolar, and 4 pins is a unipolar.
Expand Down Expand Up @@ -655,10 +688,6 @@ class AccelStepper
float _acceleration;
float _sqrt_twoa; // Precomputed sqrt(2*_acceleration)

/// The current interval between steps in microseconds.
/// 0 means the motor is currently stopped with _speed == 0
unsigned long _stepInterval;

/// The last step time in microseconds
unsigned long _lastStepTime;

Expand Down Expand Up @@ -695,9 +724,6 @@ class AccelStepper
/// Min step size in microseconds based on maxSpeed
float _cmin; // at max speed

// EX-Turntable modification: add extra flag to enable automatic inversion of two wire drivers at instantiation.
bool _invert;

};

/// @example Random.pde
Expand Down
23 changes: 12 additions & 11 deletions EEPROMFunctions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@

#include "EEPROMFunctions.h"
#include <EEPROM.h>
#include "IOFunctions.h"

char eepromFlag[4] = {'T', 'T', 'E', 'X'}; // EEPROM location 0 to 3 should contain TTEX if we have stored steps.
const uint8_t eepromVersion = EEPROM_VERSION; // Version of stored EEPROM data to invalidate stored steps if config changes.
Expand Down Expand Up @@ -46,23 +47,23 @@ long getSteps() {
if (stepsSet) {
eepromSteps = ((long)EEPROM.read(5) << 24) + ((long)EEPROM.read(6) << 16) + ((long)EEPROM.read(7) << 8) + (long)EEPROM.read(8);
if (eepromSteps <= sanitySteps) {
#ifdef DEBUG
Serial.print(F("DEBUG: TTEX steps defined in EEPROM: "));
Serial.println(eepromSteps);
#endif
if (debug) {
Serial.print(F("DEBUG: TTEX steps defined in EEPROM: "));
Serial.println(eepromSteps);
}
return eepromSteps;
} else {
#ifdef DEBUG
Serial.print(F("DEBUG: TTEX steps defined in EEPROM are invalid: "));
Serial.println(eepromSteps);
#endif
if (debug) {
Serial.print(F("DEBUG: TTEX steps defined in EEPROM are invalid: "));
Serial.println(eepromSteps);
}
calibrating = true;
return 0;
}
} else {
#ifdef DEBUG
Serial.println(F("DEBUG: TTEX steps not defined in EEPROM"));
#endif
if (debug) {
Serial.println(F("DEBUG: TTEX steps not defined in EEPROM"));
}
calibrating = true;
return 0;
}
Expand Down
Loading

0 comments on commit 50b0f90

Please sign in to comment.