Skip to content

Commit 4614264

Browse files
authored
Merge pull request #134 from ichbtm/master
fix hardcoded pulse limits
2 parents 3f959e9 + f07db89 commit 4614264

File tree

3 files changed

+22
-18
lines changed

3 files changed

+22
-18
lines changed

libraries/Servo/src/Servo.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -111,8 +111,8 @@ class Servo
111111
bool attached(); // return true if this servo is attached, otherwise false
112112
private:
113113
uint8_t servoIndex; // index into the channel data for this servo
114-
int8_t min; // minimum is this value times 4 added to MIN_PULSE_WIDTH
115-
int8_t max; // maximum is this value times 4 added to MAX_PULSE_WIDTH
114+
int16_t min; // minimum pulse in µs
115+
int16_t max; // maximum pulse in µs
116116
};
117117

118118
#endif

libraries/Servo/src/nrf52/Servo.cpp

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -44,19 +44,21 @@ Servo::Servo()
4444
uint8_t Servo::attach(int pin)
4545
{
4646

47-
return this->attach(pin, 0, 2500);
47+
return this->attach(pin, MIN_PULSE_WIDTH, MAX_PULSE_WIDTH);
4848
}
4949

5050

5151
uint8_t Servo::attach(int pin, int min, int max)
5252
{
53-
int servo_min, servo_max;
5453
if (this->servoIndex < MAX_SERVOS) {
5554
pinMode(pin, OUTPUT); // set servo pin to output
5655
servos[this->servoIndex].Pin.nbr = pin;
5756

58-
if(min < servo_min) min = servo_min;
59-
if (max > servo_max) max = servo_max;
57+
if(min < MIN_PULSE_WIDTH) min = MIN_PULSE_WIDTH;
58+
if (max > MAX_PULSE_WIDTH) max = MAX_PULSE_WIDTH;
59+
//fix min if conversion to pulse cycle value is too low
60+
if((min/DUTY_CYCLE_RESOLUTION)*DUTY_CYCLE_RESOLUTION<min) min+=DUTY_CYCLE_RESOLUTION;
61+
6062
this->min = min;
6163
this->max = max;
6264

@@ -67,9 +69,8 @@ uint8_t Servo::attach(int pin, int min, int max)
6769
{
6870
if ( HwPWMx[i]->addPin(pin) )
6971
{
70-
// 20ms - 50Hz
71-
HwPWMx[i]->setMaxValue(2500);
72-
HwPWMx[i]->setClockDiv(PWM_PRESCALER_PRESCALER_DIV_128);
72+
HwPWMx[i]->setMaxValue(MAXVALUE);
73+
HwPWMx[i]->setClockDiv(CLOCKDIV);
7374

7475
break;
7576
}
@@ -93,7 +94,7 @@ void Servo::write(int value)
9394
value = 0;
9495
else if (value > 180)
9596
value = 180;
96-
value = map(value, 0, 180, MIN_PULSE, MAX_PULSE);
97+
value = map(value, 0, 180, this->min, this->max);
9798

9899
writeMicroseconds(value);
99100
}
@@ -127,17 +128,17 @@ void Servo::writeMicroseconds(int value)
127128
nrf_pwm_task_trigger(PWMInstance, NRF_PWM_TASK_SEQSTART0);
128129
#else
129130
uint8_t pin = servos[this->servoIndex].Pin.nbr;
130-
131+
131132
for(int i=0; i<HWPWM_MODULE_NUM; i++)
132133
{
133-
if ( HwPWMx[i]->writePin(pin, value) ) break;
134+
if ( HwPWMx[i]->writePin(pin, value/DUTY_CYCLE_RESOLUTION) ) break;
134135
}
135136
#endif
136137
}
137138

138139
int Servo::read() // return the value as degrees
139140
{
140-
return map(readMicroseconds(), MIN_PULSE, MAX_PULSE, 0, 180);
141+
return map(readMicroseconds(), this->min, this->max, 0, 180);
141142
}
142143

143144
int Servo::readMicroseconds()
@@ -156,7 +157,7 @@ int Servo::readMicroseconds()
156157
{
157158
if ( HwPWMx[i]->checkPin(pin) )
158159
{
159-
return HwPWMx[i]->readPin(pin);
160+
return HwPWMx[i]->readPin(pin)*DUTY_CYCLE_RESOLUTION;
160161
}
161162
}
162163

libraries/Servo/src/nrf52/ServoTimers.h

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,11 +28,14 @@
2828
* NRF52 Only definitions
2929
* ---------------------
3030
*/
31-
32-
#define MIN_PULSE 55
33-
#define MAX_PULSE 284
31+
//PWM_PRESCALER_PRESCALER_DIV_128 -> NRF_PWM_CLK_125kHz -> resolution 8µs
32+
//MaxValue = 2500 -> PWM period = 20ms
33+
//20ms - 50Hz
34+
#define DUTY_CYCLE_RESOLUTION 8
35+
#define MAXVALUE 2500
36+
#define CLOCKDIV PWM_PRESCALER_PRESCALER_DIV_128
3437

3538
// define one timer in order to have MAX_SERVOS = 12
3639
typedef enum { _timer1, _Nbr_16timers } timer16_Sequence_t;
3740

38-
#endif // __SERVO_TIMERS_H__
41+
#endif // __SERVO_TIMERS_H__

0 commit comments

Comments
 (0)