-
Notifications
You must be signed in to change notification settings - Fork 32
/
README.md
184 lines (125 loc) · 7.8 KB
/
README.md
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
# ROS2_PX4_Offboard_Example
## Overview
This tutorial explains at a basic level how to use ROS2 and PX4 in order to control a simulated UAV's velocity with keyboard controls. The goal is to create a simple example that a complete beginner can follow and understand, even with no ROS2 or PX4 experience.
This repo is a derivative of Jaeyoung Lim's Offboard example
https://github.com/Jaeyoung-Lim/px4-offboard
I've taken his example and added some functionality.
### Prerequisites
* ROS2 Humble
* PX4 Autopilot
* Micro XRCE-DDS Agent
* px4_msgs
* Ubuntu 22.04
* Python 3.10
## Setup Steps
### Install PX4 Autopilot
To [Install PX4](https://docs.px4.io/main/en/dev_setup/building_px4.html#download-the-px4-source-code) run this code
```
git clone https://github.com/PX4/PX4-Autopilot.git --recursive
```
### Install ROS2 Humble
To install ROS2 Humble follow the steps [here](https://docs.ros.org/en/humble/Installation/Ubuntu-Install-Debians.html)
### Install Dependencies
Install Python dependencies as mentioned in the [PX4 Docs](https://docs.px4.io/main/en/ros/ros2_comm.html#install-ros-2) with this code
```
pip3 install --user -U empy pyros-genmsg setuptools
```
I also found that without these packages installed Gazebo has issues loading
```
pip3 install kconfiglib
pip install --user jsonschema
pip install --user jinja2
```
### Build Micro DDS
As mentioned in the [PX4 Docs](https://docs.px4.io/main/en/ros/ros2_comm.html#setup-micro-xrce-dds-agent-client) run this code in order to build MicroDDS on your machine
```
git clone https://github.com/eProsima/Micro-XRCE-DDS-Agent.git
cd Micro-XRCE-DDS-Agent
mkdir build
cd build
cmake ..
make
sudo make install
sudo ldconfig /usr/local/lib/
```
### Setup Workspace
This git repo is intended to be a ROS2 package that is cloned into a ROS2 workspace.
We're going to create a workspace in our home directory, and then clone in this repo and also the px4_msgs repo.
For more information on creating workspaces, see [here](https://docs.ros.org/en/humble/Tutorials/Workspace/Creating-A-Workspace.html)
Run this code to create a workspace in your home directory
```
mkdir -p ~/ros2_px4_offboard_example_ws/src
cd ~/ros2_px4_offboard_example_ws/src
```
*ros2_px4_offboard_example_ws* is just a name I chose for the workspace. You can name it whatever you want. But after we run *colcon build* you might have issues changing your workspace name so choose wisely.
We are now in the src directory of our workspace. This is where ROS2 packages go, and is where we will clone in our two repos.
### Clone in Packages
We first will need the px4_msgs package. Our ROS2 nodes will rely on the message definitions in this package in order to communicate with PX4. Read [here](https://docs.px4.io/main/en/ros/ros2_comm.html#overview:~:text=ROS%202%20applications,different%20PX4%20releases) for more information.
Be sure you're in the src directory of your workspace and then run this code to clone in the px4_msgs repo
```
git clone https://github.com/PX4/px4_msgs.git
```
Once again be sure you are still in the src directory of your workspace. Run this code to clone in our example package
```
git clone https://github.com/ARK-Electronics/ROS2_PX4_Offboard_Example.git
```
Run this code to clone the repo
### Building the Workspace
The two packages in this workspace are px4_msgs and px4_offboard. px4_offboard is a ROS2 package that contains the code for the offboard control node that we will implement. It lives inside the ROS2_PX4_Offboard_Example directory.
Before we build these two packages, we need to source our ROS2 installation. Run this code to do that
```
source /opt/ros/humble/setup.bash
```
This will need to be run in every terminal that wants to run ROS2 commands. An easy way to get around this, is to add this command to your .bashrc file. This will run this command every time you open a new terminal window.
To build these two packages, be sure your are still in the src directory of your workspace, and run this code
```
colcon build
```
As mentioned in Jaeyoung Lim's [example](https://github.com/Jaeyoung-Lim/px4-offboard/blob/master/doc/ROS2_PX4_Offboard_Tutorial.md) you will get some warnings about setup.py but as long as there are no errors, you should be good to go.
After this runs, we should never need to build px4_msgs again. However, we will need to build px4_offboard every time we make changes to the code. To do this, and save time, we can run
```
colcon build --packages-select px4_offboard
```
If you tried to run our code now, it would not work. This is because we need to source our current workspace. This is always done after a build. To do this, be sure you are in the src directory, and then run this code
```
source install/setup.bash
```
We will run this every time we build. It will also need to be run in every terminal that we want to run ROS2 commands in.
### Running the Code
This example has been designed to run from one launch file that will start all the necessary nodes. The launch file will run a python script that uses gnome terminal to open a new terminal window for MicroDDS and Gazebo.
Run this code to start the example
```
ros2 launch px4_offboard offboard_velocity_control.launch.py
```
This will run numerous things. In no particular order, it will run:
* processes.py in a new window
* MicroDDS in a new terminal window
* Gazebo will open in a second tab in the same terminal window
* Gazebo GUI will open in it's own window
* control.py in a new window
* Sends ROS2 Teleop commands to the /offboard_velocity_cmd topic based on keyboard input
* RVIZ will open in a new window
* velocity_control.py runs as it's own node, and is the main node of this example
Once everything is running, you should be able to focus into the control.py terminal window, arm, and takeoff. The controls mimic Mode 2 RC Transmitter controls with WASD being the left joystick and the arrow keys being the right joystick. The controls are as follows:
* W: Up
* S: Down
* A: Yaw Left
* D: Yaw Right
* Up Arrow: Pitch Forward
* Down Arrow: Pitch Backward
* Left Arrow: Roll Left
* Right Arrow: Roll Right
* Space: Arm/Disarm
Pressing *Space* will arm the drone. Wait a moment and it will takeoff and switch into offboard mode. You can now control it using the above keys. If you land the drone, it will disarm and to takeoff again you will need to toggle the arm switch off and back on with the space bar.
Using the controls, click *W* to send a vertical veloctiy command and take off. Once in the air you can control it as you see fit.
## Closing Simulation *IMPORTANT*
When closing the simulation, it is very tempting to just close the terminal windows. However, this will leave Gazebo running in the background, potentially causing issues when you run Gazebo in the future. To correctly end the Gazebo simulation, go to it's terminal window and click *Ctrl+C*. This will close Gazebo and all of it's child processes. Then, you can close the other terminal windows.
## Explanation of processes.py
This code runs each set of bash commands in a new tab of a gnome terminal window. It assumes that your PX4 installation is accessible from your root directory, and it is using the gz_x500 simulation. There is no current implementation to change these commands when running the launch file, however you can modify the command string within processes.py to change these values to what you need.
If line 17 of processes.py were uncommented
```
17 # "cd ~/QGroundControl && ./QGroundControl.AppImage"
```
then QGroundControl would run in a new tab of the terminal window and the QGroundControl GUI would then open up. This is commented out by default because it is not necessary for the simulation to run, but it is useful for debugging, and is a simple example showing how to add another command to the launch file.
## Questions
Message Braden Wagstaff on the PX4 Discord for questions or email me at braden@arkelectron.com