This repository contains a collection of Lua scripts designed for use with ArduPilot-based autonomous vehicles (e.g., drones, rovers). These scripts extend ArduPilot's functionality by adding custom behaviors and automation.
This repository includes the following scripts:
- Obstacle Avoidance Script (Front LiDAR)
- Auto Mode → 90° Left → Fly 200m
- High Current Monitoring Script
This Lua script implements a basic obstacle avoidance system using a front-facing rangefinder (like LiDAR). When the vehicle is in AUTO
mode and detects an obstacle within a specified threshold, it switches to BRAKE
mode. Once the obstacle is cleared, the vehicle resumes AUTO
mode. If the vehicle remains in BRAKE
mode for a defined timeout, it switches to RTL
(Return To Launch) mode.
- Front Obstacle Detection: Uses data from a front-facing rangefinder.
- Automatic Braking: Switches to
BRAKE
mode when an obstacle is detected. - Resumption of Autonomous Flight: Resumes
AUTO
mode after the obstacle is cleared. - Timeout to RTL: Switches to
RTL
mode if the vehicle is stuck inBRAKE
mode. - GCS Messaging: Sends status and warning messages to the Ground Control Station (GCS).
The following parameters can be configured within the script:
FRONT
: Orientation ID of the front-facing rangefinder.AUTO
: Numerical ID for theAUTO
flight mode.BRAKE
: Numerical ID for theBRAKE
flight mode.RTL
: Numerical ID for theRTL
flight mode.threshold_m
: Obstacle detection threshold (meters).timeout_ms
: Brake timeout (milliseconds).
- Save the script to your ArduPilot flight controller's SD card (e.g.,
APM/lua/
). - Enable Lua scripting in ArduPilot (
LUA_ENABLED = 1
). - Configure your rangefinder in ArduPilot.
- Reboot the flight controller.
The script automatically monitors for obstacles when the vehicle is in AUTO
mode.
- Ensure your rangefinder is properly calibrated and functioning.
- Adjust
threshold_m
based on your vehicle's braking distance. - Test in a safe environment.
This script automates a flight sequence: when the vehicle is armed and switched to AUTO
mode, it calculates a waypoint 200 meters to the left of its initial heading and flies to that waypoint at 20 meters altitude.
- Automated Trigger: Triggers when the flight mode is set to
AUTO
after arming. - Relative Calculation: Calculates the waypoint relative to the vehicle's initial position and heading.
- Single Waypoint Mission: Creates a simple mission to fly to the calculated waypoint.
- Altitude Control: Sets the target altitude for the mission.
- GCS Feedback: Sends mission status and waypoint information to the GCS.
The following parameters can be configured by modifying the script:
- Turn angle: The script contains a hardcoded value of 90 degrees for the left turn.
- Flight distance: The script contains a hardcoded value of 200 meters.
- Target altitude: The script contains a hardcoded value of 20 meters.
- Save the script to your ArduPilot flight controller's SD card.
- Enable Lua scripting in ArduPilot (
LUA_ENABLED = 1
). - Reboot the flight controller.
- Arm the vehicle.
- Switch the flight mode to
AUTO
.
- Requires a good GPS lock.
- Ensure accurate compass calibration.
- The target altitude is relative to the home altitude.
- The script executes the maneuver only once per arming cycle.
This script monitors the total current draw from the vehicle's batteries. If the current exceeds a defined threshold, it sends a critical warning to the GCS.
- Continuous Current Monitoring: Periodically checks the total current.
- Configurable Threshold: The current threshold can be adjusted in the script.
- Multi-Battery Support: Supports up to two batteries.
- Critical Overcurrent Warning: Sends a high-priority warning to the GCS.
- Debug Information: Sends total current readings and arming status to the GCS.
- Pause on Disarm: Pauses monitoring when the vehicle is disarmed.
The following parameters can be configured within the script:
CHECK_INTERVAL_MS
: Interval (milliseconds) for checking the current.CURRENT_THRESHOLD
: Maximum allowed current (Amperes).
- Save the script to your ArduPilot flight controller's SD card.
- Enable Lua scripting in ArduPilot (
LUA_ENABLED = 1
). - Reboot the flight controller.
The script automatically starts monitoring the battery current when the vehicle is armed.
- The script assumes a maximum of two batteries. Modify the script for more batteries.
- Ensure your current sensors are properly calibrated.
- Set the
CURRENT_THRESHOLD
appropriately for your power system. - A stable GCS connection is required for receiving warnings.
- All scripts are written in Lua and designed to run on ArduPilot firmware.
- Ensure that Lua scripting is enabled in your ArduPilot configuration.
- Refer to the ArduPilot documentation for more information on Lua scripting and supported functions.
- Test these scripts thoroughly in a safe environment before using them in critical applications.