-
Notifications
You must be signed in to change notification settings - Fork 1
/
Big_Test.py
317 lines (272 loc) · 11.1 KB
/
Big_Test.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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
'''
This class creates an object for each face of Yearling.
Authors: Antony Macar, Michael Pham
Updated June 3, 2022
v1
'''
'''from cgitb import handler'''
import time
import board
import busio
import adafruit_apds9960.apds9960
from adafruit_apds9960.apds9960 import APDS9960
import adafruit_mcp9808
import adafruit_tca9548a
import mf_initialize
import adafruit_mpu6050
import adafruit_lis3mdl
import adafruit_lis3mdl
import adafruit_drv2605
import adafruit_mcp9600
import digitalio
from pycubed import cubesat
#I think this is extraneous, put it back if I am wrong
# Create I2C bus as normal
i2c = busio.I2C(board.SCL2, board.SDA2)
# Create the TCA9548A object and give it the I2C bus
tca = adafruit_tca9548a.TCA9548A(i2c)
class Face:
def __init__(self, Add, Pos):
self.address = Add
self.position = Pos
#Sensor List Contains Expected Sensors Based on Face
self.senlist = []
if Pos == "x+":
self.senlist.append("MCP")
self.senlist.append("ADPS")
self.senlist.append("DRV")
elif Pos == "x-":
self.senlist.append("MCP")
self.senlist.append("ADPS")
self.senlist.append("DRV")
elif Pos == "y+":
self.senlist.append("MCP")
self.senlist.append("ADPS")
self.senlist.append("MPU IMU") #Note difference between IMUs
elif Pos == "y-":
self.senlist.append("MCP")
self.senlist.append("ADPS")
self.senlist.append("IMU")
elif Pos == "z+":
self.senlist.append("MAG")
self.senlist.append("MCP")
self.senlist.append("ADPS")
self.senlist.append("DRV")
self.senlist.append("COUPLE")
elif Pos == "z-":
self.senlist.append("MCP")
self.senlist.append("ADPS")
else:
print("[ERROR] Please input a propper face")
#This sensors set contains information as to whether sensors are actually working
self.sensors = {
'MCP': False,
'ADPS': False,
'DRV': False,
'IMU': False,
'MPU IMU': False,
'BNO IMU': False,
'LSX IMU': False,
'MAG': False,
'COUPLE': False,
}
# function to initialize all the sensors on that face
def Sensorinit(self,senlist,address):
#Initialize Temperature Sensor
if "MCP" in senlist:
try:
self.mcp = adafruit_mcp9808.MCP9808(tca[address])
self.sensors['MCP'] = True
print('[ACTIVE][Temperature Sensor]')
except Exception as e:
print('[ERROR][Temperature Sensor]',e)
#Initialize Light Sensor
if "ADPS" in senlist:
try:
self.light1 = adafruit_apds9960.apds9960.APDS9960(tca[address])
self.light1.enable_color =True
self.light1.enable_proximity = True
self.sensors['ADPS'] = True
print('[ACTIVE][Light Sensor]')
except Exception as e:
print('[ERROR][Light Sensor]',e)
#Initialize Motor Driver
if "DRV" in senlist:
try:
self.drv1 = adafruit_drv2605.DRV2605(tca[address])
self.sensors['DRV'] = True
print('[ACTIVE][Motor Driver]')
except Exception as e:
print('[ERROR][Motor Driver]',e)
#Initialize MPU IMU
if "MPU IMU" in senlist:
try:
self.mpu1 = adafruit_mpu6050.MPU6050(tca[address])
self.sensors['MPU IMU'] = True
print('[ACTIVE][MPU IMU]')
except Exception as e:
print('[ERROR][MPU IMU]',e)
#Initialize Magnetometer
if "MAG" in senlist:
try:
self.mag1 = adafruit_lis3mdl.LIS3MDL(tca[address])
self.sensors['MAG'] = True
print('[ACTIVE][Magnetometer]')
except Exception as e:
print('[ERROR][Magnetometer]',e)
#Initialize Thermocouple
if "COUPLE" in senlist:
try:
self.couple1 = adafruit_mcp9600.MCP9600(tca[address])
self.sensors['COUPLE'] = True
print('[ACTIVE][Thermocouple]')
except Exception as e:
print('[ERROR][Thermocouple]',e)
print('Initialization Complete')
#Meta Info Getters
@property #Gives what sensors should be present
def senlist_what(self):
return self.senlist
@property #Givens what sensors are actually present
def active_sensors(self):
return self.sensors
#Sensor Data Getters
@property #Temperature Data Getter
def temperature(self):
if self.sensors['MCP']:
return self.mcp.temperature
else:
print('[WARNING]Temperature sensor not initialized')
@property #Light Sensor Color Data Getter
def color_data(self):
if self.sensors['ADPS']:
r1, g1, b1, c1 = self.light1.color_data
return r1, g1, b1, c1
else:
print('[WARNING]Light sensor not initialized')
#BEWARE: I think none of this motor driver stuff works
#https://learn.adafruit.com/adafruit-drv2605-haptic-controller-breakout/python-circuitpython
#
#
@property #Motor Driver Getter
def drv_actuate(self, duration):
if self.sensors['DRV']:
print('Actuating Sequence')
self.drv1.play()
time.sleep(duration)
self.drv1.stop()
print('Actuation Complete')
else:
print('[WARNING]Motor driver not initialized')
@drv_actuate.setter #I don't know if this is how this actually works
def drv_actuate(self, sequence):
if self.sensors['DRV']:
print('Encoding Sequence')
self.drv1.sequence[0] = adafruit_drv2605.Effect(1)
print('Complete')
else:
print('[WARNING]Motor driver not initialized')
#
#
#
#BEWARE: NOT WORKING MOTOR DRIVER CODE
@property #MPU IMU Getter
def mpu_data(self):
if self.sensors['MPU IMU']:
acel = self.mpu1.acceleration
gyro = self.mpu1.gyro
temp = self.mpu1.temperature
return acel,gyro,temp #Note return is a tuple
else:
print('[WARNING]MPU IMU not initialized')
@property #Magnetometer Getter
def mag(self):
if self.sensors['MAG']:
mag_x, mag_y, mag_z = self.mag1.magnetic
return mag_x, mag_y, mag_z#Note return is a tuple
else:
print('[WARNING]Magnetometer not initialized')
@property #Thermocouple Getter
def couple(self):
if self.sensors['COUPLE']:
amb = self.couple1.ambient_temperature
tip = self.couple1.temperature
dif = self.couple1.delta_temperature
return amb, tip, dif #Note return is a tuple
else:
print('[WARNING]Thermocouple not initialized')
#Function to test all sensors that should be on each face.
#Function takes number of tests "num" and polling rate in hz "rate"
def test_all(self, num, rate):
print('Expected Sensors: ', self.senlist_what)
print('Initialized Sensors: ', self.active_sensors)
time.sleep(1) #Remove later for performance boost!
print('Initializing Test')
for i in range(num):
print('Test Number: ', i, ' /', num)
#Test Temperature Sensor
print('~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~')
if ("MCP" in self.senlist) and (self.sensors.get("MCP") == True):
try:
print('Temperature Sensor')
print('Face Temperature: ', self.temperature)
except Exception as e:
print('[ERROR][Temperature Sensor]',e)
else:
print('[ERROR]Temperature Sensor Failure')
print('~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~')
#Initialize Light Sensor
if ("ADPS" in self.senlist) and (self.sensors.get('ADPS') == True ):
try:
print('Light Sensor')
print(self.color_data)
except Exception as e:
print('[ERROR][Light Sensor]',e)
else:
print('[ERROR]Light Sensor Failure')
print('~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~')
#Initialize Motor Driver
if ("DRV" in self.senlist) and (self.sensors.get('DRV') == True ):
try:
print('Motor Driver')
print('[ACTIVE][Motor Driver]') #No function defined here yet to use the driver
except Exception as e:
print('[ERROR][Motor Driver]',e)
print('~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~')
#Initialize MPU IMU
if ("MPU IMU" in self.senlist) and (self.sensors.get('MPU IMU') == True ):
try:
print('MPU IMU')
print(self.mpu_data)
except Exception as e:
print('[ERROR][M=PU IMU]',e)
elif self.position == "x+" or self.position == "x-":
print()
else:
print('[Error]MPU IMU Failure')
print('~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~')
#Initialize Magnetometer
if ("MAG" in self.senlist) and (self.sensors.get('MAG') == True ):
try:
print('Magnetometer')
print('X:{0:10.2f}, Y:{1:10.2f}, Z:{2:10.2f} uT'.format(self.mag))
except Exception as e:
print('[ERROR][Magnetometer]',e)
elif self.position == "x+" or self.position== "x-" or self.position == "y+" or self.position == "y-":
print()
else:
print('[Error]Magnetometer Failure')
print('~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~')
#Initialize Thermocouple
if ("COUPLE" in self.senlist) and (self.sensors.get('COUPLE') == True ):
try:
print('Thermocouple')
print(self.couple) #Unformatted
except Exception as e:
print('[ERROR][Thermocouple]',e)
elif self.position == "x+" or self.position== "x-" or self.position == "y+" or self.position == "y-":
print()
else:
print('[ERROR]Thermocouple Failure')
print('~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~')
time.sleep(rate) #Remove later for performance boost!