Skip to content

Commit d27139b

Browse files
committed
Switch to using GameController by default, use Robot constructor, and fix some bugs in SDL usage
1 parent 15b8fb4 commit d27139b

9 files changed

Lines changed: 49 additions & 51 deletions

File tree

CMakeLists.txt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,8 @@ find_package(SDL2 REQUIRED)
2222
add_definitions(-DUNIT_LIB_DISABLE_FMT -DUNIT_LIB_ENABLE_IOSTREAM)
2323

2424
# Add all CPP files to the executable
25-
# Note: Users using GameController should swap out Joystick.cpp with GameController.cpp
26-
add_executable(${PROJECT_NAME} main.cpp RobotBase.cpp Joystick.cpp)
25+
# Note: Users using Joystick should swap out GameController.cpp with Joystick.cpp
26+
add_executable(${PROJECT_NAME} main.cpp RobotBase.cpp GameController.cpp)
2727

2828
# Specify libraries to link against
2929
target_link_libraries(${PROJECT_NAME} phoenix6)

GameController.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ bool GameController::IsConnected() const
1212

1313
/* poll for game controller disconnects */
1414
SDL_Event event;
15-
if (SDL_PollEvent(&event)) {
15+
while (SDL_PollEvent(&event)) {
1616
if (event.type == SDL_QUIT) {
1717
/* SDL shut down */
1818
return false;

GameController.hpp

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,6 @@
88

99
/**
1010
* Manages a game controller using the SDL 2 library.
11-
*
12-
* Note: This requires Ubuntu 22.04+ or Debian Bullseye.
1311
*/
1412
class GameController {
1513
private:
@@ -155,7 +153,7 @@ class GameController {
155153
}
156154

157155
private:
158-
static constexpr auto kErrorTimeMs = 2000;
156+
static constexpr auto kErrorTimeMs = 3000;
159157
std::chrono::time_point<std::chrono::steady_clock> _lastErrorTime = std::chrono::steady_clock::now();
160158

161159
/** Reports a missing game controller with debouncing. */

Joystick.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ bool Joystick::IsConnected() const
1212

1313
/* poll for joystick disconnects */
1414
SDL_Event event;
15-
if (SDL_PollEvent(&event)) {
15+
while (SDL_PollEvent(&event)) {
1616
if (event.type == SDL_QUIT) {
1717
/* SDL shut down */
1818
return false;

Joystick.hpp

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,8 @@
99
/**
1010
* Manages a joystick using the SDL 2 library.
1111
*
12-
* Note: This is a legacy class for users on Ubuntu
13-
* 20.04 and older. Users on newer systems
14-
* can use the GameController class.
12+
* Note: This is a legacy class for users with
13+
* joysticks not supported by GameController.
1514
*/
1615
class Joystick {
1716
private:
@@ -193,7 +192,7 @@ class Joystick {
193192
}
194193

195194
private:
196-
static constexpr auto kErrorTimeMs = 2000;
195+
static constexpr auto kErrorTimeMs = 3000;
197196
std::chrono::time_point<std::chrono::steady_clock> _lastErrorTime = std::chrono::steady_clock::now();
198197

199198
/** Reports a missing joystick with debouncing. */

RobotBase.cpp

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,9 @@
11
#include "RobotBase.hpp"
2-
#include "ctre/phoenix6/unmanaged/Unmanaged.hpp"
2+
#include <ctre/phoenix6/unmanaged/Unmanaged.hpp>
33

44
int RobotBase::Run()
55
{
6-
printf("Starting robot program...\n");
7-
8-
/* this is robot startup, run robot init */
9-
RobotInit();
6+
printf("Robot program startup complete\n");
107

118
while (IsRunning()) {
129
auto const start = std::chrono::steady_clock::now();
@@ -59,10 +56,6 @@ int RobotBase::Run()
5956
SleepFor(0_ms);
6057
}
6158
}
62-
63-
/* program shutting down */
64-
printf("Stopping robot program...\n");
65-
6659
return 0;
6760
}
6861

RobotBase.hpp

Lines changed: 24 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
#pragma once
22

3-
#include "units/time.h"
3+
#include <units/time.h>
44
#include <chrono>
55
#include <optional>
66
#include <thread>
@@ -10,8 +10,26 @@
1010
* Manages a robot program.
1111
*/
1212
class RobotBase {
13+
units::millisecond_t _loopTime;
14+
std::optional<bool> _lastEnabled;
15+
1316
public:
14-
virtual void RobotInit() = 0;
17+
RobotBase(units::millisecond_t loopTime = 20_ms) :
18+
_loopTime{loopTime}
19+
{}
20+
21+
template <std::derived_from<RobotBase> Robot, typename... Args>
22+
static int StartRobot(Args&&... args)
23+
{
24+
printf("Starting robot program...\n");
25+
26+
Robot robot(std::forward<Args>(args)...);
27+
int const retval = robot.Run();
28+
29+
printf("Stopping robot program...\n");
30+
return retval;
31+
}
32+
1533
virtual void RobotPeriodic() = 0;
1634

1735
virtual bool IsEnabled() = 0;
@@ -23,11 +41,6 @@ class RobotBase {
2341

2442
virtual bool IsRunning() { return true; }
2543

26-
private:
27-
units::millisecond_t _loopTime = 20_ms;
28-
std::optional<bool> _lastEnabled;
29-
30-
public:
3144
/**
3245
* Sleeps for the specified amount of time.
3346
*/
@@ -44,15 +57,15 @@ class RobotBase {
4457
_loopTime = loopTime;
4558
}
4659

60+
private:
61+
static constexpr auto kErrorTimeMs = 3000;
62+
std::chrono::time_point<std::chrono::steady_clock> _lastErrorTime = std::chrono::steady_clock::now();
63+
4764
/**
4865
* Runs the robot program.
4966
*/
5067
int Run();
5168

52-
private:
53-
static constexpr auto kErrorTimeMs = 3000;
54-
std::chrono::time_point<std::chrono::steady_clock> _lastErrorTime = std::chrono::steady_clock::now();
55-
5669
/** Reports a loop overrun with debouncing. */
5770
void ReportLoopOverrun(units::millisecond_t measured);
5871
};

main.cpp

Lines changed: 14 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
#include "ctre/phoenix6/TalonFX.hpp"
1+
#include <ctre/phoenix6/TalonFX.hpp>
22
#include "RobotBase.hpp"
3-
#include "Joystick.hpp"
3+
#include "GameController.hpp"
44

55
using namespace ctre::phoenix6;
66

@@ -25,11 +25,12 @@ class Robot : public RobotBase {
2525
controls::DutyCycleOut rightOut{0};
2626

2727
/* joystick */
28-
Joystick joy{0};
28+
GameController joy{0};
2929

3030
public:
31+
Robot();
32+
3133
/* main robot interface */
32-
void RobotInit() override;
3334
void RobotPeriodic() override;
3435

3536
bool IsEnabled() override;
@@ -40,10 +41,8 @@ class Robot : public RobotBase {
4041
void DisabledPeriodic() override;
4142
};
4243

43-
/**
44-
* Runs once at code initialization.
45-
*/
46-
void Robot::RobotInit()
44+
Robot::Robot()
45+
// : RobotBase{20_ms} // optionally specify loop time for periodic calls
4746
{
4847
configs::TalonFXConfiguration fx_cfg{};
4948

@@ -74,10 +73,8 @@ void Robot::RobotPeriodic()
7473
*/
7574
bool Robot::IsEnabled()
7675
{
77-
/* enable while joystick is an Xbox controller (6 axes),
78-
* and we are holding the right bumper */
79-
if (joy.GetNumAxes() < 6) return false;
80-
return joy.GetButton(5); // SDL_CONTROLLER_BUTTON_RIGHTSHOULDER
76+
/* enable while holding the right bumper */
77+
return joy.GetButton(SDL_CONTROLLER_BUTTON_RIGHTSHOULDER);
8178
}
8279

8380
/**
@@ -91,11 +88,11 @@ void Robot::EnabledInit() {}
9188
void Robot::EnabledPeriodic()
9289
{
9390
/* arcade drive */
94-
double speed = -joy.GetAxis(1); // SDL_CONTROLLER_AXIS_LEFTY
95-
double turn = joy.GetAxis(4); // SDL_CONTROLLER_AXIS_RIGHTX
91+
double speed = -joy.GetAxis(SDL_CONTROLLER_AXIS_LEFTY);
92+
double turn = -joy.GetAxis(SDL_CONTROLLER_AXIS_RIGHTX);
9693

97-
leftOut.Output = speed + turn;
98-
rightOut.Output = speed - turn;
94+
leftOut.Output = speed - turn;
95+
rightOut.Output = speed + turn;
9996

10097
leftLeader.SetControl(leftOut);
10198
rightLeader.SetControl(rightOut);
@@ -120,7 +117,5 @@ void Robot::DisabledPeriodic()
120117
int main()
121118
{
122119
/* create and run robot */
123-
Robot robot{};
124-
// robot.SetLoopTime(20_ms); // optionally change loop time for periodic calls
125-
return robot.Run();
120+
return RobotBase::StartRobot<Robot>();
126121
}

readme.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111

1212
The main robot program is located inside main.cpp.
1313

14-
By default, the Joystick class is used for controller input. Users may choose to use the GameController class instead.
14+
By default, the GameController class is used for controller input. Users may choose to use the Joystick class instead.
1515

1616
# Build Process
1717

0 commit comments

Comments
 (0)