Skip to content

Commit

Permalink
Tools: improve sitl-on-hw copter handling
Browse files Browse the repository at this point in the history
  • Loading branch information
khancyr committed Jul 31, 2024
1 parent 7632927 commit a63b7c7
Show file tree
Hide file tree
Showing 2 changed files with 84 additions and 12 deletions.
7 changes: 7 additions & 0 deletions Tools/scripts/sitl-on-hardware/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,13 @@ and quadplane:
cd $HOME/ardupilot
./Tools/scripts/sitl-on-hardware/sitl-on-hw.py --board MatekH743 --vehicle plane --simclass QuadPlane

### Copter :

Only the default quad frame is enable by default, to enable another frame type, you need to enable the right compile flag :
e.g. for octa-quad frame, AP_MOTORS_FRAME_OCTAQUAD_ENABLED 1 in the hwdef file. Compile flags list is in AP_Motors_class.h
Passing --frame parameter will enable the right compile flag for you.


## Configuring

Wipe the parameters on the board; this can be done with a mavlink command, or by setting the FORMAT_VERSION parameter to 0.
Expand Down
89 changes: 77 additions & 12 deletions Tools/scripts/sitl-on-hardware/sitl-on-hw.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,34 @@
import os
import tempfile

sys.path.append(os.path.join(os.path.dirname(os.path.realpath(__file__)), '../../../Tools', 'autotest'))
from pysim import vehicleinfo

from argparse import ArgumentParser

vinfo = vehicleinfo.VehicleInfo()

vehicle_map = {
"APMrover2": "Rover",
"Copter": "ArduCopter",
"Plane": "ArduPlane",
"Sub": "ArduSub",
"Blimp": "Blimp",
"Rover": "Rover",
}
# add lower-case equivalents too
for k in list(vehicle_map.keys()):
vehicle_map[k.lower()] = vehicle_map[k]

vehicle_choices = list(vinfo.options.keys())
# add vehicle aliases to argument parser options:
for c in vehicle_map.keys():
vehicle_choices.append(c)

parser = ArgumentParser("SITL on hardware builder")
parser.add_argument("--board", default=None, help="board type")
parser.add_argument("--vehicle", default=None, help="vehicle type")
parser.add_argument("--frame", default=None, help="frame type")
parser.add_argument("-v", "--vehicle", choices=vehicle_choices, required=True, help="vehicle type")
parser.add_argument("-f", "--frame", default=None, help="frame type")
parser.add_argument("--simclass", default=None, help="simulation class")
parser.add_argument("--defaults", default=None, help="extra defaults file")
parser.add_argument("--upload", action='store_true', default=False, help="upload firmware")
Expand All @@ -22,33 +45,46 @@

extra_hwdef = None


def run_program(cmd_list):
'''run a program from a command list'''
print("Running (%s)" % " ".join(cmd_list))
retcode = subprocess.call(cmd_list)
if retcode != 0:
print("FAILED: %s" % (' '.join(cmd_list)))
global extra_hwdef
if extra_hwdef is not None:
extra_hwdef.close()
os.unlink(extra_hwdef.name)
sys.exit(1)
print("FAILED: %s" % (' '.join(cmd_list)))
global extra_hwdef
if extra_hwdef is not None:
extra_hwdef.close()
os.unlink(extra_hwdef.name)
sys.exit(1)


frame_options = sorted(vinfo.options[vehicle_map[args.vehicle]]["frames"].keys())
frame_options_string = ' '.join(frame_options)
if args.frame and args.frame not in frame_options:
print(f"ERROR: frame must be one of {frame_options_string}")
sys.exit(1)


extra_hwdef = tempfile.NamedTemporaryFile(mode='w')
extra_defaults = tempfile.NamedTemporaryFile(mode='w')


def hwdef_write(s):
'''write to the hwdef temp file'''
extra_hwdef.write(s)


def defaults_write(s):
'''write to the hwdef temp file'''
extra_defaults.write(s)


def sohw_path(fname):
'''get path to a file in on-hardware directory'''
return os.path.join(os.path.dirname(os.path.realpath(__file__)), fname)


if args.vehicle == "plane":
extra_hwdef_base = "plane-extra-hwdef-sitl-on-hw.dat"
defaults_base = "plane-default.param"
Expand All @@ -63,14 +99,41 @@ def sohw_path(fname):
defaults_write(open(sohw_path(defaults_base), "r").read() + "\n")

if args.defaults:
defaults_write(open(args.defaults,"r").read() + "\n")
defaults_write(open(args.defaults, "r").read() + "\n")

if args.simclass:
if args.simclass == 'Glider':
hwdef_write("define AP_SIM_GLIDER_ENABLED 1\n")
hwdef_write("define AP_SIM_FRAME_CLASS %s\n" % args.simclass)
if args.frame:
hwdef_write('define AP_SIM_FRAME_STRING "%s"\n' % args.frame)
if vehicle_map[args.vehicle] == "ArduCopter" or args.simclass == "MultiCopter":
frame_defines = {
"quad": "AP_MOTORS_FRAME_QUAD_ENABLED",
"+": "AP_MOTORS_FRAME_QUAD_ENABLED",
"X": "AP_MOTORS_FRAME_QUAD_ENABLED",
"cwx": "AP_MOTORS_FRAME_QUAD_ENABLED",
"djix": "AP_MOTORS_FRAME_QUAD_ENABLED",
"quad-can": "AP_MOTORS_FRAME_QUAD_ENABLED",
"hexa": "AP_MOTORS_FRAME_HEXA_ENABLED",
"hexa-cwx": "AP_MOTORS_FRAME_HEXA_ENABLED",
"hexa-dji": "AP_MOTORS_FRAME_HEXA_ENABLED",
"hexax": "AP_MOTORS_FRAME_HEXA_ENABLED",
"deca": "AP_MOTORS_FRAME_DECA_ENABLED",
"deca-cwx": "AP_MOTORS_FRAME_DECA_ENABLED",
"dodeca-hexa": "AP_MOTORS_FRAME_DODECAHEXA_ENABLED",
"octa": "AP_MOTORS_FRAME_OCTA_ENABLED",
"octa-dji": "AP_MOTORS_FRAME_OCTA_ENABLED",
"octa-cwx": "AP_MOTORS_FRAME_OCTA_ENABLED",
"octa-quad": "AP_MOTORS_FRAME_OCTAQUAD_ENABLED",
"octa-quad-cwx": "AP_MOTORS_FRAME_OCTAQUAD_ENABLED",
"y6": "AP_MOTORS_FRAME_Y6_ENABLED"
}
for frame, define in frame_defines.items():
if args.frame == frame:
print(f"Auto enabling {define} for frame {args.frame}")
hwdef_write(f'define {define} 1')
break

extra_hwdef.flush()
extra_defaults.flush()
Expand All @@ -82,12 +145,14 @@ def sohw_path(fname):
configure_args.extend(unknown_args)
run_program(configure_args)

build_cmd = ["./waf", args.vehicle]
# Remove "Ardu", "Heli", and "Antenna"
waf_vehicle = args.vehicle.lower().replace("ardu", "").replace("heli", "").replace("antenna", "")

build_cmd = ["./waf", waf_vehicle]
if args.upload:
build_cmd.append("--upload")
build_cmd.append("--upload")

run_program(build_cmd)

# cleanup
extra_hwdef.close()

0 comments on commit a63b7c7

Please sign in to comment.