From 97018ae974e1f1dab9e77b4967ee7726b60b74c1 Mon Sep 17 00:00:00 2001 From: Khoi Hoang <57012152+khoih-prog@users.noreply.github.com> Date: Sun, 22 Jan 2023 00:29:35 -0500 Subject: [PATCH] v1.0.1 to add `PWM_StepperControl` example ### Releases v1.0.1 1. Add example [PWM_StepperControl](https://github.com/khoih-prog/MBED_RP2040_PWM/examples/PWM_StepperControl) to demo how to control Stepper Motor using PWM. --- CONTRIBUTING.md | 13 +++- changelog.md | 5 ++ .../PWM_StepperControl/PWM_StepperControl.ino | 76 +++++++++++++++++++ library.json | 2 +- library.properties | 2 +- src/MBED_RP2040_PWM.h | 3 +- src/MBED_RP2040_PWM.hpp | 9 ++- src/MBED_RP2040_PWM_Impl.h | 38 +++++----- src/PWM_Generic_Debug.h | 3 +- 9 files changed, 121 insertions(+), 30 deletions(-) create mode 100644 examples/PWM_StepperControl/PWM_StepperControl.ino diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md index a098715..ac6d2d6 100644 --- a/CONTRIBUTING.md +++ b/CONTRIBUTING.md @@ -10,12 +10,14 @@ However, before reporting a bug please check through the following: If you don't find anything, please [open a new issue](https://github.com/khoih-prog/MBED_RP2040_PWM/issues/new). +--- + ### How to submit a bug report Please ensure to specify the following: * Arduino IDE version (e.g. 1.8.19) or Platform.io version -* `ArduinoCore-mbed` Core Version (e.g. `ArduinoCore-mbed` mbed_nano core v3.4.1) +* `ArduinoCore-mbed` Core Version (e.g. `ArduinoCore-mbed` mbed_nano core v3.5.4) * `RP2040` Board type (e.g. Nano_RP2040_Connect, RaspberryPi Pico,etc.) * Contextual information (e.g. what you were trying to achieve) * Simplest possible steps to reproduce @@ -23,15 +25,18 @@ Please ensure to specify the following: * Operating system (Windows, Ubuntu, etc.) and the output of `uname -a` * Network configuration +Please be educated, civilized and constructive as you've always been. Disrespective posts against [GitHub Code of Conduct](https://docs.github.com/en/site-policy/github-terms/github-event-code-of-conduct) will be ignored and deleted. + +--- ### Example ``` Arduino IDE version: 1.8.19 -`ArduinoCore-mbed` mbed_nano core v3.4.1 +`ArduinoCore-mbed` mbed_nano core v3.5.4 Nano_RP2040_Connect OS: Ubuntu 20.04 LTS -Linux xy-Inspiron-3593 5.15.0-52-generic #58~20.04.1-Ubuntu SMP Thu Oct 13 13:09:46 UTC 2022 x86_64 x86_64 x86_64 GNU/Linux +Linux xy-Inspiron-3593 5.15.0-58-generic #64~20.04.1-Ubuntu SMP Fri Jan 6 16:42:31 UTC 2023 x86_64 x86_64 x86_64 GNU/Linux Context: I encountered a crash while using this library @@ -70,4 +75,6 @@ xy@xy-Inspiron-3593:~/Arduino/xy/MBED_RP2040_PWM_GitHub$ ``` xy@xy-Inspiron-3593:~/Arduino/xy/MBED_RP2040_PWM_GitHub$ bash utils/restyle.sh + + ``` diff --git a/changelog.md b/changelog.md index 0cd8add..418402b 100644 --- a/changelog.md +++ b/changelog.md @@ -17,6 +17,7 @@ ## Table of Contents * [Changelog](#changelog) + * [Releases v1.0.1](#Releases-v101) * [Initial Releases v1.0.0](#Initial-Releases-v100) --- @@ -24,6 +25,10 @@ ## Changelog +### Releases v1.0.1 + +1. Add example [PWM_StepperControl](https://github.com/khoih-prog/MBED_RP2040_PWM/examples/PWM_StepperControl) to demo how to control Stepper Motor using PWM. Check [Using PWM to step a stepper driver #16](https://github.com/khoih-prog/RP2040_PWM/issues/16) + ### Initial Releases v1.0.0 1. Initial coding to support RP2040-based boards such as **Nano_RP2040_Connect and RASPBERRY_PI_PICO**, etc. using [**Arduino-mbed** core](https://github.com/arduino/ArduinoCore-mbed) diff --git a/examples/PWM_StepperControl/PWM_StepperControl.ino b/examples/PWM_StepperControl/PWM_StepperControl.ino new file mode 100644 index 0000000..9e0ac59 --- /dev/null +++ b/examples/PWM_StepperControl/PWM_StepperControl.ino @@ -0,0 +1,76 @@ +/**************************************************************************************************************************** + PWM_StepperControl.ino + For RP2040 boards + Written by Khoi Hoang + + Built by Khoi Hoang https://github.com/khoih-prog/MBED_RP2040_PWM + Licensed under MIT license + + Credits of Paul van Dinther (https://github.com/dinther). Check https://github.com/khoih-prog/RP2040_PWM/issues/16 +*****************************************************************************************************************************/ + +// Use with Stepper-Motor driver, such as TMC2209 + +#if !( defined(ARDUINO_NANO_RP2040_CONNECT) || defined(ARDUINO_RASPBERRY_PI_PICO) || defined(ARDUINO_ADAFRUIT_FEATHER_RP2040) || \ + defined(ARDUINO_GENERIC_RP2040) ) && defined(ARDUINO_ARCH_MBED) + #error This code is intended to run on the MBED RP2040 platform! Please check your Tools->Board setting. +#endif + +#define _PWM_LOGLEVEL_ 1 + +#include + +mbed::PwmOut* stepper = nullptr; + +#define STEP_PIN 8 +#define DIR_PIN 9 + +void setSpeed(int speed) +{ + if (speed == 0) + { + // Use DC = 0 to stop stepper + setPWM(stepper, STEP_PIN, 500, 0); + } + else + { + // Set the frequency of the PWM output and a duty cycle of 50% + digitalWrite(DIR_PIN, (speed < 0)); + setPWM(stepper, STEP_PIN, abs(speed), 50); + } +} + +void setup() +{ + pinMode(DIR_PIN, OUTPUT); + + Serial.begin(115200); + + while (!Serial && millis() < 5000); + + delay(100); + + Serial.print(F("\nStarting PWM_StepperControl on ")); + Serial.println(BOARD_NAME); + Serial.println(MBED_RP2040_PWM_VERSION); + + //setSpeed(0); +} + +void loop() +{ + setSpeed(1000); + delay(3000); + + // Stop before reversing + setSpeed(0); + delay(3000); + + // Reversing + setSpeed(-500); + delay(3000); + + // Stop before reversing + setSpeed(0); + delay(3000); +} diff --git a/library.json b/library.json index ff6a17c..ff98e6b 100644 --- a/library.json +++ b/library.json @@ -1,6 +1,6 @@ { "name": "MBED_RP2040_PWM", - "version": "1.0.0", + "version": "1.0.1", "keywords": "timing, device, control, timer, pwm, interrupt, hardware-pwm, hardware-timer, mission-critical, accuracy, non-blocking, mbed, mbed-rp2040, mbed-nano, rp2040, nano-rp2040-connect, raspberry-pi-pico, precise, hardware", "description": "This library enables you to use Hardware Timers on RP2040-based RP2040 board to create and output PWM to pins. These PWM channels, using RP2040 Hardware-PWM channels, still work even if other functions are blocking. Moreover, they are much more precise (certainly depending on clock frequency accuracy) than other software ISR-based PWM, using millis(), micros() or Timer Interrupt. This important feature is absolutely necessary for mission-critical tasks. You can start, stop, change and restore the settings of any PWM channel on-the-fly", "authors": diff --git a/library.properties b/library.properties index bb468ff..5da6cad 100644 --- a/library.properties +++ b/library.properties @@ -1,5 +1,5 @@ name=MBED_RP2040_PWM -version=1.0.0 +version=1.0.1 author=Khoi Hoang maintainer=Khoi Hoang sentence=This library enables you to use Hardware-based PWM to create and output PWM to pins on RP2040 board to create and output PWM to pins. diff --git a/src/MBED_RP2040_PWM.h b/src/MBED_RP2040_PWM.h index 99f5a70..e0c39b6 100644 --- a/src/MBED_RP2040_PWM.h +++ b/src/MBED_RP2040_PWM.h @@ -6,11 +6,12 @@ Built by Khoi Hoang https://github.com/khoih-prog/MBED_RP2040_PWM Licensed under MIT license - Version: 1.0.0 + Version: 1.0.1 Version Modified By Date Comments ------- ----------- ---------- ----------- 1.0.0 K.Hoang 09/02/2022 Initial coding for RP2040 using ArduinoCore-mbed mbed_rp2040 core + 1.0.1 K.Hoang 21/01/2023 Add `PWM_StepperControl` example *****************************************************************************************************************************/ #pragma once diff --git a/src/MBED_RP2040_PWM.hpp b/src/MBED_RP2040_PWM.hpp index 2a9b20d..11ab8d9 100644 --- a/src/MBED_RP2040_PWM.hpp +++ b/src/MBED_RP2040_PWM.hpp @@ -6,11 +6,12 @@ Built by Khoi Hoang https://github.com/khoih-prog/MBED_RP2040_PWM Licensed under MIT license - Version: 1.0.0 + Version: 1.0.1 Version Modified By Date Comments ------- ----------- ---------- ----------- 1.0.0 K.Hoang 09/02/2022 Initial coding for RP2040 using ArduinoCore-mbed mbed_rp2040 core + 1.0.1 K.Hoang 21/01/2023 Add `PWM_StepperControl` example *****************************************************************************************************************************/ #pragma once @@ -24,13 +25,13 @@ #endif #ifndef MBED_RP2040_PWM_VERSION - #define MBED_RP2040_PWM_VERSION "MBED_RP2040_PWM v1.0.0" + #define MBED_RP2040_PWM_VERSION "MBED_RP2040_PWM v1.0.1" #define MBED_RP2040_PWM_VERSION_MAJOR 1 #define MBED_RP2040_PWM_VERSION_MINOR 0 - #define MBED_RP2040_PWM_VERSION_PATCH 0 + #define MBED_RP2040_PWM_VERSION_PATCH 1 - #define MBED_RP2040_PWM_VERSION_INT 1000000 + #define MBED_RP2040_PWM_VERSION_INT 1000001 #endif diff --git a/src/MBED_RP2040_PWM_Impl.h b/src/MBED_RP2040_PWM_Impl.h index 503a787..72b3f9c 100644 --- a/src/MBED_RP2040_PWM_Impl.h +++ b/src/MBED_RP2040_PWM_Impl.h @@ -6,11 +6,12 @@ Built by Khoi Hoang https://github.com/khoih-prog/MBED_RP2040_PWM Licensed under MIT license - Version: 1.0.0 + Version: 1.0.1 Version Modified By Date Comments ------- ----------- ---------- ----------- 1.0.0 K.Hoang 09/02/2022 Initial coding for RP2040 using ArduinoCore-mbed mbed_rp2040 core + 1.0.1 K.Hoang 21/01/2023 Add `PWM_StepperControl` example *****************************************************************************************************************************/ #ifndef MBED_RP2040_PWM_IMPL_H @@ -32,9 +33,9 @@ bool isValidPWMPin(const pin_size_t& pin) { return true; } - + PWM_LOGERROR1("Not PWM pin = ", pin); - + return false; } @@ -43,7 +44,7 @@ bool isValidPWMDutyCycle(const pin_size_t& pin, const float& dutyCycle) if ( (dutyCycle < 0.0f) || (dutyCycle > 100.0f) ) { PWM_LOGERROR3("Bad dutyCycle = ", dutyCycle, ", pin = ", pin); - + return false; } @@ -59,54 +60,53 @@ bool isValidPWMFreq(const pin_size_t& pin, const float& frequency) return false; } - + return true; } bool isValidPWMSettings(const pin_size_t& pin, const float& frequency, const float& dutyCycle) { if ( !isValidPWMPin(pin) || !isValidPWMFreq(pin, frequency) || !isValidPWMDutyCycle(pin, dutyCycle) ) - { + { return false; - } - + } + return true; } // dutyCycle from 0.0f to 100.0f mbed::PwmOut* setPWM(mbed::PwmOut* &pwm, const pin_size_t& pin, const float& frequency, const float& dutyCycle) { - PWM_LOGDEBUG7("Freq = ", frequency, ", \tDutyCycle = ", dutyCycle, ", \tDutyCycle % = ", dutyCycle / 100, ", \tPin = ", - pin); - + PWM_LOGDEBUG7("Freq = ", frequency, ", \tDutyCycle = ", dutyCycle, ", \tDutyCycle % = ", dutyCycle / 100, ", \tPin = ", pin); + if ( !isValidPWMSettings(pin, frequency, dutyCycle) ) - { + { return NULL; } float percent = dutyCycle / 100.0f; - + if (digitalPinToPwm(pin) == NULL) { PWM_LOGDEBUG("New pwm"); - + pwm = new mbed::PwmOut(digitalPinToPinName(pin)); - + digitalPinToPwm(pin) = pwm; - pwm->period_us( 1000000.0f / frequency ); + pwm->period_us( 1000000.0f/frequency ); pwm->write(percent); } else if (pwm && (digitalPinToPwm(pin) == pwm) ) { PWM_LOGDEBUG("Use existing pwm"); - - pwm->period_us( 1000000.0f / frequency ); + + pwm->period_us( 1000000.0f/frequency ); pwm->write(percent); } - + return pwm; } diff --git a/src/PWM_Generic_Debug.h b/src/PWM_Generic_Debug.h index ea6ae93..72c3179 100644 --- a/src/PWM_Generic_Debug.h +++ b/src/PWM_Generic_Debug.h @@ -6,11 +6,12 @@ Built by Khoi Hoang https://github.com/khoih-prog/RP2040_PWM Licensed under MIT license - Version: 1.0.0 + Version: 1.0.1 Version Modified By Date Comments ------- ----------- ---------- ----------- 1.0.0 K.Hoang 09/02/2022 Initial coding for RP2040 using ArduinoCore-mbed mbed_rp2040 core + 1.0.1 K.Hoang 21/01/2023 Add `PWM_StepperControl` example *****************************************************************************************************************************/ #pragma once