-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcli_example.py
More file actions
177 lines (141 loc) · 5.28 KB
/
Copy pathcli_example.py
File metadata and controls
177 lines (141 loc) · 5.28 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
from tiger_bridge import TSPro
import signal
import sys
import os
def move_finished_handler():
print("\nmove finished!")
def received_position_handler(*args):
position = float(args[0])
print(f"\nreceived a position: {position}")
def error_handler(*args):
error_code = int(args[0])
print(f"received an error {TSPro.ERROR_CODES(error_code).name}")
def tool_engaged_handler():
print("tool down")
def tool_disengaged_handler():
print("tool UP")
def get_setting_handler(*args):
print("received setting data:", args)
def edge_detect_sensor_activated_handler():
print("edge detect sensor activated")
def edge_detect_sensor_deactivated_handler():
print("edge detect sensor deactivated")
def defect_sensor_activated_handler():
print("defect sensor activated")
def disconnection_handler():
print("socket connection lost. exiting...")
os._exit(1)
def clamps_engaged_handler():
print("clamps engaged")
def tool_enabled_handler():
print("tool enabled")
def tool_left_rest_handler():
print("tool left rest position")
def tool_extended_handler():
print("tool at extension")
def tool_left_extension_handler():
print("tool left extended position")
def tool_at_rest_handler():
print("tool reached rest position")
def clamps_disengaged_handler():
print("clamps disengaged")
def tool_cycle_completed_handler():
print("tool cycle completed")
def tool_motor_enabled_handler():
print("tool motor enabled")
def print_help():
print("""
Here's a list of valid commands:
- move_to {position}
- stop
- get_position
- get_setting
- home
- calibrate {position}
- cycle_tool
""")
def parse_command(tsp: TSPro, command: str):
segments = command.split(" ")
command_id = segments[0]
if command_id == "move_to":
position = segments[1]
try:
position = float(position)
tsp.request_move_to_position(position)
except ValueError:
print_help()
return
elif command_id == "stop":
tsp.request_stop()
elif command_id == "get_position":
tsp.request_current_position()
elif command_id == "exit":
os._exit(1)
elif command_id == "home":
tsp.request_home()
elif command_id == "calibrate":
if len(segments) < 2:
print("no position value provided")
return
position = segments[1]
try:
position = float(position)
tsp.request_calibrate(position)
except ValueError:
print_help()
return
elif command_id == "get_setting":
if len(segments) < 2:
print("no setting name provided")
return
setting_name = segments[1]
tsp.request_setting(setting_name)
elif command_id == "cycle_tool":
tsp.request_cycle_tool()
else:
print_help()
return
def main():
print("Welcome to the TigerBridge command-line interface example. Press Ctrl+C at any point to exit.")
tsp = TSPro()
ip = ""
if len(sys.argv) >= 2:
ip = sys.argv[1]
else:
ip = input("Enter a TigerStop Pro's IP address: ")
while True:
result = tsp.connect(ip)
if not result:
print("failed to connect")
ip = input("Enter a TigerStop Pro's IP address: ")
continue
else:
break
tsp.set_event_hook(TSPro.MESSAGE_CODES.MOVE_FINISHED, move_finished_handler)
tsp.set_event_hook(TSPro.MESSAGE_CODES.RECEIVED_POSITION, received_position_handler)
tsp.set_event_hook(TSPro.MESSAGE_CODES.ERROR, error_handler)
tsp.set_event_hook(TSPro.MESSAGE_CODES.SIK_TOOL_DISENGAGED, tool_disengaged_handler)
tsp.set_event_hook(TSPro.MESSAGE_CODES.SIK_TOOL_ENGAGED, tool_engaged_handler)
tsp.set_event_hook(TSPro.MESSAGE_CODES.EDGE_DETECT_SENSOR_ACTIVATED, edge_detect_sensor_activated_handler)
tsp.set_event_hook(TSPro.MESSAGE_CODES.EDGE_DETECT_SENSOR_DEACTIVATED, edge_detect_sensor_deactivated_handler)
tsp.set_event_hook(TSPro.MESSAGE_CODES.DEFECT_SENSOR_ACTIVATED, defect_sensor_activated_handler)
tsp.set_event_hook(TSPro.MESSAGE_CODES.DISCONNECTED, disconnection_handler)
tsp.set_event_hook(TSPro.MESSAGE_CODES.CLAMPS_ENGAGED, clamps_engaged_handler)
tsp.set_event_hook(TSPro.MESSAGE_CODES.TOOL_ENABLED, tool_enabled_handler)
tsp.set_event_hook(TSPro.MESSAGE_CODES.TOOL_LEFT_REST, tool_left_rest_handler)
tsp.set_event_hook(TSPro.MESSAGE_CODES.TOOL_EXTENDED, tool_extended_handler)
tsp.set_event_hook(TSPro.MESSAGE_CODES.TOOL_LEFT_EXTENSION, tool_left_extension_handler)
tsp.set_event_hook(TSPro.MESSAGE_CODES.TOOL_AT_REST, tool_at_rest_handler)
tsp.set_event_hook(TSPro.MESSAGE_CODES.CLAMPS_DISENGAGED, clamps_disengaged_handler)
tsp.set_event_hook(TSPro.MESSAGE_CODES.TOOL_CYCLE_COMPLETED, tool_cycle_completed_handler)
tsp.set_event_hook(TSPro.MESSAGE_CODES.TOOL_MOTOR_ENABLED, tool_motor_enabled_handler)
while True:
parse_command(tsp, input("Enter a command: "))
if __name__ == "__main__":
# set the SIGINT handler back to the kernel default
# python by default changes this, which breaks CTRL + C input
signal.signal(signal.SIGINT, signal.SIG_DFL)
try:
main()
except KeyboardInterrupt:
exit()