001/*----------------------------------------------------------------------------*/
002/* Copyright (c) 2016-2018 FIRST. All Rights Reserved.                        */
003/* Open Source Software - may be modified and shared by FRC teams. The code   */
004/* must be accompanied by the FIRST BSD license file in the root directory of */
005/* the project.                                                               */
006/*----------------------------------------------------------------------------*/
007
008package edu.wpi.first.wpilibj.vision;
009
010import edu.wpi.cscore.VideoSource;
011
012/**
013 * A vision thread is a special thread that runs a vision pipeline. It is a <i>daemon</i> thread;
014 * it does not prevent the program from exiting when all other non-daemon threads
015 * have finished running.
016 *
017 * @see VisionPipeline
018 * @see VisionRunner
019 * @see Thread#setDaemon(boolean)
020 */
021public class VisionThread extends Thread {
022  /**
023   * Creates a vision thread that continuously runs a {@link VisionPipeline}.
024   *
025   * @param visionRunner the runner for a vision pipeline
026   */
027  public VisionThread(VisionRunner<?> visionRunner) {
028    super(visionRunner::runForever, "WPILib Vision Thread");
029    setDaemon(true);
030  }
031
032  /**
033   * Creates a new vision thread that continuously runs the given vision pipeline. This is
034   * equivalent to {@code new VisionThread(new VisionRunner<>(videoSource, pipeline, listener))}.
035   *
036   * @param videoSource the source for images the pipeline should process
037   * @param pipeline    the pipeline to run
038   * @param listener    the listener to copy outputs from the pipeline after it runs
039   * @param <P>         the type of the pipeline
040   */
041  public <P extends VisionPipeline> VisionThread(VideoSource videoSource,
042                                                 P pipeline,
043                                                 VisionRunner.Listener<? super P> listener) {
044    this(new VisionRunner<>(videoSource, pipeline, listener));
045  }
046
047}