RobotControl.cpp
Go to the documentation of this file.
1 /*
2 * This file is part of ArmarX.
3 *
4 * ArmarX is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License version 2 as
6 * published by the Free Software Foundation.
7 *
8 * ArmarX is distributed in the hope that it will be useful, but
9 * WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License
14 * along with this program. If not, see <http://www.gnu.org/licenses/>.
15 *
16 * @package ArmarX::
17 * @author Mirko Waechter ( mirko.waechter at kit dot edu)
18 * @date 2012
19 * @copyright http://www.gnu.org/licenses/gpl-2.0.txt
20 * GNU General Public License
21 */
22 
23 #include "RobotControl.h"
24 
28 
30 
31 namespace armarx
32 {
33  void
35  {
36  addState<Statechart_Robot>("RobotControl");
37  }
38 
39  void
41  {
43  }
44 
45  void
47  {
48  removeInstance(stateId);
49  robotFunctionalState = NULL;
50  }
51 
52  // this function creates an instance of the robot-statechart,
53  // so there is always one running-instance active that can
54  // be controlled by the GUI
55  void
57  {
58 
59 
60  task = new RunningTask<RobotControl>(this, &RobotControl::createStaticInstance);
61  task->start();
62  }
63 
64  void
65  RobotControl::hardReset(const Ice::Current&)
66  {
67  removeInstance(robotFunctionalState->getLocalUniqueId());
69  }
70 
73  {
75  }
76 
77  void
78  RobotControl::createStaticInstance()
79  {
80  getObjectScheduler()->waitForObjectState(eManagedIceObjectStarted, 5000);
81  stateId = createRemoteStateInstance("RobotControl", NULL, "RobotStatechart", "");
82  std::map<int, StateBasePtr> stateList = getChildStatesByName(stateId, "Functional");
83  ARMARX_INFO << "Functional State Id:" << stateList.begin()->first << flush;
84  assert(stateList.size() > 0);
85  int robotFunctionalStateId = stateList.begin()->first;
86  robotFunctionalState = stateList.begin()->second;
87  callRemoteState(stateId, StringVariantContainerBaseMap());
88 
89  const std::string proxyName = getProperty<std::string>("XMLStatechartProfile").getValue() +
90  getProperty<std::string>("proxyName").getValue();
91  const std::string stateName = getProperty<std::string>("stateName").getValue();
92  ARMARX_IMPORTANT << VAROUT(proxyName) << VAROUT(stateName);
93 
94  if (!proxyName.empty() && !stateName.empty())
95  {
96  auto statebase = robotFunctionalState->findSubstateByName("DynamicRemoteState");
97  ARMARX_CHECK_EXPRESSION(statebase);
98  DynamicRemoteStatePtr state = DynamicRemoteStatePtr::dynamicCast(statebase);
99 
100  if (!state)
101  {
102  ARMARX_ERROR << "dynamic state pointer null";
103  return;
104  }
105 
106  state->getObjectScheduler()->waitForObjectState(eManagedIceObjectStarted, 5000);
107  EventPtr evt = createEvent<EvLoadScenario>();
108  evt->eventReceiverName = "toAll";
109  evt->properties["proxyName"] = new SingleVariant(proxyName);
110  evt->properties["stateName"] = new SingleVariant(stateName);
111  issueEvent(robotFunctionalStateId, evt);
112  }
113  }
114 
115  void
117  {
118  }
119 
120  void
122  {
123  //add substates
124  StateBasePtr stateFunctional = setInitState(addState<StateRobotControl>("Functional"));
125  StateBasePtr stateStopped = addState<State>("Stopped");
126  StateBasePtr stateFailure = addState<State>("Failure");
127 
128  // add transitions
129  addTransition<Failure>(stateFunctional, stateFailure);
130  addTransition<EvStopRobot>(stateFunctional, stateStopped);
131  addTransition<EvStartRobot>(stateStopped, stateFunctional);
132  addTransition<EvStartRobot>(stateFailure, stateFunctional);
133  }
134 
135  void
137  {
138  // add substates
139  StateBasePtr stateIdling = addState<State>("Idling");
140  StateBasePtr stateRobotPreInitialized =
141  addState<RobotPreInitialized>("RobotPreInitialized");
142  StateBasePtr stateRobotInitialized = addState<State>("RobotInitialized");
143  StateBasePtr stateScenarioRunning = addDynamicRemoteState("DynamicScenarioState");
144  StateBasePtr stateFatalError = addState<FailureState>("FatalError");
145 
146  setInitState(stateRobotInitialized);
147  // add transitions
148  addTransition<EvInit>(stateIdling, stateRobotPreInitialized);
149  addTransition<EvInitialized>(stateRobotPreInitialized, stateRobotInitialized);
150  addTransition<EvLoadScenario>(stateRobotInitialized,
151  stateScenarioRunning,
152  PM::createMapping()->mapFromEvent("*", "*"));
153  addTransition<EvInit>(stateScenarioRunning, stateRobotPreInitialized);
154  addTransition<Success>(stateScenarioRunning, stateRobotInitialized);
155 
156  // failure transitions
157  addTransition<EvLoadingFailed>(stateScenarioRunning, stateFatalError);
158  addTransition<EvConnectionLost>(stateScenarioRunning, stateFatalError);
159  addTransition<EvInitFailed>(stateRobotPreInitialized, stateFatalError);
160  addTransitionFromAllStates<Failure>(stateFatalError);
161  }
162 
163  void
165  {
166  // install global running conditions for the robot (e.g. temperature < xx°C)
167  // KinematicUnitObserverInterfacePrx prx = getContext()->getProxy<KinematicUnitObserverInterfacePrx>("KinematicUnitObserver");
168  // installCondition<Failure>(Expression::create()
169  // ->add(channels::KinematicUnitObserver::jointTemperatures.getDatafield("KinematicUnitObserver", "NECK_JOINT0"), checks::KinematicUnitObserver::larger->getCheckStr(), 50.f));
170  }
171 
173  {
174  }
175 
176  void
178  {
179  // issue init on remote components and install condition "RobotInitialized"
180  }
181 
182 } // namespace armarx
armarx::RemoteStateOffererBase::createRemoteStateInstance
int createRemoteStateInstance(const std::string &stateName, const RemoteStateIceBasePrx &remoteStatePrx, const std::string &parentStateItentifierStr, const std::string &instanceName, const Ice::Current &context=Ice::emptyCurrent) override
Definition: RemoteStateOfferer.cpp:99
armarx::EventPtr
IceInternal::Handle< Event > EventPtr
Typedef of EventPtr as IceInternal::Handle<Event> for convenience.
Definition: Event.h:40
ARMARX_IMPORTANT
#define ARMARX_IMPORTANT
Definition: Logging.h:190
armarx::RobotControl::hardReset
void hardReset(const Ice::Current &=Ice::emptyCurrent) override
Definition: RobotControl.cpp:65
armarx::RobotPreInitialized::RobotPreInitialized
RobotPreInitialized()
Definition: RobotControl.cpp:172
armarx::RobotControl::onInitRemoteStateOfferer
void onInitRemoteStateOfferer() override
Pure virtual function, in which the states must be added, that should be remote-accessable.
Definition: RobotControl.cpp:34
armarx::ParameterMapping::createMapping
static ParameterMappingPtr createMapping()
Creates a new instance of a ParameterMapping. Since the constructors are private, this method must be...
Definition: ParameterMapping.cpp:573
armarx::RobotControl::robotFunctionalState
StateBasePtr robotFunctionalState
Refernce to the currently active Functionsl state.
Definition: RobotControl.h:70
armarx::RemoteStateOffererBase::removeInstance
void removeInstance(int stateId, const Ice::Current &=Ice::emptyCurrent) override
Definition: RemoteStateOfferer.cpp:457
armarx::RunningTask
Definition: ArmarXMultipleObjectsScheduler.h:36
armarx::StateRobotControl::onEnter
void onEnter() override
Definition: RobotControl.cpp:164
armarx::RemoteStateOffererBase::issueEvent
void issueEvent(int receivingStateId, const EventBasePtr &evt, const Ice::Current &=Ice::emptyCurrent) override
Definition: RemoteStateOfferer.cpp:479
armarx::RemoteStateOffererBase::getChildStatesByName
std::map< int, StateBasePtr > getChildStatesByName(int parentId, std::string stateName)
Definition: RemoteStateOfferer.cpp:617
IceInternal::Handle
Definition: forward_declarations.h:8
armarx::RobotControl::startRobotStatechart
void startRobotStatechart()
Definition: RobotControl.cpp:56
armarx::flush
const LogSender::manipulator flush
Definition: LogSender.h:251
armarx::RobotControl::createPropertyDefinitions
PropertyDefinitionsPtr createPropertyDefinitions() override
Definition: RobotControl.cpp:72
armarx::RobotControl::onConnectRemoteStateOfferer
void onConnectRemoteStateOfferer() override
Virtual function, in which the user can fetch some proxies.
Definition: RobotControl.cpp:40
RobotControl.h
ARMARX_ERROR
#define ARMARX_ERROR
Definition: Logging.h:196
armarx::Statechart_Robot::defineSubstates
void defineSubstates() override
Definition: RobotControl.cpp:121
armarx::RobotControlContextProperties
Definition: RobotControl.h:35
armarx::RobotPreInitialized::onEnter
void onEnter() override
Definition: RobotControl.cpp:177
ExpressionException.h
armarx::Component::getConfigIdentifier
std::string getConfigIdentifier()
Retrieve config identifier for this component as set in constructor.
Definition: Component.cpp:79
armarx::StateRobotControl::defineSubstates
void defineSubstates() override
Definition: RobotControl.cpp:136
ARMARX_CHECK_EXPRESSION
#define ARMARX_CHECK_EXPRESSION(expression)
This macro evaluates the expression and if it turns out to be false it will throw an ExpressionExcept...
Definition: ExpressionException.h:73
armarx::RemoteStateOffererBase::callRemoteState
void callRemoteState(int stateId, const StringVariantContainerBaseMap &properties, const Ice::Current &context=Ice::emptyCurrent) override
Definition: RemoteStateOfferer.cpp:185
ARMARX_INFO
#define ARMARX_INFO
Definition: Logging.h:181
VAROUT
#define VAROUT(x)
Definition: StringHelpers.h:198
IceUtil::Handle
Definition: forward_declarations.h:30
armarx::PropertyDefinitionsPtr
IceUtil::Handle< class PropertyDefinitionContainer > PropertyDefinitionsPtr
PropertyDefinitions smart pointer type.
Definition: forward_declarations.h:35
DynamicRemoteState.h
armarx::Statechart_Robot::defineState
void defineState() override
Definition: RobotControl.cpp:116
armarx
This file offers overloads of toIce() and fromIce() functions for STL container types.
Definition: ArmarXTimeserver.cpp:27
armarx::ManagedIceObject::getObjectScheduler
ArmarXObjectSchedulerPtr getObjectScheduler() const
Definition: ManagedIceObject.cpp:776
KinematicUnitObserver.h
armarx::RobotControl::onExitRemoteStateOfferer
void onExitRemoteStateOfferer() override
Virtual function, in which the user can implement some clean up.
Definition: RobotControl.cpp:46
ChannelRef.h