---
# System prepended metadata

title: Raspberry Pi 4 model B環境設置
tags: [控制組]

---

# Raspberry Pi 4 model B環境設置
> [name=徐冠東(gordondonghsu.ee10@nycu.edu.tw)]

## DNS、Internet網域設定

## 重灌RPi
### Buster image: https://downloads.raspberrypi.org/raspbian/images/raspbian-2020-02-14/

---

### 1. DNS name resolution & Internet connection
```
host raspbian.raspberrypi.org
host archive.raspberrypi.org
ping -c3 93.93.128.193
ping -c3 46.235.231.111
curl http://raspbian.raspberrypi.org/raspbian/

sudo apt update #update
sudo apt upgrade -y
```

### 2. Open rpi setting
可以打開GUI介面，去設定網路、名稱等等的設定。
```
sudo raspi-config 
```

---

## DAQ設定
1. Setup dev-tools
```
sudo apt-get install libraspberrypi-dev raspberrypi-kernel-headers
```
2. clone the repo and follow the instructions.
https://github.com/mccdaq/daqhats

---

## ROS:noetic設定

1. Install ROS on RPi:
https://hackmd.io/9eaTYjqMSF29G1E5BqtIvA)




---

## Pixhawk 4-飛控板 設定
```limux=
#.sh

#install vim&git
sudo apt update 
sudo apt upgrade -y
sudo apt install vim -y
sudo apt install git

#ROS/Gazebo Classic Installation
git clone https://github.com/PX4/PX4-Autopilot.git --recursive 
pip3 install numpy --upgrade
bash ./PX4-Autopilot/Tools/setup/ubuntu.sh --no-sim-tools --no-nuttx

#some dependencies
sudo apt-get update 
sudo apt-get upgrade -y
sudo apt-get install protobuf-compiler libeigen3-dev libopencv-dev -y
sudo apt-get install python3-catkin-tools python3-rosinstall-generator libgeographic-dev  geographiclib-tools  -y
sudo apt install python-wstool -y
sudo apt install python3-future

#build a workspace
cd ~
mkdir px4_ws
cd px4
mkdir src

#create px4_ws as workspace
cd ~/px4_ws #all commands below are executed in workspace 
catkin init    #you can ignore some missing error
wstool init src 
#git mavlink
rosinstall_generator --rosdistro noetic mavlink | tee /tmp/mavros.rosinstall
#git mavros
rosinstall_generator --upstream mavros | tee -a /tmp/mavros.rosinstall
#git some dependencies
rosinstall_generator --rosdistro noetic geographic_msgs | tee  /tmp/mavros.rosinstall
rosinstall_generator --rosdistro noetic tf2_eigen | tee  /tmp/mavros.rosinstall
rosinstall_generator --rosdistro noetic control_toolbox | tee  /tmp/mavros.rosinstall
rosinstall_generator --rosdistro noetic realtime_tools | tee  /tmp/mavros.rosinstall
rosinstall_generator --rosdistro noetic uuid_msgs | tee  /tmp/mavros.rosinstall
#create packages & deps
wstool merge -t src /tmp/mavros.rosinstall
wstool update -t src -j4
rosdep install --from-paths src --ignore-src -y
#you may need to add some deps more

#Install GeographicLib (opens new window)datasets
./src/mavros/mavros/scripts/install_geographiclib_datasets.sh
#build & source
catkin build
source devel/setup.bash

#offboard example(python)
roscd  # Should cd into ~/px4_ws/devel
cd ..
cd src
#create a new package named offboard_py
catkin_create_pkg offboard_py rospy
#build again in the workspace
cd .. # Assuming previous directory to be ~/px4_ws/src
catkin build
source devel/setup.bash

# check if into the package by using:
roscd offboard_py
# create a new folder and store python file
git clone https://github.com/NCTU-AUV/scripts.git
cd scripts
chmod +x *
```
### 最後修改launch檔設置
#### 進入launch檔: 
```
vim ~/px4_ws/src/mavros/mavros/launch/px4.launch
```
#### 覆蓋成以下的code:
```linux=
<launch>
        <arg name="fcu_url" default="/dev/ttyACM0:57600" />
        <arg name="gcs_url" default="" />
        <arg name="tgt_system" default="1" />
        <arg name="tgt_component" default="1" />
        <arg name="log_output" default="screen" />
        <arg name="fcu_protocol" default="v2.0" />
        <arg name="respawn_mavros" default="false" />

        <arg name="port" default="_port:=/dev/ttyUSB0" />
        <arg name="baud" default="_baud:=115200" />
        <include file="$(find mavros)/launch/node.launch">
                <arg name="pluginlists_yaml" value="$(find mavros)/launch/px4_pluginlists.yaml" />
                <arg name="config_yaml" value="$(find mavros)/launch/px4_config.yaml" />
                <arg name="fcu_url" value="$(arg fcu_url)" />
                <arg name="gcs_url" value="$(arg gcs_url)" />
                <arg name="tgt_system" value="$(arg tgt_system)" />
                <arg name="tgt_component" value="$(arg tgt_component)" />
                <arg name="log_output" value="$(arg log_output)" />
                <arg name="fcu_protocol" value="$(arg fcu_protocol)" />
                <arg name="respawn_mavros" value="$(arg respawn_mavros)" />

        </include>
        <node pkg="offboard_py" type="offb_node.py" name="offb_node_py" required="true" output="screen" />
                <node pkg="rosserial_python" type="serial_node.py" name="serial_node_py" required = "true" output="screen" args="$(arg baud) $(arg port)"/>
        <node pkg="offboard_py" type="rpi_to_oring.py" name="rpi_to_oring_py" required="true" output="screen" />
</launch>
```
### 參考:

1. ROS with MAVROS Installation Guide:
https://docs.px4.io/main/en/ros/mavros_installation.html
2. MAVROS Offboard control example (Python):
https://docs.px4.io/main/en/ros/mavros_offboard_python.html

---

## ROS1/2 bridge設置
照著github註解做，就可以安裝完了
https://github.com/NCTU-AUV/orca_ros_bridge

## handsfree IMU設置

https://github.com/NCTU-AUV/imu_pkg
如果有找不到ros-noetic-imu-tools 或 ros-noetic-rviz-imu-plugin 的錯誤:
參考飛控板的安裝：ros-install
如果還有缺東西就是改rosinstall_generator的imu_tools
改成缺的package 注意底線或減號
```
mkdir -p handsfree_ros_ws/src
cd handsfree_ros_ws
wstool init src
rosinstall_generator --rosdistro noetic imu_tools | tee  /tmp/handsfree.rosinstall
wstool merge -t src /tmp/handsfree.rosinstall
wstool update -t src -j4
rosdep install --from-paths src --ignore-src -y
```
## rosserial_python設置 
Rpi跟stm32做ros通訊要用的，依照下面文章操作。
1. 做完Rpi的ROS端設定就好:
https://hackmd.io/@JINGCCC/rosserial_1#ROS%E7%AB%AF%E5%AE%89%E8%A3%9D%E8%A8%AD%E5%AE%9A
2. Extend到rosserial到其他workspace，這樣可以分享到rosserial_python這個package，例如使用指令:
```
cd ~/px4_ws
catkin config --extend ~/rosserial_ws/devel
cakin build
```
---
## 備份Rpi
1. https://yanwei-liu.medium.com/raspberry-pi%E5%AD%B8%E7%BF%92%E7%AD%86%E8%A8%98-%E5%85%AD-%E5%82%99%E4%BB%BD%E9%82%84%E5%8E%9F%E6%A8%B9%E8%8E%93%E6%B4%BE%E7%9A%84microsd%E5%8D%A1-2c5ec79061f9
2. https://blog.csdn.net/ChenWenHaoHaoHao/article/details/130779705

