Skip to content

Commit

Permalink
Updated hard stop stuff.
Browse files Browse the repository at this point in the history
  • Loading branch information
= committed Jan 27, 2025
1 parent fc25ac8 commit cb7dd83
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 28 deletions.
15 changes: 11 additions & 4 deletions opensourceleg/control/virtual_hard_stop.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,11 @@ def __init__(self, joint: Joint, direction:Direction, start_position_rad:float,
self.start_position = start_position_rad
self.stiffness = stiffness
self.damping = damping
self._is_active = False

@property
def is_active(self):
return self._is_active

def calculate_hard_stop_torque(self):
"""
Expand All @@ -37,21 +42,23 @@ def calculate_hard_stop_torque(self):
float: The calculated torque to slow the joint
"""
current_pos = self.joint.output_position
delta_theta = current_pos - self.start_position
delta_theta = (10*(current_pos - self.start_position))**3
current_vel = self.joint.output_velocity

# Check the direction of the hard stop and calculate torque accordingly
if self.direction == Direction.POSITIVE:
if current_pos > self.start_position:
torque = -self.stiffness * delta_theta - self.damping * current_vel
else:
return 0
torque = 0.0
else:
if current_pos < self.start_position:
torque = -self.stiffness * delta_theta - self.damping * current_vel
else:
return 0

torque = 0.0

self._is_active = torque!=0.0

return torque

def calculate_eq_angle_bias(self, joint_K):
Expand Down
51 changes: 27 additions & 24 deletions test_hard_stop.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from opensourceleg.opensourceleg.osl import OpenSourceLeg
from opensourceleg.opensourceleg.control.virtual_hard_stop import VirtualHardStop, Direction
from opensourceleg.opensourceleg.tools.units import convert_to_default
from opensourceleg.osl import OpenSourceLeg
from opensourceleg.control.virtual_hard_stop import VirtualHardStop, Direction
from opensourceleg.tools.units import convert_to_default
import numpy as np

LOOP_FREQUENCY = 200 # Hz
osl = OpenSourceLeg(frequency=LOOP_FREQUENCY, file_name='./' + 'hardStopTest')
Expand All @@ -9,35 +10,37 @@
osl.add_joint("knee", gear_ratio=9 * 83 / 18, offline_mode=False)
osl.add_joint("ankle", gear_ratio=9 * 83 / 18, offline_mode=False)

ankle_hard_stop_pos = VirtualHardStop(osl.ankle, Direction.POSITIVE, 5, 10, 5)
ankle_hard_stop_neg = VirtualHardStop(osl.ankle, Direction.NEGATIVE, -5, 10, 5)
ankle_hard_stop_pos = VirtualHardStop(osl.ankle, Direction.POSITIVE, np.deg2rad(20), 100, 9)
ankle_hard_stop_neg = VirtualHardStop(osl.ankle, Direction.NEGATIVE, np.deg2rad(-20), 100, 9)

with osl:
osl.home()
osl.update()

# Testing Impedance Mode
# ankle_stiffness = 10
# osl.knee.set_mode(osl.knee.control_modes.impedance)
# osl.knee.set_joint_impedance(K = 10, B = 5)
# osl.knee.set_output_position(osl.knee.output_position)
# osl.ankle.set_mode(osl.ankle.control_modes.impedance)
# osl.ankle.set_joint_impedance(K = ankle_stiffness, B = 5)
# osl.ankle.set_output_position(osl.ankle.output_position)
ankle_stiffness = 10
osl.knee.set_mode(osl.knee.control_modes.impedance)
osl.knee.set_joint_impedance(K = 10, B = 5)
osl.knee.set_output_position(osl.knee.output_position)
osl.ankle.set_mode(osl.ankle.control_modes.impedance)
osl.ankle.set_joint_impedance(K = ankle_stiffness, B = 1)
osl.ankle.set_output_position(osl.ankle.output_position)


# for time in osl.clock:
# osl.update()
# hard_stop_eq_angle_bias = ankle_hard_stop_pos.calculate_eq_angle_bias(ankle_stiffness) + ankle_hard_stop_neg.calculate_eq_angle_bias(ankle_stiffness)
# osl.ankle.set_output_position(hard_stop_eq_angle_bias)
for time in osl.clock:
osl.update()
hard_stop_eq_angle_bias = ankle_hard_stop_pos.calculate_eq_angle_bias(ankle_stiffness) + ankle_hard_stop_neg.calculate_eq_angle_bias(ankle_stiffness)
osl.ankle.set_output_position(hard_stop_eq_angle_bias)
print(hard_stop_eq_angle_bias, ankle_hard_stop_neg.is_active, ankle_hard_stop_pos.is_active)

# Testing Torque Mode
osl.knee.set_mode(osl.knee.control_modes.torque)
osl.knee.set_output_torque(0)
osl.ankle.set_mode(osl.ankle.control_modes.torque)
osl.ankle.set_output_torque(0)
# osl.knee.set_mode(osl.knee.control_modes.current)
# osl.knee.set_output_torque(0)
# osl.ankle.set_mode(osl.ankle.control_modes.current)
# osl.ankle.set_output_torque(0)

for time in osl.clock:
osl.update()
hard_stop_torque = ankle_hard_stop_pos.calculate_hard_stop_torque() + ankle_hard_stop_neg.calculate_hard_stop_torque()
osl.ankle.set_output_torque(hard_stop_torque)
# for time in osl.clock:
# osl.update()
# hard_stop_torque = ankle_hard_stop_pos.calculate_hard_stop_torque() + ankle_hard_stop_neg.calculate_hard_stop_torque()
# print(hard_stop_torque)
# osl.ankle.set_output_torque(hard_stop_torque)

0 comments on commit cb7dd83

Please sign in to comment.