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"
27 
28 namespace armarx
29 {
30 
31 
32 
34  {
35 
36  std::cout << "onInitComponent" << std::endl;
37  setToplevelState(Statechart_StateParameterExample::createInstance("Statechart_StateParameterExample"));
38 
39  }
40 
41 
43  {
44  // ARMARX_LOG << eINFO << "Starting StateParameterExample" << flush;
45 
46  // StringParameterBaseMap input;
47  //// input["x"] = new VariantParameter(3.f);
48  //// input["y"] = new VariantParameter(4.f);
49  //// input["timeout"] = new VariantParameter(3);
50  // if(statechart->isInitialized())
51  // statechart->start(&input); // if the initial state requires input parameters, pass them as a StringParameterBaseMap to start()
52  // else{
53  // ARMARX_LOG << eERROR << "Cannot start statechart. Statechart '" << statechart->getStateName() << "' was not initialized.\n" << flush;
54  // }
55 
56  }
57 
59  {
60  }
61 
63  {
64  }
65 
67  {
68  //add substates
69  StatePtr stateRun = addState<StateRun>("Running");
70  setInitState(stateRun, createMapping()
71  ->mapFromParent("*", "*")
72  ->mapFromParent("timeout", "timeout2")
73  ->mapFromParent("x", "x")
74  ->mapFromParent("y", "y")
75  ->mapFromParent("ObjectList", "ObjectList")
76  ->setSourcePriority(3, eOutput)
77  ->setSourcePriority(2, eParent));
78  StatePtr stateResult = addState<StateResult>("Result");
79  StatePtr stateSuccess = addState<SuccessState>("Success"); // preimplemented state derived from FinalState, that sends triggers a transition on the upper state
80  StatePtr stateFailure = addState<FailureState>("Failure"); // preimplemented state derived from FinalState, that sends triggers a transition on the upper state
81 
82 
83  // add transitions
84  addTransition<EvNext>(stateRun, stateResult,
85  createMapping()->mapFromOutput("*", "my.*"));
86  addTransition<EvNext>(stateResult, stateSuccess,
87  createMapping()->mapFromOutput("*", "*"));
88 
89 
90  // ...add more transitions
91  }
92 
94  {
95 
96 
97  // add input
98  addToInput("timeout", VariantType::Int, false);
99  addToInput("x", VariantType::Float, true);
100  addToInput("y", VariantType::Float, true);
101  addToInput("ObjectList", VariantType::List(VariantType::String), false);
102 
103 
104 
105  // add output
106  addToOutput("result", VariantType::Float, true);
107 
108  }
109 
111  {
112  //ARMARX_INFO << "Pos: " << getInput<Variant>("Position")->getLinkedPosition()->x << endl;
113 
114  }
115 
117  {
118  //add input
119  addToInput("timeout2", VariantType::Int, false);
120  addToInput("x", VariantType::Float, true);
121  addToInput("y", VariantType::Float, true);
122  addToInput("ObjectList", VariantType::List(VariantType::String), false);
123 
124  // add output
125  addToOutput("result", VariantType::Float, true);
126 
127  }
128 
129  void StateRun::onEnter()
130  {
131  // call is optional, just a simple output that this state was entered
132  setOutput("result", Variant(getInput<float>("x") + getInput<float>("y")));
133  sendEvent<EvNext>();
134 
135  }
136 
138  {
139 
140  }
141 
142  void StateRun::onExit()
143  {
144  removeCondition(condId);
145 
146  }
147 
149  {
150  //add input
151  addToInput("my.result", VariantType::Float, false);
152  // add output
153  addToOutput("result", VariantType::Float, true);
154 
155  }
156 
158  {
159  setOutput("result", getInput<float>("my.result"));
160  ARMARX_INFO << "my.result: " << getInput<float>("my.result") << flush;
161  sendEvent(createEvent<EvNext>());
162 
163  }
164 
165 
166 }
armarx::VariantType::Float
const VariantTypeId Float
Definition: Variant.h:918
armarx::VariantType::List
const VariantContainerType List
Definition: SingleTypeVariantList.h:191
armarx::StateRun::onEnter
void onEnter() override
Definition: StatechartPerformanceTest.cpp:122
armarx::Statechart_StateParameterExample::onEnter
void onEnter() override
Definition: StateParameterExample.cpp:110
armarx::StateRun::defineParameters
void defineParameters() override
Definition: StatechartPerformanceTest.cpp:113
armarx::StateResult::defineParameters
void defineParameters() override
Definition: StateParameterExample.cpp:148
armarx::createMapping
ParameterMappingPtr createMapping()
Returns a new and empty instance of ParameterMapping.
Definition: ParameterMapping.cpp:235
armarx::Statechart_StateParameterExample::defineSubstates
void defineSubstates() override
Definition: StateParameterExample.cpp:66
IceInternal::Handle< State >
armarx::StateResult::onEnter
void onEnter() override
Definition: StateParameterExample.cpp:157
armarx::Statechart_StateParameterExample::defineState
void defineState() override
Definition: StateParameterExample.cpp:62
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:180
armarx::StateRun::condId
ConditionIdentifier condId
Definition: StatechartPerformanceTest.h:86
armarx::StateParameterExample::onInitStatechart
void onInitStatechart() override
onInitStatechart this method is called when the statechart is started.
Definition: StateParameterExample.cpp:33
armarx::StateRun::onBreak
void onBreak() override
Definition: StateParameterExample.cpp:137
armarx::flush
const LogSender::manipulator flush
Definition: LogSender.h:251
armarx::Statechart_StateParameterExample::defineParameters
void defineParameters() override
Definition: StateParameterExample.cpp:93
armarx::StateParameterExample::run
void run()
Definition: StateParameterExample.cpp:58
StateParameterExample.h
ARMARX_INFO
#define ARMARX_INFO
Definition: Logging.h:174
armarx::VariantType::Int
const VariantTypeId Int
Definition: Variant.h:916
armarx::StateRun::onExit
void onExit() override
Definition: StatechartPerformanceTest.cpp:160
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:95
armarx::VariantType::String
const VariantTypeId String
Definition: Variant.h:920
ArmarXDataPath.h
armarx::StateParameterExample::onConnectStatechart
void onConnectStatechart() override
onConnectStatechart is called before armarx::StatechartContext::startStatechart() and after armarx::S...
Definition: StateParameterExample.cpp:42
armarx
This file offers overloads of toIce() and fromIce() functions for STL container types.
Definition: ArmarXTimeserver.cpp:28