-
Notifications
You must be signed in to change notification settings - Fork 1
/
ConvexMPCLocomotion.h
185 lines (153 loc) · 4.15 KB
/
ConvexMPCLocomotion.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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
#ifndef CHEETAH_SOFTWARE_CONVEXMPCLOCOMOTION_H
#define CHEETAH_SOFTWARE_CONVEXMPCLOCOMOTION_H
#include "Gait.h"
#include "cppTypes.h"
#include <ControlFSMData.h>
#include <FootSwingTrajectory.h>
#include <SparseCMPC/SparseCMPC.h>
#include <geometry_msgs/PoseStamped.h>
#include <nav_msgs/Path.h>
#include <ros/ros.h>
#include <visualization_msgs/Marker.h>
#include <cstdio>
using Eigen::Array4f;
using Eigen::Array4i;
template<typename T>
struct CMPC_Result
{
LegControllerCommand<T> commands[4];
Vec4<T> contactPhase;
};
struct CMPC_Jump
{
static constexpr int START_SEG = 6;
static constexpr int END_SEG = 0;
static constexpr int END_COUNT = 2;
bool jump_pending = false;
bool jump_in_progress = false;
bool pressed = false;
int seen_end_count = 0;
int last_seg_seen = 0;
int jump_wait_counter = 0;
void debug(int seg)
{
(void)seg;
// printf("[%d] pending %d running %d\n", seg, jump_pending, jump_in_progress);
}
void trigger_pressed(int seg, bool trigger)
{
(void)seg;
if (!pressed && trigger)
{
if (!jump_pending && !jump_in_progress)
{
jump_pending = true;
// printf("jump pending @ %d\n", seg);
}
}
pressed = trigger;
}
bool should_jump(int seg)
{
debug(seg);
if (jump_pending && seg == START_SEG)
{
jump_pending = false;
jump_in_progress = true;
// printf("jump begin @ %d\n", seg);
seen_end_count = 0;
last_seg_seen = seg;
return true;
}
if (jump_in_progress)
{
if (seg == END_SEG && seg != last_seg_seen)
{
seen_end_count++;
if (seen_end_count == END_COUNT)
{
seen_end_count = 0;
jump_in_progress = false;
// printf("jump end @ %d\n", seg);
last_seg_seen = seg;
return false;
}
}
last_seg_seen = seg;
return true;
}
last_seg_seen = seg;
return false;
}
};
class ConvexMPCLocomotion
{
public:
EIGEN_MAKE_ALIGNED_OPERATOR_NEW
ConvexMPCLocomotion(float _dt, int _iterations_between_mpc, ControlFSMData<float>* data);
void initialize();
template<typename T>
void run(ControlFSMData<T>& data);
bool currently_jumping = false;
Vec3<float> pBody_des;
Vec3<float> vBody_des;
Vec3<float> aBody_des;
Vec3<float> pBody_RPY_des;
Vec3<float> vBody_Ori_des;
Vec3<float> pFoot_des[4];
Vec3<float> vFoot_des[4];
Vec3<float> aFoot_des[4];
Vec3<float> Fr_des[4];
Vec4<float> contact_state;
private:
void _SetupCommand(ControlFSMData<float>& data);
void recompute_timing(int iterations_per_mpc);
void updateMPCIfNeeded(int* mpcTable, ControlFSMData<float>& data, bool omniMode);
void solveDenseMPC(int* mpcTable, ControlFSMData<float>& data);
void solveSparseMPC(int* mpcTable, ControlFSMData<float>& data);
void initSparseMPC();
ControlFSMData<float>* _fsm_data;
float _yaw_turn_rate;
float _yaw_des;
float _roll_des;
float _pitch_des;
float _x_vel_des = 0.;
float _y_vel_des = 0.;
// High speed running
// float _body_height = 0.34;
float _body_height = 0.29;
float _body_height_running = 0.29;
float _body_height_jumping = 0.36;
int _iterationsBetweenMPC;
be2r_cmpc_unitree::ros_dynamic_paramsConfig* _dyn_params = nullptr;
int _gait_period;
int horizonLength;
int default_iterations_between_mpc;
float dt;
float dtMPC;
int iterationCounter = 0;
Vec3<float> f_ff[4];
Vec4<float> swingTimes;
FootSwingTrajectory<float> footSwingTrajectories[4];
OffsetDurationGait trotting, bounding, pronking, jumping, galloping, standing, trotRunning, walking, walking2, pacing;
MixedFrequncyGait random, random2;
Mat3<float> Kp, Kd, Kp_stance, Kd_stance;
bool firstRun = true;
bool firstSwing[4];
float swingTimeRemaining[4];
float stand_traj[6];
int current_gait;
int gaitNumber;
Vec3<float> world_position_desired;
Vec3<float> rpy_int;
Vec3<float> rpy_comp;
float x_comp_integral = 0;
Vec3<float> pFoot[4];
CMPC_Result<float> result;
float trajAll[12 * 36];
ros::NodeHandle _nh;
CMPC_Jump jump_state;
vectorAligned<Vec12<double>> _sparseTrajectory;
SparseCMPC _sparseCMPC;
};
#endif // CHEETAH_SOFTWARE_CONVEXMPCLOCOMOTION_H