Skip to content

Commit 7f345c3

Browse files
author
fpr
committed
Fix pulse width error. Clean code.
Signed-off-by: fpr <[email protected]>
1 parent 53876e6 commit 7f345c3

File tree

2 files changed

+12
-13
lines changed

2 files changed

+12
-13
lines changed

libraries/Servo/src/stm32/Servo.cpp

+11-12
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
Copyright (c) 2015 Arduino LLC. All right reserved.
2+
Copyright (c) 2017 Arduino LLC. All right reserved.
33
44
This library is free software; you can redistribute it and/or
55
modify it under the terms of the GNU Lesser General Public
@@ -21,15 +21,11 @@
2121
#include <Arduino.h>
2222
#include <Servo.h>
2323

24-
#define usToTicks(_us) (_us * 2) // converts microseconds to tick
25-
#define ticksToUs(_ticks) (_ticks / 2) // converts from ticks back to microseconds
26-
27-
#define TRIM_DURATION 2 // compensation ticks to trim adjust for digitalWrite delays
28-
2924
static servo_t servos[MAX_SERVOS]; // static array of servo structures
3025
static volatile int8_t timerChannel[_Nbr_16timers ]; // counter for the servo being pulsed for each timer (or -1 if refresh interval)
3126

3227
uint8_t ServoCount = 0; // the total number of attached servos
28+
stimer_t _timer;
3329

3430
// convenience macros
3531
#define SERVO_INDEX_TO_TIMER(_servo_nbr) ((timer16_Sequence_t)(_servo_nbr / SERVOS_PER_TIMER)) // returns the timer controlling this servo
@@ -69,8 +65,8 @@ static void ServoIrqHandle(stimer_t *obj, uint32_t channel)
6965
}
7066
else {
7167
// finished all channels so wait for the refresh period to expire before starting over
72-
if( getTimerCounter(obj) + 4 < usToTicks(REFRESH_INTERVAL) ) { // allow a few ticks to ensure the next OCR1A not missed
73-
setCCRRegister(obj, channel, (unsigned int)usToTicks(REFRESH_INTERVAL));
68+
if( getTimerCounter(obj) + 4 < REFRESH_INTERVAL ) { // allow a few ticks to ensure the next OCR1A not missed
69+
setCCRRegister(obj, channel, (unsigned int)REFRESH_INTERVAL);
7470
} else {
7571
setCCRRegister(obj, channel, getTimerCounter(obj) + 4); // at least REFRESH_INTERVAL has elapsed
7672
}
@@ -80,6 +76,11 @@ static void ServoIrqHandle(stimer_t *obj, uint32_t channel)
8076

8177
static void initISR(stimer_t *obj)
8278
{
79+
/*
80+
* Timer clock set by default at 1us.
81+
* Period set to REFRESH_INTERVAL*3
82+
* Default pulse width set to DEFAULT_PULSE_WIDTH
83+
*/
8384
TimerPulseInit(obj, REFRESH_INTERVAL*3, DEFAULT_PULSE_WIDTH, ServoIrqHandle);
8485
}
8586

@@ -104,7 +105,7 @@ Servo::Servo()
104105
{
105106
if (ServoCount < MAX_SERVOS) {
106107
this->servoIndex = ServoCount++; // assign a servo index to this instance
107-
servos[this->servoIndex].ticks = usToTicks(DEFAULT_PULSE_WIDTH); // store default values
108+
servos[this->servoIndex].ticks = DEFAULT_PULSE_WIDTH; // store default values
108109
} else {
109110
this->servoIndex = INVALID_SERVO; // too many servos
110111
}
@@ -173,8 +174,6 @@ void Servo::writeMicroseconds(int value)
173174
else if (value > SERVO_MAX())
174175
value = SERVO_MAX();
175176

176-
value = value - TRIM_DURATION;
177-
value = usToTicks(value); // convert to ticks after compensating for interrupt overhead
178177
servos[channel].ticks = value;
179178
}
180179
}
@@ -188,7 +187,7 @@ int Servo::readMicroseconds()
188187
{
189188
unsigned int pulsewidth;
190189
if (this->servoIndex != INVALID_SERVO)
191-
pulsewidth = ticksToUs(servos[this->servoIndex].ticks) + TRIM_DURATION;
190+
pulsewidth = servos[this->servoIndex].ticks;
192191
else
193192
pulsewidth = 0;
194193

libraries/Servo/src/stm32/ServoTimers.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
Copyright (c) 2015 Arduino LLC. All right reserved.
2+
Copyright (c) 2017 Arduino LLC. All right reserved.
33
44
This library is free software; you can redistribute it and/or
55
modify it under the terms of the GNU Lesser General Public

0 commit comments

Comments
 (0)