|
| 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) - [email protected] |
| 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 | +// Default min/max pulse widths (in microseconds) and angles (in |
| 69 | +// degrees). Values chosen for Arduino compatibility. These values |
| 70 | +// are part of the public API; DO NOT CHANGE THEM. |
| 71 | +#define MIN_ANGLE 0 |
| 72 | +#define MAX_ANGLE 180 |
| 73 | + |
| 74 | +#define MIN_PULSE_WIDTH 544 // the shortest pulse sent to a servo |
| 75 | +#define MAX_PULSE_WIDTH 2400 // the longest pulse sent to a servo |
| 76 | + |
| 77 | +/** Class for interfacing with RC servomotors. */ |
| 78 | +class Servo { |
| 79 | +public: |
| 80 | + /** |
| 81 | + * @brief Construct a new Servo instance. |
| 82 | + * |
| 83 | + * The new instance will not be attached to any pin. |
| 84 | + */ |
| 85 | + Servo(); |
| 86 | + |
| 87 | + /** |
| 88 | + * @brief Associate this instance with a servomotor whose input is |
| 89 | + * connected to pin. |
| 90 | + * |
| 91 | + * If this instance is already attached to a pin, it will be |
| 92 | + * detached before being attached to the new pin. This function |
| 93 | + * doesn't detach any interrupt attached with the pin's timer |
| 94 | + * channel. |
| 95 | + * |
| 96 | + * @param pin Pin connected to the servo pulse wave input. This |
| 97 | + * pin must be capable of PWM output. |
| 98 | + * |
| 99 | + * @param minPulseWidth Minimum pulse width to write to pin, in |
| 100 | + * microseconds. This will be associated |
| 101 | + * with a minAngle degree angle. Defaults to |
| 102 | + * SERVO_DEFAULT_MIN_PW = 544. |
| 103 | + * |
| 104 | + * @param maxPulseWidth Maximum pulse width to write to pin, in |
| 105 | + * microseconds. This will be associated |
| 106 | + * with a maxAngle degree angle. Defaults to |
| 107 | + * SERVO_DEFAULT_MAX_PW = 2400. |
| 108 | + * |
| 109 | + * @param minAngle Target angle (in degrees) associated with |
| 110 | + * minPulseWidth. Defaults to |
| 111 | + * SERVO_DEFAULT_MIN_ANGLE = 0. |
| 112 | + * |
| 113 | + * @param maxAngle Target angle (in degrees) associated with |
| 114 | + * maxPulseWidth. Defaults to |
| 115 | + * SERVO_DEFAULT_MAX_ANGLE = 180. |
| 116 | + * |
| 117 | + * @sideeffect May set pinMode(pin, PWM). |
| 118 | + * |
| 119 | + * @return true if successful, false when pin doesn't support PWM. |
| 120 | + */ |
| 121 | + |
| 122 | + bool attach(uint8 pin, |
| 123 | + uint16 minPulseWidth=MIN_PULSE_WIDTH, |
| 124 | + uint16 maxPulseWidth=MAX_PULSE_WIDTH, |
| 125 | + int16 minAngle=MIN_ANGLE, |
| 126 | + int16 maxAngle=MAX_ANGLE); |
| 127 | + /** |
| 128 | + * @brief Stop driving the servo pulse train. |
| 129 | + * |
| 130 | + * If not currently attached to a motor, this function has no effect. |
| 131 | + * |
| 132 | + * @return true if this call did anything, false otherwise. |
| 133 | + */ |
| 134 | + bool detach(); |
| 135 | + |
| 136 | + /** |
| 137 | + * @brief Set the servomotor target angle. |
| 138 | + * |
| 139 | + * @param angle Target angle, in degrees. If the target angle is |
| 140 | + * outside the range specified at attach() time, it |
| 141 | + * will be clamped to lie in that range. |
| 142 | + * |
| 143 | + * @see Servo::attach() |
| 144 | + */ |
| 145 | + void write(int angle); |
| 146 | + |
| 147 | + /** |
| 148 | + * @brief Set the pulse width, in microseconds. |
| 149 | + * |
| 150 | + * @param pulseWidth Pulse width to send to the servomotor, in |
| 151 | + * microseconds. If outside of the range |
| 152 | + * specified at attach() time, it is clamped to |
| 153 | + * lie in that range. |
| 154 | + * |
| 155 | + * @see Servo::attach() |
| 156 | + */ |
| 157 | + void writeMicroseconds(uint16 pulseWidth); |
| 158 | + |
| 159 | + /** |
| 160 | + * Get the servomotor's target angle, in degrees. This will |
| 161 | + * lie inside the range specified at attach() time. |
| 162 | + * |
| 163 | + * @see Servo::attach() |
| 164 | + */ |
| 165 | + int read() const; |
| 166 | + |
| 167 | + /** |
| 168 | + * Get the current pulse width, in microseconds. This will |
| 169 | + * lie within the range specified at attach() time. |
| 170 | + * |
| 171 | + * @see Servo::attach() |
| 172 | + */ |
| 173 | + uint16 readMicroseconds() const; |
| 174 | + |
| 175 | + |
| 176 | + /** |
| 177 | + * @brief Check if this instance is attached to a servo. |
| 178 | + * @return true if this instance is attached to a servo, false otherwise. |
| 179 | + * @see Servo::attachedPin() |
| 180 | + */ |
| 181 | + bool attached() const { return this->pin != NOT_ATTACHED; } |
| 182 | + |
| 183 | + /** |
| 184 | + * @brief Get the pin this instance is attached to. |
| 185 | + * @return Pin number if currently attached to a pin, NOT_ATTACHED |
| 186 | + * otherwise. |
| 187 | + * @see Servo::attach() |
| 188 | + */ |
| 189 | + int attachedPin() const { return this->pin; } |
| 190 | + |
| 191 | +private: |
| 192 | + int16 pin; |
| 193 | + uint16 minPW; |
| 194 | + uint16 maxPW; |
| 195 | + int16 minAngle; |
| 196 | + int16 maxAngle; |
| 197 | + |
| 198 | + void resetFields(void); |
| 199 | +}; |
| 200 | + |
| 201 | + |
| 202 | + |
| 203 | +#endif /* _SERVO_H_ */ |
0 commit comments