unitreerobotics / unitree_ros

BSD 3-Clause "New" or "Revised" License
542 stars 249 forks source link

Customizing control loop frequency #23

Open edward9503 opened 2 years ago

edward9503 commented 2 years ago

Hi there,

recently, I was trying to increase the control loop frequency in both gazebo and our Unitree robot A1. I found in the software guide that the maximum real-time communication bandwidth is 1kHz. It suddenly occurred to me that in the gazebo codes and even the real-time control codes there are some "sleep" functions called after publishing the joint commands (for instance, the "usleep(1000)" in function "sendServoCmd()" in "unitree_controller/src/body.cpp" for simulation, and the "loop_rate.sleep()" in "unitree_legged_real/src/exe/position_mode.cpp" for real robot).

I know such "sleep" functions are meant to be used to set the control loop at the desired cycle. However, the time for one cycle is instead increased after using the "sleep" function (for example, I want the control loop to be run at 1kHz. But using sleep function reduces the frequency to less than half of it). So I was wondering:

  1. Why do you still use these functions? To make the command-sending more stable?
  2. If I want to increase the frequency, what should I do? Can I just command out the "usleep" function in gazebo, and also the "sleep" functions in control node + LCM server node for controlling the real robot with ROS?

Thanks in advance!

Best

yiyuc commented 2 years ago

You can remove that one line of code and use ROS rate to set the control rate

On Sat, Oct 23, 2021 at 02:38 Shengzhi Wang @.***> wrote:

Hi there,

recently, I was trying to increase the control loop frequency in both gazebo and our Unitree robot A1. I found in the software guide that the maximum real-time communication bandwidth is 1kHz. It suddenly occurred to me that in the gazebo codes and even the real-time control codes there are some "sleep" functions called after publishing the joint commands (for instance, the "usleep(1000)" in function "sendServoCmd()" in "unitree_controller/src/body.cpp" for simulation, and the "loop_rate.sleep()" in "unitree_legged_real/src/exe/position_mode.cpp" for real robot).

I know such "sleep" functions are meant to be used to set the control loop at the desired cycle. However, the time for one cycle is instead increased after using the "sleep" function (for example, I want the control loop to be run at 1kHz. But using sleep function reduces the frequency to less than half of it). So I was wondering:

  1. Why do you still use these functions? To make the command-sending more stable?
  2. If I want to increase the frequency, what should I do? Can I just command out the "usleep" function in gazebo, and also the "sleep" functions in control node + LCM server node for controlling the real robot with ROS?

Thanks in advance!

Best

— You are receiving this because you are subscribed to this thread. Reply to this email directly, view it on GitHub https://urldefense.com/v3/__https://github.com/unitreerobotics/unitree_ros/issues/23__;!!LIr3w8kk_Xxm!4YGwvMoF6ZT-dOrAbVc2ntw00QvEBVqftFU8QwUdLuWwL3pRFpaga2NcYv1jdw$, or unsubscribe https://urldefense.com/v3/__https://github.com/notifications/unsubscribe-auth/AF22HWZUCPJHRN35SNVSTOTUIJ7AHANCNFSM5GSCN7BQ__;!!LIr3w8kk_Xxm!4YGwvMoF6ZT-dOrAbVc2ntw00QvEBVqftFU8QwUdLuWwL3pRFpaga2OVJ-KbyA$ . Triage notifications on the go with GitHub Mobile for iOS https://urldefense.com/v3/__https://apps.apple.com/app/apple-store/id1477376905?ct=notification-email&mt=8&pt=524675__;!!LIr3w8kk_Xxm!4YGwvMoF6ZT-dOrAbVc2ntw00QvEBVqftFU8QwUdLuWwL3pRFpaga2M8hrJIXQ$ or Android https://urldefense.com/v3/__https://play.google.com/store/apps/details?id=com.github.android&referrer=utm_campaign*3Dnotification-email*26utm_medium*3Demail*26utm_source*3Dgithub__;JSUlJSU!!LIr3w8kk_Xxm!4YGwvMoF6ZT-dOrAbVc2ntw00QvEBVqftFU8QwUdLuWwL3pRFpaga2PLd9ss1A$.

edward9503 commented 2 years ago

You can remove that one line of code and use ROS rate to set the control rate On Sat, Oct 23, 2021 at 02:38 Shengzhi Wang @.**> wrote: Hi there, recently, I was trying to increase the control loop frequency in both gazebo and our Unitree robot A1. I found in the software guide that the maximum real-time communication bandwidth is 1kHz. It suddenly occurred to me that in the gazebo codes and even the real-time control codes there are some "sleep" functions called after publishing the joint commands (for instance, the "usleep(1000)" in function "sendServoCmd()" in "unitree_controller/src/body.cpp" for simulation, and the "loop_rate.sleep()" in "unitree_legged_real/src/exe/position_mode.cpp" for real robot). I know such "sleep" functions are meant to be used to set the control loop at the desired cycle. However, the time for one cycle is instead increased after using the "sleep" function (for example, I want the control loop to be run at 1kHz. But using sleep function reduces the frequency to less than half of it). So I was wondering: 1. Why do you still use these functions? To make the command-sending more stable? 2. If I want to increase the frequency, what should I do? Can I just command out the "usleep" function in gazebo, and also the "sleep" functions in control node + LCM server node for controlling the real robot with ROS? Thanks in advance! Best — You are receiving this because you are subscribed to this thread. Reply to this email directly, view it on GitHub <https://urldefense.com/v3/__https://github.com/unitreerobotics/unitree_ros/issues/23__;!!LIr3w8kk_Xxm!4YGwvMoF6ZT-dOrAbVc2ntw00QvEBVqftFU8QwUdLuWwL3pRFpaga2NcYv1jdw$>, or unsubscribe <https://urldefense.com/v3/https://github.com/notifications/unsubscribe-auth/AF22HWZUCPJHRN35SNVSTOTUIJ7AHANCNFSM5GSCN7BQ;!!LIr3w8kk_Xxm!4YGwvMoF6ZT-dOrAbVc2ntw00QvEBVqftFU8QwUdLuWwL3pRFpaga2OVJ-KbyA$> . Triage notifications on the go with GitHub Mobile for iOS <https://urldefense.com/v3/https://apps.apple.com/app/apple-store/id1477376905?ct=notification-email&mt=8&pt=524675;!!LIr3w8kk_Xxm!4YGwvMoF6ZT-dOrAbVc2ntw00QvEBVqftFU8QwUdLuWwL3pRFpaga2M8hrJIXQ$> or Android <[https://urldefense.com/v3/__https://play.google.com/store/apps/details?id=com.github.android&referrer=utm_campaign3Dnotification-email26utm_medium3Demail26utm_source3Dgithub;JSUlJSU!!LIr3w8kk_Xxm!4YGwvMoF6ZT-dOrAbVc2ntw00QvEBVqftFU8QwUdLuWwL3pRFpaga2PLd9ss1A$](https://urldefense.com/v3/__https://play.google.com/store/apps/details?id=com.github.android&referrer=utm_campaign*3Dnotification-email*26utm_medium*3Demail*26utm_source*3Dgithub;JSUlJSU!!LIr3w8kk_Xxm!4YGwvMoF6ZT-dOrAbVc2ntw00QvEBVqftFU8QwUdLuWwL3pRFpaga2PLd9ss1A%24)>.

Hi,

thanks for your reply. But the thing is, I want to increase the loop frequency. Let's say I want to control in 1kHz, than the loop time should be 1ms. I realize that either I use sleep function or ros rate function cannot increase the frequency (i.e. reduce the loop time), because without using the sleep function or setting the ROS rate, my control loop already exceeds 1ms. So I think in my case, the sleep function or setting the ROS rate will even "worsen" the loop time. I also tried remove the sleep and ros rate line, and I saw the loop time was reduced significantly. So I was wondering:

  1. Is it safe to remove the sleep and ros rate function for my case?
  2. What will happen if I don't use any sleep or ros rate setting function?

In general, ROS rate is used to force the loop frequency to be a desired value when the main loop time doesn't exceed the maximum time corresponding to the frequency (if I understand correctly). But when the computation load in the loop is large and exceeds that loop time, I am not sure if the ros rate function still works. Looking forward to any feedback!

Best

yiyuc commented 2 years ago

You can definitely remove the sleep function and use ros rate function and use ros rate and its corresponding sleep function. If you don't use any sleep or ros rate functions, the commands are sent to robot as fast as possible and this is not what you want for your control rate. When the computation load in the loop exceeds the desired loop time, the loop will "stuck" there until the computation finishes. It depends on your use case to see if your controller still works if you have some iterations that takes longer than your loop rate but the ROS rate function needs to wait for your thing to finish before it impose the desired control rate