Skip to main content

A package to run 12V DC Motors on the Raspberry Pi

Project description

MotorLib: A configurable library for 12V DC Motors

Author: Tiernan Lindauer

MotorLib is for use with any microcontroller/computer that has GPIO, like the popular Raspberry Pi or Arduino boards. It also requires the use of a PWM motor controller, with examples provided below. Examples have been provided on how to use this library are provided in the MotorTest.py and PlotPID.py files. MotorTest.py is also reproduced below:

import math
from time import time

from pymotorlib import Motor, MotorType, RunMode
from pymotorlib.PWMDriver import GoBildaControl

# Time to run the motor, in seconds
RUN_TIME = 10

# Number of periods
N_PERIODS = 2

# Define pwm and encoder pins
pwm = 12
encoder = (11, 12)

# Establish a motor object for a gobilda 312 RPM motor, with the specified pwm and encoder ports and position type control
my_motor = Motor(MotorType.GOBILDA_312, pwm, *encoder, GoBildaControl, RunMode.POSITION_CONTROL)

# Set PID for the motor
my_motor.set_pid_coefficients(kP=0.001)

# Target position function to follow - sin wave of N_PERIODS over RUN_TIME length
target = lambda t: my_motor.type.TICKS_PER_REV * math.sin(N_PERIODS * math.pi * t / (RUN_TIME / 2))

before = time()
current = 0
while current < RUN_TIME:
    my_motor.set_target(target(current))
    my_motor.update_pid()
    current = time() - before

my_motor.stop()

As you can see, it is relatively easy to get your motor up and running with this library. Here are the different config options available:

Motor Configuration

Motor type (What motor you're running):

ORIBITAL_20, GOBILDA_312 - you can add more by creating a class that has a static TICKS_PER_REV variable

Example:

class NEW_MOTOR:
    TICKS_PER_REV = 500  # Use your own value here for the amount of encoder ticks for each revolution
Set the GPIO pins:

The PWM channel should be set to the pin the PWM wire is connected to. The encoder pins should correspond first to channel A, second to channel B.

RunMode (how to use the motor):

POSITION_CONTROL - go to a position using PID, VELOCITY_CONTROL - go to a velocity using PID, RAW_POWER - do not use PID, instead just set the power manually

Using a custom motor controller:

By default, it is assumed you are using the GoBilda Motor Controller. There is also support for the REV Robotics Spark Mini. The example uses GoBildaControl, however if you switch that to SparkMini you can run the motor off the Spark Mini as well.

To use your own custom PWM controller, set a class up as follows:

class NewController(PWMDriver):
    MIN_PULSE = 300  # Set this to whatever your controller's min pulse width is
    MAX_PULSE = 2300  # Set this to whatever your controller's max pulse width is  
    half_width = (MAX_PULSE - MIN_PULSE) / 2

    @staticmethod
    def make_pulse(power):
        return (power * NewController.half_width) + NewController.MIN_PULSE + NewController.half_width

MIN_PULSE should be equivalent to a power of -1. MAX_PULSE should equate to a power of 1.

Bugs/Issues

To report an issue or bug in the code, please start one here on GitHub!

T. Lindauer

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

pymotorlib-0.6.tar.gz (8.1 kB view details)

Uploaded Source

Built Distribution

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

pymotorlib-0.6-py3-none-any.whl (8.1 kB view details)

Uploaded Python 3

File details

Details for the file pymotorlib-0.6.tar.gz.

File metadata

  • Download URL: pymotorlib-0.6.tar.gz
  • Upload date:
  • Size: 8.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.10.8

File hashes

Hashes for pymotorlib-0.6.tar.gz
Algorithm Hash digest
SHA256 728b2713ad09b29501d32f8ef1ff07594213a17da689b654e60adfe3b8b4b8bd
MD5 26c1a8db6d406500611837f4c9afaf40
BLAKE2b-256 2803a6cf23917b7f8007155c153827d876e86e7cfa277a65cae2035b59fb5d37

See more details on using hashes here.

File details

Details for the file pymotorlib-0.6-py3-none-any.whl.

File metadata

  • Download URL: pymotorlib-0.6-py3-none-any.whl
  • Upload date:
  • Size: 8.1 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.10.8

File hashes

Hashes for pymotorlib-0.6-py3-none-any.whl
Algorithm Hash digest
SHA256 8a3d9b6c1d17484c75fb58b40669abd0fb9e6a8024714bfa1eeef379265906d6
MD5 bde42fe584c78b9a3338294edb8a501a
BLAKE2b-256 e0f68bd27193c7500cbf0bfa7b9258262bc01dcc3dd44e96457c5a2c84a2dbc8

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