Just a quick question for people who know C++ better than me,
I am writing some code for mechanisms on our robot and am trying to have one command for controlling the shooter in teleop and auto modes. I have in OI.cpp:
buttonA->WhileHeld(new ShooterControl(1)); //forwards
With my undertanding, WhileHeld makes a new instance of the command each time the previous finishes.
and in ShooterControl.cpp
#include "ShooterControl.h"
ShooterControl::ShooterControl(float speed, float target)
{
// Use Requires() here to declare subsystem dependencies
// eg. Requires(chassis);
Requires(Robot::shooterSubsystem.get());
timer.reset(new Timer());
timer->Reset();
timer->Start();
timeCurrent = 0;
timeTarget = target;
inputSpeed = speed;
isDone = false;
}
// Called just before this Command runs the first time
void ShooterControl::Initialize()
{
}
// Called repeatedly when this Command is scheduled to run
void ShooterControl::Execute()
{
timeCurrent = timer->Get();
if(timeTarget == 0) { //if zero use in teleop mode
Robot::shooterSubsystem->Shoot(1);
isDone = true;
}
else {
if(timeCurrent >= timeTarget) {
Robot::shooterSubsystem->Shoot(0);
isDone = true;
}
else {
Robot::shooterSubsystem->Shoot(inputSpeed);
isDone = false;
}
}
}
// Make this return true when this Command no longer needs to run execute()
bool ShooterControl::IsFinished()
{
return isDone;
}
// Called once after isFinished returns true
void ShooterControl::End()
{
Robot::shooterSubsystem->Shoot(0);
}
// Called when another command which requires one or more of the same
// subsystems is scheduled to run
void ShooterControl::Interrupted()
{
Robot::shooterSubsystem->Shoot(0);
}
Would this code bog down memory because of the multiple instances of the Timer Object? When does a timer get deleted?
Thanks,
Drew