Skip to content

Commit 8c72f81

Browse files
RobertLucianCleoQc
authored andcommitted
bring develop to master for DexterOS 2.0 (#44)
* Add Line Follower drivers and example * Update dexter_i2c.py Update dexter_i2c.py to be simpler and more universal by combining functions. * Update line_follower.py Update line_follower.py for recent dexter_i2c.py update * Feature/easy libraries (#35) * Move Distance Sensor and Line Follower in here * remove dependency on mock_package * remove alias for read_sensors until we know if we need it (probably not) * Feature/system wide mutex+ easydi_sensors library (#36) * Move Distance Sensor and Line Follower in here * remove dependency on mock_package * remove alias for read_sensors until we know if we need it (probably not) * get easydi_sensors from DexterOS * Check for overall mutex need * Add systemwide mutex comment * Add license header and description * Add license header * change year in license header * split easydi_sensors into 3 components for systematic naming convention * Remove copies of ifMutexAcquire and ifMutexRelease. Keep them in one file * Be more strict in what we're pulling in from easy_mutex * poll overall_mutex each time * remove print statement (#37) reconfig_bus() is now mutex protected * feature - update documentation for the latest (#41) * feature - configuring the environment for RTD * feature - bulk changes to docs * feature - add comment on how to build documentation * feature - add part of the documentation * feature - completing the documentation for easy sensors + tutorials * feature - work on the tutorials + other stuff * feature - fix the mutexes tutorial * feature - change organization of documentation * feature - remove an unnecessary section * feature - show the right command for installing di-sensors * feature - small changes * feature - fix documentation & and missing parts * feature - fix naming of the package * Revert commit * minor fixes to docs (#42) * minor fixes to docs * minor fixes to doc * fix grammar * rename safe_heading to heading_name and re-instate the parameter (#43) * rename safe_heading to heading_name and re-instate the parameter * rename from heading_name to convert_heading * add punctuation * fix - change name of method in library description section * Remove references to line follower (#45)
1 parent d6b6e69 commit 8c72f81

32 files changed

+1484
-246
lines changed

Python/Examples/EasyDistanceSensor.py

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
#!/usr/bin/env python
2+
#
3+
# https://www.dexterindustries.com
4+
#
5+
# Copyright (c) 2017 Dexter Industries
6+
# Released under the MIT license (http://choosealicense.com/licenses/mit/).
7+
# For more information see https://github.com/DexterInd/DI_Sensors/blob/master/LICENSE.md
8+
#
9+
# Python example program for the Dexter Industries Distance Sensor
10+
11+
from __future__ import print_function
12+
from __future__ import division
13+
14+
# import the modules
15+
from di_sensors.easy_distance_sensor import EasyDistanceSensor
16+
from time import sleep
17+
18+
# instantiate the distance object
19+
my_sensor = EasyDistanceSensor()
20+
21+
# and read the sensor iteratively
22+
while True:
23+
read_distance = my_sensor.read()
24+
print("distance from object: {} mm".format(read_distance))
25+
26+
sleep(0.1)
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
#!/usr/bin/env python
2+
#
3+
# https://www.dexterindustries.com
4+
#
5+
# Copyright (c) 2017 Dexter Industries
6+
# Released under the MIT license (http://choosealicense.com/licenses/mit/).
7+
# For more information see https://github.com/DexterInd/DI_Sensors/blob/master/LICENSE.md
8+
#
9+
# Python example program for the Dexter Industries Temperature Humidity Pressure Sensor
10+
11+
from __future__ import print_function
12+
from __future__ import division
13+
14+
# do the import stuff
15+
from di_sensors.easy_distance_sensor import EasyDistanceSensor
16+
from time import time, sleep
17+
from threading import Thread, Event, get_ident
18+
19+
# instantiate the distance object
20+
my_sensor = EasyDistanceSensor(use_mutex = True)
21+
start_time = time()
22+
runtime = 2.0
23+
# create an event object for triggering the "shutdown" of each thread
24+
stop_event = Event()
25+
26+
# target function for each thread
27+
def readingSensor():
28+
while not stop_event.is_set():
29+
thread_id = get_ident()
30+
distance = my_sensor.read()
31+
print("Thread ID = {} with distance value = {}".format(thread_id, distance))
32+
sleep(0.001)
33+
34+
# create an object for each thread
35+
thread1 = Thread(target = readingSensor)
36+
thread2 = Thread(target = readingSensor)
37+
38+
# and then start them
39+
thread1.start()
40+
thread2.start()
41+
42+
# let it run for [runtime] seconds
43+
while time() - start_time <= runtime:
44+
sleep(0.1)
45+
46+
# and then set the stop event variable
47+
stop_event.set()
48+
49+
# and wait both threads to end
50+
thread1.join()
51+
thread2.join()
Lines changed: 28 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,28 @@
1-
#!/usr/bin/env python
2-
#
3-
# https://www.dexterindustries.com
4-
#
5-
# Copyright (c) 2017 Dexter Industries
6-
# Released under the MIT license (http://choosealicense.com/licenses/mit/).
7-
# For more information see https://github.com/DexterInd/DI_Sensors/blob/master/LICENSE.md
8-
#
9-
# Python example program for the Dexter Industries Light Color Sensor
10-
11-
from __future__ import print_function
12-
from __future__ import division
13-
14-
import time
15-
from di_sensors.light_color_sensor import LightColorSensor
16-
17-
print("Example program for reading a Dexter Industries Light Color Sensor on an I2C port.")
18-
19-
lcs = LightColorSensor(led_state = True)
20-
21-
while True:
22-
# Read the R, G, B, C color values
23-
red, green, blue, clear = lcs.get_raw_colors()
24-
25-
# Print the values
26-
print("Red: {:5.3f} Green: {:5.3f} Blue: {:5.3f} Clear: {:5.3f}".format(red, green, blue, clear))
27-
28-
time.sleep(0.02)
1+
#!/usr/bin/env python
2+
#
3+
# https://www.dexterindustries.com
4+
#
5+
# Copyright (c) 2017 Dexter Industries
6+
# Released under the MIT license (http://choosealicense.com/licenses/mit/).
7+
# For more information see https://github.com/DexterInd/DI_Sensors/blob/master/LICENSE.md
8+
#
9+
# Python example program for the Dexter Industries Light Color Sensor
10+
11+
from __future__ import print_function
12+
from __future__ import division
13+
14+
from time import sleep
15+
from di_sensors.easy_light_color_sensor import EasyLightColorSensor
16+
17+
print("Example program for reading a Dexter Industries Light Color Sensor on an I2C port.")
18+
19+
my_lcs = EasyLightColorSensor(led_state = True)
20+
21+
while True:
22+
# Read the R, G, B, C color values
23+
red, green, blue, clear = my_lcs.safe_raw_colors()
24+
25+
# Print the values
26+
print("Red: {:5.3f} Green: {:5.3f} Blue: {:5.3f} Clear: {:5.3f}".format(red, green, blue, clear))
27+
28+
sleep(0.02)

Python/Examples/EasyTempHumPress.py

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
#!/usr/bin/env python
2+
#
3+
# https://www.dexterindustries.com
4+
#
5+
# Copyright (c) 2017 Dexter Industries
6+
# Released under the MIT license (http://choosealicense.com/licenses/mit/).
7+
# For more information see https://github.com/DexterInd/DI_Sensors/blob/master/LICENSE.md
8+
#
9+
# Python example program for the Dexter Industries Temperature Humidity Pressure Sensor
10+
11+
from __future__ import print_function
12+
from __future__ import division
13+
14+
from time import sleep
15+
from di_sensors.easy_temp_hum_press import EasyTHPSensor
16+
17+
print("Example program for reading a Dexter Industries Temperature Humidity Pressure Sensor on an I2C port.")
18+
19+
my_thp = EasyTHPSensor()
20+
21+
while True:
22+
# Read the temperature
23+
temp = my_thp.safe_celsius()
24+
25+
# Read the relative humidity
26+
hum = my_thp.safe_humidity()
27+
28+
# Read the pressure
29+
press = my_thp.safe_pressure()
30+
31+
# Print the values
32+
print("Temperature: {:5.3f} Humidity: {:5.3f} Pressure: {:5.3f}".format(temp, hum, press))
33+
34+
sleep(0.02)

Python/di_sensors/BME280.py

Lines changed: 29 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -134,53 +134,53 @@ def __init__(self, bus = "RPI_1", t_mode = OSAMPLE_1, h_mode = OSAMPLE_1, p_mode
134134
def _load_calibration(self):
135135
# Read calibration data
136136

137-
self.dig_T1 = self.i2c_bus.read_reg_16u(REG_DIG_T1)
138-
self.dig_T2 = self.i2c_bus.read_reg_16s(REG_DIG_T2)
139-
self.dig_T3 = self.i2c_bus.read_reg_16s(REG_DIG_T3)
140-
141-
self.dig_P1 = self.i2c_bus.read_reg_16u(REG_DIG_P1)
142-
self.dig_P2 = self.i2c_bus.read_reg_16s(REG_DIG_P2)
143-
self.dig_P3 = self.i2c_bus.read_reg_16s(REG_DIG_P3)
144-
self.dig_P4 = self.i2c_bus.read_reg_16s(REG_DIG_P4)
145-
self.dig_P5 = self.i2c_bus.read_reg_16s(REG_DIG_P5)
146-
self.dig_P6 = self.i2c_bus.read_reg_16s(REG_DIG_P6)
147-
self.dig_P7 = self.i2c_bus.read_reg_16s(REG_DIG_P7)
148-
self.dig_P8 = self.i2c_bus.read_reg_16s(REG_DIG_P8)
149-
self.dig_P9 = self.i2c_bus.read_reg_16s(REG_DIG_P9)
150-
151-
self.dig_H1 = self.i2c_bus.read_reg_8u(REG_DIG_H1)
152-
self.dig_H2 = self.i2c_bus.read_reg_16s(REG_DIG_H2)
153-
self.dig_H3 = self.i2c_bus.read_reg_8u(REG_DIG_H3)
154-
self.dig_H6 = self.i2c_bus.read_reg_8s(REG_DIG_H7)
155-
156-
h4 = self.i2c_bus.read_reg_8s(REG_DIG_H4)
137+
self.dig_T1 = self.i2c_bus.read_16(REG_DIG_T1)
138+
self.dig_T2 = self.i2c_bus.read_16(REG_DIG_T2, signed = True)
139+
self.dig_T3 = self.i2c_bus.read_16(REG_DIG_T3, signed = True)
140+
141+
self.dig_P1 = self.i2c_bus.read_16(REG_DIG_P1)
142+
self.dig_P2 = self.i2c_bus.read_16(REG_DIG_P2, signed = True)
143+
self.dig_P3 = self.i2c_bus.read_16(REG_DIG_P3, signed = True)
144+
self.dig_P4 = self.i2c_bus.read_16(REG_DIG_P4, signed = True)
145+
self.dig_P5 = self.i2c_bus.read_16(REG_DIG_P5, signed = True)
146+
self.dig_P6 = self.i2c_bus.read_16(REG_DIG_P6, signed = True)
147+
self.dig_P7 = self.i2c_bus.read_16(REG_DIG_P7, signed = True)
148+
self.dig_P8 = self.i2c_bus.read_16(REG_DIG_P8, signed = True)
149+
self.dig_P9 = self.i2c_bus.read_16(REG_DIG_P9, signed = True)
150+
151+
self.dig_H1 = self.i2c_bus.read_8(REG_DIG_H1)
152+
self.dig_H2 = self.i2c_bus.read_16(REG_DIG_H2, signed = True)
153+
self.dig_H3 = self.i2c_bus.read_8(REG_DIG_H3)
154+
self.dig_H6 = self.i2c_bus.read_8(REG_DIG_H7, signed = True)
155+
156+
h4 = self.i2c_bus.read_8(REG_DIG_H4, signed = True)
157157
h4 = (h4 << 4)
158-
self.dig_H4 = h4 | (self.i2c_bus.read_reg_8u(REG_DIG_H5) & 0x0F)
158+
self.dig_H4 = h4 | (self.i2c_bus.read_8(REG_DIG_H5) & 0x0F)
159159

160-
h5 = self.i2c_bus.read_reg_8s(REG_DIG_H6)
160+
h5 = self.i2c_bus.read_8(REG_DIG_H6, signed = True)
161161
h5 = (h5 << 4)
162162
self.dig_H5 = h5 | (
163-
self.i2c_bus.read_reg_8u(REG_DIG_H5) >> 4 & 0x0F)
163+
self.i2c_bus.read_8(REG_DIG_H5) >> 4 & 0x0F)
164164

165165
def _read_raw_temp(self):
166166
# read raw temperature data once it's available
167-
while (self.i2c_bus.read_reg_8u(REG_STATUS) & 0x08):
167+
while (self.i2c_bus.read_8(REG_STATUS) & 0x08):
168168
time.sleep(0.002)
169-
data = self.i2c_bus.read_reg_list(REG_TEMP_DATA, 3)
169+
data = self.i2c_bus.read_list(REG_TEMP_DATA, 3)
170170
return ((data[0] << 16) | (data[1] << 8) | data[2]) >> 4
171171

172172
def _read_raw_pressure(self):
173173
# read raw pressure data once it's available
174-
while (self.i2c_bus.read_reg_8u(REG_STATUS) & 0x08):
174+
while (self.i2c_bus.read_8(REG_STATUS) & 0x08):
175175
time.sleep(0.002)
176-
data = self.i2c_bus.read_reg_list(REG_PRESSURE_DATA, 3)
176+
data = self.i2c_bus.read_list(REG_PRESSURE_DATA, 3)
177177
return ((data[0] << 16) | (data[1] << 8) | data[2]) >> 4
178178

179179
def _read_raw_humidity(self):
180180
# read raw humidity data once it's available
181-
while (self.i2c_bus.read_reg_8u(REG_STATUS) & 0x08):
181+
while (self.i2c_bus.read_8(REG_STATUS) & 0x08):
182182
time.sleep(0.002)
183-
data = self.i2c_bus.read_reg_list(REG_HUMIDITY_DATA, 2)
183+
data = self.i2c_bus.read_list(REG_HUMIDITY_DATA, 2)
184184
return (data[0] << 8) | data[1]
185185

186186
def read_temperature(self):

Python/di_sensors/BNO055.py

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -232,10 +232,10 @@ def __init__(self, bus = "RPI_1", address = ADDRESS_A, mode = OPERATION_MODE_NDO
232232
self.i2c_bus.write_reg_8(REG_PAGE_ID, 0)
233233

234234
# check the chip ID
235-
if ID != self.i2c_bus.read_reg_8u(REG_CHIP_ID):
235+
if ID != self.i2c_bus.read_8(REG_CHIP_ID):
236236
raise RuntimeError("BNO055 failed to respond")
237237

238-
if self.i2c_bus.read_reg_8u(REG_TEMP_SOURCE) != 0x01:
238+
if self.i2c_bus.read_8(REG_TEMP_SOURCE) != 0x01:
239239
# print("Doing init")
240240

241241
# reset the device using the reset command
@@ -288,12 +288,12 @@ def get_revision(self):
288288
Returns a tuple with revision numbers for Software revision, Bootloader
289289
version, Accelerometer ID, Magnetometer ID, and Gyro ID."""
290290
# Read revision values.
291-
accel = self.i2c_bus.read_reg_8u(REG_ACCEL_REV_ID)
292-
mag = self.i2c_bus.read_reg_8u(REG_MAG_REV_ID)
293-
gyro = self.i2c_bus.read_reg_8u(REG_GYRO_REV_ID)
294-
bl = self.i2c_bus.read_reg_8u(REG_BL_REV_ID)
295-
sw_lsb = self.i2c_bus.read_reg_8u(REG_SW_REV_ID_LSB)
296-
sw_msb = self.i2c_bus.read_reg_8u(REG_SW_REV_ID_MSB)
291+
accel = self.i2c_bus.read_8(REG_ACCEL_REV_ID)
292+
mag = self.i2c_bus.read_8(REG_MAG_REV_ID)
293+
gyro = self.i2c_bus.read_8(REG_GYRO_REV_ID)
294+
bl = self.i2c_bus.read_8(REG_BL_REV_ID)
295+
sw_lsb = self.i2c_bus.read_8(REG_SW_REV_ID_LSB)
296+
sw_msb = self.i2c_bus.read_8(REG_SW_REV_ID_MSB)
297297
sw = ((sw_msb << 8) | sw_lsb) & 0xFFFF
298298
# Return the results as a tuple of all 5 values.
299299
return (sw, bl, accel, mag, gyro)
@@ -353,20 +353,20 @@ def get_system_status(self, run_self_test = True):
353353
# Switch to configuration mode if running self test.
354354
self._config_mode()
355355
# Perform a self test.
356-
sys_trigger = self.i2c_bus.read_reg_8u(REG_SYS_TRIGGER)
356+
sys_trigger = self.i2c_bus.read_8(REG_SYS_TRIGGER)
357357
self.i2c_bus.write_reg_8(REG_SYS_TRIGGER, sys_trigger | 0x1)
358358
# Wait for self test to finish.
359359
time.sleep(1.0)
360360
# Read test result.
361-
self_test = self.i2c_bus.read_reg_8u(REG_SELFTEST_RESULT)
361+
self_test = self.i2c_bus.read_8(REG_SELFTEST_RESULT)
362362
# Go back to operation mode.
363363
self._operation_mode()
364364
else:
365365
self_test = None
366366

367367
# read status and error values
368-
status = self.i2c_bus.read_reg_8u(REG_SYS_STAT)
369-
error = self.i2c_bus.read_reg_8u(REG_SYS_ERR)
368+
status = self.i2c_bus.read_8(REG_SYS_STAT)
369+
error = self.i2c_bus.read_8(REG_SYS_ERR)
370370

371371
# return the results as a tuple of all 3 values
372372
return (status, self_test, error)
@@ -395,7 +395,7 @@ def get_calibration_status(self):
395395
396396
"""
397397
# Return the calibration status register value.
398-
cal_status = self.i2c_bus.read_reg_8u(REG_CALIB_STAT)
398+
cal_status = self.i2c_bus.read_8(REG_CALIB_STAT)
399399
sys = (cal_status >> 6) & 0x03
400400
gyro = (cal_status >> 4) & 0x03
401401
accel = (cal_status >> 2) & 0x03
@@ -413,7 +413,7 @@ def get_calibration(self):
413413
# Switch to configuration mode, as mentioned in section 3.10.4 of datasheet.
414414
self._config_mode()
415415
# Read the 22 bytes of calibration data
416-
cal_data = self.i2c_bus.read_reg_list(REG_ACCEL_OFFSET_X_LSB, 22)
416+
cal_data = self.i2c_bus.read_list(REG_ACCEL_OFFSET_X_LSB, 22)
417417
# Go back to normal operation mode.
418418
self._operation_mode()
419419
return cal_data
@@ -464,12 +464,12 @@ def get_axis_remap(self):
464464
|____________|/
465465
"""
466466
# Get the axis remap register value.
467-
map_config = self.i2c_bus.read_reg_8u(REG_AXIS_MAP_CONFIG)
467+
map_config = self.i2c_bus.read_8(REG_AXIS_MAP_CONFIG)
468468
z = (map_config >> 4) & 0x03
469469
y = (map_config >> 2) & 0x03
470470
x = map_config & 0x03
471471
# Get the axis remap sign register value.
472-
sign_config = self.i2c_bus.read_reg_8u(REG_AXIS_MAP_SIGN)
472+
sign_config = self.i2c_bus.read_8(REG_AXIS_MAP_SIGN)
473473
x_sign = (sign_config >> 2) & 0x01
474474
y_sign = (sign_config >> 1) & 0x01
475475
z_sign = sign_config & 0x01
@@ -510,7 +510,7 @@ def set_axis_remap(self, x, y, z, x_sign=AXIS_REMAP_POSITIVE,
510510
def _read_vector(self, reg, count = 3):
511511
# Read count number of 16-bit signed values starting from the provided
512512
# register. Returns a tuple of the values that were read.
513-
data = self.i2c_bus.read_reg_list(reg, count*2)
513+
data = self.i2c_bus.read_list(reg, count*2)
514514
result = [0]*count
515515
for i in range(count):
516516
result[i] = (((data[(i * 2) + 1] & 0xFF) << 8) | (data[(i * 2)] & 0xFF)) & 0xFFFF
@@ -573,4 +573,4 @@ def read_temp(self):
573573
"""Read the temperature
574574
575575
Returns the current temperature in degrees celsius."""
576-
return self.i2c_bus.read_reg_8s(REG_TEMP)
576+
return self.i2c_bus.read_8(REG_TEMP, signed = True)

Python/di_sensors/PCA9570.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,4 +36,4 @@ def get_pins(self):
3636
"""Get the output pin states
3737
3838
Returns the bit values for the 4 outputs"""
39-
return (self.i2c_bus.read_8u() & 0x0F)
39+
return (self.i2c_bus.read_8() & 0x0F)

0 commit comments

Comments
 (0)