16路舵机控制板
(2018-07-09 22:00:20)分类: Arduino |
舵机的原理和控制
舵机的控制一般需要一个20ms左右的时基脉冲,该脉冲的高电平部分一般为0.5ms-2.5ms范围内的角度控制脉冲部分,总间隔为2ms。以180度角度伺服为例,那么对应的控制关系是这样的: 0.5ms--------------0度;1.0ms------------45度;1.5ms------------90度; 2.0ms-----------135度; 2.5ms-----------180度;
#include ,Wire.h,
#include ,Adafruit_PWMServoDriver.h,
// called this way, it uses the default address 0x40
Adafruit_PWMServoDriver pwm = Adafruit_PWMServoDriver();
// you can also call it with a different address you want
//Adafruit_PWMServoDriver pwm = Adafruit_PWMServoDriver(0x41);
// Depending on your servo make, the pulse width min and max may vary, you
// want these to be as small/large as possible without hitting the hard stop
// for max range. You'll have to tweak them as necessary to match the servos you
// have!
//用于驱动板的级联
Adafruit_PWMServoDriver pwm = Adafruit_PWMServoDriver();
// you can also call it with a different address you want
//Adafruit_PWMServoDriver pwm = Adafruit_PWMServoDriver(0x41);
// Depending on your servo make, the pulse width min and max may vary, you
// want these to be as small/large as possible without hitting the hard stop
// for max range. You'll have to tweak them as necessary to match the servos you
// have!
//用于驱动板的级联
#define SERVOMIN 200 // this is the
'minimum' pulse length count (out of 4096)
#define SERVOMAX 600 // this is the 'maximum'
pulse length count (out of 4096)
#define SERVOMAX
// our servo # counter
//uint8_t servonum = 0;
//uint8_t servonum = 0;
void setup() {
Serial.begin(9600);
Serial.println("16 channel
Servo test!");
pwm.begin();
pwm.setPWMFreq(60); // Analog servos run at ~60
Hz updates
}
}
// you can use this function if you'd like to set the pulse
length in seconds
// e.g. setServoPulse(0, 0.001) is a ~1 millisecond pulse width. its not precise!
//如果您想以秒为单位设置脉冲长度,可以使用此功能,例如 setServoPulse(0,0.001)是~1毫秒的脉冲宽度。 它不精确!
void setServoPulse(uint8_t n, double pulse) {
double
pulselength;//精度浮点数
pulselength =
1000000; // 1,000,000 us per
second
pulselength /=
60; // 60 Hz
Serial.print(pulselength);
Serial.println(" us per period");
pulselength /=
4096; // 12 bits of resolution
Serial.print(pulselength);
Serial.println(" us per bit");
pulse *= 1000;
pulse /= pulselength;
Serial.println(pulse);
pwm.setPWM(n, 0, pulse);
}
void loop() {
// Drive each servo one at a
time
//Serial.println(servonum);
for (uint16_t pulselen =
SERVOMIN; pulselen < SERVOMAX; ) {
pwm.setPWM(0, 0, pulselen);
//
pwm.setPWM(1, 0, pulselen-110);//角度在-110到+120之间调整120度
setServoPulse(1, 0.1);
pwm.setPWM(2, 0, pulselen);
pwm.setPWM(3, 0, pulselen);
pwm.setPWM(4, 0, pulselen);
pwm.setPWM(5, 0, pulselen);
pwm.setPWM(6, 0, pulselen);
pwm.setPWM(7, 0, pulselen);
pwm.setPWM(8, 0, pulselen);
pwm.setPWM(9, 0, pulselen);
pwm.setPWM(10, 0, pulselen);
pwm.setPWM(11, 0, pulselen);
pwm.setPWM(12, 0, pulselen);
pwm.setPWM(13, 0, pulselen);
pwm.setPWM(14, 0, pulselen);
pwm.setPWM(15, 0, pulselen);
}
delay(500);
for (uint16_t pulselen =
SERVOMAX; pulselen > SERVOMIN; pulselen--) {
pwm.setPWM(0, 0, pulselen);//调节范围-110到+110
pwm.setPWM(1, 0, pulselen);
pwm.setPWM(2, 0, pulselen);
pwm.setPWM(3, 0, pulselen);
pwm.setPWM(4, 0, pulselen);
pwm.setPWM(5, 0, pulselen);
pwm.setPWM(6, 0, pulselen);
pwm.setPWM(7, 0, pulselen);
pwm.setPWM(8, 0, pulselen);
pwm.setPWM(9, 0, pulselen);
pwm.setPWM(10, 0, pulselen);
pwm.setPWM(11, 0, pulselen);
pwm.setPWM(12, 0, pulselen);
pwm.setPWM(13, 0, pulselen);
pwm.setPWM(14, 0, pulselen);
pwm.setPWM(15, 0, pulselen);
}
delay(500);
}
// e.g. setServoPulse(0, 0.001) is a ~1 millisecond pulse width. its not precise!
//如果您想以秒为单位设置脉冲长度,可以使用此功能,例如 setServoPulse(0,0.001)是~1毫秒的脉冲宽度。 它不精确!
void setServoPulse(uint8_t n, double pulse) {
}
void loop() {
}