Configure a fake motor

Configuring a fake motor can be convenient for testing software without using any hardware. For example, you can use a fake component as a placeholder while waiting on a hardware shipment, so that other components that depend on this motor (for example, a base) won’t fail to initialize, and your SDK code won’t throw errors when it fails to find a physical motor connected to your robot.

Configuration

To configure a fake motor as a component of your robot, you don’t need any hardware, and you don’t need to configure any attributes (though you can configure optional ones).

Just configure your fake motor with a name, type and model:

FieldDescription
nameChoose a name to identify the motor.
typemotor is the type for all motor components.
modelfake
Screenshot of a G P I O motor config with the In1 and In2 pins configured and the PWM pin field left blank.
{
  "components": [
    {
      "name": <motor_name>,
      "type": "motor",
      "model": "fake",
      "attributes": {
        <...>
      },
      "depends_on": []
    }
  ]
}
{
  "components": [
    {
      "name": "fake-motor",
      "type": "motor",
      "model": "fake",
      "attributes": {
        "pins": {
          "dir": "",
          "pwm": ""
        },
        "board": "",
        "dir_flip": false
      },
      "depends_on": []
    }
  ]
}

Optional Attributes

Since a fake motor isn’t a physical piece of hardware, attributes are only representational and not required. However, if you would like to mock up a virtual placeholder to more closely represent a real, physical motor, you can configure some or all of the following attributes:

NameTypeDefault ValueDescription
boardstringName of board to which the motor driver is wired.
min_power_pctfloat0.0Sets a limit on minimum power percentage sent to the motor.
max_power_pctfloat1.0Range is 0.06 to 1.0; sets a limit on maximum power percentage sent to the motor.
pwm_frequint800Sets the PWM pulse frequency in Hz. Many motors operate optimally in the kHz range.
encoderstringThe name of an encoder attached to this motor. See encoded motor. If an encoder is configured on a fake motor, ticks_per_rotation becomes required.
max_rpmfloatThis is an estimate of the maximum RPM the motor will run at with full power under no load. The GoFor method calculates how much power to send to the motor as a percentage of max_rpm. If unknown, you can set it to 100, which will mean that giving 40 as the rpm argument to GoFor or GoTo will set it to 40% speed. For non-encoded fake motors, this is required or a default is assigned.
ticks_per_rotationintBecomes required for calculations if an encoder is configured (unlike on a real motor). For a stepper motor, the number of steps in one full rotation (200 is common). For an encoded motor, how many encoder ticks in one full rotation. See data sheet (for a real motor).
dir_flipboolFalseFlips the direction of “forward” versus “backward” rotation.
pinsobjectA structure that holds pin configuration information.

Nested within the pins struct:

NameTypeDescription
astringSee Pin Information. Corresponds to “IN1” on many driver data sheets. Pin number such as “36.” Viam uses board pin numbers, not GPIO numbers.
bstringSee Pin Information. Corresponds to “IN2” on many driver data sheets. Pin number such as “36.” Viam uses board pin numbers, not GPIO numbers.
dirstringSee Pin Information. Pin number such as “36.” Viam uses board pin numbers, not GPIO numbers.
pwmstringSee Pin Information. Pin number such as “36.” Viam uses board pin numbers, not GPIO numbers.

Pin Information

There are three common ways for the computing device to communicate with a brushed DC motor driver chip. The driver data sheet (for a real, not fake, motor) will specify which one to use.

  • PWM/DIR: One digital input (such as a GPIO pin) sends a pulse width modulation (PWM) signal to the driver to control speed while another digital input sends a high or low signal to control the direction.
  • In1/In2 (or A/B): One digital input is set to high and another set to low turns the motor in one direction and vice versa, while speed is controlled with PWM through one or both pins.
  • In1/In2 + PWM: Three pins: an In1 (A) and In2 (B) to control direction and a separate PWM pin to control speed.