-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathStabilizer.h
303 lines (259 loc) · 10.1 KB
/
Stabilizer.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
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
// -*- C++ -*-
/*!
* @file Stabilizer.h
* @brief stabilizer component
* @date $Date$
*
* $Id$
*/
#ifndef STABILIZER_COMPONENT_H
#define STABILIZER_COMPONENT_H
#include <rtm/Manager.h>
#include <rtm/DataFlowComponentBase.h>
#include <rtm/CorbaPort.h>
#include <rtm/DataInPort.h>
#include <rtm/DataOutPort.h>
#include <rtm/idl/BasicDataTypeSkel.h>
#include <rtm/idl/ExtendedDataTypesSkel.h>
//#include <hrpModel/Body.h>
#include <cnoid/Body>
#include<cnoid/BodyLoader>
#include<cnoid/VRMLBodyLoader>
#include <cnoid/EigenTypes>
#include <cnoid/JointPath>
#include <cnoid/Jacobian>
#include <cnoid/EigenUtil>
#include <cnoid/Sensor>
using namespace cnoid;
// Service implementation headers
// <rtc-template block="service_impl_h">
#include "StabilizerService_impl.h"
//#include "TwoDofController.h"
#include "ImpedanceController/JointPathEx.h"
#include "ImpedanceController/RatsMatrix.h"
#include "IVK.h"
// </rtc-template>
// Service Consumer stub headers
// <rtc-template block="consumer_stub_h">
// </rtc-template>
/**
\brief sample RT component which has one data input port and one data output port
*/
#define ST_NUM_LEGS 2
class Stabilizer
: public RTC::DataFlowComponentBase
{
public:
/**
\brief Constructor
\param manager pointer to the Manager
*/
Stabilizer(RTC::Manager* manager);
/**
\brief Destructor
*/
virtual ~Stabilizer();
// The initialize action (on CREATED->ALIVE transition)
// formaer rtc_init_entry()
virtual RTC::ReturnCode_t onInitialize();
// The finalize action (on ALIVE->END transition)
// formaer rtc_exiting_entry()
// virtual RTC::ReturnCode_t onFinalize();
// The startup action when ExecutionContext startup
// former rtc_starting_entry()
// virtual RTC::ReturnCode_t onStartup(RTC::UniqueId ec_id);
// The shutdown action when ExecutionContext stop
// former rtc_stopping_entry()
// virtual RTC::ReturnCode_t onShutdown(RTC::UniqueId ec_id);
// The activated action (Active state entry action)
// former rtc_active_entry()
virtual RTC::ReturnCode_t onActivated(RTC::UniqueId ec_id);
// The deactivated action (Active state exit action)
// former rtc_active_exit()
virtual RTC::ReturnCode_t onDeactivated(RTC::UniqueId ec_id);
// The execution action that is invoked periodically
// former rtc_active_do()
virtual RTC::ReturnCode_t onExecute(RTC::UniqueId ec_id);
// The aborting action when main logic error occurred.
// former rtc_aborting_entry()
// virtual RTC::ReturnCode_t onAborting(RTC::UniqueId ec_id);
// The error action in ERROR state
// former rtc_error_do()
// virtual RTC::ReturnCode_t onError(RTC::UniqueId ec_id);
// The reset action that is invoked resetting
// This is same but different the former rtc_init_entry()
// virtual RTC::ReturnCode_t onReset(RTC::UniqueId ec_id);
// The state update action that is invoked after onExecute() action
// no corresponding operation exists in OpenRTm-aist-0.2.0
// virtual RTC::ReturnCode_t onStateUpdate(RTC::UniqueId ec_id);
// The action that is invoked when execution context's rate is changed
// no corresponding operation exists in OpenRTm-aist-0.2.0
// virtual RTC::ReturnCode_t onRateChanged(RTC::UniqueId ec_id);
void startStabilizer(void);
void stopStabilizer(void);
void getCurrentParameters ();
void getActualParameters ();
void getTargetParameters ();
double calcAlpha (const Vector3& tmprefzmp,
const std::vector<Vector3>& ee_pos,
const std::vector<Matrix3>& ee_rot);
void calcFootOriginCoords (Vector3& foot_origin_pos, Matrix3& foot_origin_rot);
void sync_2_st ();
void sync_2_idle();
bool calcZMP(Vector3& ret_zmp, const double zmp_z);
//void calcRUNST();
//void calcTPCC();
void calcEEForceMomentControl();
void getParameter(OpenHRP::StabilizerService::stParam& i_stp);
void setParameter(const OpenHRP::StabilizerService::stParam& i_stp);
void waitSTTransition();
// funcitons for calc final torque output
//void calcContactMatrix (MatrixXd& tm, const std::vector<Vector3>& contact_p);
//void calcTorque ();
//void fixLegToCoords (const std::string& leg, const rats::coordinates& coords);
//void getFootmidCoords (rats::coordinates& ret);
double calcDampingControl (const double tau_d, const double tau, const double prev_d,
const double DD, const double TT);
inline bool isContact (const size_t idx) // 0 = right, 1 = left
{
return (prev_act_force_z[idx] > 25.0);
};
inline bool is_inside_foot (const Vector3& leg_pos, const bool is_lleg)
{
if (is_lleg) return leg_pos(1) >= -1 * eefm_leg_inside_margin;
else return leg_pos(1) <= eefm_leg_inside_margin;
};
inline bool is_front_of_foot (const Vector3& leg_pos)
{
return leg_pos(0) >= eefm_leg_front_margin;
};
inline bool is_rear_of_foot (const Vector3& leg_pos)
{
return leg_pos(0) <= -1 * eefm_leg_rear_margin;
};
protected:
// Configuration variable declaration
// <rtc-template block="config_declare">
// </rtc-template>
RTC::TimedDoubleSeq m_qCurrent;
RTC::TimedDoubleSeq m_qRef;
RTC::TimedDoubleSeq m_tau;
RTC::TimedOrientation3D m_rpy;
// RTC::TimedDoubleSeq m_forceR, m_forceL;
RTC::TimedDoubleSeq m_force[2];
RTC::TimedPoint3D m_zmpRef;
RTC::TimedPoint3D m_zmp;
RTC::TimedPoint3D m_basePos;
RTC::TimedOrientation3D m_baseRpy;
RTC::TimedBooleanSeq m_contactStates;
RTC::TimedDoubleSeq m_controlSwingSupportTime;
// for debug ouput
RTC::TimedPoint3D m_originRefZmp, m_originRefCog, m_originRefCogVel, m_originNewZmp;
RTC::TimedPoint3D m_originActZmp, m_originActCog, m_originActCogVel;
RTC::TimedDoubleSeq m_refWrenchR, m_refWrenchL;
RTC::TimedDoubleSeq m_footCompR, m_footCompL;
RTC::TimedOrientation3D m_actBaseRpy;
RTC::TimedPoint3D m_currentBasePos;
RTC::TimedOrientation3D m_currentBaseRpy;
RTC::TimedDoubleSeq m_debugData;
// DataInPort declaration
// <rtc-template block="inport_declare">
RTC::InPort<RTC::TimedDoubleSeq> m_qCurrentIn;
RTC::InPort<RTC::TimedDoubleSeq> m_qRefIn;
RTC::InPort<RTC::TimedOrientation3D> m_rpyIn;
RTC::InPort<RTC::TimedDoubleSeq> m_forceRIn;
RTC::InPort<RTC::TimedDoubleSeq> m_forceLIn;
RTC::InPort<RTC::TimedPoint3D> m_zmpRefIn;
RTC::InPort<RTC::TimedPoint3D> m_basePosIn;
RTC::InPort<RTC::TimedOrientation3D> m_baseRpyIn;
RTC::InPort<RTC::TimedBooleanSeq> m_contactStatesIn;
RTC::InPort<RTC::TimedDoubleSeq> m_controlSwingSupportTimeIn;
// </rtc-template>
// DataOutPort declaration
// <rtc-template block="outport_declare">
RTC::OutPort<RTC::TimedDoubleSeq> m_qRefOut;
RTC::OutPort<RTC::TimedDoubleSeq> m_tauOut;
RTC::OutPort<RTC::TimedPoint3D> m_zmpOut;
// for debug output
RTC::OutPort<RTC::TimedPoint3D> m_originRefZmpOut, m_originRefCogOut, m_originRefCogVelOut, m_originNewZmpOut;
RTC::OutPort<RTC::TimedPoint3D> m_originActZmpOut, m_originActCogOut, m_originActCogVelOut;
RTC::OutPort<RTC::TimedDoubleSeq> m_refWrenchROut, m_refWrenchLOut;
RTC::OutPort<RTC::TimedDoubleSeq> m_footCompROut, m_footCompLOut;
RTC::OutPort<RTC::TimedOrientation3D> m_actBaseRpyOut;
RTC::OutPort<RTC::TimedPoint3D> m_currentBasePosOut;
RTC::OutPort<RTC::TimedOrientation3D> m_currentBaseRpyOut;
RTC::OutPort<RTC::TimedDoubleSeq> m_debugDataOut;
// </rtc-template>
// CORBA Port declaration
// <rtc-template block="corbaport_declare">
// </rtc-template>
// Service declaration
// <rtc-template block="service_declare">
RTC::CorbaPort m_StabilizerServicePort;
// </rtc-template>
// Consumer declaration
// <rtc-template block="consumer_declare">
StabilizerService_impl m_service0;
// </rtc-template>
private:
// constant defines
enum {
ST_LEFT = 0,
ST_RIGHT = 1
};
struct ee_trans {
Vector3 localp;
Matrix3 localR;
};
enum cmode {MODE_IDLE, MODE_AIR, MODE_ST, MODE_SYNC_TO_IDLE, MODE_SYNC_TO_AIR} control_mode;
// members
hrp::JointPathExPtr manip2[2];
BodyPtr m_robot;
//add by wu
DeviceList<ForceSensor> forceSensors;
DeviceList<RateGyroSensor> RateGyroSensors;
unsigned int m_debugLevel;
VectorXd transition_joint_q, qorg, qrefv;
std::vector<std::string> sensor_names;//unuse
std::map<std::string, ee_trans> ee_map;
std::map<std::string, size_t> contact_states_index_map;
std::vector<bool> contact_states, prev_contact_states;
double dt;
int transition_count, loop;
bool is_legged_robot, on_ground;
Vector3 current_root_p, target_foot_p[2], target_root_p;
Matrix3 current_root_R, target_root_R, target_foot_R[2], prev_act_foot_origin_rot, prev_ref_foot_origin_rot;
rats::coordinates target_foot_midcoords;
Vector3 ref_zmp, ref_cog, ref_cogvel, prev_ref_cog, prev_ref_zmp;
Vector3 act_zmp, act_cog, act_cogvel, rel_act_zmp, prev_act_cog, prev_act_cogvel, act_base_rpy, current_base_rpy, current_base_pos;
double zmp_origin_off, transition_smooth_gain, prev_act_force_z[2];
OpenHRP::StabilizerService::STAlgorithm st_algorithm;
// TPCC
double k_tpcc_p[2], k_tpcc_x[2], d_rpy[2], k_brot_p[2], k_brot_tc[2];
// RUN ST
//TwoDofController m_tau_x[ST_NUM_LEGS], m_tau_y[ST_NUM_LEGS], m_f_z;
Vector3 pdr;
double m_torque_k[2], m_torque_d[2]; // 3D-LIP parameters (0: x, 1: y)
double pangx_ref, pangy_ref, pangx, pangy;
double k_run_b[2], d_run_b[2];
double rdx, rdy, rx, ry;
// EEFM ST
double eefm_k1[2], eefm_k2[2], eefm_k3[2], eefm_zmp_delay_time_const[2], eefm_body_attitude_control_gain[2], eefm_body_attitude_control_time_const[2];
double eefm_rot_damping_gain[2], eefm_rot_time_const, eefm_pos_damping_gain, eefm_pos_time_const_support, eefm_pos_time_const_swing, eefm_pos_transition_time, eefm_pos_margin_time, eefm_leg_inside_margin, eefm_leg_front_margin, eefm_leg_rear_margin, eefm_cogvel_cutoff_freq;
Vector3 d_foot_rpy[2], new_refzmp, rel_cog, ref_zmp_aux, ee_d_foot_rpy[2];
Vector3 ref_foot_force[2];
Vector3 ref_foot_moment[2];
double zctrl, total_mass, f_zctrl[2];
bool eefm_pos_control_switch,eefm_rot_control_switch,eefm_att_control_switch;
//wu
int step_counter;
int m_nStep;
double force_dz_offset;
Vector3 dzmp;
Vector3 wzmp;
};
extern "C"
{
void StabilizerInit(RTC::Manager* manager);
};
#endif // STABILIZER_COMPONENT_H