All publishers give this warning when a subscriber subscribes
All publishers give this warning when a subscriber subscribes to them using wait_for_message:
[WARN] [1603436136.720705]: Inbound TCP/IP connection failed: connection from sender terminated before handshake header received. 0 bytes were received. Please check sender for additional details.
The publisher:
#!/usr/bin/python3
import rospy, time, math
import RPi.GPIO as GPIO
from geometry_msgs.msg import Twist
import sys, subprocess
"""subprocess.Popen([sys.executable, 'sudo mjpg_streamer -i "/usr/local/lib/input_uvc.so" -o "/usr/local/lib/output_http.so -w /usr/local/www"'])"""
GPIO.setmode(GPIO.BCM)
GPIO.setup(27, GPIO.OUT)
lf = GPIO.PWM(27, 50)
GPIO.setup(23, GPIO.OUT)
rf = GPIO.PWM(23, 50)
lf.start(0)
rf.start(0)
GPIO.setup(17, GPIO.OUT)
lb = GPIO.PWM(17, 50)
GPIO.setup(22, GPIO.OUT)
rb = GPIO.PWM(22, 50)
lb.start(0)
rb.start(0)
wheel_pwm = rospy.Publisher("wheel_pwm", Twist, queue_size=1)
def receiveMessage(msg):
l = min((msg.linear.x + (-msg.angular.z)) * 50, 99)
r = min((msg.linear.x - (-msg.angular.z)) * 50, 99)
wheelpwm = Twist()
wheelpwm.linear.x = l; wheelpwm.linear.y = r; wheelpwm.linear.z = 0
wheel_pwm.publish(wheelpwm)
if l >= 0:
lf.ChangeDutyCycle(l)
lb.ChangeDutyCycle(0)
else:
lb.ChangeDutyCycle(l * -1)
lf.ChangeDutyCycle(0)
if r >= 0:
rf.ChangeDutyCycle(r)
rb.ChangeDutyCycle(0)
else:
rb.ChangeDutyCycle(r * -1)
rf.ChangeDutyCycle(0)
rospy.init_node('fourwd')
rospy.Subscriber('key_vel', Twist, receiveMessage)
rospy.spin()
GPIO.cleanup()
This is the subscriber:
#!/usr/bin/python3
import RPi.GPIO as GPIO
from geometry_msgs.msg import Twist
import rospy
from std_msgs.msg import Int16
from threading import Thread, Event
GPIO.setmode(GPIO.BCM)
GPIO.setup(24, GPIO.IN)
GPIO.setup(25, GPIO.IN)
pub = rospy.Publisher("wheels", Twist, queue_size=1)
rospy.init_node('pub_encoders')
event = Event()
def Tick(lr_):
while True:
lpwm = lr_[4]
rpwm = lr_[5]
if not lr_[2] and GPIO.input(24):
lr[0]+=lpwm
lr_[2] = True
elif not GPIO.input(24):
lr_[2] = False
if not lr_[3] and GPIO.input(25):
lr[1]+=rpwm
lr_[3] = True
elif not GPIO.input(25):
lr_[3] = False
rospy.sleep(0.01)
def Publisher(lr_):
while True:
_t = Twist()
_t.linear.x = lr[0]
_t.linear.y = lr[1]
pub.publish(_t)
rospy.sleep(0.1)
lr = [0,0, False, False, 1, 1]
reader = Thread(target=Tick, args=(lr,))
reader.start()
publisher = Thread(target=Publisher, args=(lr,))
publisher.start()
while True:
try:
twst = rospy.wait_for_message("wheel_pwm", Twist)
if twst.linear.x > 0:
lr[4] = 1
elif twst.linear.x < 0:
lr[4] = -1
if twst.linear.y > 0:
lr[5] = 1
elif twst.linear.y < 0:
lr[5] = -1
except rospy.exceptions.ROSException:
pass