diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..8649d80 --- /dev/null +++ b/.gitignore @@ -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 \ No newline at end of file diff --git a/.idea/.gitignore b/.idea/.gitignore new file mode 100644 index 0000000..26d3352 --- /dev/null +++ b/.idea/.gitignore @@ -0,0 +1,3 @@ +# Default ignored files +/shelf/ +/workspace.xml diff --git a/.idea/compLIB.iml b/.idea/compLIB.iml new file mode 100644 index 0000000..e5f3420 --- /dev/null +++ b/.idea/compLIB.iml @@ -0,0 +1,11 @@ + + + + + + + + + + + \ No newline at end of file diff --git a/.idea/inspectionProfiles/profiles_settings.xml b/.idea/inspectionProfiles/profiles_settings.xml new file mode 100644 index 0000000..105ce2d --- /dev/null +++ b/.idea/inspectionProfiles/profiles_settings.xml @@ -0,0 +1,6 @@ + + + + \ No newline at end of file diff --git a/.idea/misc.xml b/.idea/misc.xml new file mode 100644 index 0000000..1dae23d --- /dev/null +++ b/.idea/misc.xml @@ -0,0 +1,4 @@ + + + + \ No newline at end of file diff --git a/.idea/modules.xml b/.idea/modules.xml new file mode 100644 index 0000000..bfe6efe --- /dev/null +++ b/.idea/modules.xml @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git a/.idea/vcs.xml b/.idea/vcs.xml new file mode 100644 index 0000000..94a25f7 --- /dev/null +++ b/.idea/vcs.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/compLIB/Motor.py b/compLIB/Motor.py index 634c06a..ecd1d83 100644 --- a/compLIB/Motor.py +++ b/compLIB/Motor.py @@ -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) diff --git a/compLIB/__init__.py b/compLIB/__init__.py index f102a9c..9243134 100644 --- a/compLIB/__init__.py +++ b/compLIB/__init__.py @@ -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 \ No newline at end of file diff --git a/compLIB/ADC.py b/oldLib/ADC.py similarity index 100% rename from compLIB/ADC.py rename to oldLib/ADC.py diff --git a/compLIB/Buzzer.py b/oldLib/Buzzer.py similarity index 100% rename from compLIB/Buzzer.py rename to oldLib/Buzzer.py diff --git a/compLIB/Command.py b/oldLib/Command.py similarity index 100% rename from compLIB/Command.py rename to oldLib/Command.py diff --git a/oldLib/Motor.py b/oldLib/Motor.py new file mode 100644 index 0000000..634c06a --- /dev/null +++ b/oldLib/Motor.py @@ -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() diff --git a/compLIB/PCA9685.py b/oldLib/PCA9685.py similarity index 100% rename from compLIB/PCA9685.py rename to oldLib/PCA9685.py diff --git a/compLIB/Ultrasonic.py b/oldLib/Ultrasonic.py similarity index 100% rename from compLIB/Ultrasonic.py rename to oldLib/Ultrasonic.py diff --git a/oldLib/__init__.py b/oldLib/__init__.py new file mode 100644 index 0000000..f30a6a0 --- /dev/null +++ b/oldLib/__init__.py @@ -0,0 +1,4 @@ +__version__ = "0.0.1" + +pwm = PCA9685(0x40, debug=True) +pwm.setPWMFreq(50)