---
# System prepended metadata

title: Installing Real-time Kernel on RPi 4
tags: [NTURT, electrical_system, software]

---

# Installing Real-time Kernel on RPi 4
###### tags: `software` `electrical_system` `NTURT`

## Procedure

1. Download the image from github: [ros-realtime/ros-realtime-rpi4-image](https://github.com/ros-realtime/ros-realtime-rpi4-image).
2. Extract it and flash it onto a micro SD card using tools such as [rpi imager](https://www.raspberrypi.com/software/)
    > Note: By using rpi imager, you can configure the user name, passwprd, host name for ssh, wifi connection, locale, etc. Tutorial can be found on [youtube](https://www.youtube.com/watch?v=ntaXWS8Lk34).
4. Incert the SD card, and then you will be able to use it via ssh.

## Some setups

### ROS2 build system setup

By default, the image does not include build tools, which can be installed by

```shell=
sudo apt install ros-dev-tools
```

To source the build tool `colcon` by

```shell=
echo 'source /opt/ros/humble/setup.bash' >> ~/.bashrc \
&& echo 'source /usr/share/colcon_cd/function/colcon_cd.sh' >> ~/.bashrc \
&& echo 'export _colcon_cd_root=/opt/ros/humble/' >> ~/.bashrc \
&& echo 'source /usr/share/colcon_argcomplete/hook/colcon-argcomplete.bash' >> ~/.bashrc
```

### CAN hat configuration

There some configurations that have to be done to properly use can hat.

1. Install wiringPi form [github](https://github.com/WiringPi/WiringPi).
2. Add

    ```
    dtoverlay=mcp2515-can0,oscillator=12000000,interrupt=25,spimaxfrequency=2000000
    ```
    
    to the the start up config in `/boot/firmware/config.txt`.
    Or simply
    
    ```shell=
    echo 'dtoverlay=mcp2515-can0,oscillator=12000000,interrupt=25,spimaxfrequency=2000000' >> /boot/firmware/config.txt
    ```
3. Reboot and run
    
    ```shell=
    sudo ip link set can0 up type can bitrate 1000000
    ```
    
    to configure the can bitrate to 1m and activate it, this step have to be done after reboot.
    
### Real-time permission

Add

```shell=
<USER_NAME> -       rtprio          98
<USER_NAME> -       memlock         1048576
```

to `/etc/security/limits.conf`, which will allow `<USER_NAME>` to run real-time application up to priority of 98 and lock memory up to 1GB.

### Automate it

Using shall script [install.sh from `NTURacingTeam/nturt_rpi_deployer`](https://github.com/NTURacingTeam/nturt_rpi_deployer/blob/ros2/install.sh).

## Weird behavior

1. The ethernet does not work after boot, it can be fixed by running `sudo dhserver -4`, but it don't lasst after reboot.
