diff --git a/.project b/.project new file mode 100644 index 0000000..fc71ce5 --- /dev/null +++ b/.project @@ -0,0 +1,11 @@ + + + GB_Printer + + + + + + + + diff --git a/Source/Arduino/EggDuino/.gitattributes b/Source/Arduino/EggDuino/.gitattributes new file mode 100644 index 0000000..412eeda --- /dev/null +++ b/Source/Arduino/EggDuino/.gitattributes @@ -0,0 +1,22 @@ +# Auto detect text files and perform LF normalization +* text=auto + +# Custom for Visual Studio +*.cs diff=csharp +*.sln merge=union +*.csproj merge=union +*.vbproj merge=union +*.fsproj merge=union +*.dbproj merge=union + +# Standard to msysgit +*.doc diff=astextplain +*.DOC diff=astextplain +*.docx diff=astextplain +*.DOCX diff=astextplain +*.dot diff=astextplain +*.DOT diff=astextplain +*.pdf diff=astextplain +*.PDF diff=astextplain +*.rtf diff=astextplain +*.RTF diff=astextplain diff --git a/Source/Arduino/EggDuino/.gitignore b/Source/Arduino/EggDuino/.gitignore new file mode 100644 index 0000000..b9d6bd9 --- /dev/null +++ b/Source/Arduino/EggDuino/.gitignore @@ -0,0 +1,215 @@ +################# +## Eclipse +################# + +*.pydevproject +.project +.metadata +bin/ +tmp/ +*.tmp +*.bak +*.swp +*~.nib +local.properties +.classpath +.settings/ +.loadpath + +# External tool builders +.externalToolBuilders/ + +# Locally stored "Eclipse launch configurations" +*.launch + +# CDT-specific +.cproject + +# PDT-specific +.buildpath + + +################# +## Visual Studio +################# + +## Ignore Visual Studio temporary files, build results, and +## files generated by popular Visual Studio add-ons. + +# User-specific files +*.suo +*.user +*.sln.docstates + +# Build results + +[Dd]ebug/ +[Rr]elease/ +x64/ +build/ +[Bb]in/ +[Oo]bj/ + +# MSTest test Results +[Tt]est[Rr]esult*/ +[Bb]uild[Ll]og.* + +*_i.c +*_p.c +*.ilk +*.meta +*.obj +*.pch +*.pdb +*.pgc +*.pgd +*.rsp +*.sbr +*.tlb +*.tli +*.tlh +*.tmp +*.tmp_proj +*.log +*.vspscc +*.vssscc +.builds +*.pidb +*.log +*.scc + +# Visual C++ cache files +ipch/ +*.aps +*.ncb +*.opensdf +*.sdf +*.cachefile + +# Visual Studio profiler +*.psess +*.vsp +*.vspx + +# Guidance Automation Toolkit +*.gpState + +# ReSharper is a .NET coding add-in +_ReSharper*/ +*.[Rr]e[Ss]harper + +# TeamCity is a build add-in +_TeamCity* + +# DotCover is a Code Coverage Tool +*.dotCover + +# NCrunch +*.ncrunch* +.*crunch*.local.xml + +# Installshield output folder +[Ee]xpress/ + +# DocProject is a documentation generator add-in +DocProject/buildhelp/ +DocProject/Help/*.HxT +DocProject/Help/*.HxC +DocProject/Help/*.hhc +DocProject/Help/*.hhk +DocProject/Help/*.hhp +DocProject/Help/Html2 +DocProject/Help/html + +# Click-Once directory +publish/ + +# Publish Web Output +*.Publish.xml +*.pubxml + +# NuGet Packages Directory +## TODO: If you have NuGet Package Restore enabled, uncomment the next line +#packages/ + +# Windows Azure Build Output +csx +*.build.csdef + +# Windows Store app package directory +AppPackages/ + +# Others +sql/ +*.Cache +ClientBin/ +[Ss]tyle[Cc]op.* +~$* +*~ +*.dbmdl +*.[Pp]ublish.xml +*.pfx +*.publishsettings + +# RIA/Silverlight projects +Generated_Code/ + +# Backup & report files from converting an old project file to a newer +# Visual Studio version. Backup files are not needed, because we have git ;-) +_UpgradeReport_Files/ +Backup*/ +UpgradeLog*.XML +UpgradeLog*.htm + +# SQL Server files +App_Data/*.mdf +App_Data/*.ldf + +############# +## Windows detritus +############# + +# Windows image file caches +Thumbs.db +ehthumbs.db + +# Folder config file +Desktop.ini + +# Recycle Bin used on file shares +$RECYCLE.BIN/ + +# Mac crap +.DS_Store + + +############# +## Python +############# + +*.py[co] + +# Packages +*.egg +*.egg-info +dist/ +build/ +eggs/ +parts/ +var/ +sdist/ +develop-eggs/ +.installed.cfg + +# Installer logs +pip-log.txt + +# Unit test / coverage reports +.coverage +.tox + +#Translations +*.mo + +#Mr Developer +.mr.developer.cfg diff --git a/Source/Arduino/EggDuino/AccelStepper.cpp b/Source/Arduino/EggDuino/AccelStepper.cpp new file mode 100644 index 0000000..29e6469 --- /dev/null +++ b/Source/Arduino/EggDuino/AccelStepper.cpp @@ -0,0 +1,633 @@ +// AccelStepper.cpp +// +// Copyright (C) 2009-2013 Mike McCauley +// $Id: AccelStepper.cpp,v 1.19 2014/10/31 06:05:27 mikem Exp mikem $ + +#include "AccelStepper.h" + +#if 0 +// Some debugging assistance +void dump(uint8_t* p, int l) +{ + int i; + + for (i = 0; i < l; i++) + { + Serial.print(p[i], HEX); + Serial.print(" "); + } + Serial.println(""); +} +#endif + +void AccelStepper::moveTo(long absolute) +{ + if (_targetPos != absolute) + { + _targetPos = absolute; + computeNewSpeed(); + // compute new n? + } +} + +void AccelStepper::move(long relative) +{ + moveTo(_currentPos + relative); +} + +// Implements steps according to the current step interval +// You must call this at least once per step +// returns true if a step occurred +boolean AccelStepper::runSpeed() +{ + // Dont do anything unless we actually have a step interval + if (!_stepInterval) + return false; + + unsigned long time = micros(); + unsigned long nextStepTime = _lastStepTime + _stepInterval; + // Gymnastics to detect wrapping of either the nextStepTime and/or the current time + if ( ((nextStepTime >= _lastStepTime) && ((time >= nextStepTime) || (time < _lastStepTime))) + || ((nextStepTime < _lastStepTime) && ((time >= nextStepTime) && (time < _lastStepTime)))) + { + if (_direction == DIRECTION_CW) + { + // Clockwise + _currentPos += 1; + } + else + { + // Anticlockwise + _currentPos -= 1; + } + step(_currentPos); + + _lastStepTime = time; + return true; + } + else + { + return false; + } +} + +long AccelStepper::distanceToGo() +{ + return _targetPos - _currentPos; +} + +long AccelStepper::targetPosition() +{ + return _targetPos; +} + +long AccelStepper::currentPosition() +{ + return _currentPos; +} + +// Useful during initialisations or after initial positioning +// Sets speed to 0 +void AccelStepper::setCurrentPosition(long position) +{ + _targetPos = _currentPos = position; + _n = 0; + _stepInterval = 0; +} + +void AccelStepper::computeNewSpeed() +{ + long distanceTo = distanceToGo(); // +ve is clockwise from curent location + + long stepsToStop = (long)((_speed * _speed) / (2.0 * _acceleration)); // Equation 16 + + if (distanceTo == 0 && stepsToStop <= 1) + { + // We are at the target and its time to stop + _stepInterval = 0; + _speed = 0.0; + _n = 0; + return; + } + + if (distanceTo > 0) + { + // We are anticlockwise from the target + // Need to go clockwise from here, maybe decelerate now + if (_n > 0) + { + // Currently accelerating, need to decel now? Or maybe going the wrong way? + if ((stepsToStop >= distanceTo) || _direction == DIRECTION_CCW) + _n = -stepsToStop; // Start deceleration + } + else if (_n < 0) + { + // Currently decelerating, need to accel again? + if ((stepsToStop < distanceTo) && _direction == DIRECTION_CW) + _n = -_n; // Start accceleration + } + } + else if (distanceTo < 0) + { + // We are clockwise from the target + // Need to go anticlockwise from here, maybe decelerate + if (_n > 0) + { + // Currently accelerating, need to decel now? Or maybe going the wrong way? + if ((stepsToStop >= -distanceTo) || _direction == DIRECTION_CW) + _n = -stepsToStop; // Start deceleration + } + else if (_n < 0) + { + // Currently decelerating, need to accel again? + if ((stepsToStop < -distanceTo) && _direction == DIRECTION_CCW) + _n = -_n; // Start accceleration + } + } + + // Need to accelerate or decelerate + if (_n == 0) + { + // First step from stopped + _cn = _c0; + _direction = (distanceTo > 0) ? DIRECTION_CW : DIRECTION_CCW; + } + else + { + // Subsequent step. Works for accel (n is +_ve) and decel (n is -ve). + _cn = _cn - ((2.0 * _cn) / ((4.0 * _n) + 1)); // Equation 13 + _cn = max(_cn, _cmin); + } + _n++; + _stepInterval = _cn; + _speed = 1000000.0 / _cn; + if (_direction == DIRECTION_CCW) + _speed = -_speed; + +#if 0 + Serial.println(_speed); + Serial.println(_acceleration); + Serial.println(_cn); + Serial.println(_c0); + Serial.println(_n); + Serial.println(_stepInterval); + Serial.println(distanceTo); + Serial.println(stepsToStop); + Serial.println("-----"); +#endif +} + +// Run the motor to implement speed and acceleration in order to proceed to the target position +// You must call this at least once per step, preferably in your main loop +// If the motor is in the desired position, the cost is very small +// returns true if the motor is still running to the target position. +boolean AccelStepper::run() +{ + if (runSpeed()) + computeNewSpeed(); + return _speed != 0.0 || distanceToGo() != 0; +} + +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; + _acceleration = 0.0; + _sqrt_twoa = 1.0; + _stepInterval = 0; + _minPulseWidth = 1; + _enablePin = 0xff; + _lastStepTime = 0; + _pin[0] = pin1; + _pin[1] = pin2; + _pin[2] = pin3; + _pin[3] = pin4; + + // NEW + _n = 0; + _c0 = 0.0; + _cn = 0.0; + _cmin = 1.0; + _direction = DIRECTION_CCW; + + int i; + for (i = 0; i < 4; i++) + _pinInverted[i] = 0; + if (enable) + enableOutputs(); + // Some reasonable default + setAcceleration(1); +} + +AccelStepper::AccelStepper(void (*forward)(), void (*backward)()) +{ + _interface = 0; + _currentPos = 0; + _targetPos = 0; + _speed = 0.0; + _maxSpeed = 1.0; + _acceleration = 0.0; + _sqrt_twoa = 1.0; + _stepInterval = 0; + _minPulseWidth = 1; + _enablePin = 0xff; + _lastStepTime = 0; + _pin[0] = 0; + _pin[1] = 0; + _pin[2] = 0; + _pin[3] = 0; + _forward = forward; + _backward = backward; + + // NEW + _n = 0; + _c0 = 0.0; + _cn = 0.0; + _cmin = 1.0; + _direction = DIRECTION_CCW; + + int i; + for (i = 0; i < 4; i++) + _pinInverted[i] = 0; + // Some reasonable default + setAcceleration(1); +} + +void AccelStepper::setMaxSpeed(float speed) +{ + if (_maxSpeed != speed) + { + _maxSpeed = speed; + _cmin = 1000000.0 / speed; + // Recompute _n from current speed and adjust speed if accelerating or cruising + if (_n > 0) + { + _n = (long)((_speed * _speed) / (2.0 * _acceleration)); // Equation 16 + computeNewSpeed(); + } + } +} + +void AccelStepper::setAcceleration(float acceleration) +{ + if (acceleration == 0.0) + return; + if (_acceleration != acceleration) + { + // Recompute _n per Equation 17 + _n = _n * (_acceleration / acceleration); + // New c0 per Equation 7, with correction per Equation 15 + _c0 = 0.676 * sqrt(2.0 / acceleration) * 1000000.0; // Equation 15 + _acceleration = acceleration; + computeNewSpeed(); + } +} + +void AccelStepper::setSpeed(float speed) +{ + if (speed == _speed) + return; + speed = constrain(speed, -_maxSpeed, _maxSpeed); + if (speed == 0.0) + _stepInterval = 0; + else + { + _stepInterval = fabs(1000000.0 / speed); + _direction = (speed > 0.0) ? DIRECTION_CW : DIRECTION_CCW; + } + _speed = speed; +} + +float AccelStepper::speed() +{ + return _speed; +} + +// Subclasses can override +void AccelStepper::step(long step) +{ + switch (_interface) + { + case FUNCTION: + step0(step); + break; + + case DRIVER: + step1(step); + break; + + case FULL2WIRE: + step2(step); + break; + + case FULL3WIRE: + step3(step); + break; + + case FULL4WIRE: + step4(step); + break; + + case HALF3WIRE: + step6(step); + break; + + case HALF4WIRE: + step8(step); + break; + } +} + +// 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] +// .... +void AccelStepper::setOutputPins(uint8_t mask) +{ + uint8_t numpins = 2; + if (_interface == FULL4WIRE || _interface == HALF4WIRE) + numpins = 4; + else if (_interface == FULL3WIRE || _interface == HALF3WIRE) + numpins = 3; + uint8_t i; + for (i = 0; i < numpins; i++) + digitalWrite(_pin[i], (mask & (1 << i)) ? (HIGH ^ _pinInverted[i]) : (LOW ^ _pinInverted[i])); +} + +// 0 pin step function (ie for functional usage) +void AccelStepper::step0(long step) +{ + if (_speed > 0) + _forward(); + else + _backward(); +} + +// 1 pin step function (ie for stepper drivers) +// This is passed the current step number (0 to 7) +// Subclasses can override +void AccelStepper::step1(long step) +{ + // _pin[0] is step, _pin[1] is direction + setOutputPins(_direction ? 0b10 : 0b00); // Set direction first else get rogue pulses + setOutputPins(_direction ? 0b11 : 0b01); // step HIGH + // Caution 200ns setup time + // Delay the minimum allowed pulse width + delayMicroseconds(_minPulseWidth); + setOutputPins(_direction ? 0b10 : 0b00); // step LOW + +} + + +// 2 pin step function +// This is passed the current step number (0 to 7) +// Subclasses can override +void AccelStepper::step2(long step) +{ + switch (step & 0x3) + { + case 0: /* 01 */ + setOutputPins(0b10); + break; + + case 1: /* 11 */ + setOutputPins(0b11); + break; + + case 2: /* 10 */ + setOutputPins(0b01); + break; + + case 3: /* 00 */ + setOutputPins(0b00); + break; + } +} +// 3 pin step function +// This is passed the current step number (0 to 7) +// Subclasses can override +void AccelStepper::step3(long step) +{ + switch (step % 3) + { + case 0: // 100 + setOutputPins(0b100); + break; + + case 1: // 001 + setOutputPins(0b001); + break; + + case 2: //010 + setOutputPins(0b010); + break; + + } +} + +// 4 pin step function for half stepper +// This is passed the current step number (0 to 7) +// Subclasses can override +void AccelStepper::step4(long step) +{ + switch (step & 0x3) + { + case 0: // 1010 + setOutputPins(0b0101); + break; + + case 1: // 0110 + setOutputPins(0b0110); + break; + + case 2: //0101 + setOutputPins(0b1010); + break; + + case 3: //1001 + setOutputPins(0b1001); + break; + } +} + +// 3 pin half step function +// This is passed the current step number (0 to 7) +// Subclasses can override +void AccelStepper::step6(long step) +{ + switch (step % 6) + { + case 0: // 100 + setOutputPins(0b100); + break; + + case 1: // 101 + setOutputPins(0b101); + break; + + case 2: // 001 + setOutputPins(0b001); + break; + + case 3: // 011 + setOutputPins(0b011); + break; + + case 4: // 010 + setOutputPins(0b010); + break; + + case 5: // 011 + setOutputPins(0b110); + break; + + } +} + +// 4 pin half step function +// This is passed the current step number (0 to 7) +// Subclasses can override +void AccelStepper::step8(long step) +{ + switch (step & 0x7) + { + case 0: // 1000 + setOutputPins(0b0001); + break; + + case 1: // 1010 + setOutputPins(0b0101); + break; + + case 2: // 0010 + setOutputPins(0b0100); + break; + + case 3: // 0110 + setOutputPins(0b0110); + break; + + case 4: // 0100 + setOutputPins(0b0010); + break; + + case 5: //0101 + setOutputPins(0b1010); + break; + + case 6: // 0001 + setOutputPins(0b1000); + break; + + case 7: //1001 + setOutputPins(0b1001); + break; + } +} + +// Prevents power consumption on the outputs +void AccelStepper::disableOutputs() +{ + if (! _interface) return; + + setOutputPins(0); // Handles inversion automatically + if (_enablePin != 0xff) + digitalWrite(_enablePin, LOW ^ _enableInverted); +} + +void AccelStepper::enableOutputs() +{ + if (! _interface) + return; + + pinMode(_pin[0], OUTPUT); + pinMode(_pin[1], OUTPUT); + if (_interface == FULL4WIRE || _interface == HALF4WIRE) + { + pinMode(_pin[2], OUTPUT); + pinMode(_pin[3], OUTPUT); + } + else if (_interface == FULL3WIRE || _interface == HALF3WIRE) + { + pinMode(_pin[2], OUTPUT); + } + + if (_enablePin != 0xff) + { + pinMode(_enablePin, OUTPUT); + digitalWrite(_enablePin, HIGH ^ _enableInverted); + } +} + +void AccelStepper::setMinPulseWidth(unsigned int minWidth) +{ + _minPulseWidth = minWidth; +} + +void AccelStepper::setEnablePin(uint8_t enablePin) +{ + _enablePin = enablePin; + + // This happens after construction, so init pin now. + if (_enablePin != 0xff) + { + pinMode(_enablePin, OUTPUT); + digitalWrite(_enablePin, HIGH ^ _enableInverted); + } +} + +void AccelStepper::setPinsInverted(bool directionInvert, bool stepInvert, bool enableInvert) +{ + _pinInverted[0] = stepInvert; + _pinInverted[1] = directionInvert; + _enableInverted = enableInvert; +} + +void AccelStepper::setPinsInverted(bool pin1Invert, bool pin2Invert, bool pin3Invert, bool pin4Invert, bool enableInvert) +{ + _pinInverted[0] = pin1Invert; + _pinInverted[1] = pin2Invert; + _pinInverted[2] = pin3Invert; + _pinInverted[3] = pin4Invert; + _enableInverted = enableInvert; +} + +// Blocks until the target position is reached and stopped +void AccelStepper::runToPosition() +{ + while (run()) + ; +} + +boolean AccelStepper::runSpeedToPosition() +{ + if (_targetPos == _currentPos) + return false; + if (_targetPos >_currentPos) + _direction = DIRECTION_CW; + else + _direction = DIRECTION_CCW; + return runSpeed(); +} + +// Blocks until the new target position is reached +void AccelStepper::runToNewPosition(long position) +{ + moveTo(position); + runToPosition(); +} + +void AccelStepper::stop() +{ + if (_speed != 0.0) + { + long stepsToStop = (long)((_speed * _speed) / (2.0 * _acceleration)) + 1; // Equation 16 (+integer rounding) + if (_speed > 0) + move(stepsToStop); + else + move(-stepsToStop); + } +} diff --git a/Source/Arduino/EggDuino/AccelStepper.h b/Source/Arduino/EggDuino/AccelStepper.h new file mode 100644 index 0000000..26f9436 --- /dev/null +++ b/Source/Arduino/EggDuino/AccelStepper.h @@ -0,0 +1,666 @@ +// AccelStepper.h +// +/// \mainpage AccelStepper library for Arduino +/// +/// This is the Arduino AccelStepper library. +/// It provides an object-oriented interface for 2, 3 or 4 pin stepper motors. +/// +/// The standard Arduino IDE includes the Stepper library +/// (http://arduino.cc/en/Reference/Stepper) for stepper motors. It is +/// perfectly adequate for simple, single motor applications. +/// +/// AccelStepper significantly improves on the standard Arduino Stepper library in several ways: +/// \li Supports acceleration and deceleration +/// \li Supports multiple simultaneous steppers, with independent concurrent stepping on each stepper +/// \li API functions never delay() or block +/// \li Supports 2, 3 and 4 wire steppers, plus 3 and 4 wire half steppers. +/// \li Supports alternate stepping functions to enable support of AFMotor (https://github.com/adafruit/Adafruit-Motor-Shield-library) +/// \li Supports stepper drivers such as the Sparkfun EasyDriver (based on 3967 driver chip) +/// \li Very slow speeds are supported +/// \li Extensive API +/// \li Subclass support +/// +/// 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.47.zip +/// +/// Example Arduino programs are included to show the main modes of use. +/// +/// You can also find online help and discussion at http://groups.google.com/group/accelstepper +/// Please use that group for all questions and discussions on this topic. +/// Do not contact the author directly, unless it is to discuss commercial licensing. +/// Before asking a question or reporting a bug, please read http://www.catb.org/esr/faqs/smart-questions.html +/// +/// 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. +/// Tested on Teensy http://www.pjrc.com/teensy including Teensy 3.1 built using Arduino IDE 1.0.5 with +/// teensyduino addon 1.18 and later. +/// +/// \par Installation +/// +/// Install in the usual way: unzip the distribution zip file to the libraries +/// sub-folder of your sketchbook. +/// +/// \par Theory +/// +/// This code uses speed calculations as described in +/// "Generate stepper-motor speed profiles in real time" by David Austin +/// http://fab.cba.mit.edu/classes/MIT/961.09/projects/i0/Stepper_Motor_Speed_Profile.pdf +/// with the exception that AccelStepper uses steps per second rather than radians per second +/// (because we dont know the step angle of the motor) +/// An initial step interval is calculated for the first step, based on the desired acceleration +/// On subsequent steps, shorter step intervals are calculated based +/// on the previous step until max speed is achieved. +/// +/// \par Donations +/// +/// This library is offered under a free GPL license for those who want to use it that way. +/// We try hard to keep it up to date, fix bugs +/// and to provide free support. If this library has helped you save time or money, please consider donating at +/// http://www.airspayce.com or here: +/// +/// \htmlonly
\endhtmlonly +/// +/// \par Trademarks +/// +/// AccelStepper is a trademark of AirSpayce Pty Ltd. The AccelStepper mark was first used on April 26 2010 for +/// international trade, and is used only in relation to motor control hardware and software. +/// It is not to be confused with any other similar marks covering other goods and services. +/// +/// \par Copyright +/// +/// This software is Copyright (C) 2010 Mike McCauley. Use is subject to license +/// conditions. The main licensing options available are GPL V2 or Commercial: +/// +/// \par Open Source Licensing GPL V2 +/// This is the appropriate option if you want to share the source code of your +/// application with everyone you distribute it to, and you also want to give them +/// the right to share who uses it. If you wish to use this software under Open +/// Source Licensing, you must contribute all your source code to the open source +/// community in accordance with the GPL Version 2 when your application is +/// distributed. See http://www.gnu.org/copyleft/gpl.html +/// +/// \par Commercial Licensing +/// This is the appropriate option if you are creating proprietary applications +/// and you are not prepared to distribute and share the source code of your +/// application. Contact info@airspayce.com for details. +/// +/// \par Revision History +/// \version 1.0 Initial release +/// +/// \version 1.1 Added speed() function to get the current speed. +/// \version 1.2 Added runSpeedToPosition() submitted by Gunnar Arndt. +/// \version 1.3 Added support for stepper drivers (ie with Step and Direction inputs) with _pins == 1 +/// \version 1.4 Added functional contructor to support AFMotor, contributed by Limor, with example sketches. +/// \version 1.5 Improvements contributed by Peter Mousley: Use of microsecond steps and other speed improvements +/// to increase max stepping speed to about 4kHz. New option for user to set the min allowed pulse width. +/// Added checks for already running at max speed and skip further calcs if so. +/// \version 1.6 Fixed a problem with wrapping of microsecond stepping that could cause stepping to hang. +/// Reported by Sandy Noble. +/// Removed redundant _lastRunTime member. +/// \version 1.7 Fixed a bug where setCurrentPosition() did not always work as expected. +/// Reported by Peter Linhart. +/// \version 1.8 Added support for 4 pin half-steppers, requested by Harvey Moon +/// \version 1.9 setCurrentPosition() now also sets motor speed to 0. +/// \version 1.10 Builds on Arduino 1.0 +/// \version 1.11 Improvments from Michael Ellison: +/// Added optional enable line support for stepper drivers +/// Added inversion for step/direction/enable lines for stepper drivers +/// \version 1.12 Announce Google Group +/// \version 1.13 Improvements to speed calculation. Cost of calculation is now less in the worst case, +/// and more or less constant in all cases. This should result in slightly beter high speed performance, and +/// reduce anomalous speed glitches when other steppers are accelerating. +/// However, its hard to see how to replace the sqrt() required at the very first step from 0 speed. +/// \version 1.14 Fixed a problem with compiling under arduino 0021 reported by EmbeddedMan +/// \version 1.15 Fixed a problem with runSpeedToPosition which did not correctly handle +/// running backwards to a smaller target position. Added examples +/// \version 1.16 Fixed some cases in the code where abs() was used instead of fabs(). +/// \version 1.17 Added example ProportionalControl +/// \version 1.18 Fixed a problem: If one calls the funcion runSpeed() when Speed is zero, it makes steps +/// without counting. reported by Friedrich, Klappenbach. +/// \version 1.19 Added MotorInterfaceType and symbolic names for the number of pins to use +/// for the motor interface. Updated examples to suit. +/// Replaced individual pin assignment variables _pin1, _pin2 etc with array _pin[4]. +/// _pins member changed to _interface. +/// Added _pinInverted array to simplify pin inversion operations. +/// Added new function setOutputPins() which sets the motor output pins. +/// It can be overridden in order to provide, say, serial output instead of parallel output +/// Some refactoring and code size reduction. +/// \version 1.20 Improved documentation and examples to show need for correctly +/// specifying AccelStepper::FULL4WIRE and friends. +/// \version 1.21 Fixed a problem where desiredSpeed could compute the wrong step acceleration +/// when _speed was small but non-zero. Reported by Brian Schmalz. +/// Precompute sqrt_twoa to improve performance and max possible stepping speed +/// \version 1.22 Added Bounce.pde example +/// Fixed a problem where calling moveTo(), setMaxSpeed(), setAcceleration() more +/// frequently than the step time, even +/// with the same values, would interfere with speed calcs. Now a new speed is computed +/// only if there was a change in the set value. Reported by Brian Schmalz. +/// \version 1.23 Rewrite of the speed algorithms in line with +/// http://fab.cba.mit.edu/classes/MIT/961.09/projects/i0/Stepper_Motor_Speed_Profile.pdf +/// Now expect smoother and more linear accelerations and decelerations. The desiredSpeed() +/// function was removed. +/// \version 1.24 Fixed a problem introduced in 1.23: with runToPosition, which did never returned +/// \version 1.25 Now ignore attempts to set acceleration to 0.0 +/// \version 1.26 Fixed a problem where certina combinations of speed and accelration could cause +/// oscillation about the target position. +/// \version 1.27 Added stop() function to stop as fast as possible with current acceleration parameters. +/// Also added new Quickstop example showing its use. +/// \version 1.28 Fixed another problem where certain combinations of speed and accelration could cause +/// oscillation about the target position. +/// Added support for 3 wire full and half steppers such as Hard Disk Drive spindle. +/// Contributed by Yuri Ivatchkovitch. +/// \version 1.29 Fixed a problem that could cause a DRIVER stepper to continually step +/// with some sketches. Reported by Vadim. +/// \version 1.30 Fixed a problem that could cause stepper to back up a few steps at the end of +/// accelerated travel with certain speeds. Reported and patched by jolo. +/// \version 1.31 Updated author and distribution location details to airspayce.com +/// \version 1.32 Fixed a problem with enableOutputs() and setEnablePin on Arduino Due that +/// prevented the enable pin changing stae correctly. Reported by Duane Bishop. +/// \version 1.33 Fixed an error in example AFMotor_ConstantSpeed.pde did not setMaxSpeed(); +/// Fixed a problem that caused incorrect pin sequencing of FULL3WIRE and HALF3WIRE. +/// Unfortunately this meant changing the signature for all step*() functions. +/// Added example MotorShield, showing how to use AdaFruit Motor Shield to control +/// a 3 phase motor such as a HDD spindle motor (and without using the AFMotor library. +/// \version 1.34 Added setPinsInverted(bool pin1Invert, bool pin2Invert, bool pin3Invert, bool pin4Invert, bool enableInvert) +/// to allow inversion of 2, 3 and 4 wire stepper pins. Requested by Oleg. +/// \version 1.35 Removed default args from setPinsInverted(bool, bool, bool, bool, bool) to prevent ambiguity with +/// setPinsInverted(bool, bool, bool). Reported by Mac Mac. +/// \version 1.36 Changed enableOutputs() and disableOutputs() to be virtual so can be overridden. +/// Added new optional argument 'enable' to constructor, which allows you toi disable the +/// automatic enabling of outputs at construction time. Suggested by Guido. +/// \version 1.37 Fixed a problem with step1 that could cause a rogue step in the +/// wrong direction (or not, +/// depending on the setup-time requirements of the connected hardware). +/// Reported by Mark Tillotson. +/// \version 1.38 run() function incorrectly always returned true. Updated function and doc so it returns true +/// if the motor is still running to the target position. +/// \version 1.39 Updated typos in keywords.txt, courtesey Jon Magill. +/// \version 1.40 Updated documentation, including testing on Teensy 3.1 +/// \version 1.41 Fixed an error in the acceleration calculations, resulting in acceleration of haldf the intended value +/// \version 1.42 Improved support for FULL3WIRE and HALF3WIRE output pins. These changes were in Yuri's original +/// contribution but did not make it into production.
+/// \version 1.43 Added DualMotorShield example. Shows how to use AccelStepper to control 2 x 2 phase steppers using the +/// Itead Studio Arduino Dual Stepper Motor Driver Shield model IM120417015.
+/// \version 1.44 examples/DualMotorShield/DualMotorShield.ino examples/DualMotorShield/DualMotorShield.pde +/// was missing from the distribution.
+/// \version 1.45 Fixed a problem where if setAcceleration was not called, there was no default +/// acceleration. Reported by Michael Newman.
+/// \version 1.45 Fixed inaccuracy in acceleration rate by using Equation 15, suggested by Sebastian Gracki.
+/// Performance improvements in runSpeed suggested by Jaakko Fagerlund.
+/// \version 1.46 Fixed error in documentation for runToPosition(). +/// Reinstated time calculations in runSpeed() since new version is reported +/// not to work correctly under some circumstances. Reported by Oleg V Gavva.
+ +/// +/// \author Mike McCauley (mikem@airspayce.com) DO NOT CONTACT THE AUTHOR DIRECTLY: USE THE LISTS +// Copyright (C) 2009-2013 Mike McCauley +// $Id: AccelStepper.h,v 1.21 2014/10/31 06:05:30 mikem Exp mikem $ + +#ifndef AccelStepper_h +#define AccelStepper_h + +#include +#if ARDUINO >= 100 +#include +#else +#include +#include +#endif + +// These defs cause trouble on some versions of Arduino +#undef round + +///////////////////////////////////////////////////////////////////// +/// \class AccelStepper AccelStepper.h +/// \brief Support for stepper motors with acceleration etc. +/// +/// This defines a single 2 or 4 pin stepper motor, or stepper moter with fdriver chip, with optional +/// acceleration, deceleration, absolute positioning commands etc. Multiple +/// simultaneous steppers are supported, all moving +/// at different speeds and accelerations. +/// +/// \par Operation +/// This module operates by computing a step time in microseconds. The step +/// time is recomputed after each step and after speed and acceleration +/// parameters are changed by the caller. The time of each step is recorded in +/// microseconds. The run() function steps the motor once if a new step is due. +/// The run() function must be called frequently until the motor is in the +/// desired position, after which time run() will do nothing. +/// +/// \par Positioning +/// Positions are specified by a signed long integer. At +/// construction time, the current position of the motor is consider to be 0. Positive +/// positions are clockwise from the initial position; negative positions are +/// anticlockwise. The current position can be altered for instance after +/// initialization positioning. +/// +/// \par Caveats +/// This is an open loop controller: If the motor stalls or is oversped, +/// AccelStepper will not have a correct +/// idea of where the motor really is (since there is no feedback of the motor's +/// real position. We only know where we _think_ it is, relative to the +/// initial starting point). +/// +/// \par Performance +/// The fastest motor speed that can be reliably supported is about 4000 steps per +/// second at a clock frequency of 16 MHz on Arduino such as Uno etc. +/// Faster processors can support faster stepping speeds. +/// However, any speed less than that +/// down to very slow speeds (much less than one per second) are also supported, +/// provided the run() function is called frequently enough to step the motor +/// whenever required for the speed set. +/// Calling setAcceleration() is expensive, +/// since it requires a square root to be calculated. +class AccelStepper +{ +public: + /// \brief Symbolic names for number of pins. + /// Use this in the pins argument the AccelStepper constructor to + /// provide a symbolic name for the number of pins + /// to use. + typedef enum + { + FUNCTION = 0, ///< Use the functional interface, implementing your own driver functions (internal use only) + DRIVER = 1, ///< Stepper Driver, 2 driver pins required + FULL2WIRE = 2, ///< 2 wire stepper, 2 motor pins required + FULL3WIRE = 3, ///< 3 wire stepper, such as HDD spindle, 3 motor pins required + FULL4WIRE = 4, ///< 4 wire full stepper, 4 motor pins required + HALF3WIRE = 6, ///< 3 wire half stepper, such as HDD spindle, 3 motor pins required + HALF4WIRE = 8 ///< 4 wire half stepper, 4 motor pins required + } MotorInterfaceType; + + /// Constructor. You can have multiple simultaneous steppers, all moving + /// at different speeds and accelerations, provided you call their run() + /// functions at frequent enough intervals. Current Position is set to 0, target + /// position is set to 0. MaxSpeed and Acceleration default to 1.0. + /// The motor pins will be initialised to OUTPUT mode during the + /// constructor by a call to enableOutputs(). + /// \param[in] interface Number of pins to interface to. 1, 2, 4 or 8 are + /// supported, but it is preferred to use the \ref MotorInterfaceType symbolic names. + /// AccelStepper::DRIVER (1) means a stepper driver (with Step and Direction pins). + /// If an enable line is also needed, call setEnablePin() after construction. + /// You may also invert the pins using setPinsInverted(). + /// AccelStepper::FULL2WIRE (2) means a 2 wire stepper (2 pins required). + /// AccelStepper::FULL3WIRE (3) means a 3 wire stepper, such as HDD spindle (3 pins required). + /// AccelStepper::FULL4WIRE (4) means a 4 wire stepper (4 pins required). + /// AccelStepper::HALF3WIRE (6) means a 3 wire half stepper, such as HDD spindle (3 pins required) + /// AccelStepper::HALF4WIRE (8) means a 4 wire half stepper (4 pins required) + /// Defaults to AccelStepper::FULL4WIRE (4) pins. + /// \param[in] pin1 Arduino digital pin number for motor pin 1. Defaults + /// to pin 2. For a AccelStepper::DRIVER (interface==1), + /// this is the Step input to the driver. Low to high transition means to step) + /// \param[in] pin2 Arduino digital pin number for motor pin 2. Defaults + /// to pin 3. For a AccelStepper::DRIVER (interface==1), + /// this is the Direction input the driver. High means forward. + /// \param[in] pin3 Arduino digital pin number for motor pin 3. Defaults + /// to pin 4. + /// \param[in] pin4 Arduino digital pin number for motor pin 4. Defaults + /// to pin 5. + /// \param[in] enable If this is true (the default), enableOutputs() will be called to enable + /// the output pins at construction time. + 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 + /// at different speeds and accelerations, provided you call their run() + /// functions at frequent enough intervals. Current Position is set to 0, target + /// position is set to 0. MaxSpeed and Acceleration default to 1.0. + /// Any motor initialization should happen before hand, no pins are used or initialized. + /// \param[in] forward void-returning procedure that will make a forward step + /// \param[in] backward void-returning procedure that will make a backward step + AccelStepper(void (*forward)(), void (*backward)()); + + /// Set the target position. The run() function will try to move the motor (at most one step per call) + /// from the current position to the target position set by the most + /// recent call to this function. Caution: moveTo() also recalculates the speed for the next step. + /// If you are trying to use constant speed movements, you should call setSpeed() after calling moveTo(). + /// \param[in] absolute The desired absolute position. Negative is + /// anticlockwise from the 0 position. + void moveTo(long absolute); + + /// Set the target position relative to the current position + /// \param[in] relative The desired position relative to the current position. Negative is + /// anticlockwise from the current position. + void move(long relative); + + /// Poll the motor and step it if a step is due, implementing + /// accelerations and decelerations to acheive the target position. You must call this as + /// frequently as possible, but at least once per minimum step time interval, + /// preferably in your main loop. Note that each call to run() will make at most one step, and then only when a step is due, + /// based on the current speed and the time since the last step. + /// \return true if the motor is still running to the target position. + boolean run(); + + /// Poll the motor and step it if a step is due, implementing a constant + /// speed as set by the most recent call to setSpeed(). You must call this as + /// frequently as possible, but at least once per step interval, + /// \return true if the motor was stepped. + boolean runSpeed(); + + /// Sets the maximum permitted speed. The run() function will accelerate + /// up to the speed set by this function. + /// Caution: the maximum speed achievable depends on your processor and clock speed. + /// \param[in] speed The desired maximum speed in steps per second. Must + /// be > 0. Caution: Speeds that exceed the maximum speed supported by the processor may + /// Result in non-linear accelerations and decelerations. + void setMaxSpeed(float speed); + + /// Sets the acceleration/deceleration rate. + /// \param[in] acceleration The desired acceleration in steps per second + /// per second. Must be > 0.0. This is an expensive call since it requires a square + /// root to be calculated. Dont call more ofthen than needed + void setAcceleration(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 + /// second are unreliable. Very slow speeds may be set (eg 0.00027777 for + /// once per hour, approximately. Speed accuracy depends on the Arduino + /// crystal. Jitter depends on how frequently you call the runSpeed() function. + void setSpeed(float speed); + + /// The most recently set speed + /// \return the most recent speed in steps per second + float speed(); + + /// The distance from the current position to the target position. + /// \return the distance from the current position to the target position + /// in steps. Positive is clockwise from the current position. + long distanceToGo(); + + /// The most recently set target position. + /// \return the target position + /// in steps. Positive is clockwise from the 0 position. + long targetPosition(); + + /// The currently motor position. + /// \return the current motor position + /// in steps. Positive is clockwise from the 0 position. + long currentPosition(); + + /// Resets the current position of the motor, so that wherever the motor + /// happens to be right now is considered to be the new 0 position. Useful + /// for setting a zero position on a stepper after an initial hardware + /// positioning move. + /// Has the side effect of setting the current motor speed to 0. + /// \param[in] position The position in steps of wherever the motor + /// happens to be right now. + void setCurrentPosition(long position); + + /// Moves the motor (with acceleration/deceleration) + /// to the target position and blocks until it is at + /// position. Dont use this in event loops, since it blocks. + void runToPosition(); + + /// Runs at the currently selected speed until the target position is reached + /// Does not implement accelerations. + /// \return true if it stepped + boolean runSpeedToPosition(); + + /// Moves the motor (with acceleration/deceleration) + /// to the new target position and blocks until it is at + /// position. Dont use this in event loops, since it blocks. + /// \param[in] position The new target position. + void runToNewPosition(long position); + + /// Sets a new target position that causes the stepper + /// to stop as quickly as possible, using the current speed and acceleration parameters. + void stop(); + + /// Disable motor pin outputs by setting them all LOW + /// Depending on the design of your electronics this may turn off + /// the power to the motor coils, saving power. + /// This is useful to support Arduino low power modes: disable the outputs + /// during sleep and then reenable with enableOutputs() before stepping + /// again. + virtual void disableOutputs(); + + /// Enable motor pin outputs by setting the motor pins to OUTPUT + /// mode. Called automatically by the constructor. + virtual void enableOutputs(); + + /// Sets the minimum pulse width allowed by the stepper driver. The minimum practical pulse width is + /// approximately 20 microseconds. Times less than 20 microseconds + /// will usually result in 20 microseconds or so. + /// \param[in] minWidth The minimum pulse width in microseconds. + void setMinPulseWidth(unsigned int minWidth); + + /// Sets the enable pin number for stepper drivers. + /// 0xFF indicates unused (default). + /// Otherwise, if a pin is set, the pin will be turned on when + /// enableOutputs() is called and switched off when disableOutputs() + /// is called. + /// \param[in] enablePin Arduino digital pin number for motor enable + /// \sa setPinsInverted + void setEnablePin(uint8_t enablePin = 0xff); + + /// Sets the inversion for stepper driver pins + /// \param[in] directionInvert True for inverted direction pin, false for non-inverted + /// \param[in] stepInvert True for inverted step pin, false for non-inverted + /// \param[in] enableInvert True for inverted enable pin, false (default) for non-inverted + void setPinsInverted(bool directionInvert = false, bool stepInvert = false, bool enableInvert = false); + + /// Sets the inversion for 2, 3 and 4 wire stepper pins + /// \param[in] pin1Invert True for inverted pin1, false for non-inverted + /// \param[in] pin2Invert True for inverted pin2, false for non-inverted + /// \param[in] pin3Invert True for inverted pin3, false for non-inverted + /// \param[in] pin4Invert True for inverted pin4, false for non-inverted + /// \param[in] enableInvert True for inverted enable pin, false (default) for non-inverted + void setPinsInverted(bool pin1Invert, bool pin2Invert, bool pin3Invert, bool pin4Invert, bool enableInvert); + +protected: + + /// \brief Direction indicator + /// Symbolic names for the direction the motor is turning + typedef enum + { + DIRECTION_CCW = 0, ///< Clockwise + DIRECTION_CW = 1 ///< Counter-Clockwise + } Direction; + + /// Forces the library to compute a new instantaneous speed and set that as + /// the current speed. It is called by + /// the library: + /// \li after each step + /// \li after change to maxSpeed through setMaxSpeed() + /// \li after change to acceleration through setAcceleration() + /// \li after change to target position (relative or absolute) through + /// move() or moveTo() + void computeNewSpeed(); + + /// Low level function to set the motor output pins + /// bit 0 of the mask corresponds to _pin[0] + /// bit 1 of the mask corresponds to _pin[1] + /// You can override this to impment, for example serial chip output insted of using the + /// output pins directly + virtual void setOutputPins(uint8_t mask); + + /// Called to execute a step. Only called when a new step is + /// required. Subclasses may override to implement new stepping + /// interfaces. The default calls step1(), step2(), step4() or step8() depending on the + /// 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 step using stepper functions (pins = 0) Only called when a new step is + /// required. Calls _forward() or _backward() to perform the step + /// \param[in] step The current step phase number (0 to 7) + virtual void step0(long step); + + /// Called to execute a step on a stepper driver (ie where pins == 1). Only called when a new step is + /// required. Subclasses may override to implement new stepping + /// interfaces. The default sets or clears the outputs of Step pin1 to step, + /// and sets the output of _pin2 to the desired direction. The Step pin (_pin1) is pulsed for 1 microsecond + /// which is the minimum STEP pulse width for the 3967 driver. + /// \param[in] step The current step phase number (0 to 7) + virtual void step1(long step); + + /// Called to execute a step on a 2 pin 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 and pin2 + /// \param[in] step The current step phase number (0 to 7) + virtual void step2(long step); + + /// Called to execute a step on a 3 pin motor, such as HDD spindle. 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 + /// \param[in] step The current step phase number (0 to 7) + virtual void step3(long step); + + /// Called to execute a step on a 4 pin 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. + /// \param[in] step The current step phase number (0 to 7) + virtual void step4(long step); + + /// Called to execute a step on a 3 pin motor, such as HDD spindle. 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 + /// \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 + /// required. Subclasses may override to implement new stepping + /// interfaces. The default sets or clears the outputs of pin1, pin2, + /// pin3, pin4. + /// \param[in] step The current step phase number (0 to 7) + virtual void step8(long step); + +private: + /// Number of pins on the stepper motor. Permits 2 or 4. 2 pins is a + /// bipolar, and 4 pins is a unipolar. + uint8_t _interface; // 0, 1, 2, 4, 8, See MotorInterfaceType + + /// Arduino pin number assignments for the 2 or 4 pins required to interface to the + /// stepper motor or driver + uint8_t _pin[4]; + + /// Whether the _pins is inverted or not + uint8_t _pinInverted[4]; + + /// The current absolution position in steps. + long _currentPos; // Steps + + /// The target position in steps. The AccelStepper library will move the + /// motor from the _currentPos to the _targetPos, taking into account the + /// max speed, acceleration and deceleration + long _targetPos; // Steps + + /// The current motos speed in steps per second + /// Positive is clockwise + float _speed; // Steps per second + + /// The maximum permitted speed in steps per second. Must be > 0. + float _maxSpeed; + + /// The acceleration to use to accelerate or decelerate the motor in steps + /// per second per second. Must be > 0 + 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; + + /// The minimum allowed pulse width in microseconds + unsigned int _minPulseWidth; + + /// Is the direction pin inverted? + ///bool _dirInverted; /// Moved to _pinInverted[1] + + /// Is the step pin inverted? + ///bool _stepInverted; /// Moved to _pinInverted[0] + + /// Is the enable pin inverted? + bool _enableInverted; + + /// Enable pin for stepper driver, or 0xFF if unused. + uint8_t _enablePin; + + /// The pointer to a forward-step procedure + void (*_forward)(); + + /// The pointer to a backward-step procedure + void (*_backward)(); + + /// The step counter for speed calculations + long _n; + + /// Initial step size in microseconds + float _c0; + + /// Last step size in microseconds + float _cn; + + /// Min step size in microseconds based on maxSpeed + float _cmin; // at max speed + + /// Current direction motor is spinning in + boolean _direction; // 1 == CW + +}; + +/// @example Random.pde +/// Make a single stepper perform random changes in speed, position and acceleration + +/// @example Overshoot.pde +/// 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 + +/// @example MultiStepper.pde +/// 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 + +/// @example ConstantSpeed.pde +/// Shows how to run AccelStepper in the simplest, +/// fixed speed mode with no accelerations + +/// @example Blocking.pde +/// Shows how to use the blocking call runToNewPosition +/// Which sets a new target position and then waits until the stepper has +/// achieved it. + +/// @example AFMotor_MultiStepper.pde +/// Control both Stepper motors at the same time with different speeds +/// and accelerations. + +/// @example AFMotor_ConstantSpeed.pde +/// Shows how to run AccelStepper in the simplest, +/// fixed speed mode with no accelerations + +/// @example ProportionalControl.pde +/// 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. + +/// @example Bounce.pde +/// Make a single stepper bounce from one limit to another, observing +/// accelrations at each end of travel + +/// @example Quickstop.pde +/// 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. + +/// @example MotorShield.pde +/// 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. + +/// @example DualMotorShield.pde +/// Shows how to use AccelStepper to control 2 x 2 phase steppers using the +/// Itead Studio Arduino Dual Stepper Motor Driver Shield +/// model IM120417015 + +#endif diff --git a/Source/Arduino/EggDuino/EggDuino.ino b/Source/Arduino/EggDuino/EggDuino.ino new file mode 100644 index 0000000..eb3506e --- /dev/null +++ b/Source/Arduino/EggDuino/EggDuino.ino @@ -0,0 +1,187 @@ +/* Eggduino-Firmware by Joachim Cerny, 2014 + + Thanks for the nice libs ACCELSTEPPER and SERIALCOMMAND, which made this project much easier. + Thanks to the Eggbot-Team for such a funny and enjoable concept! + Thanks to my wife and my daughter for their patience. :-) + + */ + +// implemented Eggbot-Protocol-Version v13 +// EBB-Command-Reference, I sourced from: http://www.schmalzhaus.com/EBB/EBBCommands.html +// no homing sequence, switch-on position of pen will be taken as reference point. +// No collision-detection!! +// Supported Servos: I do not know, I use Arduino Servo Lib with TG9e- standard servo. +// Note: Maximum-Speed in Inkscape is 1000 Steps/s. You could enter more, but then Pythonscript sends nonsense. +// EBB-Coordinates are coming in for 16th-Microstepmode. The Coordinate-Transforms are done in weired integer-math. Be careful, when you diecide to modify settings. + +/* TODOs: + 1 collision control via penMin/penMax + 2 implement homing sequence via microswitch or optical device + */ + +#include "AccelStepper.h" // nice lib from http://www.airspayce.com/mikem/arduino/AccelStepper/ +#include "VarSpeedServo.h" // variable speed servo lib https://github.com/netlabtoolkit/VarSpeedServo +#include "SerialCommand.h" //nice lib from Stefan Rado, https://github.com/kroimon/Arduino-SerialCommand +#include +#include "button.h" +#include + + +#define initSting "EBBv13_and_above Protocol emulated by Eggduino-Firmware V1.x" + +//#define BOARD_ULN2003 +//#define BOARD_ZAGGO +#define BOARD_CNCSHIELD + +#ifdef BOARD_ULN2003 + // Mini Spherebot using 28BYJ-48 Steppers with ULN2003 Drivers + // http://www.thingiverse.com/thing:1461709 + #define rotMicrostep 16 + #define penMicrostep 16 + #define servoPin 13 + #define engraverPin 12 + + // These values work for my 28BYJ-48's, Your's might + // be different and may need adjustment. + + #define rotStepsPerRev 4096 + #define penStepsUseable 1100 + + //Buttons (uncomment to enable) + //#define prgButton 2 // PRG button + //#define penToggleButton 12 // pen up/down button + //#define motorsButton 4 // motors enable button +#endif + +#ifdef BOARD_ZAGGO + //Zaggo SphereBot design: http://pleasantsoftware.com/developer/3d/spherebot/ + //Rotational Stepper: + #define step1 11 + #define dir1 10 + #define enableRotMotor 9 + #define rotMicrostep 16 //MicrostepMode, only 1,2,4,8,16 allowed, because of Integer-Math in this Sketch + //Pen Stepper: + #define step2 8 + #define dir2 7 + #define enablePenMotor 6 + #define penMicrostep 16 //MicrostepMode, only 1,2,4,8,16 allowed, because of Integer-Math in this Sketch + //Servo + #define servoPin 3 + #define engraverPin 5 + //Buttons (uncomment to enable) + //#define prgButton 2 // PRG button + //#define penToggleButton 12 // pen up/down button + //#define motorsButton 4 // motors enable button +#endif + +#ifdef BOARD_CNCSHIELD + //CNC Shield: http://blog.protoneer.co.nz/arduino-cnc-shield/ + //Rotational Stepper: ("X") + #define step1 2 + #define dir1 5 + #define enableRotMotor 8 + #define rotMicrostep 16 //MicrostepMode, only 1,2,4,8,16 allowed, because of Integer-Math in this Sketch + //Pen Stepper: ("Y") + #define step2 3 + #define dir2 6 + #define enablePenMotor 8 + #define penMicrostep 16 //MicrostepMode, only 1,2,4,8,16 allowed, because of Integer-Math in this Sketch + //Servo + #define servoPin 12 // "SpnEn" + #define engraverPin 13 // "SpnDir" + //Buttons + //#define prgButton A0 // PRG button ("Abort") + //#define penToggleButton A1 // pen up/down button ("Hold") + //#define motorsButton A2 // motors enable button ("Resume") +#endif + + +//----------------------------------------------------------------------------------------------------------- + + +#define penUpPosEEAddress ((uint16_t *)0) +#define penDownPosEEAddress ((uint16_t *)2) +#define penUpRateEEAddress ((uint16_t *)4) +#define penDownRateEEAddress ((uint16_t *)6) + +void setprgButtonState(); +void doTogglePen(); +void toggleMotors(); +void makeComInterface(); +void initHardware(); +void moveOneStep(); + + + +//make Objects +#ifdef BOARD_ULN2003 +AccelStepper rotMotor(AccelStepper::HALF4WIRE, 2,4,3,5, true); +AccelStepper penMotor(AccelStepper::HALF4WIRE, 8,10,9,11, true); +//AccelStepper penMotor(AccelStepper::HALF4WIRE, 2,4,3,5, true); +//AccelStepper rotMotor(AccelStepper::HALF4WIRE, 8,10,9,11, true); +#else +AccelStepper rotMotor(1, step1, dir1); +AccelStepper penMotor(1, step2, dir2); +#endif + +VarSpeedServo penServo; +SerialCommand SCmd; +//create Buttons +#ifdef prgButton + Button prgButtonToggle(prgButton, setprgButtonState); +#endif +#ifdef penToggleButton + Button penToggle(penToggleButton, doTogglePen); +#endif +#ifdef motorsButton + Button motorsToggle(motorsButton, toggleMotors); +#endif +// Variables... be careful, by messing around here, everything has a reason and crossrelations... +int penMin=0; +int penMax=0; +int penUpPos=5; //can be overwritten from EBB-Command SC +int penDownPos=20; //can be overwritten from EBB-Command SC +int servoRateUp=0; +int servoRateDown=0; +long rotStepError=0; +long penStepError=0; +int penState=penUpPos; +uint32_t nodeCount=0; +unsigned int layer=0; +boolean prgButtonState=0; +uint8_t rotStepCorrection = 16/rotMicrostep ; //devide EBB-Coordinates by this factor to get EGGduino-Steps +uint8_t penStepCorrection = 16/penMicrostep ; //devide EBB-Coordinates by this factor to get EGGduino-Steps +float rotSpeed=0; +float penSpeed=0; // these are local variables for Function SteppermotorMove-Command, but for performance-reasons it will be initialized here +boolean motorsEnabled = 0; +word sBMP = 0; +boolean isBMP = false; +//float rotScale = (float)rotStepsPerRev / 3200.0; +//float penScale = (float)penStepsUseable / 800.0; + +InkShieldA0A3 MyInkShield(11); +int countStep=0; + +void setup() { + Serial.begin(115120); + makeComInterface(); + initHardware(); +} + +void loop() { + moveOneStep(); + + SCmd.readSerial(); + +#ifdef penToggleButton + penToggle.check(); +#endif + +#ifdef motorsButton + motorsToggle.check(); +#endif + +#ifdef prgButton + prgButtonToggle.check(); +#endif +} diff --git a/Source/Arduino/EggDuino/Functions.ino b/Source/Arduino/EggDuino/Functions.ino new file mode 100644 index 0000000..82018e5 --- /dev/null +++ b/Source/Arduino/EggDuino/Functions.ino @@ -0,0 +1,331 @@ + +void makeComInterface(){ + SCmd.addCommand("v",sendVersion); + SCmd.addCommand("EM",enableMotors); + SCmd.addCommand("SC",stepperModeConfigure); + SCmd.addCommand("SP",setPen); + SCmd.addCommand("SM",stepperMove); + SCmd.addCommand("SB",stepperBitmapMove); + SCmd.addCommand("SE",setEngraver); + SCmd.addCommand("TP",togglePen); + SCmd.addCommand("PD",ignore); + SCmd.addCommand("PO",pinOutput); + SCmd.addCommand("NI",nodeCountIncrement); + SCmd.addCommand("ND",nodeCountDecrement); + SCmd.addCommand("SN",setNodeCount); + SCmd.addCommand("QN",queryNodeCount); + SCmd.addCommand("SL",setLayer); + SCmd.addCommand("QL",queryLayer); + SCmd.addCommand("QP",queryPen); + SCmd.addCommand("QB",queryButton); //"PRG" Button, + SCmd.setDefaultHandler(unrecognized); // Handler for command that isn't matched (says "What?") +} + +void queryPen() { + char state; + if (penState==penUpPos) + state='0'; + else + state='1'; + Serial.print(String(state)+"\r\n"); + sendAck(); +} + +void queryButton() { + Serial.print(String(prgButtonState) +"\r\n"); + sendAck(); + prgButtonState = 0; +} + +void queryLayer() { + Serial.print(String(layer) +"\r\n"); + sendAck(); +} + +void setLayer() { + uint32_t value=0; + char *arg1; + arg1 = SCmd.next(); + if (arg1 != NULL) { + value = atoi(arg1); + layer=value; + sendAck(); + } + else + sendError(); +} + +void queryNodeCount() { + Serial.print(String(nodeCount) +"\r\n"); + sendAck(); + +} + +void setNodeCount() { + uint32_t value=0; + char *arg1; + arg1 = SCmd.next(); + if (arg1 != NULL) { + value = atoi(arg1); + nodeCount=value; + sendAck(); + } + else + sendError(); +} + +void nodeCountIncrement() { + nodeCount=nodeCount++; + sendAck(); +} + +void nodeCountDecrement() { + nodeCount=nodeCount--; + sendAck(); +} + +void stepperMove() { + uint16_t duration=0; //in ms + int penStepsEBB=0; //Pen + int rotStepsEBB=0; //Rot + + moveToDestination(); + + if (!parseSMArgs(&duration, &penStepsEBB, &rotStepsEBB)) { + sendError(); + return; + } + + sendAck(); + + if ( (penStepsEBB==0) && (rotStepsEBB==0) ) { + delay(duration); + return; + } + + sBMP = 0; + isBMP = false; + prepareMove(duration, penStepsEBB, rotStepsEBB); +} + +void stepperBitmapMove() { + uint16_t duration=0; //in ms + int penStepsEBB=0; //Pen + int rotStepsEBB=0; //Rot + word soploBMP=0; // + + moveToDestination(); + + if (!parseSBArgs(&duration, &penStepsEBB, &rotStepsEBB, &soploBMP)) { + sendError(); + return; + } + + sendAck(); + + if ( (penStepsEBB==0) && (rotStepsEBB==0) ) { + delay(duration); + return; + } + + sBMP = soploBMP; + isBMP = true; + prepareMove(duration, penStepsEBB, rotStepsEBB); +} + +void setPenUp() { + penServo.write(penUpPos, servoRateUp, true); + penState=penUpPos; +} + + +void setPenDown() { + penServo.write(penDownPos, servoRateDown, true); + penState=penDownPos; +} + + +void setPen(){ + int cmd; + int value; + char *arg; + + moveToDestination(); + + arg = SCmd.next(); + if (arg != NULL) { + cmd = atoi(arg); + switch (cmd) { + case 0: + sendAck(); + setPenDown(); + break; + + case 1: + sendAck(); + setPenUp(); + break; + + default: + sendError(); + } + } + char *val; + val = SCmd.next(); + if (val != NULL) { + value = atoi(val); + delay(value); + } +} + +void togglePen(){ + int value; + char *arg; + + moveToDestination(); + + arg = SCmd.next(); + if (arg != NULL) + value = atoi(arg); + else + value = 500; + + doTogglePen(); + sendAck(); + delay(value); +} + +void doTogglePen() { + if (penState==penUpPos) { + setPenDown(); + } else { + setPenUp(); + } +} + +void enableMotors(){ + int cmd; + int value; + char *arg; + char *val; + arg = SCmd.next(); + if (arg != NULL) + cmd = atoi(arg); + val = SCmd.next(); + if (val != NULL) + value = atoi(val); + //values parsed + if ((arg != NULL) && (val == NULL)){ + switch (cmd) { + case 0: motorsOff(); + sendAck(); + break; + case 1: motorsOn(); + sendAck(); + break; + default: + sendError(); + } + } + //the following implementaion is a little bit cheated, because i did not know, how to implement different values for first and second argument. + if ((arg != NULL) && (val != NULL)){ + switch (value) { + case 0: motorsOff(); + sendAck(); + break; + case 1: motorsOn(); + sendAck(); + break; + default: + sendError(); + } + } +} + +void stepperModeConfigure(){ + int cmd; + int value; + char *arg; + arg = SCmd.next(); + if (arg != NULL) + cmd = atoi(arg); + char *val; + val = SCmd.next(); + if (val != NULL) + value = atoi(val); + if ((arg != NULL) && (val != NULL)){ + switch (cmd) { + case 4: penUpPos= (int) ((float) (value-6000)/(float) 133.3); // transformation from EBB to PWM-Servo + storePenUpPosInEE(); + sendAck(); + break; + case 5: penDownPos= (int)((float) (value-6000)/(float) 133.3); // transformation from EBB to PWM-Servo + storePenDownPosInEE(); + sendAck(); + break; + case 6: //rotMin=value; ignored + sendAck(); + break; + case 7: //rotMax=value; ignored + sendAck(); + break; + case 11: servoRateUp=value / 5; + eeprom_update_word(penUpRateEEAddress, servoRateUp); + sendAck(); + break; + case 12: servoRateDown=value / 5; + eeprom_update_word(penDownRateEEAddress, servoRateDown); + sendAck(); + break; + default: + sendError(); + } + } +} + +void pinOutput(){ + char *arg1; + char *arg2; + char *arg3; + int val; + + arg1 = SCmd.next(); + arg2 = SCmd.next(); + arg3 = SCmd.next(); + if (arg1 == NULL || arg2 == NULL || arg3 == NULL) { + sendError(); + return; + } + //PO,B,3,0 = disable engraver + //PO,B,3,1 = enable engraver + if (arg1[0] == 'B' && arg2[0] == '3') { + val = atoi(arg3); + digitalWrite(engraverPin, val); + } + sendAck(); +} + +//currently inkscape extension is using PO command for engraver instead of SE +void setEngraver(){ + char *arg; + int val; + arg = SCmd.next(); + if (arg != NULL) { + val = atoi(arg); + digitalWrite(engraverPin, val); + } + sendAck(); +} + +void sendVersion(){ + Serial.print(initSting); + Serial.print("\r\n"); +} + +void unrecognized(const char *command){ + sendError(); +} + +void ignore(){ + sendAck(); +} diff --git a/Source/Arduino/EggDuino/Helper_Functions.ino b/Source/Arduino/EggDuino/Helper_Functions.ino new file mode 100644 index 0000000..714a1d4 --- /dev/null +++ b/Source/Arduino/EggDuino/Helper_Functions.ino @@ -0,0 +1,234 @@ + + +void initHardware(){ + // enable eeprom wait in avr/eeprom.h functions + SPMCSR &= ~SELFPRGEN; + + loadPenPosFromEE(); +// SM,10000,1000,1000 +#ifdef BOARD_ULN2003 + pinMode(engraverPin, OUTPUT); + rotMotor.setMaxSpeed(800.0); + rotMotor.setAcceleration(300.0); + penMotor.setMaxSpeed(800.0); + penMotor.setAcceleration(300.0); +#else + pinMode(enableRotMotor, OUTPUT); + pinMode(enablePenMotor, OUTPUT); + pinMode(engraverPin, OUTPUT); + rotMotor.setMaxSpeed(2000.0); + rotMotor.setAcceleration(10000.0); + penMotor.setMaxSpeed(2000.0); + penMotor.setAcceleration(10000.0); +#endif + motorsOff(); + penServo.attach(servoPin); + penServo.write(penState); +} + +void loadPenPosFromEE() { + penUpPos = eeprom_read_word(penUpPosEEAddress); + penDownPos = eeprom_read_word(penDownPosEEAddress); + servoRateUp = eeprom_read_word(penUpRateEEAddress); + servoRateDown = eeprom_read_word(penDownRateEEAddress); + penState = penUpPos; +} + +void storePenUpPosInEE() { + eeprom_update_word(penUpPosEEAddress, penUpPos); +} + +void storePenDownPosInEE() { + eeprom_update_word(penDownPosEEAddress, penDownPos); +} + +void sendAck(){ + Serial.print("OK\r\n"); +} + +void sendError(){ + Serial.print("!8 Err: Unknown command\r\n"); +} + +void motorsOff() { +#ifdef BOARD_ULN2003 + for (byte i= 2; i < 10; i++) + digitalWrite(i, LOW); +#else + digitalWrite(enableRotMotor, HIGH); + digitalWrite(enablePenMotor, HIGH); +#endif + motorsEnabled = 0; +} + +void motorsOn() { +#ifndef BOARD_ULN2003 + digitalWrite(enableRotMotor, LOW) ; + digitalWrite(enablePenMotor, LOW) ; +#endif + motorsEnabled = 1; +} + +void toggleMotors() { + if (motorsEnabled) { + motorsOff(); + } else { + motorsOn(); + } +} + +bool parseSMArgs(uint16_t *duration, int *penStepsEBB, int *rotStepsEBB) { + char *arg1; + char *arg2; + char *arg3; + arg1 = SCmd.next(); + if (arg1 != NULL) { + *duration = atoi(arg1); + arg2 = SCmd.next(); + } + if (arg2 != NULL) { + *penStepsEBB = atoi(arg2); + arg3 = SCmd.next(); + } + if (arg3 != NULL) { + *rotStepsEBB = atoi(arg3); + + return true; + } + + return false; +} + +bool parseSBArgs(uint16_t *duration, int *penStepsEBB, int *rotStepsEBB, word *soploBMP) { + char *arg1; + char *arg2; + char *arg3; + char *arg4; + arg1 = SCmd.next(); + if (arg1 != NULL) { + *duration = atoi(arg1); + arg2 = SCmd.next(); + } + if (arg2 != NULL) { + *penStepsEBB = atoi(arg2); + arg3 = SCmd.next(); + } + if (arg3 != NULL) { + *rotStepsEBB = atoi(arg3); + arg4 = SCmd.next(); + } + if (arg4 != NULL) { + *soploBMP = atol(arg4); + + return true; + } + + return false; +} + +void prepareMove(uint16_t duration, int penStepsEBB, int rotStepsEBB) { + if (!motorsEnabled) { + motorsOn(); + } + if( (1 == rotStepCorrection) && (1 == penStepCorrection) ){ // if coordinatessystems are identical + //set Coordinates and Speed + +#ifdef BOARD_ULN2003 + // map 3200x800 eggbot corrdinates to our 28BYJ-48's penStepsPerRev and rotStepsUseable + + /*rotStepsEBB = map(rotStepsEBB, 0, 3200, 0, penStepsPerRev); + penStepsEBB = map(penStepsEBB, 0, 800, 0, rotStepsUseable);*/ + + /*long rotStepsX16 = (long)(rotStepsEBB * 16L); + long penStepsX16 = (long)(penStepsEBB * 16L); + + // Compare regular solution against 16x magnified solution + long rotSteps = (long)((rotStepsEBB) * rotScale) + (rotStepError / 16); + long penSteps = (long)((penStepsEBB) * penScale) + (penStepError / 16); + + rotStepsX16 = (long)((rotStepsX16 * rotScale) + rotStepError); + penStepsX16 = (long)((penStepsX16 * penScale) + penStepError); + + // Compute new error terms + rotStepError = rotStepsX16 - (rotSteps * 16L); + penStepError = penStepsX16 - (penSteps * 16L); + + rotStepsEBB = rotSteps; + penStepsEBB = penSteps;*/ +#endif + + //if (penState==penDownPos) { + // MyInkShield.spray_ink(0b0000000000000001); + //} + + rotMotor.move(rotStepsEBB); + rotMotor.setSpeed( abs( (float)rotStepsEBB * (float)1000 / (float)duration ) ); + penMotor.move(penStepsEBB); + penMotor.setSpeed( abs( (float)penStepsEBB * (float)1000 / (float)duration ) ); + } else { + //incoming EBB-Steps will be multiplied by 16, then Integer-maths is done, result will be divided by 16 + // This make thinks here really complicated, but floating point-math kills performance and memory, believe me... I tried... + long rotSteps = ( (long)rotStepsEBB * 16 / rotStepCorrection) + (long)rotStepError; //correct incoming EBB-Steps to our microstep-Setting and multiply by 16 to avoid floatingpoint... + long penSteps = ( (long)penStepsEBB * 16 / penStepCorrection) + (long)penStepError; + + int rotStepsToGo = (int) (rotSteps/16); //Calc Steps to go, which are possible on our machine + int penStepsToGo = (int) (penSteps/16); + + rotStepError = (long)rotSteps - ((long) rotStepsToGo * (long)16); // calc Position-Error, if there is one + penStepError = (long)penSteps - ((long) penStepsToGo * (long)16); + + long temp_rotSpeed = ((long)rotStepsToGo * (long)1000 / (long)duration ); // calc Speed in Integer Math + long temp_penSpeed = ((long)penStepsToGo * (long)1000 / (long)duration ) ; + + float rotSpeed= (float) abs(temp_rotSpeed); // type cast + float penSpeed= (float) abs(temp_penSpeed); + + //set Coordinates and Speed + rotMotor.move(rotStepsToGo); // finally, let us set the target position... + rotMotor.setSpeed(rotSpeed); // and the Speed! + penMotor.move(penStepsToGo); + penMotor.setSpeed( penSpeed ); + } +} + +void moveOneStep() { + if ( penMotor.distanceToGo() || rotMotor.distanceToGo() ) { + sprayIfNeeded(); + penMotor.runSpeedToPosition(); // Moving.... moving... moving.... + sprayIfNeeded(); + rotMotor.runSpeedToPosition(); + } +} + +void moveToDestination() { + while ( penMotor.distanceToGo() || rotMotor.distanceToGo() ) { + + sprayIfNeeded(); + penMotor.runSpeedToPosition(); // Moving.... moving... moving.... + sprayIfNeeded(); + rotMotor.runSpeedToPosition(); + } +} + +void sprayIfNeeded() +{ + + if (penState==penDownPos) { + if (countStep>=0) { // Добавить в настройки + countStep = 0; + if (isBMP) { + if (sBMP != 0) { + MyInkShield.spray_ink(sBMP); + } + } else { + MyInkShield.spray_ink(0b0000000100000000); + } + } else { + countStep++; + } + } +} + +void setprgButtonState(){ + prgButtonState = 1; +} diff --git a/Source/Arduino/EggDuino/LICENSE.md b/Source/Arduino/EggDuino/LICENSE.md new file mode 100644 index 0000000..766a0a5 --- /dev/null +++ b/Source/Arduino/EggDuino/LICENSE.md @@ -0,0 +1,21 @@ +The MIT License (MIT) + +Copyright (c) {{{year}}} {{{fullname}}} + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. diff --git a/Source/Arduino/EggDuino/README.md b/Source/Arduino/EggDuino/README.md new file mode 100644 index 0000000..9591f36 --- /dev/null +++ b/Source/Arduino/EggDuino/README.md @@ -0,0 +1,79 @@ + +This is a fork of the Yura80/EggDuino firmware that adds support for 28BYJ-48 Stepper motors driven by ULN2003's off Arduino pins 2,3,4,5 for the rotational axis, pins 6,7,8,9 for the pen axis and pin 10 for the servo. Tested with Arduino IDE 1.8.0 running http://www.thingiverse.com/thing:1461709 + +This is the fork of cocktailyogi/EggDuino firmware with added support for servo lowering/raising speed and pin assignment for Protoneer CNC Shield. + + +Eggduino +==== + +Arduino Firmware for Eggbot / Spherebot with Inkscape-Integration + +Version 1.6a +tested with Inkscape Portable 0.91, Eggbot Extension and patched eggbot.py + +Regards: Eggduino-Firmware by Joachim Cerny, 2015 + +Thanks for the nice libs ACCELSTEPPER and SERIALCOMMAND, which made this project much easier. Thanks to the Eggbot-Team for such a funny and enjoyable concept! Thanks to my wife and my daughter for their patience. :-) + +Features: + +- Implemented Eggbot-Protocol-Version 2.1.0 +- Turn-on homing: switch-on position of pen will be taken as reference point. +- No collision-detection!! +- Supported Servos: standard analog mini servos like TG9e, SG90, ES08MA, HXT900, etc. +- Full Arduino-Compatible. I used an Arduino Uno +- Button-support (3 buttons) + +Tested and fully functional with Inkscape. + +Installation: + +- Upload Eggduino.ino with Arduino-IDE or similar tool to your Arudino (i.e. Uno) +- Disable Autoreset on Arduinoboard (there are several ways to do this... Which one does not matter...) +- Install Inkscape Tools wit Eggbot extension. Detailed instructions: (You yust need to complete Steps 1 and 2) +http://wiki.evilmadscientist.com/Installing_software + +- Eggduino cannot be detected by default by the Eggbot-extension + Hopefully, the guys will fix this later on. But we can fix it on our own. + Go to your Inkscape-Installation folder and navigate to subfolder .\App\Inkscape\share\extensions + +For version 2.5.0: + + - open file "eggbot.py" in text editor and search for the line: + "Try any devices which seem to have EBB boards attached" + + - comment that block with "#" like this: + # Try any devices which seem to have EBB boards attached + # for strComPort in eggbot_scan.findEiBotBoards(): + # serialPort = self.testSerialPort( strComPort ) + # if serialPort: + # self.svgSerialPort = strComPort + # return serialPort + + - In my version lines 1355-1360 + +For version 2.7.1: + + - open file "ebb_serial.py" in text editor and search for the following block: + + EBBport = None + for port in comPortsList: + if port[1].startswith("EiBotBoard"): + EBBport = port[0] #Success; EBB found by name match. + break #stop searching-- we are done. + if EBBport is None: + for port in comPortsList: + if port[2].startswith("USB VID:PID=04D8:FD92"): + EBBport = port[0] #Success; EBB found by VID/PID match. + break #stop searching-- we are done. + + - replace "04D8:FD92" with the VID/PID of your Arduino device. + + - alternatively, you can replace "EBBport = None" with your specific port number: + EBBport = "COMxx" #Windows + EBBport = "/dev/tty[something]" #Linux/Mac + + + + diff --git a/Source/Arduino/EggDuino/SerialCommand.cpp b/Source/Arduino/EggDuino/SerialCommand.cpp new file mode 100644 index 0000000..9d0e7af --- /dev/null +++ b/Source/Arduino/EggDuino/SerialCommand.cpp @@ -0,0 +1,144 @@ +/** + * SerialCommand - A Wiring/Arduino library to tokenize and parse commands + * received over a serial port. + * + * Copyright (C) 2012 Stefan Rado + * Copyright (C) 2011 Steven Cogswell + * http://husks.wordpress.com + * + * Version 20120522 + * + * This library is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this library. If not, see . + */ +#include "SerialCommand.h" + +/** + * Constructor makes sure some things are set. + */ +SerialCommand::SerialCommand() + : commandList(NULL), + commandCount(0), + defaultHandler(NULL), + term('\r'), // default terminator for commands, newline character + last(NULL) +{ + strcpy(delim, ","); // strtok_r needs a null-terminated string + clearBuffer(); +} + +/** + * Adds a "command" and a handler function to the list of available commands. + * This is used for matching a found token in the buffer, and gives the pointer + * to the handler function to deal with it. + */ +void SerialCommand::addCommand(const char *command, void (*function)()) { + #ifdef SERIALCOMMAND_DEBUG + Serial.print("Adding command ("); + Serial.print(commandCount); + Serial.print("): "); + Serial.println(command); + #endif + + commandList = (SerialCommandCallback *) realloc(commandList, (commandCount + 1) * sizeof(SerialCommandCallback)); + strncpy(commandList[commandCount].command, command, SERIALCOMMAND_MAXCOMMANDLENGTH); + commandList[commandCount].function = function; + commandCount++; +} + +/** + * This sets up a handler to be called in the event that the receveived command string + * isn't in the list of commands. + */ +void SerialCommand::setDefaultHandler(void (*function)(const char *)) { + defaultHandler = function; +} + + +/** + * This checks the Serial stream for characters, and assembles them into a buffer. + * When the terminator character (default '\n') is seen, it starts parsing the + * buffer for a prefix command, and calls handlers setup by addCommand() member + */ +void SerialCommand::readSerial() { + while (Serial.available() > 0) { + char inChar = Serial.read(); // Read single available character, there may be more waiting + #ifdef SERIALCOMMAND_DEBUG + Serial.print(inChar); // Echo back to serial stream + #endif + + if (inChar == term) { // Check for the terminator (default '\r') meaning end of command + #ifdef SERIALCOMMAND_DEBUG + Serial.print("Received: "); + Serial.println(buffer); + #endif + + char *command = strtok_r(buffer, delim, &last); // Search for command at start of buffer + if (command != NULL) { + boolean matched = false; + for (int i = 0; i < commandCount; i++) { + #ifdef SERIALCOMMAND_DEBUG + Serial.print("Comparing ["); + Serial.print(command); + Serial.print("] to ["); + Serial.print(commandList[i].command); + Serial.println("]"); + #endif + + // Compare the found command against the list of known commands for a match + if (strncmp(command, commandList[i].command, SERIALCOMMAND_MAXCOMMANDLENGTH) == 0) { + #ifdef SERIALCOMMAND_DEBUG + Serial.print("Matched Command: "); + Serial.println(command); + #endif + + // Execute the stored handler function for the command + (*commandList[i].function)(); + matched = true; + break; + } + } + if (!matched && (defaultHandler != NULL)) { + (*defaultHandler)(command); + } + } + clearBuffer(); + } + else if (isprint(inChar)) { // Only printable characters into the buffer + if (bufPos < SERIALCOMMAND_BUFFER) { + buffer[bufPos++] = inChar; // Put character into buffer + buffer[bufPos] = '\0'; // Null terminate + } else { + #ifdef SERIALCOMMAND_DEBUG + Serial.println("Line buffer is full - increase SERIALCOMMAND_BUFFER"); + #endif + } + } + } +} + +/* + * Clear the input buffer. + */ +void SerialCommand::clearBuffer() { + buffer[0] = '\0'; + bufPos = 0; +} + +/** + * Retrieve the next token ("word" or "argument") from the command buffer. + * Returns NULL if no more tokens exist. + */ +char *SerialCommand::next() { + return strtok_r(NULL, delim, &last); +} diff --git a/Source/Arduino/EggDuino/SerialCommand.h b/Source/Arduino/EggDuino/SerialCommand.h new file mode 100644 index 0000000..9f98c38 --- /dev/null +++ b/Source/Arduino/EggDuino/SerialCommand.h @@ -0,0 +1,75 @@ +/** + * SerialCommand - A Wiring/Arduino library to tokenize and parse commands + * received over a serial port. + * + * Copyright (C) 2012 Stefan Rado + * Copyright (C) 2011 Steven Cogswell + * http://husks.wordpress.com + * + * Version 20120522 + * + * This library is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this library. If not, see . + */ +#ifndef SerialCommand_h +#define SerialCommand_h + +#if defined(WIRING) && WIRING >= 100 + #include +#elif defined(ARDUINO) && ARDUINO >= 100 + #include +#else + #include +#endif +#include + +// Size of the input buffer in bytes (maximum length of one command plus arguments) +#define SERIALCOMMAND_BUFFER 63 +// Maximum length of a command excluding the terminating null +#define SERIALCOMMAND_MAXCOMMANDLENGTH 20 + +// Uncomment the next line to run the library in debug mode (verbose messages) +//#define SERIALCOMMAND_DEBUG + + +class SerialCommand { + public: + SerialCommand(); // Constructor + void addCommand(const char *command, void(*function)()); // Add a command to the processing dictionary. + void setDefaultHandler(void (*function)(const char *)); // A handler to call when no valid command received. + + void readSerial(); // Main entry point. + void clearBuffer(); // Clears the input buffer. + char *next(); // Returns pointer to next token found in command buffer (for getting arguments to commands). + + private: + // Command/handler dictionary + struct SerialCommandCallback { + char command[SERIALCOMMAND_MAXCOMMANDLENGTH + 1]; + void (*function)(); + }; // Data structure to hold Command/Handler function key-value pairs + SerialCommandCallback *commandList; // Actual definition for command/handler array + byte commandCount; + + // Pointer to the default handler function + void (*defaultHandler)(const char *); + + char delim[2]; // null-terminated list of character to be used as delimeters for tokenizing (default " ") + char term; // Character that signals end of command (default '\n') + + char buffer[SERIALCOMMAND_BUFFER + 1]; // Buffer of stored characters while waiting for terminator character + byte bufPos; // Current position in the buffer + char *last; // State variable used by strtok_r during processing +}; + +#endif //SerialCommand_h diff --git a/Source/Arduino/EggDuino/VarSpeedServo.cpp b/Source/Arduino/EggDuino/VarSpeedServo.cpp new file mode 100644 index 0000000..713adde --- /dev/null +++ b/Source/Arduino/EggDuino/VarSpeedServo.cpp @@ -0,0 +1,531 @@ +/* + Servo.cpp - Interrupt driven Servo library for Arduino using 16 bit timers- Version 2 + Copyright (c) 2009 Michael Margolis. All right reserved. + + This library is free software; you can redistribute it and/or + modify it under the terms of the GNU Lesser General Public + License as published by the Free Software Foundation; either + version 2.1 of the License, or (at your option) any later version. + + This library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public + License along with this library; if not, write to the Free Software + Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA + */ + +/* + Function slowmove and supporting code added 2010 by Korman. Above limitations apply + to all added code, except for the official maintainer of the Servo library. If he, + and only he deems the enhancment a good idea to add to the official Servo library, + he may add it without the requirement to name the author of the parts original to + this version of the library. +*/ + +/* + Updated 2013 by Philip van Allen (pva), + -- updated for Arduino 1.0 + + -- consolidated slowmove into the write command (while keeping slowmove() for compatibility + with Korman's version) + -- added wait parameter to allow write command to block until move is complete + -- added sequence playing ability to asynchronously move the servo through a series of positions, must be called in a loop + + A servo is activated by creating an instance of the Servo class passing the desired pin to the attach() method. + The servos are pulsed in the background using the value most recently written using the write() method + + Note that analogWrite of PWM on pins associated with the timer are disabled when the first servo is attached. + Timers are seized as needed in groups of 12 servos - 24 servos use two timers, 48 servos will use four. + The sequence used to sieze timers is defined in timers.h + + The methods are: + + VarSpeedServo - Class for manipulating servo motors connected to Arduino pins. + + attach(pin ) - Attaches a servo motor to an i/o pin. + attach(pin, min, max ) - Attaches to a pin setting min and max values in microseconds + default min is 544, max is 2400 + + write(value) - Sets the servo angle in degrees. (invalid angle that is valid as pulse in microseconds is treated as microseconds) + write(value, speed) - speed varies the speed of the move to new position 0=full speed, 1-255 slower to faster + write(value, speed, wait) - wait is a boolean that, if true, causes the function call to block until move is complete + + writeMicroseconds() - Sets the servo pulse width in microseconds + read() - Gets the last written servo pulse width as an angle between 0 and 180. + readMicroseconds() - Gets the last written servo pulse width in microseconds. (was read_us() in first release) + attached() - Returns true if there is a servo attached. + detach() - Stops an attached servos from pulsing its i/o pin. + + slowmove(value, speed) - The same as write(value, speed), retained for compatibility with Korman's version + + stop() - stops the servo at the current position + + sequencePlay(sequence, sequencePositions); // play a looping sequence starting at position 0 + sequencePlay(sequence, sequencePositions, loop, startPosition); // play sequence with number of positions, loop if true, start at position + sequenceStop(); // stop sequence at current position + + */ + +#include +#include // updated from WProgram.h to Arduino.h for Arduino 1.0+, pva + +#include "VarSpeedServo.h" + +#define usToTicks(_us) (( clockCyclesPerMicrosecond()* _us) / 8) // converts microseconds to tick (assumes prescale of 8) // 12 Aug 2009 +#define ticksToUs(_ticks) (( (unsigned)_ticks * 8)/ clockCyclesPerMicrosecond() ) // converts from ticks back to microseconds + + +#define TRIM_DURATION 2 // compensation ticks to trim adjust for digitalWrite delays // 12 August 2009 + +//#define NBR_TIMERS (MAX_SERVOS / SERVOS_PER_TIMER) + +static servo_t servos[MAX_SERVOS]; // static array of servo structures +static volatile int8_t Channel[_Nbr_16timers ]; // counter for the servo being pulsed for each timer (or -1 if refresh interval) + +uint8_t ServoCount = 0; // the total number of attached servos + +// sequence vars + +servoSequencePoint initSeq[] = {{0,100},{45,100}}; + +//sequence_t sequences[MAX_SEQUENCE]; + +// convenience macros +#define SERVO_INDEX_TO_TIMER(_servo_nbr) ((timer16_Sequence_t)(_servo_nbr / SERVOS_PER_TIMER)) // returns the timer controlling this servo +#define SERVO_INDEX_TO_CHANNEL(_servo_nbr) (_servo_nbr % SERVOS_PER_TIMER) // returns the index of the servo on this timer +#define SERVO_INDEX(_timer,_channel) ((_timer*SERVOS_PER_TIMER) + _channel) // macro to access servo index by timer and channel +#define SERVO(_timer,_channel) (servos[SERVO_INDEX(_timer,_channel)]) // macro to access servo class by timer and channel + +#define SERVO_MIN() (MIN_PULSE_WIDTH - this->min * 4) // minimum value in uS for this servo +#define SERVO_MAX() (MAX_PULSE_WIDTH - this->max * 4) // maximum value in uS for this servo + +/************ static functions common to all instances ***********************/ + +static inline void handle_interrupts(timer16_Sequence_t timer, volatile uint16_t *TCNTn, volatile uint16_t* OCRnA) +{ + if( Channel[timer] < 0 ) + *TCNTn = 0; // channel set to -1 indicated that refresh interval completed so reset the timer + else{ + if( SERVO_INDEX(timer,Channel[timer]) < ServoCount && SERVO(timer,Channel[timer]).Pin.isActive == true ) + digitalWrite( SERVO(timer,Channel[timer]).Pin.nbr,LOW); // pulse this channel low if activated + } + + Channel[timer]++; // increment to the next channel + if( SERVO_INDEX(timer,Channel[timer]) < ServoCount && Channel[timer] < SERVOS_PER_TIMER) { + + // Extension for slowmove + if (SERVO(timer,Channel[timer]).speed) { + // Increment ticks by speed until we reach the target. + // When the target is reached, speed is set to 0 to disable that code. + if (SERVO(timer,Channel[timer]).target > SERVO(timer,Channel[timer]).ticks) { + SERVO(timer,Channel[timer]).ticks += SERVO(timer,Channel[timer]).speed; + if (SERVO(timer,Channel[timer]).target <= SERVO(timer,Channel[timer]).ticks) { + SERVO(timer,Channel[timer]).ticks = SERVO(timer,Channel[timer]).target; + SERVO(timer,Channel[timer]).speed = 0; + } + } + else { + SERVO(timer,Channel[timer]).ticks -= SERVO(timer,Channel[timer]).speed; + if (SERVO(timer,Channel[timer]).target >= SERVO(timer,Channel[timer]).ticks) { + SERVO(timer,Channel[timer]).ticks = SERVO(timer,Channel[timer]).target; + SERVO(timer,Channel[timer]).speed = 0; + } + } + } + // End of Extension for slowmove + + // Todo + + *OCRnA = *TCNTn + SERVO(timer,Channel[timer]).ticks; + if(SERVO(timer,Channel[timer]).Pin.isActive == true) // check if activated + digitalWrite( SERVO(timer,Channel[timer]).Pin.nbr,HIGH); // its an active channel so pulse it high + } + else { + // finished all channels so wait for the refresh period to expire before starting over + if( (unsigned)*TCNTn < (usToTicks(REFRESH_INTERVAL) + 4) ) // allow a few ticks to ensure the next OCR1A not missed + *OCRnA = (unsigned int)usToTicks(REFRESH_INTERVAL); + else + *OCRnA = *TCNTn + 4; // at least REFRESH_INTERVAL has elapsed + Channel[timer] = -1; // this will get incremented at the end of the refresh period to start again at the first channel + } +} + +#ifndef WIRING // Wiring pre-defines signal handlers so don't define any if compiling for the Wiring platform +// Interrupt handlers for Arduino +#if defined(_useTimer1) +SIGNAL (TIMER1_COMPA_vect) +{ + handle_interrupts(_timer1, &TCNT1, &OCR1A); +} +#endif + +#if defined(_useTimer3) +SIGNAL (TIMER3_COMPA_vect) +{ + handle_interrupts(_timer3, &TCNT3, &OCR3A); +} +#endif + +#if defined(_useTimer4) +SIGNAL (TIMER4_COMPA_vect) +{ + handle_interrupts(_timer4, &TCNT4, &OCR4A); +} +#endif + +#if defined(_useTimer5) +SIGNAL (TIMER5_COMPA_vect) +{ + handle_interrupts(_timer5, &TCNT5, &OCR5A); +} +#endif + +#elif defined WIRING +// Interrupt handlers for Wiring +#if defined(_useTimer1) +void Timer1Service() +{ + handle_interrupts(_timer1, &TCNT1, &OCR1A); +} +#endif +#if defined(_useTimer3) +void Timer3Service() +{ + handle_interrupts(_timer3, &TCNT3, &OCR3A); +} +#endif +#endif + + +static void initISR(timer16_Sequence_t timer) +{ +#if defined (_useTimer1) + if(timer == _timer1) { + TCCR1A = 0; // normal counting mode + TCCR1B = _BV(CS11); // set prescaler of 8 + TCNT1 = 0; // clear the timer count +#if defined(__AVR_ATmega8__)|| defined(__AVR_ATmega128__) + TIFR |= _BV(OCF1A); // clear any pending interrupts; + TIMSK |= _BV(OCIE1A) ; // enable the output compare interrupt +#else + // here if not ATmega8 or ATmega128 + TIFR1 |= _BV(OCF1A); // clear any pending interrupts; + TIMSK1 |= _BV(OCIE1A) ; // enable the output compare interrupt +#endif +#if defined(WIRING) + timerAttach(TIMER1OUTCOMPAREA_INT, Timer1Service); +#endif + } +#endif + +#if defined (_useTimer3) + if(timer == _timer3) { + TCCR3A = 0; // normal counting mode + TCCR3B = _BV(CS31); // set prescaler of 8 + TCNT3 = 0; // clear the timer count +#if defined(__AVR_ATmega128__) + TIFR |= _BV(OCF3A); // clear any pending interrupts; + ETIMSK |= _BV(OCIE3A); // enable the output compare interrupt +#else + TIFR3 = _BV(OCF3A); // clear any pending interrupts; + TIMSK3 = _BV(OCIE3A) ; // enable the output compare interrupt +#endif +#if defined(WIRING) + timerAttach(TIMER3OUTCOMPAREA_INT, Timer3Service); // for Wiring platform only +#endif + } +#endif + +#if defined (_useTimer4) + if(timer == _timer4) { + TCCR4A = 0; // normal counting mode + TCCR4B = _BV(CS41); // set prescaler of 8 + TCNT4 = 0; // clear the timer count + TIFR4 = _BV(OCF4A); // clear any pending interrupts; + TIMSK4 = _BV(OCIE4A) ; // enable the output compare interrupt + } +#endif + +#if defined (_useTimer5) + if(timer == _timer5) { + TCCR5A = 0; // normal counting mode + TCCR5B = _BV(CS51); // set prescaler of 8 + TCNT5 = 0; // clear the timer count + TIFR5 = _BV(OCF5A); // clear any pending interrupts; + TIMSK5 = _BV(OCIE5A) ; // enable the output compare interrupt + } +#endif +} + +static void finISR(timer16_Sequence_t timer) +{ + //disable use of the given timer +#if defined WIRING // Wiring + if(timer == _timer1) { + #if defined(__AVR_ATmega1281__)||defined(__AVR_ATmega2561__) + TIMSK1 &= ~_BV(OCIE1A) ; // disable timer 1 output compare interrupt + #else + TIMSK &= ~_BV(OCIE1A) ; // disable timer 1 output compare interrupt + #endif + timerDetach(TIMER1OUTCOMPAREA_INT); + } + else if(timer == _timer3) { + #if defined(__AVR_ATmega1281__)||defined(__AVR_ATmega2561__) + TIMSK3 &= ~_BV(OCIE3A); // disable the timer3 output compare A interrupt + #else + ETIMSK &= ~_BV(OCIE3A); // disable the timer3 output compare A interrupt + #endif + timerDetach(TIMER3OUTCOMPAREA_INT); + } +#else + //For arduino - in future: call here to a currently undefined function to reset the timer +#endif +} + +static boolean isTimerActive(timer16_Sequence_t timer) +{ + // returns true if any servo is active on this timer + for(uint8_t channel=0; channel < SERVOS_PER_TIMER; channel++) { + if(SERVO(timer,channel).Pin.isActive == true) + return true; + } + return false; +} + + +/****************** end of static functions ******************************/ + +VarSpeedServo::VarSpeedServo() +{ + if( ServoCount < MAX_SERVOS) { + this->servoIndex = ServoCount++; // assign a servo index to this instance + servos[this->servoIndex].ticks = usToTicks(DEFAULT_PULSE_WIDTH); // store default values - 12 Aug 2009 + this->curSeqPosition = 0; + this->curSequence = initSeq; + } + else + this->servoIndex = INVALID_SERVO ; // too many servos +} + +uint8_t VarSpeedServo::attach(int pin) +{ + return this->attach(pin, MIN_PULSE_WIDTH, MAX_PULSE_WIDTH); +} + +uint8_t VarSpeedServo::attach(int pin, int min, int max) +{ + if(this->servoIndex < MAX_SERVOS ) { + pinMode( pin, OUTPUT) ; // set servo pin to output + servos[this->servoIndex].Pin.nbr = pin; + // todo min/max check: abs(min - MIN_PULSE_WIDTH) /4 < 128 + this->min = (MIN_PULSE_WIDTH - min)/4; //resolution of min/max is 4 uS + this->max = (MAX_PULSE_WIDTH - max)/4; + // initialize the timer if it has not already been initialized + timer16_Sequence_t timer = SERVO_INDEX_TO_TIMER(servoIndex); + if(isTimerActive(timer) == false) + initISR(timer); + servos[this->servoIndex].Pin.isActive = true; // this must be set after the check for isTimerActive + } + return this->servoIndex ; +} + +void VarSpeedServo::detach() +{ + servos[this->servoIndex].Pin.isActive = false; + timer16_Sequence_t timer = SERVO_INDEX_TO_TIMER(servoIndex); + if(isTimerActive(timer) == false) { + finISR(timer); + } +} + +void VarSpeedServo::write(int value) +{ + if(value < MIN_PULSE_WIDTH) + { // treat values less than 544 as angles in degrees (valid values in microseconds are handled as microseconds) + // updated to use constrain() instead of if(), pva + value = constrain(value, 0, 180); + value = map(value, 0, 180, SERVO_MIN(), SERVO_MAX()); + } + this->writeMicroseconds(value); +} + +void VarSpeedServo::writeMicroseconds(int value) +{ + // calculate and store the values for the given channel + byte channel = this->servoIndex; + if( (channel >= 0) && (channel < MAX_SERVOS) ) // ensure channel is valid + { + if( value < SERVO_MIN() ) // ensure pulse width is valid + value = SERVO_MIN(); + else if( value > SERVO_MAX() ) + value = SERVO_MAX(); + + value -= TRIM_DURATION; + value = usToTicks(value); // convert to ticks after compensating for interrupt overhead - 12 Aug 2009 + + uint8_t oldSREG = SREG; + cli(); + servos[channel].ticks = value; + SREG = oldSREG; + + // Extension for slowmove + // Disable slowmove logic. + servos[channel].speed = 0; + // End of Extension for slowmove + } +} + +// Extension for slowmove +/* + write(value, speed) - Just like write but at reduced speed. + + value - Target position for the servo. Identical use as value of the function write. + speed - Speed at which to move the servo. + speed=0 - Full speed, identical to write + speed=1 - Minimum speed + speed=255 - Maximum speed +*/ +void VarSpeedServo::write(int value, uint8_t speed) { + // This fuction is a copy of write and writeMicroseconds but value will be saved + // in target instead of in ticks in the servo structure and speed will be save + // there too. + + int degrees = value; + + if (speed) { + if (value < MIN_PULSE_WIDTH) { + // treat values less than 544 as angles in degrees (valid values in microseconds are handled as microseconds) + // updated to use constrain instead of if, pva + value = constrain(value, 0, 180); + value = map(value, 0, 180, SERVO_MIN(), SERVO_MAX()); + } + // calculate and store the values for the given channel + byte channel = this->servoIndex; + if( (channel >= 0) && (channel < MAX_SERVOS) ) { // ensure channel is valid + // updated to use constrain instead of if, pva + value = constrain(value, SERVO_MIN(), SERVO_MAX()); + + value = value - TRIM_DURATION; + value = usToTicks(value); // convert to ticks after compensating for interrupt overhead - 12 Aug 2009 + + // Set speed and direction + uint8_t oldSREG = SREG; + cli(); + servos[channel].target = value; + servos[channel].speed = speed; + SREG = oldSREG; + } + } + else { + write (value); + } +} + +void VarSpeedServo::write(int value, uint8_t speed, bool wait) { + write(value, speed); + if (wait) { // block until the servo is at its new position + if (value < MIN_PULSE_WIDTH) { + while (read() != value) { + delay(5); + } + } else { + while (readMicroseconds() != value) { + delay(5); + } + } + } +} + +void VarSpeedServo::stop() { + write(read()); +} + +void VarSpeedServo::slowmove(int value, uint8_t speed) { + // legacy function to support original version of VarSpeedServo + write(value, speed); +} + +// End of Extension for slowmove + + +int VarSpeedServo::read() // return the value as degrees +{ + return map( this->readMicroseconds()+1, SERVO_MIN(), SERVO_MAX(), 0, 180); +} + +int VarSpeedServo::readMicroseconds() +{ + unsigned int pulsewidth; + if( this->servoIndex != INVALID_SERVO ) + pulsewidth = ticksToUs(servos[this->servoIndex].ticks) + TRIM_DURATION ; // 12 aug 2009 + else + pulsewidth = 0; + + return pulsewidth; +} + +bool VarSpeedServo::attached() +{ + return servos[this->servoIndex].Pin.isActive ; +} + +uint8_t VarSpeedServo::sequencePlay(servoSequencePoint sequenceIn[], uint8_t numPositions, bool loop, uint8_t startPos) { + uint8_t oldSeqPosition = this->curSeqPosition; + + if( this->curSequence != sequenceIn) { + //Serial.println("newSeq"); + this->curSequence = sequenceIn; + this->curSeqPosition = startPos; + oldSeqPosition = 255; + } + + if (read() == sequenceIn[this->curSeqPosition].position && this->curSeqPosition != CURRENT_SEQUENCE_STOP) { + this->curSeqPosition++; + + if (this->curSeqPosition >= numPositions) { // at the end of the loop + if (loop) { // reset to the beginning of the loop + this->curSeqPosition = 0; + } else { // stop the loop + this->curSeqPosition = CURRENT_SEQUENCE_STOP; + } + } + } + + if (this->curSeqPosition != oldSeqPosition && this->curSeqPosition != CURRENT_SEQUENCE_STOP) { + // CURRENT_SEQUENCE_STOP position means the animation has ended, and should no longer be played + // otherwise move to the next position + write(sequenceIn[this->curSeqPosition].position, sequenceIn[this->curSeqPosition].speed); + //Serial.println(this->seqCurPosition); + } + + return this->curSeqPosition; +} + +uint8_t VarSpeedServo::sequencePlay(servoSequencePoint sequenceIn[], uint8_t numPositions) { + return sequencePlay(sequenceIn, numPositions, true, 0); +} + +void VarSpeedServo::sequenceStop() { + write(read()); + this->curSeqPosition = CURRENT_SEQUENCE_STOP; +} + +/* + To do +int VarSpeedServo::targetPosition() { + byte channel = this->servoIndex; + return map( servos[channel].target+1, SERVO_MIN(), SERVO_MAX(), 0, 180); +} + +int VarSpeedServo::targetPositionMicroseconds() { + byte channel = this->servoIndex; + return servos[channel].target; +} + +bool VarSpeedServo::isMoving() { + byte channel = this->servoIndex; + int servos[channel].target; +} +*/ \ No newline at end of file diff --git a/Source/Arduino/EggDuino/VarSpeedServo.h b/Source/Arduino/EggDuino/VarSpeedServo.h new file mode 100644 index 0000000..492c4c0 --- /dev/null +++ b/Source/Arduino/EggDuino/VarSpeedServo.h @@ -0,0 +1,180 @@ +/* + VarSpeedServo.h - Interrupt driven Servo library for Arduino using 16 bit timers- Version 2 + Copyright (c) 2009 Michael Margolis. All right reserved. + + This library is free software; you can redistribute it and/or + modify it under the terms of the GNU Lesser General Public + License as published by the Free Software Foundation; either + version 2.1 of the License, or (at your option) any later version. + + This library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public + License along with this library; if not, write to the Free Software + Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA +*/ + + +/* + Function slowmove and supporting code added 2010 by Korman. Above limitations apply + to all added code, except for the official maintainer of the Servo library. If he, + and only he deems the enhancment a good idea to add to the official Servo library, + he may add it without the requirement to name the author of the parts original to + this version of the library. +*/ + +/* + Updated 2013 by Philip van Allen (pva), + -- updated for Arduino 1.0 + + -- consolidated slowmove into the write command (while keeping slowmove() for compatibility + with Korman's version) + -- added wait parameter to allow write command to block until move is complete + -- added sequence playing ability to asynchronously move the servo through a series of positions, must be called in a loop + + + A servo is activated by creating an instance of the Servo class passing the desired pin to the attach() method. + The servos are pulsed in the background using the value most recently written using the write() method + + Note that analogWrite of PWM on pins associated with the timer are disabled when the first servo is attached. + Timers are seized as needed in groups of 12 servos - 24 servos use two timers, 48 servos will use four. + The sequence used to sieze timers is defined in timers.h + + The methods are: + + VarSpeedServo - Class for manipulating servo motors connected to Arduino pins. + + attach(pin ) - Attaches a servo motor to an i/o pin. + attach(pin, min, max ) - Attaches to a pin setting min and max values in microseconds + default min is 544, max is 2400 + + write(value) - Sets the servo angle in degrees. (invalid angle that is valid as pulse in microseconds is treated as microseconds) + write(value, speed) - speed varies the speed of the move to new position 0=full speed, 1-255 slower to faster + write(value, speed, wait) - wait is a boolean that, if true, causes the function call to block until move is complete + + writeMicroseconds() - Sets the servo pulse width in microseconds + read() - Gets the last written servo pulse width as an angle between 0 and 180. + readMicroseconds() - Gets the last written servo pulse width in microseconds. (was read_us() in first release) + attached() - Returns true if there is a servo attached. + detach() - Stops an attached servos from pulsing its i/o pin. + + slowmove(value, speed) - The same as write(value, speed), retained for compatibility with Korman's version + + stop() - stops the servo at the current position + + sequencePlay(sequence, sequencePositions); // play a looping sequence starting at position 0 + sequencePlay(sequence, sequencePositions, loop, startPosition); // play sequence with number of positions, loop if true, start at position + sequenceStop(); // stop sequence at current position + + */ + +#ifndef VarSpeedServo_h +#define VarSpeedServo_h + +#include + +/* + * Defines for 16 bit timers used with Servo library + * + * If _useTimerX is defined then TimerX is a 16 bit timer on the curent board + * timer16_Sequence_t enumerates the sequence that the timers should be allocated + * _Nbr_16timers indicates how many 16 bit timers are available. + * + */ + +// Say which 16 bit timers can be used and in what order +#if defined(__AVR_ATmega1280__) || defined(__AVR_ATmega2560__) +#define _useTimer5 +#define _useTimer1 +#define _useTimer3 +#define _useTimer4 +typedef enum { _timer5, _timer1, _timer3, _timer4, _Nbr_16timers } timer16_Sequence_t ; + +#elif defined(__AVR_ATmega32U4__) +#define _useTimer3 +#define _useTimer1 +typedef enum { _timer3, _timer1, _Nbr_16timers } timer16_Sequence_t ; + +#elif defined(__AVR_AT90USB646__) || defined(__AVR_AT90USB1286__) +#define _useTimer3 +#define _useTimer1 +typedef enum { _timer3, _timer1, _Nbr_16timers } timer16_Sequence_t ; + +#elif defined(__AVR_ATmega128__) ||defined(__AVR_ATmega1281__)||defined(__AVR_ATmega2561__) +#define _useTimer3 +#define _useTimer1 +typedef enum { _timer3, _timer1, _Nbr_16timers } timer16_Sequence_t ; + +#else // everything else +#define _useTimer1 +typedef enum { _timer1, _Nbr_16timers } timer16_Sequence_t ; +#endif + +#define VarSpeedServo_VERSION 2 // software version of this library + +#define MIN_PULSE_WIDTH 544 // the shortest pulse sent to a servo +#define MAX_PULSE_WIDTH 2400 // the longest pulse sent to a servo +#define DEFAULT_PULSE_WIDTH 1500 // default pulse width when servo is attached +#define REFRESH_INTERVAL 20000 // minumim time to refresh servos in microseconds + +#define SERVOS_PER_TIMER 12 // the maximum number of servos controlled by one timer +#define MAX_SERVOS (_Nbr_16timers * SERVOS_PER_TIMER) + +#define INVALID_SERVO 255 // flag indicating an invalid servo index + +#define CURRENT_SEQUENCE_STOP 255 // used to indicate the current sequence is not used and sequence should stop + + +typedef struct { + uint8_t nbr :6 ; // a pin number from 0 to 63 + uint8_t isActive :1 ; // true if this channel is enabled, pin not pulsed if false +} ServoPin_t ; + +typedef struct { + ServoPin_t Pin; + unsigned int ticks; + unsigned int target; // Extension for slowmove + uint8_t speed; // Extension for slowmove +} servo_t; + +typedef struct { + uint8_t position; + uint8_t speed; +} servoSequencePoint; + +class VarSpeedServo +{ +public: + VarSpeedServo(); + uint8_t attach(int pin); // attach the given pin to the next free channel, sets pinMode, returns channel number or 0 if failure + uint8_t attach(int pin, int min, int max); // as above but also sets min and max values for writes. + void detach(); + void write(int value); // if value is < 200 its treated as an angle, otherwise as pulse width in microseconds + void write(int value, uint8_t speed); // Move to given position at reduced speed. + // speed=0 is identical to write, speed=1 slowest and speed=255 fastest. + // On the RC-Servos tested, speeds differences above 127 can't be noticed, + // because of the mechanical limits of the servo. + void write(int value, uint8_t speed, bool wait); // wait parameter causes call to block until move completes + void writeMicroseconds(int value); // Write pulse width in microseconds + void slowmove(int value, uint8_t speed); + void stop(); // stop the servo where it is + + int read(); // returns current pulse width as an angle between 0 and 180 degrees + int readMicroseconds(); // returns current pulse width in microseconds for this servo (was read_us() in first release) + bool attached(); // return true if this servo is attached, otherwise false + + uint8_t sequencePlay(servoSequencePoint sequenceIn[], uint8_t numPositions, bool loop, uint8_t startPos); + uint8_t sequencePlay(servoSequencePoint sequenceIn[], uint8_t numPositions); // play a looping sequence starting at position 0 + void sequenceStop(); // stop movement +private: + uint8_t servoIndex; // index into the channel data for this servo + int8_t min; // minimum is this value times 4 added to MIN_PULSE_WIDTH + int8_t max; // maximum is this value times 4 added to MAX_PULSE_WIDTH + servoSequencePoint * curSequence; // for sequences + uint8_t curSeqPosition; // for sequences + +}; + +#endif diff --git a/Source/Arduino/EggDuino/VersionHistory.txt b/Source/Arduino/EggDuino/VersionHistory.txt new file mode 100644 index 0000000..4f74c41 --- /dev/null +++ b/Source/Arduino/EggDuino/VersionHistory.txt @@ -0,0 +1,35 @@ +15.11.2015 v1.6a +fixed bug in QP-Function + +05.05.2015 v1.6 +imported some improvements from user "bartebor" +fixed some timing issues +added button-support via #ifdefine +worked on penarm-shaking-bug +restructured some codesegments + +29.03.2015 v1.4 +added prototype for PRG-Button support. somebody has to test it. Connect pushbutton between Pin2 und GND. +disabled ccordinate transform for 16-microstepping, because Inkscape sends in 16-microstepping, as well. +removed commands for changing rotStepmodes +fixed some minor bugs + +26.03.2015 v1.3 +updated Accelstepper-Lib +set Microstepping Factor 16 by default. +fixed bug in Pen-position-transformation + +23.04.2014 v1.2 +complete rework of Move-Algorithm, no position errors any more, complete in integer math //it was the hell to debug it... ;-) +optimized timing of Ack-Answer +added command QP "Query Pen", untested, answer might be wrong 0 <--> 1? +optimized Init-String for EBBv13 +optimized some variable types to enhance memory performance +changed enablePenMotor to 6 by default to match it to Spherebot Electronics +changed default maxSpeeds from 1000 to 2000; + +22.04.2014 V1.1 +bugfix Command SM - fixed rounding error in pen and rot-axis + +17.04.2014 V1.0 +initial release diff --git a/Source/Arduino/EggDuino/button.h b/Source/Arduino/EggDuino/button.h new file mode 100644 index 0000000..f387c78 --- /dev/null +++ b/Source/Arduino/EggDuino/button.h @@ -0,0 +1,59 @@ +/* +* button.h +* +* Created: 04.05.2015 21:38:42 +* Author: Yogi +*/ + + +#ifndef __BUTTON_H__ +#define __BUTTON_H__ + +#include + +#define debounceDelay 50 + +typedef void (*ActionCb)(void); + +class Button +{ + +private: + long debounce; + byte state:1; + byte lastState:1; + byte pin; + ActionCb action; + Button( const Button &c ); + Button& operator=( const Button &c ); + +public: + + Button(byte p, ActionCb a): debounce(0), state(1), lastState(1), action(a), pin(p) { + pinMode(pin, INPUT_PULLUP); + } + + void check() { + byte b = digitalRead(pin); + long t = millis(); + + if (b != lastState) { + debounce = t; + } + + if ((t - debounce) > debounceDelay) { + if (b != state) { + state = b; + + if (!state) { + (*action)(); + } + } + } + + lastState = b; + } +}; //button + +#endif //__BUTTON_H__ +