source: trunk/Arduino/libraries/Servo/src/stm32f4/ServoTimers.h @ 7

Last change on this file since 7 was 3, checked in by markus, 7 years ago

Setup project structure

File size: 7.0 KB
Line 
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 /*
28 * Arduino srl - www.arduino.org
29 * 2017 Feb 23: Edited by Francesco Alessi (alfran) - francesco@arduino.org
30 */
31#ifndef _SERVO_H_
32#define _SERVO_H_
33
34#include "types.h"
35#include "timer.h"
36
37#include "wiring.h"             /* hack for IDE compile */
38
39/*
40 * Note on Arduino compatibility:
41 *
42 * In the Arduino implementation, PWM is done "by hand" in the sense
43 * that timer channels are hijacked in groups and an ISR is set which
44 * toggles Servo::attach()ed pins using digitalWrite().
45 *
46 * While this scheme allows any pin to drive a servo, it chews up
47 * cycles and complicates the programmer's notion of when a particular
48 * timer channel will be in use.
49 *
50 * This implementation only allows Servo instances to attach() to pins
51 * that already have a timer channel associated with them, and just
52 * uses pwmWrite() to drive the wave.
53 *
54 * This introduces an incompatibility: while the Arduino
55 * implementation of attach() returns the affected channel on success
56 * and 0 on failure, this one returns true on success and false on
57 * failure.
58 *
59 * RC Servos expect a pulse every 20ms.  Since periods are set for
60 * entire timers, rather than individual channels, attach()ing a Servo
61 * to a pin can interfere with other pins associated with the same
62 * timer.  As always, your board's pin map is your friend.
63 */
64
65// Pin number of unattached pins
66#define NOT_ATTACHED                    (-1)
67
68#define _Nbr_16timers 14 // mumber of STM32F469 Timers
69#define SERVOS_PER_TIMER 4 // Number of timer channels
70
71
72// Default min/max pulse widths (in microseconds) and angles (in
73// degrees).  Values chosen for Arduino compatibility.  These values
74// are part of the public API; DO NOT CHANGE THEM.
75#define MIN_ANGLE               0
76#define MAX_ANGLE             180
77
78#define MIN_PULSE_WIDTH       544     // the shortest pulse sent to a servo
79#define MAX_PULSE_WIDTH      2400     // the longest pulse sent to a servo
80
81/** Class for interfacing with RC servomotors. */
82class Servo {
83public:
84    /**
85     * @brief Construct a new Servo instance.
86     *
87     * The new instance will not be attached to any pin.
88     */
89    Servo();
90
91    /**
92     * @brief Associate this instance with a servomotor whose input is
93     *        connected to pin.
94     *
95     * If this instance is already attached to a pin, it will be
96     * detached before being attached to the new pin. This function
97     * doesn't detach any interrupt attached with the pin's timer
98     * channel.
99     *
100     * @param pin Pin connected to the servo pulse wave input. This
101     *            pin must be capable of PWM output.
102     *
103     * @param minPulseWidth Minimum pulse width to write to pin, in
104     *                      microseconds.  This will be associated
105     *                      with a minAngle degree angle.  Defaults to
106     *                      SERVO_DEFAULT_MIN_PW = 544.
107     *
108     * @param maxPulseWidth Maximum pulse width to write to pin, in
109     *                      microseconds.  This will be associated
110     *                      with a maxAngle degree angle. Defaults to
111     *                      SERVO_DEFAULT_MAX_PW = 2400.
112     *
113     * @param minAngle Target angle (in degrees) associated with
114     *                 minPulseWidth.  Defaults to
115     *                 SERVO_DEFAULT_MIN_ANGLE = 0.
116     *
117     * @param maxAngle Target angle (in degrees) associated with
118     *                 maxPulseWidth.  Defaults to
119     *                 SERVO_DEFAULT_MAX_ANGLE = 180.
120     *
121     * @sideeffect May set pinMode(pin, PWM).
122     *
123     * @return true if successful, false when pin doesn't support PWM.
124     */
125
126    bool attach(uint8 pin,
127                uint16 minPulseWidth=MIN_PULSE_WIDTH,
128                uint16 maxPulseWidth=MAX_PULSE_WIDTH,
129                int16 minAngle=MIN_ANGLE,
130                int16 maxAngle=MAX_ANGLE);
131    /**
132     * @brief Stop driving the servo pulse train.
133     *
134     * If not currently attached to a motor, this function has no effect.
135     *
136     * @return true if this call did anything, false otherwise.
137     */
138    bool detach();
139
140    /**
141     * @brief Set the servomotor target angle.
142     *
143     * @param angle Target angle, in degrees.  If the target angle is
144     *              outside the range specified at attach() time, it
145     *              will be clamped to lie in that range.
146     *
147     * @see Servo::attach()
148     */
149    void write(int angle);
150
151    /**
152     * @brief Set the pulse width, in microseconds.
153     *
154     * @param pulseWidth Pulse width to send to the servomotor, in
155     *                   microseconds. If outside of the range
156     *                   specified at attach() time, it is clamped to
157     *                   lie in that range.
158     *
159     * @see Servo::attach()
160     */
161    void writeMicroseconds(uint16 pulseWidth);
162
163    /**
164     * Get the servomotor's target angle, in degrees.  This will
165     * lie inside the range specified at attach() time.
166     *
167     * @see Servo::attach()
168     */
169    int read() const;
170
171    /**
172     * Get the current pulse width, in microseconds.  This will
173     * lie within the range specified at attach() time.
174     *
175     * @see Servo::attach()
176     */
177    uint16 readMicroseconds() const;
178
179
180    /**
181     * @brief Check if this instance is attached to a servo.
182     * @return true if this instance is attached to a servo, false otherwise.
183     * @see Servo::attachedPin()
184     */
185    bool attached() const { return this->pin != NOT_ATTACHED; }
186
187    /**
188     * @brief Get the pin this instance is attached to.
189     * @return Pin number if currently attached to a pin, NOT_ATTACHED
190     *         otherwise.
191     * @see Servo::attach()
192     */
193    int attachedPin() const { return this->pin; }
194
195private:
196    int16 pin;
197    uint16 minPW;
198    uint16 maxPW;
199    int16 minAngle;
200    int16 maxAngle;
201
202    void resetFields(void);
203};
204
205
206
207#endif  /* _SERVO_H_ */
Note: See TracBrowser for help on using the repository browser.