add motor class

This commit is contained in:
Konstantin Lampalzer 2021-01-15 18:22:50 +01:00
parent 5942c13721
commit b5a60438c0
No known key found for this signature in database
GPG key ID: 9A60A522835A2AD9
16 changed files with 438 additions and 100 deletions

254
.gitignore vendored Normal file
View file

@ -0,0 +1,254 @@
# Created by https://www.toptal.com/developers/gitignore/api/python,pycharm,code
# Edit at https://www.toptal.com/developers/gitignore?templates=python,pycharm,code
### Code ###
.vscode/*
!.vscode/tasks.json
!.vscode/launch.json
*.code-workspace
### PyCharm ###
# Covers JetBrains IDEs: IntelliJ, RubyMine, PhpStorm, AppCode, PyCharm, CLion, Android Studio, WebStorm and Rider
# Reference: https://intellij-support.jetbrains.com/hc/en-us/articles/206544839
# User-specific stuff
.idea/**/workspace.xml
.idea/**/tasks.xml
.idea/**/usage.statistics.xml
.idea/**/dictionaries
.idea/**/shelf
# Generated files
.idea/**/contentModel.xml
# Sensitive or high-churn files
.idea/**/dataSources/
.idea/**/dataSources.ids
.idea/**/dataSources.local.xml
.idea/**/sqlDataSources.xml
.idea/**/dynamic.xml
.idea/**/uiDesigner.xml
.idea/**/dbnavigator.xml
# Gradle
.idea/**/gradle.xml
.idea/**/libraries
# Gradle and Maven with auto-import
# When using Gradle or Maven with auto-import, you should exclude module files,
# since they will be recreated, and may cause churn. Uncomment if using
# auto-import.
# .idea/artifacts
# .idea/compiler.xml
# .idea/jarRepositories.xml
# .idea/modules.xml
# .idea/*.iml
# .idea/modules
# *.iml
# *.ipr
# CMake
cmake-build-*/
# Mongo Explorer plugin
.idea/**/mongoSettings.xml
# File-based project format
*.iws
# IntelliJ
out/
# mpeltonen/sbt-idea plugin
.idea_modules/
# JIRA plugin
atlassian-ide-plugin.xml
# Cursive Clojure plugin
.idea/replstate.xml
# Crashlytics plugin (for Android Studio and IntelliJ)
com_crashlytics_export_strings.xml
crashlytics.properties
crashlytics-build.properties
fabric.properties
# Editor-based Rest Client
.idea/httpRequests
# Android studio 3.1+ serialized cache file
.idea/caches/build_file_checksums.ser
### PyCharm Patch ###
# Comment Reason: https://github.com/joeblau/gitignore.io/issues/186#issuecomment-215987721
# *.iml
# modules.xml
# .idea/misc.xml
# *.ipr
# Sonarlint plugin
# https://plugins.jetbrains.com/plugin/7973-sonarlint
.idea/**/sonarlint/
# SonarQube Plugin
# https://plugins.jetbrains.com/plugin/7238-sonarqube-community-plugin
.idea/**/sonarIssues.xml
# Markdown Navigator plugin
# https://plugins.jetbrains.com/plugin/7896-markdown-navigator-enhanced
.idea/**/markdown-navigator.xml
.idea/**/markdown-navigator-enh.xml
.idea/**/markdown-navigator/
# Cache file creation bug
# See https://youtrack.jetbrains.com/issue/JBR-2257
.idea/$CACHE_FILE$
# CodeStream plugin
# https://plugins.jetbrains.com/plugin/12206-codestream
.idea/codestream.xml
### Python ###
# Byte-compiled / optimized / DLL files
__pycache__/
*.py[cod]
*$py.class
# C extensions
*.so
# Distribution / packaging
.Python
build/
develop-eggs/
dist/
downloads/
eggs/
.eggs/
lib/
lib64/
parts/
sdist/
var/
wheels/
pip-wheel-metadata/
share/python-wheels/
*.egg-info/
.installed.cfg
*.egg
MANIFEST
# PyInstaller
# Usually these files are written by a python script from a template
# before PyInstaller builds the exe, so as to inject date/other infos into it.
*.manifest
*.spec
# Installer logs
pip-log.txt
pip-delete-this-directory.txt
# Unit test / coverage reports
htmlcov/
.tox/
.nox/
.coverage
.coverage.*
.cache
nosetests.xml
coverage.xml
*.cover
*.py,cover
.hypothesis/
.pytest_cache/
pytestdebug.log
# Translations
*.mo
*.pot
# Django stuff:
*.log
local_settings.py
db.sqlite3
db.sqlite3-journal
# Flask stuff:
instance/
.webassets-cache
# Scrapy stuff:
.scrapy
# Sphinx documentation
docs/_build/
doc/_build/
# PyBuilder
target/
# Jupyter Notebook
.ipynb_checkpoints
# IPython
profile_default/
ipython_config.py
# pyenv
.python-version
# pipenv
# According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control.
# However, in case of collaboration, if having platform-specific dependencies or dependencies
# having no cross-platform support, pipenv may install dependencies that don't work, or not
# install all needed dependencies.
#Pipfile.lock
# PEP 582; used by e.g. github.com/David-OConnor/pyflow
__pypackages__/
# Celery stuff
celerybeat-schedule
celerybeat.pid
# SageMath parsed files
*.sage.py
# Environments
.env
.venv
env/
venv/
ENV/
env.bak/
venv.bak/
pythonenv*
# Spyder project settings
.spyderproject
.spyproject
# Rope project settings
.ropeproject
# mkdocs documentation
/site
# mypy
.mypy_cache/
.dmypy.json
dmypy.json
# Pyre type checker
.pyre/
# pytype static type analyzer
.pytype/
# profiling data
.prof
# End of https://www.toptal.com/developers/gitignore/api/python,pycharm,code

3
.idea/.gitignore generated vendored Normal file
View file

@ -0,0 +1,3 @@
# Default ignored files
/shelf/
/workspace.xml

11
.idea/compLIB.iml generated Normal file
View file

@ -0,0 +1,11 @@
<?xml version="1.0" encoding="UTF-8"?>
<module type="PYTHON_MODULE" version="4">
<component name="NewModuleRootManager">
<content url="file://$MODULE_DIR$">
<sourceFolder url="file://$MODULE_DIR$/compLIB" isTestSource="false" />
<excludeFolder url="file://$MODULE_DIR$/venv" />
</content>
<orderEntry type="jdk" jdkName="Python 3.8 (compLIB)" jdkType="Python SDK" />
<orderEntry type="sourceFolder" forTests="false" />
</component>
</module>

View file

@ -0,0 +1,6 @@
<component name="InspectionProjectProfileManager">
<settings>
<option name="USE_PROJECT_PROFILE" value="false" />
<version value="1.0" />
</settings>
</component>

4
.idea/misc.xml generated Normal file
View file

@ -0,0 +1,4 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ProjectRootManager" version="2" project-jdk-name="Python 3.8 (compLIB)" project-jdk-type="Python SDK" />
</project>

8
.idea/modules.xml generated Normal file
View file

@ -0,0 +1,8 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ProjectModuleManager">
<modules>
<module fileurl="file://$PROJECT_DIR$/.idea/compLIB.iml" filepath="$PROJECT_DIR$/.idea/compLIB.iml" />
</modules>
</component>
</project>

6
.idea/vcs.xml generated Normal file
View file

@ -0,0 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="VcsDirectoryMappings">
<mapping directory="$PROJECT_DIR$" vcs="Git" />
</component>
</project>

View file

@ -1,107 +1,35 @@
import time
from PCA9685 import PCA9685
from compLIB import *
class Motor:
def __init__(self):
self.pwm = PCA9685(0x40, debug=True)
self.pwm.setPWMFreq(50)
class Motor(object):
def duty_range(self, duty1, duty2, duty3, duty4):
if duty1 > 4095:
duty1 = 4095
elif duty1 < -4095:
duty1 = -4095
@staticmethod
def power(port: int, percent: int):
"""
Set specified motor to percentage power
:param port: Port, which the motor is connected to. 0-3, 0 -> top left, 3 -> top right
:param percent: Percentage of max speed. between -100 and 100
:return: None
"""
forward = True
if percent < 0:
percent = abs(percent)
forward = False
if duty2 > 4095:
duty2 = 4095
elif duty2 < -4095:
duty2 = -4095
adjusted_speed = min(max(0, percent), 100) * MOTOR_PERCENTAGE_MULT
if duty3 > 4095:
duty3 = 4095
elif duty3 < -4095:
duty3 = -4095
if duty4 > 4095:
duty4 = 4095
elif duty4 < -4095:
duty4 = -4095
return duty1, duty2, duty3, duty4
def left_Upper_Wheel(self, duty):
if duty > 0:
self.pwm.setMotorPwm(0, 0)
self.pwm.setMotorPwm(1, duty)
elif duty < 0:
self.pwm.setMotorPwm(1, 0)
self.pwm.setMotorPwm(0, abs(duty))
if forward:
pwm.setMotorPwm(port * 2, 0)
pwm.setMotorPwm(port * 2 + 1, adjusted_speed)
else:
self.pwm.setMotorPwm(0, 4095)
self.pwm.setMotorPwm(1, 4095)
pwm.setMotorPwm(port * 2, adjusted_speed)
pwm.setMotorPwm(port * 2 + 1, 0)
def left_Lower_Wheel(self, duty):
if duty > 0:
self.pwm.setMotorPwm(3, 0)
self.pwm.setMotorPwm(2, duty)
elif duty < 0:
self.pwm.setMotorPwm(2, 0)
self.pwm.setMotorPwm(3, abs(duty))
else:
self.pwm.setMotorPwm(2, 4095)
self.pwm.setMotorPwm(3, 4095)
def right_Upper_Wheel(self, duty):
if duty > 0:
self.pwm.setMotorPwm(6, 0)
self.pwm.setMotorPwm(7, duty)
elif duty < 0:
self.pwm.setMotorPwm(7, 0)
self.pwm.setMotorPwm(6, abs(duty))
else:
self.pwm.setMotorPwm(6, 4095)
self.pwm.setMotorPwm(7, 4095)
def right_Lower_Wheel(self, duty):
if duty > 0:
self.pwm.setMotorPwm(4, 0)
self.pwm.setMotorPwm(5, duty)
elif duty < 0:
self.pwm.setMotorPwm(5, 0)
self.pwm.setMotorPwm(4, abs(duty))
else:
self.pwm.setMotorPwm(4, 4095)
self.pwm.setMotorPwm(5, 4095)
def setMotorModel(self, duty1, duty2, duty3, duty4):
duty1, duty2, duty3, duty4 = self.duty_range(duty1, duty2, duty3, duty4)
self.left_Upper_Wheel(duty1)
self.left_Lower_Wheel(duty2)
self.right_Upper_Wheel(duty3)
self.right_Lower_Wheel(duty4)
PWM = Motor()
def loop():
PWM.setMotorModel(2000, 2000, 2000, 2000) # Forward
time.sleep(3)
PWM.setMotorModel(-2000, -2000, -2000, -2000) # Back
time.sleep(3)
PWM.setMotorModel(-500, -500, 2000, 2000) # Left
time.sleep(3)
PWM.setMotorModel(2000, 2000, -500, -500) # Right
time.sleep(3)
PWM.setMotorModel(0, 0, 0, 0) # Stop
def destroy():
PWM.setMotorModel(0, 0, 0, 0)
if __name__ == '__main__':
try:
loop()
except KeyboardInterrupt: # When 'Ctrl+C' is pressed, the child program destroy() will be executed.
destroy()
@staticmethod
def all_off():
"""
Turns of all motors
:return:
"""
for i in range(0, MOTOR_COUNT):
Motor.power(i, 0)

View file

@ -1 +1,8 @@
__version__ = "0.0.1"
pwm = PCA9685(0x40, debug=True)
pwm.setPWMFreq(50)
MOTOR_COUNT = 4
MAX_MOTOR_SPEED = 4095.0
MOTOR_PERCENTAGE_MULT = MAX_MOTOR_SPEED / 100.0

107
oldLib/Motor.py Normal file
View file

@ -0,0 +1,107 @@
import time
from PCA9685 import PCA9685
class Motor:
def __init__(self):
self.pwm = PCA9685(0x40, debug=True)
self.pwm.setPWMFreq(50)
def duty_range(self, duty1, duty2, duty3, duty4):
if duty1 > 4095:
duty1 = 4095
elif duty1 < -4095:
duty1 = -4095
if duty2 > 4095:
duty2 = 4095
elif duty2 < -4095:
duty2 = -4095
if duty3 > 4095:
duty3 = 4095
elif duty3 < -4095:
duty3 = -4095
if duty4 > 4095:
duty4 = 4095
elif duty4 < -4095:
duty4 = -4095
return duty1, duty2, duty3, duty4
def left_Upper_Wheel(self, duty):
if duty > 0:
self.pwm.setMotorPwm(0, 0)
self.pwm.setMotorPwm(1, duty)
elif duty < 0:
self.pwm.setMotorPwm(1, 0)
self.pwm.setMotorPwm(0, abs(duty))
else:
self.pwm.setMotorPwm(0, 4095)
self.pwm.setMotorPwm(1, 4095)
def left_Lower_Wheel(self, duty):
if duty > 0:
self.pwm.setMotorPwm(3, 0)
self.pwm.setMotorPwm(2, duty)
elif duty < 0:
self.pwm.setMotorPwm(2, 0)
self.pwm.setMotorPwm(3, abs(duty))
else:
self.pwm.setMotorPwm(2, 4095)
self.pwm.setMotorPwm(3, 4095)
def right_Upper_Wheel(self, duty):
if duty > 0:
self.pwm.setMotorPwm(6, 0)
self.pwm.setMotorPwm(7, duty)
elif duty < 0:
self.pwm.setMotorPwm(7, 0)
self.pwm.setMotorPwm(6, abs(duty))
else:
self.pwm.setMotorPwm(6, 4095)
self.pwm.setMotorPwm(7, 4095)
def right_Lower_Wheel(self, duty):
if duty > 0:
self.pwm.setMotorPwm(4, 0)
self.pwm.setMotorPwm(5, duty)
elif duty < 0:
self.pwm.setMotorPwm(5, 0)
self.pwm.setMotorPwm(4, abs(duty))
else:
self.pwm.setMotorPwm(4, 4095)
self.pwm.setMotorPwm(5, 4095)
def setMotorModel(self, duty1, duty2, duty3, duty4):
duty1, duty2, duty3, duty4 = self.duty_range(duty1, duty2, duty3, duty4)
self.left_Upper_Wheel(duty1)
self.left_Lower_Wheel(duty2)
self.right_Upper_Wheel(duty3)
self.right_Lower_Wheel(duty4)
PWM = Motor()
def loop():
PWM.setMotorModel(2000, 2000, 2000, 2000) # Forward
time.sleep(3)
PWM.setMotorModel(-2000, -2000, -2000, -2000) # Back
time.sleep(3)
PWM.setMotorModel(-500, -500, 2000, 2000) # Left
time.sleep(3)
PWM.setMotorModel(2000, 2000, -500, -500) # Right
time.sleep(3)
PWM.setMotorModel(0, 0, 0, 0) # Stop
def destroy():
PWM.setMotorModel(0, 0, 0, 0)
if __name__ == '__main__':
try:
loop()
except KeyboardInterrupt: # When 'Ctrl+C' is pressed, the child program destroy() will be executed.
destroy()

4
oldLib/__init__.py Normal file
View file

@ -0,0 +1,4 @@
__version__ = "0.0.1"
pwm = PCA9685(0x40, debug=True)
pwm.setPWMFreq(50)