dart
StateMachine.hpp
1 /*
2  * Copyright (c) 2011-2021, The DART development contributors
3  * All rights reserved.
4  *
5  * The list of contributors can be found at:
6  * https://github.com/dartsim/dart/blob/master/LICENSE
7  *
8  * This file is provided under the following "BSD-style" License:
9  * Redistribution and use in source and binary forms, with or
10  * without modification, are permitted provided that the following
11  * conditions are met:
12  * * Redistributions of source code must retain the above copyright
13  * notice, this list of conditions and the following disclaimer.
14  * * Redistributions in binary form must reproduce the above
15  * copyright notice, this list of conditions and the following
16  * disclaimer in the documentation and/or other materials provided
17  * with the distribution.
18  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
19  * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
20  * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
21  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
22  * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
23  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
25  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
26  * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
27  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
29  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30  * POSSIBILITY OF SUCH DAMAGE.
31  */
32 
33 #ifndef EXAMPLES_ATLASSIMBICON_STATEMACHINE_HPP_
34 #define EXAMPLES_ATLASSIMBICON_STATEMACHINE_HPP_
35 
36 #include <string>
37 #include <vector>
38 
39 #include <Eigen/Dense>
40 
41 #include <dart/dart.hpp>
42 
43 class State;
44 
47 {
48 public:
50  explicit StateMachine(const std::string& _name);
51 
53  virtual ~StateMachine();
54 
55  //------------------------------- Setting ------------------------------------
57  void setName(const std::string& _name);
58 
60  const std::string& getName() const;
61 
63  void addState(State* _state);
64 
66  void setInitialState(State* _state);
67 
68  //------------------------------- Control ------------------------------------
71  void begin(double _currentTime);
72 
74  void computeControlForce(double _dt);
75 
78  void end(double _currentTime);
79 
82 
84  void transiteToNextState(double _currentTime);
85 
87  void transiteTo(State* _state, double _currentTime);
88 
90  void transiteTo(std::string& _stateName, double _currentTime);
91 
93  void transiteTo(std::size_t _idx, double _currentTime);
94 
96  void setVerbosity(bool verbosity);
97 
98 protected:
100  std::string mName;
101 
103  std::vector<State*> mStates;
104 
107 
109  double mBeginTime;
110 
112  double mEndTime;
113 
115  int mFrame;
116 
118  double mElapsedTime;
119 
120 private:
122  bool _containState(const State* _state) const;
123 
125  bool _containState(const std::string& _name) const;
126 
128  State* _findState(const std::string& _name) const;
129 
131  bool mVerbosity;
132 };
133 
134 #endif // EXAMPLES_ATLASSIMBICON_STATEMACHINE_HPP_
void transiteTo(State *_state, double _currentTime)
Change state to _state.
Definition: StateMachine.cpp:144
const std::string & getName() const
Get name.
Definition: StateMachine.cpp:74
StateMachine for Atlas robot.
Definition: StateMachine.hpp:46
void computeControlForce(double _dt)
Compute control force and apply it to Atlas robot.
Definition: StateMachine.cpp:108
double mBeginTime
Started time.
Definition: StateMachine.hpp:109
void setVerbosity(bool verbosity)
Set the verbosity.
Definition: StateMachine.cpp:185
double mEndTime
Stopped time.
Definition: StateMachine.hpp:112
double mElapsedTime
Elapsed time which is stopped time minus started time.
Definition: StateMachine.hpp:118
StateMachine(const std::string &_name)
Constructor.
Definition: StateMachine.cpp:46
State * getCurrentState()
Get current state.
Definition: StateMachine.cpp:132
std::string mName
Name.
Definition: StateMachine.hpp:100
class State
Definition: State.hpp:65
void addState(State *_state)
Add state.
Definition: StateMachine.cpp:80
std::vector< State * > mStates
States.
Definition: StateMachine.hpp:103
void setName(const std::string &_name)
Set name.
Definition: StateMachine.cpp:68
virtual ~StateMachine()
Destructor.
Definition: StateMachine.cpp:59
State * mCurrentState
Current state.
Definition: StateMachine.hpp:106
void begin(double _currentTime)
Initiate state.
Definition: StateMachine.cpp:98
int mFrame
Frame number.
Definition: StateMachine.hpp:115
void setInitialState(State *_state)
Set initial state.
Definition: StateMachine.cpp:89
void transiteToNextState(double _currentTime)
Transite to the next state manually.
Definition: StateMachine.cpp:138
void end(double _currentTime)
Finalize state.
Definition: StateMachine.cpp:124