View Single Post
  #1   Spotlight this post!  
Unread 01-24-2016, 08:29 AM
jreneew2's Avatar
jreneew2 jreneew2 is offline
Alumni of Team 2053 Tigertronics
AKA: Drew Williams
FRC #2053 (TigerTronics)
Team Role: Programmer
 
Join Date: Jan 2014
Rookie Year: 2013
Location: Vestal, NY
Posts: 189
jreneew2 has a spectacular aura aboutjreneew2 has a spectacular aura aboutjreneew2 has a spectacular aura about
Question Multiple timer objects / deleted timers

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:

Code:
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

Code:
#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
Reply With Quote