|
| 1 | +# SITL Testing Setup |
| 2 | + |
| 3 | +This document describes how to set up and run integration tests using ArduPilot SITL (Software In The Loop) for testing the `backend_flightcontroller.py` module. |
| 4 | + |
| 5 | +## Overview |
| 6 | + |
| 7 | +SITL testing provides real MAVLink communication validation instead of mocked tests. |
| 8 | +This ensures the flight controller backend works correctly with actual ArduPilot firmware. |
| 9 | + |
| 10 | +## Architecture |
| 11 | + |
| 12 | +The SITL testing setup consists of: |
| 13 | + |
| 14 | +1. **Direct Download**: Tests download pre-built ArduCopter SITL binaries directly from the official ArduPilot firmware server (`firmware.ardupilot.org`) |
| 15 | +2. **Pytest Fixtures**: Session-scoped SITLManager class manages SITL process lifecycle |
| 16 | +3. **TCP Connection**: SITL runs on TCP port 5760 with MAVLink protocol |
| 17 | +4. **Parameter Configuration**: SITL uses `sitl/copter.param` with battery monitoring enabled |
| 18 | + |
| 19 | +## Prerequisites |
| 20 | + |
| 21 | +### For CI/CD (GitHub Actions) |
| 22 | + |
| 23 | +- No additional setup required - SITL binaries are downloaded automatically during tests |
| 24 | + |
| 25 | +### For Local Development |
| 26 | + |
| 27 | +#### Download Pre-built SITL (Recommended) |
| 28 | + |
| 29 | +Download the latest pre-built SITL binary directly from the official ArduPilot firmware server: |
| 30 | + |
| 31 | +```bash |
| 32 | +./scripts/run_sitl_tests.sh download |
| 33 | +``` |
| 34 | + |
| 35 | +This downloads ArduCopter SITL from `https://firmware.ardupilot.org/Copter/latest/SITL_x86_64_linux_gnu/arducopter` |
| 36 | + |
| 37 | +## Usage |
| 38 | + |
| 39 | +### CI/CD Testing |
| 40 | + |
| 41 | +SITL tests run automatically in GitHub Actions when SITL artifacts are available. The test workflow: |
| 42 | + |
| 43 | +1. Downloads the latest SITL artifact |
| 44 | +2. Extracts and sets up SITL binary |
| 45 | +3. Runs tests marked with `@pytest.mark.sitl` |
| 46 | +4. Falls back to mocked tests if SITL is unavailable |
| 47 | + |
| 48 | +### Local Development |
| 49 | + |
| 50 | +Use the provided script for local SITL testing. You can either download pre-built SITL or use a locally built version: |
| 51 | + |
| 52 | +#### Using Downloaded SITL (Recommended) |
| 53 | + |
| 54 | +```bash |
| 55 | +# Download ArduCopter SITL from official firmware server |
| 56 | +./scripts/run_sitl_tests.sh download |
| 57 | + |
| 58 | +# Download and run tests in one command |
| 59 | +./scripts/run_sitl_tests.sh download-test |
| 60 | + |
| 61 | +# Check if downloaded SITL is available |
| 62 | +./scripts/run_sitl_tests.sh check |
| 63 | +``` |
| 64 | + |
| 65 | +#### Using Locally Built SITL |
| 66 | + |
| 67 | +```bash |
| 68 | +# Set up environment for locally built SITL |
| 69 | +export ARDUPILOT_DIR="$HOME/ardupilot-sitl" |
| 70 | + |
| 71 | +# Check if locally built SITL is available |
| 72 | +./scripts/run_sitl_tests.sh check |
| 73 | + |
| 74 | +# Set up SITL for testing |
| 75 | +./scripts/run_sitl_tests.sh setup |
| 76 | + |
| 77 | +# Run SITL integration tests |
| 78 | +./scripts/run_sitl_tests.sh test |
| 79 | +``` |
| 80 | + |
| 81 | +#### General Commands |
| 82 | + |
| 83 | +```bash |
| 84 | +# Clean up SITL processes and cache |
| 85 | +./scripts/run_sitl_tests.sh cleanup |
| 86 | + |
| 87 | +# Show help |
| 88 | +./scripts/run_sitl_tests.sh help |
| 89 | +``` |
| 90 | + |
| 91 | +### Manual Testing |
| 92 | + |
| 93 | +Run specific SITL tests: |
| 94 | + |
| 95 | +```bash |
| 96 | +# Run all SITL tests |
| 97 | +python -m pytest tests/test_backend_flightcontroller_sitl.py -v |
| 98 | + |
| 99 | +# Run only SITL tests (skip if SITL unavailable) |
| 100 | +python -m pytest -m sitl -v |
| 101 | + |
| 102 | +# Run SITL tests or fallback to mocked tests |
| 103 | +python -m pytest -m "sitl or not sitl" -v |
| 104 | +``` |
| 105 | + |
| 106 | +## Test Coverage |
| 107 | + |
| 108 | +SITL tests cover: |
| 109 | + |
| 110 | +- **Real MAVLink Connection**: Validates actual protocol communication on TCP port 5760 |
| 111 | +- **Parameter Management**: Download, set, and verify parameters with real firmware |
| 112 | +- **Motor Testing**: Test motor commands against actual ArduPilot firmware |
| 113 | +- **Battery Monitoring**: Test battery status reporting with enabled monitoring |
| 114 | +- **Frame Information**: Validate vehicle configuration queries |
| 115 | + |
| 116 | +## Implementation Details |
| 117 | + |
| 118 | +### SITL Configuration |
| 119 | + |
| 120 | +SITL runs with the following command line parameters: |
| 121 | + |
| 122 | +```bash |
| 123 | +arducopter --model quad --home "40.071374,-105.229930,1440,0" --defaults ./sitl/copter.param --sysid 1 --speedup 10 |
| 124 | +``` |
| 125 | + |
| 126 | +### Connection Details |
| 127 | + |
| 128 | +- **Protocol**: MAVLink over TCP |
| 129 | +- **Port**: 5760 |
| 130 | +- **Connection String**: "tcp:127.0.0.1:5760" |
| 131 | +- **Vehicle Type**: ArduCopter (Quadcopter) |
| 132 | +- **System ID**: 1 |
| 133 | + |
| 134 | +### Parameter Requirements |
| 135 | + |
| 136 | +Some tests require specific parameters to be set in `sitl/copter.param`: |
| 137 | + |
| 138 | +- `BATT_MONITOR = 4` (Analog voltage and current) |
| 139 | +- `BATT_VOLT_PIN = 1` |
| 140 | +- `BATT_CURR_PIN = 2` |
| 141 | +- `BATT_VOLT_MULT = 10.0` |
| 142 | +- `BATT_AMP_PERVOLT = 17.0` |
| 143 | + |
| 144 | +## Configuration |
| 145 | + |
| 146 | +### Environment Variables |
| 147 | + |
| 148 | +- `SITL_BINARY`: Path to ArduCopter SITL binary (auto-detected in CI) |
| 149 | +- `ARDUPILOT_DIR`: Path to ArduPilot directory for local development |
| 150 | + |
| 151 | +### Test Markers |
| 152 | + |
| 153 | +- `@pytest.mark.sitl`: Marks tests requiring SITL |
| 154 | +- Tests automatically skip if SITL is unavailable |
| 155 | + |
| 156 | +## Troubleshooting |
| 157 | + |
| 158 | +### SITL Not Found |
| 159 | + |
| 160 | +- **For downloaded SITL**: Run `./scripts/run_sitl_tests.sh download` to download from ArduPilot website |
| 161 | +- **For locally built SITL**: Ensure ArduPilot is built with `./waf configure --board=sitl && ./waf copter` |
| 162 | +- Check `ARDUPILOT_DIR` environment variable for locally built SITL |
| 163 | +- Verify SITL binary exists at expected path |
| 164 | + |
| 165 | +### Connection Failures |
| 166 | + |
| 167 | +- SITL may take time to start - tests include startup delays |
| 168 | +- Check for port conflicts on TCP port 5760 |
| 169 | +- Verify MAVLink heartbeat detection |
| 170 | +- Ensure connection string format is "tcp:127.0.0.1:5760" |
| 171 | + |
| 172 | +### Test Timeouts |
| 173 | + |
| 174 | +- SITL tests are slower than mocked tests |
| 175 | +- Increase timeout values if needed |
| 176 | +- Check system performance for SITL simulation |
| 177 | + |
| 178 | +## Benefits |
| 179 | + |
| 180 | +1. **Real Validation**: Tests actual MAVLink protocol implementation |
| 181 | +2. **Regression Detection**: Catches firmware compatibility issues |
| 182 | +3. **CI/CD Integration**: Automated testing with pre-built artifacts |
| 183 | +4. **Development Flexibility**: Local testing with fallback to mocks |
| 184 | +5. **Cost Efficiency**: Monthly builds reduce CI resource usage |
| 185 | + |
| 186 | +## Future Enhancements |
| 187 | + |
| 188 | +- Multiple vehicle types (ArduPlane, Rover, etc.) |
| 189 | +- SITL version pinning for reproducible tests |
| 190 | +- Performance optimization for faster test execution |
| 191 | +- Multi-SITL instance testing for complex scenarios |
0 commit comments