Ask Your Question

jashanvir's profile - activity

2019-07-09 20:03:36 -0500 received badge  Nice Question (source)
2017-10-15 03:34:28 -0500 marked best answer Return a value from a callback function

Hello !

I want to have a value to be returned when I call a callback function while subscribing to a topic. I am using ROS-Hydro I have wrote a demo code that what I want. Please also suggest some new way to achieve this if callback function can't return anything !

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)
    get value of y from callback function when it is returning 
    y = z
    return z            
def callback(data):
    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)
           cv2.imshow("Image window", cv_image)
           return y
def my_function()
           x=cv_bridge()
           if x = 1: 
               return "true"
           else:
                perform the task

def pilot():
rospy.init_node("pilot")
    my_value = my_function()
    if my_value = true:
               stop everything...
    else: do the above process again
     rospy.spin()
if __name__ == '__main__':
    pilot()

Thanks a lot ! Looking forward for answers !

2017-04-20 17:41:25 -0500 marked best answer Identify the white section of a binary image

I want to track the white section of the binary image. When the white section is detected (which is cuboid in my case) it should get the height and width and the value of left and top most pixel.

Then after getting the coordinates i want to assign them to ROI. so that i can then subscribe to /roi topic and perform a particular action.

Thanx

2016-12-09 22:10:28 -0500 received badge  Student (source)
2016-05-08 15:08:23 -0500 marked best answer AttributeError: 'NoneType' object has no attribute 'strip' ?

Hello ! I am trying to install asctec_drivers so that I can control the quadrotor through ros in morse simulator.

When I try to run the rosdep install asctec_drivers i get the following error

ERROR: Rosdep experienced an error: 'NoneType' object has no attribute 'strip' Please go to the rosdep page [1] and file a bug report with the stack trace below. [1] : 
rosdep version: 0.10.27

Traceback (most recent call last):   File "/usr/local/lib/python2.7/dist-packages/rosdep2/main.py", line 121, in rosdep_main
    exit_code = _rosdep_main(args)   File "/usr/local/lib/python2.7/dist-packages/rosdep2/main.py", line 279, in _rosdep_main
    return _package_args_handler(command, parser, options, args)   File "/usr/local/lib/python2.7/dist-packages/rosdep2/main.py", line 335, in _package_args_handler
    val = rospkg.expand_to_packages(args, rospack, rosstack)   File "/usr/local/lib/python2.7/dist-packages/rospkg/rospack.py", line 419, in expand_to_packages
    package_list = rospack.list()   File "/usr/local/lib/python2.7/dist-packages/rospkg/rospack.py", line 179, in list
    self._update_location_cache()   File "/usr/local/lib/python2.7/dist-packages/rospkg/rospack.py", line 171, in _update_location_cache
    list_by_path(self._manifest_name, path, cache)   File "/usr/local/lib/python2.7/dist-packages/rospkg/rospack.py", line 68, in list_by_path
    resource_name = root.findtext('name').strip(' \n\r\t') AttributeError: 'NoneType' object has no attribute 'strip'

I would really appreciate if anybody can tell me how to overcome this error Thanks

2014-10-30 12:22:29 -0500 received badge  Famous Question (source)
2014-07-29 07:20:51 -0500 received badge  Notable Question (source)
2014-07-29 07:20:51 -0500 received badge  Famous Question (source)
2014-07-28 09:48:54 -0500 received badge  Notable Question (source)
2014-07-28 09:48:54 -0500 received badge  Popular Question (source)
2014-07-18 19:03:46 -0500 received badge  Famous Question (source)
2014-07-16 20:20:19 -0500 received badge  Notable Question (source)
2014-07-16 20:20:19 -0500 received badge  Famous Question (source)
2014-07-03 05:55:29 -0500 commented answer How to use variable's value (subscriber function) in another function

Yeah ! I know.. But I solved the problem by putting all these in one class.

2014-07-02 06:07:55 -0500 received badge  Famous Question (source)
2014-07-01 11:22:46 -0500 received badge  Famous Question (source)
2014-07-01 11:22:46 -0500 received badge  Famous Question (source)
2014-07-01 10:07:26 -0500 commented answer How to use variable's value (subscriber function) in another function

@McMurdo no it shows the same error, but this time when i terminate the program.

2014-07-01 10:03:16 -0500 commented answer How to use variable's value (subscriber function) in another function

@McMurdo no, it doesnt help... it prints the value when i terminate the program using ctrl+c

2014-07-01 06:02:11 -0500 commented answer How to use variable's value (subscriber function) in another function

how to get the values from the callback function and use them ? Can you give me the reference or a simple demo code which i can use. ! @dornhege

2014-07-01 04:44:45 -0500 received badge  Famous Question (source)
2014-07-01 04:44:09 -0500 received badge  Notable Question (source)
2014-06-30 09:11:44 -0500 received badge  Popular Question (source)
2014-06-30 08:45:19 -0500 commented answer How to use variable's value (subscriber function) in another function

But when i check by rostopic echo /box_positiona then it prints the data ?

2014-06-30 07:51:35 -0500 answered a question How to break and come of the for loop ?

I have tried this and it worked for me.

#!/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

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
        global flag

        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 area > 500:
                dis = timer*0.4
                my_var1= a+dis
                my_var2 = b
                my_var3 = c
                flag = 1

        cv2.imshow('mask',mask)
            cv2.imshow('res',res)
        cv2.imshow("Image window", cv_image)
        cv2.waitKey(3)

def stop(x):
        global flag
        now=time.time()
        global timer
        print 'stop'    
        cmd = rospy.Publisher("/quadrotor/rotorcraftvelocity", Twist)   
        while timer !=x:
            motion = Twist()
            motion.linear.x = +0.0
            motion.linear.y = +0.0
            motion.linear.z = +0.0
            cmd.publish(motion) 
            end = time.time()
            timer = round(end-now)

def left():
        global flag
        now = time.time()
        global timer
        print 'left'
        global b
        cmd = rospy.Publisher("/quadrotor/rotorcraftvelocity", Twist)   
        while timer !=10:
            if flag !=1:    
                motion = Twist()        
                motion.linear.y = -0.4
                cmd.publish(motion)     
                end = time.time()
                timer = round(end-now)
            if flag ==1:
                print 'flag', flag
                print('i was here')
                break
        if flag !=1:
            b = b-4 

def right():
        global flag
        now=time.time()
        global timer
        print 'right'
        global b
        cmd = rospy.Publisher("/quadrotor/rotorcraftvelocity", Twist)
        while timer !=5:
            if flag !=1:                
                motion = Twist()
                motion.linear.y = +0.4
                cmd.publish(motion) 
                end = time.time()
                timer = round(end-now)
            if flag == 1:
                print 'flag', flag
                print('i was here')
                break
        if flag !=1 ...
(more)
2014-06-30 07:21:38 -0500 asked a question How to use variable's value (subscriber function) in another function

Hello all !

I want to pass the variable which is set to its value when called the callback function. and then want to use the value of the variable in another function.

def where1(msg):
    global x1
    x1 = msg.data

def where2(gsm):
    global y1
    y1 = gsm.data
def ground():
    global x1
    global y1
    rospy.init_node("ground")
    rospy.Subscriber("/box_positiona", Float64, where1)
    rospy.Subscriber("/box_positionb", Float64, where2)

    print x1
    print y1
    rospy.spin() # this will block untill you hit Ctrl+C

if __name__ == '__main__':
    try:
            ground()
    except rospy.ROSInterruptException:
            rospy.loginfo("Ground  motion node is shut down.")

But i got the following error print x1 NameError: global name 'x1' is not defined

2014-06-30 07:08:02 -0500 commented answer Handling two nodes

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

2014-06-30 06:33:59 -0500 commented answer Handling two nodes

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

2014-06-27 17:34:40 -0500 received badge  Notable Question (source)
2014-06-27 10:17:47 -0500 received badge  Scholar (source)
2014-06-27 07:23:12 -0500 received badge  Popular Question (source)
2014-06-27 06:19:13 -0500 received badge  Popular Question (source)
2014-06-27 06:01:22 -0500 commented answer How to break and come of the for loop ?

I have one doubt. Does the callback or bridge_opencv() acts like thread. Because when run the code. The flag is set to 1 inside it but it somehow does not remain one when it comes out of it.

2014-06-27 05:34:29 -0500 commented answer How to break and come of the for loop ?

Thanks a lot for your suggestion ! I thought I have problem in opencv function as the flag is not set in there.

2014-06-27 04:34:01 -0500 asked a question How to break and come of the for loop ?

Hello guys ! I am working on Morse and ROS- Hydro version. I want to break and come out of the for loop when my flag is set to 1 ( and this flag is set in opencv part) once the flag is set, it should enter an another function and print 'flag is set' The following is the code:-

#!/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
flag =0
#def permanent_stop():
#   my_value = 1
#   stop(x=5)
#   return 1
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 area > 6000:
                print('i found the object')
                dis = timer*0.4
                #print 'dis',dis
                my_var1= a+dis
                my_var2 = b
                my_var3 = c
                flag = 1
                coorda = Float64()
                coordb = Float64()
                coordc = Float64()
                #my_value2 = False
        cv2.imshow('mask',mask)
            cv2.imshow('res',res)
        cv2.imshow("Image window", cv_image)
        cv2.waitKey(3)

def stop(x):
        now=time.time()
        global timer
        print 'stop'    
        cmd = rospy.Publisher("/quadrotor/rotorcraftvelocity", Twist)   
        while timer !=x:
            motion = Twist()
            motion.linear.x = +0.0
            motion.linear.y = +0.0
            motion.linear.z = +0.0
            cmd.publish(motion) 
            end = time.time()
            timer = round(end-now)

def left():
        now = time.time()
        global timer
        print 'left'
        global b
        cmd = rospy.Publisher("/quadrotor/rotorcraftvelocity", Twist)   
        while timer !=10:   
            motion = Twist()        
            motion.linear.y = -0.4
            cmd.publish(motion)     
            end = time.time()
            timer = round(end-now)
        b = b-4 

def right ...
(more)
2014-06-25 07:22:02 -0500 commented answer Handling two nodes

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'

2014-06-25 07:08:36 -0500 received badge  Commentator
2014-06-25 07:08:36 -0500 commented question Handling two nodes

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

2014-06-25 06:52:59 -0500 asked a question Handling two nodes

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)