001/*----------------------------------------------------------------------------*/
002/* Copyright (c) FIRST 2016-2017. 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  /**
024   * Creates a vision thread that continuously runs a {@link VisionPipeline}.
025   *
026   * @param visionRunner the runner for a vision pipeline
027   */
028  public VisionThread(VisionRunner<?> visionRunner) {
029    super(visionRunner::runForever, "WPILib Vision Thread");
030    setDaemon(true);
031  }
032
033  /**
034   * Creates a new vision thread that continuously runs the given vision pipeline. This is
035   * equivalent to {@code new VisionThread(new VisionRunner<>(videoSource, pipeline, listener))}.
036   *
037   * @param videoSource the source for images the pipeline should process
038   * @param pipeline    the pipeline to run
039   * @param listener    the listener to copy outputs from the pipeline after it runs
040   * @param <P>         the type of the pipeline
041   */
042  public <P extends VisionPipeline> VisionThread(VideoSource videoSource,
043                                                 P pipeline,
044                                                 VisionRunner.Listener<? super P> listener) {
045    this(new VisionRunner<>(videoSource, pipeline, listener));
046  }
047
048}