CrazySim: A Software-in-the-Loop Simulator Interface for Crazyflies #995
Replies: 8 comments 18 replies
-
This is very cool! Awesome job and thanks for sharing! I need to find some time to try this one out but this seems very interesting. Just tagging some people who will be interested in this work too: @whoenig and @Jaeyoung-Lim (he probably made the scripts for PX4). So that we can have a discussion and discuss the future of the integration. |
Beta Was this translation helpful? Give feedback.
-
Very impressive work! Would be interesting to know how much effort maintaining such a code would be if added to the official firmware. The firmware repo itself looks very involved (with over 200 files being changed). |
Beta Was this translation helpful? Give feedback.
-
@llanesc Awesome work!
Just want to point out that PX4 has moved away from this architecture, due to the inflexibility of using a standard message set( PX4 now uses a bridge that directly communicates internal uORB messages to gz topics: https://github.com/PX4/PX4-Autopilot/tree/main/src/modules/simulation/gz_bridge |
Beta Was this translation helpful? Give feedback.
-
Hi! Not a question yet but just an update. I managed to run a single agent in gazebo and run a cflib script! I couldn't get the cfclient to run though as it couldn't find the udp address but I only had a half an hour to try this out. I love it! I hope to look closer into the changes soon and start thinking strategy to intergrate this to our repos |
Beta Was this translation helpful? Give feedback.
-
I made some changes today to the main repo. This includes a new simulated hardware communication latency feature within the Gazebo Sim plugin. To change the latency users can modify the "cflib_latency_ms" variable in the crazyflies.sdf.jinja file. I also updated the Gazebo Sim IMU and physics rates to 1000Hz. This has a bit better drone performance with the cost of multiagent performance. I opened another branch called efficient-multiagent that doesn't have these features. |
Beta Was this translation helpful? Give feedback.
-
Some initial thoughts when going through the code, to kind of start to think about strategy. I must say that I have a lot of respect for how you were able to go through all of these changes. I would have not gotten this to work in 2 weeks so I would assume that you have a very good ability to concentrate ('goede zitvlees' is what we would call it in Dutch 😄) Anyhow. I made the following observations:
Don't see this as critique but rather as a thought-tsunami and remarks of myself going through the process of trying to figure out a strategy to integrate this in the Bitcraze ecosystem. The general thought that I had is that our firmware is very much written with hardware only in mind, and a lot of hardware-specific libraries and logic are heavily intertwined. It might just require a complete firmware overhaul to make it all maintainable (which is very important here if we want to have something that will last in the future). This is a large project but will benefit quite a lot of users, but it also means that during our quartely meeting we need to be able to allocate time for this. But it is good to start planning and laying the groundwork for it 😄 (PS, just an important comment: I saw that crazyflie_cpp was copied and used, which is fo the gazebo plugin I assume. Don't forget to give credit to wolfgang Hönig for that code 😉. Same goes for the model and mesh files that I created. These are on the MIT license originally so you are free to use it, given that you give credit when distributing the code. ) |
Beta Was this translation helpful? Give feedback.
-
@llanesc Nice work! I try installing the program in different computers but the result is the same. |
Beta Was this translation helpful? Give feedback.
-
btw, your work has been featured in the ROS news item last week! https://discourse.ros.org/t/ros-news-for-the-week-of-february-5th-2024/36011 |
Beta Was this translation helpful? Give feedback.
-
Introduction
I had an internship over the summer that involved using Crazyflies. I had never used them before, but I had 5-6 years of experience with PX4 + ROS 2 on custom racing drones. One thing that immediately stood out was the lack of a simulator feature like the SITL feature in PX4. This requires running the Crazyflie firmware on a desktop machine which communicates with the simulator of choice with its sensor package. On PX4 they use MAVLINK along with a custom Gazebo plugin to pass messages between the firmware and simulator. I found this repository https://github.com/wuwushrek/sim_cf that is the only implementation currently of Crazyflie SITL, but the firmware used is very outdated (approximately 5 years ago) and it uses ROS 1 which is nearly at end of support. I followed the same structure as in this repository while making changes to the firmware. The plugin I made for Gazebo Garden is a totally different implementation. I basically wanted users to be able to design their scripts with CFLib as they would for hardware and test that it actually flies in a simulator by just changing the URI to a UDP prefix. Then, when they are satisfied they can change to radio and deploy their script on a real Crazyflie. I also wanted there to not be a direct dependency on ROS 2 because not everyone knows ROS. Those that do want ROS can use Crazyswarm2 with CFLib as the backend.
CrazySim
The main code can be found here https://github.com/gtfactslab/Llanes_ICRA2024. If you just want to use the simulator, then don't worry about the ros2_ws folder. The code is not polished and I need to update it with the current master branch updates, but I can clean up a bit if we want to push this to master. A major concern I had was the build system. I used the CMake file from sim_cf and modified it to support the latest firmware. I also added a feature to also build the Gazebo plugin project as well. I tried to add the SITL feature as a platform using the make file system, but since the target platform is a Linux machine it would require significant changes to the make file approach. I also had to modify the proportional gain for the z-axis because it was a bit sluggish in the simulator. I can return it to the master gains and try to figure out other solutions.
Architecture
The simulator used is Gazebo Garden. This has a rendering engine that enables the use of optical flow and laser distance sensor integration in the future. I made the Gazebo Garden system plugin so that it talks to the firmware first, then users can connect a custom CFLib code with a modified UdpDriver class. The communication protocol is CRTP, but this could probably be changed to support communication with CPX instead. I'll have to look into this so we can avoid changing the CFLib master. The port for the firmware instance is 19950+N where N is the Crazyflie index starting from zero. The port used on CFLib to communicate with the Crazyflie is 19850+N. I didn't see a way to have two CRTP links open at once, so I had to use the plugin as a relay between CFLib and the firmware. This also enables a simulated communication delay feature to test control scripts with simulated radio delay. The launch scripts below are derived from PX4 where we use Jinja2 as a template system to dynamically create N number of sdf files (one for each Crazyflie) on the Linux system tmp folder. Select from any of the three launch script options below.
Option 1: Spawning a single crazyflie model with initial position (x = 0, y = 0)
Option 2: Spawning 8 crazyflie models to form a perfect square
Option 3: Spawning multiple crazyflie models with positions defined in the agents.txt file. New vehicles are defined by adding a new line with comma deliminated initial position x,y.
Below is an architecture diagram of the code.
Testing
I did some testing with model predictive control using Crazyswarm2 and my simulator. Below is a simulation of 16 Crazyflies flying a lemniscate trajectory. This simulator is part of a submission to ICRA 2024 with the MPC code used for test flights.
Integration
Let me know what you think and if you had any issues setting up the simulator. The documentation might be missing dependencies, so I'll try to work through the issues and fix them as they are discovered.
Beta Was this translation helpful? Give feedback.
All reactions