-
Notifications
You must be signed in to change notification settings - Fork 1
/
test_maxbotix.py
150 lines (107 loc) · 3.36 KB
/
test_maxbotix.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
"""test_maxbotix tests the ADC and Maxbotix rangefinder configuration.
This is just a simple program to exercise the maxbotix rangefinder.
"""
##############################################################################
# Author: Phil Moyer (phil@moyer.ai)
# Date: November 2019
#
# License: This program is released under the MIT license. Any
# redistribution must include this header.
##############################################################################
# Remember the style guide: http://www.python.org/dev/peps/pep-0008/
#
# Functions, variables, and attributes should be lowercase_underscore
# Protected instance attributes should be _leading_underscore
# Private instance attributes should be __double_leading_underscore
# Classes and exceptions should be CapitalizedWord
# Module-level constants should be ALL_CAPS
######################
# Import Libraries
######################
# Standard libraries modules
import numpy as np
import smbus
import os
import sys
from time import sleep
import io
import time
from datetime import datetime, timedelta
# Third-party modules
import board
import busio
import adafruit_ads1x15.ads1115 as ADS
from adafruit_ads1x15.analog_in import AnalogIn
# Package/application modules
######################
# Globals
######################
######################
# Pre-Main Setup
######################
##### Blinka/CircuitPython init #####
i2c = busio.I2C(board.SCL, board.SDA)
ads = ADS.ADS1115(i2c)
chan = AnalogIn(ads, ADS.P0)
ads.gain = 1
######################
# Classes and Methods
######################
######################
# Functions
######################
##### Rangefinder Section #####
# NOTE: this uses the MaxBotix LV-EZ0 ultrasonic rangefinder.
# Pin 3 -> ADC
# Pin 6 -> 3.3v
# Pin 7 -> GND
# It also uses an ADS1115 16-bit I2C ADC with programmable gain.
# VCC -> 3.3v
def adc_to_range():
"""adc_to_range provides range in inches from the
rangefinder (MaxBotix LV-EZ)
Returns:
range in inches
"""
# 3.3V yields ~6.4mV/in.
cur_value, cur_volt = read_adc()
cur_range = cur_volt * 6.4 # to give us inches?
print("%d,%0.6f," % (cur_value, cur_volt))
return(cur_range)
def read_adc():
"""
read_adc() simply reads the values from the analog-to-digital
converter and returns them. The ADS1115 returns both a "value"
and the voltage. In our case, voltage will be most useful.
Arguements: none
Returns:
Value
Voltage (need to check units)
"""
# Read the ADC
curVal = chan.value
curVolt = chan.voltage
# print("Value: %d Voltage: %0.3f" % (curVal, curVolt))
return (curVal, curVolt)
##### Main #####
def main():
"""Abstract main() into a function. Normally exits after execution.
A function abstracting the main code in the module, which
allows it to be used for libraries as well as testing (i.e., it can be
called as a script for testing or imported as a library, without
modification).
"""
while True:
adc_to_range()
sleep(5)
# NOTREACHED
# Exit from async "run until complete"
return
######################
# Main
######################
# The main code call allows this module to be imported as a library or
# called as a standalone program because __name__ will not be properly
# set unless called as a program.
if __name__ == '__main__':
main()