Skip to main content

Python API for the Raspberry Pi + MotorGo line.

Project description

🐍 | motorgo-python | 🐍

Python API for controlling PiHat-compatible MotorGo boards from the Raspberry Pi.


Introduction

motorgo-python provides a Python API to control a MotorGo when it is mounted to a Raspberry Pi as a PiHat. It enables you to control the motor channels and stream the enocder and IMU data from the MotorGo. This package also includes a motorgo command line utility (CLI) that lets you configure settings on the MotorGo and perform basic operations without the need for code.

Please follow our setup guide at (our docs)[https://docs.motorgo.net].

Supported Boards

Currently, motorgo-python only supports the Plink. Support for the Axis is on the way!

Installation

You can install motorgo-python using pip.

pip install motorgo-python

If you are using Python 3.12 or greater, you may need to use the --break-system-packages argument. motorgo-python cannot be installed in a virtual env as it needs access to

Next, you need to configure the MotorGo to be used in PiHat mode, which can be done using the included CLI. Attach the MotorGo to the 40-pin Pi header and run the command:

motorgo flash

Usage

Below is an example program that demonstrates how to use the pyplink package to control motor channels on the MotorGo Plink board.

This example, and others, can be found in the examples/ directory of this repository.

# 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.

import time

from motorgo import BrakeMode, ControlMode, Plink


def main():
    # Create a Plink object, the main interface to the MotorGo board
    plink = Plink()

    # The first thing to set up for a Plink is the power supply voltage.
    # This is the voltage you are providing to the Plink on the barrel jack.
    # If this is the battery, make sure this is the charged voltage.
    plink.power_supply_voltage = 9.0

    # The Plink object has 4 MotorChannel objects, corresponding to the 4 motor channels
    # on the board
    # You can access them directly: plink.channel1
    # Or you can save references as local variables for convenience:
    left_motor = plink.channel1
    right_motor = plink.channel2

    # Next, you need to set the motor voltage limit.
    # This is the maximum voltage your motor channels will output to protect the motors.
    # The voltage limit is 0 by default, which means the motors will not move if this is not set.
    left_motor.motor_voltage_limit = 6.0
    right_motor.motor_voltage_limit = 6.0
    plink.channel3.motor_voltage_limit = 6.0
    plink.channel4.motor_voltage_limit = 6.0

    # Finally, connect to the MotorGo board and psuh the configuration
    plink.connect()

    # You can configure how you want to control the motor channels.
    # Power mode: Set the power of the motor in the range [-1, 1]
    #            This directly corresponds to setting a voltage to the motor
    #
    # Velocity mode: Set the velocity of the motor in rad/s
    #              This mode requires setting the velocity PID gains
    #              It also requires an encoder to be connected to the motor
    left_motor.control_mode = ControlMode.POWER
    right_motor.control_mode = ControlMode.VELOCITY
    plink.channel3.control_mode = ControlMode.POWER
    plink.channel4.control_mode = ControlMode.POWER

    # If you are using ControlMode.VELOCITY, you must set the velocity PID gains
    right_motor.set_velocity_pid_gains(4.5, 0.1, 0.0)

    while True:

        # Set motor powers
        # Set the power command in the range [-1, 1]
        left_motor.power_command = 0.5

        # Set the velocity command in rad/s
        right_motor.velocity_command = 1.2

        # Set the power command in the range [-1, 1]
        plink.channel3.power_command = 0.25
        plink.channel4.power_command = -0.25

        # You can read the position and velocity of the motor channels from the encoders
        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("----")

        time.sleep(0.01)


if __name__ == "__main__":
    main()

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

motorgo_python-0.1.0.tar.gz (17.4 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

motorgo_python-0.1.0-py3-none-any.whl (20.4 kB view details)

Uploaded Python 3

File details

Details for the file motorgo_python-0.1.0.tar.gz.

File metadata

  • Download URL: motorgo_python-0.1.0.tar.gz
  • Upload date:
  • Size: 17.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.12.3

File hashes

Hashes for motorgo_python-0.1.0.tar.gz
Algorithm Hash digest
SHA256 e39057aeef7c4cb49a7df59db807ad9c0d022ff13972f0ec4e68c7eb9505687d
MD5 1ac9c97ba9547f72ef7222c3834ee041
BLAKE2b-256 d27b861279d108388055f1aa9aa764a17ca6b5dbfae8be2ecf7d00f404e47c7d

See more details on using hashes here.

File details

Details for the file motorgo_python-0.1.0-py3-none-any.whl.

File metadata

  • Download URL: motorgo_python-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 20.4 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.12.3

File hashes

Hashes for motorgo_python-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 802788b45bd89b152449064d31d7d18d8649026a2b0933c501e1025734c045e0
MD5 a9a63a03d96623102992d147a5a0e6c3
BLAKE2b-256 e2b35c74ca92fd84780cfe9cb0e149b9a170daa424e1f1e9cc58172651326037

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page