Skip to content

VFH step 4: vehicle motion integration - #83

Open
Khushi0512 wants to merge 23 commits into
ShisatoYano:mainfrom
Khushi0512:feature/vfh-step4-vehicle-motion-integration
Open

Khushi0512 wants to merge 23 commits into
ShisatoYano:mainfrom
Khushi0512:feature/vfh-step4-vehicle-motion-integration

Conversation

@Khushi0512

Copy link
Copy Markdown
Contributor

Overview

This PR implements Step 4 of the VFH obstacle avoidance roadmap for issue #52: vehicle motion integration, stacked on Step 3 (direction selection).

Adds VfhController (src/components/control/vfh/vfh_controller.py), a new controller alongside the existing control/lqr, control/mpc, control/pure_pursuit, etc. subfolders. Each cycle it reads the Step 3 mapper's currently selected global-frame direction and converts it into acceleration / yaw rate inputs via simple proportional control, wired into FourWheelsVehicle's existing controller slot - no changes needed to any shared vehicle or mapper class. It falls back to the vehicle's current heading when no direction is selected yet (e.g. every sector blocked). Cruise speed is held constant for now; adjusting speed by obstacle proximity is left for Step 5. A front-tire steering angle is also derived from the commanded yaw rate (bicycle model relation), for visualization only.

Adds a new simulation (vfh_vehicle_motion_integration) using the same three-obstacle scenario and goal point as Steps 1-3, now with the vehicle actually driving itself around the obstacles toward the goal instead of only showing the selected direction.

Step 5 (dynamic speed control) is intentionally left for the next branch.

Program File Changes in This Step Relative to Steps 1, 2 and 3

12 new tests for this step's controller (constructor validation, the heading-fallback case, proportional speed/yaw-rate control and saturation, the steer-angle derivation, and the visualization no-op), and the remaining 54 tests across angle_lib, state, vehicle_specification, polar_histogram, candidate_valley_detector, direction_selector, and polar_histogram_mapper passing unmodified.

Note: since this branch is stacked on the not-yet-merged Step 3 branch, this PR's diff currently includes Steps 1-3's commits too. It'll narrow to just this step's changes once the earlier PRs are merged.

Implements the first incremental step of the Vector Field Histogram (VFH)
roadmap agreed with the maintainer in issue ShisatoYano#52: build a 1D polar obstacle
density histogram around the vehicle directly from the existing 2D LiDAR
sensor simulation, with configurable sector resolution and smoothing.

- PolarHistogram (src/components/mapping/polar_histogram/polar_histogram.py):
  pure-logic histogram construction. Bins LiDAR returns into angular sectors,
  weights each by the classic VFH linear magnitude falloff
  (Borenstein & Koren, 1991), and applies a triangular smoothing filter
  across neighboring sectors.
- PolarHistogramMapper (polar_histogram_mapper.py): wires PolarHistogram to
  the existing OmniDirectionalLidar point cloud and renders it as a ring of
  colored wedges around the vehicle (denser sectors longer/redder), using
  the project's existing mapper slot on FourWheelsVehicle - mirrors
  PotentialFieldMapper's update(point_cloud, state) / draw(axes, elems)
  interface, so no changes were needed to shared vehicle/visualizer code.
- vfh_polar_histogram_construction.py: simulation entry point, reusing the
  same obstacle scenario as the existing lidar_obstacle_sensing sample so
  the histogram's behavior can be compared directly against the raw point
  cloud it's built from.
- Unit tests for histogram binning/smoothing math, the mapper, and the
  simulation entry point (15 tests total).
- Regenerated pyrightconfig.json / devcontainer.json extraPaths via
  generate_pyrightconfig.py for the two new module directories.

Valley/direction selection is intentionally left for Step 2 per the agreed
roadmap, to keep this PR small and reviewable.

How it was verified: all 15 new tests pass via pytest (matching the
project's existing show_plot=False test convention), and the full
simulation runs cleanly end to end.
Implements the second incremental step of the Vector Field Histogram (VFH)
roadmap agreed with the maintainer in issue ShisatoYano#52: identify contiguous runs
of low-density polar histogram sectors ("valleys") that are free enough to
be candidate steering directions, and visualize them.

This branch is stacked on the not-yet-merged Step 1 branch
(feature/vfh-step1-polar-histogram), since valley detection operates
directly on Step 1's PolarHistogram output. Its diff against upstream main
will show only this step's own changes once Step 1 is merged.

- Valley / CandidateValleyDetector (candidate_valley_detector.py): pure-logic
  valley detection. Marks sectors at/below a configurable smoothed-density
  threshold as navigable, then groups consecutive navigable sectors into
  Valley instances (start/end index, width, center angle), correctly
  handling valleys that wrap across the 0-degree boundary.
- PolarHistogramMapper (polar_histogram_mapper.py, extended): now also runs
  candidate valley detection each frame and draws each detected valley as a
  green arc just outside the existing density ring. Density-ring and valley
  drawing were split into private helper methods to keep draw() readable as
  the mapper grows across VFH steps.
- vfh_candidate_valley_detection.py: new simulation entry point, reusing the
  same obstacle scenario as Step 1's demo so valleys can be compared
  directly against that step's raw density histogram.
- Unit tests for the circular valley-grouping logic (including the
  wrap-around and full-circle edge cases), the mapper's valley drawing, and
  the new simulation entry point.
- Regenerated pyrightconfig.json / devcontainer.json extraPaths for the new
  simulation directory (the new component file lives inside Step 1's
  existing polar_histogram directory, so no new path was needed there).

One existing Step 1 test (test_draw_with_no_detections_adds_nothing) was
renamed to test_draw_with_no_detections_adds_only_the_full_valley and its
assertion updated: with candidate valley detection added, an empty LiDAR
scan now correctly produces one full-circle valley instead of zero drawn
elements.

Direction selection (choosing which valley/angle to actually steer toward)
is intentionally left for Step 3 per the agreed roadmap.

How it was verified: all 28 tests across the polar-histogram test suite
pass via pytest - 13 new tests for this step (valley detection logic,
mapper integration, and the new simulation), 1 existing Step 1 mapper test
updated to reflect the new full-circle-valley behavior, and the remaining
14 Step 1 tests passing unmodified. Both the Step 1 and Step 2 simulations
also run cleanly end to end.
Implements the third incremental step of the Vector Field Histogram (VFH)
roadmap agreed with the maintainer in issue ShisatoYano#52: choose one steering
direction from the candidate valleys detected in Step 2, by minimizing a
weighted cost function over the target(goal) direction, the vehicle's
current heading, and the previously selected direction, following
Borenstein and Koren (1991).

This branch is stacked on the not-yet-merged Step 2 branch
(feature/vfh-step2-candidate-valley-detection), since direction selection
operates directly on Step 2's Valley output. Its diff against upstream
main will show only this step's own changes once Steps 1-2 are merged.

- DirectionSelector (direction_selector.py): pure-logic direction
  selection. Scores each valley's center angle (converted from the
  vehicle-relative frame CandidateValleyDetector works in, to the global
  frame) against the target direction, current heading, and the
  previously selected direction, each with a configurable weight, and
  picks the lowest-cost candidate. The previous-direction term is what
  keeps the choice stable frame to frame instead of flipping between two
  similarly-scored valleys as the vehicle turns.
- PolarHistogramMapper (extended): now also computes a target direction
  each frame (toward a configurable goal point, or "keep going straight"
  by default) and runs direction selection. Draws the selected direction
  as a bold blue arrow and the target direction as a thin dashed line, on
  top of the existing density ring and valley arcs.
- vfh_direction_selection.py: new simulation entry point, reusing the
  same obstacle scenario as Steps 1-2, with a goal point placed beyond one
  of the moving obstacles so the selected direction visibly bends away
  from the straight-line target when that obstacle blocks the way.
- Unit tests for the cost function (including the vehicle-relative-to-
  global angle conversion, oscillation avoidance via the previous-
  direction term, and the same boundary-wrap case tested in Step 2), the
  mapper's target/selection integration, and the new simulation.

One existing Step 2 mapper test (test_draw_with_no_detections_adds_only_
the_full_valley) was renamed to test_draw_with_no_detections_adds_full_
valley_and_target_line and its assertion updated from 1 to 3 drawn
elements: direction selection now always draws a target reference line,
and - since an obstacle-free scan still produces one full-circle valley -
also a selected-direction arrow.

Vehicle motion integration (actually steering the vehicle using this
selected direction) is intentionally left for Step 4 per the agreed
roadmap; this step only selects and visualizes the direction.

How it was verified: all 42 tests across the polar-histogram test suite
pass via pytest - 14 new tests for this step (cost function logic, mapper
integration, and the new simulation), 1 existing Step 2 mapper test
updated to reflect the new drawing behavior, and the remaining 27 Step 1/2
tests passing unmodified. All three simulations (Steps 1, 2, and 3) run
cleanly end to end.
Stacked on feature/vfh-step3-direction-selection, continuing the
maintainer's roadmap for this issue one branch per step.

New files:
- src/components/control/vfh/vfh_controller.py: VfhController, a new
  controller (alongside the existing control/lqr, control/mpc,
  control/pure_pursuit, etc. subfolders) that reads the Step 3 mapper's
  currently selected global-frame direction each cycle via
  mapper.get_direction_selector().get_selected_angle_rad(), and converts
  it into acceleration / yaw rate inputs by simple proportional control.
  Wired into FourWheelsVehicle's existing controller slot, so no shared
  vehicle/mapper class needed to change. Falls back to the vehicle's
  current heading when no direction is selected yet (e.g. every sector
  blocked). Cruise speed is held constant for now; adjusting speed by
  obstacle proximity is left for Step 5. A front-tire steering angle is
  also derived from the commanded yaw rate (bicycle model relation),
  for visualization only.
- src/simulations/mapping/vfh_vehicle_motion_integration/vfh_vehicle_motion_integration.py:
  same three-obstacle scenario and goal point as Steps 1-3, now with a
  VfhController actually driving the vehicle instead of only
  visualizing the selected direction.
- test/test_vfh_controller.py: 12 tests covering constructor
  validation, the heading-fallback case, proportional speed control
  (accelerate/decelerate), proportional yaw rate control and
  saturation (including a direction reversal across two update() calls
  on the same instance), the steer-angle bicycle-model derivation, and
  the visualization no-op.
- test/test_vfh_vehicle_motion_integration.py: 1 test mirroring the
  established simulation smoke-test pattern.

Changed files:
- pyrightconfig.json / .devcontainer/devcontainer.json: +1 line each
  for the new component subfolder (src/components/control/vfh) and +1
  line each for the new simulation folder, alphabetically placed.

This step doesn't modify any file from Steps 1-3, so verification here
covers this step's own tests plus the full fast unit/component suite;
Steps 1-3's own slower animated simulation tests weren't re-run since
nothing they depend on changed.

Step 5 (dynamic speed control) is deferred to the next branch.

How it was verified: all 66 tests in the fast unit/component suite
pass via pytest - 12 new tests for this step's controller, and the
remaining 54 tests across angle_lib, state, vehicle_specification,
polar_histogram, candidate_valley_detector, direction_selector and
polar_histogram_mapper passing unmodified. This step's new simulation
test also passes (44.71s), with the vehicle now visibly driving from
its start position toward the goal, steering around all three
obstacles.
… accuracy)

1. Drawing performance: PolarHistogramMapper.draw() was calling
   axes.add_patch() once per sector wedge (~42 calls/frame in this
   scenario). Collects all of a frame's wedges into a single
   matplotlib.collections.PatchCollection and adds it with one
   axes.add_collection() call instead (match_original=True keeps each
   wedge's own color/alpha). Confirmed on this machine using the exact
   same test/measurement this repo already uses for the simulation
   (test_vfh_polar_histogram_construction.py, which runs draw() 1000
   times without opening a plot window): 98.79s before this fix, 51.72s
   after - a ~48% reduction, consistent with the ~51% reduction (76s to
   37s) reported in review. Later steps' own draw() additions
   (candidate valleys, direction selection) are untouched here since
   they add far fewer patches per frame and weren't part of this
   review.

2. Histogram origin: this is a drawing-only approximation, not a data
   or logic bug - each measurement's angle/distance is computed from
   the LiDAR's actual mounted position (OmniDirectionalLidar), so the
   histogram density itself, and everything later steps derive from it
   (valleys, selected direction), is already geometrically accurate.
   Only the ring of wedges is drawn centered on the vehicle's origin
   rather than the LiDAR's position, for simplicity. Took the
   documented-approximation option offered in review rather than
   changing where this (and, to stay visually consistent, later
   steps' valley/direction drawings) are centered, since that would be
   a larger change across all 4 stacked branches for a cosmetic-only
   difference. Added a docstring note on PolarHistogramMapper
   explaining this and roughly quantifying it (up to ~20deg for very
   close obstacles, matching the reviewer's own example).

3. Magnitude formula docstring: clarified that PolarHistogram's
   magnitude equation follows the original VFH paper with its
   certainty value fixed at 1, rather than implying an exact match to
   the paper's general certainty^2 * (a - b*d) form.

Demo GIF for doc/EXAMPLES.md (also requested in review) is a separate
follow-up commit, since it's a binary asset rather than a code change.

Verified: all 14 existing tests for this step (test_polar_histogram.py,
test_polar_histogram_mapper.py) still pass unmodified, confirming the
draw() refactor didn't change any observable behavior these tests
check. test_vfh_polar_histogram_construction.py also still passes.
…p2-candidate-valley-detection

# Conflicts:
#	src/components/mapping/polar_histogram/polar_histogram_mapper.py
…ure/vfh-step3-direction-selection

# Conflicts:
#	src/components/mapping/polar_histogram/polar_histogram_mapper.py
@ShisatoYano

Copy link
Copy Markdown
Owner

@Khushi0512
FYI: doc/EXAMPLES.md is now generated from each simulation script's docstring. The docstring of every script under src/simulations/ must be exactly the file name and one
line each of Title:, Description: and Author: (nothing else), following HOWTOCONTRIBUTE.md (Step 2, item 6). Otherwise the gallery test fails once merged with main. Could
you update the new simulation scripts in Steps 2–4? Example:

  """
  vfh_candidate_valley_detection.py

  Title: VFH Candidate Valley Detection
  Description: Detects runs of low-density sectors in the polar histogram as candidate steering directions
  Author: Khushi
  """

Per maintainer feedback on PR ShisatoYano#80: doc/EXAMPLES.md is now generated from
each simulation script's docstring, which must be exactly the filename
followed by one line each of Title/Description/Author (HOWTOCONTRIBUTE.md
Step 2, item 6). Updates this step's docstring to match.
Same fix as Step 1, applied to this step's own simulation script.
Same fix as Step 1, applied to this step's own simulation script.
Same fix as Step 1, applied to this step's own simulation script.
Upstream main already added a Title/Description to this file (with
slightly different wording) as part of rolling out the doc/EXAMPLES.md
gallery format repo-wide. Matching that wording exactly here so this
file no longer conflicts with main on Steps 2-4's PRs, which carry this
file forward through the merge chain.
@Khushi0512

Copy link
Copy Markdown
Contributor Author

Hi, @ShisatoYano, docstrings for Steps 2–4 are now updated to the Title/Description/Author format and pushed (PRs #81, #82, #83). Step 1's file already had this added as part of your repo-wide gallery rollout, so I matched that exact wording to keep everything consistent, no separate PR needed there.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Status: Todo

Development

Successfully merging this pull request may close these issues.

2 participants