|
| 1 | +HITL JSON Position Emulator - Execution Flow |
| 2 | +============================================= |
| 3 | + |
| 4 | +INITIALIZATION PHASE: |
| 5 | +--------------------- |
| 6 | +1. HITL.create() called with json_file_path parameter |
| 7 | +2. PositionEmulator.create() called with json_file_path and update_interval |
| 8 | +3. PositionEmulator.__init__() runs: |
| 9 | + - Sets up json_coordinates = [] |
| 10 | + - Sets current_coordinate_index = 0 |
| 11 | + - Sets update_interval (default 1.0 seconds) |
| 12 | + - Sets next_coordinate_time = current_time + update_interval |
| 13 | + - Calls _load_json_coordinates() if json_file_path provided |
| 14 | + |
| 15 | +4. _load_json_coordinates() runs: |
| 16 | + - Opens and parses JSON file |
| 17 | + - Validates format: list of [lat, lon, alt] lists |
| 18 | + - Stores in self.json_coordinates |
| 19 | + - Prints confirmation message |
| 20 | + |
| 21 | +RUNTIME PHASE (periodic() called every loop): |
| 22 | +--------------------------------------------- |
| 23 | +1. periodic() called by HITL thread |
| 24 | +2. Check if json_coordinates exist: |
| 25 | + |
| 26 | + IF JSON COORDINATES EXIST: |
| 27 | + --------------------------- |
| 28 | + 3a. Check if current_time >= next_coordinate_time: |
| 29 | + - IF YES: Call _next_json_coordinate() |
| 30 | + * Get coordinate: json_coordinates[current_coordinate_index] |
| 31 | + * Extract: latitude, longitude, altitude |
| 32 | + * Call set_target_position(lat, lon, alt) |
| 33 | + * Print progress message |
| 34 | + * Increment current_coordinate_index (cycle at end) |
| 35 | + * Set next_coordinate_time = current_time + update_interval |
| 36 | + - IF NO: Do nothing (wait for next cycle) |
| 37 | + |
| 38 | + ELSE (NO JSON COORDINATES): |
| 39 | + --------------------------- |
| 40 | + 3b. Call get_target_position() to get Ardupilot position |
| 41 | + - Try to get POSITION_TARGET_GLOBAL_INT from flight controller |
| 42 | + - If available: use Ardupilot coordinates |
| 43 | + - If not available: use fallback target_position |
| 44 | + |
| 45 | +4. ALWAYS: Call inject_position(*target_position) |
| 46 | + - Create GPS_INPUT MAVLink message |
| 47 | + - Send to flight controller |
| 48 | + - Flush connection |
| 49 | + |
| 50 | +EXAMPLE EXECUTION WITH test_coordinates.json: |
| 51 | +============================================= |
| 52 | + |
| 53 | +JSON File Content: |
| 54 | +[ |
| 55 | + [43.43405014107003, -80.57898027451816, 373.0], // Coordinate 0 |
| 56 | + [40.0, -40.0, 200.0], // Coordinate 1 |
| 57 | + [41.29129039399329, -81.78471782918818, 373.0] // Coordinate 2 |
| 58 | +] |
| 59 | + |
| 60 | +Time 0.0s: INITIALIZATION |
| 61 | +- Loads 3 coordinates from JSON |
| 62 | +- Sets current_coordinate_index = 0 |
| 63 | +- Sets next_coordinate_time = 0.0 + 1.0 = 1.0s |
| 64 | +- Prints: "HITL loaded 3 coordinates from test_coordinates.json" |
| 65 | + |
| 66 | +Time 0.1s: periodic() call |
| 67 | +- current_time = 0.1s |
| 68 | +- 0.1s < 1.0s, so no coordinate change |
| 69 | +- Uses current target_position (default: 43.434..., -80.578..., 373.0) |
| 70 | +- Injects position to flight controller |
| 71 | + |
| 72 | +Time 0.2s: periodic() call |
| 73 | +- current_time = 0.2s |
| 74 | +- 0.2s < 1.0s, so no coordinate change |
| 75 | +- Uses current target_position |
| 76 | +- Injects position to flight controller |
| 77 | + |
| 78 | +... (continues every ~0.1s) ... |
| 79 | + |
| 80 | +Time 1.0s: periodic() call |
| 81 | +- current_time = 1.0s |
| 82 | +- 1.0s >= 1.0s, so TIME TO CHANGE COORDINATE! |
| 83 | +- Calls _next_json_coordinate(): |
| 84 | + * Gets coordinate[0]: [43.434..., -80.578..., 373.0] |
| 85 | + * Sets target_position = (43.434..., -80.578..., 373.0) |
| 86 | + * Prints: "HITL set JSON coordinate 1/3: (43.434..., -80.578..., 373.0)" |
| 87 | + * Increments current_coordinate_index = 1 |
| 88 | + * Sets next_coordinate_time = 1.0 + 1.0 = 2.0s |
| 89 | +- Injects new position to flight controller |
| 90 | + |
| 91 | +Time 1.1s: periodic() call |
| 92 | +- current_time = 1.1s |
| 93 | +- 1.1s < 2.0s, so no coordinate change |
| 94 | +- Uses current target_position (still coordinate 0) |
| 95 | +- Injects position to flight controller |
| 96 | + |
| 97 | +... (continues every ~0.1s) ... |
| 98 | + |
| 99 | +Time 2.0s: periodic() call |
| 100 | +- current_time = 2.0s |
| 101 | +- 2.0s >= 2.0s, so TIME TO CHANGE COORDINATE! |
| 102 | +- Calls _next_json_coordinate(): |
| 103 | + * Gets coordinate[1]: [40.0, -40.0, 200.0] |
| 104 | + * Sets target_position = (40.0, -40.0, 200.0) |
| 105 | + * Prints: "HITL set JSON coordinate 2/3: (40.0, -40.0, 200.0)" |
| 106 | + * Increments current_coordinate_index = 2 |
| 107 | + * Sets next_coordinate_time = 2.0 + 1.0 = 3.0s |
| 108 | +- Injects new position to flight controller |
| 109 | + |
| 110 | +Time 3.0s: periodic() call |
| 111 | +- current_time = 3.0s |
| 112 | +- 3.0s >= 3.0s, so TIME TO CHANGE COORDINATE! |
| 113 | +- Calls _next_json_coordinate(): |
| 114 | + * Gets coordinate[2]: [41.291..., -81.784..., 373.0] |
| 115 | + * Sets target_position = (41.291..., -81.784..., 373.0) |
| 116 | + * Prints: "HITL set JSON coordinate 3/3: (41.291..., -81.784..., 373.0)" |
| 117 | + * Increments current_coordinate_index = 3, but 3 % 3 = 0 (cycles back!) |
| 118 | + * Sets next_coordinate_time = 3.0 + 1.0 = 4.0s |
| 119 | +- Injects new position to flight controller |
| 120 | + |
| 121 | +Time 4.0s: periodic() call |
| 122 | +- current_time = 4.0s |
| 123 | +- 4.0s >= 4.0s, so TIME TO CHANGE COORDINATE! |
| 124 | +- Calls _next_json_coordinate(): |
| 125 | + * Gets coordinate[0]: [43.434..., -80.578..., 373.0] (back to first!) |
| 126 | + * Sets target_position = (43.434..., -80.578..., 373.0) |
| 127 | + * Prints: "HITL set JSON coordinate 1/3: (43.434..., -80.578..., 373.0)" |
| 128 | + * Increments current_coordinate_index = 1 |
| 129 | + * Sets next_coordinate_time = 4.0 + 1.0 = 5.0s |
| 130 | +- Injects new position to flight controller |
| 131 | + |
| 132 | +... (continues cycling forever) ... |
| 133 | + |
| 134 | +KEY POINTS: |
| 135 | +=========== |
| 136 | +- Coordinates change every 1 second (or custom interval) |
| 137 | +- Cycles through all coordinates then repeats |
| 138 | +- Each coordinate is held for the full interval duration |
| 139 | +- Position is injected to flight controller every periodic() call |
| 140 | +- JSON coordinates override Ardupilot when available |
| 141 | +- If no JSON file, falls back to Ardupilot pathing |
| 142 | +- Error handling prevents crashes if JSON loading fails |
0 commit comments