极客工坊

 找回密码
 注册

QQ登录

只需一步,快速开始

查看: 16761|回复: 2

关于Adafruit_PWMServoDriver库的使用

[复制链接]
发表于 2015-10-9 22:58:34 | 显示全部楼层 |阅读模式
买了这种驱动板

示例代码如下:
  1. #include <Wire.h>
  2. #include <Adafruit_PWMServoDriver.h>

  3. // called this way, it uses the default address 0x40
  4. Adafruit_PWMServoDriver pwm = Adafruit_PWMServoDriver();
  5. // you can also call it with a different address you want
  6. //Adafruit_PWMServoDriver pwm = Adafruit_PWMServoDriver(0x41);

  7. // Depending on your servo make, the pulse width min and max may vary, you
  8. // want these to be as small/large as possible without hitting the hard stop
  9. // for max range. You'll have to tweak them as necessary to match the servos you
  10. // have!
  11. #define SERVOMIN  150 // this is the 'minimum' pulse length count (out of 4096)
  12. #define SERVOMAX  600 // this is the 'maximum' pulse length count (out of 4096)

  13. // our servo # counter
  14. //uint8_t servonum = 0;

  15. void setup() {
  16.   Serial.begin(9600);
  17.   Serial.println("16 channel Servo test!");

  18.   pwm.begin();
  19.   
  20.   pwm.setPWMFreq(60);  // Analog servos run at ~60 Hz updates
  21. }

  22. // you can use this function if you'd like to set the pulse length in seconds
  23. // e.g. setServoPulse(0, 0.001) is a ~1 millisecond pulse width. its not precise!
  24. void setServoPulse(uint8_t n, double pulse) {
  25.   double pulselength;
  26.   
  27.   pulselength = 1000000;   // 1,000,000 us per second
  28.   pulselength /= 60;   // 60 Hz
  29.   Serial.print(pulselength); Serial.println(" us per period");
  30.   pulselength /= 4096;  // 12 bits of resolution
  31.   Serial.print(pulselength); Serial.println(" us per bit");
  32.   pulse *= 1000;
  33.   pulse /= pulselength;
  34.   Serial.println(pulse);
  35.   pwm.setPWM(n, 0, pulse);
  36. }

  37. void loop() {
  38.   // Drive each servo one at a time
  39.   //Serial.println(servonum);
  40.   for (uint16_t pulselen = SERVOMIN; pulselen < SERVOMAX; pulselen++) {
  41.     pwm.setPWM(0, 0, pulselen);
  42.     pwm.setPWM(1, 0, pulselen);
  43.     pwm.setPWM(2, 0, pulselen);
  44.     pwm.setPWM(3, 0, pulselen);
  45.     pwm.setPWM(4, 0, pulselen);
  46.     pwm.setPWM(5, 0, pulselen);
  47.     pwm.setPWM(6, 0, pulselen);
  48.     pwm.setPWM(7, 0, pulselen);
  49.     pwm.setPWM(8, 0, pulselen);
  50.     pwm.setPWM(9, 0, pulselen);
  51.     pwm.setPWM(10, 0, pulselen);
  52.     pwm.setPWM(11, 0, pulselen);
  53.     pwm.setPWM(12, 0, pulselen);
  54.     pwm.setPWM(13, 0, pulselen);
  55.     pwm.setPWM(14, 0, pulselen);
  56.   }
  57.   delay(500);
  58.   for (uint16_t pulselen = SERVOMAX; pulselen > SERVOMIN; pulselen--) {
  59.     pwm.setPWM(0, 0, pulselen);
  60.     pwm.setPWM(0, 0, pulselen);
  61.     pwm.setPWM(1, 0, pulselen);
  62.     pwm.setPWM(2, 0, pulselen);
  63.     pwm.setPWM(3, 0, pulselen);
  64.     pwm.setPWM(4, 0, pulselen);
  65.     pwm.setPWM(5, 0, pulselen);
  66.     pwm.setPWM(6, 0, pulselen);
  67.     pwm.setPWM(7, 0, pulselen);
  68.     pwm.setPWM(8, 0, pulselen);
  69.     pwm.setPWM(9, 0, pulselen);
  70.     pwm.setPWM(10, 0, pulselen);
  71.     pwm.setPWM(11, 0, pulselen);
  72.     pwm.setPWM(12, 0, pulselen);
  73.     pwm.setPWM(13, 0, pulselen);
  74.     pwm.setPWM(14, 0, pulselen);
  75.   }
  76.   delay(500);
  77. }
复制代码


我对里面的参数不太明白,求教

我要让舵机分别转到0度,90度,180度,应该传入多少?

回复

使用道具 举报

 楼主| 发表于 2015-10-9 23:05:09 | 显示全部楼层
本帖最后由 绿林网页 于 2015-10-9 23:08 编辑




找到一点资料,看不太懂,谁能解释一下!

我要让舵机分别转到0度,90度,180度,应该传入多少?

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有帐号?注册

x
回复 支持 反对

使用道具 举报

发表于 2015-10-27 21:49:36 | 显示全部楼层
我也买了这个驱动板,但是我使用的是stm32的单片机,我一直在尝试这个地址到底是不是0x40或者0x41,我一直没有弄好。_i2caddr = addr;这个_i2caddr 到底是多少
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则 需要先绑定手机号

Archiver|联系我们|极客工坊

GMT+8, 2024-4-19 16:53 , Processed in 0.044569 second(s), 19 queries .

Powered by Discuz! X3.4 Licensed

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表