Newer
Older
from pathlib import Path
odri
committed
params = qrw.Params() # Object that holds all controller parameters

Pierre-Alexandre Leziart
committed
repo = git.Repo(search_parent_directories=True)
sha = repo.head.object.hexsha
if params.SIMULATION:

Pierre-Alexandre Leziart
committed
else:
import libodri_control_interface_pywrap as oci

Pierre-Alexandre Leziart
committed
odri
committed
def get_input():

Pierre-Alexandre Leziart
committed
def put_on_the_floor(device, q_init):
"""
Make the robot go to the default initial position and wait for the user

Pierre-Alexandre Leziart
committed
to press the Enter key to start the main control loop
Args:
device (robot wrapper): a wrapper to communicate with the robot
q_init (array): the default position of the robot
"""
print("PUT ON THE FLOOR.")
device.joints.set_position_gains(Kp_pos * np.ones(12))
device.joints.set_velocity_gains(Kd_pos * np.ones(12))
device.joints.set_desired_positions(q_init)
device.joints.set_desired_velocities(np.zeros(12))
device.joints.set_torques(np.zeros(12))
odri
committed
i.start()
print("Put the robot on the floor and press Enter")

Pierre-Alexandre Leziart
committed
while i.is_alive():
device.parse_sensor_data()
device.send_command_and_wait_end_of_cycle(params.dt_wbc)
# Slow increase till 1/4th of mass is supported by each foot
steps = int(duration_increase / params.dt_wbc)
tau_ff = np.array([0.0, 0.022, 0.5] * 2 + [0.0, -0.022, -0.5] * 2)
# tau_ff = np.array([0.0, 0.022, 0.5, 0.0, 0.022, 0.5, 0.0, 0.025, 0.575, 0.0, 0.025, 0.575])
for i in range(steps):
device.joints.set_torques(tau_ff * i / steps)
device.parse_sensor_data()
device.send_command_and_wait_end_of_cycle(params.dt_wbc)

Pierre-Alexandre Leziart
committed
print("Start the motion.")
def check_position_error(device, controller):
"""
Check the distance between current and desired position of the joints
Args:
device (robot wrapper): a wrapper to communicate with the robot
controller (array): the controller storing the desired position
"""
if np.max(np.abs(controller.result.q_des - device.joints.positions)) > 0.15:
print("DIFFERENCE: ", controller.result.q_des - device.joints.positions)
print("q_des: ", controller.result.q_des)
print("q_mes: ", device.joints.positions)
return True
return False
def damp_control(device, nb_motors):
"""
Damp the control during 2.5 seconds

Pierre-Alexandre Leziart
committed
Args:
device (robot wrapper): a wrapper to communicate with the robot
nb_motors (int): number of motors

Pierre-Alexandre Leziart
committed
"""
t = 0.0
t_max = 2.5
while (not device.is_timeout) and (t < t_max):
# Set desired quantities for the actuators
device.joints.set_position_gains(np.zeros(nb_motors))
device.joints.set_velocity_gains(0.1 * np.ones(nb_motors))
device.joints.set_desired_positions(np.zeros(nb_motors))
device.joints.set_desired_velocities(np.zeros(nb_motors))
device.joints.set_torques(np.zeros(nb_motors))
# Send command to the robot
device.send_command_and_wait_end_of_cycle(params.dt_wbc)
if (t % 1) < 5e-5:
print("IMU attitude:", device.imu.attitude_euler)
print("joint pos :", device.joints.positions)
print("joint vel :", device.joints.velocities)
device.robot_interface.PrintStats()

Pierre-Alexandre Leziart
committed

Pierre-Alexandre Leziart
committed

Pierre-Alexandre Leziart
committed
"""
Main function that calibrates the robot, get it into a default waiting position then launch
the main control loop once the user has pressed the Enter key
Args:
if not params.SIMULATION:
params.enable_pyb_GUI = False

Pierre-Alexandre Leziart
committed
# Default position after calibration
q_init = np.array(params.q_init.tolist())

Pierre-Alexandre Leziart
committed
if params.SIMULATION:

Pierre-Alexandre Leziart
committed
device = PyBulletSimulator()
qc = None
else:
device = oci.robot_from_yaml_file(params.config_file)

Pierre-Alexandre Leziart
committed
qc = QualisysClient(ip="140.93.16.160", body_id=0)
if params.LOGGING or params.PLOTTING:
loggerControl = LoggerControl(pd, params, log_size=params.N_SIMULATION)

Pierre-Alexandre Leziart
committed
if params.SIMULATION:
q_init,
params.envID,
params.use_flat_plane,
params.enable_pyb_GUI,
params.dt_wbc,
device.initialize(q_init[:])
device.joints.set_zero_commands()
device.parse_sensor_data()

Pierre-Alexandre Leziart
committed
put_on_the_floor(device, q_init)
# CONTROL LOOP ***************************************************
t_max = (params.N_SIMULATION - 1) * params.dt_wbc
t_log_whole = np.zeros((params.N_SIMULATION))
k_log_whole = 0
T_whole = time.time()
dT_whole = 0.0
while (not device.is_timeout) and (t < t_max) and (not controller.error):
t_start_whole = time.time()

Pierre-Alexandre Leziart
committed
if controller.compute(device, qc):
break
if t <= 10 * params.dt_wbc and check_position_error(device, controller):
break

Pierre-Alexandre Leziart
committed
device.joints.set_position_gains(controller.result.P)
device.joints.set_velocity_gains(controller.result.D)
device.joints.set_desired_positions(controller.result.q_des)
device.joints.set_desired_velocities(controller.result.v_des)
device.joints.set_torques(controller.result.FF * controller.result.tau_ff.ravel())
device.send_command_and_wait_end_of_cycle(params.dt_wbc)

Pierre-Alexandre Leziart
committed
if params.LOGGING or params.PLOTTING:

Pierre-Alexandre Leziart
committed
t_end_whole = time.time()

Pierre-Alexandre Leziart
committed
dT_whole = T_whole
T_whole = time.time()
dT_whole = T_whole - dT_whole
t_log_whole[k_log_whole] = t_end_whole - t_start_whole
k_log_whole += 1

Pierre-Alexandre Leziart
committed
# ****************************************************************

Pierre-Alexandre Leziart
committed
if params.enable_multiprocessing:
print("Stopping parallel process MPC")

Pierre-Alexandre Leziart
committed
# ****************************************************************

Pierre-Alexandre Leziart
committed
# Send 0 torques to the motors.
device.joints.set_torques(np.zeros(12))
device.send_command_and_wait_end_of_cycle(params.dt_wbc)

Pierre-Alexandre Leziart
committed
if device.is_timeout:

Pierre-Alexandre Leziart
committed
print("Masterboard timeout detected.")
date_str = datetime.now().strftime("%Y_%m_%d_%H_%M")
log_path = Path("/tmp") / "logs" / date_str
log_path.mkdir(parents=True)
with open(str(log_path / 'readme.txt'), 'w') as f:
f.write(msg)
if params.PLOTTING:
loggerControl.plot(save=True, fileName=str(log_path))
print("Plots saved in ", str(log_path) + "/")
if params.SIMULATION and params.enable_pyb_GUI:

Pierre-Alexandre Leziart
committed
device.Stop()
print("End of script")

Pierre-Alexandre Leziart
committed