- Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsamplepy.py
More file actions
Latest commit
163 lines (117 loc) · 4.25 KB
/
Copy pathsamplepy.py
File metadata and controls
163 lines (117 loc) · 4.25 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
print"Hello, World!"
# uArm Swift Pro - Python Library
# Created by: Richard Garsthagen - the.anykey@gmail.com
# V0.2 - June 2017 - Still under development
importserial
importtime
importprotocol_swiftproasprotocol
importthreading
importsys
importmath
frommathimportpi
classrobot:
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
defconnect(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 (notReady):
line=self.ser.readline()
if (self.debug): print (line)
ifline.startswith("@5"):
Ready=True
self.connected=True
if (self.debug): print ("Connected!")
returnTrue
line=self.ser.readline() # Ignore if @6 response is given
print (line)
exceptExceptionase:
if (self.debug): print ("Error trying to connect to: "+self.serialport+" - "+str(e))
self.connected=False
returnFalse
defdisconnect(self):
ifself.connected:
if (self.debug): print ("Closing serial connection")
self.connected=False
self.ser.close()
else:
if (self.debug): print ("Disconnected called while not connected")
defsendcmd(self, cmnd, waitresponse):
if (self.connected):
id=self.serid
self.serid+=1
cmnd="#{} {}".format(id,cmnd)
cmndString=bytes(cmnd+"\n")
if (self.debug): print ("Serial send: {}".format(cmndString))
self.ser.write(cmndString)
if (waitresponse):
line=self.ser.readline()
whilenotline.startswith("$"+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)
returnline
else:
if (self.debug):
print ("error, trying to send command while not connected")
self.moving=False
defgoto(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)
defasync_goto(self,x,y,z, speed):
self.moving=True
t=threading.Thread( target=self.goto , args=(x,y,z,speed) )
t.start()
defpump(self, state):
self.pumping=state
cmd=protocol.SET_PUMP.format(int(state))
self.sendcmd(cmd,True)
defmode(self, modeid):
# 0= Normal
# 1= Laser
# 2= 3D Printer
# 3= Universal holder
cmd=protocol.SET_MODE.format(modeid)
self.sendcmd(cmd,True)
@staticmethod
defPointsInCircum(r,n):
return [(math.cos(2*pi/n*x)*r,math.sin(2*pi/n*x)*r) forxinxrange(0,n+1)]
defdrawCircle(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)
forpinrange(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)