gpt4 book ai didi

java - 将 Arduino 与 Xbox 360 Controller 连接 : Game Control Plus

转载 作者:行者123 更新时间:2023-12-05 06:54:31 29 4
gpt4 key购买 nike

最近我一直在尝试使用有线 Xbox 360 Controller 与我的 Arduino Uno 接口(interface)(通过处理),我在测试电路中使用它来控制两个有刷电机。我遇到了this使用该库控制单个伺服电机的视频以及 videos由图书馆的作者制作。我对代码(从第一个链接)进行了一些修改以支持这两个电机,并且它可以工作(有点)。唯一的问题是它接受来 self 的蓝牙鼠标而不是 Xbox Controller 的输入,这两个 Controller 都连接到我的笔记本电脑的 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");

关于java - 将 Arduino 与 Xbox 360 Controller 连接 : Game Control Plus,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65514034/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com