Truoc: publish lien tuc kha ca zero Twist theo timer -> de ghi de /cmd_vel cua publisher khac khi joystick khong dieu khien. CommandPublishGate: lenh khac-zero publish binh thuong theo chu ky; ve zero thi publish DUNG MOT LAN roi im, chi publish lai khi co lenh khac-zero moi. UI: dat className "VirtualJoystickTeleop" cho WM_CLASS (truoc dung chung "Tk" voi moi app Tk khac, bi gom lan trong taskbar/window picker) + ve icon joystick 32x32 khong can file ngoai.
181 lines
6.0 KiB
Python
Executable File
181 lines
6.0 KiB
Python
Executable File
#!/usr/bin/env python3
|
|
|
|
import unittest
|
|
|
|
from virtual_joystick_teleop import CommandPublishGate, TeleopModel
|
|
|
|
|
|
class CommandPublishGateTest(unittest.TestCase):
|
|
def test_idle_is_silent_after_exactly_one_zero_transition(self):
|
|
gate = CommandPublishGate()
|
|
|
|
self.assertFalse(gate.allow_command(0.0, 0.0))
|
|
self.assertTrue(gate.allow_command(0.3, 0.0))
|
|
self.assertTrue(gate.allow_command(0.3, 0.0))
|
|
self.assertTrue(gate.allow_command(0.0, 0.0))
|
|
self.assertFalse(gate.allow_command(0.0, 0.0))
|
|
self.assertFalse(gate.allow_command(0.0, 0.0))
|
|
|
|
def test_stop_is_once_unless_explicitly_forced(self):
|
|
gate = CommandPublishGate()
|
|
|
|
gate.allow_command(0.0, 0.7)
|
|
self.assertTrue(gate.allow_stop())
|
|
self.assertFalse(gate.allow_stop())
|
|
self.assertTrue(gate.allow_stop(force=True))
|
|
self.assertFalse(gate.allow_stop())
|
|
|
|
|
|
class TeleopModelTest(unittest.TestCase):
|
|
def test_arrow_keys_create_expected_twist_components(self):
|
|
model = TeleopModel(linear_speed=0.3, angular_speed=0.7)
|
|
|
|
model.set_key("Up", True)
|
|
model.set_key("Left", True)
|
|
|
|
self.assertEqual((0.3, 0.7), model.command())
|
|
self.assertEqual((-1, -1), model.joystick_position())
|
|
|
|
def test_opposite_keys_cancel_each_axis(self):
|
|
model = TeleopModel(linear_speed=0.3, angular_speed=0.7)
|
|
for key in TeleopModel.SUPPORTED_KEYS:
|
|
model.set_key(key, True)
|
|
|
|
self.assertEqual((0.0, 0.0), model.command())
|
|
|
|
def test_release_and_clear_stop_motion(self):
|
|
model = TeleopModel()
|
|
model.set_key("Down", True)
|
|
self.assertLess(model.command()[0], 0.0)
|
|
|
|
model.set_key("Down", False)
|
|
self.assertEqual((0.0, 0.0), model.command())
|
|
|
|
model.set_key("Right", True)
|
|
model.clear_motion()
|
|
self.assertEqual((0.0, 0.0), model.command())
|
|
|
|
def test_analog_joystick_scales_and_clamps_commands(self):
|
|
model = TeleopModel(linear_speed=0.4, angular_speed=0.8)
|
|
|
|
model.set_joystick(-0.5, -0.25)
|
|
|
|
self.assertEqual((0.1, 0.4), model.command())
|
|
self.assertEqual((-0.5, -0.25), model.joystick_position())
|
|
self.assertTrue(model.joystick_active)
|
|
|
|
model.set_joystick(5.0, -5.0)
|
|
self.assertEqual((0.4, -0.8), model.command())
|
|
|
|
model.release_joystick()
|
|
self.assertFalse(model.joystick_active)
|
|
self.assertEqual((0.0, 0.0), model.command())
|
|
|
|
def test_speed_buttons_clamp_to_configured_limits(self):
|
|
model = TeleopModel(
|
|
linear_speed=0.2,
|
|
angular_speed=0.4,
|
|
linear_step=0.1,
|
|
angular_step=0.2,
|
|
min_linear_speed=0.1,
|
|
max_linear_speed=0.3,
|
|
min_angular_speed=0.2,
|
|
max_angular_speed=0.6,
|
|
)
|
|
|
|
for _ in range(10):
|
|
model.adjust_linear_speed(1)
|
|
model.adjust_angular_speed(-1)
|
|
|
|
self.assertAlmostEqual(0.3, model.linear_speed)
|
|
self.assertAlmostEqual(0.2, model.angular_speed)
|
|
|
|
def test_speed_sliders_set_and_clamp_values(self):
|
|
model = TeleopModel(
|
|
linear_speed=0.2,
|
|
angular_speed=0.4,
|
|
max_linear_speed=2.0,
|
|
max_angular_speed=3.0,
|
|
)
|
|
|
|
self.assertAlmostEqual(1.6, model.set_linear_speed(1.6))
|
|
self.assertAlmostEqual(2.5, model.set_angular_speed(2.5))
|
|
self.assertAlmostEqual(2.0, model.set_linear_speed(10.0))
|
|
self.assertAlmostEqual(3.0, model.set_angular_speed(10.0))
|
|
|
|
def test_runtime_limit_change_clamps_current_speed(self):
|
|
model = TeleopModel(
|
|
linear_speed=1.5,
|
|
angular_speed=2.5,
|
|
max_linear_speed=3.0,
|
|
max_angular_speed=4.0,
|
|
)
|
|
|
|
self.assertAlmostEqual(1.0, model.set_max_linear_speed(1.0))
|
|
self.assertAlmostEqual(1.0, model.linear_speed)
|
|
self.assertAlmostEqual(2.0, model.set_max_angular_speed(2.0))
|
|
self.assertAlmostEqual(2.0, model.angular_speed)
|
|
|
|
self.assertAlmostEqual(
|
|
model.min_linear_speed,
|
|
model.set_max_linear_speed(-1.0),
|
|
)
|
|
|
|
def test_rqt_style_manual_command_is_signed_and_persistent(self):
|
|
model = TeleopModel(
|
|
max_linear_speed=2.0,
|
|
max_angular_speed=3.0,
|
|
min_linear_command=-1.0,
|
|
min_angular_command=-2.0,
|
|
)
|
|
|
|
model.set_manual_command(linear_x=-0.7)
|
|
model.set_manual_command(angular_z=1.4)
|
|
|
|
self.assertTrue(model.manual_control_active)
|
|
self.assertEqual((-0.7, 1.4), model.command())
|
|
self.assertEqual((-0.7, 1.4), model.command())
|
|
|
|
model.clear_motion()
|
|
self.assertFalse(model.manual_control_active)
|
|
self.assertEqual((0.0, 0.0), model.command())
|
|
|
|
def test_manual_command_and_keyboard_are_clamped_to_signed_limits(self):
|
|
model = TeleopModel(
|
|
linear_speed=2.0,
|
|
angular_speed=3.0,
|
|
max_linear_speed=2.0,
|
|
max_angular_speed=3.0,
|
|
min_linear_command=-0.8,
|
|
min_angular_command=-1.5,
|
|
)
|
|
|
|
model.set_manual_command(linear_x=-10.0, angular_z=10.0)
|
|
self.assertEqual((-0.8, 3.0), model.command())
|
|
|
|
model.set_key("Down", True)
|
|
model.set_key("Right", True)
|
|
self.assertEqual((-0.8, -1.5), model.command())
|
|
|
|
model.set_min_linear_command(-0.4)
|
|
model.set_min_angular_command(-0.5)
|
|
self.assertEqual((-0.4, -0.5), model.command())
|
|
|
|
def test_invalid_speed_configuration_is_rejected(self):
|
|
with self.assertRaises(ValueError):
|
|
TeleopModel(min_linear_speed=1.0, max_linear_speed=0.5)
|
|
with self.assertRaises(ValueError):
|
|
TeleopModel(angular_step=0.0)
|
|
with self.assertRaises(ValueError):
|
|
TeleopModel(min_linear_command=0.5)
|
|
|
|
def test_unsupported_key_does_not_change_state(self):
|
|
model = TeleopModel()
|
|
|
|
self.assertFalse(model.set_key("space", True))
|
|
self.assertEqual((0.0, 0.0), model.command())
|
|
|
|
|
|
if __name__ == "__main__":
|
|
unittest.main()
|