Skip to content

armgit / flix

Public Imported from GitHub
0

Making an ESP32-based quadcopter from scratch

Fix joystick work in simulation Logic was broken as joystickGet never got called

Oleg Kalachev43be27c43dc3
diff --git a/gazebo/SBUS.h b/gazebo/SBUS.h
index c687239..f57db7f 100644
--- a/gazebo/SBUS.h
+++ b/gazebo/SBUS.h
@@ -14,7 +14,7 @@ public:
SBUS(HardwareSerial& bus, const bool inv = true) {};
SBUS(HardwareSerial& bus, const int8_t rxpin, const int8_t txpin, const bool inv = true) {};
void begin() {};
- bool read() { return joystickInitialized; };
+ bool read() { return joystickInit(); };
SBUSData data() {
SBUSData data;
joystickGet(data.ch);
diff --git a/gazebo/joystick.h b/gazebo/joystick.h
index 8c8ba84..3833340 100644
--- a/gazebo/joystick.h
+++ b/gazebo/joystick.h
@@ -15,9 +15,12 @@
#define RC_CHANNEL_MODE 4
SDL_Joystick *joystick;
-bool joystickInitialized = false, warnShown = false;
-void joystickInit() {
+bool joystickInit() {
+ static bool joystickInitialized = false;
+ static bool warnShown = false;
+ if (joystickInitialized) return true;
+
SDL_Init(SDL_INIT_JOYSTICK);
joystick = SDL_JoystickOpen(0);
if (joystick != NULL) {
@@ -27,14 +30,10 @@ void joystickInit() {
gzwarn << "Joystick not found, begin waiting for joystick..." << std::endl;
warnShown = true;
}
+ return joystickInitialized;
}
bool joystickGet(int16_t ch[16]) {
- if (!joystickInitialized) {
- joystickInit();
- return false;
- }
-
SDL_JoystickUpdate();
for (uint8_t i = 0; i < sizeof(channels) / sizeof(channels[0]); i++) {