Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013 import serial
00014
00015 import roslib; roslib.load_manifest('hrl_fabric_based_tactile_sensor')
00016 import rospy
00017 from hrl_msgs.msg import FloatArray
00018
00019
00020 def setup_serial(dev_name, baudrate):
00021 try:
00022 serial_dev = serial.Serial(dev_name, timeout=1.)
00023 if(serial_dev == None):
00024 raise RuntimeError("robotis_servo: Serial port not found!\n")
00025
00026 serial_dev.setBaudrate(baudrate)
00027 serial_dev.setParity('N')
00028 serial_dev.setStopbits(1)
00029 serial_dev.open()
00030
00031 serial_dev.flushOutput()
00032 serial_dev.flushInput()
00033 return serial_dev
00034
00035 except (serial.serialutil.SerialException), e:
00036 raise RuntimeError("robotis_servo: Serial port not found!\n")
00037
00038 def get_adc_data(serial_dev, num_adc_inputs):
00039
00040 ln = serial_dev.readline()
00041 l = map(int, ln.split(','))
00042
00043
00044
00045
00046 if len(l) != num_adc_inputs:
00047 rospy.logwarn('Number of ADC values does not match prescribed number of inputs. Something fishy with the serial port.')
00048
00049
00050
00051
00052 serial_dev.flush()
00053 l = get_adc_data(serial_dev, num_adc_inputs)
00054 return l
00055
00056
00057 if __name__ == '__main__':
00058
00059 dev_name = '/dev/ttyUSB4'
00060
00061
00062
00063
00064 baudrate = 115200
00065
00066 serial_dev = setup_serial(dev_name, baudrate)
00067
00068 pub = rospy.Publisher('/arduino/ADC', FloatArray)
00069 rospy.init_node('adc_publisher_node')
00070
00071 for i in range(10):
00072 ln = serial_dev.readline()
00073
00074 rospy.loginfo('Started publishing ADC data')
00075
00076 while not rospy.is_shutdown():
00077 fa = FloatArray()
00078 fa.header.stamp = rospy.Time.now()
00079 fa.data = get_adc_data(serial_dev, 6)
00080 pub.publish(fa)
00081
00082 serial_dev.close()
00083
00084
00085
00086