samedi 30 juin 2018

How to properly link new SimulatedEncoders class to an IMotor interface

Full Code: https://github.com/Team-2502/ezAuton

package com.team2502.ezauton.actuators;

/**
 * A motor. Takes in an input and runs the motor.
 */
public interface IMotor
{
}

package com.team2502.ezauton.actuators;

/**
 * A motor which can be run at a certain velocity
 */
public interface IVelocityMotor extends IMotor
{
    void runVelocity(double velocity);
}

package com.team2502.ezauton.actuators;

/**
 * A motor which can be run at a certain voltage
 */
public interface IVoltageMotor extends IMotor
{
    void runVoltage(double voltage);
}

package com.team2502.ezauton.localization.sensors;

/**
 * A sensor which can record revolutions/s and revolutions as a distance
 */
public interface IEncoder extends ITachometer
{
    /**
     * @return revolutions
     */
    double getPosition();
}

package com.team2502.ezauton.localization.sensors;

/**
 * A sensor which can measure revolutions / s
 */
public interface ITachometer extends ISensor
{
    /**
     * @return revolutions / s
     */
    double getVelocity();
}

  • right now all IMotor subclasses (implementations of interfaces IVelocityMotor, IVoltageMotor) are basically maps of (input -> action) (they are effectively interfaces which act as setters for a motor).
  • As I am further decoupling logic, I am making a group of SimulationEncoders ⊆ IEncoder which can be useful for...
    • simulations
    • fake encoders (i.e. if someone is using voltage drive without encoders but still wants to be able to estimate position)
  • The problem is how to link the group of implemented SimulationEncoders to IMotor implementations.
    • I could possibly have SimulatedEncoder1(ITachometer) and SimulatedEncoder2(IVoltmeter) for IVelocityMotor and IVoltageMotor, respectively. SimulatedEncoders would implement an interface Updateable and be called every x seconds to read ITachometer and IVoltometer inputs. The problem with this is it is clunky and the SimulatedEncoders might work better knowing every time a method of IMotor is called instead of periodically checking the last call.
    • I could use the Observer pattern to add SimulationEncoders which listen to setVelocity(...), setVoltage(...)
    • I could try to couple these into one class which acts as a wrapper and forwards velocity/voltage to a SimulatedEncoder and an IVelocityMotor/IVoltageMotor... the problem with this is I don't see any easy way to link specific types of SimulatedEncoders to their respective type of motor.

Any help resolving this dilemma would be greatly appreciated.

Aucun commentaire:

Enregistrer un commentaire