-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathuser_func.hpp
More file actions
340 lines (297 loc) · 12.3 KB
/
user_func.hpp
File metadata and controls
340 lines (297 loc) · 12.3 KB
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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
#ifndef USER_FUNC_HPP
#define USER_FUNC_HPP
#include <float.h>
#include <math.h>
#include <chrono>
#include <cmath>
#include <ctime>
#include <memory>
#include "controller/init_pos.hpp"
#include "ovinf/controller/policy_controller_factory.hpp"
#include "ovinf/robot/efc_mj_common.h"
#include "ovinf/robot/robot_efc_mj.hpp"
using RobotT = ovinf::RobotEfcMj;
enum Events {
InitPose = 1001,
RunPolicy,
EnableStandingPolicy,
EnableWarkingPolicy,
EnableRobustPolicy,
PolicySwitch,
VeloxIncrease = 2001,
VeloxDecrease = 2002,
VeloyIncrease = 2003,
VeloyDecrease = 2004,
VelowIncrease = 2005,
VelowDecrease = 2006,
SetVelX = 2014,
SetVelY = 2015,
SetVelW = 2016,
};
enum class States : bitbot::StateId {
Waiting = 1001,
InitPose,
PolicyRunning,
PolicySwitching,
};
class MakeBitbotEverywhere {
public:
MakeBitbotEverywhere(std::string const &kernel_config,
std::string const &controller_config)
: kernel_(kernel_config) {
logger_ = bitbot::Logger().ConsoleLogger();
YAML::Node config = YAML::LoadFile(controller_config);
switching_time_ = config["RobotConfig"]["switching_time"].as<double>();
// robot
robot_ = std::make_shared<RobotT>(config["RobotConfig"]);
// init controller
init_pos_controller_ = std::make_shared<ovinf::InitPosController>(
robot_, config["RobotConfig"]["init_pos"]);
// Policy net
standing_controller_ =
ovinf::PolicyControllerFactory::CreatePolicyController(
robot_, config["RobotConfig"]["policy_standing"]);
walking_controller_ =
ovinf::PolicyControllerFactory::CreatePolicyController(
robot_, config["RobotConfig"]["policy_walking"]);
robust_controller_ = ovinf::PolicyControllerFactory::CreatePolicyController(
robot_, config["RobotConfig"]["policy_robust"]);
current_policy_controller_ = standing_controller_;
target_policy_controller_ = standing_controller_;
command_.setZero();
}
void WillMake() {
// Config
kernel_.RegisterConfigFunc(
[this](const KernelBus &bus, UserData &) { robot_->GetDevice(bus); });
// Event
kernel_.RegisterEvent(
"init_pose", static_cast<bitbot::EventId>(Events::InitPose),
[this](bitbot::EventValue, UserData &) {
init_pos_controller_->Init();
return static_cast<bitbot::StateId>(States::InitPose);
});
kernel_.RegisterEvent(
"run_policy", static_cast<bitbot::EventId>(Events::RunPolicy),
[this](bitbot::EventValue, UserData &) {
current_policy_controller_ = standing_controller_;
current_policy_controller_->Init();
return static_cast<bitbot::StateId>(States::PolicyRunning);
});
kernel_.RegisterEvent(
"enable_standing_policy",
static_cast<bitbot::EventId>(Events::EnableStandingPolicy),
[this](bitbot::EventValue, UserData &) {
if (current_policy_controller_ == standing_controller_) {
logger_->warn("Standing policy is already enabled");
return static_cast<bitbot::StateId>(States::PolicyRunning);
} else {
logger_->info("Enabling standing policy");
target_policy_controller_ = standing_controller_;
target_policy_controller_->Init();
return static_cast<bitbot::StateId>(States::PolicySwitching);
}
});
kernel_.RegisterEvent(
"enable_warking_policy",
static_cast<bitbot::EventId>(Events::EnableWarkingPolicy),
[this](bitbot::EventValue, UserData &) {
if (current_policy_controller_ == walking_controller_) {
logger_->warn("Walking policy is already enabled");
return static_cast<bitbot::StateId>(States::PolicyRunning);
} else {
logger_->info("Enabling walking policy");
target_policy_controller_ = walking_controller_;
target_policy_controller_->Init();
return static_cast<bitbot::StateId>(States::PolicySwitching);
}
});
kernel_.RegisterEvent(
"enable_robust_policy",
static_cast<bitbot::EventId>(Events::EnableRobustPolicy),
[this](bitbot::EventValue, UserData &) {
if (current_policy_controller_ == robust_controller_) {
logger_->warn("Robust policy is already enabled");
return static_cast<bitbot::StateId>(States::PolicyRunning);
} else {
logger_->info("Enabling robust policy");
target_policy_controller_ = robust_controller_;
target_policy_controller_->Init();
return static_cast<bitbot::StateId>(States::PolicySwitching);
}
});
kernel_.RegisterEvent(
"policy_switch", static_cast<bitbot::EventId>(Events::PolicySwitch),
[this](bitbot::EventValue, UserData &) {
current_policy_controller_->Stop();
current_policy_controller_ = target_policy_controller_;
return static_cast<bitbot::StateId>(States::PolicyRunning);
});
kernel_.RegisterEvent(
"velo_x_increase", static_cast<bitbot::EventId>(Events::VeloxIncrease),
[this](bitbot::EventValue key_state, UserData &) {
if (key_state ==
static_cast<bitbot::EventValue>(bitbot::KeyboardEvent::Up)) {
command_[0] += 0.05;
logger_->info("current velocity: x={}", command_[0]);
}
return std::nullopt;
});
kernel_.RegisterEvent(
"velo_x_decrease", static_cast<bitbot::EventId>(Events::VeloxDecrease),
[this](bitbot::EventValue key_state, UserData &) {
if (key_state ==
static_cast<bitbot::EventValue>(bitbot::KeyboardEvent::Up)) {
command_[0] -= 0.05;
logger_->info("current velocity: x={}", command_[0]);
}
return std::nullopt;
});
kernel_.RegisterEvent(
"velo_y_increase", static_cast<bitbot::EventId>(Events::VeloyIncrease),
[this](bitbot::EventValue key_state, UserData &) {
if (key_state ==
static_cast<bitbot::EventValue>(bitbot::KeyboardEvent::Up)) {
command_[1] += 0.05;
logger_->info("current velocity: y={}", command_[1]);
}
return std::nullopt;
});
kernel_.RegisterEvent(
"velo_y_decrease", static_cast<bitbot::EventId>(Events::VeloyDecrease),
[this](bitbot::EventValue key_state, UserData &) {
if (key_state ==
static_cast<bitbot::EventValue>(bitbot::KeyboardEvent::Up)) {
command_[1] -= 0.05;
logger_->info("current velocity: y={}", command_[1]);
}
return std::nullopt;
});
kernel_.RegisterEvent(
"velo_w_increase", static_cast<bitbot::EventId>(Events::VelowIncrease),
[this](bitbot::EventValue key_state, UserData &) {
if (key_state ==
static_cast<bitbot::EventValue>(bitbot::KeyboardEvent::Up)) {
command_[2] += 0.05;
logger_->info("current velocity: w={}", command_[2]);
}
return std::nullopt;
});
kernel_.RegisterEvent(
"velo_w_decrease", static_cast<bitbot::EventId>(Events::VelowDecrease),
[this](bitbot::EventValue key_state, UserData &) {
if (key_state ==
static_cast<bitbot::EventValue>(bitbot::KeyboardEvent::Up)) {
command_[2] -= 0.05;
logger_->info("current velocity: w={}", command_[2]);
}
return std::nullopt;
});
kernel_.RegisterEvent(
"set_vel_x", static_cast<bitbot::EventId>(Events::SetVelX),
[this](bitbot::EventValue key_state, UserData &) {
double value = *reinterpret_cast<double *>(&key_state);
command_[0] = value;
return std::nullopt;
});
kernel_.RegisterEvent(
"set_vel_y", static_cast<bitbot::EventId>(Events::SetVelY),
[this](bitbot::EventValue key_state, UserData &) {
double value = *reinterpret_cast<double *>(&key_state);
command_[1] = value;
return std::nullopt;
});
kernel_.RegisterEvent(
"set_vel_w", static_cast<bitbot::EventId>(Events::SetVelW),
[this](bitbot::EventValue key_state, UserData &) {
double value = *reinterpret_cast<double *>(&key_state);
command_[2] = value;
logger_->info("current velocity: x={} y={} w={}", command_[0],
command_[1], command_[2]);
return std::nullopt;
});
// State
kernel_.RegisterState(
"waiting", static_cast<bitbot::StateId>(States::Waiting),
[this](const bitbot::KernelInterface &kernel,
Kernel::ExtraData &extra_data, UserData &user_data) {
static bool first = true;
if (first) {
first = false;
robot_->SetExtraData(extra_data);
}
robot_->Observer()->Update();
},
{Events::InitPose});
kernel_.RegisterState(
"init_pose", static_cast<bitbot::StateId>(States::InitPose),
[this](const bitbot::KernelInterface &kernel,
Kernel::ExtraData &extra_data, UserData &user_data) {
robot_->Observer()->Update();
init_pos_controller_->Step();
target_policy_controller_->WarmUp();
robot_->Executor()->ExecuteJointTorque();
},
{Events::RunPolicy});
kernel_.RegisterState(
"policy_running", static_cast<bitbot::StateId>(States::PolicyRunning),
[this](const bitbot::KernelInterface &kernel,
Kernel::ExtraData &extra_data, UserData &user_data) {
robot_->Observer()->Update();
current_policy_controller_->GetCommand() = command_;
current_policy_controller_->Step();
robot_->Executor()->ExecuteJointTorque();
},
{Events::VeloxDecrease, Events::VeloxIncrease, Events::VeloyDecrease,
Events::VeloyIncrease, Events::VelowIncrease, Events::VelowDecrease,
Events::SetVelX, Events::SetVelY, Events::SetVelW,
Events::EnableStandingPolicy, Events::EnableWarkingPolicy,
Events::EnableRobustPolicy});
kernel_.RegisterState(
"policy_switching",
static_cast<bitbot::StateId>(States::PolicySwitching),
[this](const bitbot::KernelInterface &kernel,
Kernel::ExtraData &extra_data, UserData &user_data) {
if (!switching_flag_) {
switching_flag_ = true;
switching_start_time_ = std::chrono::steady_clock::now();
}
double current_switching_time =
std::chrono::duration<double>(std::chrono::steady_clock::now() -
switching_start_time_)
.count();
if (current_switching_time < switching_time_) {
robot_->Observer()->Update();
current_policy_controller_->GetCommand() = command_;
current_policy_controller_->Step();
target_policy_controller_->WarmUp();
robot_->Executor()->ExecuteJointTorque();
} else {
switching_flag_ = false;
kernel.EmitEvent(Events::PolicySwitch, 0);
}
},
{Events::PolicySwitch});
// First state
kernel_.SetFirstState(static_cast<bitbot::StateId>(States::Waiting));
}
void BeMaking() { kernel_.Run(); }
void HaveMade() {
logger_->info("Make BITBOT great forever!!!!!!!!!!!!!!!!");
}
private:
Kernel kernel_;
bitbot::SpdLoggerSharedPtr logger_;
RobotT::Ptr robot_;
ovinf::InitPosController::Ptr init_pos_controller_;
ovinf::PolicyControllerBase::Ptr standing_controller_ = nullptr;
ovinf::PolicyControllerBase::Ptr walking_controller_ = nullptr;
ovinf::PolicyControllerBase::Ptr robust_controller_ = nullptr;
ovinf::PolicyControllerBase::Ptr current_policy_controller_ = nullptr;
ovinf::PolicyControllerBase::Ptr target_policy_controller_ = nullptr;
bool switching_flag_ = false;
double switching_time_;
std::chrono::steady_clock::time_point switching_start_time_;
Eigen::Vector3f command_;
};
#endif // !USER_FUNC_H