ROS Resources: Documentation | Support | Discussion Forum | Index | Service Status | ros @ Robotics Stack Exchange
Ask Your Question
1

Handling two nodes

asked 2014-06-25 06:52:59 -0600

jashanvir gravatar image

updated 2016-12-09 22:12:03 -0600

130s gravatar image

Hello guys ! I have created two nodes In my first node pilot, I am trying to make the quadrotor scan the whole room until it finds a color box and when it finds the color box i stopped my quadrotor and the publish my coordinates to another node 2. But I am getting some errors like in my node 1 my quadrotor stops above the blue box and after the completing of the node i got the following error:

[ERROR] [WallTime: 1403691673.456236] bad callback: <function callback at 0x233ade8>
Traceback (most recent call last):
  **File "/opt/ros/hydro/lib/python2.7/dist-packages/rospy/topics.py", line 682, in _invoke_callback
    cb(msg)
  File "/home/user/catkin_ws/src/my_package/scripts/pilot.py", line 84, in callback
    permanent_stop()
  File "/home/user/catkin_ws/src/my_package/scripts/pilot.py", line 117, in permanent_stop
    cmd.publish(motion)
  File "/opt/ros/hydro/lib/python2.7/dist-packages/rospy/topics.py", line 798, in publish
    self.impl.publish(data)
  File "/opt/ros/hydro/lib/python2.7/dist-packages/rospy/topics.py", line 957, in publish
    raise ROSException("publish() to a closed topic")
ROSException: publish() to a closed topic**

Node 1 (pilot) Code. The following is the first node1 (pilot) . :

#!/usr/bin/env python
import roslib
import sys
import time
import math
import rospy
import cv2
import time
#import cv2.cv as cv
import numpy as np
from std_msgs.msg import Float64
from geometry_msgs.msg import PoseStamped
from geometry_msgs.msg import Vector3
from geometry_msgs.msg import Twist
from geometry_msgs.msg import Wrench
from geometry_msgs.msg import Pose
from geometry_msgs.msg import Point
from sensor_msgs.msg import NavSatFix
from sensor_msgs.msg import CameraInfo
from sensor_msgs.msg import Image
from sensor_msgs.msg import RegionOfInterest
from cv_bridge import CvBridge, CvBridgeError
from rospy.numpy_msg import numpy_msg
a=0
b=0
c=0
timer = 0
#class my_class:

def bridge_opencv():
        image_pub = rospy.Publisher("quadrotor/videocamera1/camera_info",Image)

            cv2.namedWindow("Image window", 1)

        image_sub = rospy.Subscriber("quadrotor/videocamera1/image",Image, callback)


def callback(data):
        global timer
        global dis
        global my_var1
        global my_var2
        global my_var3
        global a
        global b
        global c
        bridge = CvBridge()
        try:
            cv_image = bridge.imgmsg_to_cv2(data, "bgr8")
        except CvBridgeError, e:
            print e
        (rows,cols,channels) = cv_image.shape
        if cols > 60 and rows > 60 :
            cv2.circle(cv_image, (50,50), 10, 255)
        #converting bgr to hsv  
        hsv=cv2.cvtColor(cv_image,cv2.COLOR_BGR2HSV)
        # define range of blue color in HSV
            lower_blue = np.array([60,0,0],dtype=np.uint8)
            upper_blue = np.array([255,255,255],dtype=np.uint8)
        # Threshold the HSV image to get only blue colors
            mask = cv2.inRange(hsv, lower_blue, upper_blue)
        new_mask = mask.copy()
        # Bitwise-AND mask and original image
            res = cv2.bitwise_and(cv_image,cv_image, mask= mask)
        #removing noise 
        kernel = np.ones((12,12),np.uint8)
        new_mask = cv2.morphologyEx(new_mask, cv2.MORPH_CLOSE, kernel)
        new_mask = cv2.morphologyEx(new_mask, cv2.MORPH_OPEN, kernel)
        contours, hierarchy = cv2.findContours(new_mask,cv2.RETR_TREE,cv2.CHAIN_APPROX_SIMPLE)
        cv2.drawContours(cv_image, contours, -1, (0,0,0), 3)
        if(contours):
            cv2.drawContours(cv_image, contours, -1, (0,0,0), 3)
            cnt = contours[0]
            area = cv2.contourArea(cnt)
            #print area 
            if ...
(more)
edit retag flag offensive close merge delete

Comments

1

It might be a good idea to point out, which lines cause the traces. Nobody wants to count lines here. Usually the error messages point to the problem.

dornhege gravatar image dornhege  ( 2014-06-25 07:03:35 -0600 )edit

in node 1 its in permanent_stop(): cmd.publish(motion) and in node 2 its self.a = x1*x1

jashanvir gravatar image jashanvir  ( 2014-06-25 07:08:36 -0600 )edit

1 Answer

Sort by ยป oldest newest most voted
2

answered 2014-06-25 07:13:24 -0600

dornhege gravatar image

The publisher cmd is permanent_stop is probably not connected as it is just constructed a couple of lines before.

For the second error: x1 simply does not exist in this function. You probably want to refer to self.x1 everywhere.

edit flag offensive delete link more

Comments

In node 1. so how can connect the publisher. i mean where i can write that command. in node 2 . I tried that. and got AttributeError: ground instance has no attribute 'x1'

jashanvir gravatar image jashanvir  ( 2014-06-25 07:22:02 -0600 )edit

1. This usually happens once when you initialize the node and then you'll pass that around. It is common to put things like this in a class so you don't mess with global variables.

dornhege gravatar image dornhege  ( 2014-06-25 07:46:30 -0600 )edit

2. You'll have to set that first and/or make sure that it is set.

dornhege gravatar image dornhege  ( 2014-06-25 07:46:45 -0600 )edit

can you tell me about the second error. Node 2. ?

jashanvir gravatar image jashanvir  ( 2014-06-30 06:33:59 -0600 )edit
1

You need to set `self.x1` somewhere. You only set `x1`.

dornhege gravatar image dornhege  ( 2014-06-30 06:52:25 -0600 )edit

what if i dont use oop ... can i use this by def where1(msg): x1=msg.data def where2(msg): y1=msg.data def ground(): rospy.init_node("ground") rospy.Subscriber("/box_positiona", Float64, where1) rospy.Subscriber("/box_positionb", Float64, where2) print x1 print y1

jashanvir gravatar image jashanvir  ( 2014-06-30 07:08:02 -0600 )edit

Code in a comment is unreadable. As these are basic python questions unrelated to ROS I'd suggest getting familiar with python, especially scoping, first.

dornhege gravatar image dornhege  ( 2014-06-30 07:58:44 -0600 )edit

Question Tools

2 followers

Stats

Asked: 2014-06-25 06:52:59 -0600

Seen: 1,737 times

Last updated: Dec 09 '16