1410
Cached Motors

Go back to 0000 Index

Cached (or rather caching) motors hold the state of the motors that they are wrapping. Some implementations also provide utility functions. A cached motor may store the motor's last velocity, direction, or other properties. Here is an example CachedMotor:

public class CachedMotor {
	private final DcMotor motor;
	private double lastVelocity;
	private double lastPower;
	
	public CachedMotor(DcMotor motor) {
		this.motor = motor;
	}
	
	public void setVelocity(double velocity) {
		motor.setVelocity(velocity);
		this.lastVelocity = velocity;
	}
	
	public void setPower(double power) {
		motor.setPower(power);
		this.lastPower = power;
	}
}

This CachedMotor implementation does not implement most methods that one would need, but it gives a general idea of what it would look like. The class should also implement any methods on the motor that you are using, such as setTargetPosition or setZeroPowerbehavior.