Skip to content

Commit ac89c2d

Browse files
authored
Merge pull request stm32duino#105 from fprwi6labs/fix_servo_lib
Fix servo lib. PR done on official Servo library github: arduino-libraries/Servo#10
2 parents 719d04c + 7f345c3 commit ac89c2d

File tree

4 files changed

+46
-28
lines changed

4 files changed

+46
-28
lines changed

Diff for: libraries/Servo/library.properties

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
name=Servo
2-
version=1.1.1
2+
version=1.1.2
33
author=Michael Margolis, Arduino
44
maintainer=Arduino <[email protected]>
5-
sentence=Allows Arduino boards to control a variety of servo motors. For all Arduino boards.
5+
sentence=Allows Arduino/Genuino boards to control a variety of servo motors.
66
paragraph=This library can control a great number of servos.<br />It makes careful use of timers: the library can control 12 servos using only 1 timer.<br />On the Arduino Due you can control up to 60 servos.<br />
77
category=Device Control
88
url=http://www.arduino.cc/en/Reference/Servo
9-
architectures=stm32
9+
architectures=avr,sam,samd,nrf52,stm32f4,stm32

Diff for: libraries/Servo/Servo.h renamed to libraries/Servo/src/Servo.h

+25-9
Original file line numberDiff line numberDiff line change
@@ -59,20 +59,36 @@
5959
*/
6060

6161
// Architecture specific include
62-
#include "ServoTimers.h"
62+
#if defined(ARDUINO_ARCH_AVR)
63+
#include "avr/ServoTimers.h"
64+
#elif defined(ARDUINO_ARCH_SAM)
65+
#include "sam/ServoTimers.h"
66+
#elif defined(ARDUINO_ARCH_SAMD)
67+
#include "samd/ServoTimers.h"
68+
#elif defined(ARDUINO_ARCH_STM32F4)
69+
#include "stm32f4/ServoTimers.h"
70+
#elif defined(ARDUINO_ARCH_NRF52)
71+
#include "nrf52/ServoTimers.h"
72+
#elif defined(ARDUINO_ARCH_STM32)
73+
#include "stm32/ServoTimers.h"
74+
#else
75+
#error "This library only supports boards with an AVR, SAM, SAMD, NRF52, STM32F4 or STM32 processor."
76+
#endif
6377

6478
#define Servo_VERSION 2 // software version of this library
6579

66-
#define MIN_PULSE_WIDTH 480 // the shortest pulse sent to a servo
67-
#define MAX_PULSE_WIDTH 2200 // the longest pulse sent to a servo
68-
#define DEFAULT_PULSE_WIDTH 1300 // default pulse width when servo is attached
80+
#define MIN_PULSE_WIDTH 544 // the shortest pulse sent to a servo
81+
#define MAX_PULSE_WIDTH 2400 // the longest pulse sent to a servo
82+
#define DEFAULT_PULSE_WIDTH 1500 // default pulse width when servo is attached
6983
#define REFRESH_INTERVAL 20000 // minumim time to refresh servos in microseconds
7084

71-
#define SERVOS_PER_TIMER 12 // the maximum number of servos controlled by one timer
85+
#define SERVOS_PER_TIMER 12 // the maximum number of servos controlled by one timer
7286
#define MAX_SERVOS (_Nbr_16timers * SERVOS_PER_TIMER)
7387

7488
#define INVALID_SERVO 255 // flag indicating an invalid servo index
7589

90+
#if !defined(ARDUINO_ARCH_STM32F4)
91+
7692
typedef struct {
7793
uint8_t nbr :6 ; // a pin number from 0 to 63
7894
uint8_t isActive :1 ; // true if this channel is enabled, pin not pulsed if false
@@ -96,10 +112,10 @@ class Servo
96112
int readMicroseconds(); // returns current pulse width in microseconds for this servo (was read_us() in first release)
97113
bool attached(); // return true if this servo is attached, otherwise false
98114
private:
99-
uint8_t servoIndex; // index into the channel data for this servo
100-
int8_t min; // minimum is this value times 4 added to MIN_PULSE_WIDTH
101-
int8_t max; // maximum is this value times 4 added to MAX_PULSE_WIDTH
102-
stimer_t _timer;
115+
uint8_t servoIndex; // index into the channel data for this servo
116+
int8_t min; // minimum is this value times 4 added to MIN_PULSE_WIDTH
117+
int8_t max; // maximum is this value times 4 added to MAX_PULSE_WIDTH
103118
};
104119

105120
#endif
121+
#endif

Diff for: libraries/Servo/Servo.cpp renamed to libraries/Servo/src/stm32/Servo.cpp

+14-13
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
@@ -16,20 +16,16 @@
1616
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
1717
*/
1818

19-
//#if defined(ARDUINO_ARCH_SAMD)
19+
#if defined(ARDUINO_ARCH_STM32)
2020

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

@@ -199,3 +198,5 @@ bool Servo::attached()
199198
{
200199
return servos[this->servoIndex].Pin.isActive;
201200
}
201+
202+
#endif // ARDUINO_ARCH_STM32

Diff for: libraries/Servo/ServoTimers.h renamed to libraries/Servo/src/stm32/ServoTimers.h

+4-3
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
@@ -27,9 +27,10 @@
2727
#ifndef __SERVO_TIMERS_H__
2828
#define __SERVO_TIMERS_H__
2929

30-
typedef enum {
30+
// Uses one timer. Allows until 12 servos.
31+
typedef enum {
3132
_timer1,
32-
_Nbr_16timers
33+
_Nbr_16timers
3334
} timer16_Sequence_t;
3435

3536
#endif // __SERVO_TIMERS_H__

0 commit comments

Comments
 (0)