Skip to content

Commit 43f9a59

Browse files
committed
add mybuddy socket and bluetooth
1 parent 42fb1bd commit 43f9a59

File tree

5 files changed

+374
-9
lines changed

5 files changed

+374
-9
lines changed

pymycobot/bluet.py

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
# coding: utf-8
2+
import bluetooth
3+
import sys
4+
5+
class BluetoothConnection:
6+
def __init__(self):
7+
self.device = []
8+
self.target_name = "mybuddy"
9+
self.nearby_devices = None
10+
11+
def find_target_device(self):
12+
available_addr = []
13+
self.nearby_devices = bluetooth.discover_devices(lookup_names=True, duration=5)
14+
if self.nearby_devices:
15+
for addr, name in self.nearby_devices:
16+
if self.target_name == name:
17+
available_addr.append(addr)
18+
return available_addr
19+
return None
20+
21+
def connect_target_device(self):
22+
target_address = self.find_target_device()
23+
if target_address:
24+
if len(target_address) > 1:
25+
device_info = ""
26+
i = 1
27+
sys.stdout.write("please select the device you want to connect:\n".format(4))
28+
for addr, name in target_address:
29+
device_info += "{} >>> {} - {} \n".format(i,addr, name)
30+
sys.stdout.write(device_info)
31+
choose_device = input("please enter 1-{}:".format(len(target_address)))
32+
target_address = target_address[int(choose_device)-1][0]
33+
sock = bluetooth.BluetoothSocket(bluetooth.RFCOMM)
34+
try:
35+
sock.connect((target_address[0][0], 1))
36+
return sock
37+
except Exception as e:
38+
# print("connection fail\n", e)
39+
sock.close()
40+
return None
41+
return None
42+
43+
if __name__ == "__main__":
44+
pass

pymycobot/generate.py

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -214,6 +214,7 @@ def send_angle(self, id, degree, speed):
214214
id : Joint id(genre.Angle)\n
215215
For mycobot: int 1-6.\n
216216
For mypalletizer: int 1-4.
217+
For mypalletizer 340: int 1-3.
217218
degree : degree value(float)(about -170 ~ 170).
218219
speed : (int) 0 ~ 100
219220
"""
@@ -228,6 +229,7 @@ def send_angles(self, degrees, speed):
228229
degrees: a list of degree values(List[float]), length 6 or 4.\n
229230
for mycobot: [0.0, 0.0, 0.0, 0.0, 0.0, 0.0].\n
230231
for mypalletizer: [0.0, 0.0, 0.0, 0.0]
232+
for mypalletizer 340: [0.0, 0.0, 0.0]
231233
speed : (int) 0 ~ 100
232234
"""
233235
self.calibration_parameters(degrees=degrees, speed=speed)
@@ -241,6 +243,7 @@ def get_coords(self):
241243
list : A float list of coord
242244
for mycobot: [x, y, z, rx, ry, rz]\n
243245
for mypalletizer: [x, y, z, θ]
246+
for mypalletizer 340: [x, y, z]
244247
"""
245248
return self._mesg(ProtocolCode.GET_COORDS, has_reply=True)
246249

@@ -251,24 +254,26 @@ def send_coord(self, id, coord, speed):
251254
id(int) : coord id(genre.Coord)\n
252255
For mycobot: int 1-6.\n
253256
For mypalletizer: int 1-4.
257+
For mypalletizer 340: int 1-3.
254258
coord(float) : coord value, mm
255259
speed(int) : 0 ~ 100
256260
"""
257-
self.calibration_parameters(id=id, speed=speed)
261+
# self.calibration_parameters(id=id, speed=speed)
258262
value = self._coord2int(coord) if id <= 3 else self._angle2int(coord)
259263
return self._mesg(ProtocolCode.SEND_COORD, id, [value], speed)
260264

261265
def send_coords(self, coords, speed, mode):
262266
"""Send all coords to robot arm.
263267
264268
Args:
265-
coords: a list of coords value(List[float]), length 6 or 4.
269+
coords: a list of coords value(List[float]).
266270
for mycobot :[x(mm), y, z, rx(angle), ry, rz]\n
267271
for mypalletizer: [x, y, z, θ]
272+
for mypalletizer 340: [x, y, z]
268273
speed : (int) 0 ~ 100
269274
mode : (int) 0 - angluar, 1 - linear
270275
"""
271-
self.calibration_parameters(coords=coords, speed=speed)
276+
# self.calibration_parameters(coords=coords, speed=speed)
272277
coord_list = []
273278
for idx in range(3):
274279
coord_list.append(self._coord2int(coords[idx]))
@@ -283,6 +288,7 @@ def is_in_position(self, data, id=0):
283288
data: A data list, angles or coords.
284289
for mycobot: len 6.
285290
for mypalletizer: len 4
291+
for mypalletizer 340: len 3
286292
id: 1 - coords, 0 - angles
287293
288294
Return:
@@ -291,14 +297,14 @@ def is_in_position(self, data, id=0):
291297
-1 - Error
292298
"""
293299
if id == 1:
294-
self.calibration_parameters(coords=data)
300+
# self.calibration_parameters(coords=data)
295301
data_list = []
296302
for idx in range(3):
297303
data_list.append(self._coord2int(data[idx]))
298304
for idx in range(3, 6):
299305
data_list.append(self._angle2int(data[idx]))
300306
elif id == 0:
301-
self.calibration_parameters(degrees=data)
307+
# self.calibration_parameters(degrees=data)
302308
data_list = [self._angle2int(i) for i in data]
303309
else:
304310
raise Exception("id is not right, please input 0 or 1")
@@ -323,6 +329,7 @@ def jog_angle(self, joint_id, direction, speed):
323329
joint_id:
324330
For mycobot: int 1-6.\n
325331
For mypalletizer: int 1-4.
332+
For mypalletizer 340: int 1-3.
326333
direction: 0 - decrease, 1 - increase
327334
speed: int (0 - 100)
328335
"""
@@ -335,6 +342,7 @@ def jog_coord(self, coord_id, direction, speed):
335342
coord_id:
336343
For mycobot: int 1-6.\n
337344
For mypalletizer: int 1-4.
345+
For mypalletizer 340: int 1-3.
338346
direction: 0 - decrease, 1 - increase
339347
speed: int (0 - 100)
340348
"""
@@ -395,6 +403,7 @@ def set_encoder(self, joint_id, encoder):
395403
joint_id:
396404
for mycobot: Joint id 1 - 6
397405
for mypalletizer: Joint id 1 - 4
406+
for mypalletizer 340: Joint id 1 - 3
398407
for mycobot gripper: Joint id 7
399408
encoder: The value of the set encoder.
400409
"""
@@ -564,6 +573,7 @@ def release_servo(self, servo_id):
564573
servo_id:
565574
for mycobot: 1 - 6.\n
566575
for mypalletizer: 1 - 4
576+
for mypalletizer 340: 1 - 3
567577
"""
568578
return self._mesg(ProtocolCode.RELEASE_SERVO, servo_id)
569579

@@ -574,6 +584,7 @@ def focus_servo(self, servo_id):
574584
servo_id:
575585
for mycobot: 1 - 6\n
576586
for mypalletizer: 1 - 4
587+
for mypalletizer 340: 1 - 3
577588
"""
578589
return self._mesg(ProtocolCode.FOCUS_SERVO, servo_id)
579590

pymycobot/mybuddybluetooth.py

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
# coding: utf-8
2+
import bluetooth
3+
4+
from pymycobot.Interface import MyBuddyCommandGenerator
5+
from pymycobot.common import ProtocolCode, write, read
6+
from pymycobot.error import calibration_parameters
7+
from pymycobot.bluet import BluetoothConnection
8+
9+
10+
class MyBuddyBlueTooth(MyBuddyCommandGenerator):
11+
"""MyBuddy bluetooth API"""
12+
def __init__(self):
13+
"""There is a default Bluetooth search time of 5 seconds"""
14+
super(MyBuddyBlueTooth).__init__()
15+
self.ble = BluetoothConnection()
16+
self.ble_obj = self.ble.connect_target_device()
17+
18+
def __read(self):
19+
while True:
20+
data = self.ble_obj.recv(1024)
21+
if data:
22+
return data
23+
24+
25+
def _mesg(self, genre, *args, **kwargs):
26+
"""
27+
28+
Args:
29+
genre: command type (Command)
30+
*args: other data.
31+
It is converted to octal by default.
32+
If the data needs to be encapsulated into hexadecimal,
33+
the array is used to include them. (Data cannot be nested)
34+
**kwargs: support `has_reply`
35+
has_reply: Whether there is a return value to accept.
36+
"""
37+
real_command, has_reply = super(
38+
MyBuddyBlueTooth, self)._mesg(genre, *args, **kwargs)
39+
self.ble_obj.write(bytes(self._flatten(real_command)))
40+
41+
if has_reply:
42+
data = self.__read()
43+
res = self._process_received(data, genre)
44+
if genre in [
45+
ProtocolCode.ROBOT_VERSION,
46+
ProtocolCode.IS_POWER_ON,
47+
ProtocolCode.IS_CONTROLLER_CONNECTED,
48+
ProtocolCode.IS_PAUSED, # TODO have bug: return b''
49+
ProtocolCode.IS_IN_POSITION,
50+
ProtocolCode.IS_MOVING,
51+
ProtocolCode.IS_SERVO_ENABLE,
52+
ProtocolCode.IS_ALL_SERVO_ENABLE,
53+
ProtocolCode.GET_SERVO_DATA,
54+
ProtocolCode.GET_DIGITAL_INPUT,
55+
ProtocolCode.GET_GRIPPER_VALUE,
56+
ProtocolCode.IS_GRIPPER_MOVING,
57+
ProtocolCode.GET_SPEED,
58+
ProtocolCode.GET_ENCODER,
59+
ProtocolCode.GET_BASIC_INPUT,
60+
ProtocolCode.GET_TOF_DISTANCE,
61+
ProtocolCode.GET_END_TYPE,
62+
ProtocolCode.GET_MOVEMENT_TYPE,
63+
ProtocolCode.GET_REFERENCE_FRAME,
64+
ProtocolCode.GET_JOINT_MIN_ANGLE,
65+
ProtocolCode.GET_JOINT_MAX_ANGLE
66+
]:
67+
return self._process_single(res)
68+
elif genre in [ProtocolCode.GET_ANGLES]:
69+
return [self._int2angle(angle) for angle in res]
70+
elif genre in [ProtocolCode.GET_COORDS, ProtocolCode.GET_TOOL_REFERENCE, ProtocolCode.GET_WORLD_REFERENCE]:
71+
if res:
72+
r = []
73+
for idx in range(3):
74+
r.append(self._int2coord(res[idx]))
75+
for idx in range(3, 6):
76+
r.append(self._int2angle(res[idx]))
77+
return r
78+
else:
79+
return res
80+
elif genre in [ProtocolCode.GET_SERVO_VOLTAGES]:
81+
return [self._int2coord(angle) for angle in res]
82+
else:
83+
return res
84+
return None
85+
86+

0 commit comments

Comments
 (0)