极客工坊

 找回密码
 注册

QQ登录

只需一步,快速开始

查看: 17910|回复: 7

各位大神~小弟新人有关于ps3手柄操控舵机的问题想请教~

[复制链接]
发表于 2014-4-7 01:43:31 | 显示全部楼层 |阅读模式
本帖最后由 pipiwapipiwa 于 2014-4-7 01:53 编辑

小弟新人,刚接触arduino板,现小弟的任务是通过PS3无线蓝牙手柄操控几个舵机及电机。目前已购入USB HOST Shield并下载了对应的lib,经example测试通讯正常,串口监视器可以收到对应的消息,步进电机已可以工作。但舵机方面有如下几个问题:
1.有时候手柄连接不上蓝牙适配器,需要反复插几次才可以。
2.连接舵机后,加入如下代码:
a.在setup()上:
Servo myservo;
int pos = 0;
b.在setup()中:
myservo.attach(9);
c.在loop()中:
for(pos = 0; pos < 70; pos += 1)   
  {                                 
      Serial.print(F("\r\nppp"));
    myservo.write(pos);            
    delay(15);                     
  }
  for(pos = 70; pos>=1; pos-=1)   
  {                                
    myservo.write(pos);              
    delay(15);                     
  }

结果是:
1.舵机不工作,但串口监视器中字符串"ppp"一直在显示,表明进入循环了。
2.蓝牙手柄无法连接蓝牙适配器,四个灯一直在闪。

经数次检查连线应无误,单独的sweep example也可正常工作,求大神指导小弟应该怎么办或者换用什么方法可以完成通过PS3手柄的按键来操控舵机及电机呢?感激不尽!!

本帖子中包含更多资源

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

x
回复

使用道具 举报

 楼主| 发表于 2014-4-7 01:45:27 | 显示全部楼层
  1. /*
  2. Example sketch for the PS3 Bluetooth library - developed by Kristian Lauszus
  3. For more information visit my blog: http://blog.tkjelectronics.dk/ or
  4. send me an e-mail:  [email protected]
  5. */

  6. #include <PS3BT.h>
  7. #include <usbhub.h>
  8. #include <Servo.h>
  9. // Satisfy IDE, which only needs to see the include statment in the ino.
  10. #ifdef dobogusinclude
  11. #include <spi4teensy3.h>
  12. #endif
  13. USB Usb;

  14. //USBHub Hub1(&Usb); // Some dongles have a hub inside

  15. BTD Btd(&Usb); // You have to create the Bluetooth Dongle instance like so
  16. /* You can create the instance of the class in two ways */
  17. PS3BT PS3(&Btd); // This will just create the instance
  18. //PS3BT PS3(&Btd, 0x00, 0x15, 0x83, 0x3D, 0x0A, 0x57); // This will also store the bluetooth address - this can be obtained from the dongle when running the sketch

  19. boolean printTemperature;
  20. boolean printAngle;
  21. Servo myservo;
  22. int pos = 0;
  23. void setup() {
  24.   myservo.attach(9);
  25.   Serial.begin(115200);
  26.   while (!Serial); // Wait for serial port to connect - used on Leonardo, Teensy and other boards with built-in USB CDC serial connection
  27.   
  28.   Serial.print(F("\r\nPS3 Bluetooth Library Started"));
  29. }

  30. void loop() {
  31.   Usb.Task();
  32.   Serial.print(F("\r\nttt"));
  33. for(pos = 0; pos < 70; pos += 1)  // goes from 0 degrees to 180 degrees
  34.   {                                  // in steps of 1 degree
  35.       Serial.print(F("\r\nppp"));
  36.     myservo.write(pos);              // tell servo to go to position in variable 'pos'
  37.     delay(15);                       // waits 15ms for the servo to reach the position
  38.   }
  39.   for(pos = 70; pos>=1; pos-=1)     // goes from 180 degrees to 0 degrees
  40.   {                                
  41.     myservo.write(pos);              // tell servo to go to position in variable 'pos'
  42.     delay(15);                       // waits 15ms for the servo to reach the position
  43.   }
  44.   if (PS3.PS3Connected || PS3.PS3NavigationConnected) {
  45.     if (PS3.getAnalogHat(LeftHatX) > 137 || PS3.getAnalogHat(LeftHatX) < 117 || PS3.getAnalogHat(LeftHatY) > 137 || PS3.getAnalogHat(LeftHatY) < 117 || PS3.getAnalogHat(RightHatX) > 137 || PS3.getAnalogHat(RightHatX) < 117 || PS3.getAnalogHat(RightHatY) > 137 || PS3.getAnalogHat(RightHatY) < 117) {
  46.       Serial.print(F("\r\nLeftHatX: "));
  47.       Serial.print(PS3.getAnalogHat(LeftHatX));
  48.       Serial.print(F("\tLeftHatY: "));
  49.       Serial.print(PS3.getAnalogHat(LeftHatY));
  50.       if (PS3.PS3Connected) { // The Navigation controller only have one joystick
  51.         Serial.print(F("\tRightHatX: "));
  52.         Serial.print(PS3.getAnalogHat(RightHatX));
  53.         Serial.print(F("\tRightHatY: "));
  54.         Serial.print(PS3.getAnalogHat(RightHatY));
  55.       }
  56.     }

  57.     // Analog button values can be read from almost all buttons
  58.     if (PS3.getAnalogButton(L2) || PS3.getAnalogButton(R2)) {
  59.       Serial.print(F("\r\nL2: "));
  60.       Serial.print(PS3.getAnalogButton(L2));
  61.       if (PS3.PS3Connected) {
  62.         Serial.print(F("\tR2: "));
  63.         Serial.print(PS3.getAnalogButton(R2));
  64.       }
  65.     }
  66.     if (PS3.getButtonClick(PS)) {
  67.       Serial.print(F("\r\nPS"));
  68.       PS3.disconnect();
  69.     }
  70.     else {
  71.       if (PS3.getButtonClick(TRIANGLE))
  72.         Serial.print(F("\r\nTraingle"));
  73.       if (PS3.getButtonClick(CIRCLE))
  74.         Serial.print(F("\r\nCircle"));
  75.       if (PS3.getButtonClick(CROSS))
  76.       {
  77.        Serial.print(F("\r\nCROSS"));
  78.       }
  79.       if (PS3.getButtonClick(SQUARE))
  80.       {
  81.         Serial.print(F("\r\nSquare"));
  82.       
  83.       }

  84.       if (PS3.getButtonClick(UP)) {
  85.         Serial.print(F("\r\nUp"));
  86.         if (PS3.PS3Connected) {
  87.           PS3.setLedOff();
  88.           PS3.setLedOn(LED4);
  89.         }
  90.       }
  91.       if (PS3.getButtonClick(RIGHT)) {
  92.         Serial.print(F("\r\nRight"));
  93.         if (PS3.PS3Connected) {
  94.           PS3.setLedOff();
  95.           PS3.setLedOn(LED1);
  96.         }
  97.       }
  98.       if (PS3.getButtonClick(DOWN)) {
  99.         Serial.print(F("\r\nDown"));
  100.         if (PS3.PS3Connected) {
  101.           PS3.setLedOff();
  102.           PS3.setLedOn(LED2);
  103.         }
  104.       }
  105.       if (PS3.getButtonClick(LEFT)) {
  106.         Serial.print(F("\r\nLeft"));
  107.         if (PS3.PS3Connected) {
  108.           PS3.setLedOff();
  109.           PS3.setLedOn(LED3);
  110.         }
  111.       }

  112.       if (PS3.getButtonClick(L1))
  113.         Serial.print(F("\r\nL1"));
  114.       if (PS3.getButtonClick(L3))
  115.         Serial.print(F("\r\nL3"));
  116.       if (PS3.getButtonClick(R1))
  117.         Serial.print(F("\r\nR1"));
  118.       if (PS3.getButtonClick(R3))
  119.         Serial.print(F("\r\nR3"));

  120.       if (PS3.getButtonClick(SELECT)) {
  121.         Serial.print(F("\r\nSelect - "));
  122.         PS3.printStatusString();
  123.       }
  124.       if (PS3.getButtonClick(START)) {
  125.         Serial.print(F("\r\nStart"));
  126.         printAngle = !printAngle;
  127.       }
  128.     }
  129.     if (printAngle) {
  130.       Serial.print(F("\r\nPitch: "));
  131.       Serial.print(PS3.getAngle(Pitch));
  132.       Serial.print(F("\tRoll: "));
  133.       Serial.print(PS3.getAngle(Roll));
  134.     }
  135.   }
  136.   else if (PS3.PS3MoveConnected) {
  137.     if (PS3.getAnalogButton(T)) {
  138.       Serial.print(F("\r\nT: "));
  139.       Serial.print(PS3.getAnalogButton(T));
  140.     }
  141.     if (PS3.getButtonClick(PS)) {
  142.       Serial.print(F("\r\nPS"));
  143.       PS3.disconnect();
  144.     }
  145.     else {
  146.       if (PS3.getButtonClick(SELECT)) {
  147.         Serial.print(F("\r\nSelect"));
  148.         printTemperature = !printTemperature;
  149.       }
  150.       if (PS3.getButtonClick(START)) {
  151.         Serial.print(F("\r\nStart"));
  152.         printAngle = !printAngle;
  153.       }
  154.       if (PS3.getButtonClick(TRIANGLE)) {
  155.         Serial.print(F("\r\nTriangle"));
  156.         PS3.moveSetBulb(Red);
  157.       }
  158.       if (PS3.getButtonClick(CIRCLE)) {
  159.         Serial.print(F("\r\nCircle"));
  160.         PS3.moveSetBulb(Green);
  161.       }
  162.       if (PS3.getButtonClick(SQUARE)) {
  163.         Serial.print(F("\r\nSquare"));
  164.         PS3.moveSetBulb(Blue);
  165.       }
  166.       if (PS3.getButtonClick(CROSS)) {
  167.         Serial.print(F("\r\nCross"));
  168.         PS3.moveSetBulb(Yellow);
  169.       }
  170.       if (PS3.getButtonClick(MOVE)) {
  171.         PS3.moveSetBulb(Off);
  172.         Serial.print(F("\r\nMove"));
  173.         Serial.print(F(" - "));
  174.         PS3.printStatusString();
  175.       }
  176.     }
  177.     if (printAngle) {
  178.       Serial.print(F("\r\nPitch: "));
  179.       Serial.print(PS3.getAngle(Pitch));
  180.       Serial.print(F("\tRoll: "));
  181.       Serial.print(PS3.getAngle(Roll));
  182.     }
  183.     else if (printTemperature) {
  184.       Serial.print(F("\r\nTemperature: "));
  185.       Serial.print(PS3.getTemperature());
  186.     }
  187.   }
  188. }
复制代码
回复 支持 反对

使用道具 举报

 楼主| 发表于 2014-4-7 01:46:27 | 显示全部楼层
  1. /*
  2. Example sketch for the PS3 Bluetooth library - developed by Kristian Lauszus
  3. For more information visit my blog: http://blog.tkjelectronics.dk/ or
  4. send me an e-mail:  [email protected]
  5. */

  6. #include <PS3BT.h>
  7. #include <usbhub.h>
  8. #include <Servo.h>
  9. // Satisfy IDE, which only needs to see the include statment in the ino.
  10. #ifdef dobogusinclude
  11. #include <spi4teensy3.h>
  12. #endif
  13. USB Usb;

  14. //USBHub Hub1(&Usb); // Some dongles have a hub inside

  15. BTD Btd(&Usb); // You have to create the Bluetooth Dongle instance like so
  16. /* You can create the instance of the class in two ways */
  17. PS3BT PS3(&Btd); // This will just create the instance
  18. //PS3BT PS3(&Btd, 0x00, 0x15, 0x83, 0x3D, 0x0A, 0x57); // This will also store the bluetooth address - this can be obtained from the dongle when running the sketch

  19. boolean printTemperature;
  20. boolean printAngle;
  21. Servo myservo;
  22. int pos = 0;
  23. void setup() {
  24.   myservo.attach(9);
  25.   Serial.begin(115200);
  26.   while (!Serial); // Wait for serial port to connect - used on Leonardo, Teensy and other boards with built-in USB CDC serial connection
  27.   
  28.   Serial.print(F("\r\nPS3 Bluetooth Library Started"));
  29. }

  30. void loop() {
  31.   Usb.Task();
  32.   Serial.print(F("\r\nttt"));
  33. for(pos = 0; pos < 70; pos += 1)  // goes from 0 degrees to 180 degrees
  34.   {                                  // in steps of 1 degree
  35.       Serial.print(F("\r\nppp"));
  36.     myservo.write(pos);              // tell servo to go to position in variable 'pos'
  37.     delay(15);                       // waits 15ms for the servo to reach the position
  38.   }
  39.   for(pos = 70; pos>=1; pos-=1)     // goes from 180 degrees to 0 degrees
  40.   {                                
  41.     myservo.write(pos);              // tell servo to go to position in variable 'pos'
  42.     delay(15);                       // waits 15ms for the servo to reach the position
  43.   }
  44.   if (PS3.PS3Connected || PS3.PS3NavigationConnected) {
  45.     if (PS3.getAnalogHat(LeftHatX) > 137 || PS3.getAnalogHat(LeftHatX) < 117 || PS3.getAnalogHat(LeftHatY) > 137 || PS3.getAnalogHat(LeftHatY) < 117 || PS3.getAnalogHat(RightHatX) > 137 || PS3.getAnalogHat(RightHatX) < 117 || PS3.getAnalogHat(RightHatY) > 137 || PS3.getAnalogHat(RightHatY) < 117) {
  46.       Serial.print(F("\r\nLeftHatX: "));
  47.       Serial.print(PS3.getAnalogHat(LeftHatX));
  48.       Serial.print(F("\tLeftHatY: "));
  49.       Serial.print(PS3.getAnalogHat(LeftHatY));
  50.       if (PS3.PS3Connected) { // The Navigation controller only have one joystick
  51.         Serial.print(F("\tRightHatX: "));
  52.         Serial.print(PS3.getAnalogHat(RightHatX));
  53.         Serial.print(F("\tRightHatY: "));
  54.         Serial.print(PS3.getAnalogHat(RightHatY));
  55.       }
  56.     }

  57.     // Analog button values can be read from almost all buttons
  58.     if (PS3.getAnalogButton(L2) || PS3.getAnalogButton(R2)) {
  59.       Serial.print(F("\r\nL2: "));
  60.       Serial.print(PS3.getAnalogButton(L2));
  61.       if (PS3.PS3Connected) {
  62.         Serial.print(F("\tR2: "));
  63.         Serial.print(PS3.getAnalogButton(R2));
  64.       }
  65.     }
  66.     if (PS3.getButtonClick(PS)) {
  67.       Serial.print(F("\r\nPS"));
  68.       PS3.disconnect();
  69.     }
  70.     else {
  71.       if (PS3.getButtonClick(TRIANGLE))
  72.         Serial.print(F("\r\nTraingle"));
  73.       if (PS3.getButtonClick(CIRCLE))
  74.         Serial.print(F("\r\nCircle"));
  75.       if (PS3.getButtonClick(CROSS))
  76.       {
  77.        Serial.print(F("\r\nCROSS"));
  78.       }
  79.       if (PS3.getButtonClick(SQUARE))
  80.       {
  81.         Serial.print(F("\r\nSquare"));
  82.       
  83.       }

  84.       if (PS3.getButtonClick(UP)) {
  85.         Serial.print(F("\r\nUp"));
  86.         if (PS3.PS3Connected) {
  87.           PS3.setLedOff();
  88.           PS3.setLedOn(LED4);
  89.         }
  90.       }
  91.       if (PS3.getButtonClick(RIGHT)) {
  92.         Serial.print(F("\r\nRight"));
  93.         if (PS3.PS3Connected) {
  94.           PS3.setLedOff();
  95.           PS3.setLedOn(LED1);
  96.         }
  97.       }
  98.       if (PS3.getButtonClick(DOWN)) {
  99.         Serial.print(F("\r\nDown"));
  100.         if (PS3.PS3Connected) {
  101.           PS3.setLedOff();
  102.           PS3.setLedOn(LED2);
  103.         }
  104.       }
  105.       if (PS3.getButtonClick(LEFT)) {
  106.         Serial.print(F("\r\nLeft"));
  107.         if (PS3.PS3Connected) {
  108.           PS3.setLedOff();
  109.           PS3.setLedOn(LED3);
  110.         }
  111.       }

  112.       if (PS3.getButtonClick(L1))
  113.         Serial.print(F("\r\nL1"));
  114.       if (PS3.getButtonClick(L3))
  115.         Serial.print(F("\r\nL3"));
  116.       if (PS3.getButtonClick(R1))
  117.         Serial.print(F("\r\nR1"));
  118.       if (PS3.getButtonClick(R3))
  119.         Serial.print(F("\r\nR3"));

  120.       if (PS3.getButtonClick(SELECT)) {
  121.         Serial.print(F("\r\nSelect - "));
  122.         PS3.printStatusString();
  123.       }
  124.       if (PS3.getButtonClick(START)) {
  125.         Serial.print(F("\r\nStart"));
  126.         printAngle = !printAngle;
  127.       }
  128.     }
  129.     if (printAngle) {
  130.       Serial.print(F("\r\nPitch: "));
  131.       Serial.print(PS3.getAngle(Pitch));
  132.       Serial.print(F("\tRoll: "));
  133.       Serial.print(PS3.getAngle(Roll));
  134.     }
  135.   }
  136.   else if (PS3.PS3MoveConnected) {
  137.     if (PS3.getAnalogButton(T)) {
  138.       Serial.print(F("\r\nT: "));
  139.       Serial.print(PS3.getAnalogButton(T));
  140.     }
  141.     if (PS3.getButtonClick(PS)) {
  142.       Serial.print(F("\r\nPS"));
  143.       PS3.disconnect();
  144.     }
  145.     else {
  146.       if (PS3.getButtonClick(SELECT)) {
  147.         Serial.print(F("\r\nSelect"));
  148.         printTemperature = !printTemperature;
  149.       }
  150.       if (PS3.getButtonClick(START)) {
  151.         Serial.print(F("\r\nStart"));
  152.         printAngle = !printAngle;
  153.       }
  154.       if (PS3.getButtonClick(TRIANGLE)) {
  155.         Serial.print(F("\r\nTriangle"));
  156.         PS3.moveSetBulb(Red);
  157.       }
  158.       if (PS3.getButtonClick(CIRCLE)) {
  159.         Serial.print(F("\r\nCircle"));
  160.         PS3.moveSetBulb(Green);
  161.       }
  162.       if (PS3.getButtonClick(SQUARE)) {
  163.         Serial.print(F("\r\nSquare"));
  164.         PS3.moveSetBulb(Blue);
  165.       }
  166.       if (PS3.getButtonClick(CROSS)) {
  167.         Serial.print(F("\r\nCross"));
  168.         PS3.moveSetBulb(Yellow);
  169.       }
  170.       if (PS3.getButtonClick(MOVE)) {
  171.         PS3.moveSetBulb(Off);
  172.         Serial.print(F("\r\nMove"));
  173.         Serial.print(F(" - "));
  174.         PS3.printStatusString();
  175.       }
  176.     }
  177.     if (printAngle) {
  178.       Serial.print(F("\r\nPitch: "));
  179.       Serial.print(PS3.getAngle(Pitch));
  180.       Serial.print(F("\tRoll: "));
  181.       Serial.print(PS3.getAngle(Roll));
  182.     }
  183.     else if (printTemperature) {
  184.       Serial.print(F("\r\nTemperature: "));
  185.       Serial.print(PS3.getTemperature());
  186.     }
  187.   }
  188. }
复制代码
回复 支持 反对

使用道具 举报

发表于 2014-4-7 09:38:18 | 显示全部楼层
既然 "蓝牙手柄无法连接蓝牙适配器,四个灯一直在闪。", 是否應先解決 手柄連接的問題?
回复 支持 反对

使用道具 举报

发表于 2014-4-9 20:08:53 | 显示全部楼层
舵机不工作是不是舵机控制口被占用了?也许PS3库与舵机库在IO口上有冲突,你打开这两个库看看。
回复 支持 反对

使用道具 举报

发表于 2014-8-4 13:00:22 | 显示全部楼层
应该需要PS3手柄接收器
回复 支持 反对

使用道具 举报

发表于 2014-8-5 09:41:12 | 显示全部楼层
啥都先不说,第一感觉就是供电不足。单靠板上供电很难带动大舵机
回复 支持 反对

使用道具 举报

发表于 2017-4-20 22:56:52 | 显示全部楼层
接PS2手柄,arduino直接驱动舵机这种感性电机时,会出现重新配对的情况,应该是要另外的电源接舵机吧。
回复 支持 反对

使用道具 举报

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

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

Archiver|联系我们|极客工坊

GMT+8, 2024-3-28 21:03 , Processed in 0.051308 second(s), 24 queries .

Powered by Discuz! X3.4 Licensed

Copyright © 2001-2021, Tencent Cloud.

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