Added TimedRobot (#520)

This commit is contained in:
Tyler Veness
2017-07-08 10:50:56 -04:00
committed by Peter Johnson
parent f826216a28
commit 89d3b08e77
16 changed files with 722 additions and 582 deletions

View File

@@ -9,12 +9,14 @@
#include "DriverStation.h"
#include "HAL/HAL.h"
#include "LiveWindow/LiveWindow.h"
#include "llvm/raw_ostream.h"
#include "networktables/NetworkTable.h"
using namespace frc;
IterativeRobot::IterativeRobot() {
HAL_Report(HALUsageReporting::kResourceType_Framework,
HALUsageReporting::kFramework_Iterative);
}
/**
* Provide an alternate "main loop" via StartCompetition().
*
@@ -22,244 +24,11 @@ using namespace frc;
* the DS packets.
*/
void IterativeRobot::StartCompetition() {
HAL_Report(HALUsageReporting::kResourceType_Framework,
HALUsageReporting::kFramework_Iterative);
LiveWindow* lw = LiveWindow::GetInstance();
// first and one-time initialization
NetworkTable::GetTable("LiveWindow")
->GetSubTable("~STATUS~")
->PutBoolean("LW Enabled", false);
RobotInit();
// Tell the DS that the robot is ready to be enabled
HAL_ObserveUserProgramStarting();
// loop forever, calling the appropriate mode-dependent function
lw->SetEnabled(false);
// Loop forever, calling the appropriate mode-dependent function
while (true) {
// wait for driver station data so the loop doesn't hog the CPU
m_ds.WaitForData();
// Call the appropriate function depending upon the current robot mode
if (IsDisabled()) {
// call DisabledInit() if we are now just entering disabled mode from
// either a different mode or from power-on
if (!m_disabledInitialized) {
lw->SetEnabled(false);
DisabledInit();
m_disabledInitialized = true;
// reset the initialization flags for the other modes
m_autonomousInitialized = false;
m_teleopInitialized = false;
m_testInitialized = false;
}
HAL_ObserveUserProgramDisabled();
DisabledPeriodic();
} else if (IsAutonomous()) {
// call AutonomousInit() if we are now just entering autonomous mode from
// either a different mode or from power-on
if (!m_autonomousInitialized) {
lw->SetEnabled(false);
AutonomousInit();
m_autonomousInitialized = true;
// reset the initialization flags for the other modes
m_disabledInitialized = false;
m_teleopInitialized = false;
m_testInitialized = false;
}
HAL_ObserveUserProgramAutonomous();
AutonomousPeriodic();
} else if (IsTest()) {
// call TestInit() if we are now just entering test mode from
// either a different mode or from power-on
if (!m_testInitialized) {
lw->SetEnabled(true);
TestInit();
m_testInitialized = true;
// reset the initialization flags for the other modes
m_disabledInitialized = false;
m_autonomousInitialized = false;
m_teleopInitialized = false;
}
HAL_ObserveUserProgramTest();
TestPeriodic();
} else {
// call TeleopInit() if we are now just entering teleop mode from
// either a different mode or from power-on
if (!m_teleopInitialized) {
lw->SetEnabled(false);
TeleopInit();
m_teleopInitialized = true;
// reset the initialization flags for the other modes
m_disabledInitialized = false;
m_autonomousInitialized = false;
m_testInitialized = false;
Scheduler::GetInstance()->SetEnabled(true);
}
HAL_ObserveUserProgramTeleop();
TeleopPeriodic();
}
RobotPeriodic();
}
}
/**
* Robot-wide initialization code should go here.
*
* Users should override this method for default Robot-wide initialization which
* will be called when the robot is first powered on. It will be called exactly
* one time.
*
* Warning: the Driver Station "Robot Code" light and FMS "Robot Ready"
* indicators will be off until RobotInit() exits. Code in RobotInit() that
* waits for enable will cause the robot to never indicate that the code is
* ready, causing the robot to be bypassed in a match.
*/
void IterativeRobot::RobotInit() {
llvm::outs() << "Default " << __FUNCTION__ << "() method... Overload me!\n";
}
/**
* Initialization code for disabled mode should go here.
*
* Users should override this method for initialization code which will be
* called each time
* the robot enters disabled mode.
*/
void IterativeRobot::DisabledInit() {
llvm::outs() << "Default " << __FUNCTION__ << "() method... Overload me!\n";
}
/**
* Initialization code for autonomous mode should go here.
*
* Users should override this method for initialization code which will be
* called each time the robot enters autonomous mode.
*/
void IterativeRobot::AutonomousInit() {
llvm::outs() << "Default " << __FUNCTION__ << "() method... Overload me!\n";
}
/**
* Initialization code for teleop mode should go here.
*
* Users should override this method for initialization code which will be
* called each time the robot enters teleop mode.
*/
void IterativeRobot::TeleopInit() {
llvm::outs() << "Default " << __FUNCTION__ << "() method... Overload me!\n";
}
/**
* Initialization code for test mode should go here.
*
* Users should override this method for initialization code which will be
* called each time the robot enters test mode.
*/
void IterativeRobot::TestInit() {
llvm::outs() << "Default " << __FUNCTION__ << "() method... Overload me!\n";
}
/**
* Periodic code for all modes should go here.
*
* This function is called each time a new packet is received from the driver
* station.
*
* Packets are received approximately every 20ms. Fixed loop timing is not
* guaranteed due to network timing variability and the function may not be
* called at all if the Driver Station is disconnected. For most use cases the
* variable timing will not be an issue. If your code does require guaranteed
* fixed periodic timing, consider using Notifier or PIDController instead.
*/
void IterativeRobot::RobotPeriodic() {
static bool firstRun = true;
if (firstRun) {
llvm::outs() << "Default " << __FUNCTION__ << "() method... Overload me!\n";
firstRun = false;
}
}
/**
* Periodic code for disabled mode should go here.
*
* Users should override this method for code which will be called each time a
* new packet is received from the driver station and the robot is in disabled
* mode.
*
* Packets are received approximately every 20ms. Fixed loop timing is not
* guaranteed due to network timing variability and the function may not be
* called at all if the Driver Station is disconnected. For most use cases the
* variable timing will not be an issue. If your code does require guaranteed
* fixed periodic timing, consider using Notifier or PIDController instead.
*/
void IterativeRobot::DisabledPeriodic() {
static bool firstRun = true;
if (firstRun) {
llvm::outs() << "Default " << __FUNCTION__ << "() method... Overload me!\n";
firstRun = false;
}
}
/**
* Periodic code for autonomous mode should go here.
*
* Users should override this method for code which will be called each time a
* new packet is received from the driver station and the robot is in autonomous
* mode.
*
* Packets are received approximately every 20ms. Fixed loop timing is not
* guaranteed due to network timing variability and the function may not be
* called at all if the Driver Station is disconnected. For most use cases the
* variable timing will not be an issue. If your code does require guaranteed
* fixed periodic timing, consider using Notifier or PIDController instead.
*/
void IterativeRobot::AutonomousPeriodic() {
static bool firstRun = true;
if (firstRun) {
llvm::outs() << "Default " << __FUNCTION__ << "() method... Overload me!\n";
firstRun = false;
}
}
/**
* Periodic code for teleop mode should go here.
*
* Users should override this method for code which will be called each time a
* new packet is received from the driver station and the robot is in teleop
* mode.
*
* Packets are received approximately every 20ms. Fixed loop timing is not
* guaranteed due to network timing variability and the function may not be
* called at all if the Driver Station is disconnected. For most use cases the
* variable timing will not be an issue. If your code does require guaranteed
* fixed periodic timing, consider using Notifier or PIDController instead.
*/
void IterativeRobot::TeleopPeriodic() {
static bool firstRun = true;
if (firstRun) {
llvm::outs() << "Default " << __FUNCTION__
<< "()v method... Overload me!\n";
firstRun = false;
}
}
/**
* Periodic code for test mode should go here.
*
* Users should override this method for code which will be called each time a
* new packet is received from the driver station and the robot is in test mode.
*
* Packets are received approximately every 20ms. Fixed loop timing is not
* guaranteed due to network timing variability and the function may not be
* called at all if the Driver Station is disconnected. For most use cases the
* variable timing will not be an issue. If your code does require guaranteed
* fixed periodic timing, consider using Notifier or PIDController instead.
*/
void IterativeRobot::TestPeriodic() {
static bool firstRun = true;
if (firstRun) {
llvm::outs() << "Default " << __FUNCTION__ << "() method... Overload me!\n";
firstRun = false;
DriverStation::GetInstance().WaitForData();
LoopFunc();
}
}

View File

@@ -0,0 +1,200 @@
/*----------------------------------------------------------------------------*/
/* Copyright (c) FIRST 2017. All Rights Reserved. */
/* Open Source Software - may be modified and shared by FRC teams. The code */
/* must be accompanied by the FIRST BSD license file in the root directory of */
/* the project. */
/*----------------------------------------------------------------------------*/
#include "IterativeRobotBase.h"
#include <cstdio>
#include "HAL/HAL.h"
#include "LiveWindow/LiveWindow.h"
#include "llvm/raw_ostream.h"
using namespace frc;
/**
* Robot-wide initialization code should go here.
*
* Users should override this method for default Robot-wide initialization which
* will be called when the robot is first powered on. It will be called exactly
* one time.
*
* Warning: the Driver Station "Robot Code" light and FMS "Robot Ready"
* indicators will be off until RobotInit() exits. Code in RobotInit() that
* waits for enable will cause the robot to never indicate that the code is
* ready, causing the robot to be bypassed in a match.
*/
void IterativeRobotBase::RobotInit() {
llvm::outs() << "Default " << __FUNCTION__ << "() method... Overload me!\n";
}
/**
* Initialization code for disabled mode should go here.
*
* Users should override this method for initialization code which will be
* called each time
* the robot enters disabled mode.
*/
void IterativeRobotBase::DisabledInit() {
llvm::outs() << "Default " << __FUNCTION__ << "() method... Overload me!\n";
}
/**
* Initialization code for autonomous mode should go here.
*
* Users should override this method for initialization code which will be
* called each time the robot enters autonomous mode.
*/
void IterativeRobotBase::AutonomousInit() {
llvm::outs() << "Default " << __FUNCTION__ << "() method... Overload me!\n";
}
/**
* Initialization code for teleop mode should go here.
*
* Users should override this method for initialization code which will be
* called each time the robot enters teleop mode.
*/
void IterativeRobotBase::TeleopInit() {
llvm::outs() << "Default " << __FUNCTION__ << "() method... Overload me!\n";
}
/**
* Initialization code for test mode should go here.
*
* Users should override this method for initialization code which will be
* called each time the robot enters test mode.
*/
void IterativeRobotBase::TestInit() {
llvm::outs() << "Default " << __FUNCTION__ << "() method... Overload me!\n";
}
/**
* Periodic code for all modes should go here.
*
* This function is called each time a new packet is received from the driver
* station.
*/
void IterativeRobotBase::RobotPeriodic() {
static bool firstRun = true;
if (firstRun) {
llvm::outs() << "Default " << __FUNCTION__ << "() method... Overload me!\n";
firstRun = false;
}
}
/**
* Periodic code for disabled mode should go here.
*
* Users should override this method for code which will be called each time a
* new packet is received from the driver station and the robot is in disabled
* mode.
*/
void IterativeRobotBase::DisabledPeriodic() {
static bool firstRun = true;
if (firstRun) {
llvm::outs() << "Default " << __FUNCTION__ << "() method... Overload me!\n";
firstRun = false;
}
}
/**
* Periodic code for autonomous mode should go here.
*
* Users should override this method for code which will be called each time a
* new packet is received from the driver station and the robot is in autonomous
* mode.
*/
void IterativeRobotBase::AutonomousPeriodic() {
static bool firstRun = true;
if (firstRun) {
llvm::outs() << "Default " << __FUNCTION__ << "() method... Overload me!\n";
firstRun = false;
}
}
/**
* Periodic code for teleop mode should go here.
*
* Users should override this method for code which will be called each time a
* new packet is received from the driver station and the robot is in teleop
* mode.
*/
void IterativeRobotBase::TeleopPeriodic() {
static bool firstRun = true;
if (firstRun) {
llvm::outs() << "Default " << __FUNCTION__ << "() method... Overload me!\n";
firstRun = false;
}
}
/**
* Periodic code for test mode should go here.
*
* Users should override this method for code which will be called each time a
* new packet is received from the driver station and the robot is in test mode.
*/
void IterativeRobotBase::TestPeriodic() {
static bool firstRun = true;
if (firstRun) {
llvm::outs() << "Default " << __FUNCTION__ << "() method... Overload me!\n";
firstRun = false;
}
}
IterativeRobotBase::IterativeRobotBase() {
RobotInit();
// Tell the DS that the robot is ready to be enabled
HAL_ObserveUserProgramStarting();
}
void IterativeRobotBase::LoopFunc() {
// Call the appropriate function depending upon the current robot mode
if (IsDisabled()) {
// call DisabledInit() if we are now just entering disabled mode from
// either a different mode or from power-on
if (m_lastMode != Mode::kDisabled) {
LiveWindow::GetInstance()->SetEnabled(false);
DisabledInit();
m_lastMode = Mode::kDisabled;
}
HAL_ObserveUserProgramDisabled();
DisabledPeriodic();
} else if (IsAutonomous()) {
// call AutonomousInit() if we are now just entering autonomous mode from
// either a different mode or from power-on
if (m_lastMode != Mode::kAutonomous) {
LiveWindow::GetInstance()->SetEnabled(false);
AutonomousInit();
m_lastMode = Mode::kAutonomous;
}
HAL_ObserveUserProgramAutonomous();
AutonomousPeriodic();
} else if (IsOperatorControl()) {
// call TeleopInit() if we are now just entering teleop mode from
// either a different mode or from power-on
if (m_lastMode != Mode::kTeleop) {
LiveWindow::GetInstance()->SetEnabled(false);
TeleopInit();
m_lastMode = Mode::kTeleop;
Scheduler::GetInstance()->SetEnabled(true);
}
HAL_ObserveUserProgramTeleop();
TeleopPeriodic();
} else {
// call TestInit() if we are now just entering test mode from
// either a different mode or from power-on
if (m_lastMode != Mode::kTest) {
LiveWindow::GetInstance()->SetEnabled(true);
TestInit();
m_lastMode = Mode::kTest;
}
HAL_ObserveUserProgramTest();
TestPeriodic();
}
RobotPeriodic();
}

View File

@@ -13,6 +13,7 @@
#include "HAL/HAL.h"
#include "HLUsageReporting.h"
#include "Internal/HardwareHLReporting.h"
#include "LiveWindow/LiveWindow.h"
#include "RobotState.h"
#include "SmartDashboard/SmartDashboard.h"
#include "Utility.h"
@@ -53,6 +54,13 @@ RobotBase::RobotBase() : m_ds(DriverStation::GetInstance()) {
std::fputs(WPILibVersion, file);
std::fclose(file);
}
// First and one-time initialization
NetworkTable::GetTable("LiveWindow")
->GetSubTable("~STATUS~")
->PutBoolean("LW Enabled", false);
LiveWindow::GetInstance()->SetEnabled(false);
}
/**

View File

@@ -28,13 +28,6 @@ using namespace frc;
void SampleRobot::StartCompetition() {
LiveWindow* lw = LiveWindow::GetInstance();
HAL_Report(HALUsageReporting::kResourceType_Framework,
HALUsageReporting::kFramework_Simple);
NetworkTable::GetTable("LiveWindow")
->GetSubTable("~STATUS~")
->PutBoolean("LW Enabled", false);
RobotInit();
// Tell the DS that the robot is ready to be enabled
@@ -43,9 +36,6 @@ void SampleRobot::StartCompetition() {
RobotMain();
if (!m_robotMainOverridden) {
// first and one-time initialization
lw->SetEnabled(false);
while (true) {
if (IsDisabled()) {
m_ds.InDisabled(true);
@@ -147,4 +137,7 @@ void SampleRobot::Test() {
*/
void SampleRobot::RobotMain() { m_robotMainOverridden = false; }
SampleRobot::SampleRobot() : m_robotMainOverridden(true) {}
SampleRobot::SampleRobot() {
HAL_Report(HALUsageReporting::kResourceType_Framework,
HALUsageReporting::kFramework_Simple);
}

View File

@@ -0,0 +1,55 @@
/*----------------------------------------------------------------------------*/
/* Copyright (c) FIRST 2017. All Rights Reserved. */
/* Open Source Software - may be modified and shared by FRC teams. The code */
/* must be accompanied by the FIRST BSD license file in the root directory of */
/* the project. */
/*----------------------------------------------------------------------------*/
#include "TimedRobot.h"
#include <chrono>
#include "HAL/HAL.h"
using namespace frc;
using namespace std::chrono_literals;
/**
* Provide an alternate "main loop" via StartCompetition().
*/
void TimedRobot::StartCompetition() {
// Loop forever, calling the appropriate mode-dependent function
m_startLoop = true;
m_loop->StartPeriodic(m_period);
while (true) {
std::this_thread::sleep_for(24h);
}
}
/**
* Set time period between calls to Periodic() functions.
*
* A timer event is queued for periodic event notification. Each time the
* interrupt occurs, the event will be immediately requeued for the same time
* interval.
*
* @param period Period in seconds.
*/
void TimedRobot::SetPeriod(double period) {
m_period = period;
if (m_startLoop) {
m_loop->StartPeriodic(m_period);
}
}
TimedRobot::TimedRobot() {
m_loop = std::make_unique<Notifier>(&TimedRobot::LoopFunc, this);
// HAL_Report(HALUsageReporting::kResourceType_Framework,
// HALUsageReporting::kFramework_Periodic);
HAL_Report(HALUsageReporting::kResourceType_Framework,
HALUsageReporting::kFramework_Iterative);
}
TimedRobot::~TimedRobot() { m_loop->Stop(); }