diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md index e1dab16..c8472c3 100644 --- a/CONTRIBUTING.md +++ b/CONTRIBUTING.md @@ -29,7 +29,7 @@ Please ensure to specify the following: Arduino IDE version: 1.8.19 Arduino NRF52 Core Version 1.3.0 OS: Ubuntu 20.04 LTS -Linux xy-Inspiron-3593 5.4.0-100-generic #113-Ubuntu SMP Thu Feb 3 18:43:29 UTC 2022 x86_64 x86_64 x86_64 GNU/Linux +Linux xy-Inspiron-3593 5.13.0-40-generic #45~20.04.1-Ubuntu SMP Mon Apr 4 09:38:31 UTC 2022 x86_64 x86_64 x86_64 GNU/Linux Context: I encountered a crash while trying to use the Timer Interrupt. diff --git a/README.md b/README.md index a652bae..bc7d726 100644 --- a/README.md +++ b/README.md @@ -232,7 +232,7 @@ https://github.com/khoih-prog/NRF52_ISR_Servo/blob/4260125278f38956f8fdeaca58249 ``` Starting NRF52_MultipleRandomServos on NRF52840_FEATHER -NRF52_ISR_Servo v1.1.0 +NRF52_ISR_Servo v1.2.0 Setup OK Servo index = 0 Setup OK Servo index = 1 Setup OK Servo index = 2 @@ -305,7 +305,7 @@ Servos sweeps from 0-180 degree ``` Starting NRF52_MultipleRandomServos on NRF52840_ITSYBITSY -NRF52_ISR_Servo v1.1.0 +NRF52_ISR_Servo v1.2.0 Setup OK Servo index = 0 Setup OK Servo index = 1 Setup OK Servo index = 2 @@ -422,7 +422,7 @@ Submit issues to: [NRF52_ISR_Servo issues](https://github.com/khoih-prog/NRF52_I 6. Add example [multiFileProject](examples/multiFileProject) to demo for multiple-file project 7. Optimize code by using passing by `reference` instead of by `value` 8. Add support to `Sparkfun Pro nRF52840 Mini` - +9. Permit using servos with different pulse ranges simultaneously --- --- diff --git a/changelog.md b/changelog.md index 9b3c359..768ab58 100644 --- a/changelog.md +++ b/changelog.md @@ -12,6 +12,7 @@ ## Table of Contents * [Changelog](#changelog) + * [Releases v1.2.0](#releases-v120) * [Releases v1.1.0](#releases-v110) * [Releases v1.0.0](#releases-v100) @@ -21,6 +22,11 @@ ## Changelog +### Releases v1.2.0 + +1. Permit using servos with different pulse ranges simultaneously. Check [Fix Bug #5 : Permit using servos with different pulse ranges simultaneously #4](https://github.com/khoih-prog/RP2040_ISR_Servo/pull/4) +2. Delete left-over `cpp` + ### Releases v1.1.0 1. Convert to `h-only` style. diff --git a/examples/nRF52_MultipleRandomServos/nRF52_MultipleRandomServos.ino b/examples/nRF52_MultipleRandomServos/nRF52_MultipleRandomServos.ino index c4aa717..11931c6 100644 --- a/examples/nRF52_MultipleRandomServos/nRF52_MultipleRandomServos.ino +++ b/examples/nRF52_MultipleRandomServos/nRF52_MultipleRandomServos.ino @@ -52,7 +52,6 @@ #define TIMER_INTERRUPT_DEBUG 4 #define ISR_SERVO_DEBUG 4 -// To be included only in main(), .ino with setup() to avoid `Multiple Definitions` Linker Error #include "NRF52_ISR_Servo.h" // Published values for SG90 servos; adjust if needed @@ -68,7 +67,7 @@ typedef struct { - int8_t servoIndex; + int servoIndex; uint8_t servoPin; } ISR_servo_t; diff --git a/examples/nRF52_MultipleServos/nRF52_MultipleServos.ino b/examples/nRF52_MultipleServos/nRF52_MultipleServos.ino index 4cc10c9..6d5f34a 100644 --- a/examples/nRF52_MultipleServos/nRF52_MultipleServos.ino +++ b/examples/nRF52_MultipleServos/nRF52_MultipleServos.ino @@ -52,7 +52,6 @@ #define TIMER_INTERRUPT_DEBUG 1 #define ISR_SERVO_DEBUG 1 -// To be included only in main(), .ino with setup() to avoid `Multiple Definitions` Linker Error #include "NRF52_ISR_Servo.h" // Published values for SG90 servos; adjust if needed @@ -68,7 +67,7 @@ typedef struct { - int8_t servoIndex; + int servoIndex; uint8_t servoPin; } ISR_servo_t; @@ -89,7 +88,7 @@ void setup() Serial.print(F("\nStarting NRF52_MultipleServos on ")); Serial.println(BOARD_NAME); Serial.println(NRF52_ISR_SERVO_VERSION); - for (int8_t index = 0; index < NUM_SERVOS; index++) + for (int index = 0; index < NUM_SERVOS; index++) { ISR_servo[index].servoIndex = NRF52_ISR_Servos.setupServo(ISR_servo[index].servoPin, MIN_MICROS, MAX_MICROS); diff --git a/library.json b/library.json index 6690f28..3c720b0 100644 --- a/library.json +++ b/library.json @@ -1,6 +1,6 @@ { "name": "NRF52_ISR_Servo", - "version": "1.1.0", + "version": "1.2.0", "keywords": "timing, device, control, timer, interrupt, isr, pwm, servo, isr-servo, hardware-timer, mission-critical, accuracy, precise, non-blocking, nrf52, nrf52840, nrf52832, feather-nrf52840, sparkfun-nrf52840, itsybitsy-nrf52840", "description": "This library enables you to use 1 Hardware PWM module on nRF52832/nRF52840-based board, such as AdaFruit Feather nRF52832, nRF52840 Express, Itsy-Bitsy nRF52840 Express, etc. to control up to 16 or more servo motors.", "authors": diff --git a/library.properties b/library.properties index 6a184d3..427095f 100644 --- a/library.properties +++ b/library.properties @@ -1,5 +1,5 @@ name=NRF52_ISR_Servo -version=1.1.0 +version=1.2.0 author=Khoi Hoang maintainer=Khoi Hoang license=MIT diff --git a/src/NRF52_ISR_Servo.h b/src/NRF52_ISR_Servo.h index b44ddf4..22fd770 100644 --- a/src/NRF52_ISR_Servo.h +++ b/src/NRF52_ISR_Servo.h @@ -9,12 +9,13 @@ Built by Khoi Hoang https://github.com/khoih-prog/NRF52_ISR_Servo Licensed under MIT license - Version: 1.1.0 + Version: 1.2.0 Version Modified By Date Comments ------- ----------- ---------- ----------- 1.0.0 K Hoang 22/08/2021 Initial coding for nRF52832/nRF52840 boards 1.1.0 K Hoang 03/03/2022 Convert to `h-only` style. Optimize code. Add support to `Sparkfun Pro nRF52840 Mini` + 1.2.0 K Hoang 23/04/2022 Permit using servos with different pulse ranges simultaneously. Delete left-over `cpp` *****************************************************************************************************************************/ #pragma once diff --git a/src/NRF52_ISR_Servo.hpp b/src/NRF52_ISR_Servo.hpp index ab6f94a..88d3929 100644 --- a/src/NRF52_ISR_Servo.hpp +++ b/src/NRF52_ISR_Servo.hpp @@ -9,12 +9,13 @@ Built by Khoi Hoang https://github.com/khoih-prog/NRF52_ISR_Servo Licensed under MIT license - Version: 1.1.0 + Version: 1.2.0 Version Modified By Date Comments ------- ----------- ---------- ----------- 1.0.0 K Hoang 22/08/2021 Initial coding for nRF52832/nRF52840 boards 1.1.0 K Hoang 03/03/2022 Convert to `h-only` style. Optimize code. Add support to `Sparkfun Pro nRF52840 Mini` + 1.2.0 K Hoang 23/04/2022 Permit using servos with different pulse ranges simultaneously. Delete left-over `cpp` *****************************************************************************************************************************/ #pragma once @@ -33,13 +34,13 @@ #include #if !defined(NRF52_ISR_SERVO_VERSION) - #define NRF52_ISR_SERVO_VERSION "NRF52_ISR_Servo v1.1.0" + #define NRF52_ISR_SERVO_VERSION "NRF52_ISR_Servo v1.2.0" #define NRF52_ISR_SERVO_VERSION_MAJOR 1 - #define NRF52_ISR_SERVO_VERSION_MINOR 1 + #define NRF52_ISR_SERVO_VERSION_MINOR 2 #define NRF52_ISR_SERVO_VERSION_PATCH 0 - #define NRF52_ISR_SERVO_VERSION_INT 1001000 + #define NRF52_ISR_SERVO_VERSION_INT 1002000 #endif @@ -104,7 +105,9 @@ class NRF52_ISR_Servo ////////////////////////////////////////////////// // Bind servo to the timer and pin, return servoIndex - int8_t setupServo(const uint8_t& pin, const uint16_t& minUs = MIN_PULSE_WIDTH, const uint16_t& maxUs = MAX_PULSE_WIDTH, int value = 0); + //int8_t setupServo(const uint8_t& pin, const uint16_t& minUs = MIN_PULSE_WIDTH, const uint16_t& maxUs = MAX_PULSE_WIDTH, int value = 0); + int8_t setupServo(const uint8_t& pin, const uint16_t& minPulseUs = MIN_PULSE_WIDTH, + const uint16_t& maxPulseUs = MAX_PULSE_WIDTH, uint16_t value = 0); // if value is < MIN_PULSE_WIDTH its treated as an angle, otherwise as pulse width in microseconds void write(const uint8_t& servoIndex, uint16_t value); @@ -186,15 +189,15 @@ class NRF52_ISR_Servo ////////////////////////////////////////////////// - uint16_t _minUs; - uint16_t _maxUs; - typedef struct { uint8_t pin; // pin servo connected to int position; // In degrees bool enabled; // true if enabled + uint16_t minPulseUs; // The minimum pulse width the servo can handle + uint16_t maxPulseUs; // The maximum pulse width the servo can handle + HardwarePWM* pwm; } servo_t; diff --git a/src/NRF52_ISR_Servo_Debug.h b/src/NRF52_ISR_Servo_Debug.h index e40e3c5..e058b1a 100644 --- a/src/NRF52_ISR_Servo_Debug.h +++ b/src/NRF52_ISR_Servo_Debug.h @@ -9,12 +9,13 @@ Built by Khoi Hoang https://github.com/khoih-prog/NRF52_ISR_Servo Licensed under MIT license - Version: 1.1.0 + Version: 1.2.0 Version Modified By Date Comments ------- ----------- ---------- ----------- 1.0.0 K Hoang 22/08/2021 Initial coding for nRF52832/nRF52840 boards 1.1.0 K Hoang 03/03/2022 Convert to `h-only` style. Optimize code. Add support to `Sparkfun Pro nRF52840 Mini` + 1.2.0 K Hoang 23/04/2022 Permit using servos with different pulse ranges simultaneously. Delete left-over `cpp` *****************************************************************************************************************************/ #pragma once diff --git a/src/NRF52_ISR_Servo_Impl.h b/src/NRF52_ISR_Servo_Impl.h index b22a46f..8af6406 100644 --- a/src/NRF52_ISR_Servo_Impl.h +++ b/src/NRF52_ISR_Servo_Impl.h @@ -9,12 +9,13 @@ Built by Khoi Hoang https://github.com/khoih-prog/NRF52_ISR_Servo Licensed under MIT license - Version: 1.1.0 + Version: 1.2.0 Version Modified By Date Comments ------- ----------- ---------- ----------- 1.0.0 K Hoang 22/08/2021 Initial coding for nRF52832/nRF52840 boards 1.1.0 K Hoang 03/03/2022 Convert to `h-only` style. Optimize code. Add support to `Sparkfun Pro nRF52840 Mini` + 1.2.0 K Hoang 23/04/2022 Permit using servos with different pulse ranges simultaneously. Delete left-over `cpp` *****************************************************************************************************************************/ #pragma once @@ -65,7 +66,8 @@ int8_t NRF52_ISR_Servo::findFirstFreeSlot() ////////////////////////////////////////////////// -int8_t NRF52_ISR_Servo::setupServo(const uint8_t& pin, const uint16_t& minUs, const uint16_t& maxUs, int value) +//int8_t NRF52_ISR_Servo::setupServo(const uint8_t& pin, const uint16_t& minUs, const uint16_t& maxUs, int value) +int8_t NRF52_ISR_Servo::setupServo(const uint8_t& pin, const uint16_t& minPulseUs, const uint16_t& maxPulseUs, uint16_t value) { int8_t servoIndex; @@ -81,10 +83,10 @@ int8_t NRF52_ISR_Servo::setupServo(const uint8_t& pin, const uint16_t& minUs, co return -1; pinMode(pin, OUTPUT); - - _maxUs = maxUs; - _minUs = minUs; + servo[servoIndex].maxPulseUs = maxPulseUs; + servo[servoIndex].minPulseUs = minPulseUs; + servo[servoIndex].position = 0; numServos++; @@ -139,7 +141,7 @@ int8_t NRF52_ISR_Servo::setupServo(const uint8_t& pin, const uint16_t& minUs, co /////////////////////////////////////////// ISR_SERVO_LOGDEBUG1("Index =", servoIndex); - ISR_SERVO_LOGDEBUG3("min =", _minUs, ", max =", _maxUs); + ISR_SERVO_LOGDEBUG3("min =", servo[servoIndex].minPulseUs, ", max =", servo[servoIndex].maxPulseUs); Serial.print("setupServo Index ="); Serial.println(servoIndex); @@ -155,7 +157,7 @@ void NRF52_ISR_Servo::write(const uint8_t& servoIndex, uint16_t value) { // assumed to be 0-180 degrees servo value = constrain(value, 0, 180); - value = map(value, 0, 180, _minUs, _maxUs); + value = map(value, 0, 180, servo[servoIndex].minPulseUs, servo[servoIndex].maxPulseUs); } writeMicroseconds(servoIndex, value); @@ -165,7 +167,7 @@ void NRF52_ISR_Servo::write(const uint8_t& servoIndex, uint16_t value) void NRF52_ISR_Servo::writeMicroseconds(const uint8_t& servoIndex, uint16_t value) { - value = constrain(value, _minUs, _maxUs); + value = constrain(value, servo[servoIndex].minPulseUs, servo[servoIndex].maxPulseUs); servo[servoIndex].position = value; if ( (servo[servoIndex].enabled) && (servo[servoIndex].pwm) ) @@ -189,7 +191,7 @@ bool NRF52_ISR_Servo::setPosition(const uint8_t& servoIndex, uint16_t position) { // assumed to be 0-180 degrees servo position = constrain(position, 0, 180); - position = map(position, 0, 180, _minUs, _maxUs); + position = map(position, 0, 180, servo[servoIndex].minPulseUs, servo[servoIndex].maxPulseUs); } servo[servoIndex].position = position; @@ -239,12 +241,12 @@ bool NRF52_ISR_Servo::setPulseWidth(const uint8_t& servoIndex, uint16_t& pulseWi // Updates interval of existing specified servo if ( servo[servoIndex].enabled && (servo[servoIndex].pin <= NRF52_MAX_PIN) ) { - if (pulseWidth < _minUs) - pulseWidth = _minUs; - else if (pulseWidth > _maxUs) - pulseWidth = _maxUs; + if (pulseWidth < servo[servoIndex].minPulseUs) + pulseWidth = servo[servoIndex].minPulseUs; + else if (pulseWidth > servo[servoIndex].maxPulseUs) + pulseWidth = servo[servoIndex].maxPulseUs; - servo[servoIndex].position = map(pulseWidth, _minUs, _maxUs, 0, 180); + servo[servoIndex].position = map(pulseWidth, servo[servoIndex].minPulseUs, servo[servoIndex].maxPulseUs, 0, 180); ISR_SERVO_LOGERROR3("Idx =", servoIndex, ", pos =",servo[servoIndex].position); @@ -344,7 +346,7 @@ bool NRF52_ISR_Servo::enable(const uint8_t& servoIndex) return false; } - if ( servo[servoIndex].position >= _minUs ) + if ( servo[servoIndex].position >= servo[servoIndex].minPulseUs ) servo[servoIndex].enabled = true; return true; @@ -373,7 +375,7 @@ void NRF52_ISR_Servo::enableAll() for (int8_t servoIndex = 0; servoIndex < MAX_SERVOS; servoIndex++) { - if ( (servo[servoIndex].position >= _minUs) && !servo[servoIndex].enabled + if ( (servo[servoIndex].position >= servo[servoIndex].minPulseUs) && !servo[servoIndex].enabled && (servo[servoIndex].pin <= NRF52_MAX_PIN) ) { servo[servoIndex].enabled = true;