新浪博客

16路舵机控制板

2018-07-09 22:00阅读:2,430
舵机的原理和控制
控制信号由接收机的通道进入信号调制芯片,获得直流偏置电压。它内部有一个基准电路,产生周期20ms,宽度为1.5ms的基准信号,将获得的直流偏置电压与电位器的电压比较,获得电压差输出。最后,电压差的正负输出到电机驱动芯片决定电机的正反转。当电机转速一定时,通过级联减速齿轮带动电位器旋转,使得电压差为0,电机停止转动。
舵机的控制一般需要一个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 pos
sible 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)
// our servo # counter
//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);
}

留言板

下载客户端体验更佳,还能发布图片和表情~

亿万博主正在被热评!
999+

给此博文留言吧! 有机会上热评榜! 攒金币兑换礼品!

好的评论会让人崇拜发布评论

我的更多文章

下载客户端阅读体验更佳

APP专享