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

Userdata change type

asked 2016-10-10 04:38:52 -0500

pv gravatar image

updated 2016-10-10 05:03:36 -0500

Hi,

I try to du a small smach tree and pass a class between the state with userdata, the code look like this

class Auto_move_info(object):
        __pallet_position = None
        __pallet_place_position = None

    def __init__(self, pallet_position=None, pallet_place_position=None):
        self.__pallet_position = pallet_position
        self.__pallet_place_position = pallet_place_position

    def set_pallet_position(self, pallet_position):
        self.__pallet_position = pallet_position

    def get_pallet_position(self):
        return self.__pallet_position

    def set_pallet_place_position(self, pallet_place_position):
        self.__pallet_place_position = pallet_place_position

    def get_pallet_place_position(self):
        return self.__pallet_place_position

class Get_pallet_position(smach.State):
    def __init__(self):
        smach.State.__init__(self, outcomes=['pallet_position_found'],
                            output_keys=['get_pallet_position_op'])
        self.move_pallet_A_to_X = Auto_move_info('A','X')

    def execute(self, userdata):
        time.sleep(2)   
        userdata.get_pallet_position_op = self.move_pallet_A_to_X
        print ("1")
        print(self.move_pallet_A_to_X)
        print ("2")
        rospy.loginfo('Get_pallet_position')
        return 'pallet_position_found'

class Calculate_pallet_position(smach.State):
    def __init__(self):
        smach.State.__init__(self, outcomes=['calculation_pallet_done'],
                            input_keys=['calculate_pallet_position_ip'],
                            output_keys=['calculate_pallet_position_op'])

    def execute(self, userdata):
        time.sleep(2)
        print ("3")
        print(userdata.calculate_pallet_position_ip)
        print ("4")
        rospy.loginfo('Executing state Calculate_pallet_position')
        return 'calculation_pallet_done'

The result the become:

[INFO] [WallTime: 1476091443.061490] State machine starting in initial state 'Get_pallet_position' with userdata: 
    []
1
<class_definition.Auto_move_info object at 0xb59ced2c>
2
[INFO] [WallTime: 1476091445.064946] Get_pallet_position
[INFO] [WallTime: 1476091445.065734] State machine transitioning 'Get_pallet_position':'pallet_position_found'-->'Calculate_pallet_position'
3
<smach.user_data.Const object at 0xb6158b4c>
4
[INFO] [WallTime: 1476091447.070927] Executing state Calculate_pallet_position

Why does the object move_pallet_A_to_X change from an Auto_move_info object to a Const?

edit retag flag offensive close merge delete

2 Answers

Sort by ยป oldest newest most voted
0

answered 2016-10-12 02:09:24 -0500

pv gravatar image

It works, but I wounder if I can have any problem with that the input_key and the output_key has the same name? Should I do in some other way? Right now I have declared both like input and output but only one of them is remapping with another state.

class Calculate_pallet_position(smach.State):
    def __init__(self):
        smach.State.__init__(self, outcomes=['calculation_pallet_done'],
                            input_keys=['calculate_pallet_position_ip','calculate_pallet_position_op'],
                            output_keys=['calculate_pallet_position_ip','calculate_pallet_position_op'])


#-------------------------------------------------------------------------------    
    # Create the sub SMACH state machine 
        sm_go_to_pallet = smach.StateMachine(outcomes=['go_to_pallet_done'])

        # Open the container 
        with sm_go_to_pallet:

            # Add states to the container 
            smach.StateMachine.add('Get_pallet_position', Get_pallet_position(),
                                   transitions={'pallet_position_found':'Calculate_pallet_position'},
                                   remapping={'get_pallet_position_op':'calculate_pallet_position_ip'})

            smach.StateMachine.add('Calculate_pallet_position', Calculate_pallet_position(),
                                   transitions={'calculation_pallet_done':'Move_forklift_to_pallet'},
                                   remapping={'calculate_pallet_position_op':'move_forklift_to_pallet_ip'})
edit flag offensive delete link more

Comments

1

It is completely fine to use a userdata key as input and output. If you need to use a custom class instantiation in userdata, this reflects the fact that calling methods on this object might have side effects.

Philipp Schillinger gravatar image Philipp Schillinger  ( 2016-10-12 13:18:07 -0500 )edit

Thanks for the answers!

pv gravatar image pv  ( 2016-10-13 08:27:23 -0500 )edit
1

answered 2016-10-11 16:39:29 -0500

Not sure if this is the reason here, but could very well be the case: If a userdata key is only declared as input key, but not as output key, it is read-only. This could explain why it appears as Const object.

Could you try to declare it as output key as well in the second state?

edit flag offensive delete link more

Question Tools

1 follower

Stats

Asked: 2016-10-10 04:38:52 -0500

Seen: 383 times

Last updated: Oct 12 '16