-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathmin_jerk_servoj.py
256 lines (198 loc) · 7.52 KB
/
min_jerk_servoj.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
"""
Code written based on servoj example from: https://github.com/davizinho5/RTDE_control_example
"""
import sys
sys.path.append('')
import logging
import rtde.rtde as rtde
import rtde.rtde_config as rtde_config
import time
from matplotlib import pyplot as plt
from min_jerk_planner_translation import PathPlanTranslation
# -------- functions -------------
def setp_to_list(setp):
temp = []
for i in range(0, 6):
temp.append(setp.__dict__["input_double_register_%i" % i])
return temp
def list_to_setp(setp, list):
for i in range(0, 6):
setp.__dict__["input_double_register_%i" % i] = list[i]
return setp
# ------------- robot communication stuff -----------------
ROBOT_HOST = '192.168.1.103'
ROBOT_PORT = 30004
config_filename = 'control_loop_configuration.xml' # specify xml file for data synchronization
logging.getLogger().setLevel(logging.INFO)
conf = rtde_config.ConfigFile(config_filename)
state_names, state_types = conf.get_recipe('state') # Define recipe for access to robot output ex. joints,tcp etc.
setp_names, setp_types = conf.get_recipe('setp') # Define recipe for access to robot input
watchdog_names, watchdog_types= conf.get_recipe('watchdog')
# -------------------- Establish connection--------------------
con = rtde.RTDE(ROBOT_HOST, ROBOT_PORT)
connection_state = con.connect()
# check if connection has been established
while connection_state != 0:
time.sleep(0.5)
connection_state = con.connect()
print("---------------Successfully connected to the robot-------------\n")
# get controller version
con.get_controller_version()
# ------------------- setup recipes ----------------------------
FREQUENCY = 500 # send data in 500 Hz instead of default 125Hz
con.send_output_setup(state_names, state_types, FREQUENCY)
setp = con.send_input_setup(setp_names, setp_types) # Configure an input package that the external application will send to the robot controller
watchdog = con.send_input_setup(watchdog_names, watchdog_types)
setp.input_double_register_0 = 0
setp.input_double_register_1 = 0
setp.input_double_register_2 = 0
setp.input_double_register_3 = 0
setp.input_double_register_4 = 0
setp.input_double_register_5 = 0
setp.input_bit_registers0_to_31 = 0
watchdog.input_int_register_0 = 0
# start data synchronization
if not con.send_start():
sys.exit()
start_pose = [-0.18507570121045797, -0.43755157063468963, 0.21101969081827837, -0.06998478570599498, -3.0949971695297402, 0.10056260631290592]
desired_pose = [-0.41227681851594755, -0.553539320093064, 0.07077025734923525, -0.06990025901302169, -3.0949715741835195, 0.10065200008528846]
orientation_const = start_pose[3:]
state = con.receive()
tcp1 = state.actual_TCP_pose
print(tcp1)
# ------------ mode = 1 (Connection) -----------
while True:
print('Boolean 1 is False, please click CONTINUE on the Polyscope')
state = con.receive()
con.send(watchdog)
# print(f"runtime state is {state.runtime_state}")
if state.output_bit_registers0_to_31 == True:
print('Boolean 1 is True, Robot Program can proceed to mode 1\n')
break
print("-------Executing moveJ -----------\n")
watchdog.input_int_register_0 = 1
con.send(watchdog) # sending mode == 1
list_to_setp(setp, start_pose) # changing initial pose to setp
con.send(setp) # sending initial pose
while True:
print('Waiting for movej() to finish')
state = con.receive()
con.send(watchdog)
if state.output_bit_registers0_to_31 == False:
print('Proceeding to mode 2\n')
break
print("-------Executing servoJ -----------\n")
watchdog.input_int_register_0 = 2
con.send(watchdog) # sending mode == 2
trajectory_time = 8 # time of min_jerk trajectory
dt = 1/500 # 500 Hz # frequency
plotter = True
# ------------------ Control loop initialization -------------------------
planner = PathPlanTranslation(start_pose, desired_pose, trajectory_time)
# ----------- minimum jerk preparation -----------------------
if plotter:
time_plot = []
min_jerk_x = []
min_jerk_y = []
min_jerk_z = []
min_jerk_vx = []
min_jerk_vy = []
min_jerk_vz = []
px = []
py = []
pz = []
vx = []
vy = []
vz = []
# -------------------------Control loop --------------------
state = con.receive()
tcp = state.actual_TCP_pose
t_current = 0
t_start = time.time()
while time.time() - t_start < trajectory_time:
t_init = time.time()
state = con.receive()
t_prev = t_current
t_current = time.time() - t_start
print(f"dt:{t_current-t_prev}")
# read state from the robot
if state.runtime_state > 1:
# ----------- minimum_jerk trajectory --------------
if t_current <= trajectory_time:
[position_ref, lin_vel_ref, acceleration_ref] = planner.trajectory_planning(t_current)
# ------------------ impedance -----------------------
current_pose = state.actual_TCP_pose
current_speed = state.actual_TCP_speed
pose = position_ref.tolist() + orientation_const
list_to_setp(setp, pose)
con.send(setp)
if plotter:
time_plot.append(time.time() - t_start)
min_jerk_x.append(position_ref[0])
min_jerk_y.append(position_ref[1])
min_jerk_z.append(position_ref[2])
min_jerk_vx.append(lin_vel_ref[0])
min_jerk_vy.append(lin_vel_ref[1])
min_jerk_vz.append(lin_vel_ref[2])
px.append(current_pose[0])
py.append(current_pose[1])
pz.append(current_pose[2])
vx.append(current_speed[0])
vy.append(current_speed[1])
vz.append(current_speed[2])
print(f"It took {time.time()-t_start}s to execute the servoJ")
print(f"time needed for min_jerk {trajectory_time}\n")
state = con.receive()
print('--------------------\n')
print(state.actual_TCP_pose)
# ====================mode 3===================
watchdog.input_int_register_0 = 3
con.send(watchdog)
con.send_pause()
con.disconnect()
if plotter:
# ----------- position -------------
plt.figure()
plt.plot(time_plot, min_jerk_x, label="x_min_jerk")
plt.plot(time_plot, px, label="x_robot")
plt.legend()
plt.grid()
plt.ylabel('Position in x[m]')
plt.xlabel('Time [sec]')
plt.figure()
plt.plot(time_plot, min_jerk_y, label="y_min_jerk")
plt.plot(time_plot, py, label="y_robot")
plt.legend()
plt.grid()
plt.ylabel('Position in y[m]')
plt.xlabel('Time [sec]')
plt.figure()
plt.plot(time_plot, min_jerk_z, label="z_min_jerk")
plt.plot(time_plot, pz, label="z_robot")
plt.legend()
plt.grid()
plt.ylabel('Position in z[m]')
plt.xlabel('Time [sec]')
# ----------- velocity -------------
plt.figure()
plt.plot(time_plot, min_jerk_vx, label="vx_min_jerk")
plt.plot(time_plot, vx, label="vx_robot")
plt.legend()
plt.grid()
plt.ylabel('Velocity [m/s]')
plt.xlabel('Time [sec]')
plt.figure()
plt.plot(time_plot, min_jerk_vy, label="vvy_min_jerk")
plt.plot(time_plot, vy, label="vy_robot")
plt.legend()
plt.grid()
plt.ylabel('Velocity [m/s]')
plt.xlabel('Time [sec]')
plt.figure()
plt.plot(time_plot, min_jerk_vz, label="vz_min_jerk")
plt.plot(time_plot, vz, label="vz_robot")
plt.legend()
plt.grid()
plt.ylabel('Velocity [m/s]')
plt.xlabel('Time [sec]')
plt.show()