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

Subscribe to a topic with flag

asked 2014-08-28 03:17:18 -0500

guodi gravatar image

updated 2014-08-28 12:27:59 -0500

I have a gripper embedded with a sensor in the finger. The sensor will tell if there is something between the gripper by True/False in 500 Hz. So I hope the gripper to go to successive positions to detect if there is something between the gripper, if there the sensor data is true, a marker is drawn in the rviz. I imitate some codes and modified as follows(I write it in a general way). While it seems scan_sub can't subscribe the data properly, I am confused with when the scan_sub begins to subscribe and when it is shut down. I have also write some notes in the following code which I am confused about.....Hope some one could kindly help me with that ...

import scan_execute
class PlanServer:
    def __init__(self):
        ......
    rospy.Service('scan_object', Empty, scan_object_callback)
    self.sc = scan_execute.ScanExecute()

    def scan_object_callback(self, req):
        result = False
        result = self.sc.pretouch_scan()

***the scan_execute file***
class ScanExecute:
    def __init__(self):
        ......
    def pretouch_scan(self):
        self.detected = False
        self.start_time = rospy.Time.now()
        self.scan_sub = rospy.Subcsriber("sensor/data", SensorData, sensor_callback)
        # what does this *while* mean for ....
        while not self.detected:
            rospy.sleep(0.5)
        return True

    def sensor_callback(msg):
        time_limit = rospy.Duration(10.0)
        time_past = rospy.Time.now() - self.start_time

        if time_past > time_limit:
            self.detected = True  # when *detected* is True, will the *scan_object_callback* return True?
            self.count = 0
            self.scan_sub.unregister()
        else:
            self.count += 1

        # I hope the gripper of robot will go to the successively position for 5 times.
        # and when it detect the sensor data, a marker is drawn in rviz
            self.current_mat = self.current_mat * self.step_mat
            self.moveit_cmd.go(current_mat, wait = True)
            if msg.data
                self.detected = True
                self.draw_marker()
                if self.count > 5:
                    self.detected = True
edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
0

answered 2014-08-28 03:58:16 -0500

BennyRe gravatar image

updated 2014-08-28 03:58:44 -0500

The two detected variables are in two different scopes. You have to use a global variable, if you want to solve the problem like this.

Your count variable will also not work as intended. Have a look at pythons variable scoping. It seems you haven't understood it yet completely.

edit flag offensive delete link more

Comments

Yes, you are right. I just tried to simply the code to make it clearer, however it may be more confused. I am sorry for that...Here I modify the codes. Actually they are from two files....Could you please help me see if there are some problems now. Thank you !

guodi gravatar image guodi  ( 2014-08-28 12:31:20 -0500 )edit

Looks good to me at a first glance.

BennyRe gravatar image BennyRe  ( 2014-08-29 01:44:03 -0500 )edit

Question Tools

1 follower

Stats

Asked: 2014-08-28 03:17:18 -0500

Seen: 306 times

Last updated: Aug 28 '14