I have the following python code to perform a straight line movement of a robot end-effector in operational space. How could I set the end-effector velocity?
"""
This file demonstrates the use of the Cartesian planner to generate a trajectory
"""
import sys
import os
if os.path.join(os.path.dirname(os.path.abspath(file)), "..", "src") not in sys.path:
sys.path.append(os.path.join(os.path.dirname(os.path.abspath(file)), "..", "src"))
import numpy as np
import time
pin
import pinocchio as pin
from pinocchio.visualize import MeshcatVisualizer
from pinocchio.robot_wrapper import RobotWrapper
POLYTHESIS
from polythesis.core.utils import generate_mod_file
from polythesis.robot.robot_loader import loadABB_IRB7600325, loadABB_IRB7600325v2, loadABB_IRB4600255, loadABB_IRB4600255v2
from polythesis.ik.differential_ik import DifferentialIk, DifferentialIkOptions
from polythesis.ik.mydifferential_ik import DLS_Options, DLS_Ik
from polythesis.planning.cartesian_planner import (CartesianPlanner,CartesianPlannerOptions)
from polythesis.planning.my_cartesian_planner import (CartesianPlanner,CartesianPlannerOptions)
from polythesis.visualization.meshcat_utils import visualize_frames
from polythesis.plot.plot_results import PlotResults
import polythesis.EGM.Core as egm
import matplotlib.pyplot as plt
Add access if it is not in the system path.
CONSTANTS
VERBOSE=True # Print out debug information
VISUAL=True # Visualize the robot in MeshCat
PLOT=True # Plot results
Constants for the simulation
dt=0.04 # [s] Time step for the simulation
SIMU_REAL_TIME=True # Set to True if you want to run the simulation in real time
ndt=10 # [s] Number of integration steps for each control loop
Constants for the REAL robot controller
CONTROL_REAL_ROBOT=False # Set to True if you want to control the real robotABB
IP_CONTROLLER='172.18.206.202' # IP address of the ABB controller
PORT_CONTROLLER=6511 # Port of the ABB controller
Constants for the robot max values
MAX_LINEAR_VELOCITY=0.4 # [m/s] Maximum TCP linear velocity
MAX_LINEAR_ACCELERATION=0.4 # [m/s^2] Maximum TCP linear acceleration
MAX_ANGULAR_VELOCITY=0.5 # [rad/s] Maximum TCP angular velocity
MAX_ANGULAR_ACCELERATION=0.4 # [rad/s^2] Maximum TCP angular acceleration
Other constants
LINE_WIDTH = 80
Load your robot's URDF model
robot = loadABB_IRB7600325v2() # Available models: loadABB_IRB4600255,loadABB_IRB4600255v2, loadABB_IRB7600325,loadABB_IRB7600325v2
model = robot.model
data = robot.data
for i in range(num_steps):
# Compute time and interpolation factor
t = i * dt
alpha = t / T # Interpolation ratio from 0 to 1
# Interpolate to get desired TCP pose at current time
x_desired = interpolate_pose(start_TCP_pose, end_TCP_pose, alpha)
# Compute current TCP pose using forward kinematics
pin.forwardKinematics(model, data, q)
pin.updateFramePlacement(model, data, frame_id)
oMf = data.oMf[frame_id] # Current transform of the end-effector frame
x_current = pin.SE3(oMf.rotation, oMf.translation)
x_list.append(x_current)
# Compute error between current and desired poses
e, e_translation, e_rotation = compute_pose_error(x_current, x_desired)
# Record the norms of translation and rotation errors
e_translation_norm_list.append(np.linalg.norm(e_translation))
e_rotation_norm_list.append(np.linalg.norm(e_rotation))
# Compute the Jacobian of the end-effector
J = pin.computeFrameJacobian(model, data, q, frame_id, pin.ReferenceFrame.LOCAL_WORLD_ALIGNED)
nn = J.shape[0] # Number of rows
nm = J.shape[1] # Number of columns
# Compute SVD of the Jacobian
U, S, Vh = np.linalg.svd(J, full_matrices=False)
# Determine the damping factor lambda based on singular values
singular_threshold = 0.05
# Minimum singular value
S_min = S[-1]
S_min_list.append(S_min) # Record S_min
# If near singularity, increase damping
if S_min < singular_threshold:
lambda_damping = (1 - (S_min / singular_threshold)) * 0.01
else:
lambda_damping = 0.0 # No damping needed
# Small regularization to avoid division by zero
#lambda_damping = max(lambda_damping, 1e-6)
# Record lambda_damping
lambda_damping_list.append(lambda_damping)
# Normalizing matrices
Nx = np.identity(nm)
Nq = np.identity(nn)
# Weight matrices
W1 = Nx.T @ Nx
W2 = lambda_damping**2 * (Nq.T @ Nq)
JJT = J @(W1@ J.T) + W2
delta_q = J.T@ (W1 @ np.linalg.solve(JJT, e))
# Optional: Limit the maximum joint update step to avoid too large movements
max_delta_q = 0.05 # Maximum joint update (radians)
norm_delta_q = np.linalg.norm(delta_q)
if norm_delta_q > max_delta_q:
delta_q *= max_delta_q / norm_delta_q
# Update joint positions using integration
q_new = pin.integrate(model, q, delta_q)
# Apply joint limits
q_new = np.minimum(np.maximum(q_new, lower_limits), upper_limits)
# Check if the joint update was feasible
if not np.allclose(q_new, q + delta_q, atol=1e-6):
# Joint limits have modified the joint update, which may affect the end-effector pose
# Consider recomputing delta_q with joint limits projection (advanced topic)
pass # For this simple implementation, we proceed with the limited q_new
# Update joint positions
q = q_new
# Record the joint positions and time
q_list.append(q.copy())
time_list.append(t + dt)
# (Optional) Print progress
if i % 100 == 0:
print(f"Step {i}/{num_steps}, Time {t:.2f}s, Damping {lambda_damping:.4f}")
The q_list now contains the joint configurations over time, considering joint limits
plt.figure(figsize=(10, 6))
plt.plot(time_list[1:], [x.translation[0] for x in x_list], label="X")
plt.plot(time_list[1:], [x.translation[1] for x in x_list], label="Y")
plt.plot(time_list[1:], [x.translation[2] for x in x_list], label="Z")
plt.xlabel("Time [s]")
plt.ylabel("Position [m]")
plt.title("End-Effector Position Over Time")
plt.legend()
plt.grid(True)
plt.show()
Plot joint positions over time
plt.figure(figsize=(10, 6))
num_joints = q_array.shape[1]
for joint_index in range(num_joints):
plt.plot(time_list, q_array[:, joint_index], label=f"Joint {joint_index + 1}")
plt.xlabel("Time [s]")
plt.ylabel("Joint Angles [rad]")
plt.title("Joint Angles Over Time")
plt.legend()
plt.grid(True)
plt.show()
Plot joint velocities over time
plt.figure(figsize=(10, 6))
for joint_index in range(num_joints):
plt.plot(time_list_qdot[1:], q_dot[1:, joint_index], label=f"Joint {joint_index + 1}")
plt.xlabel("Time [s]")
plt.ylabel("Joint Velocities [rad/s]")
plt.title("Joint Velocities Over Time")
plt.legend()
plt.grid(True)
plt.show()
Plot joint accelerations over time
plt.figure(figsize=(10, 6))
for joint_index in range(num_joints):
plt.plot(time_list_qddot[1:], q_ddot[1:, joint_index], label=f"Joint {joint_index + 1}")
plt.xlabel("Time [s]")
plt.ylabel("Joint Accelerations [rad/s²]")
plt.title("Joint Accelerations Over Time")
plt.legend()
plt.grid(True)
plt.show()
plt.figure(figsize=(10, 6))
plt.plot(time_list[:-1], S_min_array, label="Smallest Singular Value")
plt.xlabel("Time [s]")
plt.ylabel("Singular Value")
plt.title("Smallest Singular Value Over Time")
plt.legend()
plt.grid(True)
plt.show()
Optional: Visualize the robot's motion
for idx in range(q_array.shape[0]):
robot.viz.display(q_array[idx, :])
time.sleep(dt)
input("Press 'q' to exit the program...")
To set the end-effector velocity in your code, you'll need to adjust the time parameterization of your trajectory so that the end-effector moves at the desired speed. Currently, your code interpolates between the start and end poses over a fixed total time T = 7.0 seconds, which implicitly sets the end-effector velocity based on the distance traveled and the total time.
Here's how you can modify your code to explicitly set the end-effector velocity:
Calculate the Total Linear and Angular Distances:
Compute the linear distance between the start and end positions and the angular distance between the start and end orientations.
Set Desired Linear and Angular Velocities:
Define your desired end-effector linear (v_desired) and angular (omega_desired) velocities.
Compute the Required Time for Each Motion:
Calculate the time required to traverse the linear and angular distances at the desired velocities.
Set the Total Motion Time:
Use the maximum of the linear and angular times to ensure neither velocity limit is exceeded.
Adjust the Trajectory Parameterization:
Modify the interpolation factor alpha using the new total time so that the end-effector moves at the desired velocities.
Below is the modified code with these adjustments:
# ... [Your existing imports and setup code] ...
# Time parameters (adjusted to set the end-effector velocity)
dt = 0.01 # Time step (seconds)
# Desired velocities
v_desired = 0.2 # [m/s] Desired linear velocity
omega_desired = 0.1 # [rad/s] Desired angular velocity
# Compute the linear distance to move
L_trans = np.linalg.norm(start_TCP_pose.translation - end_TCP_pose.translation)
# Compute the angular distance to rotate
delta_R = end_TCP_pose.rotation @ start_TCP_pose.rotation.T
delta_angle = np.linalg.norm(pin.log3(delta_R))
# Compute required times
T_trans = L_trans / v_desired if v_desired > 0 else 0
T_rot = delta_angle / omega_desired if omega_desired > 0 else 0
# Set the total motion time as the maximum of the two
T = max(T_trans, T_rot)
# Calculate the number of time steps
num_steps = int(T / dt)
print(f"Total motion time T = {T:.2f} seconds")
print(f"Number of steps = {num_steps}")
print(f"Linear distance to move = {L_trans:.3f} meters")
print(f"Angular distance to rotate = {delta_angle:.3f} radians")
# ... [Rest of your code] ...
# Main control loop
for i in range(num_steps):
# Compute time and interpolation factor
t = i * dt
alpha = t / T # Interpolation ratio from 0 to 1
# Ensure alpha does not exceed 1.0 due to numerical errors
alpha = min(alpha, 1.0)
# Interpolate to get desired TCP pose at current time
x_desired = interpolate_pose(start_TCP_pose, end_TCP_pose, alpha)
# ... [Rest of your control loop code] ...
# (Optional) Print progress
if i % 100 == 0:
print(f"Step {i}/{num_steps}, Time {t:.2f}s, Damping {lambda_damping:.4f}")
# ... [Rest of your code] ...
T_trans is the time required to move L_trans meters at v_desired m/s.
T_rot is the time required to rotate delta_angle radians at omega_desired rad/s.
Set Total Motion Time:
T = max(T_trans, T_rot)
The total time T ensures that neither the linear nor the angular velocity exceeds the desired velocities.
Adjust the Number of Steps:
num_steps = int(T / dt)
Recalculate the number of steps based on the new total motion time T.
Adjust Interpolation Factor:
alpha = t / T
alpha = min(alpha, 1.0)
The interpolation factor alpha is adjusted to ensure it progresses from 0 to 1 over the duration T.
We use min(alpha, 1.0) to prevent alpha from exceeding 1.0 due to any numerical errors at the final time step.
Notes:
Orientation Interpolation:
The orientation interpolation using spherical linear interpolation (SLERP) ensures that the rotational motion is smooth and follows the shortest path between the two orientations.
Velocity Limits:
By adjusting the total time T based on the desired velocities, you ensure that both linear and angular velocities stay within your specified limits.
Error Handling:
Make sure to handle cases where v_desired or omega_desired is zero to avoid division by zero errors.
Verification:
After making these modifications, you can verify that the end-effector velocities are as desired:
Compute the Actual Velocities:
For linear velocity:
actual_v = L_trans / T
print(f"Actual linear velocity: {actual_v:.3f} m/s")
For angular velocity:
actual_omega = delta_angle / T
print(f"Actual angular velocity: {actual_omega:.3f} rad/s")
Ensure Velocities Are Within Limits:
The computed actual_v and actual_omega should be less than or equal to v_desired and omega_desired, respectively.
Visualization:
When you run the simulation and visualize the robot's motion, you should see the end-effector moving at the specified velocities.
Further Enhancements:
If you need a more sophisticated velocity profile (e.g., trapezoidal or sinusoidal), you can modify the interpolation factor alpha to follow the desired profile:
Implement a Velocity Profile Function:
Define a function alpha = s(t) that represents the cumulative distance traveled along the path as a function of time, considering acceleration and deceleration phases.
Modify the Interpolation:
Use the new alpha in your interpolate_pose function to adjust both position and orientation over time according to the velocity profile.
Example of a Trapezoidal Velocity Profile:
Here's a simplified example of how you might implement a trapezoidal velocity profile for alpha:
def trapezoidal_velocity_profile(t, T_accel, T_flat, T_total):
if t < T_accel:
# Acceleration phase
alpha = 0.5 * (t / T_accel) ** 2
elif t < (T_accel + T_flat):
# Constant velocity phase
alpha = (t - T_accel / 2)
elif t <= T_total:
# Deceleration phase
t_dec = t - T_accel - T_flat
alpha = (T_accel + T_flat) - 0.5 * (t_dec / T_accel) ** 2
else:
alpha = 1.0 # Ensure alpha does not exceed 1.0
# Normalize alpha by the total motion time
alpha /= (T_accel + T_flat)
return alpha
# Parameters for the trapezoidal profile
T_total = T
T_accel = T_total * 0.2 # 20% of the motion time for acceleration
T_flat = T_total - 2 * T_accel # Constant velocity phase
# In your main loop
for i in range(num_steps):
t = i * dt
alpha = trapezoidal_velocity_profile(t, T_accel, T_flat, T_total)
alpha = min(alpha, 1.0)
x_desired = interpolate_pose(start_TCP_pose, end_TCP_pose, alpha)
# ... [Rest of your control loop code] ...
Conclusion:
By adjusting the total motion time and interpolation factor based on your desired end-effector velocities, you can control the speed of the end-effector along the path. This approach ensures that the robot moves smoothly and adheres to the specified velocity constraints.
Feel free to incorporate these changes into your code to set the end-effector velocity as needed. If you have any questions or need further assistance, don't hesitate to ask!