arduino与xbox 360控制器的接口:game control plus

h43kikqp  于 2021-06-27  发布在  Java
关注(0)|答案(1)|浏览(744)

最近,我一直在尝试使用有线Xbox360控制器与我的arduino uno接口(通过处理),我在测试电路中使用它来控制两个有刷电机。我遇到了这个视频,使用图书馆来控制一个单一的伺服电机连同图书馆的作者所作的视频。我对代码做了一些修改(从第一个链接开始),只是为了支持这两个马达,它可以工作(有点)。唯一的问题是,它接受来自我的蓝牙鼠标而不是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);

}
rjee0c15

rjee0c151#

更新:在查看了库提供的一些我以前不知道的示例代码之后,我能够修复这个问题。在我的辩护中,这个库几乎没有支持,我观看的视频没有使用我在示例中找到的代码行。我希望这对将来的人有好处。
最后我改变了这个:

cont = control.getMatchedDevice("xboxtest");

对此:

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

相关问题