|
| 1 | +# This example compares the position control accuracy between 'control_dofs_position' and |
| 2 | +# 'control_dofs_position_velocity' when tracking a dynamic trajectory. |
| 3 | +# While both are equivalent in static, the former lacks the target velocity term of true PD controller in robotics, |
| 4 | +# making it underperform compared to 'control_dofs_position_velocity'. |
| 5 | +import argparse |
| 6 | +import math |
| 7 | + |
| 8 | +import matplotlib.pyplot as plt |
| 9 | + |
| 10 | +import genesis as gs |
| 11 | + |
| 12 | + |
| 13 | +def main(): |
| 14 | + parser = argparse.ArgumentParser() |
| 15 | + parser.add_argument("-v", "--vis", action="store_true", default=False) |
| 16 | + parser.add_argument("-c", "--cpu", action="store_true", default=False) |
| 17 | + args = parser.parse_args() |
| 18 | + |
| 19 | + ########################## init ########################## |
| 20 | + gs.init(backend=gs.cpu if args.cpu else gs.gpu) |
| 21 | + |
| 22 | + ########################## create a scene ########################## |
| 23 | + scene = gs.Scene( |
| 24 | + viewer_options=gs.options.ViewerOptions( |
| 25 | + camera_pos=(0, -3.5, 2.5), |
| 26 | + camera_lookat=(0.0, 0.0, 0.5), |
| 27 | + camera_fov=30, |
| 28 | + max_FPS=None, |
| 29 | + ), |
| 30 | + sim_options=gs.options.SimOptions( |
| 31 | + dt=0.005, |
| 32 | + ), |
| 33 | + show_viewer=False, |
| 34 | + show_FPS=True, |
| 35 | + ) |
| 36 | + |
| 37 | + ########################## entities ########################## |
| 38 | + plane = scene.add_entity( |
| 39 | + gs.morphs.Plane(), |
| 40 | + ) |
| 41 | + franka = scene.add_entity( |
| 42 | + gs.morphs.MJCF( |
| 43 | + file="xml/franka_emika_panda/panda.xml", |
| 44 | + ), |
| 45 | + ) |
| 46 | + ########################## build ########################## |
| 47 | + scene.build() |
| 48 | + |
| 49 | + joints_name = ( |
| 50 | + "joint1", |
| 51 | + "joint2", |
| 52 | + "joint3", |
| 53 | + "joint4", |
| 54 | + "joint5", |
| 55 | + "joint6", |
| 56 | + "joint7", |
| 57 | + "finger_joint1", |
| 58 | + "finger_joint2", |
| 59 | + ) |
| 60 | + motors_dof_idx = [franka.get_joint(name).dofs_idx_local[0] for name in joints_name] |
| 61 | + |
| 62 | + ############ Optional: set control gains ############ |
| 63 | + # set positional gains |
| 64 | + franka.set_dofs_kp( |
| 65 | + kp=[4500, 4500, 3500, 3500, 2000, 2000, 2000, 100, 100], |
| 66 | + dofs_idx_local=motors_dof_idx, |
| 67 | + ) |
| 68 | + # set velocity gains |
| 69 | + franka.set_dofs_kv( |
| 70 | + kv=[450, 450, 350, 350, 200, 200, 200, 10, 10], |
| 71 | + dofs_idx_local=motors_dof_idx, |
| 72 | + ) |
| 73 | + # set force range for safety |
| 74 | + franka.set_dofs_force_range( |
| 75 | + lower=[-87, -87, -87, -87, -12, -12, -12, -100, -100], |
| 76 | + upper=[87, 87, 87, 87, 12, 12, 12, 100, 100], |
| 77 | + dofs_idx_local=motors_dof_idx, |
| 78 | + ) |
| 79 | + # Hard reset |
| 80 | + # Follow a sinusoid trajectory |
| 81 | + A = 0.5 # motion amplitude, rad |
| 82 | + f = 1.0 # motion frequency, Hz |
| 83 | + |
| 84 | + # Use control_dofs_position |
| 85 | + pos_simulation_result = [] |
| 86 | + franka.set_dofs_position([A, 0, 0, 0, 0, 0, 0, 0, 0], motors_dof_idx) |
| 87 | + t0 = scene.t |
| 88 | + while (t := (scene.t - t0) * scene.dt) < 2.0: |
| 89 | + target_position = A * (1 + math.sin(2 * math.pi * f * t)) |
| 90 | + |
| 91 | + current_position = float(franka.get_qpos()[0]) |
| 92 | + pos_simulation_result.append([t, current_position, target_position]) |
| 93 | + franka.control_dofs_position([target_position, 0, 0, 0, 0, 0, 0, 0, 0], motors_dof_idx) |
| 94 | + scene.step() |
| 95 | + |
| 96 | + # Use control_dofs_position_velocity |
| 97 | + pos_vel_simulation_result = [] |
| 98 | + franka.set_dofs_position([A, 0, 0, 0, 0, 0, 0, 0, 0], motors_dof_idx) |
| 99 | + t0 = scene.t |
| 100 | + while (t := (scene.t - t0) * scene.dt) < 2.0: |
| 101 | + target_position = A * (1 + math.sin(2 * math.pi * f * t)) |
| 102 | + target_velocity = 2 * math.pi * f * A * math.cos(2 * math.pi * f * t) |
| 103 | + |
| 104 | + current_position = float(franka.get_qpos()[0]) |
| 105 | + pos_vel_simulation_result.append([t, current_position, target_position]) |
| 106 | + franka.control_dofs_position_velocity( |
| 107 | + [target_position, 0, 0, 0, 0, 0, 0, 0, 0], |
| 108 | + [target_velocity, 0, 0, 0, 0, 0, 0, 0, 0], |
| 109 | + motors_dof_idx, |
| 110 | + ) |
| 111 | + scene.step() |
| 112 | + |
| 113 | + # Plot results |
| 114 | + pos_simulation_result = tuple(zip(*pos_simulation_result)) |
| 115 | + pos_vel_simulation_result = tuple(zip(*pos_vel_simulation_result)) |
| 116 | + |
| 117 | + plt.plot(pos_simulation_result[0], pos_simulation_result[1], label="control_dofs_position") |
| 118 | + plt.plot(pos_vel_simulation_result[0], pos_vel_simulation_result[1], label="control_dofs_position_velocity") |
| 119 | + plt.plot(pos_vel_simulation_result[0], pos_vel_simulation_result[2], color="black", label="Target position") |
| 120 | + plt.xlabel("Time (s)") |
| 121 | + plt.ylabel("Joint position (rad)") |
| 122 | + plt.title("Comparison of joint position tracking with two different controllers") |
| 123 | + plt.grid() |
| 124 | + plt.legend() |
| 125 | + plt.show() |
| 126 | + |
| 127 | + |
| 128 | +if __name__ == "__main__": |
| 129 | + main() |
0 commit comments