Raspberry Pi with ROS for IoT
Published:
Raspberry Pi with ROS for IoT
OS selection
Raspberry pi is a very convenient platform for prototype development and application in Precision Agriculture and IoT. The hardware support multiple Linux Operation Systems. The official one is Raspbian which has good hardware support and low-level GPIO operations. The Ubuntu Mate is very user-friendly with ROS. If you want to build a ROS system, Ubuntu is strongly suggested. This tutorial mainly summarizes the applications in Rpi-Ubuntu. The Ubuntu-20.04 has been offcially supported on Raspberry pi and Python3 is offcially supported on the corresponded ROS version, Noetic. The suggested sofware are listed as follows:
Network setting up
After installing OS on RPI SD card, the first thing to do is setting up the network. For my developing case, I often have a RPI and a laptop/desktop (Ubuntu). As we need to install many applications on RPI, we need to visit RPI and have it connected to the Internet. The common way is to share Internet from your computer to RPI, which needs an Ethernet cable. From UI of your Ubuntu computer, it is easy to share a network and you may want to change the default IP address. You just need to add the IP range in the file of your laptop Ubuntu: /etc/NetworkManager/system-connections/connection_name, add “address1=192.168.4.1/50,192.168.4.1” in “ipv4” block. This might be essential, if you want to configure your RPI as a Hotspot, as the default address is 10.42.0.** for the cases of network sharing.
Real Time Clock configuration for Ubuntu
If your RPI can go on the Internet, RTC is not essential, as RPI will sync the time from the Internet by default. If not, a RTC module might be needed. We use DS1307 as a case. The other module is similar. The ROS uses system by default, so it is better to have a real time on your RPI.
- Install i2c tools on Rpi:
sudo apt-get install i2c-tools
- Check i2c devices on ubuntu (address: 68). DONOT CONNECT YOUR CLOCK NOW
sudo i2cdetect -y 1
- Create a file /etc/systemd/system/ds1307.service for starting the RTC hardware set up:
[Unit]
Description=Enable DS1307 I2C RTC
[Service]
Type=oneshot
ExecStartPre=/bin/bash -c "echo ds1307 0x68 | tee /sys/class/i2c-adapter/i2c-1/new_device"
ExecStart=/sbin/hwclock -s
[Install]
WantedBy=basic.target
Start the device when booting:
sudo systemctl enable ds1307.service
sudo systemctl start ds1307.service
- Setup the time manually and write it to RTC module
sudo date -s "DAY MONTH YEAR HR:MN:SC" sudo hwclock -w
- Verify the date has been saved:
sudo hwclock -r
- Reboot and check the system time
date
Image file generation and shrink
- Find your SD card path:
lsblk
- Back up your system:
sudo dcfldd if=/dev/path/to/rpi/sd-card of=/dest/to/*.img
- Shrink the backuped system:
wget https://raw.githubusercontent.com/Drewsif/PiShrink/master/pishrink.sh chmod +x pishrink.sh sudo pishrink.sh *.img
- Upload the image file to your cloud for backing up
USB devices detection and rename
When multiple USB devices are connected to your laptop, you normally do not want to use the system specified devices name, like ttyACM0 to recognize your devices, because if you unplug them, the name will be messed. You can have your system recognize the device with the attributes obtained from the ports.
- Find attributes of the devices. If the UUID is provided, it is best to use that.
udevadm info /dev/ttyACM2
- Set up rules and write it up into file: cd etc/udev/rules.d/99-usb-serial.rules. One example of the rule file:
# usb port setting for MC160 controller
SUBSYSTEM=="tty", ACTION=="add", ATTRS{product}=="Arduino Due Prog. Port", MODE="666", SYMLINK+="Arduino_due"
# piksi's uuid: 203aea3c5593491982cf76e8ca8b9845 /dev/ttyACM*
ACTION=="add", SUBSYSTEM=="tty", ENV{ID_SERIAL_SHORT}=="203aea3c5593491982cf76e8ca8b9845", SYMLINK+="piksi", MODE="666"
- Reboot your RPI.