-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
325 lines (298 loc) · 12.7 KB
/
main.py
File metadata and controls
325 lines (298 loc) · 12.7 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
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
"""
RSIPI Test Runner
=================
Uncomment one example at a time and run this file.
Uses RSI_EthernetConfig.xml from the project root by default.
Usage:
python main.py
python main.py --config path/to/other_config.xml
"""
import argparse
import time # noqa: F401 - used by commented examples
from multiprocessing import freeze_support
from RSIPI import RSIAPI
if __name__ == '__main__':
freeze_support()
parser = argparse.ArgumentParser(description="RSIPI Test Runner")
parser.add_argument("--config", type=str, default="RSI_EthernetConfig.xml",
help="Path to RSI config XML file")
parser.add_argument("--mode", type=str, default="relative", choices=["absolute", "relative"],
help="RSI correction mode (must match KRL program)")
parser.add_argument("--max-cart-rate", type=float, default=0.5,
help="Max Cartesian correction per cycle in mm (0 = no limit)")
parser.add_argument("--max-joint-rate", type=float, default=0.2,
help="Max joint correction per cycle in degrees (0 = no limit)")
parser.add_argument("--cycle-time", type=float, default=0.004,
help="RSI cycle time in seconds (0.004 = 4ms/250Hz, 0.012 = 12ms/83Hz)")
args = parser.parse_args()
api = RSIAPI(
args.config,
rsi_mode=args.mode,
max_cartesian_rate=args.max_cart_rate,
max_joint_rate=args.max_joint_rate,
cycle_time=args.cycle_time
)
# =========================================================================
# Example 01: Start / Stop
# =========================================================================
# api.start()
# print("RSI started. Press Enter to stop.")
# input()
# api.stop()
# =========================================================================
# Example 02: Send Cartesian correction (move TCP 50mm along X)
# =========================================================================
# api.start()
# print("Waiting for robot connection...")
# if not api.wait_for_connection(timeout=10):
# print("Timeout waiting for robot. Exiting.")
# api.stop()
# exit(1)
# print(f"Connected. IPOC: {api.monitoring.get_ipoc()}")
# api.motion.update_cartesian(X=0.01, Y=0, Z=0)
# print("Sent Cartesian correction. Press Enter to stop.")
# input()
# api.stop()
# =========================================================================
# Example 03: Send Joint correction (move A1 by 10 degrees) DOESNT WORK
# =========================================================================
# api.start()
# time.sleep(1)
# api.motion.update_joints(A1=50)
# print("Sent joint correction. Press Enter to stop.")
# input()
# api.stop()
# =========================================================================
# Example 04: External axes (move E1 by 100mm)
# =========================================================================
# api.start()
# time.sleep(1)
# api.motion.move_external_axis('E1', 100)
# print("Sent external axis correction. Press Enter to stop.")
# input()
# api.stop()
# =========================================================================
# Example 05: Digital I/O (toggle outputs)
# =========================================================================
# api.start()
# time.sleep(1)
# api.io.set_output(1, True)
# time.sleep(5)
# api.io.set_output(1, False)
# print("Set digital outputs. Press Enter to stop.")
# input()
# api.stop()
# =========================================================================
# Example 06: CSV Logging
# =========================================================================
# api.logging.start()
# api.start()
# print("Logging to CSV. Press Enter to stop.")
# input()
# api.logging.stop()
# api.stop()
# =========================================================================
# Example 07: Live Graphing
# =========================================================================
# api.start()
# api.viz.start_live_plot('3d')
# print("Live graph running. Press Enter to stop.")
# input()
# api.viz.stop_live_plot()
# api.stop()
# =========================================================================
# Example 08: Safety Limits (restrict X-axis to +/-500mm)
# =========================================================================
# api.safety.set_limit("RKorr.X", -500, 500)
# api.start()
# print("Safety limits active. Press Enter to stop.")
# input()
# api.stop()
# =========================================================================
# Example 09: Cartesian Trajectory (square pattern)
# =========================================================================
# api.start()
# time.sleep(1)
# waypoints = [
# {"X": 50, "Y": 0, "Z": 0},
# {"X": 50, "Y": 50, "Z": 0},
# {"X": 0, "Y": 50, "Z": 0},
# {"X": 0, "Y": 0, "Z": 0},
# ]
# for wp in waypoints:
# api.motion.update_cartesian(**wp)
# time.sleep(0.5)
# print("Trajectory complete. Press Enter to stop.")
# input()
# api.stop()
# =========================================================================
# Example 10: Safe Shutdown (Ctrl+C handler)
# =========================================================================
# try:
# api.start()
# print("Running. Press Ctrl+C for safe shutdown.")
# while True:
# time.sleep(0.1)
# except KeyboardInterrupt:
# print("Emergency stop triggered.")
# api.safety.stop()
# api.stop()
# =========================================================================
# Coordination 01: Basic Handshake (KRL <-> Python I/O signalling)
# =========================================================================
# api.start()
# time.sleep(1)
# print("Waiting for KRL ready signal on input 1...")
# if api.krl.wait_for_signal(1, timeout=10.0):
# print("KRL ready. Processing...")
# time.sleep(1)
# api.krl.signal_complete(1)
# print("Handshake complete.")
# else:
# print("Timeout waiting for KRL signal.")
# input("Press Enter to stop.")
# api.stop()
# =========================================================================
# Coordination 02: Parameter Passing (read/write Tech variables)
# =========================================================================
# api.start()
# time.sleep(1)
# pos_x = api.krl.read_param('T11')
# pos_y = api.krl.read_param('T12')
# pos_z = api.krl.read_param('T13')
# print(f"Current position from KRL: X={pos_x}, Y={pos_y}, Z={pos_z}")
# api.krl.write_param('C11', pos_x + 50)
# api.krl.write_param('C12', pos_y)
# api.krl.write_param('C13', pos_z)
# api.krl.signal_complete(1)
# print("Parameters sent. Press Enter to stop.")
# input()
# api.stop()
# =========================================================================
# Coordination 03: State Machine (multi-state workflow)
# =========================================================================
# IDLE, CALIBRATING, READY, EXECUTING, COMPLETE, ERROR = 0, 1, 2, 3, 4, 5
# api.start()
# time.sleep(1)
# state = IDLE
# print("State machine running. Press Ctrl+C to exit.")
# try:
# while state != COMPLETE:
# krl_state = int(api.krl.read_param('T11'))
# if krl_state == CALIBRATING:
# print("Calibrating...")
# time.sleep(2)
# api.krl.write_param('C11', READY)
# state = READY
# elif krl_state == EXECUTING:
# print("Executing motion...")
# state = EXECUTING
# elif krl_state == COMPLETE:
# print("Complete.")
# state = COMPLETE
# elif krl_state == ERROR:
# print("Error detected!")
# break
# time.sleep(0.05)
# except KeyboardInterrupt:
# pass
# api.stop()
# =========================================================================
# Advanced Motion 01: Velocity Profiles (trapezoidal vs S-curve)
# =========================================================================
# api.start()
# time.sleep(4)
# # Relative mode: each point is a per-cycle delta
# # 200 steps × 0.5mm = 100mm total at max rate limit
# traj = api.motion.generate_trajectory(
# {"X": 0, "Y": 0, "Z": 0},
# {"X": 100, "Y": 0, "Z": 0},
# steps=200,
# mode="relative")
# print(f"Executing trajectory: {len(traj)} steps, {traj[0]['X']:.2f}mm per step")
# api.motion.execute_trajectory(traj, space="cartesian", rate=0.012)
# print("Trajectory executed. Press Enter to stop.")
# input()
# api.stop()
# =========================================================================
# Advanced Motion 02: Geometric Primitives (arc, circle, spiral)
# =========================================================================
api.start()
print("Waiting for robot connection...")
if not api.wait_for_connection(timeout=10):
print("Timeout waiting for robot. Exiting.")
api.stop()
exit(1)
print(f"Connected. IPOC: {api.monitoring.get_ipoc()}")
input("Press Enter to start movement...")
# Generate absolute circle waypoints, convert to relative deltas
circle_abs = api.motion.generate_circle(
center={"X": 0, "Y": 0, "Z": 0},
radius=5, steps=200)
# Convert absolute → relative (delta between consecutive points)
# Start prev at first point so there's no initial jump
circle_rel = []
prev = circle_abs[0]
for pt in circle_abs[1:]:
delta = {k: pt[k] - prev.get(k, 0) for k in pt}
circle_rel.append(delta)
prev = pt
print(f"Executing circle: {len(circle_rel)} relative steps, radius=5mm")
api.motion.execute_trajectory(circle_rel, space="cartesian", rate=0.012)
# Zero out corrections so robot stops moving in relative mode
api.motion.update_cartesian(X=0, Y=0, Z=0)
print("Circle complete. Press Enter to stop.")
input()
api.stop()
# =========================================================================
# Advanced Motion 03: Path Blending (smooth corner transitions)
# =========================================================================
# api.start()
# time.sleep(1)
# traj1 = [{"X": i, "Y": 0, "Z": 0} for i in range(0, 50, 5)]
# traj2 = [{"X": 50, "Y": i, "Z": 0} for i in range(0, 50, 5)]
# blended = api.motion.blend_trajectories(traj1, traj2, blend_radius=15)
# print(f"Blended trajectory: {len(blended)} points")
# print("Press Enter to stop.")
# input()
# api.stop()
# =========================================================================
# Advanced Motion 04: Coordinate Transforms (frame conversions)
# =========================================================================
# api.start()
# time.sleep(1)
# base_pos = {"X": 500, "Y": 100, "Z": 800, "A": 0, "B": 0, "C": 0}
# world_pos = api.motion.transform_coordinates(
# base_pos, from_frame="BASE", to_frame="WORLD",
# frame_offset={"X": 100, "Y": 50, "Z": 0})
# print(f"Base: {base_pos}")
# print(f"World: {world_pos}")
# print("Press Enter to stop.")
# input()
# api.stop()
# =========================================================================
# Advanced Motion 05: Combined Motion (production drilling pattern)
# =========================================================================
# api.start()
# time.sleep(1)
# # Generate trajectory to work area
# nav_traj = api.motion.generate_trajectory(
# {"X": 0, "Y": 0, "Z": 0},
# {"X": 200, "Y": 200, "Z": 0},
# steps=30)
# # Apply S-curve velocity profile
# profiled = api.motion.generate_velocity_profile(
# nav_traj, max_velocity=80, max_acceleration=200, profile='s-curve')
# print(f"Navigation: {len(profiled)} profiled points")
# # Spiral inspection pattern
# inspection = api.motion.generate_spiral(
# center={"X": 200, "Y": 200, "Z": 0},
# start_radius=5, end_radius=40, pitch=0, revolutions=2, steps=40)
# # Drilling descent spiral
# drill = api.motion.generate_spiral(
# center={"X": 200, "Y": 200, "Z": 0},
# start_radius=3, end_radius=3, pitch=-5.0, revolutions=5, steps=50)
# print(f"Inspection: {len(inspection)} pts, Drill: {len(drill)} pts")
# print("Press Enter to stop.")
# input()
# api.stop()