00001 /********************************************************************* 00002 * Software License Agreement (BSD License) 00003 * 00004 * Copyright (c) 2008, Willow Garage, Inc. 00005 * Copyright (c) 2014, SRI International 00006 * All rights reserved. 00007 * 00008 * Redistribution and use in source and binary forms, with or without 00009 * modification, are permitted provided that the following conditions 00010 * are met: 00011 * 00012 * * Redistributions of source code must retain the above copyright 00013 * notice, this list of conditions and the following disclaimer. 00014 * * Redistributions in binary form must reproduce the above 00015 * copyright notice, this list of conditions and the following 00016 * disclaimer in the documentation and/or other materials provided 00017 * with the distribution. 00018 * * Neither the name of Willow Garage nor the names of its 00019 * contributors may be used to endorse or promote products derived 00020 * from this software without specific prior written permission. 00021 * 00022 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 00023 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 00024 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS 00025 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE 00026 * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, 00027 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 00028 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 00029 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER 00030 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 00031 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN 00032 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 00033 * POSSIBILITY OF SUCH DAMAGE. 00034 *********************************************************************/ 00035 00036 /* Author: Ioan Sucan, Acorn Pooley */ 00037 00038 #ifndef MOVEIT_ROBOT_INTERACTION_LOCKED_ROBOT_STATE_ 00039 #define MOVEIT_ROBOT_INTERACTION_LOCKED_ROBOT_STATE_ 00040 00041 #include <moveit/robot_state/robot_state.h> 00042 #include <boost/function.hpp> 00043 #include <boost/thread.hpp> 00044 00045 namespace robot_interaction 00046 { 00047 00049 // 00050 // Only allow one thread to modify the RobotState at a time. 00051 // 00052 // Allow any thread access to the RobotState when it is not being modified. If 00053 // a (const) reference to the robot state is held when the RobotState needs to 00054 // be modified, a copy is made and the copy is modified. Any externally held 00055 // references will be out of date but still valid. 00056 // 00057 // The RobotState can only be modified by passing a callback function which 00058 // does the modification. 00059 class LockedRobotState 00060 { 00061 public: 00062 LockedRobotState(const robot_state::RobotState& state); 00063 LockedRobotState(const robot_model::RobotModelPtr& model); 00064 00065 virtual ~LockedRobotState(); 00066 00068 // 00069 // This state may go stale, meaning the maintained state has been updated, 00070 // but it will never be modified while the caller is holding a reference to 00071 // it. 00072 // 00073 // The transforms in the returned state will always be up to date. 00074 robot_state::RobotStateConstPtr getState() const; 00075 00077 void setState(const robot_state::RobotState& state); 00078 00079 // This is a function that can modify the maintained state. 00080 typedef boost::function<void (robot_state::RobotState*)> ModifyStateFunction; 00081 00082 // Modify the state. 00083 // 00084 // This modifies the state by calling \e modify on it. 00085 // The \e modify function is passed a reference to the state which it can 00086 // modify. No threads will be given access to the state while the \e modify 00087 // function is running. 00088 void modifyState(const ModifyStateFunction& modify); 00089 00090 protected: 00091 // This is called when the internally maintained state has changed. 00092 // This is called with state_lock_ unlocked. 00093 // Default definition does nothing. Override to get notification of state 00094 // change. 00095 // TODO: is this needed? 00096 virtual void robotStateChanged(); 00097 00098 protected: 00099 // this locks all accesses to the state_ member. 00100 // The lock can also be used by subclasses to lock additional fields. 00101 mutable boost::mutex state_lock_; 00102 00103 private: 00104 // The state maintained by this class. 00105 // When a modify function is being called this is NULL. 00106 // PROTECTED BY state_lock_ 00107 robot_state::RobotStatePtr state_; 00108 }; 00109 00110 typedef boost::shared_ptr<LockedRobotState> LockedRobotStatePtr; 00111 typedef boost::shared_ptr<const LockedRobotState> LockedRobotStateConstPtr; 00112 00113 } 00114 00115 #endif