Skip to content

Commit c3479a5

Browse files
authored
Merge pull request #8 from alfran/development
Add support for Arduino STAR OTTO
2 parents 32777b9 + d7cd2d6 commit c3479a5

File tree

6 files changed

+434
-3
lines changed

6 files changed

+434
-3
lines changed

library.properties

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@ name=Servo
22
version=1.1.2
33
author=Michael Margolis, Arduino
44
maintainer=Arduino <[email protected]>
5-
sentence=Allows Arduino/Genuino boards to control a variety of servo motors.
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=avr,sam,samd,nrf52
9+
architectures=avr,sam,samd,nrf52,stm32f4

src/STM32F4/Icon

Whitespace-only changes.

src/STM32F4/Servo.cpp

Lines changed: 190 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,190 @@
1+
/******************************************************************************
2+
* The MIT License
3+
*
4+
* Copyright (c) 2010, LeafLabs, LLC.
5+
*
6+
* Permission is hereby granted, free of charge, to any person
7+
* obtaining a copy of this software and associated documentation
8+
* files (the "Software"), to deal in the Software without
9+
* restriction, including without limitation the rights to use, copy,
10+
* modify, merge, publish, distribute, sublicense, and/or sell copies
11+
* of the Software, and to permit persons to whom the Software is
12+
* furnished to do so, subject to the following conditions:
13+
*
14+
* The above copyright notice and this permission notice shall be
15+
* included in all copies or substantial portions of the Software.
16+
*
17+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
18+
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
19+
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
20+
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
21+
* BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
22+
* ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
23+
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
24+
* SOFTWARE.
25+
*****************************************************************************/
26+
27+
#include "ServoTimers.h"
28+
29+
#include "boards.h"
30+
#include "io.h"
31+
#include "pwm.h"
32+
#include "math.h"
33+
34+
// 20 millisecond period config. For a 1-based prescaler,
35+
//
36+
// (prescaler * overflow / CYC_MSEC) msec = 1 timer cycle = 20 msec
37+
// => prescaler * overflow = 20 * CYC_MSEC
38+
//
39+
// This picks the smallest prescaler that allows an overflow < 2^16.
40+
#define MAX_OVERFLOW ((1 << 16) - 1)
41+
#define CYC_MSEC (1000 * CYCLES_PER_MICROSECOND)
42+
#define TAU_MSEC 20
43+
#define TAU_USEC (TAU_MSEC * 1000)
44+
#define TAU_CYC (TAU_MSEC * CYC_MSEC)
45+
#define SERVO_PRESCALER (TAU_CYC / MAX_OVERFLOW + 1)
46+
#define SERVO_OVERFLOW ((uint16)round((double)TAU_CYC / SERVO_PRESCALER))
47+
48+
// Unit conversions
49+
#define US_TO_COMPARE(us) ((uint16)map((us), 0, TAU_USEC, 0, SERVO_OVERFLOW))
50+
#define COMPARE_TO_US(c) ((uint32)map((c), 0, SERVO_OVERFLOW, 0, TAU_USEC))
51+
#define ANGLE_TO_US(a) ((uint16)(map((a), this->minAngle, this->maxAngle, \
52+
this->minPW, this->maxPW)))
53+
#define US_TO_ANGLE(us) ((int16)(map((us), this->minPW, this->maxPW, \
54+
this->minAngle, this->maxAngle)))
55+
56+
Servo::Servo() {
57+
this->resetFields();
58+
}
59+
60+
bool Servo::attach(uint8 pin, uint16 minPW, uint16 maxPW, int16 minAngle, int16 maxAngle)
61+
{
62+
// SerialUSB.begin(115200);
63+
// SerialUSB.println(MAX_OVERFLOW);
64+
65+
66+
timer_dev *tdev = PIN_MAP[pin].timer_device;
67+
68+
analogWriteResolution(16);
69+
70+
int prescaler = 6;
71+
int overflow = 65400;
72+
int minPW_correction = 300;
73+
int maxPW_correction = 300;
74+
75+
pinMode(pin, OUTPUT);
76+
77+
78+
if (tdev == NULL) {
79+
// don't reset any fields or ASSERT(0), to keep driving any
80+
// previously attach()ed servo.
81+
return false;
82+
}
83+
84+
if ( (tdev == TIMER1) || (tdev == TIMER8) || (tdev == TIMER10) || (tdev == TIMER11))
85+
{
86+
prescaler = 54;
87+
overflow = 65400;
88+
minPW_correction = 40;
89+
maxPW_correction = 50;
90+
}
91+
92+
if ( (tdev == TIMER2) || (tdev == TIMER3) || (tdev == TIMER4) || (tdev == TIMER5) )
93+
{
94+
prescaler = 6;
95+
overflow = 64285;
96+
minPW_correction = 370;
97+
maxPW_correction = 350;
98+
}
99+
100+
if ( (tdev == TIMER6) || (tdev == TIMER7) )
101+
{
102+
prescaler = 6;
103+
overflow = 65400;
104+
minPW_correction = 0;
105+
maxPW_correction = 0;
106+
}
107+
108+
if ( (tdev == TIMER9) || (tdev == TIMER12) || (tdev == TIMER13) || (tdev == TIMER14) )
109+
{
110+
prescaler = 6;
111+
overflow = 65400;
112+
minPW_correction = 30;
113+
maxPW_correction = 0;
114+
}
115+
116+
if (this->attached()) {
117+
this->detach();
118+
}
119+
120+
this->pin = pin;
121+
this->minPW = (minPW + minPW_correction);
122+
this->maxPW = (maxPW + maxPW_correction);
123+
this->minAngle = minAngle;
124+
this->maxAngle = maxAngle;
125+
126+
timer_pause(tdev);
127+
timer_set_prescaler(tdev, prescaler); // prescaler is 1-based
128+
timer_set_reload(tdev, overflow);
129+
timer_generate_update(tdev);
130+
timer_resume(tdev);
131+
132+
return true;
133+
}
134+
135+
bool Servo::detach() {
136+
if (!this->attached()) {
137+
return false;
138+
}
139+
140+
timer_dev *tdev = PIN_MAP[this->pin].timer_device;
141+
uint8 tchan = PIN_MAP[this->pin].timer_channel;
142+
timer_set_mode(tdev, tchan, TIMER_DISABLED);
143+
144+
this->resetFields();
145+
146+
return true;
147+
}
148+
149+
void Servo::write(int degrees) {
150+
degrees = constrain(degrees, this->minAngle, this->maxAngle);
151+
this->writeMicroseconds(ANGLE_TO_US(degrees));
152+
}
153+
154+
int Servo::read() const {
155+
int a = US_TO_ANGLE(this->readMicroseconds());
156+
// map() round-trips in a weird way we mostly correct for here;
157+
// the round-trip is still sometimes off-by-one for write(1) and
158+
// write(179).
159+
return a == this->minAngle || a == this->maxAngle ? a : a + 1;
160+
}
161+
162+
void Servo::writeMicroseconds(uint16 pulseWidth) {
163+
if (!this->attached()) {
164+
ASSERT(0);
165+
return;
166+
}
167+
pulseWidth = constrain(pulseWidth, this->minPW, this->maxPW);
168+
analogWrite(this->pin, US_TO_COMPARE(pulseWidth));
169+
}
170+
171+
uint16 Servo::readMicroseconds() const {
172+
if (!this->attached()) {
173+
ASSERT(0);
174+
return 0;
175+
}
176+
177+
stm32_pin_info pin_info = PIN_MAP[this->pin];
178+
uint16 compare = timer_get_compare(pin_info.timer_device,
179+
pin_info.timer_channel);
180+
181+
return COMPARE_TO_US(compare);
182+
}
183+
184+
void Servo::resetFields(void) {
185+
this->pin = NOT_ATTACHED;
186+
this->minAngle = MIN_ANGLE;
187+
this->maxAngle = MAX_ANGLE;
188+
this->minPW = MIN_PULSE_WIDTH;
189+
this->maxPW = MAX_PULSE_WIDTH;
190+
}

0 commit comments

Comments
 (0)