Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add a page on how to setup SITL on Mac OSX #3307

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion dev/source/docs/SITL-setup-landingpage.rst
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,14 @@
Setting Up SITL
===============

These documents discuss how to install and setup the SITL environment in Linux and Windows
These documents discuss how to install and setup the SITL environment in Linux, Windows and Mac OSX

.. toctree::
:maxdepth: 1

SITL setup on Linux <setting-up-sitl-on-linux>
SITL setup on Windows <sitl-native-on-windows>
SITL setup on MacOSX <sitl-native-on-macosx>
SITL setup on Windows in a VM <setting-up-sitl-on-windows>
SITL setup using Vagrant <setting-up-sitl-using-vagrant>

Expand Down
137 changes: 137 additions & 0 deletions dev/source/docs/setting-up-sitl-on-macosx.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,137 @@
.. _setting-up-sitl-on-macosx:

===========================
Setting up SITL on Mac OS X
===========================

This page describes how to setup the :ref:`SITL (Software In The Loop) <sitl-simulator-software-in-the-loop>` on Mac OS X. The specific commands were tested on macOS Catalina 10.15.7 and macOS Big Sur 11.1.

Overview
========

The SITL simulator allows you to run Plane, Copter or Rover without any
hardware. It is a build of the autopilot code using an ordinary C++
compiler, giving you a native executable that allows you to test the
behaviour of the code without hardware.

SITL runs natively on Linux, Windows and Mac OSX.

.. image:: ../images/SITL_macosx.png
:target: ../_images/SITL_macosx.png


Install steps
=============

This guide assumes that a build environment is already setup for Ardupilot. If that is not the case please follow the available guides to setup the build environment for Mac OSX. The following step provide a condensed versions of the steps to take.


Prerequisites
-------------

The following tools need to be installed to complete the installation. Between brackets the version used by the author at time of writing.

* Xcode (12.4)
* Xcode commandline tools (12.4)
* Homebrew (3.0.4-52-gbc37074)

Preparing Python
----------------

Install the required python 3 version using homebrew and activate it. If you are familiar with pyenv or virtualenv it might be better to use one of those to setup the environment. This limits potential problems with other tools that require specific versions of python. How to do that is beyond the scope of this document.

The following steps install python version 3.9.x for system wide usage.

::

brew install [email protected]
Copy link
Contributor

@timtuxworth timtuxworth Jan 8, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I found that in order to get ArduPilot to compile using waf on an M1Pro Mac running OSX 12.1 Monterey I had to install Python 3.10 from python.org. But this version of python will not run SITL. To both compile using waf and run SITL the only version (2022 April 3) I have found to work is Anaconda python from anaconda.com.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wondering if we need two sub-guides for macOS - one for Intel based machines and one for M1 versions. The default install locations for brew are different for each architecture and I suspect we'll keep seeing more differences as more functionality is documented. For example the instructions for adding other simulators like Ignition will be specialised for each architecture.

Copy link

@ntamas ntamas Apr 3, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I can compile ArduPilot just fine with Python 3.9 on an M1-based MacBook Pro so I don't think you'll need Python 3.10. I'm using Python 3.9 from Homebrew.

Edit: I usually don't install mavproxy because it brings lots of dependencies with it that I don't need, so the differences outlined above may be due to mavproxy dependencies. Also, note that a few months have passed since January so it may be the case that some dependencies of mavproxy have published pre-built M1 or universal2 wheels on PyPI since then.

export PATH="$(brew --prefix)/opt/python/libexec/bin":$PATH

pip install wheel
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This command may require sudo. Best practice to install for the local user is to use the --user option to install for the user only.

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure how much we want to dive into this in the instructions, but in my experience the best is to isolate ArduPilot-related Python dependencies in a virtualenv; what I do is as follows:

python3 -m venv .venv
.venv/bin/pip install -U pip wheel future pymavlink mavproxy

and then I run everything from the virtualenv. This way I don't mess up the Homebrew-installed Python with random dependencies that are needed for ArduPilot only.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm pretty sure it would help to put this in the instructions. Setting up a workable M1 Mac dev environment should be easy and without clear instructions it's definitely not. When you say "run everything from the virtualenv" - you mean mavproxy.py or waf or both?

What I find now is that waf needs a different python environment to mavproxy, but sim_vehicle calls both waf and mavproxy so I can't get sim_vehicle->waf to use a different python from sim_vehicle->maxproxy.

For now in order to get sim_vehicle to work, I just run it 'headless', which seems like what @srmainwaring and others are doing but I shouldn't have to do that.

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have to admit I don't use mavproxy in the virtualenv because I'm using my own tools to talk to the simulator. mavproxy is problematic because it insists on bringing the entire wxPython distribution with it and I don't need that. wxPython currently provides pre-compiled wheels for Intel Macs only on PyPI, and only for Python 3.9, so until wxPython publishes newer wheels one would have to compile wxPython from source.

So, ultimately my goal was to get the build process running and to be able to run most of the non-GUI scripts from the Tools folder (e.g., the one that builds the bootloaders). For that, a virtualenv is fine, the only catch is that some of the scripts invoke other Python scripts or waf in a way that it does not respect the current Python interpreter setting (like this one ) and just fall back to whatever Python is at the front of the system PATH. Activating the virtualenv from the shell with source .venv/bin/activate does the trick in 99% of the cases, for instance it ensures that waf picks up the correct (virtualenv-based) Python instead of the system Python.

Edit: I tried the wxPython snapshot builds from here; it seems like they have universal2 wheels for Python 3.9 there so there would be no need to compile wxPython from source, but it did not work for me so I just gave up on GUI stuff.

pip install future pymavlink mavproxy
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Using Python 3.10 from Python.org for Mac M1 ARM version, this will fail with the following errors:
wx/svg/_nanosvg.c:20946:19: error: implicit declaration of function '_PyGen_Send' is invalid in C99 [-Werror,-Wimplicit-function-declaration]
ret = _PyGen_Send((PyGenObject*)yf, value == Py_None ? NULL : value);
^
wx/svg/_nanosvg.c:20946:17: warning: incompatible integer to pointer conversion assigning to 'PyObject ' (aka 'struct _object ') from 'int' [-Wint-conversion]
ret = _PyGen_Send((PyGenObject
)yf, value == Py_None ? NULL : value);
^ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
wx/svg/_nanosvg.c:20951:19: error: implicit declaration of function '_PyGen_Send' is invalid in C99 [-Werror,-Wimplicit-function-declaration]
ret = _PyGen_Send((PyGenObject
)yf, value == Py_None ? NULL : value);
^
wx/svg/_nanosvg.c:20951:17: warning: incompatible integer to pointer conversion assigning to 'PyObject ' (aka 'struct _object ') from 'int' [-Wint-conversion]
ret = _PyGen_Send((PyGenObject
)yf, value == Py_None ? NULL : value);
^ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
wx/svg/_nanosvg.c:21035:19: error: implicit declaration of function '_PyGen_Send' is invalid in C99 [-Werror,-Wimplicit-function-declaration]
ret = _PyGen_Send((PyGenObject
)yf, NULL);
^
wx/svg/_nanosvg.c:21035:17: warning: incompatible integer to pointer conversion assigning to 'PyObject *' (aka 'struct _object ') from 'int' [-Wint-conversion]
ret = _PyGen_Send((PyGenObject
)yf, NULL);

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not totally sure what these lines do, but it seems that there is some code in _nanosvg.c that needs to be fixed.


Optional: Add the following to the either .bashrc or .zshrc to enable this version of python be default

::

export PATH="$(brew --prefix)/opt/python/libexec/bin":$PATH


Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's also useful to add

################################################################################
# ARDUPILOT
#
export PATH=$PATH:$HOME/Code/ardupilot/ardupilot/Tools/autotest
export ARDUPILOT_ROOT=$HOME/Code/ardupilot/ardupilot

to the shell initialisation scripts while you're at it. I put customisations including the PATH modification for Python in ~/.zprofile.

Copy link
Contributor

@timtuxworth timtuxworth Apr 3, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This may cause problems if you run more than one clone in different directories. I often do that.

Optional: Install sfml (used by sitl for audio and osd)

::

brew install sfml

Note that not all sfml features work on Mac OSX currently. The following error can appear `Cannot create a window from a worker thread. (OS X limitation)`, this is a result of Mac OSX specifics that haven't been catered for in the sitl code yet.

Start SITL simulator
--------------------

.. warning::

Due to ideosyncracies with Python and using screens there are limitations when Python needs to use the screen to display a graphical UI. Python 3.9 solves part of the problem by no longer requiring a separate pythonw binary. However the requirement to run on the main display still stands. When observing the error "This program needs access to the screen. Please run with a Framework build of python, and only when you are logged in on the main display of your Mac.", disconnect external displays during startup. When SITL is started the windows can be moved around as normal.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

sp.

- ideosyncracies
+ idiosyncrasies

Copy link
Contributor

@timtuxworth timtuxworth Apr 3, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I haven't been able to get this to work at all on my M1 Mac. So I just run SiTL with no UI. It works fine and I connect to the running model with with QGroundControl which shows the map and all the messages.
Maybe the wiki should say the UI is optional?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@timtuxworth unfortunately I don't have a M1 to test at present - still Intel based machines for me. When I get round to upgrading my macbook I'll take another look at the UI modules in MAVProxy which seem to be causing the problems. As you suggest it might be worth emphasising the difference between a bare bones SITL install (with alternative GCS) and full MAVProxy support for macOS which should have all the UI elements functioning correctly.

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Indeed; it seems like most of the difficulties that arise in installing SITL stuff on an M1 Mac are due to the GUI stuff. I run the SITL without a GUI and I haven't encountered any significant issues.


To start the simulator first change directory to the vehicle directory.
For example, for the multicopter code change to **ardupilot/ArduCopter**:

::

cd ardupilot/ArduCopter

The sim_vehicle script checks several environment variables to determine the OS it's running on. For Mac OSX it
requires that DISPLAY is set.

::

export DISPLAY=:0.0
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Don't you need the macOS X environment installed for this to work?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've not needed to set the DISPLAY when running SITL (no need for an X server that I'm aware of?)


Then select the correct python environment (if not enabled by default)

::

export PATH="$(brew --prefix)/opt/python/libexec/bin":$PATH

Then start the simulator using **sim_vehicle.py**. The first time you
run it you should use the -w option to wipe the virtual EEPROM and load
the right default parameters for your vehicle.

::

sim_vehicle.py -w

After the default parameters are loaded you can start the simulator
normally. First kill the sim_vehicle.py you are running using Ctrl-C. Then:

::

sim_vehicle.py --console --map

.. tip::

`sim_vehicle.py <https://github.com/ArduPilot/ardupilot/blob/master/Tools/autotest/sim_vehicle.py>`__
has many useful options, ranging from setting the simulation speed
through to choosing the initial vehicle location. These can be listed by
calling it with the ``-h`` flag (and some are demonstrated in :ref:`Using SITL for ArduPilot Testing <using-sitl-for-ardupilot-testing>`).

.. tip::

If the map tiles don't load, you can temporarily change the map provider in the map window by clicking View/Service.
To keep the new map service between launches, add the following lines to the end of your ".bashrc" (change MicrosoftHyb to the provider you want):

::

export MAP_SERVICE="MicrosoftHyb"


Learn MAVProxy
--------------

To get the most out of SITL you really need to learn to use MAVProxy.
Have a read of the :ref:`MAVProxy documentation <mavproxy:home>`. Enjoy flying!


Next steps
==========

After installation, see :ref:`Using SITL for ArduPilot Testing <using-sitl-for-ardupilot-testing>` for guidance on flying and testing with SITL.
Binary file added dev/source/images/SITL_macosx.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.