制造工程实践课程例程
main.cpp@0:2e707e50fd73, 2019-10-24 (annotated)
- Committer:
- lant19
- Date:
- Thu Oct 24 06:39:03 2019 +0000
- Revision:
- 0:2e707e50fd73
with Chinese
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
lant19 | 0:2e707e50fd73 | 1 | |
lant19 | 0:2e707e50fd73 | 2 | #include"PCA9685.h" |
lant19 | 0:2e707e50fd73 | 3 | #include"mbed.h" |
lant19 | 0:2e707e50fd73 | 4 | |
lant19 | 0:2e707e50fd73 | 5 | |
lant19 | 0:2e707e50fd73 | 6 | |
lant19 | 0:2e707e50fd73 | 7 | PCA9685 pwm(PB_7,PB_6);//设置SDA与SCL连接的引脚 |
lant19 | 0:2e707e50fd73 | 8 | |
lant19 | 0:2e707e50fd73 | 9 | void setServoPulse(uint8_t n, float pulse) { //n为通道,pulse为下降沿位置 |
lant19 | 0:2e707e50fd73 | 10 | pwm.setPWM(n, 0, pulse);//如果希望调整上升沿位置,可在main函数中可直接使用pwm.setPWM函数 |
lant19 | 0:2e707e50fd73 | 11 | } |
lant19 | 0:2e707e50fd73 | 12 | |
lant19 | 0:2e707e50fd73 | 13 | void initServoDriver() {//整体初始化 |
lant19 | 0:2e707e50fd73 | 14 | pwm.begin(); |
lant19 | 0:2e707e50fd73 | 15 | pwm.setPrescale(121); // 设置20ms用于通用伺服 |
lant19 | 0:2e707e50fd73 | 16 | pwm.frequencyI2C(400000); //400kHz 快速I2C通信 |
lant19 | 0:2e707e50fd73 | 17 | } |
lant19 | 0:2e707e50fd73 | 18 | |
lant19 | 0:2e707e50fd73 | 19 | int main() { |
lant19 | 0:2e707e50fd73 | 20 | |
lant19 | 0:2e707e50fd73 | 21 | while(1){ |
lant19 | 0:2e707e50fd73 | 22 | initServoDriver(); |
lant19 | 0:2e707e50fd73 | 23 | setServoPulse(0, 308); //运动试验 |
lant19 | 0:2e707e50fd73 | 24 | setServoPulse(1, 495); //运动试验 |
lant19 | 0:2e707e50fd73 | 25 | wait(1); |
lant19 | 0:2e707e50fd73 | 26 | setServoPulse(0, 135); |
lant19 | 0:2e707e50fd73 | 27 | setServoPulse(1, 308); |
lant19 | 0:2e707e50fd73 | 28 | wait(1); |
lant19 | 0:2e707e50fd73 | 29 | setServoPulse(1, 135); |
lant19 | 0:2e707e50fd73 | 30 | setServoPulse(0, 509); |
lant19 | 0:2e707e50fd73 | 31 | wait(1); |
lant19 | 0:2e707e50fd73 | 32 | for (int mov = 495; mov > 135; mov--){ |
lant19 | 0:2e707e50fd73 | 33 | setServoPulse(1, mov); |
lant19 | 0:2e707e50fd73 | 34 | wait(0.003);} |
lant19 | 0:2e707e50fd73 | 35 | for (int mov = 115; mov < 509; mov++){ |
lant19 | 0:2e707e50fd73 | 36 | setServoPulse(0, mov); |
lant19 | 0:2e707e50fd73 | 37 | wait(0.003); |
lant19 | 0:2e707e50fd73 | 38 | } |
lant19 | 0:2e707e50fd73 | 39 | } |
lant19 | 0:2e707e50fd73 | 40 | } |