Spring:
git clone https://github.com/ArduPilot/ardupilot
cd ardupilot
chmod u+x Tools/environment_install/install-prereqs-ubuntu.sh
Tools/environment_install/install-prereqs-ubuntu.sh -y
sudo apt-get install gdebi python-matplotlib python-serial python-wxgtk2.8 python-lxml
wget http://firmware.ardupilot.org/Tools/APMPlanner/apm_planner_2.0.26_bionic64.deb
sudo apt install ./apm_planner_2.0.26_bionic64.deb -y
sudo apt upgrade -y
sudo apt check
cd ~/Desktop/
ln -s ~/gps-sdr-sim ./gps-sdr-sim
ln -s ~/ardupilot ardupilot
One way to supply the GPS data is using the MAVLink interface and passing messages to Ardupilot.
I could not get this to work, see update above
https://mavlink.io/en/messages/common.html#GPS_INPUT
https://mavlink.io/en/messages/common.html#HIL_GPS
https://discuss.ardupilot.org/t/fake-gps-via-mavlink-hil-gps-message/26237/3
Fall:
In the HAL of the SITL there is a EPROM parameter we can send. In MAVProxy you can run param get SIM_GPS_GLITCH* to return:
SIM_GPS_GLITCH_X 0.000000 SIM_GPS_GLITCH_Y 0.000000 SIM_GPS_GLITCH_Z 0.000000
The value set here will be passed directly into ardupilot/libraries/AP_HAL_SITL/sitl_gps.cpp (1258):
Vector3f glitch_offsets = _sitl->gps_glitch; d.latitude += glitch_offsets.x; d.longitude += glitch_offsets.y; d.altitude += glitch_offsets.z;
This is a floating point number that will directly be offset to the current simulated position. The resulting position will be sent over the simulated serial pipe.
To help automate test I used two libraries pymavlink and dronekit:
I made a test spoofing script here that has a few helper functions and hardcoded spoofs for X direction:
from pymavlink import mavutil, mavwp
import time
import json
from dronekit import connect, VehicleMode, LocationGlobalRelative
# run sim_vehicle.py -w --console
# pymavlink is used to send raw MAV frames
mavlink = mavutil.mavlink_connection('udpin:0.0.0.0:14551')
# dronekit is used to simiplify to non-raw frames
vehicle = connect('127.0.0.1:14550', wait_ready=True)
time.sleep(2) # Wait for connection
def arm_and_takeoff(aTargetAltitude):
"""
Arms vehicle and fly to aTargetAltitude.
This code is used under Apache 2.0 from Dronekit
https://dronekit-python.readthedocs.io/en/latest/examples/simple_goto.html
Is is unmodified except to provide this copyright notice in accorandance
the FOSS license
"""
print("Basic pre-arm checks")
# Don't try to arm until autopilot is ready
while not vehicle.is_armable:
print(" Waiting for vehicle to initialise...")
time.sleep(1)
print("Arming motors")
# Copter should arm in GUIDED mode
vehicle.mode = VehicleMode("GUIDED")
vehicle.armed = True
# Confirm vehicle armed before attempting to take off
while not vehicle.armed:
print(" Waiting for arming...")
time.sleep(1)
print("Taking off!")
vehicle.simple_takeoff(aTargetAltitude) # Take off to target altitude
# Wait until the vehicle reaches a safe height before processing the goto
# (otherwise the command after Vehicle.simple_takeoff will execute
# immediately).
while True:
print(" Altitude: ", vehicle.location.global_relative_frame.alt)
# Break and return from function just below target altitude.
if vehicle.location.global_relative_frame.alt >= aTargetAltitude * 0.95:
print("Reached target altitude")
break
time.sleep(1)
def setGlitch(axis, size):
#build parameter string
parameter = "SIM_GPS_GLITCH_" + axis.upper()
# Size is a floating point REAL32 to add to axis
mavlink.mav.param_set_send(
mavlink.target_system, mavlink.target_component,
parameter,
size,
mavutil.mavlink.MAV_PARAM_TYPE_REAL32
)
#Adjust airspeed with dronekit
vehicle.airspeed = 40
#takeoff 10 m
arm_and_takeoff(10)
#Print home
print(mavlink.location())
#Hardcoded glitch
setGlitch("x", 0.0002)
time.sleep(4)
setGlitch("x", 0.0004)
time.sleep(4)
setGlitch("x", 0.0006)
time.sleep(4)
setGlitch("x", 0.0008)
time.sleep(4)
setGlitch("x", 0.001)
time.sleep(4)
When we run the SITL simulator with the sim_vehicle.py -w --console, it opens two ports 14550 and 14551. The above python script will connect to the ports once we launch the simulator.
PS: (1) we need to install 'dronekit' Python package. On Ubuntu, 'sudo pip install dronekit'
(2) Assume we already installed MAVLink Python package, which has pymavlink package.
In addition to using this script I add this to a file ~/.mavinit.src:
module load map
map set showsimpos 1
This sets the map up how use it showing the real location.