Go2Py_SIM/Go2Py/robot/safety.py

33 lines
1.2 KiB
Python
Raw Normal View History

import time
2024-11-05 06:27:32 +08:00
import numpy as np
2024-05-21 06:45:59 +08:00
class SafetyHypervisor():
2024-11-05 06:27:32 +08:00
def __init__(self, robot, max_temperature=70, body_stability_idx=0.7):
self.robot = robot
2024-11-05 06:27:32 +08:00
self.max_temperature = max_temperature
self.normalize = lambda v: v/np.linalg.norm(v)
self.compute_body_stability_idx = lambda v: (self.normalize(v).T@np.array([0,0,-1]))
self.body_stability_idx = body_stability_idx
def unsafe(self):
2024-11-05 06:27:32 +08:00
if self.robot.simulated:
return False
else:
state = self.robot.getJointStates()
temperature = state['temperature']
if np.any(np.array(temperature)>self.max_temperature):
print(f'One of the actuators reached max temperature of {self.max_temperature}')
return True
if self.compute_body_stability_idx(self.robot.getGravityInBody()) < self.body_stability_idx:
print('Body is tilted too much! deactivating the controller')
return True
return False
2024-05-21 06:45:59 +08:00
def controlTimeout(self):
if time.time() - self.robot.latest_command_stamp > 0.1:
print('controller timeout')
return True
else:
2024-05-21 06:45:59 +08:00
return False