Skip to content

Commit 519fe78

Browse files
committed
Work on servo driver script.
1 parent 727c9e7 commit 519fe78

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

52 files changed

+102046
-8
lines changed

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
sftp-config.json

adafruit/Adafruit_ADS1x15/Adafruit_ADS1x15.py

+722
Large diffs are not rendered by default.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
#!/usr/bin/python
2+
3+
import time, signal, sys
4+
from Adafruit_ADS1x15 import ADS1x15
5+
6+
def signal_handler(signal, frame):
7+
print 'You pressed Ctrl+C!'
8+
print adc.getLastConversionResults()/1000.0
9+
adc.stopContinuousConversion()
10+
sys.exit(0)
11+
12+
signal.signal(signal.SIGINT, signal_handler)
13+
# Print 'Press Ctrl+C to exit'
14+
15+
ADS1015 = 0x00 # 12-bit ADC
16+
ADS1115 = 0x01 # 16-bit ADC
17+
18+
# Initialise the ADC using the default mode (use default I2C address)
19+
# Set this to ADS1015 or ADS1115 depending on the ADC you are using!
20+
adc = ADS1x15(ic=ADS1115)
21+
22+
# start comparator on channel 2 with a thresholdHigh=200mV and low=100mV
23+
# in traditional mode, non-latching, +/-1.024V and 250sps
24+
adc.startSingleEndedComparator(2, 200, 100, pga=1024, sps=250, activeLow=True, traditionalMode=True, latching=False, numReadings=1)
25+
26+
while True:
27+
print adc.getLastConversionResults()/1000.0
28+
time.sleep(0.25)
29+
30+
#time.sleep(0.1)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
#!/usr/bin/python
2+
3+
import time, signal, sys
4+
from Adafruit_ADS1x15 import ADS1x15
5+
6+
def signal_handler(signal, frame):
7+
#print 'You pressed Ctrl+C!'
8+
sys.exit(0)
9+
signal.signal(signal.SIGINT, signal_handler)
10+
#print 'Press Ctrl+C to exit'
11+
12+
ADS1015 = 0x00 # 12-bit ADC
13+
ADS1115 = 0x01 # 16-bit ADC
14+
15+
# Initialise the ADC using the default mode (use default I2C address)
16+
# Set this to ADS1015 or ADS1115 depending on the ADC you are using!
17+
adc = ADS1x15(ic=ADS1115)
18+
19+
# Read channels 2 and 3 in single-ended mode, at +/-4.096V and 250sps
20+
volts2 = adc.readADCSingleEnded(2, 4096, 250)/1000.0
21+
volts3 = adc.readADCSingleEnded(3, 4096, 250)/1000.0
22+
23+
# Now do a differential reading of channels 2 and 3
24+
voltsdiff = adc.readADCDifferential23(4096, 250)/1000.0
25+
26+
# Display the two different reading for comparison purposes
27+
print "%.8f %.8f %.8f %.8f" % (volts2, volts3, volts3-volts2, -voltsdiff)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
#!/usr/bin/python
2+
3+
import time, signal, sys
4+
from Adafruit_ADS1x15 import ADS1x15
5+
6+
def signal_handler(signal, frame):
7+
print 'You pressed Ctrl+C!'
8+
sys.exit(0)
9+
signal.signal(signal.SIGINT, signal_handler)
10+
#print 'Press Ctrl+C to exit'
11+
12+
ADS1015 = 0x00 # 12-bit ADC
13+
ADS1115 = 0x01 # 16-bit ADC
14+
15+
# Initialise the ADC using the default mode (use default I2C address)
16+
# Set this to ADS1015 or ADS1115 depending on the ADC you are using!
17+
adc = ADS1x15(ic=ADS1115)
18+
19+
# Read channel 0 in single-ended mode, +/-4.096V, 250sps
20+
volts = adc.readADCSingleEnded(0, 4096, 250) / 1000
21+
22+
# To read channel 3 in single-ended mode, +/- 1.024V, 860 sps use:
23+
# volts = adc.readADCSingleEnded(3, 1024, 860)
24+
25+
print "%.6f" % (volts)
+247
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,247 @@
1+
#!/usr/bin/python
2+
3+
import time
4+
from Adafruit_I2C import Adafruit_I2C
5+
6+
# ===========================================================================
7+
# BMP085 Class
8+
# ===========================================================================
9+
10+
class BMP085 :
11+
i2c = None
12+
13+
# Operating Modes
14+
__BMP085_ULTRALOWPOWER = 0
15+
__BMP085_STANDARD = 1
16+
__BMP085_HIGHRES = 2
17+
__BMP085_ULTRAHIGHRES = 3
18+
19+
# BMP085 Registers
20+
__BMP085_CAL_AC1 = 0xAA # R Calibration data (16 bits)
21+
__BMP085_CAL_AC2 = 0xAC # R Calibration data (16 bits)
22+
__BMP085_CAL_AC3 = 0xAE # R Calibration data (16 bits)
23+
__BMP085_CAL_AC4 = 0xB0 # R Calibration data (16 bits)
24+
__BMP085_CAL_AC5 = 0xB2 # R Calibration data (16 bits)
25+
__BMP085_CAL_AC6 = 0xB4 # R Calibration data (16 bits)
26+
__BMP085_CAL_B1 = 0xB6 # R Calibration data (16 bits)
27+
__BMP085_CAL_B2 = 0xB8 # R Calibration data (16 bits)
28+
__BMP085_CAL_MB = 0xBA # R Calibration data (16 bits)
29+
__BMP085_CAL_MC = 0xBC # R Calibration data (16 bits)
30+
__BMP085_CAL_MD = 0xBE # R Calibration data (16 bits)
31+
__BMP085_CONTROL = 0xF4
32+
__BMP085_TEMPDATA = 0xF6
33+
__BMP085_PRESSUREDATA = 0xF6
34+
__BMP085_READTEMPCMD = 0x2E
35+
__BMP085_READPRESSURECMD = 0x34
36+
37+
# Private Fields
38+
_cal_AC1 = 0
39+
_cal_AC2 = 0
40+
_cal_AC3 = 0
41+
_cal_AC4 = 0
42+
_cal_AC5 = 0
43+
_cal_AC6 = 0
44+
_cal_B1 = 0
45+
_cal_B2 = 0
46+
_cal_MB = 0
47+
_cal_MC = 0
48+
_cal_MD = 0
49+
50+
# Constructor
51+
def __init__(self, address=0x77, mode=1, debug=False):
52+
self.i2c = Adafruit_I2C(address)
53+
54+
self.address = address
55+
self.debug = debug
56+
# Make sure the specified mode is in the appropriate range
57+
if ((mode < 0) | (mode > 3)):
58+
if (self.debug):
59+
print "Invalid Mode: Using STANDARD by default"
60+
self.mode = self.__BMP085_STANDARD
61+
else:
62+
self.mode = mode
63+
# Read the calibration data
64+
self.readCalibrationData()
65+
66+
def readCalibrationData(self):
67+
"Reads the calibration data from the IC"
68+
self._cal_AC1 = self.i2c.readS16(self.__BMP085_CAL_AC1) # INT16
69+
self._cal_AC2 = self.i2c.readS16(self.__BMP085_CAL_AC2) # INT16
70+
self._cal_AC3 = self.i2c.readS16(self.__BMP085_CAL_AC3) # INT16
71+
self._cal_AC4 = self.i2c.readU16(self.__BMP085_CAL_AC4) # UINT16
72+
self._cal_AC5 = self.i2c.readU16(self.__BMP085_CAL_AC5) # UINT16
73+
self._cal_AC6 = self.i2c.readU16(self.__BMP085_CAL_AC6) # UINT16
74+
self._cal_B1 = self.i2c.readS16(self.__BMP085_CAL_B1) # INT16
75+
self._cal_B2 = self.i2c.readS16(self.__BMP085_CAL_B2) # INT16
76+
self._cal_MB = self.i2c.readS16(self.__BMP085_CAL_MB) # INT16
77+
self._cal_MC = self.i2c.readS16(self.__BMP085_CAL_MC) # INT16
78+
self._cal_MD = self.i2c.readS16(self.__BMP085_CAL_MD) # INT16
79+
if (self.debug):
80+
self.showCalibrationData()
81+
82+
def showCalibrationData(self):
83+
"Displays the calibration values for debugging purposes"
84+
print "DBG: AC1 = %6d" % (self._cal_AC1)
85+
print "DBG: AC2 = %6d" % (self._cal_AC2)
86+
print "DBG: AC3 = %6d" % (self._cal_AC3)
87+
print "DBG: AC4 = %6d" % (self._cal_AC4)
88+
print "DBG: AC5 = %6d" % (self._cal_AC5)
89+
print "DBG: AC6 = %6d" % (self._cal_AC6)
90+
print "DBG: B1 = %6d" % (self._cal_B1)
91+
print "DBG: B2 = %6d" % (self._cal_B2)
92+
print "DBG: MB = %6d" % (self._cal_MB)
93+
print "DBG: MC = %6d" % (self._cal_MC)
94+
print "DBG: MD = %6d" % (self._cal_MD)
95+
96+
def readRawTemp(self):
97+
"Reads the raw (uncompensated) temperature from the sensor"
98+
self.i2c.write8(self.__BMP085_CONTROL, self.__BMP085_READTEMPCMD)
99+
time.sleep(0.005) # Wait 5ms
100+
raw = self.i2c.readU16(self.__BMP085_TEMPDATA)
101+
if (self.debug):
102+
print "DBG: Raw Temp: 0x%04X (%d)" % (raw & 0xFFFF, raw)
103+
return raw
104+
105+
def readRawPressure(self):
106+
"Reads the raw (uncompensated) pressure level from the sensor"
107+
self.i2c.write8(self.__BMP085_CONTROL, self.__BMP085_READPRESSURECMD + (self.mode << 6))
108+
if (self.mode == self.__BMP085_ULTRALOWPOWER):
109+
time.sleep(0.005)
110+
elif (self.mode == self.__BMP085_HIGHRES):
111+
time.sleep(0.014)
112+
elif (self.mode == self.__BMP085_ULTRAHIGHRES):
113+
time.sleep(0.026)
114+
else:
115+
time.sleep(0.008)
116+
msb = self.i2c.readU8(self.__BMP085_PRESSUREDATA)
117+
lsb = self.i2c.readU8(self.__BMP085_PRESSUREDATA+1)
118+
xlsb = self.i2c.readU8(self.__BMP085_PRESSUREDATA+2)
119+
raw = ((msb << 16) + (lsb << 8) + xlsb) >> (8 - self.mode)
120+
if (self.debug):
121+
print "DBG: Raw Pressure: 0x%04X (%d)" % (raw & 0xFFFF, raw)
122+
return raw
123+
124+
def readTemperature(self):
125+
"Gets the compensated temperature in degrees celcius"
126+
UT = 0
127+
X1 = 0
128+
X2 = 0
129+
B5 = 0
130+
temp = 0.0
131+
132+
# Read raw temp before aligning it with the calibration values
133+
UT = self.readRawTemp()
134+
X1 = ((UT - self._cal_AC6) * self._cal_AC5) >> 15
135+
X2 = (self._cal_MC << 11) / (X1 + self._cal_MD)
136+
B5 = X1 + X2
137+
temp = ((B5 + 8) >> 4) / 10.0
138+
if (self.debug):
139+
print "DBG: Calibrated temperature = %f C" % temp
140+
return temp
141+
142+
def readPressure(self):
143+
"Gets the compensated pressure in pascal"
144+
UT = 0
145+
UP = 0
146+
B3 = 0
147+
B5 = 0
148+
B6 = 0
149+
X1 = 0
150+
X2 = 0
151+
X3 = 0
152+
p = 0
153+
B4 = 0
154+
B7 = 0
155+
156+
UT = self.readRawTemp()
157+
UP = self.readRawPressure()
158+
159+
# You can use the datasheet values to test the conversion results
160+
# dsValues = True
161+
dsValues = False
162+
163+
if (dsValues):
164+
UT = 27898
165+
UP = 23843
166+
self._cal_AC6 = 23153
167+
self._cal_AC5 = 32757
168+
self._cal_MB = -32768;
169+
self._cal_MC = -8711
170+
self._cal_MD = 2868
171+
self._cal_B1 = 6190
172+
self._cal_B2 = 4
173+
self._cal_AC3 = -14383
174+
self._cal_AC2 = -72
175+
self._cal_AC1 = 408
176+
self._cal_AC4 = 32741
177+
self.mode = self.__BMP085_ULTRALOWPOWER
178+
if (self.debug):
179+
self.showCalibrationData()
180+
181+
# True Temperature Calculations
182+
X1 = ((UT - self._cal_AC6) * self._cal_AC5) >> 15
183+
X2 = (self._cal_MC << 11) / (X1 + self._cal_MD)
184+
B5 = X1 + X2
185+
if (self.debug):
186+
print "DBG: X1 = %d" % (X1)
187+
print "DBG: X2 = %d" % (X2)
188+
print "DBG: B5 = %d" % (B5)
189+
print "DBG: True Temperature = %.2f C" % (((B5 + 8) >> 4) / 10.0)
190+
191+
# Pressure Calculations
192+
B6 = B5 - 4000
193+
X1 = (self._cal_B2 * (B6 * B6) >> 12) >> 11
194+
X2 = (self._cal_AC2 * B6) >> 11
195+
X3 = X1 + X2
196+
B3 = (((self._cal_AC1 * 4 + X3) << self.mode) + 2) / 4
197+
if (self.debug):
198+
print "DBG: B6 = %d" % (B6)
199+
print "DBG: X1 = %d" % (X1)
200+
print "DBG: X2 = %d" % (X2)
201+
print "DBG: X3 = %d" % (X3)
202+
print "DBG: B3 = %d" % (B3)
203+
204+
X1 = (self._cal_AC3 * B6) >> 13
205+
X2 = (self._cal_B1 * ((B6 * B6) >> 12)) >> 16
206+
X3 = ((X1 + X2) + 2) >> 2
207+
B4 = (self._cal_AC4 * (X3 + 32768)) >> 15
208+
B7 = (UP - B3) * (50000 >> self.mode)
209+
if (self.debug):
210+
print "DBG: X1 = %d" % (X1)
211+
print "DBG: X2 = %d" % (X2)
212+
print "DBG: X3 = %d" % (X3)
213+
print "DBG: B4 = %d" % (B4)
214+
print "DBG: B7 = %d" % (B7)
215+
216+
if (B7 < 0x80000000):
217+
p = (B7 * 2) / B4
218+
else:
219+
p = (B7 / B4) * 2
220+
221+
if (self.debug):
222+
print "DBG: X1 = %d" % (X1)
223+
224+
X1 = (p >> 8) * (p >> 8)
225+
X1 = (X1 * 3038) >> 16
226+
X2 = (-7357 * p) >> 16
227+
if (self.debug):
228+
print "DBG: p = %d" % (p)
229+
print "DBG: X1 = %d" % (X1)
230+
print "DBG: X2 = %d" % (X2)
231+
232+
p = p + ((X1 + X2 + 3791) >> 4)
233+
if (self.debug):
234+
print "DBG: Pressure = %d Pa" % (p)
235+
236+
return p
237+
238+
def readAltitude(self, seaLevelPressure=101325):
239+
"Calculates the altitude in meters"
240+
altitude = 0.0
241+
pressure = float(self.readPressure())
242+
altitude = 44330.0 * (1.0 - pow(pressure / seaLevelPressure, 0.1903))
243+
if (self.debug):
244+
print "DBG: Altitude = %d" % (altitude)
245+
return altitude
246+
247+
return 0
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
#!/usr/bin/python
2+
3+
from Adafruit_BMP085 import BMP085
4+
5+
# ===========================================================================
6+
# Example Code
7+
# ===========================================================================
8+
9+
# Initialise the BMP085 and use STANDARD mode (default value)
10+
# bmp = BMP085(0x77, debug=True)
11+
bmp = BMP085(0x77)
12+
13+
# To specify a different operating mode, uncomment one of the following:
14+
# bmp = BMP085(0x77, 0) # ULTRALOWPOWER Mode
15+
# bmp = BMP085(0x77, 1) # STANDARD Mode
16+
# bmp = BMP085(0x77, 2) # HIRES Mode
17+
# bmp = BMP085(0x77, 3) # ULTRAHIRES Mode
18+
19+
temp = bmp.readTemperature()
20+
21+
# Read the current barometric pressure level
22+
pressure = bmp.readPressure()
23+
24+
# To calculate altitude based on an estimated mean sea level pressure
25+
# (1013.25 hPa) call the function as follows, but this won't be very accurate
26+
altitude = bmp.readAltitude()
27+
28+
# To specify a more accurate altitude, enter the correct mean sea level
29+
# pressure level. For example, if the current pressure level is 1023.50 hPa
30+
# enter 102350 since we include two decimal places in the integer value
31+
# altitude = bmp.readAltitude(102350)
32+
33+
print "Temperature: %.2f C" % temp
34+
print "Pressure: %.2f hPa" % (pressure / 100.0)
35+
print "Altitude: %.2f" % altitude

0 commit comments

Comments
 (0)