-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcontrol.py
57 lines (57 loc) · 1.92 KB
/
control.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
import serial
steps_per_rev = 1440
arduino = ''
def init(port):
global arduino
arduino = serial.Serial(port,9600)
def bits_to_byte(bits):
byte = 0
if type(bits) != list:
raise TypeError("type must be list")
if len(bits) != 8:
raise ValueError("you must input 8 bits, no more or less")
for i in range(0,8):
if bits[i] != 1 and bits[i] != 0:
raise TypeError("bits can only be 0 or 1")
byte += bits[i] << i
return bytes([byte])
def send_command(command, data1,data2,data3,data4,data5,data6): # example command [1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]
command1 = 0
command2 = 0
#print(command)
command1 = bits_to_byte(command[0:8])
command2 = bits_to_byte(command[8:])
#print(command1,command2)
data_and_command = command1 + command2 + bytes([data1]) + bytes([data2]) + bytes([data3]) + bytes([data4]) + bytes([data5]) + bytes([data6])
#print(data_and_command)
arduino.write(data_and_command)
return arduino.read()
def step_with_home(ang):
print(ang)
steps = (ang/360)*steps_per_rev
print(steps)
steps = int(steps)
print(steps)
data1 = (steps & 65280) >> 8
data2 = (steps & 255) >> 0
print(data1,data2)
send_command([1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],data1,data2,0,0,0,0)
def step_without_home(ang):
print(ang)
steps = 1+((ang/360)*steps_per_rev)
steps = int(steps)
#print(steps)
data1 = (steps & 65280) >> 8
data2 = (steps & 255) >> 0
#print(data1,data2)
send_command([0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0],data1,data2,0,0,0,0)
def home():
print("HOME")
send_command([0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0],0,0,0,0,0,0)
def servo_goto(ang):
ang = ang + 90
send_command([0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0],0,ang,0,0,0,0)
def set_laser(state):
send_command([0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0],0,state,0,0,0,0)
def ping():
return send_command([0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],0,0,0,0,0,0)