11#include < chrono>
22#include < thread>
3- #include < unitree/hg_idl /HandState_.hpp> // replace your sdk path
4- #include < unitree/hg_idl /HandCmd_.hpp> // replace your sdk path
3+ #include < unitree/idl/hg /HandState_.hpp> // replace your sdk path
4+ #include < unitree/idl/hg /HandCmd_.hpp> // replace your sdk path
55#include < unitree/robot/channel/channel_publisher.hpp>
66#include < unitree/robot/channel/channel_subscriber.hpp>
77#include < iostream>
@@ -22,15 +22,13 @@ enum State {
2222 PRINT
2323};
2424
25- // �ο�urdf�����λ
26- const float maxTorqueLimits_left[7 ]= { 1.05 , 1.05 , 1.75 , 0 , 0 , 0 , 0 }; // set max motor value
27- const float minTorqueLimits_left[7 ]= { -1.05 , -0.724 , 0 , -1.57 , -1.75 , -1.57 ,-1.75 };
28- const float maxTorqueLimits_right[7 ]= { 1.05 , 0.742 , 0 , 1.57 , 1.75 , 1.57 , 1.75 };
29- const float minTorqueLimits_right[7 ]= { -1.05 , -1.05 , -1.75 , 0 , 0 , 0 ,0 };
30-
31-
32- // State currentState = INIT;
25+ // set URDF Limits
26+ const float maxLimits_left[7 ]= { 1.05 , 1.05 , 1.75 , 0 , 0 , 0 , 0 }; // set max motor value
27+ const float minLimits_left[7 ]= { -1.05 , -0.724 , 0 , -1.57 , -1.75 , -1.57 ,-1.75 };
28+ const float maxLimits_right[7 ]= { 1.05 , 0.742 , 0 , 1.57 , 1.75 , 1.57 , 1.75 };
29+ const float minLimits_right[7 ]= { -1.05 , -1.05 , -1.75 , 0 , 0 , 0 ,0 };
3330
31+ // Initing the dds configuration
3432std::string dds_namespace = " rt/dex3/left" ;
3533std::string sub_namespace = " rt/dex3/left/state" ;
3634unitree::robot::ChannelPublisherPtr<unitree_hg::msg::dds_::HandCmd_> handcmd_publisher;
@@ -50,7 +48,7 @@ typedef struct {
5048 uint8_t timeout: 1 ;
5149} RIS_Mode_t;
5250
53- // stateToString ��������
51+ // stateToString Method
5452const char * stateToString (State state) {
5553 switch (state) {
5654 case INIT: return " INIT" ;
@@ -62,7 +60,7 @@ const char* stateToString(State state) {
6260 }
6361}
6462
65-
63+ // Monitor user's input
6664char getNonBlockingInput () {
6765 struct termios oldt, newt;
6866 char ch;
@@ -75,16 +73,14 @@ char getNonBlockingInput() {
7573 oldf = fcntl (STDIN_FILENO, F_GETFL, 0 );
7674 fcntl (STDIN_FILENO, F_SETFL, oldf | O_NONBLOCK);
7775
78- ch = getchar (); // ��ȡ�����ַ�
76+ ch = getchar ();
7977
8078 tcsetattr (STDIN_FILENO, TCSANOW, &oldt);
8179 fcntl (STDIN_FILENO, F_SETFL, oldf);
8280
8381 return ch;
8482}
8583
86-
87-
8884void userInputThread () {
8985 while (true ) {
9086 char ch = getNonBlockingInput ();
@@ -105,11 +101,12 @@ void userInputThread() {
105101 }
106102}
107103
104+ // this method can send kp and kd to motors
108105void rotateMotors (bool isLeftHand) {
109106 static int _count = 1 ;
110107 static int dir = 1 ;
111- const float * maxTorqueLimits = isLeftHand ? maxTorqueLimits_left : maxTorqueLimits_right ;
112- const float * minTorqueLimits = isLeftHand ? minTorqueLimits_left : minTorqueLimits_right ;
108+ const float * maxLimits = isLeftHand ? maxLimits_left : maxLimits_right ;
109+ const float * minLimits = isLeftHand ? minLimits_left : minLimits_right ;
113110
114111 for (int i = 0 ; i < MOTOR_MAX; i++) {
115112 RIS_Mode_t ris_mode;
@@ -127,8 +124,8 @@ void rotateMotors(bool isLeftHand) {
127124 msg.motor_cmd ()[i].kd (0.1 );
128125
129126
130- float range = maxTorqueLimits [i] - minTorqueLimits [i];
131- float mid = (maxTorqueLimits [i] + minTorqueLimits [i]) / 2.0 ;
127+ float range = maxLimits [i] - minLimits [i];
128+ float mid = (maxLimits [i] + minLimits [i]) / 2.0 ;
132129 float amplitude = range / 2.0 ;
133130 float q = mid + amplitude * sin (_count / 20000.0 * M_PI);
134131
@@ -149,15 +146,16 @@ void rotateMotors(bool isLeftHand) {
149146 usleep (100 );
150147}
151148
149+ // this method can send static position to motors
152150void gripHand (bool isLeftHand) {
153151
154- const float * maxTorqueLimits = isLeftHand ? maxTorqueLimits_left : maxTorqueLimits_right ;
155- const float * minTorqueLimits = isLeftHand ? minTorqueLimits_left : minTorqueLimits_right ;
152+ const float * maxLimits = isLeftHand ? maxLimits_left : maxLimits_right ;
153+ const float * minLimits = isLeftHand ? minLimits_left : minLimits_right ;
156154
157155 for (int i = 0 ; i < MOTOR_MAX; i++) {
158156 RIS_Mode_t ris_mode;
159157 ris_mode.id = i;
160- ris_mode.status = 0x01 ; /
158+ ris_mode.status = 0x01 ;
161159
162160
163161 uint8_t mode = 0 ;
@@ -168,7 +166,7 @@ void gripHand(bool isLeftHand) {
168166 msg.motor_cmd ()[i].tau (0 );
169167
170168
171- float mid = (maxTorqueLimits [i] + minTorqueLimits [i]) / 2.0 ;
169+ float mid = (maxLimits [i] + minLimits [i]) / 2.0 ;
172170
173171
174172 msg.motor_cmd ()[i].q (mid);
@@ -182,6 +180,7 @@ void gripHand(bool isLeftHand) {
182180 usleep (1000000 );
183181}
184182
183+ // this method can send dynamic position to motors
185184void stopMotors () {
186185 for (int i = 0 ; i < MOTOR_MAX; i++) {
187186 RIS_Mode_t ris_mode;
@@ -205,17 +204,17 @@ void stopMotors() {
205204 usleep (1000000 );
206205}
207206
208-
207+ // this method can subscribe dds and show the position for now
209208void printState (bool isLeftHand){
210209 Eigen::Matrix<float , 7 , 1 > q;
211210
212- const float * maxTorqueLimits = isLeftHand ? maxTorqueLimits_left : maxTorqueLimits_right ;
213- const float * minTorqueLimits = isLeftHand ? minTorqueLimits_left : minTorqueLimits_right ;
211+ const float * maxLimits = isLeftHand ? maxLimits_left : maxLimits_right ;
212+ const float * minLimits = isLeftHand ? minLimits_left : minLimits_right ;
214213 for (int i = 0 ; i < 7 ; i++)
215214 {
216215 q (i) = state.motor_state ()[i].q ();
217216
218- q (i) = (q (i) - minTorqueLimits [i] ) / (maxTorqueLimits [i] - minTorqueLimits [i]);
217+ q (i) = (q (i) - minLimits [i] ) / (maxLimits [i] - minLimits [i]);
219218 q (i) = std::clamp (q (i), 0 .0f , 1 .0f );
220219 }
221220 std::cout << " \033 [2J\033 [H" ;
@@ -240,7 +239,8 @@ void StateHandler(const void *message) {
240239
241240
242241
243- int main () {
242+ int main (int argc, const char ** argv)
243+ {
244244 std::cout << " --- Unitree Robotics --- \n " ;
245245 std::cout << " Dex3 Hand Example \n\n " ;
246246 std::string input;
@@ -260,7 +260,12 @@ int main() {
260260 return -1 ;
261261 }
262262
263- unitree::robot::ChannelFactory::Instance ()->Init (0 );
263+ if (argc < 2 )
264+ {
265+ std::cout << " Usage: " << argv[0 ] << " networkInterface" << std::endl;
266+ exit (-1 );
267+ }
268+ unitree::robot::ChannelFactory::Instance ()->Init (0 , argv[1 ]);
264269 handcmd_publisher.reset (new unitree::robot::ChannelPublisher<unitree_hg::msg::dds_::HandCmd_>(dds_namespace + " /cmd" ));
265270 handstate_subscriber.reset (new unitree::robot::ChannelSubscriber<unitree_hg::msg::dds_::HandState_>(sub_namespace));
266271 handcmd_publisher->InitChannel ();
0 commit comments