Autonomous Racing  1
f1tenth Project Group of Technical University Dortmund, Germany
keyboard_controller.h
Go to the documentation of this file.
1 #pragma once
2 
3 #include <SDL2/SDL.h>
4 #include <SDL2/SDL_keycode.h>
5 #include <algorithm>
6 #include <array>
7 #include <drive_msgs/drive_param.h>
8 #include <ros/package.h>
9 #include <ros/ros.h>
10 #include <signal.h>
11 #include <std_msgs/Int32.h>
12 #include <stdexcept>
13 
14 #include <dynamic_reconfigure/server.h>
15 #include <teleoperation/keyboard_controllerConfig.h>
16 
17 constexpr const char* TOPIC_DRIVE_PARAMETERS = "input/drive_param/keyboard";
18 constexpr const char* TOPIC_HEARTBEAT_MANUAL = "/input/heartbeat_manual";
19 constexpr const char* TOPIC_HEARTBEAT_AUTONOMOUS = "/input/heartbeat_autonomous";
20 constexpr const char* TOPIC_DRIVE_MODE = "/commands/drive_mode";
21 
22 enum class Keycode : int
23 {
24  SPACE = SDLK_SPACE,
25  A = SDLK_a,
26  B = SDLK_b,
27  D = SDLK_d,
28  S = SDLK_s,
29  W = SDLK_w,
30 };
31 
32 enum class KeyIndex : size_t
33 {
34  ACCELERATE = 0,
35  DECELERATE = 2,
36  STEER_LEFT = 1,
37  STEER_RIGHT = 3,
38  ENABLE_MANUAL = 4,
40 };
41 
42 enum class DriveMode : int
43 {
44  LOCKED = 0,
45  MANUAL = 1,
46  AUTONOMOUS = 2
47 };
48 
49 constexpr double PARAMETER_UPDATE_FREQUENCY = 90;
50 constexpr int SCREEN_EDGE_MARGIN = 20;
51 
53 {
54  public:
57  KeyboardController(const KeyboardController&) = default;
59 
60  private:
61  // How fast the steering value changes, in units per second
62  double m_steering_speed = 6;
63  // How fast the velocity changes, in units per second
64  double m_acceleration = 0.4;
65  // How fast the velocity changes when decelerating, in units per second
66  double m_braking = 2;
67 
68  // MAX_STEERING is multiplied by this when travelling at maximum velocity, by 1.0 when resting and by an
69  // interpolated value otherwise
70  double m_fast_steer_limit = 0.6;
71 
72  // When no steering key is pressed, the steering value will change towards 0 at this rate, in units per second
73  double m_steering_gravity = 2;
74  // When no throttle key is pressed, the velocity will change towards 0 at this rate, in units per second
75  double m_throttle_gravity = 3;
76 
77  double m_max_throttle = 0.35;
78 
79  static constexpr size_t KEY_COUNT = 6;
80  std::array<Keycode, KEY_COUNT> m_key_codes = { { Keycode::W, Keycode::A, Keycode::S, Keycode::D, Keycode::SPACE,
81  Keycode::B } };
82 
83  dynamic_reconfigure::Server<teleoperation::keyboard_controllerConfig> m_dyn_cfg_server;
84 
85  ros::NodeHandle m_node_handle;
86 
87  ros::Publisher m_drive_parameters_publisher;
88  ros::Publisher m_enable_manual_publisher;
89  ros::Publisher m_enable_autonomous_publisher;
90 
91  ros::Subscriber m_drive_mode_subscriber;
92 
93  SDL_Window* m_window;
94 
95  SDL_Surface* m_image_locked;
96  SDL_Surface* m_image_manual;
97  SDL_Surface* m_image_autonomous;
98 
99  std::array<bool, KEY_COUNT> m_key_pressed_state = { { false, false, false, false } };
100 
101  double m_velocity = 0;
102  double m_angle = 0;
103 
104  ros::Timer m_timer;
105 
106  DriveMode m_drive_mode = DriveMode::LOCKED;
107 
108  void pollWindowEvents();
109 
110  void updateDriveParameters(double delta_time);
111 
112  void publishDriveParameters();
113 
114  void updateDeadMansSwitch();
115 
116  void createWindow();
117  void timerCallback(const ros::TimerEvent& event);
118  void driveModeCallback(const std_msgs::Int32::ConstPtr& drive_mode_message);
119  void updateWindow();
120  void loadImages();
121 
125  void updateDynamicConfig();
126 };
constexpr const char * TOPIC_DRIVE_MODE
constexpr double PARAMETER_UPDATE_FREQUENCY
constexpr const char * TOPIC_HEARTBEAT_MANUAL
DriveMode
Definition: drive_mode.h:3
constexpr int SCREEN_EDGE_MARGIN
constexpr const char * TOPIC_HEARTBEAT_AUTONOMOUS
constexpr const char * TOPIC_DRIVE_PARAMETERS