Coder Social home page Coder Social logo

gavinlyonsrepo / rpimotorlib Goto Github PK

View Code? Open in Web Editor NEW
302.0 29.0 73.0 1.94 MB

A python 3 library to connect various motor systems to a Raspberry pi single board computer

Home Page: https://gavinlyonsrepo.github.io/

License: Other

Python 100.00%
raspberry-pi motor servo sg90 28byj-48 library uln2003 python python3 28byj-48-motor

rpimotorlib's Introduction

Website Rss Donate

RpiMotorLib

ScreenShot dcmotor ScreenShot Nema ScreenShot L298N ScreenShot A4988

Table of contents

Overview

  • Name: RpiMotorLib
  • Title: Raspberry pi motor library.
  • Description:

A python 3 library to drive motor controllers and servos with a Raspberry pi.

These components supported are some of the most widely used by maker community. There are three categories in library. Stepper motors, DC Motors and Servos. The end user can import this library into their projects and then control the components with short snippets of code. The library is modular so user can just import/use the section they need.

  • Project URL: URL LINK

  • Tested on Toolchains:

    1. RPI 3 model B. Raspbian 10 Buster, 32 bit. Python 3.7.3.
    2. RPI 3 model B. Raspbian 12 Bookworm, 64 bit. Python 3.11.2.

Installation

Latest version 3.2 (10-2022)

From PyPi with pip or pipx

The Python Package Index (PyPI) is a repository of software for the Python programming language. The program is present in python package index, Pypi. Install using pip or pipx to the location or environment of your choice. Package name = rpimotorlib Link.

NB see notes section for more on pipx.

From Github

Manually install from github The package is also archived on github and can be manually download and installed via python and setup.py. Not recommended.

curl -sL https://github.com/gavinlyonsrepo/RpiMotorLib/archive/3.2.tar.gz | tar xz
cd RpiMotorLib-3.2
python3 setup.py build 
python3 setup.py install --user

Hardware

Supported Components:

Stepper motors

Motor tested Motor controller Help File URL link
Unipolar 28BYJ-48 ULN2003 driver module URL
Bipolar Nema TB6612FNG Dual Driver Carrier URL
Bipolar Nema L298N H-Bridge controller module URL
Bipolar Nema A4988 Stepper Driver Carrier URL
Bipolar Nema DRV8825 Stepper Driver Carrier URL
Bipolar Nema A3967 Stepper Driver aka "easy driver v4.4" URL
Bipolar (untested on hw) LV8729 Stepper Driver Carrier URL
Bipolar (untested) DV8833 Motor controller module TODO
Bipolar (untested) L9110S Motor controller module TODO

DC motors

Motor Motor controller Help File URL link
DC Brushed Motor L298N Motor controller module. URL
DC Brushed Motor L9110S Motor controller module. URL
DC Brushed Motor DV8833 Motor controller module. URL
DC Brushed Motor TB6612FNG Dual Motor Driver Carrier URL
DC Brushed Motor Transistor control URL

Servos

There are two different options for controlling the servo. When using Rpi_GPIO option you may notice twitching at certain delays and stepsizes. This is the result of the implementation of the RPIO PWM software timing. If the application requires precise control the user can pick the pigpio library which uses hardware based timing. The disadvantage being they must install a dependency(pigpio) and start its daemon.

Servo Link
Servo software timing RPi.GPIO module PWM
Servo hardware timing pigpio library module PWM

Software

  1. Separate help files are in documentation folder to learn how to use library. Click on the relevant URL link in tables in hardware section.
  2. Test files used during development are in test folder of repository.
  3. There is a "Software matrix" showing which classes are used to drive which components. This is in the Software_Matrix.md file in documentation folder.

File System

RpiMotorLib files are listed below:

File Path Description
RPiMotorLib/RpiMotorLib.py stepper motor python library file
RPiMotorLib/rpiservolib.py servo python library RPi.GPIO PWM file
RPiMotorLib/rpi_pservo_lib.py servo python library pigpio PWM file
RPiMotorLib/rpi_dc_lib.py DC python motor library file
documentation/*.md 15 markdown library documentation files
test/*Test.py 14 python test files
/usr/share/doc/RpiMotorLib/README.md This help file
RPiMotorLib/RpiMotorScriptLib.py small script with meta data about library

A small script is installed to display version and help information. Run the information script by typing. RpiMotorScriptLib.py -[options]

Option Description
-h Print help information and exit
-v Print version information and exit

Dependencies

  1. RPi.GPIO 0.6.3 Rpi.GPIO pypi page

A module to control Raspberry Pi GPIO channels. This package provides a class to control the GPIO on a Raspberry Pi. This should already be installed on most Raspberry Pis.

  1. pigpio 1.64-1 Homepage

This Dependency is Optional, it is currently only used in one of the two servo control options. pigpio is a library for the Raspberry which allows control of the General Purpose Input Outputs (GPIO).

Notes and issues

RPI 5

Will NOT work on raspberry pi 5's at present as RPi.GPIO does not work anymore due to change's in way raspberry pi 5 handles the peripheral access. See github issue #26

Two Motors simultaneously

Running two motors simultaneously, See github issue #11

If you want to control two or more steppers simultaneously, there are two basic setup files for using threading in test/Multi_Threading_Example folder.

  1. For Unipolar 28BYJ-48 MultiMotorThreading_BYJ.py
  2. For Bipolar DRV8825 Stepper MultiMotorThreading_DRV8825.py

GPIO cleanup method

Potential Issue with GPIO.cleanup() method not working* See github issue #18 and #21

Some users are reporting that GPIO.cleanup() does not work. It does not switch off or "cleanup" GPIO as it should. This is external function from RPi.GPIO. It is mainly used in the test scripts. It is also called by the classes in DC motor if the cleanup method is passed argument "true". If you see this issue simply don't use GPIO.cleanup() or remove GPIO.cleanup and clear the GPIO you set manually or use python "del" method to destroy the relevant class object, to free resources if you need them again.

pipx

As of pep668, Users on many systems will now get an error if they try and install packages on system with pip("~environment is externally managed"). One solution is to use pip to install to a virtual environment.

Another is to use package. PIPX, which installs packages globally into isolated Virtual Environments.

The first problem I had was getting my test files to "see" this isolated Virtual environment so they could import the modules. In test/pipx I have created two pipx examples files showing a solution. In example 1 I append the package location to sys.path using sys.path.insert and in example 2 I simply change the shebang at first line of file(the new shebang is from the pipx installed RpiMotorScriptLib.py file at .local/bin). I will learn more about pipx and see if there is a better solution.

The second problem after finding the correct path is the dependency RPi.GPIO module is not in the pipx venv. So must be "injected" into the venv. this is because I did not include RPi.GPIO in the setup.py as it is always there globally(for most users). I will correct this in next update.

pipx install rpimotorlib
pipx inject rpimotorlib RPi.GPIO

rpimotorlib's People

Contributors

erezlevanon avatar funkyremi avatar gavinlyonsrepo avatar nahakiole avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

rpimotorlib's Issues

Stepper motor runs for ever

Hi, I am currently trying to drive a Nema 17 stepper motor with my Raspberry Pi using an A4988 Stepper Motor Driver. When I run the python scripts they work so far, but when the script is terminated with GPIO.cleanup() the motor slowly rotates forever in one direction. I am quite new to this, what am I doing wrong?
Best regards
Valentin

Gecko G251X

Hello!

Looking to see if someone more knowledgeable then myself has any insight into if this library would be useful in controlling a NEMA17 Stepper (https://openbuildspartstore.com/nema-17-stepper-motor/) with a Gecko G251X stepper driver (https://www.geckodrive.com/g251x-digital-stepper-drive.html).

Per the documentation for the G251X, I should only need a step and direction input from the pi, which pretty much any of the different components offered in this library look like they do. I don't need to worry about any of the micro stepping pins so I could set those to 0 (or some other unused pin).

Would anyone be able to provide their insight into if this would work or not? Am I missing something? Thanks in advance!

Nema 17 stepper with DRV8825 only vibrating

I was looking to use this library with a stepper motor and the 8825 driver, but after running A4998NemaTest.py, the motor vibrates but does not step. I have been also running my own script on the side, which makes the stepper rotate (given that I run the pigpiod daemon).

Are there additional steps I need to do before getting this library to work with an 8825?

[question] motor turning back and forth

I am trying to rotate a stepper motor using RpiMotorLib, following the instructions of a post in makersportal.com (using the same equipment - a DVR8852 module and a Nema 17 and the initialization of the motor from the code in the article is

mymotortest = RpiMotorLib.A4988Nema(direction, step, (21,21,21), "DRV8825")

The motor starts but moves back and forth. I reduced the step delay to 0.05 and I can see that each step is in a different direction (effectively not turning the motor)

I tried to understand what the tuple (21,21,21) means, the docs mention

mode_pins type=tuple of 3 ints, help=GPIO pins connected to, Microstep Resolution pins MS1-MS3 of IC. Pass in (-1, -1, -1) for hardwired MS-X connection

I do not understand that entry and passing (-1,-1,-1) does not change the behaviour.

Would you have an idea what the problem can be?

The DC motor slows down after some time for forward and backward movement

The driver is moving the motors but if the movement is continued the motor slows down and stops for a while (1 sec). The first time it happens just after around 5 secs of running the motor. Then the motor starts running again and it happens repeatedly after about a minute. My guess is there is some sleep time set in the library.
Related code I am using in raspberry pi 3B is

import time
import RPi.GPIO as GPIO
import pigpio
#from RpiMotorLib import RpiMotorLib
from time import sleep
from RpiMotorLib import rpi_pservo_lib
from RpiMotorLib import rpi_dc_lib
MotorOne = rpi_dc_lib.L298NMDc(6 ,12 ,13 ,30 ,False, "motor_one")
MotorTwo = rpi_dc_lib.L298NMDc(26 ,20 ,21 ,30 ,False, "motor_two")

    MotorOne.forward(30)
    MotorTwo.forward(30)

Enable / disable stepping motor A4988

On A4988 one can see pins: direction, step and enable. Direction and step are controlled by A4988Nema(direction, step, …), but one can alse enable and disable motor sending 0 or 1 to «enable»-pin. For example, if one send GPIO.output(enable-pin, 1) motor goes into free mode, where one can manually change the position of the motor (motor_go doesn't rotate motor), and when one send GPIO.output(enable-pin, 0) motor goes into locked-position mode, where one can't manually change the position of the motor and only motor_go can rotate motor.

May be it would be convenient to have such an interface in the class with the ability to control the motor mode?

Write lib for MicroPython

Hi I just found this repository, cause I want to make with the Raspberry Pi Pico an RTC with a analog clock out of gears and a stepper motor.
Is there a way from your side to make a fork for MicroPython?

Turn -90°

Hello I´m using this library to controll a 28BYJ stepper motor. I want to be able to enter an angle and then the motor turn to that position. I wrote the following code:

from RpiMotorLib import RpiMotorLib
import RPi.GPIO as GPIO

GpioPins = [21,20,16,12]

val = int(input("val:"))
stepsByAngle = round((val*0.71111111)*2)


mymotortest = RpiMotorLib.BYJMotor("MyMotorOne", "28BYJ")

if stepsByAngle > 0:
        mymotortest.motor_run(GpioPins , .002, stepsByAngle, False, True, "half", .05)
        print("positive")
elif stepsByAngle < 0:
        mymotortest.motor_run(GpioPins , .002, stepsByAngle, True, True, "half", .05)
        print("negative")

GPIO.cleanup()

It´s working properly. I enter an angle and the code translates it into steps and the motor turn exactly 90°. I want also that the motor can turn -90° but that doesn´t work. What´s the problem?

two steppers simultaneously example issue

Hello,

I was trying to use RpiMotorLib to manage 2 steppers at the same time (with different motions) but failed to do so.
I mean it work, but both steppers are playing one after the other, not simultaneously.

Config ; raspi4 > 2X easy drivers > 2X Nema 17.

here's the script i'm using based on your example.
Am i missing something ?

Regards

`

"""
import time
import concurrent.futures
import RPi.GPIO as GPIO

from RpiMotorLib import RpiMotorLib

GPIO_pins_1 = (-1,-1)
direction_1 = 24 # Direction -> GPIO Pin
step_1 = 23 # Step -> GPIO Pin

GPIO_pins_2 = (-1,-1)
direction_2 = 8 # Direction -> GPIO Pin
step_2 = 25 # Step -> GPIO Pin

mymotortestOne = RpiMotorLib.A3967EasyNema(direction_1, step_1, GPIO_pins_1)
mymotortestTwo = RpiMotorLib.A3967EasyNema(direction_2, step_2, GPIO_pins_2)

def main():
with concurrent.futures.ThreadPoolExecutor() as executor:
f1 = executor.submit(mymotortestOne.motor_move(.0003, 16002 , False, True, "Full", .05))
f2 = executor.submit(mymotortestTwo.motor_move(.0003
2.0, 1600 , False, True, "Full", .05))

if name == 'main':
print("START")
main()
GPIO.cleanup()
print("END")
exit()

`

Motor stop button don't work

When I comment the Stop button in like in the demo for A4988 then the motor don't stop. Only the following error on console:
Test file: Stopping motor Traceback (most recent call last): File "/usr/src/RpiMotorLib-3.2/test/./A4988_Nema_Test.py", line 90, in button_callback mymotortest.motor_stop() NameError: name 'mymotortest' is not defined

What have I made wrong?

Thanks

i used 103H5210-0457 dvr8825 but strange...

hello i try this example
import RPi.GPIO as GPIO

import the library

from RpiMotorLib import RpiMotorLib

#define GPIO pins
GPIO_pins = (14, 15, 18) # Microstep Resolution MS1-MS3 -> GPIO Pin
direction= 20 # Direction -> GPIO Pin
step = 21 # Step -> GPIO Pin

Declare a instance of class pass GPIO pins numbers and the motor type

mymotortest = RpiMotorLib.A4988Nema(direction, step, GPIO_pins, "DRV8825")

call the function, pass the arguments

mymotortest.motor_go(False, "Full" , 100, .01, False, .05)

good practise to cleanup GPIO at some point before exit

GPIO.cleanup()

and my motor 103H5210-0457 this is just use 24v ... so i just use and adjust current power...

but
It's trembling, it's vibrating ... And it's feverish ... Can you tell me how to match it in my code?

Unable to use "mode_pins"

I have no idea what mode_pins are supposed to stand for in the code so I don't know what to hook it up to. I'm using A4988 motor type

User Keyboard Interrupt : RpiMotorLib:

When I stop the execution of my script with Ctrl+C, occasionally I get this message:

User Keyboard Interrupt : RpiMotorLib:

But the code keeps running. I have to press a couple more times to stop it.

What's weird is that the behavior is not consistent, sometimes I won't get the message and the script stops. My guess is that when the motor is running I can't stop the script.

Issues with 28BYJ-48 motor

I'm trying to run the example documentation but I keep getting a syntax error on line 281:

  File "libstepper.py", line 2, in <module>
    from RpiMotorLib import RpiMotorLib
  File "/usr/local/lib/python2.7/dist-packages/RpiMotorLib/RpiMotorLib.py", line 281
    print("Steps count {}".format(i+1), end="\r", flush=True)
                                           ^
SyntaxError: invalid syntax

Any help would be appreciated!

L298n Motor problem

Good day to you

Trying to run the following code in Python

from time import sleep
from RpiMotorLib.rpi_dc_lib import L298NMDc

left_motor = L298NMDc(6, 13, 18, name="left")
left_motor.forward()
sleep(5)
left_motor.stop()
left_motor.cleanup(True)

# Change pin order
left_motor = L298NMDc(13, 6, 18, name="left")
left_motor.forward()
sleep(5)
left_motor.stop()
left_motor.cleanup(True)

It returns the following

pi@raspberrypi:~ $ python3 motorl.py
Traceback (most recent call last):
File "motorl.py", line 11, in
left_motor = L298NMDc(13, 6, 18, name="left")
File "/usr/local/lib/python3.7/dist-packages/RpiMotorLib/rpi_dc_lib.py", line 58, in init
self.my_pwm = GPIO.PWM(self.pwm_pin, self.freq)
RuntimeError: A PWM object already exists for this GPIO channel
pi@raspberrypi:~ $

Could you please assist

Thank you sincerely

Anton

Unable to install on Windows.

Description

Unable to install the library on a windows 10 computer (bad workaround suggested).

System

OS: Windows 10
Python: 3.7.1
pip: 18.1
pipenv: 2018.11.26
setuptools: 40.6.3

Details:

While trying to install the library on a windows machine (I wish to develop on windows and deploy to a raspberry pi) The installation repeatedly failed.

Error

The error seemed to be happening in distutils.

  File "setup.py", line 22, in <module>
    "License :: OSI Approved :: GNU General Public License v3 (GPLv3)",
  File "C:\Program Files (x86)\Python37-32\lib\site-packages\setuptools\__init__.py", line 143, in setup
    return distutils.core.setup(**attrs)
  File "C:\Program Files (x86)\Python37-32\lib\distutils\core.py", line 148, in setup
    dist.run_commands()
  File "C:\Program Files (x86)\Python37-32\lib\distutils\dist.py", line 966, in run_commands
    self.run_command(cmd)
  File "C:\Program Files (x86)\Python37-32\lib\distutils\dist.py", line 985, in run_command
    cmd_obj.run()
  File "C:\Program Files (x86)\Python37-32\lib\site-packages\setuptools\command\install.py", line 67, in run
    self.do_egg_install()
  File "C:\Program Files (x86)\Python37-32\lib\site-packages\setuptools\command\install.py", line 109, in do_egg_install
    self.run_command('bdist_egg')
  File "C:\Program Files (x86)\Python37-32\lib\distutils\cmd.py", line 313, in run_command
    self.distribution.run_command(command)
  File "C:\Program Files (x86)\Python37-32\lib\distutils\dist.py", line 985, in run_command
    cmd_obj.run()
  File "C:\Program Files (x86)\Python37-32\lib\site-packages\setuptools\command\bdist_egg.py", line 192, in run
    self.do_install_data()
  File "C:\Program Files (x86)\Python37-32\lib\site-packages\setuptools\command\bdist_egg.py", line 144, in do_install_data
    self.call_command('install_data', force=0, root=None)
  File "C:\Program Files (x86)\Python37-32\lib\site-packages\setuptools\command\bdist_egg.py", line 158, in call_command
    self.run_command(cmdname)
  File "C:\Program Files (x86)\Python37-32\lib\distutils\cmd.py", line 313, in run_command
    self.distribution.run_command(command)
  File "C:\Program Files (x86)\Python37-32\lib\distutils\dist.py", line 985, in run_command
    cmd_obj.run()
  File "C:\Program Files (x86)\Python37-32\lib\distutils\command\install_data.py", line 56, in run
    dir = convert_path(f[0])
  File "C:\Program Files (x86)\Python37-32\lib\distutils\util.py", line 110, in convert_path
    raise ValueError("path '%s' cannot be absolute" % pathname)
ValueError: path '/usr/share/doc/rpimotorlib/' cannot be absolute

specifically ValueError: path '/usr/share/doc/rpimotorlib/' cannot be absolute

Attempted installation methods (failed)

pipenv install rpimotorlib
pip install rpimotorlib

pip3 install rpimotorlib

downloading the library manually and from it's root:
python setup.py install

Attempted solutions (failed)

update pip, pipenv, setuptools
run setup.py with -W ignore::UserWarning:distutils.dist Suggested Here

Workaround

Finally I've downloaded the library manually and changed line 16 on setup.py from
data_files=[('/usr/share/doc/rpimotorlib/', ['README.md'])],
to a absolute path in windows path format.
I'm not sure if this have completely solved my problem or if it just pushed it further down the line.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.