Arduino Servo 库使用教程
ServoServo Library for Arduino项目地址:https://gitcode.com/gh_mirrors/serv/Servo
1. 项目的目录结构及介绍
Arduino Servo 库的目录结构相对简单,主要包含以下几个部分:
Servo/
├── keywords.txt
├── library.properties
├── README.md
├── src/
│ └── Servo.h
│ └── Servo.cpp
keywords.txt
: 定义了在 Arduino IDE 中高亮显示的关键词。library.properties
: 包含库的元数据,如名称、版本、作者等。README.md
: 项目的说明文档,通常包含安装和使用说明。src/
: 源代码目录,包含库的主要实现文件Servo.h
和Servo.cpp
。
2. 项目的启动文件介绍
在 Arduino Servo 库中,主要的启动文件是 src/Servo.h
。这个文件定义了 Servo 类的接口和一些常量。以下是 Servo.h
文件的部分内容:
#ifndef Servo_h
#define Servo_h
#include <inttypes.h>
#define Servo_VERSION 2 // software version of this library
#define MIN_PULSE_WIDTH 544 // the shortest pulse sent to a servo
#define MAX_PULSE_WIDTH 2400 // the longest pulse sent to a servo
#define DEFAULT_PULSE_WIDTH 1500 // default pulse width when servo is attached
#define REFRESH_INTERVAL 20000 // minumim time to refresh servos in microseconds
#define SERVOS_PER_TIMER 12 // the maximum number of servos controlled by one timer
#define MAX_SERVOS SERVOS_PER_TIMER
class Servo {
public:
Servo();
uint8_t attach(int pin); // attach the given pin to the next free channel, sets pinMode, returns channel number or 0 if failure
uint8_t attach(int pin, int min, int max); // as above but also sets min and max values for writes
void detach();
void write(int value); // if value is < 200 its treated as an angle, otherwise as pulse width in microseconds
void writeMicroseconds(int value); // Write pulse width in microseconds
int read(); // returns current pulse width as an angle between 0 and 180 degrees
int readMicroseconds(); // returns current pulse width in microseconds for this servo (was read_us() in first release)
bool attached(); // return true if this servo is attached, otherwise false
private:
uint8_t servoIndex; // index into the channel data for this servo
int8_t pin; // PWM output pin number
};
#endif
3. 项目的配置文件介绍
在 Arduino Servo 库中,主要的配置文件是 library.properties
。这个文件包含了库的基本信息,如名称、版本、作者等。以下是 library.properties
文件的内容:
name=Servo
version=1.1.8
author=Michael Margolis
maintainer=Arduino
sentence=Allows Arduino/Genuino boards to control a variety of servo motors.
paragraph=This library allows an Arduino/Genuino board to control RC (hobby) servo motors. Servos have integrated gears and a shaft that can be precisely controlled. Standard servos allow the shaft to be positioned at various angles, usually between 0 and 180 degrees. Continuous rotation servos allow the rotation of the shaft to be set to various speeds.
category=Device Control
url=https://github.com/arduino-libraries/Servo
architectures=*
name
: 库的名称。version
: 库的版本号。author
: 库的作者。maintainer
: 库的维护者。sentence
: 库的简短描述。paragraph
: 库的详细描述。category
: 库的分类。url
: 库的 GitHub 地址。architectures
: 支持的架构。
通过以上介绍,您可以更好地理解和使用 Arduino Servo 库。希望这个教程对您有所帮助!
ServoServo Library for Arduino项目地址:https://gitcode.com/gh_mirrors/serv/Servo