-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathMonitorControl.py
More file actions
140 lines (128 loc) · 4.37 KB
/
Copy pathMonitorControl.py
File metadata and controls
140 lines (128 loc) · 4.37 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
137
138
139
140
from OnshapePlus import *
## Initialize connection to Spike
port = serial_ports()
def serial_write(string):
ser.write(string + b'\r\n')
time.sleep(0.1)
while ser.in_waiting:
# print(ser.read(ser.in_waiting).decode())
response = ser.read(ser.in_waiting).decode()
result = []
try:
for s in response.split():
num = ''
for x in s:
# print(x)
if x.isdigit() or x == "-":
num += x
if len(num) > 0:
try:
result.append(int(num))
except:
pass
if result == []:
print(response)
except:
pass
return result
try:
ser = serial.Serial(
port=port,
baudrate=115200,
parity=serial.PARITY_NONE,
stopbits=serial.STOPBITS_ONE,
bytesize=serial.EIGHTBITS
)
print("Port is open")
except:
serial.Serial(
port=port,
baudrate=115200,
parity=serial.PARITY_NONE,
stopbits=serial.STOPBITS_ONE,
bytesize=serial.EIGHTBITS
).close()
print("Port is closed")
ser = serial.Serial(
port=port,
baudrate=115200,
parity=serial.PARITY_NONE,
stopbits=serial.STOPBITS_ONE,
bytesize=serial.EIGHTBITS
)
print("Port is open again")
print(ser.isOpen())
code = '''while True:
pin0.write_analog(75)
sleep(1000)
pin0.write_analog(50)
sleep(1000)
pin0.write_analog(100)
sleep(1000)'''
ser.write(b'\x03')
time.sleep(1)
serial_write(b'from microbit import *')
serial_write(b'display.show(Image.HAPPY)')
##
##
## Config Client
try:
try:
try:
exec(open('../apikeys.py').read())
base = 'https://cad.onshape.com'
client = Client(configuration={"base_url": base,
"access_key": access,
"secret_key": secret})
print('client configured')
except:
exec(open('../../apikeys.py').read())
base = 'https://cad.onshape.com'
client = Client(configuration={"base_url": base,
"access_key": access,
"secret_key": secret})
print('client configured')
except:
exec(open('apikeys.py').read())
base = 'https://cad.onshape.com'
client = Client(configuration={"base_url": base,
"access_key": access,
"secret_key": secret})
print('client configured')
except:
print('Api keys file not found. Input keys manually.')
access = input('Please enter your access key: ')
secret = input('Please enter your secret key: ')
base = 'https://cad.onshape.com'
client = Client(configuration={"base_url": base,
"access_key": access,
"secret_key": secret})
print('client configured')
# print()
url = str(input('What is the url of your Onshape assembly? (paste URL then press enter twice): '))
## Bug - url input does not continue after copy paste. placeholder fix for now
# placeholder = input()
print(serial_write(b'accelerometer.get_x()'))
while True:
mates = getMates(client,url,base)
try:
monitorValue = serial_write(b'accelerometer.get_x()')
print('accelerometer value: ' + str(monitorValue[0]))
for names in mates['mateValues']:
if names['mateName'] == 'Monitor':
setMateJSON = names
if names['jsonType'] == "Revolute":
setMateJSON['rotationZ'] = translate(monitorValue[0],-1024,1024,0,2*math.pi)
elif names['jsonType'] == "Slider":
setMateJSON['translationZ'] = translate(monitorValue[0],-1024,1024,0,1)
setMates(client,url,base,{'mateValues':[setMateJSON]})
for names in mates['mateValues']:
if names['mateName'] == 'Control':
if names['jsonType'] == "Revolute":
val = str(math.floor(translate(names['rotationZ'],-math.pi,math.pi,0,9)))
elif names['jsonType'] == "Slider":
val = str(math.floor(translate(names['translationZ'],0,1,0,9)))
string = 'display.show('+str(val)+')'
serial_write(string.encode())
except:
pass