RTduino Source Code Reference Manual
Servo.h
1 /*
2  * Copyright (c) 2021-2022, RTduino Development Team
3  *
4  * SPDX-License-Identifier: LGPL-v2.1
5  *
6  * https://github.com/RTduino/RTduino
7  * https://gitee.com/rtduino/RTduino
8  *
9  * Change Logs:
10  * Date Author Notes
11  * 2021-12-10 Meco Man first version
12  */
13 /*
14  Servo.h - Interrupt driven Servo library for Arduino using 16 bit timers- Version 2
15  Copyright (c) 2009 Michael Margolis. All right reserved.
16 
17  This library is free software; you can redistribute it and/or
18  modify it under the terms of the GNU Lesser General Public
19  License as published by the Free Software Foundation; either
20  version 2.1 of the License, or (at your option) any later version.
21 
22  This library is distributed in the hope that it will be useful,
23  but WITHOUT ANY WARRANTY; without even the implied warranty of
24  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
25  Lesser General Public License for more details.
26 
27  You should have received a copy of the GNU Lesser General Public
28  License along with this library; if not, write to the Free Software
29  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
30 */
31 /*
32  A servo is activated by creating an instance of the Servo class passing
33  the desired pin to the attach() method.
34  The servos are pulsed in the background using the value most recently
35  written using the write() method.
36 
37  Note that analogWrite of PWM on pins associated with the timer are
38  disabled when the first servo is attached.
39  Timers are seized as needed in groups of 12 servos - 24 servos use two
40  timers, 48 servos will use four.
41  The sequence used to seize timers is defined in timers.h
42 
43  The methods are:
44 
45  Servo - Class for manipulating servo motors connected to Arduino pins.
46 
47  attach(pin ) - Attaches a servo motor to an I/O pin.
48  attach(pin, min, max ) - Attaches to a pin setting min and max values in microseconds
49  default min is 544, max is 2400
50 
51  write() - Sets the servo angle in degrees. (invalid angle that is valid as pulse in microseconds is treated as microseconds)
52  writeMicroseconds() - Sets the servo pulse width in microseconds
53  read() - Gets the last written servo pulse width as an angle between 0 and 180.
54  readMicroseconds() - Gets the last written servo pulse width in microseconds. (was read_us() in first release)
55  attached() - Returns true if there is a servo attached.
56  detach() - Stops an attached servos from pulsing its I/O pin.
57  */
58 
59 #ifndef __SERVO_H__
60 #define __SERVO_H__
61 
62 #include <inttypes.h>
63 #include <Arduino.h>
64 
65 #define INVALID_SERVO 255 // flag indicating an invalid servo index
66 
67 typedef struct
68 {
69  uint8_t nbr :7 ; // a pin number from 0 to 128
70  uint8_t isActive :1 ; // true if this channel is enabled, pin not pulsed if false
71 }ServoPin_t;
72 
73 typedef struct
74 {
75  ServoPin_t Pin;
76  unsigned int pulsewidth; // current PWM pulse in us on this pin
77  struct rt_device_pwm* rt_dev;
78 }servo_t;
79 
80 class Servo
81 {
82 public:
83  Servo();
84  uint8_t attach(int pin); // attach the given pin to the next free channel, sets pinMode, returns channel number or INVALID_SERVO if failure
85  uint8_t attach(int pin, int min, int max); // as above but also sets min and max values for writes.
86  void detach();
87  void write(int angle); // if value is < 200 its treated as an angle, otherwise as pulse width in microseconds
88  void writeMicroseconds(int pulsewidth); // Write pulse width in microseconds
89  int read(); // returns current pulse width as an angle between 0 and 180 degrees
90  int readMicroseconds(); // returns current pulse width in microseconds for this servo (was read_us() in first release)
91  bool attached(); // return true if this servo is attached, otherwise false
92 private:
93  servo_t servo_info; // servo information
94  uint16_t min; // the pulse width, in microseconds, corresponding to the minimum (0 degree) angle on the servo (defaults to 544)
95  uint16_t max; // the pulse width, in microseconds, corresponding to the maximum (180 degree) angle on the servo (defaults to 2400)
96 };
97 
98 #endif /* __SERVO_H__ */
Arduino Core Header File.
Definition: Servo.h:81
#define min(a, b)
Returns the smaller of two values.
Definition: Arduino.h:298
#define max(a, b)
Returns the larger of two values.
Definition: Arduino.h:307
Definition: Servo.h:68
Definition: Servo.h:74