|
| 1 | +#!/usr/bin/python |
| 2 | + |
| 3 | +from socket import * |
| 4 | +import sys |
| 5 | +from multiprocessing import Lock |
| 6 | +import logging |
| 7 | +from pymycobot.log import setup_logging |
| 8 | + |
| 9 | + |
| 10 | +mutex = Lock() |
| 11 | + |
| 12 | + |
| 13 | +class ElephantRobot(object): |
| 14 | + def __init__(self, host, port, debug=False): |
| 15 | + # setup connection |
| 16 | + self.debug = debug |
| 17 | + setup_logging(self.debug) |
| 18 | + self.log = logging.getLogger(__name__) |
| 19 | + self.BUFFSIZE = 2048 |
| 20 | + self.ADDR = (host, port) |
| 21 | + self.tcp_client = socket(AF_INET, SOCK_STREAM) |
| 22 | + |
| 23 | + def start_client(self): |
| 24 | + try: |
| 25 | + self.tcp_client.connect(self.ADDR) |
| 26 | + return "" |
| 27 | + except Exception as e: |
| 28 | + return e |
| 29 | + |
| 30 | + def stop_client(self): |
| 31 | + self.tcp_client.close() |
| 32 | + |
| 33 | + def send_command(self, command): |
| 34 | + with mutex: |
| 35 | + self.tcp_client.send(command.encode()) |
| 36 | + recv_data = self.tcp_client.recv(self.BUFFSIZE).decode() |
| 37 | + res_str = str(recv_data) |
| 38 | + print("recv = " + res_str) |
| 39 | + res_arr = res_str.split(":") |
| 40 | + if len(res_arr) == 2: |
| 41 | + return res_arr[1] |
| 42 | + else: |
| 43 | + return "" |
| 44 | + |
| 45 | + def string_to_coords(self, data): |
| 46 | + data = data.replace("[", "") |
| 47 | + data = data.replace("]", "") |
| 48 | + data_arr = data.split(",") |
| 49 | + if len(data_arr) == 6: |
| 50 | + try: |
| 51 | + coords_1 = float(data_arr[0]) |
| 52 | + coords_2 = float(data_arr[1]) |
| 53 | + coords_3 = float(data_arr[2]) |
| 54 | + coords_4 = float(data_arr[3]) |
| 55 | + coords_5 = float(data_arr[4]) |
| 56 | + coords_6 = float(data_arr[5]) |
| 57 | + coords = [coords_1, coords_2, coords_3, coords_4, coords_5, coords_6] |
| 58 | + return coords |
| 59 | + except: |
| 60 | + return self.invalid_coords() |
| 61 | + return self.invalid_coords() |
| 62 | + |
| 63 | + def string_to_double(self, data): |
| 64 | + try: |
| 65 | + val = float(data) |
| 66 | + return val |
| 67 | + except: |
| 68 | + return -9999.99 |
| 69 | + |
| 70 | + def string_to_int(self, data): |
| 71 | + try: |
| 72 | + val = int(data) |
| 73 | + return val |
| 74 | + except: |
| 75 | + return -9999 |
| 76 | + |
| 77 | + def invalid_coords(self): |
| 78 | + coords = [-1, -2, -3, -4, -1, -1] |
| 79 | + return coords |
| 80 | + |
| 81 | + def get_angles(self): |
| 82 | + command = "get_angles()\n" |
| 83 | + res = self.send_command(command) |
| 84 | + return self.string_to_coords(res) |
| 85 | + |
| 86 | + def get_coords(self): |
| 87 | + command = "get_coords()\n" |
| 88 | + res = self.send_command(command) |
| 89 | + return self.string_to_coords(res) |
| 90 | + |
| 91 | + def get_speed(self): |
| 92 | + command = "get_speed()\n" |
| 93 | + res = self.send_command(command) |
| 94 | + return self.string_to_double(res) |
| 95 | + |
| 96 | + def power_on(self): |
| 97 | + command = "power_on()\n" |
| 98 | + res = self.send_command(command) |
| 99 | + return True |
| 100 | + |
| 101 | + def power_off(self): |
| 102 | + command = "power_off()\n" |
| 103 | + res = self.send_command(command) |
| 104 | + return True |
| 105 | + |
| 106 | + def start_robot(self): |
| 107 | + command = "start_robot()\n" |
| 108 | + res = self.send_command(command) |
| 109 | + return True |
| 110 | + |
| 111 | + def check_running(self): |
| 112 | + command = "check_running()\n" |
| 113 | + res = self.send_command(command) |
| 114 | + return res == "1" |
| 115 | + |
| 116 | + def state_check(self): |
| 117 | + command = "state_check()\n" |
| 118 | + res = self.send_command(command) |
| 119 | + return res == "1" |
| 120 | + |
| 121 | + def program_open(self, file_path): |
| 122 | + command = "program_open(" + file_path + ")\n" |
| 123 | + res = self.send_command(command) |
| 124 | + return self.string_to_int(res) |
| 125 | + |
| 126 | + def program_run(self, start_line): |
| 127 | + command = "program_run(" + str(start_line) + ")\n" |
| 128 | + res = self.send_command(command) |
| 129 | + return self.string_to_int(res) |
| 130 | + |
| 131 | + def read_next_error(self): |
| 132 | + command = "read_next_error()\n" |
| 133 | + res = self.send_command(command) |
| 134 | + return res |
| 135 | + |
| 136 | + def write_coords(self, coords, speed): |
| 137 | + command = "set_coords(" |
| 138 | + for item in coords: |
| 139 | + command += str(item) + "," |
| 140 | + command += str(speed) + ")\n" |
| 141 | + self.send_command(command) |
| 142 | + |
| 143 | + def write_coord(self, axis, value, speed): |
| 144 | + coords = self.get_coords() |
| 145 | + if coords != self.invalid_coords(): |
| 146 | + coords[axis] = value |
| 147 | + self.write_coords(coords, speed) |
| 148 | + |
| 149 | + def write_angles(self, angles, speed): |
| 150 | + command = "set_angles(" |
| 151 | + for item in angles: |
| 152 | + command += str(item) + "," |
| 153 | + command += str(speed) + ")\n" |
| 154 | + self.send_command(command) |
| 155 | + |
| 156 | + def write_angle(self, joint, value, speed): |
| 157 | + angles = self.get_angles() |
| 158 | + if angles != self.invalid_coords(): |
| 159 | + angles[joint] = value |
| 160 | + self.write_angles(angles, speed) |
| 161 | + |
| 162 | + def set_speed(self, percentage): |
| 163 | + command = "set_speed(" + str(percentage) + ")\n" |
| 164 | + self.send_command(command) |
| 165 | + |
| 166 | + def set_carte_torque_limit(self, axis_str, value): |
| 167 | + command = "set_torque_limit(" + axis_str + "," + str(value) + ")\n" |
| 168 | + self.send_command(command) |
| 169 | + |
| 170 | + def set_upside_down(self, up_down): |
| 171 | + up = "1" |
| 172 | + if up_down: |
| 173 | + up = "0" |
| 174 | + command = "set_upside_down(" + up + ")\n" |
| 175 | + self.send_command(command) |
| 176 | + |
| 177 | + def set_payload(self, payload): |
| 178 | + command = "set_speed(" + str(payload) + ")\n" |
| 179 | + self.send_command(command) |
| 180 | + |
| 181 | + def state_on(self): |
| 182 | + command = "state_on()\n" |
| 183 | + self.send_command(command) |
| 184 | + |
| 185 | + def state_off(self): |
| 186 | + command = "state_off()\n" |
| 187 | + self.send_command(command) |
| 188 | + |
| 189 | + def task_stop(self): |
| 190 | + command = "task_stop()\n" |
| 191 | + self.send_command(command) |
| 192 | + |
| 193 | + def jog_angle(self, joint_str, direction, speed): |
| 194 | + command = ( |
| 195 | + "jog_angle(" + joint_str + "," + str(direction) + "," + str(speed) + ")\n" |
| 196 | + ) |
| 197 | + self.send_command(command) |
| 198 | + |
| 199 | + def jog_coord(self, axis_str, direction, speed): |
| 200 | + command = ( |
| 201 | + "jog_coord(" + axis_str + "," + str(direction) + "," + str(speed) + ")\n" |
| 202 | + ) |
| 203 | + self.send_command(command) |
| 204 | + |
| 205 | + def get_digital_in(self, pin_number): |
| 206 | + command = "get_digital_in(" + str(pin_number) + ")\n" |
| 207 | + self.send_command(command) |
| 208 | + |
| 209 | + def get_digital_out(self, pin_number): |
| 210 | + command = "get_digital_out(" + str(pin_number) + ")\n" |
| 211 | + print(command) |
| 212 | + self.send_command(command) |
| 213 | + |
| 214 | + def get_joint_current(self, joint_number: int): |
| 215 | + command = "get_joint_current(" + str(joint_number) + ")\n" |
| 216 | + print(command) |
| 217 | + self.send_command(command) |
| 218 | + |
| 219 | + def set_digital_out(self, pin_number, pin_signal): |
| 220 | + command = "set_digital_out(" + str(pin_number) + "," + str(pin_signal) + ")\n" |
| 221 | + self.send_command(command) |
| 222 | + |
| 223 | + def set_analog_out(self, pin_number, pin_signal): |
| 224 | + command = "set_analog_out(" + str(pin_number) + "," + str(pin_signal) + ")\n" |
| 225 | + self.send_command(command) |
| 226 | + |
| 227 | + def send_feed_override(self, override): |
| 228 | + command = "set_feed_rate(" + str(override) + ")\n" |
| 229 | + res = self.send_command(command) |
| 230 | + return self.string_to_int(res) |
| 231 | + |
| 232 | + def get_acceleration(self): |
| 233 | + command = "get_acceleration()\n" |
| 234 | + res = self.send_command(command) |
| 235 | + return self.string_to_int(res) |
| 236 | + |
| 237 | + def set_acceleration(self, acceleration): |
| 238 | + command = "set_acceleration(" + str(acceleration) + ")\n" |
| 239 | + self.send_command(command) |
| 240 | + |
| 241 | + def command_wait_done(self): |
| 242 | + command = "wait_command_done()\n" |
| 243 | + self.send_command(command) |
| 244 | + |
| 245 | + def wait(self, seconds): |
| 246 | + command = "wait(" + str(seconds) + ")\n" |
| 247 | + self.send_command(command) |
| 248 | + |
| 249 | + def assign_variable(self, var_name, var_value): |
| 250 | + command = 'assign_variable("' + str(var_name) + '",' + str(var_value) + ")\n" |
| 251 | + self.send_command(command) |
| 252 | + |
| 253 | + def get_variable(self, var_name): |
| 254 | + command = 'get_variable("' + str(var_name) + '")\n' |
| 255 | + return self.send_command(command) |
| 256 | + |
| 257 | + |
| 258 | +if __name__ == "__main__": |
| 259 | + ep = ElephantRobot("192.168.124.28", 5001) |
| 260 | + res = ep.start_client() |
| 261 | + if res != "": |
| 262 | + print(res) |
| 263 | + sys.exit(1) |
| 264 | + print(ep.wait(5)) |
| 265 | + print(ep.get_angles()) |
| 266 | + print(ep.get_coords()) |
| 267 | + print(ep.get_speed()) |
| 268 | + # print(ep.power_on()) |
| 269 | + # print(ep.power_off()) |
| 270 | + print(ep.check_running()) |
| 271 | + print(ep.state_check()) |
| 272 | + print(ep.program_open("a.tax")) |
| 273 | + print(ep.program_run(0)) |
| 274 | + print(ep.read_next_error()) |
| 275 | + print(ep.write_coords([1, 2, 3, 4, 5, 6], 110)) |
| 276 | + print(ep.write_coord(1, 100, 200)) |
| 277 | + print(ep.write_angles([10, 20, 30, 40, 50, 60], 110)) |
| 278 | + print(ep.write_angle(3, 180, 200)) |
| 279 | + print(ep.set_speed(377)) |
| 280 | + print(ep.set_carte_torque_limit("x", 55)) |
| 281 | + print(ep.set_upside_down(False)) |
| 282 | + print(ep.set_payload(100)) |
| 283 | + print(ep.state_on()) |
| 284 | + print(ep.state_off()) |
| 285 | + print(ep.task_stop()) |
| 286 | + print(ep.jog_angle("j2", 1, 300)) |
| 287 | + print(ep.jog_coord("rY", 0, 200)) |
| 288 | + print(ep.get_digital_in(3)) |
| 289 | + print(ep.get_digital_out(3)) |
| 290 | + print(ep.set_digital_out(3, 1)) |
| 291 | + print(ep.get_digital_out(3)) |
| 292 | + print(ep.set_analog_out(1, 3.5)) |
| 293 | + print(ep.get_acceleration()) |
| 294 | + print(ep.set_acceleration(55)) |
| 295 | + print(ep.command_wait_done()) |
| 296 | + print(ep.get_variable("f")) |
| 297 | + print(ep.assign_variable("ss", '"eee"')) |
| 298 | + print(ep.get_joint_current(1)) |
| 299 | + |
| 300 | + ep.stop_client() |
| 301 | + |
| 302 | + name = input("input:") |
| 303 | + print(name) |
0 commit comments