how do I make a ros publisher run under python 3?
I made a python3 script that will read left, right encoder ticks from an arduino. I am trying to now get that same code to publish those ticks. This is what I have so far. sorry I not at all good with python.
#!/usr/bin/env python3
import serial
import time
import rospy
from std_msgs.msg import Int16
# assign your port and speed
ser = serial.Serial('/dev/ttyUSB0', 1152000)
print("Reset Arduino")
time.sleep(3)
while (1) # WORKING PART
#left encoder read
ser.write(bytes('l', 'UTF-8'))
left = ser.readline().decode('ascii').strip()
print("left")
#print(left)
#right encoder read
ser.write(bytes('r', 'UTF-8'))
right = ser.readline().decode('ascii').strip()
right = right.rstrip('\r\n')
print('right')
#print(right)
time.sleep(.0125)
#node to publish left to lwheel (std_msgs/Int16)
#not working part
def leftticks():
pub = rospy.Publisher('lwheel', Int16)
rospy.init_node('leftticks')
while not rospy.is_shutdown():
lstr = left
rospy.loginfo(lstr)
pub.publish(String(lstr))
rospy.sleep(.5)
if __name__ == '__main__':
try:
leftticks()
except rospy.ROSInterruptException:
pass
#node to pulish right to rwheel (std_msgs/Int16)
def rightticks():
pub = rospy.Publisher('rwheel', Int16)
rospy.init_node('rightticks')
while not rospy.is_shutdown():
rstr = right
rospy.loginfo(rstr)
pub.publish(String(rstr))
rospy.sleep(.5)
if __name__ == '__main__':
try:
rightticks()
except rospy.ROSInterruptException:
pass
How do I get it all to work together. As you can tell I have no clue what I am doing. Thanks for any help you can give.
Right now that is really hard to do. You don't mention what OS or ROS version you are using.
BTW, the shebang (#!) needs to be left justified.
I am using Ubuntu 12.04lts and groovy. If I can't get to work on python3 how do I make it work under python 2.7 or what ros uses?