forked from thomasfla/solopython
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdemo_viewer_solo12.py
More file actions
137 lines (103 loc) · 4.17 KB
/
demo_viewer_solo12.py
File metadata and controls
137 lines (103 loc) · 4.17 KB
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
# coding: utf8
import numpy as np
import argparse
from solo12 import Solo12
from pynput import keyboard
from utils.qualisysClient import QualisysClient
from utils.viewerClient import viewerClient, NonBlockingViewerFromRobot
DT = 0.001
key_pressed = False
def on_press(key):
"""Wait for a specific key press on the keyboard
Args:
key (keyboard.Key): the key we want to wait for
"""
global key_pressed
try:
if key == keyboard.Key.enter:
key_pressed = True
# Stop listener
return False
except AttributeError:
print('Unknown key {0} pressed'.format(key))
def put_on_the_floor(device, q_init):
"""Make the robot go to the default initial position and wait for the user
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
"""
global key_pressed
key_pressed = False
Kp_pos = 3.
Kd_pos = 0.01
imax = 3.0
pos = np.zeros(device.nb_motors)
for motor in range(device.nb_motors):
pos[motor] = q_init[device.motorToUrdf[motor]] * device.gearRatioSigned[motor]
listener = keyboard.Listener(on_press=on_press)
listener.start()
print("Put the robot on the floor and press Enter")
while not key_pressed:
device.UpdateMeasurment()
for motor in range(device.nb_motors):
ref = Kp_pos*(pos[motor] - device.hardware.GetMotor(motor).GetPosition() - Kd_pos*device.hardware.GetMotor(motor).GetVelocity())
ref = min(imax, max(-imax, ref))
device.hardware.GetMotor(motor).SetCurrentReference(ref)
device.SendCommand(WaitEndOfCycle=True)
print("Start the motion.")
def mcapi_playback(name_interface):
"""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:
name_interface (string): name of the interface that is used to communicate with the robot
"""
device = Solo12(name_interface, dt=DT)
nb_motors = device.nb_motors
q_viewer = np.array((7 + nb_motors) * [0.,])
v = viewerClient()
v.display(q_viewer)
# Default position after calibration
q_init = np.zeros(12)
# Calibrate encoders
device.Init(calibrateEncoders=True, q_init=q_init)
# Wait for Enter input before starting the control loop
put_on_the_floor(device, q_init)
# CONTROL LOOP ***************************************************
t = 0.0
t_max = 300.0
while ((not device.hardware.IsTimeout()) and (t < t_max)):
device.UpdateMeasurment() # Retrieve data from IMU and Motion capture
# Zero desired torques
tau = np.zeros(12)
# Set desired torques for the actuators
device.SetDesiredJointTorque(tau)
# Send command to the robot
device.SendCommand(WaitEndOfCycle=True)
if ((device.cpt % 100) == 0):
device.Print()
# Gepetto GUI
q_viewer[3:7] = device.baseOrientation # IMU Attitude
q_viewer[7:] = device.q_mes # Encoders
q_viewer[2] = 0.5
v.display(q_viewer)
t += DT
# ****************************************************************
# Whatever happened we send 0 torques to the motors.
device.SetDesiredJointTorque([0]*nb_motors)
device.SendCommand(WaitEndOfCycle=True)
if device.hardware.IsTimeout():
print("Masterboard timeout detected.")
print("Either the masterboard has been shut down or there has been a connection issue with the cable/wifi.")
device.hardware.Stop() # Shut down the interface between the computer and the master board
def main():
"""Main function
"""
parser = argparse.ArgumentParser(description='Playback trajectory to show the extent of solo12 workspace.')
parser.add_argument('-i',
'--interface',
required=True,
help='Name of the interface (use ifconfig in a terminal), for instance "enp1s0"')
mcapi_playback(parser.parse_args().interface)
if __name__ == "__main__":
main()