├── README.md ├── .gitignore ├── LICENSE └── talker.py /README.md: -------------------------------------------------------------------------------- 1 | # ultrasonic_module 2 | sensor module uses and publishes info from the ultrasonic device 3 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # Byte-compiled / optimized / DLL files 2 | __pycache__/ 3 | *.py[cod] 4 | 5 | # C extensions 6 | *.so 7 | 8 | # Distribution / packaging 9 | .Python 10 | env/ 11 | build/ 12 | develop-eggs/ 13 | dist/ 14 | downloads/ 15 | eggs/ 16 | .eggs/ 17 | lib/ 18 | lib64/ 19 | parts/ 20 | sdist/ 21 | var/ 22 | *.egg-info/ 23 | .installed.cfg 24 | *.egg 25 | 26 | # PyInstaller 27 | # Usually these files are written by a python script from a template 28 | # before PyInstaller builds the exe, so as to inject date/other infos into it. 29 | *.manifest 30 | *.spec 31 | 32 | # Installer logs 33 | pip-log.txt 34 | pip-delete-this-directory.txt 35 | 36 | # Unit test / coverage reports 37 | htmlcov/ 38 | .tox/ 39 | .coverage 40 | .coverage.* 41 | .cache 42 | nosetests.xml 43 | coverage.xml 44 | *,cover 45 | 46 | # Translations 47 | *.mo 48 | *.pot 49 | 50 | # Django stuff: 51 | *.log 52 | 53 | # Sphinx documentation 54 | docs/_build/ 55 | 56 | # PyBuilder 57 | target/ 58 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Copyright (c) 2015, oddbotics 2 | All rights reserved. 3 | 4 | Redistribution and use in source and binary forms, with or without 5 | modification, are permitted provided that the following conditions are met: 6 | 7 | * Redistributions of source code must retain the above copyright notice, this 8 | list of conditions and the following disclaimer. 9 | 10 | * Redistributions in binary form must reproduce the above copyright notice, 11 | this list of conditions and the following disclaimer in the documentation 12 | and/or other materials provided with the distribution. 13 | 14 | * Neither the name of ultrasonic_module nor the names of its 15 | contributors may be used to endorse or promote products derived from 16 | this software without specific prior written permission. 17 | 18 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 19 | AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 20 | IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 21 | DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE 22 | FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 23 | DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 24 | SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER 25 | CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 26 | OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 27 | OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 28 | 29 | -------------------------------------------------------------------------------- /talker.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | import rospy 3 | import serial 4 | import string 5 | 6 | from std_msgs.msg import String 7 | from std_msgs.msg import Float32 8 | 9 | ser = serial.Serial( 10 | # make the port a param 11 | rospy.get_param('port', '/dev/ttyUSB0'), 12 | # port='/dev/ttyUSB0', 13 | baudrate=9600, 14 | parity=serial.PARITY_NONE, 15 | stopbits=serial.STOPBITS_ONE, 16 | bytesize=serial.EIGHTBITS 17 | #timeout=1 18 | ) 19 | 20 | windowSize = 5 21 | 22 | ser.isOpen() 23 | 24 | def talker(): 25 | pub = rospy.Publisher('chatter', Float32, queue_size=10) 26 | rospy.init_node('talker', anonymous=True) 27 | rate = rospy.Rate(10) # 10hz 28 | 29 | sensorReadingsList = [] 30 | 31 | while not rospy.is_shutdown(): 32 | byteData = ser.read(6) 33 | byteData += ser.read(ser.inWaiting()) 34 | byteData = byteData.translate(None, 'R') 35 | strByteData = byteData[0]+byteData[1]+byteData[2]+byteData[3] 36 | 37 | if strByteData != '': 38 | try: 39 | intByteData = int(strByteData) 40 | floatByteData = intByteData/1000.0 41 | 42 | # use moving average 43 | sensorReadingsList.append(floatByteData) 44 | if len(sensorReadingsList) > windowSize: 45 | sensorReadingsList.pop(0) 46 | if len(sensorReadingsList) == windowSize: 47 | sensorAverage = sum(sensorReadingsList)/len(sensorReadingsList) 48 | pub.publish(sensorAverage) 49 | rate.sleep() 50 | except ValueError: 51 | rate.sleep() 52 | 53 | if __name__ == '__main__': 54 | try: 55 | talker() 56 | except rospy.ROSInterruptException: 57 | pass 58 | --------------------------------------------------------------------------------