001/*----------------------------------------------------------------------------*/ 002/* Copyright (c) 2017-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; 009 010import edu.wpi.first.wpilibj.hal.FRCNetComm.tInstances; 011import edu.wpi.first.wpilibj.hal.FRCNetComm.tResourceType; 012import edu.wpi.first.wpilibj.hal.HAL; 013 014/** 015 * TimedRobot implements the IterativeRobotBase robot program framework. 016 * 017 * <p>The TimedRobot class is intended to be subclassed by a user creating a robot program. 018 * 019 * <p>periodic() functions from the base class are called on an interval by a Notifier instance. 020 */ 021public class TimedRobot extends IterativeRobotBase { 022 public static final double DEFAULT_PERIOD = 0.02; 023 024 private double m_period = DEFAULT_PERIOD; 025 026 // Prevents loop from starting if user calls setPeriod() in robotInit() 027 private boolean m_startLoop = false; 028 029 private Notifier m_loop = new Notifier(() -> { 030 loopFunc(); 031 }); 032 033 public TimedRobot() { 034 // HAL.report(tResourceType.kResourceType_Framework, tInstances.kFramework_Periodic); 035 HAL.report(tResourceType.kResourceType_Framework, tInstances.kFramework_Iterative); 036 } 037 038 /** 039 * Provide an alternate "main loop" via startCompetition(). 040 */ 041 public void startCompetition() { 042 robotInit(); 043 044 // Tell the DS that the robot is ready to be enabled 045 HAL.observeUserProgramStarting(); 046 047 // Loop forever, calling the appropriate mode-dependent function 048 m_startLoop = true; 049 m_loop.startPeriodic(m_period); 050 while (true) { 051 try { 052 Thread.sleep(1000 * 60 * 60 * 24); 053 } catch (InterruptedException ex) { 054 Thread.currentThread().interrupt(); 055 } 056 } 057 } 058 059 /** 060 * Set time period between calls to Periodic() functions. 061 * 062 * @param period Period in seconds. 063 */ 064 public void setPeriod(double period) { 065 m_period = period; 066 067 if (m_startLoop) { 068 m_loop.startPeriodic(m_period); 069 } 070 } 071}