Ask Your Question
0

Extract single values from Joy

asked 2017-04-15 09:17:50 -0500

marcoresk gravatar image

updated 2017-04-15 09:32:07 -0500

I created a class, in which I want to subscribe both to image_raw and Joy topics, then I want to combine the data to draw/write something on the image with openCV. My code works and is able to

  • correctly subscribe image_raw

  • correctly subscribe Joy

  • publish some extracted values from Joy (PS3 Joystick)

  • write things on the image with openCV.

The code:

class image_converter:

  def __init__(self):
    self.image_pub = rospy.Publisher("image_topic_2",Image,queue_size=10)
    self.bridge = CvBridge()
    self.image_sub = rospy.Subscriber("usb_cam/image_raw",Image,self.callback)

  def callback(self,data):
    try:
      cv_image = self.bridge.imgmsg_to_cv2(data, "bgr8")
      gray = cv2.cvtColor(cv_image,cv2.COLOR_BGR2GRAY)
      start = time.time() #simulate FPS count
      global myjoy_p
      global myjoy
      myjoy3 = rospy.Subscriber("joy", Joy, self.joyread)
      myjoy_p = rospy.Publisher("copy_topic", Int16MultiArray,queue_size=1) 
      #AA = myjoy_p.data[1] THIS IS MY QUESTION, SEE BELOW  
      BB = 7.5
      cv2.putText(cv_image, 'JOY: {0}'.format(int(BB)),(40,300),cv2.FONT_HERSHEY_SIMPLEX,0.5,(0,0,255))
      elapsed = time.time() - start
      cv2.putText(cv_image, 'FPS: {0}'.format(int(1 / elapsed)),(10,40),cv2.FONT_HERSHEY_SIMPLEX,0.5,(0,0,255))
      cv2.imshow("Modified Image", cv_image)
      cv2.waitKey(1)
    except CvBridgeError as e:
    print(e)

try:
  self.image_pub.publish(self.bridge.cv2_to_imgmsg(cv_image, "bgr8"))
except CvBridgeError as e:
  print(e)

  def joyread(self,data):
    global myjoy
    #Declaration type
    myjoy = Int16MultiArray()
    myjoy1 = Int16MultiArray()
    myjoy1.data.append(int(255*data.axes[0])) #LEFT LEFTWARD
    myjoy1.data.append(int(255*data.axes[1])) #LEFT UPWARD
    myjoy1.data.append(int(255*data.axes[2])) #RIGHT LEFTWARD
    myjoy1.data.append(int(255*data.axes[3])) #RIGHT UPWARD
    myjoy = myjoy1
    #publish
    myjoy_p.publish(myjoy1)

This code works, even if there are 4 different myjoy variables (myjoy, myjoy_p, myjoy1, myjoy3) because I'm not a ROS expert and I made a lot of tries.

The question: I would like to extract one of the values from myjoy and store the value in AA, then use AA instead of BB. This is what I tried to do in the commented line

   AA = myjoy_p.data[1]

The problem: I can not access to myjoy values out of the joyread callback. the error message is

AttributeError: 'Publisher' object has no attribute 'data'

I tried all of myjoy variables. They are different object types (the error message changes a bit) but none of them seems to work. What I have to do? Thank you in advance. (ROS Indigo and Python script)

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
0

answered 2017-04-15 10:08:48 -0500

marcoresk gravatar image

I finally found the solution. Myjoy mantains joy structure in the array. It was

AA = int(255*myjoy.axes[0])

and all other myjoy were quite useless.

edit flag offensive delete link more

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools

1 follower

Stats

Asked: 2017-04-15 09:17:50 -0500

Seen: 374 times

Last updated: Apr 15 '17