Skip to content
This repository has been archived by the owner on Feb 4, 2023. It is now read-only.

Latest commit

 

History

History
322 lines (222 loc) · 20.7 KB

README.md

File metadata and controls

322 lines (222 loc) · 20.7 KB

ContinuousStepper_Generic Library

arduino-library-badge GitHub release GitHub contributions welcome GitHub issues

Donate to my libraries using BuyMeACoffee



Table of Contents



Features

This PWM-wrapper library enables you to use Hardware-PWM use PWM to control continuous Stepper Motor.

This library is the wrapper of the following FastPWM libraries to enable you to port your PWM code easily between platforms

  1. RP2040_PWM
  2. AVR_PWM
  3. megaAVR_PWM
  4. ESP32_FastPWM
  5. SAMD_PWM
  6. SAMDUE_PWM
  7. nRF52_PWM
  8. Teensy_PWM
  9. ATtiny_PWM
  10. Dx_PWM
  11. Portenta_H7_PWM
  12. MBED_ContinuousStepper_Generic
  13. nRF52_MBED_PWM
  14. STM32_PWM

The most important feature is they're purely hardware-based PWM channels. Therefore, their operations are not blocked by bad-behaving software functions / tasks.

This important feature is absolutely necessary for mission-critical tasks. These hardware PWM-channels, still work even if other software functions are blocking. Moreover, they are much more precise (certainly depending on clock frequency accuracy) than other software timers using millis() or micros(). That's necessary if you need to control external systems (Servo, etc.) requiring better accuracy.

Being hardware-based PWM, their executions are not blocked by bad-behaving functions / tasks, such as connecting to WiFi, Internet or Blynk services. You can also have many (up to 16) PWM output signals to use.

This non-being-blocked important feature is absolutely necessary for mission-critical tasks.


Why using hardware-based PWM is better

Imagine you have a system with a mission-critical function, controlling a robot or doing something much more important. You normally use a software timer to poll, or even place the function in loop(). But what if another function is blocking the loop() or setup().

So your function might not be executed, and the result would be disastrous.

You'd prefer to have your function called, no matter what happening with other functions (busy loop, bug, etc.).

The correct choice is to use a Hardware Timer with Interrupt to call your function.

These hardware-based PWM channels still work even if other software functions are blocking. Moreover, they are much more precise (certainly depending on clock frequency accuracy) than other software-based PWMs, using millis() or micros(). That's necessary if you need to measure some data requiring better accuracy.

Functions using normal software-based PWMs, relying on loop() and calling millis(), won't work if the loop() or setup() is blocked by certain operation. For example, certain function is blocking while it's connecting to WiFi or some services.


Currently supported Boards

This ContinuousStepper_Generic library currently supports these following boards:

  1. nRF52 boards, such as AdaFruit Feather nRF52832, nRF52840 Express, BlueFruit Sense, Itsy-Bitsy nRF52840 Express, Metro nRF52840 Express, NINA_B302_ublox, NINA_B112_ublox, etc.
  2. SAM DUE
  3. SAMD21
  • Arduino SAMD21: ZERO, MKRs, NANO_33_IOT, etc.
  • Adafruit SAMD21 (M0): ItsyBitsy M0, Feather M0, Feather M0 Express, Metro M0 Express, Circuit Playground Express, Trinket M0, PIRkey, Hallowing M0, Crickit M0, etc.
  • Seeeduino: LoRaWAN, Zero, Femto M0, XIAO M0, Wio GPS Board, etc.
  1. SAMD51
  • Adafruit SAMD51 (M4): Metro M4, Grand Central M4, ItsyBitsy M4, Feather M4 Express, Trellis M4, Metro M4 AirLift Lite, MONSTER M4SK Express, Hallowing M4, etc.
  • Seeeduino: Wio Terminal, Grove UI Wireless
  1. Teensy (4.1, 4.0, 3.6, 3.5, 3,2, 3.1, 3.0, LC) with SPI, SPI1, SPI2

  2. AVR Mega1280, 2560, ADK.

  3. ESP32

  4. ESP8266 To be added

  5. RP2040-based boards, such as RASPBERRY_PI_PICO, ADAFRUIT_FEATHER_RP2040 and GENERIC_RP2040, using Arduino-mbed RP2040 core or Earle Philhower's arduino-pico core with SPI, SPI1

  6. Portenta_H7

  7. Arduino UNO WiFi Rev2, AVR_NANO_EVERY, etc.

  8. Arduino AVR_Dx boards using DxCore

  • AVRDA-based boards (AVR128DA, AVR64DA, AVR32DA, etc.)

  • AVRDB-based boards (AVR128DB, AVR64DB, AVR32DB, etc.)

  1. Seeeduino nRF52840-based boards such as XIAO_NRF52840 and XIAO_NRF52840_SENSE, etc. using Seeed mbed or nRF52 core


Prerequisites

  1. Arduino IDE 1.8.19+ for Arduino. GitHub release
  2. Arduino AVR core 1.8.6+ for Arduino (Use Arduino Board Manager) AVR boards. GitHub release
  3. Teensy core v1.57+ for Teensy (4.1, 4.0, 3.6, 3.5, 3,2, 3.1, 3.0) boards. GitHub release
  4. Arduino SAM DUE core v1.6.12+ for SAM DUE ARM Cortex-M3 boards.
  5. Arduino SAMD core 1.8.13+ for SAMD ARM Cortex-M0+ boards. GitHub release
  6. Adafruit SAMD core 1.7.11+ for SAMD ARM Cortex-M0+ and M4 boards (Nano 33 IoT, etc.). GitHub release
  7. Seeeduino SAMD core 1.8.3+ for SAMD21/SAMD51 boards (XIAO M0, Wio Terminal, etc.). Latest release
  8. Adafruit nRF52 v1.3.0+ for nRF52 boards such as Adafruit NRF52840_FEATHER, NRF52832_FEATHER, NRF52840_FEATHER_SENSE, NRF52840_ITSYBITSY, NRF52840_CIRCUITPLAY, NRF52840_CLUE, NRF52840_METRO, NRF52840_PCA10056, PARTICLE_XENON, NINA_B302_ublox, etc. GitHub release
  9. ESP32 Core 2.0.6+ for ESP32-based boards. Latest release
  10. ESP8266 Core 3.1.1+ for ESP8266-based boards. Latest release. To use ESP8266 core 2.7.1+ for LittleFS.
  11. ArduinoCore-mbed mbed_rp2040, mbed_nano, mbed_portenta core 3.5.4+ for Arduino (Use Arduino Board Manager) Portenta_H7, RP2040-based boards, such as Nano_RP2040_Connect, RASPBERRY_PI_PICO. GitHub release
  12. Earle Philhower's arduino-pico core v2.7.1+ for RP2040-based boards such as RASPBERRY_PI_PICO, ADAFRUIT_FEATHER_RP2040 and GENERIC_RP2040, etc. GitHub release
  13. Arduino megaAVR core 1.8.7+ for Arduino megaAVR boards such as Arduino UNO WiFi Rev2, AVR_NANO_EVERY, etc.. GitHub release
  14. SpenceKonde DxCore core 1.5.3+ for Arduino AVRDx boards. GitHub release. Follow DxCore Installation.
  15. Seeeduino nRF52 core 1.1.0+ for Seeed nRF52840-based boards such as Seeed_XIAO_NRF52840 and Seeed_XIAO_NRF52840_SENSE. GitHub release
  16. Seeeduino mbed core 2.9.0+ for Seeed nRF52840-based boards such as SEEED_XIAO_NRF52840 and SEEED_XIAO_NRF52840_SENSE. GitHub release

  1. RP2040_PWM library v1.4.1+. To install. check arduino-library-badge
  2. AVR_PWM library v1.0.1+. To install. check arduino-library-badge
  3. megaAVR_PWM library v1.0.1+. To install. check arduino-library-badge
  4. ESP32_FastPWM library v1.0.1+. To install. check arduino-library-badge
  5. SAMD_PWM library v1.0.1+. To install. check arduino-library-badge
  6. SAMDUE_PWM library v1.0.1+. To install. check arduino-library-badge
  7. nRF52_PWM library v1.0.1+. To install. check arduino-library-badge
  8. Teensy_PWM library v1.1.1+. To install. check arduino-library-badge
  9. ATtiny_PWM library v1.0.1+. To install. check arduino-library-badge
  10. Dx_PWM library v1.1.1+. To install. check arduino-library-badge
  11. Portenta_H7_PWM library v2.0.3+. To install. check arduino-library-badge
  12. MBED_RP2040_PWM library v1.0.1+. To install. check arduino-library-badge
  13. nRF52_MBED_PWM library v1.0.3+. To install. check arduino-library-badge
  14. STM32_PWM library v1.0.1+. To install. check arduino-library-badge


Installation

Use Arduino Library Manager

The best and easiest way is to use Arduino Library Manager. Search for ContinuousStepper_Generic, then select / install the latest version. You can also use this link arduino-library-badge for more detailed instructions.

Manual Install

Another way to install is to:

  1. Navigate to ContinuousStepper_Generic page.
  2. Download the latest release ContinuousStepper_Generic-main.zip.
  3. Extract the zip file to ContinuousStepper_Generic-main directory
  4. Copy whole ContinuousStepper_Generic-main folder to Arduino libraries' directory such as ~/Arduino/libraries/.

VS Code & PlatformIO

  1. Install VS Code
  2. Install PlatformIO
  3. Install ContinuousStepper_Generic library by using Library Manager. Search for ContinuousStepper_Generic in Platform.io Author's Libraries
  4. Use included platformio.ini file from examples to ensure that all dependent libraries will installed automatically. Please visit documentation for the other options and examples at Project Configuration File


Examples:

  1. ATtiny_PWM_StepperControl
  2. AVR_PWM_StepperControl
  3. Dx_PWM_StepperControl
  4. ESP32_PWM_StepperControl
  5. MBED_RP2040_PWM_StepperControl
  6. megaAVR_PWM_StepperControl
  7. nRF52_MBED_PWM_StepperControl
  8. nRF52_PWM_StepperControl
  9. Portenta_H7_PWM_StepperControl
  10. RP2040_PWM_StepperControl
  11. SAMD_PWM_StepperControl
  12. SAMDUE_PWM_StepperControl
  13. STM32_PWM_StepperControl
  14. Teensy_PWM_StepperControl


// Use with Stepper-Motor driver, such as TMC2209
#define _PWM_LOGLEVEL_ 1
#if ( defined(ARDUINO_ARCH_RP2040) || defined(ARDUINO_RASPBERRY_PI_PICO) || defined(ARDUINO_ADAFRUIT_FEATHER_RP2040) || \
defined(ARDUINO_GENERIC_RP2040) ) && !defined(ARDUINO_ARCH_MBED)
#if(_PWM_LOGLEVEL_>3)
#warning USING_RP2040_PWM
#endif
#else
#error This code is intended to run on the RP2040 arduino-pico platform! Please check your Tools->Board setting.
#endif
#include "ContinuousStepper_Generic.h"
RP2040_PWM* stepper;
#define STEP_PIN 8
#define DIR_PIN 9
// The Stepper RPM will be ( speed * 60 ) / steps-per-rev
// For example, 28BYJ-48 Stepper Motor (https://lastminuteengineers.com/28byj48-stepper-motor-arduino-tutorial/) has 32 Steps/Rev
// Speed = 640 Hz => Stepper RPM = (640 * 60 / 32) = 1200 RPM
void setSpeed(int speed)
{
if (speed == 0)
{
// Use DC = 0 to stop stepper
stepper->setPWM(STEP_PIN, 500, 0);
}
else
{
// Set the frequency of the PWM output and a duty cycle of 50%
digitalWrite(DIR_PIN, (speed < 0));
stepper->setPWM(STEP_PIN, abs(speed), 50);
}
}
void setup()
{
pinMode(DIR_PIN, OUTPUT);
Serial.begin(115200);
while (!Serial && millis() < 5000);
delay(100);
Serial.print(F("\nStarting RP2040_PWM_StepperControl on "));
Serial.println(BOARD_NAME);
Serial.println(RP2040_PWM_VERSION);
Serial.println(CONTINUOUS_STEPPER_GENERIC_VERSION);
// Create PWM object and passed just a random frequency of 500 in it
// The duty cycle is how you turn the motor on and off
stepper = new RP2040_PWM(STEP_PIN, 500, 0);
}
void loop()
{
// The Stepper RPM will be ( speed * 60 ) / steps-per-rev
setSpeed(1000);
delay(3000);
// Stop before reversing
setSpeed(0);
delay(3000);
// Reversing
setSpeed(-500);
delay(3000);
// Stop before reversing
setSpeed(0);
delay(3000);
}



Debug

Debug is enabled by default on Serial.

You can also change the debugging level _PWM_LOGLEVEL_ from 0 to 4

// Don't define _PWM_LOGLEVEL_ > 0. Only for special ISR debugging only. Can hang the system.
#define _PWM_LOGLEVEL_     0

Troubleshooting

If you get compilation errors, more often than not, you may need to install a newer version of the core for Arduino boards.

Sometimes, the library will only work if you update the board core to the latest version because I am using newly added functions.



Issues

Submit issues to: ContinuousStepper_Generic issues


TO DO

  1. Search for bug and improvement
  2. Similar features for remaining or new Arduino boards

DONE

  1. Initial coding to use PWM to control continuous Stepper Motor. Check Using PWM to step a stepper driver #16
  2. Use allman astyle and add utils


Contributions and Thanks

Many thanks for everyone for bug reporting, new feature suggesting, testing and contributing to the development of this library.

  1. Thanks to Paul van Dinther for proposing new way to use PWM to drive Stepper-Motor in Using PWM to step a stepper driver #16, leading to this library
dinther
Paul van Dinther


Contributing

If you want to contribute to this project:

  • Report bugs and errors
  • Ask for enhancements
  • Create issues and pull requests
  • Tell other people about this library

License

  • The library is licensed under MIT

Copyright

Copyright 2023- Khoi Hoang