Cheetah Software  1.0
GameController.cpp
Go to the documentation of this file.
1 
11 #include "GameController.h"
12 
13 #include <QtCore/QObject>
14 #include <QtGamepad/QGamepad>
15 
22 GameController::GameController(QObject *parent) : QObject(parent) {
24 }
25 
32  delete _qGamepad;
33  _qGamepad = nullptr; // in case this doesn't work!
34 
35  auto gamepadList = QGamepadManager::instance()->connectedGamepads();
36  if (gamepadList.empty()) {
37  printf(
38  "[ERROR: GameController] No controller was connected! All joystick "
39  "commands will be zero!\n");
40  } else {
41  if (gamepadList.size() > 1) {
42  printf(
43  "[ERROR: GameController] There are %d joysticks connected. Using "
44  "the first one.\n",
45  gamepadList.size());
46  } else {
47  printf("[GameController] Found 1 joystick\n");
48  }
49 
50  _qGamepad = new QGamepad(*gamepadList.begin());
51  }
52 }
53 
60  if (_qGamepad) {
61  gamepadCommand.leftBumper = _qGamepad->buttonL1();
62  gamepadCommand.rightBumper = _qGamepad->buttonR1();
63  gamepadCommand.leftTriggerButton = _qGamepad->buttonL2() != 0.;
64  gamepadCommand.rightTriggerButton = _qGamepad->buttonR2() != 0.;
65  gamepadCommand.back = _qGamepad->buttonSelect();
66  gamepadCommand.start = _qGamepad->buttonStart();
67  gamepadCommand.a = _qGamepad->buttonA();
68  gamepadCommand.b = _qGamepad->buttonB();
69  gamepadCommand.x = _qGamepad->buttonX();
70  gamepadCommand.y = _qGamepad->buttonY();
71  gamepadCommand.leftStickButton = _qGamepad->buttonL3();
72  gamepadCommand.rightStickButton = _qGamepad->buttonR3();
73  gamepadCommand.leftTriggerAnalog = (float)_qGamepad->buttonL2();
74  gamepadCommand.rightTriggerAnalog = (float)_qGamepad->buttonR2();
75  gamepadCommand.leftStickAnalog =
76  Vec2<float>(_qGamepad->axisLeftX(), -_qGamepad->axisLeftY());
77  gamepadCommand.rightStickAnalog =
78  Vec2<float>(_qGamepad->axisRightX(), -_qGamepad->axisRightY());
79  } else {
80  gamepadCommand.zero(); // no joystick, return all zeros
81  }
82 
83  // printf("%s\n", gamepadCommand.toString().c_str());
84 }
85 
QGamepad * _qGamepad
Vec2< float > rightStickAnalog
void updateGamepadCommand(GamepadCommand &gamepadCommand)
GameController(QObject *parent=0)
typename Eigen::Matrix< T, 2, 1 > Vec2
Definition: cppTypes.h:22
Vec2< float > leftStickAnalog
float rightTriggerAnalog
Code to read the Logitech F310 Game Controller Creates a DriverCommand object to be sent to the robot...
void findNewController()
float leftTriggerAnalog