-
Notifications
You must be signed in to change notification settings - Fork 0
/
man.h
187 lines (168 loc) · 4.31 KB
/
man.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
#ifndef MAN_H
#define MAN_H
#include "body.h"
#include "joint.h"
#include <vector>
#include <fstream>
enum Bodies{
back_forearm, back_arm, back_foot, back_calf, back_thigh, torso,
head, front_thigh, front_calf, front_foot, front_arm, front_forearm
};
enum Joints{
back_shoulder, front_shoulder, back_elbow, front_elbow, neck,
back_hip, front_hip, back_knee, front_knee, back_ankle, front_ankle
};
class Controller;
class Man{
friend class Controller;
public:
void Initialize(double x, double y);
Body* RegisterBodies();
Joint* RegisterJoints();
Vector2D GetPos();
private:
std::vector<Body*> body;
std::vector<Joint*> joint;
int b, j;
Vector2D comPosition;
Vector2D comVelocity;
void updateCoM();
};
struct TrajComp{
TrajComp() : offset(0) {}
trajectory1D base;
trajectory1D dscale;
trajectory1D vscale;
double offset;
double compute(double phi, Vector2D d, Vector2D v){
double baseAngle = offset;
double scale = 1;
if (dscale.getKnotCount() > 0)
scale *= dscale.evaluate_linear(d.x);
if (vscale.getKnotCount() > 0)
scale *= vscale.evaluate_linear(v.x);
if (base.getKnotCount() > 0)
baseAngle += base.evaluate_catmull_rom(phi) * scale;
return baseAngle;
};
};
struct Trajectory{
Trajectory() {}
Trajectory(int l, int r) : leftstance(l), rightstance(r) {}
int getJoint(bool s) { return s ? leftstance : rightstance; }
std::vector<TrajComp> comp;
trajectory1D strength;
int leftstance;
int rightstance;
double evaluate(double phi, Vector2D d, Vector2D v){
double q = 0;
for (int i = 0; i < comp.size(); i++)
q += comp[i].compute(phi, d, v);
return q;
}
double evaluateStrength(double phi){
if (strength.getKnotCount() == 0) return 1.0;
return strength.evaluate_catmull_rom(phi);
}
};
struct State{
State() {}
State(int n, double t, bool r, bool k, bool s, bool f) :
nextstate(n), stime(t), reverse(r), keep(k), setstance(s), transonfoot(f) {}
std::vector<Trajectory> traj;
int nextstate;
double stime;
bool reverse;
bool keep;
bool setstance;
bool transonfoot;
trajectory1D dTraj;
trajectory1D vTraj;
};
struct ControlParams{
ControlParams() {}
ControlParams(Joint* j, double p, double d, double m) :
joint(j), controlled(false), kp(p), kd(d), maxAbsTorque(m), strength(1) {}
Joint* joint;
bool controlled;
double kp, kd;
double maxAbsTorque;
double strength;
};
struct PoseJoint{
double orient;
double angvel;
};
struct Pose{
std::vector<PoseJoint> joints;
void zero() {
for (PoseJoint &a : joints)
{
a.orient = 0;
a.angvel = 0;
}
}
};
class Controller{
public:
Controller(Man* m);
void loadFSM(std::string filename);
void Step(double dt);
void checkTransition(double dt);
int advanceTime(double dt);
void computeTorques();
void applyTorques(double dt);
void computeIKSwingTargets(double dt);
Vector2D getSwingTarget(double t, Vector2D com);
void computePDTorques();
void compensateGravity();
void COMJT();
void computeLegTorques();
void computeHipTorques();
void setStanceSwing();
Vector2D computeIP();
Vector2D computeEstimate(Vector2D comPos, double phase);
void computeDesired();
void updateCoM();
mRay SwRay, StRay;
Vector2D endpoint, elbow;
void SetSpeed(double speed) {desiredvelocity = speed;}
private:
Man* man;
std::vector<double> torques;
std::vector<double> oldtorques;
std::vector<State> states;
std::vector<ControlParams> controlParams;
ControlParams rootControlParams;
Pose desiredPose;
double desiredroot;
double rootTorque;
bool stance;
bool doubleStance;
int FSMStateIndex;
Joint* SwHip,* SwKnee,* SwAnkle,* StHip,* StKnee,* StAnkle;
Body* SwThigh,* SwCalf,* SwFoot,* StThigh,* StCalf,* StFoot;
Vector2D SwFootStart;
Vector2D StancetoCoM;
Vector2D dsCoMErr;
double phi;
double desiredvelocity;
trajectory1D swingTraj;
trajectory1D heightTraj;
inline void computeD0(double phi, Vector2D* d0) {
State* curState = &states[FSMStateIndex];
computeDorV(phi, &curState->dTraj, d0);
}
inline void computeV0(double phi, Vector2D* v0) {
State* curState = &states[FSMStateIndex];
computeDorV(phi, &curState->vTraj, v0);
}
inline static void computeDorV(double phi, trajectory1D* traj, Vector2D* result) {
result->y = 0;
if(traj->getKnotCount() == 0)
result->x = 0;
else
result->x = traj->evaluate_catmull_rom(phi);
}
};
#endif