From e99bd4dbaa999254586c6f90a54a5c99ac66ae53 Mon Sep 17 00:00:00 2001 From: ichbtm <7622549+ichbtm@users.noreply.github.com> Date: Fri, 30 Mar 2018 17:00:19 +0200 Subject: [PATCH 1/3] use user limits MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit and µs functions use our return µs --- libraries/Servo/src/nrf52/Servo.cpp | 25 +++++++++++++------------ 1 file changed, 13 insertions(+), 12 deletions(-) diff --git a/libraries/Servo/src/nrf52/Servo.cpp b/libraries/Servo/src/nrf52/Servo.cpp index 5f5414ff..455552d6 100644 --- a/libraries/Servo/src/nrf52/Servo.cpp +++ b/libraries/Servo/src/nrf52/Servo.cpp @@ -44,19 +44,21 @@ Servo::Servo() uint8_t Servo::attach(int pin) { - return this->attach(pin, 0, 2500); + return this->attach(pin, MIN_PULSE_WIDTH, MAX_PULSE_WIDTH); } uint8_t Servo::attach(int pin, int min, int max) { - int servo_min, servo_max; if (this->servoIndex < MAX_SERVOS) { pinMode(pin, OUTPUT); // set servo pin to output servos[this->servoIndex].Pin.nbr = pin; - if(min < servo_min) min = servo_min; - if (max > servo_max) max = servo_max; + if(min < MIN_PULSE_WIDTH) min = MIN_PULSE_WIDTH; + if (max > MAX_PULSE_WIDTH) max = MAX_PULSE_WIDTH; + //fix min if conversion to pulse cycle value is too low + if((min/DUTY_CYCLE_RESOLUTION)*DUTY_CYCLE_RESOLUTIONmin = min; this->max = max; @@ -67,9 +69,8 @@ uint8_t Servo::attach(int pin, int min, int max) { if ( HwPWMx[i]->addPin(pin) ) { - // 20ms - 50Hz - HwPWMx[i]->setMaxValue(2500); - HwPWMx[i]->setClockDiv(PWM_PRESCALER_PRESCALER_DIV_128); + HwPWMx[i]->setMaxValue(MAXVALUE); + HwPWMx[i]->setClockDiv(CLOCKDIV); break; } @@ -93,7 +94,7 @@ void Servo::write(int value) value = 0; else if (value > 180) value = 180; - value = map(value, 0, 180, MIN_PULSE, MAX_PULSE); + value = map(value, 0, 180, this->min, this->max); writeMicroseconds(value); } @@ -127,17 +128,17 @@ void Servo::writeMicroseconds(int value) nrf_pwm_task_trigger(PWMInstance, NRF_PWM_TASK_SEQSTART0); #else uint8_t pin = servos[this->servoIndex].Pin.nbr; - + for(int i=0; iwritePin(pin, value) ) break; + if ( HwPWMx[i]->writePin(pin, value/DUTY_CYCLE_RESOLUTION) ) break; } #endif } int Servo::read() // return the value as degrees { - return map(readMicroseconds(), MIN_PULSE, MAX_PULSE, 0, 180); + return map(readMicroseconds(), this->min, this->max, 0, 180); } int Servo::readMicroseconds() @@ -156,7 +157,7 @@ int Servo::readMicroseconds() { if ( HwPWMx[i]->checkPin(pin) ) { - return HwPWMx[i]->readPin(pin); + return HwPWMx[i]->readPin(pin)*DUTY_CYCLE_RESOLUTION; } } From a6b744fe3cfb58eb36220991df7c28c0ed9b2b53 Mon Sep 17 00:00:00 2001 From: ichbtm <7622549+ichbtm@users.noreply.github.com> Date: Fri, 30 Mar 2018 17:02:01 +0200 Subject: [PATCH 2/3] added named constants and removed hoardcoded pulse cycle limits --- libraries/Servo/src/nrf52/ServoTimers.h | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/libraries/Servo/src/nrf52/ServoTimers.h b/libraries/Servo/src/nrf52/ServoTimers.h index 51759bea..ce6c7de6 100644 --- a/libraries/Servo/src/nrf52/ServoTimers.h +++ b/libraries/Servo/src/nrf52/ServoTimers.h @@ -28,11 +28,14 @@ * NRF52 Only definitions * --------------------- */ - -#define MIN_PULSE 55 -#define MAX_PULSE 284 +//PWM_PRESCALER_PRESCALER_DIV_128 -> NRF_PWM_CLK_125kHz -> resolution 8µs +//MaxValue = 2500 -> PWM period = 20ms +//20ms - 50Hz +#define DUTY_CYCLE_RESOLUTION 8 +#define MAXVALUE 2500 +#define CLOCKDIV PWM_PRESCALER_PRESCALER_DIV_128 // define one timer in order to have MAX_SERVOS = 12 typedef enum { _timer1, _Nbr_16timers } timer16_Sequence_t; -#endif // __SERVO_TIMERS_H__ \ No newline at end of file +#endif // __SERVO_TIMERS_H__ From f07db89047fc728db21473b41de2570f6c06808e Mon Sep 17 00:00:00 2001 From: ichbtm <7622549+ichbtm@users.noreply.github.com> Date: Fri, 30 Mar 2018 17:03:24 +0200 Subject: [PATCH 3/3] changed min and max to uint16_t --- libraries/Servo/src/Servo.h | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/libraries/Servo/src/Servo.h b/libraries/Servo/src/Servo.h index 4890a141..640f7436 100644 --- a/libraries/Servo/src/Servo.h +++ b/libraries/Servo/src/Servo.h @@ -111,8 +111,8 @@ public: bool attached(); // return true if this servo is attached, otherwise false private: uint8_t servoIndex; // index into the channel data for this servo - int8_t min; // minimum is this value times 4 added to MIN_PULSE_WIDTH - int8_t max; // maximum is this value times 4 added to MAX_PULSE_WIDTH + int16_t min; // minimum pulse in µs + int16_t max; // maximum pulse in µs }; #endif