forked from AnykeyNL/uArmProPython
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathuArmRobot.py
187 lines (139 loc) · 5.4 KB
/
uArmRobot.py
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
# uArm Swift Pro - Python Library
# Created by: Richard Garsthagen - [email protected]
# V0.2 - June 2017 - Still under development
import serial
import time
import protocol_swiftpro as protocol
import threading
import sys
import math
from math import pi
class robot:
serid = 100
num_of_robots = 0
baud = 115200
serial_timeout = 1
connect_timeout = 1
debug = False
baseThreadCount = threading.activeCount()
delay_after_move = 0.1
def __init__(self, serialport):
self.serialport = serialport
self.connected = False
robot.num_of_robots += 1
self.moving = False
self.pumping = False
def connect(self):
try:
if (self.debug): print ("trying to connect to: " + self.serialport)
self.ser = serial.Serial(self.serialport, 115200, timeout=1)
time.sleep(self.connect_timeout)
Ready = False
while (not Ready):
line = self.ser.readline()
if (self.debug): print (line)
if str(line)[2:4] == '@5':
Ready = True
self.connected = True
if (self.debug): print ("Connected!")
return True
line = self.ser.readline() # Ignore if @6 response is given
print (line)
except Exception as e:
if (self.debug): print ("Error trying to connect to: " + self.serialport + " - " + str(e))
self.connected = False
return False
def disconnect(self):
if self.connected:
if (self.debug): print ("Closing serial connection")
self.connected = False
self.ser.close()
else:
if (self.debug): print ("Disconnected called while not connected")
def sendcmd(self, cmnd, waitresponse):
if (self.connected):
id = self.serid
self.serid += 1
cmnd = "#{} {}".format(id,cmnd)
cmndString = bytes(cmnd + "\n",'ascii')
if (self.debug): print ("Serial send: {}".format(cmndString))
self.ser.write(cmndString)
if (waitresponse):
line = self.ser.readline()
while not str(line)[2:3+len(str(id))] == "$" + str(id):
line = self.ser.readline()
if (self.debug): print ("Response {}".format(line))
if (self.moving):
self.moving = False
time.sleep(self.delay_after_move)
return line
else:
if (self.debug):
print ("error, trying to send command while not connected")
self.moving = False
def goto(self,x,y,z,speed):
self.moving = True
x = str(round(x, 2))
y = str(round(y, 2))
z = str(round(z, 2))
s = str(round(speed, 2))
cmd = protocol.SET_POSITION.format(x,y,z,s)
self.sendcmd(cmd, True)
def laser_goto(self,x,y,z,speed):
self.moving = True
x = str(round(x, 2))
y = str(round(y, 2))
z = str(round(z, 2))
s = str(round(speed, 2))
cmd = protocol.SET_POSITION_LASER.format(x,y,z,s)
self.sendcmd(cmd, True)
cmd = protocol.SET_POSITION.format(x,y,z,s)
self.sendcmd(cmd, True)
def get_position(self):
cmd = protocol.GET_COOR
line = self.sendcmd(cmd, True)
x = float(str(line).split(' ')[2][1:])
y = float(str(line).split(' ')[3][1:])
z = float(str(line).split(' ')[4][1:-5])
return [x,y,z]
def async_goto(self,x,y,z, speed):
self.moving = True
t = threading.Thread( target=self.goto , args=(x,y,z,speed) )
t.start()
def pump(self, state):
self.pumping = state
cmd = protocol.SET_PUMP.format(int(state))
self.sendcmd(cmd,True)
def mode(self, modeid):
# 0= Normal
# 1= Laser
# 2= 3D Printer
# 3= Universal holder
cmd = protocol.SET_MODE.format(modeid)
self.sendcmd(cmd,True)
def set_acceleration(self,p,t):
self.moving = True
p = str(round(p, 0))
t = str(round(t, 0))
cmd = protocol.SET_ACCELERATION.format(p,t)
self.sendcmd(cmd, True)
@staticmethod
def PointsInCircum(r,n):
return [(math.cos(2*pi/n*x)*r,math.sin(2*pi/n*x)*r) for x in xrange(0,n+1)]
def drawCircle(self, centerX, centerY, Radius, Resolution, Speed, DrawingHeight, StartFinishedHeight):
if (Resolution < 4):
#ignore drwaing circle, to low resoution
if (self.debug): print ("Ignoring drwaing circle, to low resolution requested")
return
if (self.debug): print ("Drwaing circle of {} radius in {} steps".format(Radius,Resolution))
offsetx = centerX
offsety = centerY
c = self.PointsInCircum(Radius,Resolution)
bx,by = c[0]
self.goto(offsetx+bx,offsety+by,StartFinishedHeight,Speed)
for p in range(0,Resolution):
x,y = c[p]
self.goto(offsetx+x,offsety+y,DrawingHeight,Speed)
self.goto(offsetx+bx,offsety+by,DrawingHeight,Speed)
time.sleep(0.5)
self.goto(offsetx+bx,offsety+by,StartFinishedHeight,Speed)