-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathpointfoot_controller.py
432 lines (362 loc) · 20.9 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
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
import os
import sys
import copy
import numpy as np
import yaml
import time
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, start_controller):
# Initialize robot and type information
self.robot = robot
self.robot_type = robot_type
self.is_point_foot = self.robot_type.startswith("PF")
self.is_wheel_foot = self.robot_type.startswith("WF")
self.is_sole_foot = self.robot_type.startswith("SF")
# 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)
# Set up a callback to receive diagnostic data
self.robot_diagnostic_callback_partial = partial(self.robot_diagnostic_callback)
self.robot.subscribeDiagnosticValue(self.robot_diagnostic_callback_partial)
# Initialize the calibration state to -1, indicating no calibration has occurred.
self.calibration_state = -1
# Flag to start the controller
self.start_controller = start_controller
# 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)
if self.is_wheel_foot:
self.joint_pos_idxs = config['PointfootCfg']['size']['jointpos_idxs']
self.wheel_joint_damping = config['PointfootCfg']['control']['wheel_joint_damping']
self.wheel_joint_torque_limit = config['PointfootCfg']['control']['wheel_joint_torque_limit']
elif self.is_sole_foot:
self.ankle_joint_damping = config['PointfootCfg']['control']['ankle_joint_damping']
self.ankle_joint_torque_limit = config['PointfootCfg']['control']['ankle_joint_torque_limit']
# 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):
# Wait until the controller is started
while not self.start_controller:
time.sleep(1)
# 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 self.start_controller:
self.update()
rate.sleep()
# Reset robot command values to ensure a safe stop when exiting the loop
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 = [0. for x in range(0, self.joint_num)]
self.robot_cmd.Kd = [1.0 for x in range(0, self.joint_num)]
self.robot.publishRobotCmd(self.robot_cmd)
time.sleep(1)
# 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(j, pos_des, 0, 0, self.control_cfg['stiffness'], self.control_cfg['damping'])
# 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"
# 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 = 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)):
if self.is_point_foot or (i + 1) % 4 != 0:
# 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(i, pos_des, 0, 0, self.control_cfg['stiffness'], self.control_cfg['damping'])
# Save the last action for reference
self.last_actions[i] = self.actions[i]
elif self.is_wheel_foot:
action_min = joint_vel[i] - self.wheel_joint_torque_limit / self.wheel_joint_damping
action_max = joint_vel[i] + self.wheel_joint_torque_limit / self.wheel_joint_damping
self.last_actions[i] = self.actions[i]
self.actions[i] = max(action_min / self.wheel_joint_damping,
min(action_max / self.wheel_joint_damping, self.actions[i]))
velocity_des = self.actions[i] * self.wheel_joint_damping
self.set_joint_command(i, 0, velocity_des, 0, 0, self.wheel_joint_damping)
elif self.is_sole_foot:
action_min = joint_vel[i] - self.ankle_joint_torque_limit / self.ankle_joint_damping
action_max = joint_vel[i] + self.ankle_joint_torque_limit / self.ankle_joint_damping
self.last_actions[i] = self.actions[i]
self.actions[i] = max(action_min / self.ankle_joint_damping,
min(action_max / self.ankle_joint_damping, self.actions[i]))
velocity_des = self.actions[i] * self.ankle_joint_damping
self.set_joint_command(i, 0, velocity_des, 0, 0, self.ankle_joint_damping)
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)
# Populate observation vector
joint_pos_value = (joint_positions - self.init_joint_angles) * self.obs_scales['dof_pos']
# In WF, joint pos does not include wheel speed, index(3, 7) needs to be removed
if self.is_wheel_foot:
joint_pos_input = np.array([joint_pos_value[idx] for idx in self.joint_pos_idxs])
else:
joint_pos_input = joint_pos_value
# 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_pos_input, # 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)
# 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, q, dq, tau, kp, kd):
"""
Sends a command to configure the state of a specific joint.
This method updates the joint's desired position, velocity, torque, and control gains.
Replace this implementation with the actual communication logic for your hardware.
Parameters:
joint_index (int): The index of the joint to be controlled.
q (float): The desired joint position, typically in radians or degrees.
dq (float): The desired joint velocity, typically in radians/second or degrees/second.
tau (float): The desired joint torque, typically in Newton-meters (Nm).
kp (float): The proportional gain for position control.
kd (float): The derivative gain for velocity control.
"""
self.robot_cmd.q[joint_index] = q
self.robot_cmd.dq[joint_index] = dq
self.robot_cmd.tau[joint_index] = tau
self.robot_cmd.Kp[joint_index] = kp
self.robot_cmd.Kd[joint_index] = kd
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):
# Check if the robot is in the calibration state and both L1 (button index 4) and Y (button index 3) buttons are pressed.
if not self.start_controller and self.calibration_state == 0 and sensor_joy.buttons[4] == 1 and sensor_joy.buttons[3] == 1:
print(f"L1 + Y: start_controller...")
self.start_controller = True
# Check if both L1 (button index 4) and X (button index 2) are pressed to stop the controller
if self.start_controller and sensor_joy.buttons[4] == 1 and sensor_joy.buttons[2] == 1:
print(f"L1 + X: stop_controller...")
self.start_controller = False
linear_x = sensor_joy.axes[1]
linear_y = sensor_joy.axes[0]
angular_z = sensor_joy.axes[2]
linear_x = 1.0 if linear_x > 1.0 else (-1.0 if linear_x < -1.0 else linear_x)
linear_y = 1.0 if linear_y > 1.0 else (-1.0 if linear_y < -1.0 else linear_y)
angular_z = 1.0 if angular_z > 1.0 else (-1.0 if angular_z < -1.0 else angular_z)
self.commands[0] = linear_x * 0.5
self.commands[1] = linear_y * 0.5
self.commands[2] = angular_z * 0.5
# Callback function for receiving diagnostic data
def robot_diagnostic_callback(self, diagnostic_value: datatypes.DiagnosticValue):
# Check if the received diagnostic data is related to calibration.
if diagnostic_value.name == "calibration":
print(f"Calibration state: {diagnostic_value.code}")
self.calibration_state = diagnostic_value.code
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()
# Determine if the simulation is running
start_controller = robot_ip == "127.0.0.1"
# Create and run the PointfootController
controller = PointfootController(f'{os.path.dirname(os.path.abspath(__file__))}/model/pointfoot', robot, robot_type, start_controller)
controller.run()