微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

Arduino 与 Xbox 360 控制器的接口:Game Control Plus

如何解决Arduino 与 Xbox 360 控制器的接口:Game Control Plus

最近我一直在尝试使用有线 XBox 360 控制器与我的 Arduino Uno(通过处理)连接,我在测试电路中使用它来控制两个有刷电机。我遇到了 this 视频,该视频使用库控制单个伺服电机以及库作者制作的 videos。我对代码(来自第一个链接)进行了一些修改支持两个电机,并且它可以工作(有点)。唯一的问题是它接受来自我的蓝牙鼠标而不是 XBox 控制器的输入,这两个控制器都连接到我的笔记本电脑的 USB 端口。我设置了文本配置文件,以便“按钮 0”和“按钮 2” - 分别对应于 A 和 X 按钮 - 使 Arduino 上的两个引脚变高,它们馈入控制电机的两个晶体管的基极。相反,我的蓝牙鼠标上的鼠标左键和滚轮按钮控制这些输出

我对为什么会发生这种情况感到有些困惑,我尝试了一些不同的方法解决该问题。我以为在创建配置文件时(使用库附带的程序)我不小心选择了我的鼠标作为输入设备,所以我制作了另一个配置文件只是为了确保不是这种情况,尽管我是不完全确定配置文件中的内容指示要使用的正确设备。也许我错过了一些非常明显的东西,我只知道我需要第二双眼睛来为我检查。如果您有使用此库的经验,我们将不胜感激,谢谢。

///////////////////////////////////////////// ///////////////////////////////////////////////// ////////

配置文件

Tests the xBox controller with motors.
lMotor  Left Motor  1   BUTTON  Button 0    0   0.0 0.0
rMotor  Right Motor 1   BUTTON  Button 2    0   0.0 0.0

///////////////////////////////////////////// ///////////////////////////////////////////////// ////////

来自处理的 Java:

import processing.serial.*;

import net.java.games.input.*;
import org.gamecontrolplus.*;
import org.gamecontrolplus.gui.*;

import cc.arduino.*;
import org.firmata.*;

ControlDevice cont;
ControlIO control;

Arduino arduino;

//variables for the "A Button" and "X Button" on the xBox controller
ControlButton aButton;
ControlButton xButton;

//needed variables of type int for background function to work (change window color when buttons are pressed)
int aInt;
int xInt;

void setup() {
  size(360,200);
  
  control = ControlIO.getInstance(this);
  cont = control.getMatchedDevice("xBoxtest");
  
  if(cont == null) {
    println("Error,something went wrong");
    System.exit(-1);
    
  }
  //println(Arduino.list());
  arduino = new Arduino(this,Arduino.list()[0],57600);
  arduino.pinMode(8,Arduino.OUTPUT);
  arduino.pinMode(11,Arduino.OUTPUT);
  
}

//gets the input and is called in the looping function (void draw)
public void getUserinput() {
  //rMotor and lMotor are references to the configuration file
  aButton = cont.getButton("lMotor");
  xButton = cont.getButton("rMotor");
  
  aInt = 0;
  xInt = 0;
  
  if (aButton.pressed() == true) {
    aInt = 1;
    
  }
  
  if (xButton.pressed() == true) {
    xInt = 1;
    
  }
  
  
}

void draw() {
  getUserinput();
  
  //changes the color of the interactive window when the input changes
  background(100 * aInt,100,100 * xInt);
  
  arduino.digitalWrite(8,aInt * 255);
  arduino.digitalWrite(11,xInt * 255);

}

解决方法

更新:在查看了我以前不知道的库提供的一些示例代码后,我能够解决这个问题。在我的辩护中,这个库几乎没有支持,我观看的视频没有使用我在示例中找到的代码行。我希望这对未来的某人有益。

我最终改变了这个:

cont = control.getMatchedDevice("xboxtest");

为此:

cont = control.filter(GCP.GAMEPAD).getMatchedDevice("xboxtest");

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。