From ed24f03294da7074bd45ac0494ed9f37e93b11fa Mon Sep 17 00:00:00 2001 From: Thomas Neill Date: Wed, 18 Oct 2023 12:23:23 -0700 Subject: [PATCH] Add ardupilot dockerfile (#23) --- .devcontainer/Dockerfile | 2 +- .devcontainer/devcontainer.json | 10 +- .gitignore | 6 +- Dockerfile | 79 ++++++++--- README.md | 238 ++++++-------------------------- sim_instructions.md | 39 ++++++ 6 files changed, 150 insertions(+), 224 deletions(-) create mode 100644 sim_instructions.md diff --git a/.devcontainer/Dockerfile b/.devcontainer/Dockerfile index 3b891477..122b344d 100644 --- a/.devcontainer/Dockerfile +++ b/.devcontainer/Dockerfile @@ -1 +1 @@ -FROM epedley/uavf_2024-dev \ No newline at end of file +FROM t0mmyn/uavf_2024 \ No newline at end of file diff --git a/.devcontainer/devcontainer.json b/.devcontainer/devcontainer.json index e07e75b7..8afd1697 100644 --- a/.devcontainer/devcontainer.json +++ b/.devcontainer/devcontainer.json @@ -9,16 +9,16 @@ "workspaceFolder": "/home/ws/uavf_2024", "workspaceMount": "source=${localWorkspaceFolder},target=/home/ws/uavf_2024,type=bind", "containerEnv": { - "DISPLAY": "unix:0", - "ROS_LOCALHOST_ONLY": "1", - "ROS_DOMAIN_ID": "42" + "DISPLAY": "unix:0" }, + "mounts": [ + "source=/tmp/.X11-unix,target=/tmp/.X11-unix,type=bind,consistency=cached" + ], "runArgs": [ "--net=host", "-e", "DISPLAY=${env:DISPLAY}" ], "forwardPorts": [ 6080 - ], - "postCreateCommand": "echo 'source /opt/ros/humble/setup.sh' > /root/.bashrc" + ] } \ No newline at end of file diff --git a/.gitignore b/.gitignore index f37cfb64..c4fd38e2 100644 --- a/.gitignore +++ b/.gitignore @@ -1,4 +1,8 @@ c_generated_code acados_ocp_nlp.json __pycache__ -*.egg-info \ No newline at end of file +*.egg-info +mav.* +terrain +eeprom.bin +logs diff --git a/Dockerfile b/Dockerfile index b76f2a14..0367356c 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,23 +1,56 @@ -FROM ros:humble - -RUN apt update -RUN apt-get update --fix-missing -RUN apt-get install -y python3-pip -RUN apt-get install -y ffmpeg libsm6 libxext6 -# RUN apt install -y curl -# RUN apt-get install -y wget - -# RUN git clone --recursive https://github.com/PX4/PX4-Autopilot.git /home/ws/PX4-Autopilot -# RUN curl -sSL http://get.gazebosim.org | sh - -# WORKDIR "/home/ws/PX4-Autopilot" -# RUN Tools/setup/ubuntu.sh -# RUN make px4_sitl -WORKDIR "/home/ws/uavf_2024" -COPY setup.py setup.py -RUN pip install -e . - -CMD ["/bin/bash"] - - -# docker build -t epedley/uavf_2024-dev . \ No newline at end of file +FROM ardupilot/ardupilot-dev-ros + +RUN apt-get update + +# opencv dependencies (https://stackoverflow.com/questions/55313610/importerror-libgl-so-1-cannot-open-shared-object-file-no-such-file-or-directo) +RUN apt-get install -y ffmpeg libsm6 libxext6 + +# utils (not strictly necessary) +RUN apt-get install -y tmux vim + +# comment this out if you have a GPU +RUN pip3 install torch torchvision --index-url https://download.pytorch.org/whl/cpu + +WORKDIR "/home/ws/uavf_2024" +COPY setup.py setup.py +RUN pip install -e . + + +RUN apt-get install -y default-jre socat ros-humble-geographic-msgs ros-dev-tools + +WORKDIR /root/ros2_ws/src + +RUN wget https://raw.githubusercontent.com/ArduPilot/ardupilot/master/Tools/ros2/ros2.repos +RUN vcs import --recursive < ros2.repos +RUN wget https://raw.githubusercontent.com/ArduPilot/ardupilot_gz/main/ros2_gz.repos +RUN vcs import --recursive < ros2_gz.repos +ARG GZ_VERSION=garden + +RUN apt-get -y install lsb-release wget gnupg +RUN wget https://packages.osrfoundation.org/gazebo.gpg -O /usr/share/keyrings/pkgs-osrf-archive-keyring.gpg +RUN echo "deb [arch=$(dpkg --print-architecture) signed-by=/usr/share/keyrings/pkgs-osrf-archive-keyring.gpg] http://packages.osrfoundation.org/gazebo/ubuntu-stable $(lsb_release -cs) main" | sudo tee /etc/apt/sources.list.d/gazebo-stable.list > /dev/null +RUN apt-get update +RUN apt-get -y install gz-garden + + +WORKDIR /root/ros2_ws +RUN apt update +RUN rosdep update +RUN rosdep install --rosdistro ${ROS_DISTRO} --from-paths src -i -r -y +RUN bash -c "source /opt/ros/humble/setup.bash && colcon build --cmake-args -DBUILD_TESTING=ON" + +RUN echo 'source /opt/ros/humble/setup.bash' >> ~/.bashrc +RUN echo 'source /root/ros2_ws/install/setup.bash' >> ~/.bashrc + +RUN echo 'export PATH=/root/ros2_ws/src/ardupilot/Tools/autotest:$PATH' >> ~/.bashrc +RUN echo 'export PATH=/usr/lib/ccache:$PATH' >> ~/.bashrc + +RUN sudo apt-get install -y python3-dev python3-opencv python3-wxgtk4.0 python3-pip python3-matplotlib python3-lxml python3-pygame +RUN pip3 install PyYAML mavproxy --user +RUN echo 'export PATH="$PATH:$HOME/.local/bin"' >> ~/.bashrc + +RUN cp src/ardupilot/Tools/vagrant/mavinit.scr /root/.mavinit.scr +# i love python env management!!! <.< +RUN python -m pip uninstall matplotlib -y + +CMD ["bash"] diff --git a/README.md b/README.md index 768088b1..6a6b9a04 100644 --- a/README.md +++ b/README.md @@ -1,194 +1,44 @@ -# UAV Forge's ROS2 package for GN&C and Aerial Imagery Object Detection. - -## Dev Container Setup -1. Clone the repo -2. In VSCode, open the command palette and run `rebuild and reopen in dev container` -3. To verify your setup, run `run_tests.sh` - -## Usage - -1. Start the DDS agent. - ``` - MicroXRCEAgent udp4 -p 8888 - ``` - -2. In a new terminal window, cd into PX4-Autopilot and start the SITL. - ``` - sudo make px4_sitl gazebo-classic - ``` - -3. In a new terminal window, run your roslaunch or script. - - -## Install instructions - -### Install required and local Python libraries - -1. cd into this repo's root directory. - -2. Run: - ``` - pip install -e . - ``` - - -### Dev container - -1. Open this project in vscode -2. Install the "Dev Containers" extension -3. Open the command pallete (ctrl-shift-p), then search for and execute "Dev Containers: (Re-)build and Reopen in Container" -4. Congratulations, you get to skip all those tedious steps to install ROS 2 manually, and your environment is isolated from the rest of your computer -5. To make downloading dependencies reproducible, add any important software installation steps to the Dockerfile in this repo. -6. To use git inside the docker container, you may have to manually log in to GitHub again if the built-in credential forwarding isn't working. I recommend using the [GitHub CLI](https://cli.github.com/) to do this. -7. If you want to use the simulator: - 1. If you want to run in headless, `cd /home/ws/PX4-Autopilot` then `HEADLESS=1 make px4_sitl gazebo-classic` - 2. If you want it to run it in a GUI, one way is using the remote desktop environment in the dev container. Open `localhost:6080` in a web browser, then enter password `vscode`, then use the menu in the bottom left to open a terminal, `cd /home/ws/PX4-Autopilot`, then run `make px4_sitl gazebo-classic`. - - -I copied a lot of the config from this tutorial: https://docs.ros.org/en/foxy/How-To-Guides/Setup-ROS-2-with-VSCode-and-Docker-Container.html - - -### Manual - -#### [ROS 2 Foxy for Ubuntu 20.04 Focal](https://docs.ros.org/en/foxy/Installation/Ubuntu-Install-Debians.html) - -1. Uninstall ROS Noetic. - ``` - sudo apt-get remove ros-* - sudo apt-get autoremove - ``` - Remove the installations of Noetic in /opt/ros and /mnt/c/opt/ros. - ``` - cd /opt/ros - sudo rm -r noetic - cd /mnt/c/opt/ros - sudo rm -r noetic - ``` - Edit your ~/.bashrc and remove all ROS related commands. - -2. Make sure locale has UTF-8. - ``` - locale # check for UTF-8 - sudo apt update && sudo apt install locales - sudo locale-gen en_US en_US.UTF-8 - sudo update-locale LC_ALL=en_US.UTF-8 LANG=en_US.UTF-8 - export LANG=en_US.UTF-8 - locale # verify settings - ``` - -3. Set up sources. - ``` - sudo apt install software-properties-common - sudo add-apt-repository universe - sudo apt update && sudo apt install curl -y - sudo curl -sSL https://raw.githubusercontent.com/ros/rosdistro/master/ros.key -o /usr/share/keyrings/ros-archive-keyring.gpg - echo "deb [arch=$(dpkg --print-architecture) signed-by=/usr/share/keyrings/ros-archive-keyring.gpg] http://packages.ros.org/ros2/ubuntu $(. /etc/os-release && echo $UBUNTU_CODENAME) main" | sudo tee /etc/apt/sources.list.d/ros2.list > /dev/null - ``` - -4. Install ROS 2 packages. - ``` - sudo apt update - sudo apt upgrade - sudo apt install ros-foxy-desktop python3-argcomplete # full install with extra visualizer tools and tutorials (RECOMMENDED) - sudo apt install ros-foxy-ros-base python3-argcomplete # only has communication libraries and command line tools - sudo apt install ros-dev-tools - ``` - -5. Source environment and test if it's working. - ``` - source /opt/ros/foxy/setup.bash - echo 'source /opt/ros/foxy/setup.bash' >> ~/.bashrc - ros2 run demo_nodes_cpp talker - ``` - In another window run: - ``` - ros2 run demo_nodes_py listener - ``` - -6. Not sure what this is but PX4 says to install it. - ``` - pip install --user -U empy pyros-genmsg setuptools - ``` - - -### [PX4 SITL for Ubuntu 20.04 Focal](https://docs.px4.io/main/en/ros/ros2_comm.html#foxy) - -1. Install PX4 development environment in your directory of choice. - ``` - git clone https://github.com/PX4/PX4-Autopilot.git --recursive - bash ./PX4-Autopilot/Tools/setup/ubuntu.sh - cd PX4-Autopilot/ - make px4_sitl - ``` - - -### [Micro XRCE-DDS Agent and Client (ROS 2 Middleware)](https://docs.px4.io/main/en/ros/ros2_comm.html#foxy) - -1. Clone the DDS agent library to your directory of choice. - ``` - 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/ - ``` - If you get an error relating to "ASIO_INCLUDE_DIR" when running "make", install this package: - ``` - sudo apt-get install libasio-dev - ``` - -2. Start the DDS agent. - ``` - MicroXRCEAgent udp4 -p 8888 - ``` - -3. The PX4 SITL contains the DDS client. Run the SITL in a new window. - ``` - sudo make px4_sitl gazebo-classic - ``` - If you get a build error, check if you have any existing installations of ROS Noetic and delete them. - After it successfully builds, you should see the simulation environment in one window and the DDS agent outputting "INFO" messages in its terminal window. - - -### [Set up PX4 ROS 2 workspace](https://docs.px4.io/main/en/ros/ros2_comm.html#foxy) - -1. Create a ROS 2 workspace and name it whatever you want. This is where all you develop in. - ``` - mkdir -p ros2_ws/src - ``` - -2. Clone PX4's packages and [our custom messages](https://github.com/Herpderk/uavf_msgs/tree/master/msg) into your ROS 2 workspace and compile it. - ``` - cd ros2_ws/src - git clone https://github.com/PX4/px4_msgs.git - git clone git@github.com:uci-uav-forge/uavf_ros2_msgs.git - git clone git@github.com:uci-uav-forge/uavf_2024.git - cd .. - source /opt/ros/foxy/setup.bash - ``` - Everytime you make a change to your workspace, rebuild with this command: - ``` - colcon build --merge-install - ``` - If you compile with only "colcon build", you will get import errors with your ROS message types. - - -3. Run this command after everytime you've built the workspace: - ``` - source install/setup.bash - ``` - -4. Test it out with an example - ``` - ros2 run uavf_2024 commander_node.py - ``` - - -### Acados Installation (for MPC usage) - -1. [Follow these instructions to install Acados.](https://docs.acados.org/installation/) - -2. [Install the Python interface afterwards.](https://docs.acados.org/python_interface/index.html) +# UAV Forge's ROS2 package for GN&C and Aerial Imagery Object Detection. + +## Dev Container Setup +1. Clone the repo +2. In VSCode, open the command palette and run `rebuild and reopen in dev container` +3. To verify your setup, run `run_tests.sh` + +## Usage + +1. Refer to `sim_instructions.md` for instructions on starting and running the simulation. + + +## Install instructions + +### Install required and local Python libraries + +1. cd into this repo's root directory. + +2. Run: + ``` + pip install -e . + ``` + + +### Dev container + +1. Open this project in vscode +2. Install the "Dev Containers" extension +3. Open the command pallete (ctrl-shift-p), then search for and execute "Dev Containers: (Re-)build and Reopen in Container" +4. Congratulations, you get to skip all those tedious steps to install ROS 2 manually, and your environment is isolated from the rest of your computer +5. To make downloading dependencies reproducible, add any important software installation steps to the Dockerfile in this repo. +6. To use git inside the docker container, you may have to manually log in to GitHub again if the built-in credential forwarding isn't working. I recommend using the [GitHub CLI](https://cli.github.com/) to do this. +7. If you want to use the simulator: + 1. Follow instructions in `sim_instructions.md`. + 2. If you want it to run it in a GUI, one way is using the remote desktop environment in the dev container. Open `localhost:6080` in a web browser, then enter password `vscode`, then use the menu in the bottom left to open a terminal, `cd /home/ws/PX4-Autopilot`, then run `make px4_sitl gazebo-classic`. + 3. The X sockets should also be mounted and should work if you run `xhost +` on your machine. + + +I copied a lot of the config from this tutorial: https://docs.ros.org/en/foxy/How-To-Guides/Setup-ROS-2-with-VSCode-and-Docker-Container.html + + +### Manual + +(WIP). It's recommended to use the Dockerfile for development. \ No newline at end of file diff --git a/sim_instructions.md b/sim_instructions.md new file mode 100644 index 00000000..0ed5a9e4 --- /dev/null +++ b/sim_instructions.md @@ -0,0 +1,39 @@ +# Instructions for running the Gazebo sim + +## Set up params +``` +sim_vehicle.py -w -v ArduCopter --console -DG --enable-dds +``` +This command builds the SITL for arducopter and wipes existing params. + +Once it's started, type the following command and then control-c: +``` +param set DDS_ENABLE 1 +``` + +## Launch SITL + +## Simple SITL + +Run the following to start a simple SITL + +``` +ros2 launch ardupilot_sitl sitl_dds_udp.launch.py transport:=udp4 refs:=$(ros2 pkg prefix ardupilot_sitl)/share/ardupilot_sitl/config/dds_xrce_profile.xml synthetic_clock:=True wipe:=False model:=quad speedup:=1 slave:=0 instance:=0 defaults:=$(ros2 pkg prefix ardupilot_sitl)/share/ardupilot_sitl/config/default_params/copter.parm,$(ros2 pkg prefix ardupilot_sitl)/share/ardupilot_sitl/config/default_params/dds_udp.parm sim_address:=127.0.0.1 master:=tcp:127.0.0.1:5760 sitl:=127.0.0.1:5501 +``` + +After starting the SITL you can connect to it with MAVProxy. + +``` +mavproxy.py --console --map --aircraft test --master=:14550 +``` + +Follow https://ardupilot.org/dev/docs/copter-sitl-mavproxy-tutorial.html for instructions on using MAVProxy to control the sim. + + +### Launch Gazebo +``` +ros2 launch ardupilot_gz_bringup iris_runway.launch.py +``` +Gazebo is another option, but I found it very slow through the layer of virtualization on my machine, and harder to set up. + +