Python example: mrpt_kinematics_example.py

Velocity commands and kinematic simulators of mobile robots with mrpt.kinematics.

Modules: mrpt.kinematics, mrpt.math

 1#!/usr/bin/env python3
 2"""
 3Velocity commands and kinematic simulators of mobile robots with mrpt.kinematics.
 4
 5Demonstrates:
 6  - CVehicleVelCmd_DiffDriven: diff-drive velocity command
 7  - CVehicleVelCmd_Holo: holonomic velocity command
 8  - CVehicleSimul_DiffDriven: simulate a diff-drive robot over time
 9  - CVehicleSimul_Holo: simulate a holonomic robot
10"""
11
12import math
13from mrpt.kinematics import (
14    CVehicleVelCmd_DiffDriven,
15    CVehicleVelCmd_Holo,
16    CVehicleSimul_DiffDriven,
17    CVehicleSimul_Holo,
18)
19from mrpt.math import TPose2D
20
21# ---------------------------------------------------------------------------
22# Velocity commands
23# ---------------------------------------------------------------------------
24cmd_diff = CVehicleVelCmd_DiffDriven()
25cmd_diff.lin_vel = 1.0   # 1 m/s forward
26cmd_diff.ang_vel = 0.2   # slight left turn
27print(f"DiffDrive cmd: {cmd_diff}")
28assert not cmd_diff.isStopCmd()
29
30cmd_diff.setToStop()
31assert cmd_diff.isStopCmd()
32print("  setToStop ✓")
33
34cmd_holo = CVehicleVelCmd_Holo(1.0, 0.0, 0.5, 0.1)  # vel, dir, ramp_time, rot_speed
35print(f"\nHolo cmd: {cmd_holo}")
36assert not cmd_holo.isStopCmd()
37
38# ---------------------------------------------------------------------------
39# Diff-drive simulator
40# ---------------------------------------------------------------------------
41sim_diff = CVehicleSimul_DiffDriven()
42sim_diff.movementCommand(1.0, 0.0)   # straight ahead at 1 m/s
43
44dt = 0.1
45for _ in range(20):                  # 2 seconds
46    sim_diff.simulateOneTimeStep(dt)
47
48pose = sim_diff.getCurrentGTPose()
49print(f"\nDiffDrive after 2 s at 1 m/s straight:")
50print(f"  GT pose: x={pose.x:.2f} y={pose.y:.2f} phi={math.degrees(pose.phi):.1f} deg")
51assert abs(pose.x - 2.0) < 0.05, f"Expected x≈2.0, got {pose.x}"
52print("  position check ✓")
53
54# ---------------------------------------------------------------------------
55# Holonomic simulator
56# ---------------------------------------------------------------------------
57sim_holo = CVehicleSimul_Holo()
58sim_holo.sendVelRampCmd(1.0, 0.0, 0.5, 0.0)   # vel=1, dir=0 (fwd), ramp=0.5s, no rotation
59
60for _ in range(20):
61    sim_holo.simulateOneTimeStep(dt)
62
63pose_h = sim_holo.getCurrentGTPose()
64print(f"\nHolo after 2 s:")
65print(f"  GT pose: x={pose_h.x:.2f} y={pose_h.y:.2f} phi={math.degrees(pose_h.phi):.1f} deg")
66print(f"  {sim_holo}")