Skip to content

VFH Candidate Valley Detection - #81

Open
Khushi0512 wants to merge 12 commits into
ShisatoYano:mainfrom
Khushi0512:feature/vfh-step2-candidate-valley-detection
Open

Khushi0512 wants to merge 12 commits into
ShisatoYano:mainfrom
Khushi0512:feature/vfh-step2-candidate-valley-detection

Conversation

@Khushi0512

@Khushi0512 Khushi0512 commented Sep 24, 2026 •

Copy link
Copy Markdown
Contributor

Overview

Implements Step 2 of the Vector Field Histogram (VFH) roadmap agreed with the maintainer in issue #52: identify contiguous runs of low-density polar histogram sectors ("valleys") that are free enough to be candidate steering directions, and visualize them.

This PR is stacked on the (not-yet)-merged Step 1 PR (#80), since valley detection operates directly on Step 1's PolarHistogram output. Its diff will show only this step's own changes once Step 1 is merged into main.

  • CandidateValleyDetector: 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°/360° boundary.
  • PolarHistogramMapper (extended): now also runs candidate valley detection each frame and draws each detected valley as a green arc just outside the existing density ring.
  • New simulation (vfh_candidate_valley_detection), reusing Step 1's obstacle scenario for direct comparison.

Direction selection is intentionally left for Step 3 per the agreed roadmap.

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.
… 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
@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
  """

@Khushi0512 Khushi0512 changed the title Contributes to VFH Step 2: candidate valley detection Feature/vfh step2 candidate valley detection VFH Candidate Valley Detection Sep 30, 2026
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.
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.

@ShisatoYano

Copy link
Copy Markdown
Owner

Thanks a lot for updating the docstrings, @Khushi0512, and sorry for the long wait on this series! I re-checked Step 2 merged with the current main:

  • The valley detection is clear and correct. Rotating the array to start just after a blocked sector is a neat way to handle the wrap-around, and I confirmed the center angle numerically for a wrapping valley (sectors 4..1 of 8 → -45°, as expected).
  • The demo GIF shows the valleys opening exactly between the three obstacles, the runtime matches Step 1 (9.5 s), and the gallery entry generates correctly.

One request before merging:

  1. Since this PR was opened, every simulation must be listed in one chapter of the web book, and test/test_book_chapters.py fails without it once merged with main. Could you add this line under "Sections" in doc/obstacle_avoidance/index.md, after 12.1?
    - 12.2 VFH candidate valleys: [`vfh_candidate_valley_detection`](/src/simulations/mapping/vfh_candidate_valley_detection)
    The later steps (Feature/vfh step3 direction selection #82, VFH step 4: vehicle motion integration #83, VFH Step 5: Dynamic Speed Control #85, VFH Step 6: Trajectory Verification #145, VFH Step 7: Performance Benchmarking #146) will need the same: 12.3, 12.4, and so on.

Suggestions:

  1. test_valley_wraps_across_zero_index: please also assert the center angle (-45°). The wrap-around is the case most likely to break later, and the center is where mid_index goes past the last sector.
  2. test_update_detects_valley_between_two_obstacle_clusters: the two clusters give exactly two valleys ((1, 16) and (18, 35) when I ran it), so == 2 would test the behavior more tightly than >= 1.

Once 1 is in, I'll merge this and move on to Step 3 (#82).

🤖 Generated with Claude Code

…r valley tests

Per the maintainer's review of VFH Step 2 (candidate valley detection):

- Added "12.2 VFH candidate valleys" to doc/obstacle_avoidance/index.md's
  Sections, after the existing 12.1 entry, so test_book_chapters.py finds
  this step's simulation listed once merged with main.
- test_valley_wraps_across_zero_index: also asserts the wrapped valley's
  center angle(-45[deg]), since that's the case where mid_index runs past
  the last sector and is most likely to break later.
- test_update_detects_valley_between_two_obstacle_clusters: tightened
  len(valleys) >= 1 to == 2, matching the two obstacle clusters' two
  actual gaps((1, 16) and (18, 35)) instead of just allowing at least one.
@Khushi0512

Copy link
Copy Markdown
Contributor Author

@ShisatoYano added 12.2 to doc/obstacle_avoidance/index.md after 12.1.

I also tightened the two suggested tests: test_valley_wraps_across_zero_index now asserts the center angle (-45°), and test_update_detects_valley_between_two_obstacle_clusters now asserts exactly 2 valleys instead of >= 1.

Pushed to the branch.

@ShisatoYano ShisatoYano left a comment

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the quick update, @Khushi0512! I checked the new commit merged with the current main:

  • The 12.2 entry is in the right place, and test/test_book_chapters.py passes with it.
  • Both tightened tests look good: the wrap-around case now pins the -45° center angle, and the two-cluster case asserts exactly two valleys.
  • All 29 tests for the polar histogram and valley detection pass, and the gallery entry still generates correctly.

One request before merging:

  1. The PR now conflicts with main in doc/obstacle_avoidance/index.md (add/add). Your branch last merged main on Oct 3, and that file was created on main after that, so Git sees it as added on both sides. Because of the conflict, the unit-test CI (Linux/macOS/Windows) also didn't run on your latest commit. Could you merge the current main into your branch and resolve it by keeping main's file plus your 12.2 line? Once CI is green, I'll merge this and move on to Step 3 (#82).

🤖 Generated with Claude Code

@ShisatoYano

Copy link
Copy Markdown
Owner

Hi @Khushi0512, thank you for the VFH series! There are now six open PRs (#81, #82, #83, #85, #145 and #146) that build on each other, and the later ones are hard to review before the first ones are merged.

Could we do it this way?

  1. Finish the fixes on VFH Candidate Valley Detection #81 and Feature/vfh step3 direction selection #82, and I'll merge them.
  2. Then I'll close VFH step 4: vehicle motion integration #83, VFH Step 5: Dynamic Speed Control #85, VFH Step 6: Trajectory Verification #145 and VFH Step 7: Performance Benchmarking #146 for now, and you open the remaining steps again as one PR on top of the merged code.

Nothing is lost: the branches stay, and the closed PRs can still be linked from the new one. Does that work for you?

guptuv added a commit to guptuv/AutonomousVehicleControlBeginnersGuide that referenced this pull request Nov 25, 2026
ShisatoYano#81 and ShisatoYano#82 already use 12.2 and 12.3 for the VFH steps.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_012s7DKDx3Dr8YBEdQxQH5wU

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

enhancement New feature or request

Projects

Status: Todo

Development

Successfully merging this pull request may close these issues.

2 participants