---
# System prepended metadata

title: Onboarding and ros2_crazyflie_ws setup

---

### Setting up ros2_crazyflie_ws with Ubuntu 22.04+ROS2 Humble
1. Install [ROS2-Humble](https://docs.ros.org/en/humble/Installation/Ubuntu-Install-Debians.html)
2. Install [mavros](https://github.com/mavlink/mavros/blob/ros2/mavros/README.md#binary-installation-deb)
3. Install [colcon](https://colcon.readthedocs.io/en/released/user/installation.html#using-debian-packages)
4. ```
    mkdir projects
    cd projects
    ```
4. ```
    git clone git@gitlab.developers.cam.ac.uk:cst/prorok-lab/drones/crazyflies/ros2_crazyflie_ws.git`
    ```
5. ```
    cd ~/projects/ros2_crazyflie_ws/src/Freyja
    git checkout ros2-devel
    ```
6. ```
    cd ~/projects/ros2_crazyflie_ws/src/motion_capture_tracking/
    git checkout ros2
    ```
7. ```
    git submodule update --init --recursive
    ```
9. ```
    cd ~/projects/ros2_crazyflie_ws
    colcon build --cmake-args -DNO_ROBOMASTER=True
    ```
Troubleshoot and install the missing packages until the workspace is properly built. For instance, I had to include <cfloat.h> to a file in motion_capture_tracking to make it build properly.

### Setting up SSH keys for git
[Reference](https://www.servers.com/support/knowledge/linux-administration/how-to-create-a-new-ssh-key-pair)

1. First in the computer for which you want to setup the SSH key, open a terminal and do the following. (Proceed without any filename or passkey or any other input.)
    ```
    ssh-keygen -t rsa
    ```
2. the cd to `~/.ssh`, do `vim 'id_rsa.pub'` and copy the whole ssh key starting with 'ssh-rsa'.
3. Now go to this link [gitlab-ssh-settings](https://gitlab.developers.cam.ac.uk/-/user_settings/ssh_keys) or open gitlab.developers.cam.ac.uk > click on your user photo on the top-left > Preferences > SSH Keys.
4. Then Click `Add new key`. Use the username@computer_name as tittle. and paste the copied ssh key into the 'key' field.
5. For your personal github, go to [github-ssh-settings](https://github.com/settings/keys) or go to github.com > click on your user photo on the top right > settings > SSH and GPG keys. Then do repeat setp 4.

### Configuring a Crazyflie drone
Now the next step is to set up a crazyflie and run the code. For setting up a Crazyflie we have to do the following:
1. Clone the [crazyflie-clients-python](https://github.com/bitcraze/crazyflie-clients-python) repo. 
    ```
    git clone git@github.com:bitcraze/crazyflie-clients-python.git
    ```
2. From a fresh Ubuntu 20.04 system and up, running the client from source requires git, pip and a lib for the Qt GUI.

    ```
    sudo apt install git python3-pip libxcb-xinerama0 libxcb-cursor0
    pip3 install --upgrade pip
    ```
    For some versions of Ubuntu 20.04 you might need to install more packages like `libxcb-cursor0`. Check out [QT6 package dependency list](https://doc.qt.io/qt-6/linux-requirements.html).
    
    **Setting udev permissions:** Using Crazyradio on Linux requires that you set udev permissions. See the cflib [installation guide](https://www.bitcraze.io/documentation/repository/crazyflie-lib-python/master/installation/usb_permissions/) for more information.
3. Install it from the pypi repository:
    ```
    pip3 install cfclient
    ```
4. Launch the cfclient:
    ```
    cd ~/projects/crazyflie-clients-python/bin
    ./cfclient
    ```
5. Input the drone address and Scan to find it. Hit connect to establish connection and then move the drone to see the GUI moving. You can change the channel or configure other parameters using the GUI.
6. Clone the [crazyflie-link-cpp](https://github.com/bitcraze/crazyflie-link-cpp) repo.
    ```
    cd ~/projects
    git clone git@github.com:bitcraze/crazyflie-link-cpp.git
    cd crazyflie-link-cpp/
    ```
    Install the dependencies, update the submodules, Build C++ lib and examples, and Build and install development version of the python binding.

### How to run one or more crazyflies

1. Configure the `channel`, `uri` and `initial_position` (ENU frame) for the specific `cfXX` in the `config/robots_set1.yaml`. Even though the `initial_position` is given in ENU, all the poses are in NED.
2. Open multiple terminals and launch the following:
    For launching the motion_capture_tracking, Freyja, crtp_broadcasters, etc., run:
    ```
    ros2 launch launch/launch_set1.py
    ```
    For sending waypoints to hover at 1 m altitude, run:
    ```
    ros2 launch launch/hover.py config:=./config/robots_set1.yaml
    ```
    For sending waypoints for the line_curriculum training 6360, run:
    ```
    ros2 run example_trajectories mission_tracking --ros-args -p mission:=./2_drone/line_curriculum/6360.npy -p config:=./config/robots_set1.yaml
    ```
    For recording data from the run in a csv file, run:
    ```
    ros2 run data_processing collect_csv --ros-args -p config:=./config/robots_set1.yaml -p experiment:='170524/2380_run1'
    ```
    For flying in a circle reference trajectory, run:
    ```
    ros2 run example_trajectories reference_tracking --ros-args -p shape:=circle -p duration:=300.0 -p num_repeat:=1 -p config:=./config/robots_set1.yaml
    ```
    For sending discrete_waypoint_target (smooth transition, extrapolates a smooth trajectory), you can directly publish to a topic:
    ```
    ros2 topic pub --once cf08/discrete_waypoint_target freyja_msgs/msg/WaypointTarget "{terminal_pn: 1.0, terminal_pe: 0.0, terminal_pd: -.2, allocated_time: 5.0, waypoint_mode: 0}"
    ```
    For sending a reference_state (instantaneous change of pose, often dangerous), you can publish:
    ```
    ros2 topic pub -r 20 /cf08/reference_state freyja_msgs/msg/ReferenceState "{pn: 0.0, pe: 0.0, pd: -2.0}"
    ```
    For landing:
    ```
    ros2 launch launch/land.py config:=./config/robots_set1.yaml
    ```
    
### How to flash Firmware to Crazyflies
1. Clone the [crazyflie_firmware](https://gitlab.developers.cam.ac.uk/cst/prorok-lab/drones/crazyflies/crazyflie-firmware) repo.
    ```
    cd projects/
    git clone --recursive git@gitlab.developers.cam.ac.uk:cst/prorok-lab/drones/crazyflies/crazyflie-firmware.git
    ```

2. `cd projects/crazyflie-firmware/`
3. `sudo apt-get install make gcc-arm-none-eabi`
4. `make cf2_defconfig`
5. `make -j 12`
6. Press and hold the reset button on the crazyflie you want to flash the new firmware to. Then plug in battery while holding the reset button. After you see three blue blinks, release the button. You will see two blue LEDs blinking intermittently. That indicates the crazyflie has entered the bootloader mode.
7. Once the crazyflie is in bootloader mode, attach the receiver to your laptop. and run:
    ```
    make cload
    ```
