Skip to content

Commit 077663c

Browse files
committed
more joystick modes
1 parent 29fb863 commit 077663c

2 files changed

Lines changed: 48 additions & 5 deletions

File tree

config/joystick.lua

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,12 @@ joystick_port="/dev/input/js0"
44
-- joystick_name="Logitech_F710"
55
joystick_name="Sony_DualShock_4"
66

7+
-- Joystick control mode configuration
8+
-- "both": left stick horizontal for steering, right stick vertical for drive (default)
9+
-- "left": left stick only (horizontal for steering, vertical for drive)
10+
-- "right": right stick only (horizontal for steering, vertical for drive)
11+
joystick_mode = "both"
12+
713

814

915

src/vesc_driver/vesc_driver.cpp

Lines changed: 42 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ CONFIG_FLOAT(max_accel_, "max_acceleration");
3535
CONFIG_FLOAT(max_decel_, "max_deceleration");
3636
CONFIG_FLOAT(turbo_speed_, "joystick_turbo_speed");
3737
CONFIG_FLOAT(normal_speed_, "joystick_normal_speed");
38+
CONFIG_STRING(joystick_mode_, "joystick_mode");
3839
CONFIG_STRING(serial_port_, "serial_port");
3940

4041
DEFINE_string(config_dir, "/home/orin/roboracer_ws/src/ut_automata/config",
@@ -71,7 +72,8 @@ VescDriver::VescDriver(rclcpp::Node::SharedPtr nh,
7172
// Load config.
7273
config_reader::ConfigReader reader({
7374
FLAGS_config_dir + "/car.lua",
74-
FLAGS_config_dir + "/vesc.lua"
75+
FLAGS_config_dir + "/vesc.lua",
76+
FLAGS_config_dir + "/joystick.lua"
7577
});
7678
}
7779
state_msg_.header.frame_id = "base_link";
@@ -227,16 +229,51 @@ void VescDriver::joystickCallback(const sensor_msgs::msg::Joy::SharedPtr msg) {
227229
mux_steering_angle_ = 0;
228230
}
229231
if (drive_mode_ == kJoystickDrive) {
230-
if (msg->axes.size() < 5) return;
231-
const float steer_joystick = -msg->axes[0];
232-
const float drive_joystick = -msg->axes[4];
232+
// Check minimum axes requirement based on mode
233+
size_t min_axes = 5; // Default for "both" mode (needs axes 0 and 4)
234+
if (joystick_mode_ == "left") {
235+
min_axes = 2; // Needs axes 0 and 1
236+
} else if (joystick_mode_ == "right") {
237+
min_axes = 5; // Needs axes 3 and 4
238+
}
239+
240+
if (msg->axes.size() < min_axes) {
241+
if (kDebug) printf("Insufficient axes for joystick mode '%s': need %zu, have %zu\n",
242+
joystick_mode_.c_str(), min_axes, msg->axes.size());
243+
return;
244+
}
245+
246+
float steer_joystick = 0.0;
247+
float drive_joystick = 0.0;
248+
249+
// Parse joystick mode configuration
250+
if (joystick_mode_ == "both") {
251+
// Default mode: left stick for steering, right stick for drive
252+
steer_joystick = -msg->axes[0]; // Left stick horizontal
253+
drive_joystick = -msg->axes[4]; // Right stick vertical
254+
} else if (joystick_mode_ == "left") {
255+
// Left stick only: horizontal for steering, vertical for drive
256+
steer_joystick = -msg->axes[0]; // Left stick horizontal
257+
drive_joystick = -msg->axes[1]; // Left stick vertical
258+
} else if (joystick_mode_ == "right") {
259+
// Right stick only: horizontal for steering, vertical for drive
260+
steer_joystick = -msg->axes[3]; // Right stick horizontal
261+
drive_joystick = -msg->axes[4]; // Right stick vertical
262+
} else {
263+
// Default to both mode if invalid configuration
264+
if (kDebug) printf("Invalid joystick_mode '%s', using default 'both'\n", joystick_mode_.c_str());
265+
steer_joystick = -msg->axes[0]; // Left stick horizontal
266+
drive_joystick = -msg->axes[4]; // Right stick vertical
267+
}
268+
233269
const bool turbo_mode = (msg->axes[2] >= 0.9);
234270
const float max_speed = (turbo_mode ? turbo_speed_ : normal_speed_);
235271
float speed = drive_joystick * max_speed;
236272
float steering_angle = steer_joystick * kMaxTurnRate;
237273
mux_drive_speed_ = speed;
238274
mux_steering_angle_ = steering_angle;
239-
if (kDebug) printf("%7.2f %.1f\u00b0\n", speed, math_util::RadToDeg(steering_angle));
275+
if (kDebug) printf("Mode: %s, Speed: %7.2f, Steering: %.1f\u00b0\n",
276+
joystick_mode_.c_str(), speed, math_util::RadToDeg(steering_angle));
240277
}
241278

242279
if (drive_mode_ == kAutonomousDrive ||

0 commit comments

Comments
 (0)