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 control code won’t throw errors when it fails to find a physical motor connected to your machine.

Navigate to the CONFIGURE tab of your machine’s page in the Viam app. Click the + icon next to your machine part in the left-hand menu and select Component. Select the motor type, then select the fake model. Enter a name or use the suggested name for your motor and click Create.

A fake motor config.

Edit and fill in the attributes as applicable.

{
  "components": [
    {
      "name": "<your-motor-name>",
      "model": "fake",
      "type": "motor",
      "namespace": "rdk",
      "attributes": {
        <...>
      },
      "depends_on": []
    }
  ]
}
{
  "components": [
    {
      "name": "fake-motor",
      "model": "fake",
      "type": "motor",
      "namespace": "rdk",
      "attributes": {
        "pins": {
          "dir": "",
          "pwm": ""
        },
        "board": "",
        "dir_flip": false
      },
      "depends_on": []
    }
  ]
}

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 for a real, physical motor, you can configure some or all of the following attributes:

NameTypeRequired?Description
boardstringOptionalname of the board to which the motor driver is wired.
min_power_pctfloatOptionalSets a limit on minimum power percentage sent to the motor.
Default: 0.0
max_power_pctfloatOptionalRange is 0.06 to 1.0; sets a limit on maximum power percentage sent to the motor.
Default: 1.0
pwm_freqintOptionalSets the PWM pulse frequency in Hz. Many motors operate optimally in the kHz range.
Default: 800
encoderstringOptionalThe 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_rpmfloatOptionalAn estimate of the max revolutions per minute (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.
Default: 100
ticks_per_rotationintOptional**Required for calculations if an encoder is configured. 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_flipboolOptionalFlips the direction of “forward” versus “backward” rotation.
Default: false
pinsobjectOptionalA struct that holds pin configuration information.

Nested within pins:

NameTypeRequired?Description
astringOptionalSee Pin Information. Corresponds to “IN1” on many driver data sheets. Pin number of the GPIO pin this is wired to on the board.
bstringOptionalSee Pin Information. Corresponds to “IN2” on many driver data sheets. Pin number of the GPIO pin this is wired to on the board.
dirstringOptionalSee Pin Information. Pin number of the GPIO pin this is wired to on the board.
pwmstringOptionalSee Pin Information. Pin number of the GPIO pin this is wired to on the board.

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.

Test the motor

Once your motor is configured and your machine is connected, go to the CONTROL tab and click on the motor’s dropdown panel. Use the buttons to try turning your motor forwards or backwards at different power levels and notice how the Position number changes.

Motor control panel.

Troubleshooting

If your motor is not working as expected, follow these steps:

  1. Check your machine logs on the LOGS tab to check for errors.
  2. Review this motor model’s documentation to ensure you have configured all required attributes.
  3. Check that all wires are securely attached to the correct pins.
  4. Click on the TEST panel on the CONFIGURE or CONTROL tab and test if you can use the motor there.

If none of these steps work, reach out to us on the Community Discord and we will be happy to help.

Next steps

For more configuration and usage info, see:

Have questions, or want to meet other people working on robots? Join our Community Discord.

If you notice any issues with the documentation, feel free to file an issue or edit this file.