OLD: auto starting ROS nodes - LoCO-AUV/loco_config GitHub Wiki
Raspberry Pi
Our system is configured to run the ROS Master Node, the MAVROS Bridge, and the pycontroller.py teleop node on the Raspberry Pi.
Static IP
This configuration requires the Raspberry Pi to have a static IP because we have a direct connection from the control laptop to the Raspberry Pi with an ethernet cable for teleop control. To do this, load up the desktop environment then go to System Settings->Network->Wired->Options->IPv4 Settings. Change the Method to Manual, then click Add to add a new static IP address. Set the address to 10.42.1.231, Netmask to 255.255.255.0, and leave the Gateway empty. Save these settings, then try connecting to the Raspberry Pi directly with an ethernet cable.
Next, edit /etc/hosts and add the following line:
10.42.1.1 -laptopHostname-
Replace -laptopHostname- with the hostname of the laptop. In our case, this was rohu.
Auto Starting the ROS Master Node, MAVROS Bridge, and other nodes on the Raspberry Pi
Script
The first step is to create a shell script to start the master node and bridge. Our script is named pixhawk_ros_startup.sh and looks like this:
#!/bin/bash
sleep 60
source /opt/ros/melodic/setup.bash
roslaunch mavros apm.launch &
#!/bin/bashtells the system to run this as a bash script.sleep 60This is in the script as more of a power management scheme, on startup the Raspberry Pi is starting a lot of really heavy programs all at once, which is causing mild power problems; Once in the robot connected to a battery I suspect that this will no longer be necessary, but until then this keeps everything from starting at the same time.source /opt/ros/melodic/setup.bashsets up the ROS commands for the script (you need to have this even if it's in your .bashrc, this gets run by the systemd user, not you or root).roslaunch mavros app.launch &starts the master node and the bridge in the background, this way the script keeps running instead of waiting for this to complete (to be clear, it never will complete).
systemd
To run this script on boot we must first create a systemd service.
First create the file /etc/systemd/system/pixhawk.service, you can name the file whatever you like, but it needs to end in .service.
Next our service looks like this (yours should too):
[Unit]
Description=Service to start MAVROS.
[Service]
ExecStart=/usr/local/bin/pixhawk_ros_startup.sh
[Install]
WantedBy=default.target
- The [Unit] section is mostly irrelevant, feel free to put whatever description you like.
- The [Service] section is the most important, in the ExecStart field put the path to the executable shell script that we created in the last section. I recommend putting it in
/usr/local/binfor safe keeping and be sure to runchmod +x <name of your script>before doing so, if it's not marked as executable there will be problems. - The [Install] section seems relevant and important, but to be honest I have no idea what it does. I suspect it handles which process spawns this one/handles when this runs.
Once this is done you must run sudo systemctl enable pixhawk.service. This will enable it to run on boot.
Useful tools
sudo systemctl status <name of service>is really useful for checking the status and some logs from the running service
Running your own Nodes on Boot
To execute your own nodes on startup you should modify the bridge startup script (be sure to modify the one in /usr/local/bin, the one that actually gets run on boot). This is our script currently that runs the pycontroller.py teleop code:
#!/bin/bash
sleep 60
source /opt/ros/melodic/setup.bash
export ROS_MASTER_URI=http://10.42.1.231:11311
export ROS_HOSTNAME=10.42.1.231
roslaunch mavros app.launch &
sleep 30
source /home/pi/pycontroller_ws/devel/setup.bash
chmod +x /home/pi/pycontroller_ws/src/pycontroller/src/pycontroller.py
rosrun pycontroller pycontroller.py
export ROS_MASTER_URI=http://10.42.1.231:11311andexport ROS_HOSTNAME=10.42.1.231set the location of the master node.sleep 30sleeps the script to give the master node and MAVROS bridge time to start up before this runs (this is very important).source /home/pi/pycontroller_ws/devel/setup.bashsets up the package for use by the systemd user.chmod +x /home/pi/pycontroller_ws/src/pycontroller/src/pycontroller.pysets our code as an executable if it's not already.rosrun pycontroller pycontroller.pyruns our node.
This process can be repeated for any other nodes one wants to start. Having a sleep before the startup of other nodes is likely not necessary so long as they don't depend on each other. The only change you need to make to run other nodes (other than putting there relevant code in) is to make all rosrun command end in an & that way they get backgrounded and the script will move on to launch the next nodes. It's also important that the last command not be backgrounded (the & backgrounds a process), this way bash keeps it running and doesn't kill the processes.
Laptop
The control laptop has only a ROS joy_node running on it. The ROS joy_node is a standard node that comes with ROS.
Connecting the laptop
Since the ROS Master Node is running on the Raspberry Pi, we need to configure the laptop to find the node. Edit the .bashrc file in your desired user's home directory. Add the following lines to the bottom of the file:
source /opt/ros/kinetic/setup.bash
ROS_HOSTNAME=10.42.1.1
ROS_MASTER_URI=http://10.42.1.231:11311
Next, edit /etc/hosts and add the following line:
10.42.1.231 -piHostname-
Replace -piHostname- with the Raspberry Pi's hostname. In our case, this was minibot.
Finally, we need to modify some network settings. In the desktop environment, go to System Settings->Network->Wired->Options->IPv4 Settings and change Method to Shared to other computers.