Just as the title states, how would I retrieve a variable from one subsystem to implement in another subsystem? For example, if I had a dedicated sensor subsystem, is there any way that I could call a method (which returned the sensor value) from within the sensor subsystem to be used in a method in another subsystem (for example, the collector)?
This is really just a classic C++ question. If a method or member in one class is public any other code (in the same space that has a handle/pointer to the class) can access the member or call the method. In practice it is wiser to keep everything things private and add public methods to access your private data. This way your data (or devices) controlled by the object have only carefully defined external interfaces, making your objects more “coherent”.
HTH
Keeping with the intent of the Command Based programming approach the only way to accomplish what you are asking is in commands. All commands inherit CommandBase and CommandBase contains static instances of each subsystem. In your command you could get the value of a sensor from your sensor subsystem and then based on it’s value call different methods in your collector subsystem. This year we had a DistanceSensor subsystem. It essentially provided the distance we were from the goal in feet via a getDistance method. Our shooter had a setDistance method. In a command called AutoSetShooter we did the following. shooter->setDistance(distanceSensor->getDistance());
Depending on the application it might be better to restructure your subsystems so that the corresponding sensors for your collector are in your collector subsystem. The best example of this is limit switches and pots. They belong in the same subsystem that the actuator is in.
Kyle
Yes, you can access other subsystems to get information; depending on how strictly you want to adhere to the command based approach, this may be ‘cheating’ a little, but for read only access it doesn’t do any harm.
The Robot class has static instances of all subsystems, so you can access them from anywhere that includes Robot.h (just as is done in commands).
The syntax is as follows:
bool ShooterPiston::Fire()
{
bool returnValue = false;
if ((Robot::shooterWheels->IsUpToSpeed() == true) &&
(Robot::shooterArm->IsOnTarget() == true) &&
....
{
firingSolenoid->Set(DoubleSolenoid::kReverse);
returnValue = true;
}
return returnValue;
}
In case anyone is confused, both Kyle and Joe are correct, depending on how you started your project. Currently, if you build your build your project with the Windriver plugins, you get a CommandBase that contains all subsystems. If you use RobotBuilder, the Robot class contains all subsystems.