forked from oddbotics/ultrasonic_module
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtalker.py
executable file
·57 lines (47 loc) · 1.55 KB
/
talker.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
#!/usr/bin/env python
import rospy
import serial
import string
from std_msgs.msg import String
from std_msgs.msg import Float32
ser = serial.Serial(
# make the port a param
rospy.get_param('port', '/dev/ttyUSB0'),
# port='/dev/ttyUSB0',
baudrate=9600,
parity=serial.PARITY_NONE,
stopbits=serial.STOPBITS_ONE,
bytesize=serial.EIGHTBITS
#timeout=1
)
windowSize = 5
ser.isOpen()
def talker():
pub = rospy.Publisher('chatter', Float32, queue_size=10)
rospy.init_node('talker', anonymous=True)
rate = rospy.Rate(10) # 10hz
sensorReadingsList = []
while not rospy.is_shutdown():
byteData = ser.read(6)
byteData += ser.read(ser.inWaiting())
byteData = byteData.translate(None, 'R')
strByteData = byteData[0]+byteData[1]+byteData[2]+byteData[3]
if strByteData != '':
try:
intByteData = int(strByteData)
floatByteData = intByteData/1000.0
# use moving average
sensorReadingsList.append(floatByteData)
if len(sensorReadingsList) > windowSize:
sensorReadingsList.pop(0)
if len(sensorReadingsList) == windowSize:
sensorAverage = sum(sensorReadingsList)/len(sensorReadingsList)
pub.publish(sensorAverage)
rate.sleep()
except ValueError:
rate.sleep()
if __name__ == '__main__':
try:
talker()
except rospy.ROSInterruptException:
pass