Introduction
This tutorial will give you a first taste of controlling the Crazyflie in the Webots simulator by Cyberbotics.
Prerequisites
All you need is the Webots simulator.
- Make sure that your computer meets the Webots system requirements.
- Then install the latest version of Webots on your computer by following Cyberbotics’ installation instructions.
Trying out the Crazyflie Model in Webots
Let’s dive right into it! This section explains how to open Webots, load the Crazyflie model, and fly it.
Open Webots and Find the Crazyflie Sample World
- Open Webots by typing ‘webots’ in a terminal in Ubuntu or by searching for the Webots installation in the search bar on Windows or Mac.
- Go to ‘File’ > ‘Open Sample World…’.
- In the dialog that appears, navigate to ‘robots’ > ‘bitcraze’ > ‘crazyflie’ > ‘crazyflie.wbt’.
Explore the Webots User Interface
The Webots window should now look like this:
Here’s the purpose of each marked window:
- This is the ‘object’ tree, where you can add robots, obstacles, etc.
- This is the simulation window, where you can see the Crazyflie, its sensors, or camera output.
- This is the controller editor, showing the controller used by the Crazyflie.
- This is the simulation control panel, where you can start, stop, pause, and speed up the simulator.
For a more detailed description of the user interface, check out Webots’ ‘The User Interface’ user guide.
Fly the Crazyflie Using Your Keyboard
- In the simulation control panel (4), press play.
- The simulated Crazyflie’s propellers will start to rotate, and it will take off.
- Click on the simulation window (2) to ensure it is selected.
- Use the arrow keys (up, back, right, and left) to control the Crazyflie’s velocity in the body-fixed x, y direction.
- Use ‘Q’ and ‘E’ to control yaw rotation.
- Use ‘W’ and ‘S’ to control altitude (up and down).
- Now you can experiment and play with the Crazyflie’s controls!
You can examine the C code controlling the Crazyflie in the controller editor (3). If it’s not visible, find ‘Crazyflie “Crazyflie”’ in the object tree, click on ‘controller’, then click ‘edit’.
Note that you can’t currently modify this code yourself, but this will be explained later.
Try Out the Autonomy Example
Let’s explore another example!
- Open another Crazyflie sample world following the same steps as above, but this time select ‘crazyflie_apartment.wbt’.
- You will now see the Crazyflie in an apartment example. There are also range sensors acting like the Multi-ranger deck . A virtual camera is attached, allowing you to see what the Crazyflie “sees”.
- Press play in the simulation control panel. The Crazyflie will take off.
- Control the Crazyflie with the keyboard as before.
- Position the Crazyflie facing a wall, then press ‘A’ on the keyboard. The Crazyflie will follow the wall.
- You can disable wall following by pressing ‘D’ on the keyboard. The Crazyflie will respond to manual arrow keys again.
Note that there might be an error when restarting the simulation, causing the Crazyflie to spawn in a different location. Use the simulation reload/refresh button () to fix this.
Edit the Controller
In the previous examples, you couldn’t modify the controller’s content due to restricted access. Now, we’ll show you how to save the project elsewhere, change the controller, or add an obstacle.
Save the Project in a Different Location
- In the ‘crazyflie_apartment.wbt’ from the previous example, refresh/reload the simulation if you’ve already started it.
- Access the controller editor. If it’s not visible, find ‘Crazyflie “Crazyflie”’ in the object tree, click on ‘controller’, then click ‘edit’.
-
Modify the following lines as suggested:
# OLD direction = WallFollowing.WallFollowingDirection.LEFT range_side_value = range_right_value # NEW direction = WallFollowing.WallFollowingDirection.RIGHT range_side_value = range_left_value
- Click the save icon. You may receive a notice that you’re not allowed to change the files. Select a folder in your working space where you have write access and click ‘copy’. You’ll now be allowed to make changes.
- Play the simulation, press ‘A’ to start autonomy mode. You’ll see the Crazyflie moving in the opposite direction.
Next Steps
You’ve now experienced the Webots simulator. You can continue modifying the controller or clone the ‘crazyflie-simulation’ repository:
git clone https://github.com/bitcraze/crazyflie-simulation
You can try different examples from the source:
Note that the latter ‘wall following’ example from the source should directly link to files in the cflib examples. This allows you to try out the exact same state machine on a real Crazyflie with a Flow deck V2 and Multi-ranger deck .
Further Reading
- Visit the Cyberbotics Webots user guide to learn more about working with Webots.
- Explore the Crazyflie simulation repository documentation for more explanation about the crazyflie-simulation repository and its content.