将 Arduino 与 Xbox 360 控制器连接:Game Control Plus
Interfacing Arduino with Xbox 360 Controller: Game Control Plus
最近我一直在尝试使用有线 Xbox 360 控制器与我的 Arduino Uno 接口(通过处理),我在测试电路中使用它来控制两个有刷电机。我遇到了图书馆作者制作的 this video that uses the library to control a single servo motor along with videos。我对代码进行了一些修改(从第一个 link 开始)只是为了支持这两个电机,并且它可以工作(有点)。唯一的问题是它接受来自我的蓝牙鼠标而不是 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");
最近我一直在尝试使用有线 Xbox 360 控制器与我的 Arduino Uno 接口(通过处理),我在测试电路中使用它来控制两个有刷电机。我遇到了图书馆作者制作的 this video that uses the library to control a single servo motor along with videos。我对代码进行了一些修改(从第一个 link 开始)只是为了支持这两个电机,并且它可以工作(有点)。唯一的问题是它接受来自我的蓝牙鼠标而不是 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");