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

Commit

Permalink
v1.0.1 to add PWM_StepperControl example
Browse files Browse the repository at this point in the history
### 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.
  • Loading branch information
khoih-prog committed Jan 22, 2023
1 parent 4c44c83 commit 97018ae
Show file tree
Hide file tree
Showing 9 changed files with 121 additions and 30 deletions.
13 changes: 10 additions & 3 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,28 +10,33 @@ 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
* Anything that might be relevant in your opinion, such as:
* 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
Expand Down Expand Up @@ -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
```
5 changes: 5 additions & 0 deletions changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,18 @@
## Table of Contents

* [Changelog](#changelog)
* [Releases v1.0.1](#Releases-v101)
* [Initial Releases v1.0.0](#Initial-Releases-v100)

---
---

## 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)
Expand Down
76 changes: 76 additions & 0 deletions examples/PWM_StepperControl/PWM_StepperControl.ino
Original file line number Diff line number Diff line change
@@ -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_RP2040_PWM.h>

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);
}
2 changes: 1 addition & 1 deletion library.json
Original file line number Diff line number Diff line change
@@ -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":
Expand Down
2 changes: 1 addition & 1 deletion library.properties
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
name=MBED_RP2040_PWM
version=1.0.0
version=1.0.1
author=Khoi Hoang <khoih.prog@gmail.com>
maintainer=Khoi Hoang <khoih.prog@gmail.com>
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.
Expand Down
3 changes: 2 additions & 1 deletion src/MBED_RP2040_PWM.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
9 changes: 5 additions & 4 deletions src/MBED_RP2040_PWM.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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


Expand Down
38 changes: 19 additions & 19 deletions src/MBED_RP2040_PWM_Impl.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -32,9 +33,9 @@ bool isValidPWMPin(const pin_size_t& pin)
{
return true;
}

PWM_LOGERROR1("Not PWM pin = ", pin);

return false;
}

Expand All @@ -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;
}

Expand All @@ -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;
}

Expand Down
3 changes: 2 additions & 1 deletion src/PWM_Generic_Debug.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 97018ae

Please sign in to comment.