Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
name: Build and test

on:
pull_request:
push:
branches: [main]

permissions:
contents: read

jobs:
linux:
runs-on: ubuntu-24.04
steps:
- uses: actions/checkout@v4
- name: Configure
run: cmake -S . -B build -DCMAKE_BUILD_TYPE=Debug
- name: Build
run: cmake --build build --parallel 2
- name: Test
run: ctest --test-dir build --output-on-failure
7 changes: 7 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
/build/
/firmware/esp32/build/
/firmware/esp32/sdkconfig
/firmware/esp32/sdkconfig.old
/firmware/esp32/dependencies.lock
/firmware/esp32/managed_components/
.DS_Store
17 changes: 17 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
cmake_minimum_required(VERSION 3.20)

if(ESP_PLATFORM)
idf_component_register(
SRC_DIRS src src/pd_esp32 src/fas_ramp src/log2 src/fas_queue
INCLUDE_DIRS src
REQUIRES esp_driver_gpio esp_driver_rmt esp_driver_mcpwm esp_driver_pcnt esp_driver_i2s
)
return()
endif()

project(StepperMotionWorkbench LANGUAGES CXX)

include(CTest)
if(BUILD_TESTING)
add_subdirectory(tests/host)
endif()
16 changes: 16 additions & 0 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# Contributing

Stepper Motion Workbench welcomes fixes and improvements to its motion
planner, command queue, drivers, and examples.

1. Create a topic branch from the default branch and keep the pull request
focused on one change.
2. Follow the surrounding C++ style. Add a host test for changes to planning
or queue behavior when that behavior can be simulated.
3. Run the host build and CTest commands in [README.md](README.md).
4. For changes to a hardware backend, state the target and SDK version. For
hardware runs, include the driver and board, test inputs, and observed
pulse timing or position.

The project is distributed under [LICENSE](LICENSE). Keep the source and
third-party attributions in [NOTICE](NOTICE) intact.
21 changes: 21 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2020 J.Kiemes

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.
35 changes: 35 additions & 0 deletions NOTICE
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
Stepper Motion Workbench
========================

The motion library, Arduino examples, and host test sources were adapted
from FastAccelStepper by Jochen Kiemes and contributors.
Source: https://github.com/gin66/FastAccelStepper
Imported commit: 8e55103cf1af98d89be1710ee220844c0cd178f4
Copyright (c) 2020 J.Kiemes
License: MIT (see LICENSE for the full terms)

The original project credits the following contributions:

- ixil: ATmega2560 support
- gagank1: ESP-IDF integration
- clazarowitz: Atmel SAM Due port
- HeldeReis: ESP32-S3 port
- DaAwesomeP: ATmega168/168P/328 support
- turley: stepper count initialization
- GarmischWg: ESP32-S3 RMT support
- SHWotever: AVR direction pin handling
- HalfVoxel: documentation and preprocessor fixes
- pvginkel: engine initialization race fix
- freelancer1845: ESP-IDF integration documentation
- viteo: pulse counter direction pin support
- Magnus: Pico SDK support and implementation suggestions
- Sam W: SAM3X queue allocation fix
- drayde: ESP-IDF 6 RMT header fix
- Loriland: Microchip SAMD51 port
- seifemad (ARDUTECH0): experimental Teensy 4.0/4.1 port and bring-up

The Teensy 4.x backend adapts QuadTimer register sequencing from TMR.h
and dynamic-pin GPIO handling from stepperbase.h/.cpp in TeensyStep4.
Source: https://github.com/luni64/TeensyStep4
Copyright (c) 2022 luni64
License: MIT (see licenses/TeensyStep4-MIT.txt for the full terms)
70 changes: 68 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,68 @@
# stepper-motion-workbench
Build and validate embedded STEP/DIR motion control.
# Stepper Motion Workbench

Stepper Motion Workbench is an embedded C++ library for driving stepper drivers
through STEP and DIR signals. It queues pulse commands ahead of the timer
peripherals and provides acceleration ramps, timed segments, and coordinated
multi-axis movement.

## Host build

On Linux with GCC or Clang, install CMake 3.20 or newer and a C++17 compiler.

```sh
git clone https://github.com/cpp-hardtech/stepper-motion-workbench.git
cd stepper-motion-workbench
cmake -S . -B build -DCMAKE_BUILD_TYPE=Debug
cmake --build build --parallel
ctest --test-dir build --output-on-failure
```

The tests use a simulated stepper queue. They exercise ramp calculations,
position tracking, timed moves, coordinated paths, and the ESP32 I2S and RMT
pulse encoders without connecting a motor. To run the timed-move example
against the simulator and see its output, use
`ctest --test-dir build -V -R '^test_29$'`.

## ESP32 quick start

The [ESP-IDF example](firmware/esp32/) builds against this checkout as a local
component. Use an ESP-IDF 5.x environment, version 5.3 or newer, with the
`esp32` target tools installed.

```sh
cd firmware/esp32
idf.py set-target esp32
idf.py build
idf.py -p PORT flash monitor
```

The example commands 200 steps with GPIO 17 for STEP, GPIO 18 for DIR, and
GPIO 26 for active-low ENABLE. Adjust the pins and enable polarity in
[main.cpp](firmware/esp32/main/main.cpp) for your board and driver. Connect the
signals and common ground to a compatible external stepper driver. The serial
monitor reports the commanded position from the step queue.

To use the library in an existing ESP-IDF application, add the checkout to
`EXTRA_COMPONENT_DIRS` before including the ESP-IDF project CMake file. In an
Arduino project, install this checkout as a local library named
`StepperMotionWorkbench` and open [UsageExample](examples/UsageExample/UsageExample.ino).

## API

- [StepperMotionEngine](src/StepperMotionEngine.h) binds a STEP output to a
driver queue and manages steppers.
- [StepperMotion](src/StepperMotion.h) sets speed and acceleration, starts
point-to-point and continuous moves, and queues step or pause commands.
- [FasTimed](src/FasTimed.h) queues coordinated segments with caller-supplied
timing. [FasNAxis](src/FasNAxis.h) plans coordinated motion along waypoints.
- [Examples](examples/) include single-axis, timed, and multi-axis sketches.
Backend availability and usable pins depend on the board and SDK.

## Contributing

See [CONTRIBUTING.md](CONTRIBUTING.md).

## License

MIT. See [LICENSE](LICENSE) and [NOTICE](NOTICE) for the license and source
attribution, including the Teensy timer code.
86 changes: 86 additions & 0 deletions examples/ExternalCall/ExternalCall.ino
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
#include "StepperMotion.h"

#ifdef SIMULATOR
#include <avr/sleep.h>
#endif
#if defined(ARDUINO_ARCH_ESP32)
#include <esp_task_wdt.h>
#endif

// As in StepperDemo for Motor 1 on AVR
#define dirPinStepper1 5
#define enablePinStepper1 6
#define stepPinStepper1 9 // OC1A in case of AVR

// As in StepperDemo for Motor 2 on AVR
#define dirPinStepper2 7
#define enablePinStepper2 8
#define stepPinStepper2 10 // OC1B in case of AVR

StepperMotionEngine engine = StepperMotionEngine();
StepperMotion *stepper1 = NULL;
StepperMotion *stepper2 = NULL;

void setup() {
Serial.begin(115200);
Serial.println("External call test");
Serial.println("Stepper 1 uses external dir pin");
Serial.println("Stepper 2 uses direct dir pin");

engine.init();
engine.setExternalCallForPin(setExternalPin);
stepper1 = engine.stepperConnectToPin(stepPinStepper1);
if (stepper1) {
stepper1->setDirectionPin(dirPinStepper1 | PIN_EXTERNAL_FLAG);
stepper1->setEnablePin(enablePinStepper1);
stepper1->setAutoEnable(true);

stepper1->setSpeedInUs(10000); // the parameter is us/step !!!
stepper1->setAcceleration(1000);
}

stepper2 = engine.stepperConnectToPin(stepPinStepper2);
if (stepper2) {
stepper2->setDirectionPin(dirPinStepper2);
stepper2->setEnablePin(enablePinStepper2);
stepper2->setAutoEnable(true);

stepper2->setSpeedInUs(10000); // the parameter is us/step !!!
stepper2->setAcceleration(1000);
}
}

bool setExternalPin(uint8_t pin, uint8_t value) {
// This example returns the previous value of the output.
// Consequently, StepperMotion needs to call setExternalPin twice
// in order to successfully change the output value.
pin = pin & ~PIN_EXTERNAL_FLAG;
pinMode(pin, OUTPUT);
bool oldValue = digitalRead(pin);
digitalWrite(pin, value);
return oldValue;
}

void loop() {
// Blocking moves are used.
if (stepper1) {
stepper1->move(1000, true);
stepper1->move(-900, true);
stepper1->move(500, true);
stepper1->move(-450, true);
stepper1->move(40, true);
}
if (stepper2) {
stepper2->move(1000, true);
stepper2->move(-900, true);
stepper2->move(500, true);
stepper2->move(-450, true);
stepper2->move(40, true);
}

#ifdef SIMULATOR
delay(1000);
noInterrupts();
sleep_cpu();
#endif
}
Loading
Loading