|
| 1 | +# coding=utf-8 |
| 2 | + |
| 3 | +from __future__ import division |
| 4 | +import time |
| 5 | +import math |
| 6 | +import socket |
| 7 | +import logging |
| 8 | + |
| 9 | +from pymycobot.log import setup_logging |
| 10 | +from pymycobot.generate import MyCobotCommandGenerator |
| 11 | +from pymycobot.common import ProtocolCode, write, read |
| 12 | +from pymycobot.error import calibration_parameters |
| 13 | + |
| 14 | + |
| 15 | +class MyPalletizerSocket(MyCobotCommandGenerator): |
| 16 | + """MyCobot Python API Serial communication class. |
| 17 | + Note: Please use this class under the same network |
| 18 | +
|
| 19 | + Supported methods: |
| 20 | +
|
| 21 | + # Overall status |
| 22 | + Look at parent class: `MyCobotCommandGenerator`. |
| 23 | +
|
| 24 | + # MDI mode and operation |
| 25 | + get_radians() |
| 26 | + send_radians() |
| 27 | + sync_send_angles() * |
| 28 | + sync_send_coords() * |
| 29 | + Other look at parent class: `MyCobotCommandGenerator`. |
| 30 | +
|
| 31 | + # JOG mode and operation |
| 32 | + Look at parent class: `MyCobotCommandGenerator`. |
| 33 | +
|
| 34 | + # Running status and Settings |
| 35 | + Look at parent class: `MyCobotCommandGenerator`. |
| 36 | +
|
| 37 | + # Servo control |
| 38 | + Look at parent class: `MyCobotCommandGenerator`. |
| 39 | +
|
| 40 | + # Atom IO |
| 41 | + Look at parent class: `MyCobotCommandGenerator`. |
| 42 | +
|
| 43 | + # Basic |
| 44 | + Look at parent class: `MyCobotCommandGenerator`. |
| 45 | +
|
| 46 | + # Other |
| 47 | + wait() * |
| 48 | + """ |
| 49 | + _write = write |
| 50 | + _read = read |
| 51 | + |
| 52 | + def __init__(self, ip, netport=9000): |
| 53 | + """ |
| 54 | + Args: |
| 55 | + ip: Server ip |
| 56 | + netport: Server port |
| 57 | + """ |
| 58 | + super(MyPalletizerSocket, self).__init__() |
| 59 | + self.calibration_parameters = calibration_parameters |
| 60 | + self.SERVER_IP = ip |
| 61 | + self.SERVER_PORT = netport |
| 62 | + self.rasp = False |
| 63 | + self.sock = self.connect_socket() |
| 64 | + |
| 65 | + def connect(self, serialport="/dev/ttyAMA0", baudrate="1000000", timeout='0.1'): |
| 66 | + """Connect the robot arm through the serial port and baud rate (Use only when connecting the pi version) |
| 67 | + |
| 68 | + Args: |
| 69 | + serialport: (str) default /dev/ttyAMA0 |
| 70 | + baudrate: default 1000000 |
| 71 | + timeout: default 0.1 |
| 72 | + |
| 73 | + """ |
| 74 | + self.rasp = True |
| 75 | + self._write(serialport, "socket") |
| 76 | + self._write(baudrate, "socket") |
| 77 | + self._write(timeout, "socket") |
| 78 | + |
| 79 | + def connect_socket(self): |
| 80 | + sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) |
| 81 | + sock.connect((self.SERVER_IP, self.SERVER_PORT)) |
| 82 | + return sock |
| 83 | + |
| 84 | + def _mesg(self, genre, *args, **kwargs): |
| 85 | + """ |
| 86 | +
|
| 87 | + Args: |
| 88 | + genre: command type (Command) |
| 89 | + *args: other data. |
| 90 | + It is converted to octal by default. |
| 91 | + If the data needs to be encapsulated into hexadecimal, |
| 92 | + the array is used to include them. (Data cannot be nested) |
| 93 | + **kwargs: support `has_reply` |
| 94 | + has_reply: Whether there is a return value to accept. |
| 95 | + """ |
| 96 | + real_command, has_reply = super( |
| 97 | + MyPalletizerSocket, self)._mesg(genre, *args, **kwargs) |
| 98 | + # [254,...,255] |
| 99 | + data = self._write(self._flatten(real_command), "socket") |
| 100 | + if data: |
| 101 | + res = self._process_received(data, genre) |
| 102 | + if genre in [ |
| 103 | + ProtocolCode.ROBOT_VERSION, |
| 104 | + ProtocolCode.IS_POWER_ON, |
| 105 | + ProtocolCode.IS_CONTROLLER_CONNECTED, |
| 106 | + ProtocolCode.IS_PAUSED, |
| 107 | + ProtocolCode.IS_IN_POSITION, |
| 108 | + ProtocolCode.IS_MOVING, |
| 109 | + ProtocolCode.IS_SERVO_ENABLE, |
| 110 | + ProtocolCode.IS_ALL_SERVO_ENABLE, |
| 111 | + ProtocolCode.GET_SERVO_DATA, |
| 112 | + ProtocolCode.GET_DIGITAL_INPUT, |
| 113 | + ProtocolCode.GET_GRIPPER_VALUE, |
| 114 | + ProtocolCode.IS_GRIPPER_MOVING, |
| 115 | + ProtocolCode.GET_SPEED, |
| 116 | + ProtocolCode.GET_ENCODER, |
| 117 | + ProtocolCode.GET_BASIC_INPUT, |
| 118 | + ProtocolCode.GET_TOF_DISTANCE |
| 119 | + ]: |
| 120 | + return self._process_single(res) |
| 121 | + elif genre in [ProtocolCode.GET_ANGLES]: |
| 122 | + return [self._int2angle(angle) for angle in res] |
| 123 | + elif genre in [ProtocolCode.GET_COORDS]: |
| 124 | + if res: |
| 125 | + r = [] |
| 126 | + for idx in range(3): |
| 127 | + r.append(self._int2coord(res[idx])) |
| 128 | + if len(res)>3: |
| 129 | + r.append(self._int2angle(res[3])) |
| 130 | + return r |
| 131 | + else: |
| 132 | + return res |
| 133 | + elif genre in [ |
| 134 | + ProtocolCode.GET_JOINT_MIN_ANGLE, |
| 135 | + ProtocolCode.GET_JOINT_MAX_ANGLE, |
| 136 | + ]: |
| 137 | + return self._int2angle(res[0]) if res else 0 |
| 138 | + else: |
| 139 | + return res |
| 140 | + return None |
| 141 | + |
| 142 | + def get_radians(self): |
| 143 | + """Get all angle return a list |
| 144 | +
|
| 145 | + Return: |
| 146 | + data_list (list[radian...]): |
| 147 | + """ |
| 148 | + angles = self._mesg(ProtocolCode.GET_ANGLES, has_reply=True) |
| 149 | + return [round(angle * (math.pi / 180), 3) for angle in angles] |
| 150 | + |
| 151 | + def send_radians(self, radians, speed): |
| 152 | + """Send all angles |
| 153 | +
|
| 154 | + Args: |
| 155 | + radians (list): example [0, 0, 0, 0, 0, 0] |
| 156 | + speed (int): 0 ~ 100 |
| 157 | + """ |
| 158 | + calibration_parameters(len6=radians, speed=speed) |
| 159 | + degrees = [self._angle2int(radian * (180 / math.pi)) |
| 160 | + for radian in radians] |
| 161 | + return self._mesg(ProtocolCode.SEND_ANGLES, degrees, speed) |
| 162 | + |
| 163 | + def sync_send_angles(self, degrees, speed, timeout=7): |
| 164 | + t = time.time() |
| 165 | + self.send_angles(degrees, speed) |
| 166 | + while time.time() - t < timeout: |
| 167 | + f = self.is_in_position(degrees, 0) |
| 168 | + if f: |
| 169 | + break |
| 170 | + time.sleep(0.1) |
| 171 | + return self |
| 172 | + |
| 173 | + def sync_send_coords(self, coords, speed, mode, timeout=7): |
| 174 | + t = time.time() |
| 175 | + self.send_coords(coords, speed, mode) |
| 176 | + while time.time() - t < timeout: |
| 177 | + if self.is_in_position(coords, 1): |
| 178 | + break |
| 179 | + time.sleep(0.1) |
| 180 | + return self |
| 181 | + |
| 182 | + def set_gpio_mode(self, mode): |
| 183 | + """Set pin coding method |
| 184 | + Args: |
| 185 | + mode: (str) BCM or BOARD |
| 186 | + """ |
| 187 | + self.calibration_parameters(gpiomode=mode) |
| 188 | + if mode == "BCM": |
| 189 | + return self._mesg(ProtocolCode.SET_GPIO_MODE, 0) |
| 190 | + else: |
| 191 | + return self._mesg(ProtocolCode.SET_GPIO_MODE, 1) |
| 192 | + |
| 193 | + def set_gpio_out(self, pin_no, mode): |
| 194 | + """Set the pin as input or output |
| 195 | + Args: |
| 196 | + pin_no: (int) pin id |
| 197 | + mode: (str) "in" or "out" |
| 198 | + """ |
| 199 | + if mode == "in": |
| 200 | + return self._mesg(ProtocolCode.SET_GPIO_UP, pin_no, 0) |
| 201 | + else: |
| 202 | + return self._mesg(ProtocolCode.SET_GPIO_UP, pin_no, 1) |
| 203 | + |
| 204 | + def set_gpio_output(self, pin_no, state): |
| 205 | + """Set the pin to high or low level |
| 206 | + Args: |
| 207 | + pin_no: (int) pin id. |
| 208 | + state: (int) 0 or 1 |
| 209 | + """ |
| 210 | + return self._mesg(ProtocolCode.SET_GPIO_OUTPUT, pin_no, state) |
| 211 | + |
| 212 | + def get_gpio_in(self, pin_no): |
| 213 | + """Get pin level status. |
| 214 | + Args: |
| 215 | + pin_no: (int) pin id. |
| 216 | + """ |
| 217 | + return self._mesg(ProtocolCode.GET_GPIO_IN, pin_no) |
| 218 | + |
| 219 | + # Other |
| 220 | + def wait(self, t): |
| 221 | + time.sleep(t) |
| 222 | + return self |
0 commit comments