locked_robot_state.h
Go to the documentation of this file.
1 /*********************************************************************
2  * Software License Agreement (BSD License)
3  *
4  * Copyright (c) 2008, Willow Garage, Inc.
5  * Copyright (c) 2014, SRI International
6  * All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  *
12  * * Redistributions of source code must retain the above copyright
13  * notice, this list of conditions and the following disclaimer.
14  * * Redistributions in binary form must reproduce the above
15  * copyright notice, this list of conditions and the following
16  * disclaimer in the documentation and/or other materials provided
17  * with the distribution.
18  * * Neither the name of Willow Garage nor the names of its
19  * contributors may be used to endorse or promote products derived
20  * from this software without specific prior written permission.
21  *
22  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
23  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
24  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
25  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
26  * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
27  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
28  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
29  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
30  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
32  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
33  * POSSIBILITY OF SUCH DAMAGE.
34  *********************************************************************/
35 
36 /* Author: Ioan Sucan, Acorn Pooley */
37 
38 #pragma once
39 
42 #include <boost/function.hpp>
43 #include <boost/thread.hpp>
44 
45 namespace robot_interaction
46 {
47 MOVEIT_CLASS_FORWARD(LockedRobotState); // Defines LockedRobotStatePtr, ConstPtr, WeakPtr... etc
48 
50 //
51 // Only allow one thread to modify the RobotState at a time.
52 //
53 // Allow any thread access to the RobotState when it is not being modified. If
54 // a (const) reference to the robot state is held when the RobotState needs to
55 // be modified, a copy is made and the copy is modified. Any externally held
56 // references will be out of date but still valid.
57 //
58 // The RobotState can only be modified by passing a callback function which
59 // does the modification.
60 class LockedRobotState
61 {
62 public:
64  LockedRobotState(const moveit::core::RobotModelPtr& model);
65 
66  virtual ~LockedRobotState();
67 
69  //
70  // This state may go stale, meaning the maintained state has been updated,
71  // but it will never be modified while the caller is holding a reference to
72  // it.
73  //
74  // The transforms in the returned state will always be up to date.
75  moveit::core::RobotStateConstPtr getState() const;
76 
78  void setState(const moveit::core::RobotState& state);
79 
80  // This is a function that can modify the maintained state.
81  typedef boost::function<void(moveit::core::RobotState*)> ModifyStateFunction;
82 
83  // Modify the state.
84  //
85  // This modifies the state by calling \e modify on it.
86  // The \e modify function is passed a reference to the state which it can
87  // modify. No threads will be given access to the state while the \e modify
88  // function is running.
89  void modifyState(const ModifyStateFunction& modify);
90 
91 protected:
92  // This is called when the internally maintained state has changed.
93  // This is called with state_lock_ unlocked.
94  // Default definition does nothing. Override to get notification of state
95  // change.
96  // TODO: is this needed?
97  virtual void robotStateChanged();
98 
99 protected:
100  // this locks all accesses to the state_ member.
101  // The lock can also be used by subclasses to lock additional fields.
102  mutable boost::mutex state_lock_;
103 
104 private:
105  // The state maintained by this class.
106  // When a modify function is being called this is NULL.
107  // PROTECTED BY state_lock_
108  moveit::core::RobotStatePtr state_;
109 };
110 } // namespace robot_interaction
robot_interaction::LockedRobotState::state_lock_
boost::mutex state_lock_
Definition: locked_robot_state.h:135
robot_interaction::LockedRobotState::modifyState
void modifyState(const ModifyStateFunction &modify)
Definition: locked_robot_state.cpp:79
robot_interaction::LockedRobotState::~LockedRobotState
virtual ~LockedRobotState()
robot_interaction::LockedRobotState::robotStateChanged
virtual void robotStateChanged()
Definition: locked_robot_state.cpp:95
moveit::core::RobotState
robot_state.h
robot_interaction::LockedRobotState::ModifyStateFunction
boost::function< void(moveit::core::RobotState *)> ModifyStateFunction
Definition: locked_robot_state.h:114
robot_interaction::LockedRobotState::LockedRobotState
LockedRobotState(const moveit::core::RobotState &state)
Definition: locked_robot_state.cpp:41
robot_interaction::MOVEIT_CLASS_FORWARD
MOVEIT_CLASS_FORWARD(InteractionHandler)
robot_interaction::LockedRobotState::setState
void setState(const moveit::core::RobotState &state)
Set the state to the new value.
Definition: locked_robot_state.cpp:62
class_forward.h
robot_interaction::LockedRobotState::getState
moveit::core::RobotStateConstPtr getState() const
get read-only access to the state.
Definition: locked_robot_state.cpp:56
robot_interaction
Definition: interaction.h:54
robot_interaction::LockedRobotState::state_
moveit::core::RobotStatePtr state_
Definition: locked_robot_state.h:141


robot_interaction
Author(s): Ioan Sucan
autogenerated on Sat Mar 15 2025 02:26:54