120 @brief Get a single key from the user.
122 tty.setraw(sys.stdin.fileno())
123 rlist, _, _ = select([sys.stdin], [], [], timeout)
124 key = sys.stdin.read(1)
if rlist
else ""
125 termios.tcsetattr(sys.stdin, termios.TCSADRAIN, settings)
131 @brief Print current status without clearing the screen
134 sys.stdout.write(
"\r")
135 sys.stdout.write(
" " * 80)
136 sys.stdout.write(
"\r")
139 accel_bar =
create_bar(acceleration, -1.0, 1.0, 20)
140 steer_bar =
create_bar(steering, -0.34, 0.34, 20)
142 status = f
"Throttle: {acceleration:+.2f} {accel_bar} | Steering: {steering:+.2f} {steer_bar}"
143 sys.stdout.write(status)
149 @brief Create a visual bar representation of a value
152 normalized = (value - min_val) / (max_val - min_val)
153 normalized = max(0, min(1, normalized))
156 filled = int(normalized * length)
157 bar =
"█" * filled +
"░" * (length - filled)
164 @brief Main function to control the pacsim robot using the keyboard
166 @details This function reads the keyboard inputs and updates
167 the control commands accordingly, for the other thread to publish them.
171 settings = termios.tcgetattr(sys.stdin)
175 acceleration: float = 0.0
176 steering: float = 0.0
184 pub_thread.update(acceleration, steering)
186 key: str = str(
get_key(settings, key_timeout))
188 command_changed =
False
192 acceleration = min(acceleration, 1.0)
193 command_changed =
True
196 acceleration = max(acceleration, -1.0)
197 command_changed =
True
200 steering = min(steering, 0.34)
201 command_changed =
True
204 steering = max(steering, -0.34)
205 command_changed =
True
212 pub_thread.update(acceleration, steering)
215 except Exception
as e:
216 print(f
"\nError: {e}")
219 print(
"\nShutting down...")
221 termios.tcsetattr(sys.stdin, termios.TCSADRAIN, settings)
Thread to publish control commands to the robot.
run(self)
Run the thread to publish control commands.
update(self, float acceleration, float steering_angle)
Update the control commands to be published.
__init__(self, rate)
Constructor for the PublishThread class.
create_bar(value, min_val, max_val, length)
Create a visual bar representation of a value.
get_key(settings, timeout)
Get a single key from the user.
main()
Main function to control the pacsim robot using the keyboard.
print_status(acceleration, steering)
Print current status without clearing the screen.