PiHat ModeQuickstart

PiHat Quickstart: Spin a Motor

⚠️

MotorGo PiHat support is in beta. This guide reflects the setup process for release, but will not work with the current software. Please check back soon for updates.

🤖

Applies to Plink, Axis

This guide will walk you through the process of spinning a motor using the MotorGo driver in PiHat mode.

On Your Robot:

Gather the hardware

  • Raspberry Pi
  • MicroSD Card for the Raspberry Pi
  • Motor - Brushed (Plink) or Brushless (Axis)
  • EncoderGo (optional for Plink)
  • A power supply with a barrel jack (Plink) or XT30 connector (Axis)

Connect the MotorGo to the Raspberry Pi

Attach the MotorGo to the Raspberry Pi 40 pin header. Use standoffs that are at least 11 mm tall to prevent the MotorGo from contacting the Raspberry Pi. If you are using a Raspberry Pi with a heatsink, you may need longer standoffs.

Connect the Motor and Encoder to the MotorGo

If you’re using a Plink, you do not need to connect an encoder to spin the motor.

On Your Computer:

Setup the Raspberry Pi

  • Use Raspberry Pi Imager to image the microSD card

    • Use the latest Raspberry Pi OS (64-bit)
    • Configure WiFi and SSH before writing the disk image
  • Insert the microSD card into the Raspberry Pi and power everything on by connecting the power supply to the MotorGo.

More Details

Enable communication interfaces on the Raspberry Pi

Open a terminal on the Raspberry Pi and run the following commands:

bash
sudo raspi-config

Select 3. Interface Options and enable:

  • SPI
  • I2C
  • Serial

Reboot the Raspberry Pi after making the changes.

Flash the MotorGo with the latest PiHat firmware

Open a terminal on the Raspberry Pi and run the following commands:

bash
pip install motorgo
motorgo flash

If you are using Python 3.12 or greater, you may have to include --break-system-packages in the pip install command.


More Details

Run the example code

Copy the Python script below to a file on the Raspberry Pi and run it with Python. More examples can be found at the MotorGo Python GitHub repository.

python
# spin_motors.py
# Before running this script, ensure that the MotorGo Plink is
# connected to the Raspberry Pi and that it has been flashed with the
# MotorGo firmware.
 
from pyplink import Plink, BrakeMode, ControlMode
 
# Create a Plink object
plink = Plink()
 
# This command will initiate communications and confirm
# that the Plink is connected/available
plink.connect()
 
# The Plink object has 4 MotorChannel objects, corresponding to the 4 motor channels
# on the board
# You can save references as local variables for convenience (as below) or
# access them directly from the Plink object
left_motor = plink.channel1
right_motor = plink.channel2
 
# You can configure the control mode of the motor channels
# The two options are ControlMode.VELOCITY and ControlMode.POWER
left_motor.control_mode = ControlMode.VELOCITY
right_motor.control_mode = ControlMode.VELOCITY
plink.channel3.control_mode = ControlMode.POWER
plink.channel4.control_mode = ControlMode.POWER
 
# You can configure the brake mode of the motor channels
# The two options are BrakeMode.BRAKE and BrakeMode.COAST
left_motor.brake_mode = BrakeMode.BRAKE
right_motor.brake_mode = BrakeMode.BRAKE
plink.channel3.brake_mode = BrakeMode.COAST
plink.channel4.brake_mode = BrakeMode.COAST
 
# Main program loop
velocity_command = 0.0
while True:
 
    # Set the velocity command in rad/s
    left_motor.velocity_command = velocity_command
    right_motor.velocity_command = -velocity_command
 
    # Ramp up the velocity command
    velocity_command += 0.1
    if velocity_command > 5.0:
        velocity_command = -5.0
 
    # Set the power command in the range [-1, 1]
    plink.channel3.power_command = 0.5
    plink.channel4.power_command = -0.5
 
    # You can read the position and velocity of the motor channels
    print("----")
    print(
        f"Channel 1 position: {plink.channel1.position}, velocity: {plink.channel1.velocity}"
    )
    print(
        f"Channel 2 position: {plink.channel2.position}, velocity: {plink.channel2.velocity}"
    )
    print(
        f"Channel 3 position: {plink.channel3.position}, velocity: {plink.channel3.velocity}"
    )
    print(
        f"Channel 4 position: {plink.channel4.position}, velocity: {plink.channel4.velocity}"
    )
    print("----")
 
    # Delay as long as you need, communications continue in the background
    time.sleep(0.1)