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 
25 #include "StateParameterExample.h"
26 
28 
29 namespace 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
60  {
61  }
62 
63  void
65  {
66  }
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")));
137  sendEvent<EvNext>();
138  }
139 
140  void
142  {
143  }
144 
145  void
147  {
148  removeCondition(condId);
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;
165  sendEvent(createEvent<EvNext>());
166  }
167 
168 
169 } // namespace armarx
armarx::VariantType::Float
const VariantTypeId Float
Definition: Variant.h:919
armarx::VariantType::List
const VariantContainerType List
Definition: SingleTypeVariantList.h:204
armarx::StateRun::onEnter
void onEnter() override
Definition: StatechartPerformanceTest.cpp:129
armarx::Statechart_StateParameterExample::onEnter
void onEnter() override
Definition: StateParameterExample.cpp:114
armarx::StateRun::defineParameters
void defineParameters() override
Definition: StatechartPerformanceTest.cpp:119
armarx::StateResult::defineParameters
void defineParameters() override
Definition: StateParameterExample.cpp:152
armarx::createMapping
ParameterMappingPtr createMapping()
Returns a new and empty instance of ParameterMapping.
Definition: ParameterMapping.cpp:251
armarx::Statechart_StateParameterExample::defineSubstates
void defineSubstates() override
Definition: StateParameterExample.cpp:69
IceInternal::Handle< State >
armarx::StateResult::onEnter
void onEnter() override
Definition: StateParameterExample.cpp:161
armarx::Statechart_StateParameterExample::defineState
void defineState() override
Definition: StateParameterExample.cpp:64
armarx::StatechartContext::setToplevelState
bool setToplevelState(const armarx::StatePtr &newToplevelState, StringVariantContainerBaseMap startParameters=StringVariantContainerBaseMap())
setToplevelState initializes newToplevelState with the current StatechartContext and the current Stat...
Definition: StatechartContext.cpp:195
armarx::StateRun::condId
ConditionIdentifier condId
Definition: StatechartPerformanceTest.h:90
armarx::StateParameterExample::onInitStatechart
void onInitStatechart() override
onInitStatechart this method is called when the statechart is started.
Definition: StateParameterExample.cpp:34
armarx::StateRun::onBreak
void onBreak() override
Definition: StateParameterExample.cpp:141
armarx::flush
const LogSender::manipulator flush
Definition: LogSender.h:251
armarx::Statechart_StateParameterExample::defineParameters
void defineParameters() override
Definition: StateParameterExample.cpp:98
armarx::StateParameterExample::run
void run()
Definition: StateParameterExample.cpp:59
StateParameterExample.h
ARMARX_INFO
#define ARMARX_INFO
Definition: Logging.h:181
armarx::VariantType::Int
const VariantTypeId Int
Definition: Variant.h:917
armarx::StateRun::onExit
void onExit() override
Definition: StatechartPerformanceTest.cpp:172
armarx::StateTemplate< Statechart_StateParameterExample >::createInstance
static IceInternal::Handle< StateType > createInstance(std::string stateName="")
Creates a new state instance of the type of the template parameter.
Definition: StateTemplate.h:98
armarx::VariantType::String
const VariantTypeId String
Definition: Variant.h:921
ArmarXDataPath.h
armarx::StateParameterExample::onConnectStatechart
void onConnectStatechart() override
onConnectStatechart is called before armarx::StatechartContext::startStatechart() and after armarx::S...
Definition: StateParameterExample.cpp:43
armarx
This file offers overloads of toIce() and fromIce() functions for STL container types.
Definition: ArmarXTimeserver.cpp:27