diff --git a/.agent/workflows/build_and_test.md b/.agent/workflows/build_and_test.md index e50fdff..56f743f 100644 --- a/.agent/workflows/build_and_test.md +++ b/.agent/workflows/build_and_test.md @@ -9,14 +9,12 @@ description: Build the project and run tests to ensure code integrity // turbo platformio run -2. Build using Arduino CLI (covers AVR) +3. Build using Arduino CLI (covers AVR) // turbo make all TARGET=arduino:avr:uno -3. Run PlatformIO Unit Tests +4. Run examples (covers all examples) // turbo - platformio test - -4. Run examples (covers one example) - // turbo - platformio ci --lib src --keep-build-dir --board esp32dev examples/BasicStepperDriver + for sketch in examples/*; do + platformio ci --lib src --keep-build-dir --board esp32dev ${sketch} + done diff --git a/README.md b/README.md index 9f26d22..383e738 100644 --- a/README.md +++ b/README.md @@ -18,6 +18,7 @@ Hardware currently supported: - A4988 Stepper Motor Driver up to 1:16 - DRV8825 up to 1:32 - DRV8880 up to 1:16, with current/torque control + - TMC2100 up to 1:16 native (1:256 interpolated) - any other 2-pin stepper via DIR and STEP pins, microstepping up to 1:128 externally set Microstepping @@ -60,6 +61,9 @@ This is suggested wiring for running the examples unmodified. All the pins below - DRV8834/DRV8880 microstep control - M0 - D10 - M1 - D11 + - TMC2100 microstep control + - CFG1 - D10 + - CFG2 - D11 - ~SLEEP (optional) D13 - driver board to motor (this varies from motor to motor, check motor coils schematic). diff --git a/examples/TMC2100/TMC2100.ino b/examples/TMC2100/TMC2100.ino new file mode 100644 index 0000000..245087b --- /dev/null +++ b/examples/TMC2100/TMC2100.ino @@ -0,0 +1,57 @@ +/* + * TMC2100 Stepper Driver Verification + * + * This example uses the TMC2100 driver. + */ +#include +#include "TMC2100.h" + +// Motor steps per revolution. Most steppers are 200 steps or 1.8 degrees/step +#define MOTOR_STEPS 200 +#define RPM 120 + +// Microstepping mode. If you hardwired it to save pins, set to the same value here. +#define MICROSTEPS 16 + +#define DIR 8 +#define STEP 9 +#define SLEEP 13 // optional (just delete SLEEP from constructor if not used) +#define CFG1 10 +#define CFG2 11 + +/* + * Choose one of the sections below that match your hookup + */ + +// Basic connection: only DIR, STEP are connected. +// TMC2100 stepper(MOTOR_STEPS, DIR, STEP); + +// Fully wired: DIR, STEP, SLEEP/ENABLE, CFG1, CFG2 +TMC2100 stepper(MOTOR_STEPS, DIR, STEP, SLEEP, CFG1, CFG2); + +void setup() { + stepper.begin(RPM, MICROSTEPS); + // if using enable/disable on ENABLE pin (active LOW) instead of SLEEP uncomment next line + // stepper.setEnableActiveState(LOW); +} + +void loop() { + // energize coils - the motor will hold position + // stepper.enable(); + + /* + * Moving motor one full revolution using the block command + */ + stepper.rotate(360); + stepper.rotate(-360); + + delay(1000); + + /* + * Moving motor to specific microstep levels + */ + stepper.setMicrostep(1); + stepper.rotate(360); + stepper.setMicrostep(16); + stepper.rotate(360); +} diff --git a/keywords.txt b/keywords.txt index 4247a05..c677e3e 100644 --- a/keywords.txt +++ b/keywords.txt @@ -8,6 +8,7 @@ DRV8825 KEYWORD1 A4988 KEYWORD1 MultiDriver KEYWORD1 SyncDriver KEYWORD1 +TMC2100 KEYWORD1 setMicrostep KEYWORD2 setSpeedProfile KEYWORD2 diff --git a/library.json b/library.json index 5a2649a..161c255 100644 --- a/library.json +++ b/library.json @@ -1,7 +1,7 @@ { "name": "StepperDriver", "version": "1.4.2", - "description": "Control steppers via a driver board providing STEP+DIR like the ones from Pololu. Microstepping is supported. Acceleration is supported. Supported drivers are A4988, DRV8824, DRV8825, DRV8834, DRV8880.", + "description": "Control steppers via a driver board providing STEP+DIR like the ones from Pololu. Microstepping is supported. Acceleration is supported. Supported drivers are A4988, DRV8824, DRV8825, DRV8834, DRV8880, TMC2100.", "repository": { "type": "git", "url": "https://github.com/laurb9/StepperDriver" @@ -12,7 +12,8 @@ "A4988", "DRV8824", "DRV8825", - "DRV8880" + "DRV8880", + "TMC2100" ], "authors": [ { diff --git a/library.properties b/library.properties index b82938b..6bcce77 100644 --- a/library.properties +++ b/library.properties @@ -2,8 +2,8 @@ name=StepperDriver version=1.4.2 author=Laurentiu Badea maintainer=Laurentiu Badea -sentence=A4988, DRV8825 and generic two-pin stepper motor driver library. -paragraph=Control steppers via a driver board providing STEP+DIR like the ones from Pololu. Microstepping is supported. Acceleration is supported. Supported drivers are A4988, DRV8824, DRV8825, DRV8834, DRV8880. +sentence=A4988, DRV8825, TMC2100 and generic two-pin stepper motor driver library. +paragraph=Control steppers via a driver board providing STEP+DIR like the ones from Pololu. Microstepping is supported. Acceleration is supported. Supported drivers are A4988, DRV8824, DRV8825, DRV8834, DRV8880, TMC2100. category=Device Control url=https://github.com/laurb9/StepperDriver architectures=* diff --git a/src/A4988.h b/src/A4988.h index 454e287..cadf2e8 100644 --- a/src/A4988.h +++ b/src/A4988.h @@ -45,7 +45,7 @@ class A4988 : public BasicStepperDriver { A4988(short steps, short dir_pin, short step_pin); A4988(short steps, short dir_pin, short step_pin, short enable_pin); - void begin(float rpm=60, short microsteps=1); + void begin(float rpm=60, short microsteps=1) override; /* * Fully wired. All the necessary control pins for A4988 are connected. */ diff --git a/src/BasicStepperDriver.h b/src/BasicStepperDriver.h index 20c3414..033ddb0 100644 --- a/src/BasicStepperDriver.h +++ b/src/BasicStepperDriver.h @@ -115,7 +115,7 @@ class BasicStepperDriver { /* * Initialize pins, calculate timings etc */ - void begin(float rpm=60, short microsteps=1); + virtual void begin(float rpm=60, short microsteps=1); /* * Set current microstep level, 1=full speed, 32=fine microstepping * Returns new level or previous level if value out of range diff --git a/src/DRV8880.h b/src/DRV8880.h index 15c7722..8bc4e17 100644 --- a/src/DRV8880.h +++ b/src/DRV8880.h @@ -50,7 +50,7 @@ class DRV8880 : public BasicStepperDriver { DRV8880(short steps, short dir_pin, short step_pin, short m0, short m1, short trq0, short trq1); DRV8880(short steps, short dir_pin, short step_pin, short enable_pin, short m0, short m1, short trq0, short trq1); - void begin(float rpm=60, short microsteps=1); + void begin(float rpm=60, short microsteps=1) override; short setMicrostep(short microsteps) override; diff --git a/src/TMC2100.cpp b/src/TMC2100.cpp new file mode 100644 index 0000000..f8bd10d --- /dev/null +++ b/src/TMC2100.cpp @@ -0,0 +1,102 @@ +/* + * TMC2100 - SilentStepStick Stepper Motor Driver + * + * This file may be redistributed under the terms of the MIT license. + * A copy of this license has been included with this distribution in the file LICENSE. + */ +#include "TMC2100.h" + +/* + * Basic connection: only DIR, STEP are connected. + * Microstepping controls should be hardwired. + */ +TMC2100::TMC2100(short steps, short dir_pin, short step_pin) +:BasicStepperDriver(steps, dir_pin, step_pin) +{} + +TMC2100::TMC2100(short steps, short dir_pin, short step_pin, short enable_pin) +:BasicStepperDriver(steps, dir_pin, step_pin, enable_pin) +{} + +/* + * Fully wired. + * All the necessary control pins for TMC2100 are connected. + */ +TMC2100::TMC2100(short steps, short dir_pin, short step_pin, short cf1_pin, short cf2_pin) +:BasicStepperDriver(steps, dir_pin, step_pin), + cf1_pin(cf1_pin), cf2_pin(cf2_pin) +{} + +TMC2100::TMC2100(short steps, short dir_pin, short step_pin, short enable_pin, short cf1_pin, short cf2_pin) +:BasicStepperDriver(steps, dir_pin, step_pin, enable_pin), +cf1_pin(cf1_pin), cf2_pin(cf2_pin) +{} + +void TMC2100::begin(float rpm, short microsteps){ + BasicStepperDriver::begin(rpm, microsteps); + + if (!IS_CONNECTED(cf1_pin) || !IS_CONNECTED(cf2_pin)){ + return; + } + + pinMode(cf1_pin, OUTPUT); + pinMode(cf2_pin, OUTPUT); +} + +/* + * Set microstepping mode (1:divisor) + * Allowed ranges for TMC2100 are 1:1 to 1:16 + * If the control pins are not connected, we recalculate the timing only + */ +short TMC2100::setMicrostep(short microsteps){ + BasicStepperDriver::setMicrostep(microsteps); + + if (!IS_CONNECTED(cf1_pin) || !IS_CONNECTED(cf2_pin)){ + return this->microsteps; + } + + // CFG1 CFG2 Microsteps Mode + // GND GND 1 SpreadCycle + // VIO GND 2 SpreadCycle + // OPEN GND 2 SpreadCycle + // GND VIO 4 SpreadCycle + // VIO VIO 16 SpreadCycle + // OPEN VIO 4 SpreadCycle + // GND OPEN 8 SpreadCycle + // VIO OPEN 16 StealthChop + // OPEN OPEN 16 StealthChop + + switch(this->microsteps){ + case 1: + // GND, GND + pinMode(cf1_pin, OUTPUT); digitalWrite(cf1_pin, LOW); + pinMode(cf2_pin, OUTPUT); digitalWrite(cf2_pin, LOW); + break; + case 2: + // VIO, GND + pinMode(cf1_pin, OUTPUT); digitalWrite(cf1_pin, HIGH); + pinMode(cf2_pin, OUTPUT); digitalWrite(cf2_pin, LOW); + break; + case 4: + // GND, VIO + pinMode(cf1_pin, OUTPUT); digitalWrite(cf1_pin, LOW); + pinMode(cf2_pin, OUTPUT); digitalWrite(cf2_pin, HIGH); + break; + case 8: + // GND, OPEN + pinMode(cf1_pin, OUTPUT); digitalWrite(cf1_pin, LOW); + pinMode(cf2_pin, INPUT); + break; + case 16: + // VIO, OPEN (StealthChop) + pinMode(cf1_pin, OUTPUT); digitalWrite(cf1_pin, HIGH); + pinMode(cf2_pin, INPUT); + break; + } + + return this->microsteps; +} + +short TMC2100::getMaxMicrostep(){ + return TMC2100::MAX_MICROSTEP; +} diff --git a/src/TMC2100.h b/src/TMC2100.h new file mode 100644 index 0000000..27ed754 --- /dev/null +++ b/src/TMC2100.h @@ -0,0 +1,48 @@ +/* + * TMC2100 - SilentStepStick Stepper Motor Driver + * + * This file may be redistributed under the terms of the MIT license. + * A copy of this license has been included with this distribution in the file LICENSE. + */ +#ifndef TMC2100_H +#define TMC2100_H +#include +#include "BasicStepperDriver.h" + +class TMC2100 : public BasicStepperDriver { +protected: + short cf1_pin = PIN_UNCONNECTED; + short cf2_pin = PIN_UNCONNECTED; + // tA STEP minimum, HIGH pulse width (1us) + static const int step_high_min = 1; + // tB STEP minimum, LOW pulse width (1us) + static const int step_low_min = 1; + // wakeup time, nSLEEP inactive to STEP (1000us) + static const int wakeup_time = 1000; + + // Get max microsteps supported by the device + short getMaxMicrostep() override; + +private: + // microstep range (1, 2, 4, 8, 16) + // maximum level controllable by CFG pins is 1/16, TMC2100 can interpolate to 1/256 internally + static const short MAX_MICROSTEP = 16; + +public: + /* + * Basic connection: only DIR, STEP are connected. + * Microstepping controls should be hardwired. + */ + TMC2100(short steps, short dir_pin, short step_pin); + TMC2100(short steps, short dir_pin, short step_pin, short enable_pin); + + void begin(float rpm=60, short microsteps=1) override; + /* + * Fully wired. All the necessary control pins for TMC2100 are connected. + * The CFG pins are used to set microstepping. + */ + TMC2100(short steps, short dir_pin, short step_pin, short cf1_pin, short cf2_pin); + TMC2100(short steps, short dir_pin, short step_pin, short enable_pin, short cf1_pin, short cf2_pin); + short setMicrostep(short microsteps) override; +}; +#endif // TMC2100_H