forked from limxdynamics/rl-deploy-with-python
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpointfoot_controller.py
366 lines (304 loc) · 16.6 KB
/
pointfoot_controller.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
import os
import sys
import copy
import numpy as np
import yaml
import onnxruntime as ort
from scipy.spatial.transform import Rotation as R
from functools import partial
import limxsdk
import limxsdk.robot.Rate as Rate
import limxsdk.robot.Robot as Robot
import limxsdk.robot.RobotType as RobotType
import limxsdk.datatypes as datatypes
class PointfootController:
def __init__(self, model_dir, robot, robot_type):
# Initialize robot and type information
self.robot = robot
self.robot_type = robot_type
# Load configuration and model file paths based on robot type
self.config_file = f'{model_dir}/{self.robot_type}/params.yaml'
self.model_file = f'{model_dir}/{self.robot_type}/policy/policy.onnx'
# Load configuration settings from the YAML file
self.load_config(self.config_file)
# Load the ONNX model and set up input and output names
self.policy_session = ort.InferenceSession(self.model_file)
self.policy_input_names = [self.policy_session.get_inputs()[0].name]
self.policy_output_names = [self.policy_session.get_outputs()[0].name]
# Prepare robot command structure with default values for mode, q, dq, tau, Kp, Kd
self.robot_cmd = datatypes.RobotCmd()
self.robot_cmd.mode = [0. for x in range(0, self.joint_num)]
self.robot_cmd.q = [0. for x in range(0, self.joint_num)]
self.robot_cmd.dq = [0. for x in range(0, self.joint_num)]
self.robot_cmd.tau = [0. for x in range(0, self.joint_num)]
self.robot_cmd.Kp = [self.control_cfg['stiffness'] for x in range(0, self.joint_num)]
self.robot_cmd.Kd = [self.control_cfg['damping'] for x in range(0, self.joint_num)]
# Prepare robot state structure
self.robot_state = datatypes.RobotState()
self.robot_state.tau = [0. for x in range(0, self.joint_num)]
self.robot_state.q = [0. for x in range(0, self.joint_num)]
self.robot_state.dq = [0. for x in range(0, self.joint_num)]
self.robot_state_tmp = copy.deepcopy(self.robot_state)
# Initialize IMU (Inertial Measurement Unit) data structure
self.imu_data = datatypes.ImuData()
self.imu_data.quat[0] = 0
self.imu_data.quat[1] = 0
self.imu_data.quat[2] = 0
self.imu_data.quat[3] = 1
self.imu_data_tmp = copy.deepcopy(self.imu_data)
# Set up a callback to receive updated robot state data
self.robot_state_callback_partial = partial(self.robot_state_callback)
self.robot.subscribeRobotState(self.robot_state_callback_partial)
# Set up a callback to receive updated IMU data
self.imu_data_callback_partial = partial(self.imu_data_callback)
self.robot.subscribeImuData(self.imu_data_callback_partial)
# Set up a callback to receive updated SensorJoy
self.sensor_joy_callback_partial = partial(self.sensor_joy_callback)
self.robot.subscribeSensorJoy(self.sensor_joy_callback_partial)
# Load the configuration from a YAML file
def load_config(self, config_file):
with open(config_file, 'r') as f:
config = yaml.safe_load(f)
# Assign configuration parameters to controller variables
self.joint_names = config['PointfootCfg']['joint_names']
self.init_state = config['PointfootCfg']['init_state']['default_joint_angle']
self.stand_duration = config['PointfootCfg']['stand_mode']['stand_duration']
self.control_cfg = config['PointfootCfg']['control']
self.rl_cfg = config['PointfootCfg']['normalization']
self.obs_scales = config['PointfootCfg']['normalization']['obs_scales']
self.actions_size = config['PointfootCfg']['size']['actions_size']
self.observations_size = config['PointfootCfg']['size']['observations_size']
self.imu_orientation_offset = np.array(list(config['PointfootCfg']['imu_orientation_offset'].values()))
self.user_cmd_cfg = config['PointfootCfg']['user_cmd_scales']
self.loop_frequency = config['PointfootCfg']['loop_frequency']
# Initialize variables for actions, observations, and commands
self.actions = np.zeros(self.actions_size)
self.observations = np.zeros(self.observations_size)
self.last_actions = np.zeros(self.actions_size)
self.commands = np.zeros(3) # command to the robot (e.g., velocity, rotation)
self.scaled_commands = np.zeros(3)
self.base_lin_vel = np.zeros(3) # base linear velocity
self.base_position = np.zeros(3) # robot base position
self.loop_count = 0 # loop iteration count
self.stand_percent = 0 # percentage of time the robot has spent in stand mode
self.policy_session = None # ONNX model session for policy inference
self.joint_num = len(self.joint_names) # number of joints
self.commands = np.zeros(3)
# Initialize joint angles based on the initial configuration
self.init_joint_angles = np.zeros(len(self.joint_names))
for i in range(len(self.joint_names)):
self.init_joint_angles[i] = self.init_state[self.joint_names[i]]
# Set initial mode to "STAND"
self.mode = "STAND"
# Main control loop
def run(self):
# Initialize default joint angles for standing
self.default_joint_angles = np.array([0.0] * len(self.joint_names))
self.stand_percent += 1 / (self.stand_duration * self.loop_frequency)
self.mode = "STAND"
self.loop_count = 0
# Set the loop rate based on the frequency in the configuration
rate = Rate(self.loop_frequency)
while True:
self.update()
rate.sleep()
# Handle the stand mode for smoothly transitioning the robot into standing
def handle_stand_mode(self):
if self.stand_percent < 1:
for j in range(len(self.joint_names)):
# Interpolate between initial and default joint angles during stand mode
pos_des = self.default_joint_angles[j] * (1 - self.stand_percent) + self.init_state[self.joint_names[j]] * self.stand_percent
self.set_joint_command_aligned(j, pos_des)
# Increment the stand percentage over time
self.stand_percent += 1 / (self.stand_duration * self.loop_frequency)
else:
# Switch to walk mode after standing
self.mode = "WALK"
def align_robot_state(self, robot_state: datatypes.RobotState):
aligned_robot_state = copy.deepcopy(robot_state)
aligned_robot_state.q[1] = robot_state.q[3]
aligned_robot_state.dq[1] = robot_state.dq[3]
aligned_robot_state.tau[1] = robot_state.tau[3]
aligned_robot_state.q[2] = robot_state.q[1]
aligned_robot_state.dq[2] = robot_state.dq[1]
aligned_robot_state.tau[2] = robot_state.tau[1]
aligned_robot_state.q[3] = robot_state.q[4]
aligned_robot_state.dq[3] = robot_state.dq[4]
aligned_robot_state.tau[3] = robot_state.tau[4]
aligned_robot_state.q[4] = robot_state.q[2]
aligned_robot_state.dq[4] = robot_state.dq[2]
aligned_robot_state.tau[4] = robot_state.tau[2]
return aligned_robot_state
# Handle the walk mode where the robot moves based on computed actions
def handle_walk_mode(self):
# Update the temporary robot state and IMU data
self.robot_state_tmp = self.align_robot_state(copy.deepcopy(self.robot_state))
self.imu_data_tmp = copy.deepcopy(self.imu_data)
# Execute actions every 'decimation' iterations
if self.loop_count % self.control_cfg['decimation'] == 0:
self.compute_observation()
self.compute_actions()
# Clip the actions within predefined limits
action_min = -self.rl_cfg['clip_scales']['clip_actions']
action_max = self.rl_cfg['clip_scales']['clip_actions']
self.actions = np.clip(self.actions, action_min, action_max)
# Iterate over the joints and set commands based on actions
joint_pos = np.array(self.robot_state_tmp.q)
joint_vel = np.array(self.robot_state_tmp.dq)
for i in range(len(joint_pos)):
# Compute the limits for the action based on joint position and velocity
action_min = (joint_pos[i] - self.init_joint_angles[i] +
(self.control_cfg['damping'] * joint_vel[i] - self.control_cfg['user_torque_limit']) /
self.control_cfg['stiffness'])
action_max = (joint_pos[i] - self.init_joint_angles[i] +
(self.control_cfg['damping'] * joint_vel[i] + self.control_cfg['user_torque_limit']) /
self.control_cfg['stiffness'])
# Clip action within limits
self.actions[i] = max(action_min / self.control_cfg['action_scale_pos'],
min(action_max / self.control_cfg['action_scale_pos'], self.actions[i]))
# Compute the desired joint position and set it
pos_des = self.actions[i] * self.control_cfg['action_scale_pos'] + self.init_joint_angles[i]
self.set_joint_command_aligned(i, pos_des)
# Save the last action for reference
self.last_actions[i] = self.actions[i]
def compute_observation(self):
# Convert IMU orientation from quaternion to Euler angles (ZYX convention)
imu_orientation = np.array(self.imu_data_tmp.quat)
q_wi = R.from_quat(imu_orientation).as_euler('zyx') # Quaternion to Euler ZYX conversion
inverse_rot = R.from_euler('zyx', q_wi).inv().as_matrix() # Get the inverse rotation matrix
# Project the gravity vector (pointing downwards) into the body frame
gravity_vector = np.array([0, 0, -1]) # Gravity in world frame (z-axis down)
projected_gravity = np.dot(inverse_rot, gravity_vector) # Transform gravity into body frame
# Retrieve base angular velocity from the IMU data
base_ang_vel = np.array(self.imu_data_tmp.gyro)
# Apply IMU orientation offset correction (using Euler angles)
rot = R.from_euler('zyx', self.imu_orientation_offset).as_matrix() # Rotation matrix for offset correction
base_ang_vel = np.dot(rot, base_ang_vel) # Apply correction to angular velocity
projected_gravity = np.dot(rot, projected_gravity) # Apply correction to projected gravity
# Retrieve joint positions and velocities from the robot state
joint_positions = np.array(self.robot_state_tmp.q)
joint_velocities = np.array(self.robot_state_tmp.dq)
# Retrieve the last actions that were applied to the robot
actions = np.array(self.last_actions)
# Create a command scaler matrix for linear and angular velocities
command_scaler = np.diag([
self.user_cmd_cfg['lin_vel_x'], # Scale factor for linear velocity in x direction
self.user_cmd_cfg['lin_vel_y'], # Scale factor for linear velocity in y direction
self.user_cmd_cfg['ang_vel_yaw'] # Scale factor for yaw (angular velocity)
])
# Apply scaling to the command inputs (velocity commands)
scaled_commands = np.dot(command_scaler, self.commands)
# Create the observation vector by concatenating various state variables:
# - Base angular velocity (scaled)
# - Projected gravity vector
# - Joint positions (difference from initial angles, scaled)
# - Joint velocities (scaled)
# - Last actions applied to the robot
# - Scaled command inputs
obs = np.concatenate([
base_ang_vel * self.obs_scales['ang_vel'], # Scaled base angular velocity
projected_gravity, # Projected gravity vector in body frame
(joint_positions - self.init_joint_angles) * self.obs_scales['dof_pos'], # Scaled joint positions
joint_velocities * self.obs_scales['dof_vel'], # Scaled joint velocities
actions, # Last actions taken by the robot
scaled_commands # Scaled velocity commands from user input
])
# Clip the observation values to within the specified limits for stability
self.observations = np.clip(
obs,
-self.rl_cfg['clip_scales']['clip_observations'], # Lower limit for clipping
self.rl_cfg['clip_scales']['clip_observations'] # Upper limit for clipping
)
def compute_actions(self):
"""
Computes the actions based on the current observations using the policy session.
"""
# Concatenate observations into a single tensor and convert to float32
input_tensor = np.concatenate([self.observations], axis=0)
input_tensor = input_tensor.astype(np.float32).reshape(1,-1)
# Create a dictionary of inputs for the policy session
inputs = {self.policy_input_names[0]: input_tensor}
# Run the policy session and get the output
output = self.policy_session.run(self.policy_output_names, inputs)
# Flatten the output and store it as actions
self.actions = np.array(output).flatten()
def set_joint_command(self, joint_index, position):
"""
Sends a command to set a joint to the desired position.
Replace this method with actual implementation according to your hardware.
Parameters:
joint_index (int): The index of the joint to command.
position (float): The desired position of the joint.
"""
self.robot_cmd.q[joint_index] = position
def set_joint_command_aligned(self, joint_index, position):
if joint_index == 2:
self.robot_cmd.q[1] = position
elif joint_index == 4:
self.robot_cmd.q[2] = position
elif joint_index == 1:
self.robot_cmd.q[3] = position
elif joint_index == 3:
self.robot_cmd.q[4] = position
else:
self.robot_cmd.q[joint_index] = position
def update(self):
"""
Updates the robot's state based on the current mode and publishes the robot command.
"""
if self.mode == "STAND":
self.handle_stand_mode()
elif self.mode == "WALK":
self.handle_walk_mode()
# Increment the loop count
self.loop_count += 1
# Publish the robot command
self.robot.publishRobotCmd(self.robot_cmd)
# Callback function for receiving robot command data
def robot_state_callback(self, robot_state: datatypes.RobotState):
"""
Callback function to update the robot state from incoming data.
Parameters:
robot_state (datatypes.RobotState): The current state of the robot.
"""
self.robot_state = robot_state
# Callback function for receiving imu data
def imu_data_callback(self, imu_data: datatypes.ImuData):
"""
Callback function to update IMU data from incoming data.
Parameters:
imu_data (datatypes.ImuData): The IMU data containing stamp, acceleration, gyro, and quaternion.
"""
self.imu_data.stamp = imu_data.stamp
self.imu_data.acc = imu_data.acc
self.imu_data.gyro = imu_data.gyro
# Rotate quaternion values
self.imu_data.quat[0] = imu_data.quat[1]
self.imu_data.quat[1] = imu_data.quat[2]
self.imu_data.quat[2] = imu_data.quat[3]
self.imu_data.quat[3] = imu_data.quat[0]
# Callback function for receiving sensor joy data
def sensor_joy_callback(self, sensor_joy: datatypes.SensorJoy):
self.commands[0] = sensor_joy.axes[1] * 0.5
self.commands[1] = sensor_joy.axes[0] * 0.5
self.commands[2] = sensor_joy.axes[2] * 0.5
if __name__ == '__main__':
# Get the robot type from the environment variable
robot_type = os.getenv("ROBOT_TYPE")
# Check if the ROBOT_TYPE environment variable is set, otherwise exit with an error
if not robot_type:
print("Error: Please set the ROBOT_TYPE using 'export ROBOT_TYPE=<robot_type>'.")
sys.exit(1)
# Create a Robot instance of the specified type
robot = Robot(RobotType.PointFoot)
# Default IP address for the robot
robot_ip = "127.0.0.1"
# Check if command-line argument is provided for robot IP
if len(sys.argv) > 1:
robot_ip = sys.argv[1]
# Initialize the robot with the provided IP address
if not robot.init(robot_ip):
sys.exit()
# Create and run the PointfootController
controller = PointfootController(f'{os.path.dirname(os.path.abspath(__file__))}/model/pointfoot', robot, robot_type)
controller.run()