-
Notifications
You must be signed in to change notification settings - Fork 298
Expand file tree
/
Copy pathg1_dex3_example.cpp
More file actions
326 lines (271 loc) · 9.69 KB
/
g1_dex3_example.cpp
File metadata and controls
326 lines (271 loc) · 9.69 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
#include <chrono>
#include <thread>
#include <unitree/idl/hg/HandState_.hpp> //replace your sdk path
#include <unitree/idl/hg/HandCmd_.hpp> //replace your sdk path
#include <unitree/robot/channel/channel_publisher.hpp>
#include <unitree/robot/channel/channel_subscriber.hpp>
#include <iostream>
#include <unistd.h>
#include <atomic>
#include <mutex>
#include <cmath>
#include <termios.h>
#include <unistd.h>
#include <eigen3/Eigen/Dense>
enum State {
INIT,
ROTATE,
GRIP,
STOP,
PRINT
};
// set URDF Limits
const float maxLimits_left[7]= { 1.05 , 1.05 , 1.75 , 0 , 0 , 0 , 0 }; // set max motor value
const float minLimits_left[7]= { -1.05 , -0.724 , 0 , -1.57 , -1.75 , -1.57 ,-1.75};
const float maxLimits_right[7]= { 1.05 , 0.742 , 0 , 1.57 , 1.75 , 1.57 , 1.75};
const float minLimits_right[7]= { -1.05 , -1.05 , -1.75, 0 , 0 , 0 ,0 };
// Initing the dds configuration
std::string dds_namespace = "rt/dex3/left";
std::string sub_namespace = "rt/dex3/left/state";
unitree::robot::ChannelPublisherPtr<unitree_hg::msg::dds_::HandCmd_> handcmd_publisher;
unitree::robot::ChannelSubscriberPtr<unitree_hg::msg::dds_::HandState_> handstate_subscriber;
unitree_hg::msg::dds_::HandCmd_ msg;
unitree_hg::msg::dds_::HandState_ state;
std::atomic<State> currentState(INIT);
std::mutex stateMutex;
#define MOTOR_MAX 7
#define SENSOR_MAX 9
uint8_t hand_id = 0;
typedef struct {
uint8_t id : 4;
uint8_t status : 3;
uint8_t timeout: 1;
} RIS_Mode_t;
// stateToString Method
const char* stateToString(State state) {
switch (state) {
case INIT: return "INIT";
case ROTATE: return "ROTATE";
case GRIP: return "GRIP";
case STOP: return "STOP";
case PRINT: return "PRINT";
default: return "UNKNOWN";
}
}
// Monitor user's input
char getNonBlockingInput() {
struct termios oldt, newt;
char ch;
int oldf;
tcgetattr(STDIN_FILENO, &oldt);
newt = oldt;
newt.c_lflag &= ~(ICANON | ECHO);
tcsetattr(STDIN_FILENO, TCSANOW, &newt);
oldf = fcntl(STDIN_FILENO, F_GETFL, 0);
fcntl(STDIN_FILENO, F_SETFL, oldf | O_NONBLOCK);
ch = getchar();
tcsetattr(STDIN_FILENO, TCSANOW, &oldt);
fcntl(STDIN_FILENO, F_SETFL, oldf);
return ch;
}
void userInputThread() {
while (true) {
char ch = getNonBlockingInput();
if (ch == 'q') {
std::cout << "Exiting..." << std::endl;
currentState = STOP;
break;
} else if (ch == 'r') {
currentState = ROTATE;
} else if (ch == 'g') {
currentState = GRIP;
} else if (ch == 'p') {
currentState = PRINT;
} else if (ch == 's') {
currentState = STOP;
}
std::this_thread::sleep_for(std::chrono::milliseconds(100));
}
}
// this method can send kp and kd to motors
void rotateMotors(bool isLeftHand) {
static int _count = 1;
static int dir = 1;
const float* maxLimits = isLeftHand ? maxLimits_left : maxLimits_right;
const float* minLimits = isLeftHand ? minLimits_left : minLimits_right;
for (int i = 0; i < MOTOR_MAX; i++) {
RIS_Mode_t ris_mode;
ris_mode.id = i;
ris_mode.status = 0x01;
uint8_t mode = 0;
mode |= (ris_mode.id & 0x0F);
mode |= (ris_mode.status & 0x07) << 4;
mode |= (ris_mode.timeout & 0x01) << 7;
msg.motor_cmd()[i].mode(mode);
msg.motor_cmd()[i].tau(0);
msg.motor_cmd()[i].kp(0.5);
msg.motor_cmd()[i].kd(0.1);
float range = maxLimits[i] - minLimits[i];
float mid = (maxLimits[i] + minLimits[i]) / 2.0;
float amplitude = range / 2.0;
float q = mid + amplitude * sin(_count / 20000.0 * M_PI);
msg.motor_cmd()[i].q(q);
}
handcmd_publisher->Write(msg);
_count += dir;
if (_count >= 10000) {
dir = -1;
}
if (_count <= -10000) {
dir = 1;
}
usleep(100);
}
// this method can send static position to motors
void gripHand(bool isLeftHand) {
const float* maxLimits = isLeftHand ? maxLimits_left : maxLimits_right;
const float* minLimits = isLeftHand ? minLimits_left : minLimits_right;
for (int i = 0; i < MOTOR_MAX; i++) {
RIS_Mode_t ris_mode;
ris_mode.id = i;
ris_mode.status = 0x01;
uint8_t mode = 0;
mode |= (ris_mode.id & 0x0F);
mode |= (ris_mode.status & 0x07) << 4;
mode |= (ris_mode.timeout & 0x01) << 7;
msg.motor_cmd()[i].mode(mode);
msg.motor_cmd()[i].tau(0);
float mid = (maxLimits[i] + minLimits[i]) / 2.0;
msg.motor_cmd()[i].q(mid);
msg.motor_cmd()[i].dq(0);
msg.motor_cmd()[i].kp(1.5);
msg.motor_cmd()[i].kd(0.1);
}
handcmd_publisher->Write(msg);
usleep(1000000);
}
// this method can send dynamic position to motors
void stopMotors() {
for (int i = 0; i < MOTOR_MAX; i++) {
RIS_Mode_t ris_mode;
ris_mode.id = i;
ris_mode.status = 0x01;
ris_mode.timeout = 0x01;
uint8_t mode = 0;
mode |= (ris_mode.id & 0x0F);
mode |= (ris_mode.status & 0x07) << 4;
mode |= (ris_mode.timeout & 0x01) << 7;
msg.motor_cmd()[i].mode(mode);
msg.motor_cmd()[i].tau(0);
msg.motor_cmd()[i].dq(0);
msg.motor_cmd()[i].kp(0);
msg.motor_cmd()[i].kd(0);
msg.motor_cmd()[i].q(0);
}
handcmd_publisher->Write(msg);
usleep(1000000);
}
// this method can subscribe dds and show the position for now
void printState(bool isLeftHand){
Eigen::Matrix<float, 7, 1> q;
const float* maxLimits = isLeftHand ? maxLimits_left : maxLimits_right;
const float* minLimits = isLeftHand ? minLimits_left : minLimits_right;
for(int i = 0; i < 7; i++)
{
q(i) = state.motor_state()[i].q();
q(i) = (q(i) - minLimits[i] ) / (maxLimits[i] - minLimits[i]);
q(i) = std::clamp(q(i), 0.0f, 1.0f);
}
std::cout << "\033[2J\033[H";
std::cout << "-- Hand State --\n";
std::cout << "--- Current State: " << "Test" << " ---\n";
std::cout << "Commands:\n";
std::cout << " r - Rotate\n";
std::cout << " g - Grip\n";
std::cout << " t - Test\n";
std::cout << " q - Quit\n";
if(isLeftHand){
std::cout << " L: " << q.transpose() << std::endl;
}else std::cout << " R: " << q.transpose() << std::endl;
usleep(0.1 * 1e6);
}
void StateHandler(const void *message) {
state = *(unitree_hg::msg::dds_::HandState_ *)message;
}
int main(int argc, const char** argv)
{
std::cout << " --- Unitree Robotics --- \n";
std::cout << " Dex3 Hand Example \n\n";
std::string input;
std::cout << "Please input the hand id (L for left hand, R for right hand): ";
std::cin >> input;
if (input == "L") {
hand_id = 0;
dds_namespace = "rt/dex3/left";
sub_namespace = "rt/lf/dex3/left/state";
} else if (input == "R") {
hand_id = 1;
dds_namespace = "rt/dex3/right";
sub_namespace = "rt/lf/dex3/right/state";
} else {
std::cout << "Invalid hand id. Please input 'L' or 'R'." << std::endl;
return -1;
}
if (argc < 2)
{
std::cout << "Usage: " << argv[0] << " networkInterface" << std::endl;
exit(-1);
}
unitree::robot::ChannelFactory::Instance()->Init(0, argv[1]);
handcmd_publisher.reset(new unitree::robot::ChannelPublisher<unitree_hg::msg::dds_::HandCmd_>(dds_namespace + "/cmd"));
handstate_subscriber.reset(new unitree::robot::ChannelSubscriber<unitree_hg::msg::dds_::HandState_>(sub_namespace));
handcmd_publisher->InitChannel();
handstate_subscriber->InitChannel(
std::bind(&StateHandler, std::placeholders::_1), 1);
state.motor_state().resize(MOTOR_MAX);
state.press_sensor_state().resize(SENSOR_MAX);
msg.motor_cmd().resize(MOTOR_MAX);
// handcmd_publisher->msg_.motor_cmd().resize(MOTOR_MAX);
std::thread inputThread(userInputThread);
State lastState = INIT;
while (true) {
State state;
{
std::lock_guard<std::mutex> lock(stateMutex);
state = currentState.load();
}
if (state != lastState) {
std::cout << "\n--- Current State: " << stateToString(state) << " ---\n";
std::cout << "Commands:\n";
std::cout << " r - Rotate\n";
std::cout << " g - Grip\n";
std::cout << " p - Print_state\n";
std::cout << " q - Quit\n";
std::cout << " s - Stop\n";
lastState = state;
}
switch (state) {
case INIT:
std::cout << "Initializing..." << std::endl;
currentState = ROTATE;
break;
case ROTATE:
rotateMotors(input == "L");
break;
case GRIP:
gripHand(input == "L");
break;
case STOP:
stopMotors();
break;
case PRINT:
printState(input == "L");
break;
default:
std::cout << "Invalid state!" << std::endl;
inputThread.join();
break;
}
}
return 0;
}