-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathFSM_State.h
152 lines (122 loc) · 4 KB
/
FSM_State.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
#ifndef FSM_State_H
#define FSM_State_H
#include <stdio.h>
#include "ControlFSMData.h"
#include "Controllers/GaitScheduler.h"
#include "TransitionData.h"
#include <dynamic_reconfigure/server.h>
#include <ros/ros.h>
#include <thread>
// #include <be2r_cmpc_unitree/ros_dynamic_paramsConfig.h>
// Normal robot states
#define K_PASSIVE 0
#define K_STAND_UP 1
#define K_BALANCE_VBL 2
#define K_LAY_DOWN 7
#define K_BALANCE_STAND 3
#define K_LOCOMOTION 4
#define K_LOCOMOTION_TEST 5
#define K_RECOVERY_STAND 6
#define K_VISION 8
#define K_BACKFLIP 9
#define K_FRONTJUMP 11
#define K_TESTING 12
#define K_TESTING_CV 14
// Specific control states
#define K_JOINT_PD 51
#define K_IMPEDANCE_CONTROL 52
#define K_INVALID 100
#define DEG_TO_RAD M_PI / 180.0
#define RAD_TO_DEG 180.0 / M_PI
/**
* Enumerate all of the FSM states so we can keep track of them.
*/
enum class FSM_StateName
{
INVALID,
PASSIVE,
JOINT_PD,
IMPEDANCE_CONTROL,
STAND_UP,
LAYDOWN,
BALANCE_STAND,
LOCOMOTION,
RECOVERY_STAND,
VISION,
BACKFLIP,
FRONTJUMP,
TESTING,
BALANCE_VBL,
TESTING_CV
};
/**
*
*/
template<typename T>
class FSM_State
{
public:
EIGEN_MAKE_ALIGNED_OPERATOR_NEW
// Generic constructor for all states
FSM_State(ControlFSMData<T>* _controlFSMData, FSM_StateName stateNameIn, std::string stateStringIn);
// Behavior to be carried out when entering a state
virtual void onEnter() = 0; // {}
// Run the normal behavior for the state
virtual void run() = 0; //{}
// Manages state specific transitions
virtual FSM_StateName checkTransition() { return FSM_StateName::INVALID; }
// Runs the transition behaviors and returns true when done transitioning
virtual TransitionData<T> transition() { return transitionData; }
// Behavior to be carried out when exiting a state
virtual void onExit() = 0; // {}
//
void jointPDControl(int leg, Vec3<T> qDes, Vec3<T> qdDes);
void cartesianImpedanceControl(int leg, Vec3<T> pDes, Vec3<T> vDes, Vec3<double> kp_cartesian, Vec3<double> kd_cartesian);
void footstepHeuristicPlacement(int leg);
Vec3<T> findAngles(uint8_t leg_num, Vec3<T> p_act);
//
void runControls();
// void runBalanceController();
// void runWholeBodyController();
// void runConvexModelPredictiveController();
// void runRegularizedPredictiveController();
//
void turnOnAllSafetyChecks();
void turnOffAllSafetyChecks();
// Holds all of the relevant control data
ControlFSMData<T>* _data;
// FSM State info
FSM_StateName stateName; // enumerated name of the current state
FSM_StateName nextStateName; // enumerated name of the next state
std::string stateString; // state name string
// Transition parameters
T transitionDuration; // transition duration time
T tStartTransition; // time transition starts
TransitionData<T> transitionData;
// Pre controls safety checks
bool checkSafeOrientation = false; // check roll and pitch
bool checkJointLimits = false; // check joint limits
// Post control safety checks
bool checkPDesFoot = false; // do not command footsetps too far
bool checkForceFeedForward = false; // do not command huge forces
bool checkLegSingularity = false; // do not let leg
// Leg controller command placeholders for the whole robot (3x4 matrices)
Mat34<T> jointFeedForwardTorques; // feed forward joint torques
Mat34<T> jointPositions; // joint angle positions
Mat34<T> jointVelocities; // joint angular velocities
Mat34<T> footFeedForwardForces; // feedforward forces at the feet
Mat34<T> footPositions; // cartesian foot positions
Mat34<T> footVelocities; // cartesian foot velocities
// Footstep locations for next step
Mat34<T> footstepLocations;
// Higher level Robot body controllers
// BalanceController balanceController;
// ModelPredictiveController cMPC
// RegularizedPredictiveController RPC
private:
// Create the cartesian P gain matrix
Mat3<float> kpMat;
// Create the cartesian D gain matrix
Mat3<float> kdMat;
};
#endif // FSM_State_H