# Steps to build ROS in Python3 on Raspbian(3B) ###### tags: `ROS` `Python3` `Raspbian` > Branch of Project [SelfDriving](https://github.com/a885566885566/NCKUES-SelfDriving) ## Table of Content [ToC] ## Brift Description Accoring to ROS wiki, it was recommended to install ROS in ubuntu with this [instruction](http://wiki.ros.org/melodic/Installation/Ubuntu), however, because we have to use the **can bus** functions, we have no chioce but to use `raspbian` which have a better naive raspberry hardware intergration. The original ROS instruction on raspbian would install ROS with Python2 version only, nevertheless, our other program need python3 module, therefore, we can't build ROS from [this](http://wiki.ros.org/ROSberryPi/Installing%20ROS%20Melodic%20on%20the%20Raspberry%20Pi) ## Environment - Raspbian buster - Raspberry Pi 3B ## Step 1: After flash your pi with raspbian Cause the ram of the pi I used is too small and may crush during compiling, I extended the swap ram of the os. >Follow this tutorial to extend the sway ram. http://raspberrypimaker.com/adding-swap-to-the-raspberrypi/ I just modified the following line in `/etc/dphys-swapfile` and reboot. ```bash $ CONF_SWAPSIZE=1024 ``` ## Step 2: System setup First, update and upgrade the apt. ```bash $ sudo apt update $ sudo apt upgrade $ sudo apt-get remove python-* ``` Install Python3 dependencies ```bash $ sudo apt install -y python3 python3-dev python3-pip build-essential $ sudo -H pip3 install rosdep rospkg rosinstall_generator rosinstall wstool vcstools catkin_tools catkin_pkg ``` ### Build `catkin_tools` from sourse Because the `catkin_tools` in pip3 containing old style Python2 module `trollius` according to this [issue](https://github.com/catkin/catkin_tools/issues/558), we need to install it from sourse. Follow by this [guildline](https://catkin-tools.readthedocs.io/en/latest/installing.html). ```bash $ git clone https://github.com/catkin/catkin_tools.git $ cd catkin_tools ``` Remember to install it with python3 ```bash $ pip3 install -r requirements.txt --upgrade $ python3 setup.py install --record install_manifest.txt ``` At the end of the installing, the script will indicate you to add a source script into your`.bashrc`. ## Step 3: Initialize catkin build environment Initialize rosdep ```bash $ sudo rosdep init $ rosdep update ``` Create workspace ```bash $ cd ~ $ mkdir ros_catkin_ws $ cd ros_catkin_ws ``` Initialize the catkin workspace ```bash $ catkin config --init -DCMAKE_BUILD_TYPE=Release --blacklist rqt_rviz rviz_plugin_tutorials librviz_tutorial --install ``` ## Step 4: Setup ROS install I installed the basic *ROS-Comm* version, which has no GUI tools. ```bash $ rosinstall_generator ros_comm --rosdistro melodic --deps --wet-only --tar > melodic-ros_comm-wet.rosinstall $ wstool init src melodic-ros_comm-wet.rosinstall ``` If the workspace is already initialized before, run ```bash wstool update -j4 -t src ``` ## Step 5: Setup environment and install dependencies Set ros python version to 3 ```bash $ export ROS_PYTHON_VERSION=3 ``` Create the following script ```bash #!/bin/bash #Check whether root if [ $(whoami) != root ]; then echo You must be root or use sudo to install packages. return fi #Call apt-get for each package for pkg in "$@" do echo "Installing $pkg" sudo apt-get -my install $pkg >> install.log done ``` and make the script executable: ```bash $ chmod +x install_skip ``` ### Install dependencies with magic tips Install dependencies for ROS sourse in python3 instead of python2. ```bash $ sudo ./install_skip `rosdep check --from-paths src --ignore-src | grep python | sed -e "s/^apt\t//g" | sed -z "s/\n/ /g" | sed -e "s/python/python3/g"` ``` :::warning When I ran above script, I obtain the following error: ```bash Unable to locate python33-* module ``` So I rewrite the above scrip to ```bash $ sudo ./install_skip `rosdep check --from-paths src --ignore-src | grep python | sed -e "s/^apt\t//g" | sed -z "s/\n/ /g" | sed -e "s/python/python/g"` ``` and then it could find the correct package. ::: Install all remaining dependencies and skip python2 based dependencies. ```bash $ rosdep install --from-paths src --ignore-src -y --skip-keys="`rosdep check --from-paths src --ignore-src | grep python | sed -e "s/^apt\t//g" | sed -z "s/\n/ /g"`" ``` Replacing all references to python3 ```bash $ find . -type f -exec sed -i 's/\/usr\/bin\/env[ ]*python/\/usr\/bin\/env python3/g' {} + ``` ## Step 6: Build ROS To here, we already have all required package in Python3 version, including catkin. Building ROS with the following command, it might take several hours: ```bash $ catkin build ``` Export the PYTHON_PATH environment variable to let ROS know where all of the system python3 packages are installed. ```bash $ export PYTHONPATH=/usr/lib/python3/dist-packages ``` ```bash $ source ~/ros_catkin_ws/install/setup.bash ``` ```bash $ export ROS_PYTHON_VERSION=3 $ export PYTHONPATH=/usr/lib/python3/dist-packages ``` ## Step 7: Test the install Run python3 and try to import `rospy` --- ## Reference - [Add swap ram](http://raspberrypimaker.com/adding-swap-to-the-raspberrypi/) - [Building ROS Melodic with Python3 support](https://www.miguelalonsojr.com/blog/robotics/ros/python3/2019/08/20/ros-melodic-python-3-build.html) - [Installing ROS Melodic on the Raspberry Pi](http://wiki.ros.org/ROSberryPi/Installing%20ROS%20Melodic%20on%20the%20Raspberry%20Pi) - [Ubuntu install of ROS Melodic](http://wiki.ros.org/melodic/Installation/Ubuntu) # Setup can bus funcitons ## Chip connection In order to fix the voltage gap problem, follow this [guildline](https://www.raspberrypi.org/forums/viewtopic.php?t=141052) to modify the Vcc connection of the can tranciever. And then connect the CS pin to CE0, that is, gpio 25. ## Boot configuration Configure the boot setting by editing `/boot/config.txt`. Add or modift to the following configurations. ```bash dtparam=spi=on dtoverlay=mcp2515-can0,oscillator=8000000,interrupt=25 dtoverlay=spi-bcm2835-overlay ``` After reboot the raspberry pi, one should observe the spi and can module has been turned on. ## Self Test Install can tools. ```bash sudo apt-get install can-utils pip3 install python-can ``` ```bash sudo ip link set can0 up type can bitrate 500000 ```