-
Notifications
You must be signed in to change notification settings - Fork 176
/
Copy pathexample_position.cpp
159 lines (135 loc) · 4.57 KB
/
example_position.cpp
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
/*****************************************************************
Copyright (c) 2020, Unitree Robotics.Co.Ltd. All rights reserved.
******************************************************************/
#include "unitree_legged_sdk/unitree_legged_sdk.h"
#include <math.h>
#include <iostream>
#include <stdio.h>
#include <stdint.h>
using namespace std;
using namespace UNITREE_LEGGED_SDK;
class Custom
{
public:
Custom(uint8_t level): safe(LeggedType::Go1), udp(level) {
udp.InitCmdData(cmd);
}
void UDPRecv();
void UDPSend();
void RobotControl();
Safety safe;
UDP udp;
LowCmd cmd = {0};
LowState state = {0};
float qInit[3]={0};
float qDes[3]={0};
float sin_mid_q[3] = {0.0, 1.2, -2.0};
float Kp[3] = {0};
float Kd[3] = {0};
double time_consume = 0;
int rate_count = 0;
int sin_count = 0;
int motiontime = 0;
float dt = 0.002; // 0.001~0.01
};
void Custom::UDPRecv()
{
udp.Recv();
}
void Custom::UDPSend()
{
udp.Send();
}
double jointLinearInterpolation(double initPos, double targetPos, double rate)
{
double p;
rate = std::min(std::max(rate, 0.0), 1.0);
p = initPos*(1-rate) + targetPos*rate;
return p;
}
void Custom::RobotControl()
{
motiontime++;
udp.GetRecv(state);
// printf("%d %f\n", motiontime, state.motorState[FR_2].q);
printf("%d %f\n", motiontime, state.imu.quaternion[2]);
// gravity compensation
cmd.motorCmd[FR_0].tau = -0.65f;
cmd.motorCmd[FL_0].tau = +0.65f;
cmd.motorCmd[RR_0].tau = -0.65f;
cmd.motorCmd[RL_0].tau = +0.65f;
// if( motiontime >= 100){
if( motiontime >= 0){
// first, get record initial position
// if( motiontime >= 100 && motiontime < 500){
if( motiontime >= 0 && motiontime < 10){
qInit[0] = state.motorState[FR_0].q;
qInit[1] = state.motorState[FR_1].q;
qInit[2] = state.motorState[FR_2].q;
}
// second, move to the origin point of a sine movement with Kp Kd
// if( motiontime >= 500 && motiontime < 1500){
if( motiontime >= 10 && motiontime < 400){
rate_count++;
double rate = rate_count/200.0; // needs count to 200
Kp[0] = 5.0; Kp[1] = 5.0; Kp[2] = 5.0;
Kd[0] = 1.0; Kd[1] = 1.0; Kd[2] = 1.0;
qDes[0] = jointLinearInterpolation(qInit[0], sin_mid_q[0], rate);
qDes[1] = jointLinearInterpolation(qInit[1], sin_mid_q[1], rate);
qDes[2] = jointLinearInterpolation(qInit[2], sin_mid_q[2], rate);
}
double sin_joint1, sin_joint2;
// last, do sine wave
if( motiontime >= 400){
sin_count++;
sin_joint1 = 0.6 * sin(3*M_PI*sin_count/1000.0);
sin_joint2 = -0.6 * sin(1.8*M_PI*sin_count/1000.0);
qDes[0] = sin_mid_q[0];
qDes[1] = sin_mid_q[1];
qDes[2] = sin_mid_q[2] + sin_joint2;
// qDes[2] = sin_mid_q[2];
}
cmd.motorCmd[FR_0].q = qDes[0];
cmd.motorCmd[FR_0].dq = 0;
cmd.motorCmd[FR_0].Kp = Kp[0];
cmd.motorCmd[FR_0].Kd = Kd[0];
cmd.motorCmd[FR_0].tau = -0.65f;
cmd.motorCmd[FR_1].q = qDes[1];
cmd.motorCmd[FR_1].dq = 0;
cmd.motorCmd[FR_1].Kp = Kp[1];
cmd.motorCmd[FR_1].Kd = Kd[1];
cmd.motorCmd[FR_1].tau = 0.0f;
cmd.motorCmd[FR_2].q = qDes[2];
cmd.motorCmd[FR_2].dq = 0;
cmd.motorCmd[FR_2].Kp = Kp[2];
cmd.motorCmd[FR_2].Kd = Kd[2];
cmd.motorCmd[FR_2].tau = 0.0f;
}
if(motiontime > 10){
safe.PositionLimit(cmd);
int res1 = safe.PowerProtect(cmd, state, 1);
// You can uncomment it for position protection
// int res2 = safe.PositionProtect(cmd, state, 0.087);
if(res1 < 0) exit(-1);
}
udp.SetSend(cmd);
}
int main(void)
{
std::cout << "Communication level is set to LOW-level." << std::endl
<< "WARNING: Make sure the robot is hung up." << std::endl
<< "Press Enter to continue..." << std::endl;
std::cin.ignore();
Custom custom(LOWLEVEL);
// InitEnvironment();
LoopFunc loop_control("control_loop", custom.dt, boost::bind(&Custom::RobotControl, &custom));
LoopFunc loop_udpSend("udp_send", custom.dt, 3, boost::bind(&Custom::UDPSend, &custom));
LoopFunc loop_udpRecv("udp_recv", custom.dt, 3, boost::bind(&Custom::UDPRecv, &custom));
loop_udpSend.start();
loop_udpRecv.start();
loop_control.start();
while(1){
sleep(10);
};
return 0;
}