WARNING: topic [/chatter] does not appear to be published yet

asked 2019-05-02 07:57:18 -0500

achraf gravatar image

updated 2019-05-02 07:58:10 -0500

gvdhoorn gravatar image

I have a problem with a publisher node that does not publish in the desired topic. the idea is to retrieve image processing data and send them to the chatter topic

here is my python code:

import rospy
from std_msgs.msg import Float64
import numpy as np
import cv2
import serial

video_capture = cv2.VideoCapture(1)
video_capture.set(3, 320)
video_capture.set(4, 240)

while(True):
    flag, frame = video_capture.read()
    crop_img = frame
    gray = cv2.cvtColor(crop_img, cv2.COLOR_BGR2GRAY)
    blur = cv2.GaussianBlur(gray,(5,5),0)
    ret,thresh = cv2.threshold(blur,60,255,cv2.THRESH_BINARY_INV)
    hierarchy, contours, hierarchy = cv2.findContours(thresh.copy(), 1, cv2.CHAIN_APPROX_NONE)

    if cv2.waitKey(1) & 0xFF == ord('q'):
            break

    if len(contours) > 0:
            c = max(contours, key=cv2.contourArea)
            M = cv2.moments(c)
            cx = int(M['m10']/M['m00'])
            cy = int(M['m01']/M['m00'])
            cv2.line(crop_img,(cx,0),(cx,720),(255,0,0),1)
            cv2.line(crop_img,(0,cy),(1280,cy),(255,0,0),1)
            cv2.drawContours(crop_img, contours, -1, (0,255,0), 1)
            cv2.imshow('frame',frame)
            if cx >= 120:
                cmd=255
                print (cmd)

            if cx < 120 and cx > 50:
                cmd=1
                print (cmd)

            if cx <= 50:
                cmd=-255
                print (cmd)

            else:
                cmd=0
                print (cmd)
#ROS PUB
    def talker():
            rospy.init_node('opencvachraf_node', anonymous=True)
            rate = rospy.Rate(10) # 10hz
        while not rospy.is_shutdown(): 
            pub = rospy.Publisher('chatter', Float64,queue_size=3)

                 pub.publish(cmd)
             rate.sleep()


        if __name__ == '__main__':
                try:
                    opencvachraf_node()
            except rospy.ROSInterruptException:
                    pass
edit retag flag offensive close merge delete

Comments

2

can you please check the indenting of the code? Right now it's hard to figure out what the scopes are and the resulting control flow.

Be sure to properly format the code using the Preformatted Text button (101010).


Edit: also: where is opencvachraf_node defined? It's not a method in the code you show.

gvdhoorn gravatar image gvdhoorn  ( 2019-05-02 07:59:07 -0500 )edit