StateParameterExample.cpp
Go to the documentation of this file.
1/*
2* This file is part of ArmarX.
3*
4* Copyright (C) 2011-2016, High Performance Humanoid Technologies (H2T), Karlsruhe Institute of Technology (KIT), all rights reserved.
5*
6* ArmarX is free software; you can redistribute it and/or modify
7* it under the terms of the GNU General Public License version 2 as
8* published by the Free Software Foundation.
9*
10* ArmarX is distributed in the hope that it will be useful, but
11* WITHOUT ANY WARRANTY; without even the implied warranty of
12* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13* GNU General Public License for more details.
14*
15* You should have received a copy of the GNU General Public License
16* along with this program. If not, see <http://www.gnu.org/licenses/>.
17*
18* @package ArmarXCore::StatechartExamples::StateParameterExample
19* @author Mirko Waechter (mirko.waechter at kit dot edu)
20* @date 2012
21* @copyright http://www.gnu.org/licenses/gpl-2.0.txt
22* GNU General Public License
23*/
24
26
28
29namespace armarx
30{
31
32
33 void
35 {
36
37 std::cout << "onInitComponent" << std::endl;
39 Statechart_StateParameterExample::createInstance("Statechart_StateParameterExample"));
40 }
41
42 void
44 {
45 // ARMARX_LOG << eINFO << "Starting StateParameterExample" << flush;
46
47 // StringParameterBaseMap input;
48 //// input["x"] = new VariantParameter(3.f);
49 //// input["y"] = new VariantParameter(4.f);
50 //// input["timeout"] = new VariantParameter(3);
51 // if(statechart->isInitialized())
52 // statechart->start(&input); // if the initial state requires input parameters, pass them as a StringParameterBaseMap to start()
53 // else{
54 // ARMARX_LOG << eERROR << "Cannot start statechart. Statechart '" << statechart->getStateName() << "' was not initialized.\n" << flush;
55 // }
56 }
57
58 void
62
63 void
67
68 void
70 {
71 //add substates
72 StatePtr stateRun = addState<StateRun>("Running");
73 setInitState(stateRun,
75 ->mapFromParent("*", "*")
76 ->mapFromParent("timeout", "timeout2")
77 ->mapFromParent("x", "x")
78 ->mapFromParent("y", "y")
79 ->mapFromParent("ObjectList", "ObjectList")
80 ->setSourcePriority(3, eOutput)
81 ->setSourcePriority(2, eParent));
82 StatePtr stateResult = addState<StateResult>("Result");
83 StatePtr stateSuccess = addState<SuccessState>(
84 "Success"); // preimplemented state derived from FinalState, that sends triggers a transition on the upper state
85 StatePtr stateFailure = addState<FailureState>(
86 "Failure"); // preimplemented state derived from FinalState, that sends triggers a transition on the upper state
87
88
89 // add transitions
90 addTransition<EvNext>(stateRun, stateResult, createMapping()->mapFromOutput("*", "my.*"));
91 addTransition<EvNext>(stateResult, stateSuccess, createMapping()->mapFromOutput("*", "*"));
92
93
94 // ...add more transitions
95 }
96
97 void
99 {
100
101
102 // add input
103 addToInput("timeout", VariantType::Int, false);
104 addToInput("x", VariantType::Float, true);
105 addToInput("y", VariantType::Float, true);
106 addToInput("ObjectList", VariantType::List(VariantType::String), false);
107
108
109 // add output
110 addToOutput("result", VariantType::Float, true);
111 }
112
113 void
115 {
116 //ARMARX_INFO << "Pos: " << getInput<Variant>("Position")->getLinkedPosition()->x << endl;
117 }
118
119 void
121 {
122 //add input
123 addToInput("timeout2", VariantType::Int, false);
124 addToInput("x", VariantType::Float, true);
125 addToInput("y", VariantType::Float, true);
126 addToInput("ObjectList", VariantType::List(VariantType::String), false);
127
128 // add output
129 addToOutput("result", VariantType::Float, true);
130 }
131
132 void
134 {
135 // call is optional, just a simple output that this state was entered
136 setOutput("result", Variant(getInput<float>("x") + getInput<float>("y")));
138 }
139
140 void
142 {
143 }
144
145 void
147 {
149 }
150
151 void
153 {
154 //add input
155 addToInput("my.result", VariantType::Float, false);
156 // add output
157 addToOutput("result", VariantType::Float, true);
158 }
159
160 void
162 {
163 setOutput("result", getInput<float>("my.result"));
164 ARMARX_INFO << "my.result: " << getInput<float>("my.result") << flush;
166 }
167
168
169} // namespace armarx
void onInitStatechart() override
onInitStatechart this method is called when the statechart is started.
void onConnectStatechart() override
onConnectStatechart is called before armarx::StatechartContext::startStatechart() and after armarx::S...
static IceInternal::Handle< Statechart_StateParameterExample > createInstance(std::string stateName="")
void removeCondition(ConditionIdentifier conditionId)
Utility function to remove an installed condition on the distributed conditionhandler.
EventPtr createEvent()
Utility function to create a new Event.
Definition StateUtil.h:63
void sendEvent(const EventPtr event, StateBasePtr eventProcessor=nullptr)
Function to send an event to a specific state from an onEnter()-function. Must not be called anywhere...
Definition StateUtil.cpp:40
void setOutput(std::string const &key, const Variant &value)
setOuput() sets an output parameter of this state.
Definition State.cpp:482
StateBasePtr addState(StateBasePtr pNewState)
Definition State.cpp:140
bool addToInput(const std::string &key, const ContainerType &type, bool optional, VariantContainerBasePtr defaultValue=VariantContainerBasePtr())
Adds a key,type-pair to the input parameters.
Definition State.cpp:309
std::enable_if_t< std::is_base_of_v< VariantDataClass, T >, IceInternal::Handle< T > > getInput(const std::string &key) const
getInput can be used to access a specific input parameter.
Definition State.h:620
StateBasePtr setInitState(StateBasePtr initState, ParameterMappingPtr initialStateMapping=ParameterMappingPtr())
Sets the initial substate of this state.
Definition State.cpp:354
TransitionIceBase & addTransition(EventPtr event, StateIceBasePtr sourceState, StateIceBasePtr destinationState, ParameterMappingIceBasePtr mappingToNextStatesInput=nullptr, ParameterMappingIceBasePtr mappingToParentStatesLocal=nullptr, ParameterMappingIceBasePtr mappingToParentStatesOutput=nullptr)
Definition State.cpp:250
bool addToOutput(const std::string &key, VariantTypeId type, bool optional)
Adds a key,type-pair to the output parameters.
Definition State.cpp:319
bool setToplevelState(const armarx::StatePtr &newToplevelState, StringVariantContainerBaseMap startParameters=StringVariantContainerBaseMap())
setToplevelState initializes newToplevelState with the current StatechartContext and the current Stat...
#define ARMARX_INFO
The normal logging level.
Definition Logging.h:181
const VariantTypeId String
Definition Variant.h:921
const VariantTypeId Int
Definition Variant.h:917
const VariantTypeId Float
Definition Variant.h:919
const VariantContainerType List
This file offers overloads of toIce() and fromIce() functions for STL container types.
IceInternal::Handle< State > StatePtr
Definition State.h:44
const LogSender::manipulator flush
Definition LogSender.h:251
ParameterMappingPtr createMapping()
Returns a new and empty instance of ParameterMapping.
void defineParameters() override
Virtual function, in which input/local/output parameters can be specified.
void onEnter() override
Virtual function, in which the behaviour of state is defined, when it is entered. Can be overridden,...
void defineParameters() override
Virtual function, in which input/local/output parameters can be specified.
void onBreak() override
Virtual function, in which the behaviour of state is defined, when it is abnormally exited....
ConditionIdentifier condId
void onEnter() override
Virtual function, in which the behaviour of state is defined, when it is entered. Can be overridden,...
void onExit() override
Virtual function, in which the behaviour of state is defined, when it is exited. Can be overridden,...
void defineParameters() override
Virtual function, in which input/local/output parameters can be specified.
void onEnter() override
Virtual function, in which the behaviour of state is defined, when it is entered. Can be overridden,...
void defineState() override
Virtual function, in which this state can be configured.
void defineSubstates() override
Virtual function, in which substates, transition and mappings can be added.