Skip to content

Create a mediator object (currently Mock*) for each Device at startup #26

Description

@brettle

This assumes that #24 and #25 have completed.

Instead of waiting to create a mediator object until we receive a message that targets a particular device, create the mediator object for each device at startup and let the mediator object use the device's fields (from #24) to determine which which messages to register callbacks for. This would allow us to validate the robot's devices before starting the simulation so we could catch errors earlier.

Note that, despite their names, WebotsMotorForwarder, MockGyro, MockedSparkEncoder, and MockedEncoder all implement something close to the mediator design pattern. It's not a perfect match, but it's the closest well-recognized pattern I could find. Anyway, currently they all implement the pattern differently and are not particularly amenable to unit testing. Let's rework them to be consistent and testable. This will make the code easier to maintain and easier for other developers to understand and write their own mediators. To that end, I suggest that each mediator class should:

  1. Have a name of the form <Device>Mediator where <Device> is the name of the corresponding Webots device (typically a PROTO defined in Create PROTOs corresponding to common FRC parts #24).
  2. Have a constructor that takes:
    a. the Webots Device
    b. an optional object that implements a new WebotsDeviceFieldGetter interface, which can be used to access the Webots' device's fields. Default to a simple implementation that uses the controller's Supervisor.
    c. optional objects that implement new WPIDeviceInitNotifier<T> interfaces, which can be used to register to get notified of the initialization of any *Sim objects that the mediator might need. The initialized *Sim object itself should be passed to the callback that is registered. Default to simple wrapper objects that use the static *Sim.registerStaticInitializedCallback() methods. Those wrapper objects should probably be static members of the associated *Sim classes.
  3. upon creation (i.e. in the constructor):
    a. Use the field getter object to parse the Webots device's config info from it's fields.
    b. Use the config info and the notifier objects to get notified when the the relevant *Sim object(s) are initialized.
  4. Implement Runnable with a run method that, once all needed *Sim objects have been initialized, performs the device-specific mediation between the webots Device and *Sim objects. This may involve use of additional config info (e.g. gearmotor reduction ratio, or encoder resolution).
  5. Optionally, once all needed *Sim objects have been initialized, register callbacks on the *Sim objects to get notified of state changes and propagate them to the webots Device.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions