From 2abb2a6c020252e542609901f5f64cc6c517ca7d Mon Sep 17 00:00:00 2001 From: EchoFUN Date: Sun, 8 Jan 2017 19:24:53 +0800 Subject: [PATCH] Init Commit. --- .gitignore | 89 ++++++++++++++++++++ .vscode/launch.json | 168 +++++++++++++++++++++++++++++++++++++ .vscode/settings.json | 4 + LICENSE | 21 +++++ README.md | 2 + app.py | 57 +++++++++++++ conf.py | 39 +++++++++ directives/__init__.py | 17 ++++ directives/camera_down.py | 52 ++++++++++++ directives/camera_left.py | 54 ++++++++++++ directives/camera_pause.py | 30 +++++++ directives/camera_right.py | 52 ++++++++++++ directives/camera_up.py | 54 ++++++++++++ directives/car_back.py | 26 ++++++ directives/car_forward.py | 26 ++++++ directives/car_left.py | 26 ++++++ directives/car_pause.py | 4 + directives/car_right.py | 26 ++++++ requirements.txt | 2 + tests/camera_pwm.py | 49 +++++++++++ tests/camera_pwm_1.py | 53 ++++++++++++ tests/diode.py | 44 ++++++++++ tests/diode_1.py | 41 +++++++++ tests/diode_2.py | 40 +++++++++ tests/diode_3.py | 44 ++++++++++ tests/echo.py | 44 ++++++++++ tests/echo_1.py | 37 ++++++++ tests/echo_2.py | 47 +++++++++++ tests/fan.py | 23 +++++ tests/python.py | 9 ++ tests/run.py | 34 ++++++++ tests/run_1.py | 36 ++++++++ tests/run_2.py | 34 ++++++++ tests/run_3.py | 34 ++++++++ 34 files changed, 1318 insertions(+) create mode 100644 .gitignore create mode 100644 .vscode/launch.json create mode 100644 .vscode/settings.json create mode 100644 LICENSE create mode 100644 README.md create mode 100755 app.py create mode 100644 conf.py create mode 100644 directives/__init__.py create mode 100644 directives/camera_down.py create mode 100644 directives/camera_left.py create mode 100644 directives/camera_pause.py create mode 100644 directives/camera_right.py create mode 100644 directives/camera_up.py create mode 100644 directives/car_back.py create mode 100644 directives/car_forward.py create mode 100644 directives/car_left.py create mode 100644 directives/car_pause.py create mode 100644 directives/car_right.py create mode 100644 requirements.txt create mode 100644 tests/camera_pwm.py create mode 100644 tests/camera_pwm_1.py create mode 100644 tests/diode.py create mode 100644 tests/diode_1.py create mode 100644 tests/diode_2.py create mode 100644 tests/diode_3.py create mode 100644 tests/echo.py create mode 100644 tests/echo_1.py create mode 100644 tests/echo_2.py create mode 100644 tests/fan.py create mode 100644 tests/python.py create mode 100644 tests/run.py create mode 100644 tests/run_1.py create mode 100644 tests/run_2.py create mode 100644 tests/run_3.py diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..a0ad585 --- /dev/null +++ b/.gitignore @@ -0,0 +1,89 @@ +# Byte-compiled / optimized / DLL files +__pycache__/ +*.py[cod] +*$py.class + +# C extensions +*.so + +# Distribution / packaging +.Python +env/ +build/ +develop-eggs/ +dist/ +downloads/ +eggs/ +.eggs/ +lib/ +lib64/ +parts/ +sdist/ +var/ +*.egg-info/ +.installed.cfg +*.egg + +# 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/ +.coverage +.coverage.* +.cache +nosetests.xml +coverage.xml +*,cover +.hypothesis/ + +# Translations +*.mo +*.pot + +# Django stuff: +*.log +local_settings.py + +# Flask stuff: +instance/ +.webassets-cache + +# Scrapy stuff: +.scrapy + +# Sphinx documentation +docs/_build/ + +# PyBuilder +target/ + +# IPython Notebook +.ipynb_checkpoints + +# pyenv +.python-version + +# celery beat schedule file +celerybeat-schedule + +# dotenv +.env + +# virtualenv +venv/ +ENV/ + +# Spyder project settings +.spyderproject + +# Rope project settings +.ropeproject \ No newline at end of file diff --git a/.vscode/launch.json b/.vscode/launch.json new file mode 100644 index 0000000..980c165 --- /dev/null +++ b/.vscode/launch.json @@ -0,0 +1,168 @@ +{ + "version": "0.2.0", + "configurations": [ + { + "name": "Python", + "type": "python", + "request": "launch", + "stopOnEntry": true, + "pythonPath": "${config.python.pythonPath}", + "program": "${file}", + "cwd": "${workspaceRoot}", + "debugOptions": [ + "WaitOnAbnormalExit", + "WaitOnNormalExit", + "RedirectOutput" + ] + }, + { + "name": "PySpark", + "type": "python", + "request": "launch", + "stopOnEntry": true, + "osx": { + "pythonPath": "${env.SPARK_HOME}/bin/spark-submit" + }, + "windows": { + "pythonPath": "${env.SPARK_HOME}/bin/spark-submit.cmd" + }, + "linux": { + "pythonPath": "${env.SPARK_HOME}/bin/spark-submit" + }, + "program": "${file}", + "cwd": "${workspaceRoot}", + "debugOptions": [ + "WaitOnAbnormalExit", + "WaitOnNormalExit", + "RedirectOutput" + ] + }, + { + "name": "Python Module", + "type": "python", + "request": "launch", + "stopOnEntry": true, + "pythonPath": "${config.python.pythonPath}", + "module": "module.name", + "cwd": "${workspaceRoot}", + "debugOptions": [ + "WaitOnAbnormalExit", + "WaitOnNormalExit", + "RedirectOutput" + ] + }, + { + "name": "Integrated Terminal/Console", + "type": "python", + "request": "launch", + "stopOnEntry": true, + "pythonPath": "${config.python.pythonPath}", + "program": "${file}", + "cwd": "null", + "console": "integratedTerminal", + "debugOptions": [ + "WaitOnAbnormalExit", + "WaitOnNormalExit" + ] + }, + { + "name": "External Terminal/Console", + "type": "python", + "request": "launch", + "stopOnEntry": true, + "pythonPath": "${config.python.pythonPath}", + "program": "${file}", + "cwd": "null", + "console": "externalTerminal", + "debugOptions": [ + "WaitOnAbnormalExit", + "WaitOnNormalExit" + ] + }, + { + "name": "Django", + "type": "python", + "request": "launch", + "stopOnEntry": true, + "pythonPath": "${config.python.pythonPath}", + "program": "${workspaceRoot}/manage.py", + "cwd": "${workspaceRoot}", + "args": [ + "runserver", + "--noreload" + ], + "debugOptions": [ + "WaitOnAbnormalExit", + "WaitOnNormalExit", + "RedirectOutput", + "DjangoDebugging" + ] + }, + { + "name": "Flask", + "type": "python", + "request": "launch", + "stopOnEntry": false, + "pythonPath": "${config.python.pythonPath}", + "program": "fully qualified path fo 'flask' executable. Generally located along with python interpreter", + "cwd": "${workspaceRoot}", + "env": { + "FLASK_APP": "${workspaceRoot}/quickstart/app.py" + }, + "args": [ + "run", + "--no-debugger", + "--no-reload" + ], + "debugOptions": [ + "WaitOnAbnormalExit", + "WaitOnNormalExit", + "RedirectOutput" + ] + }, + { + "name": "Flask (old)", + "type": "python", + "request": "launch", + "stopOnEntry": false, + "pythonPath": "${config.python.pythonPath}", + "program": "${workspaceRoot}/run.py", + "cwd": "${workspaceRoot}", + "args": [], + "debugOptions": [ + "WaitOnAbnormalExit", + "WaitOnNormalExit", + "RedirectOutput" + ] + }, + { + "name": "Watson", + "type": "python", + "request": "launch", + "stopOnEntry": true, + "pythonPath": "${config.python.pythonPath}", + "program": "${workspaceRoot}/console.py", + "cwd": "${workspaceRoot}", + "args": [ + "dev", + "runserver", + "--noreload=True" + ], + "debugOptions": [ + "WaitOnAbnormalExit", + "WaitOnNormalExit", + "RedirectOutput" + ] + }, + { + "name": "Attach (Remote Debug)", + "type": "python", + "request": "attach", + "localRoot": "${workspaceRoot}", + "remoteRoot": "${workspaceRoot}", + "port": 3000, + "secret": "my_secret", + "host": "localhost" + } + ] +} \ No newline at end of file diff --git a/.vscode/settings.json b/.vscode/settings.json new file mode 100644 index 0000000..b51bf5f --- /dev/null +++ b/.vscode/settings.json @@ -0,0 +1,4 @@ +{ + "python.linting.pylintEnabled": false, + "python.formatting.provider": "yapf" +} \ No newline at end of file diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..4479463 --- /dev/null +++ b/LICENSE @@ -0,0 +1,21 @@ +MIT License + +Copyright (c) 2016 穿鞋子的电脑 + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. diff --git a/README.md b/README.md new file mode 100644 index 0000000..50a3e89 --- /dev/null +++ b/README.md @@ -0,0 +1,2 @@ +# RaspberryPi +Programming upon it. diff --git a/app.py b/app.py new file mode 100755 index 0000000..d079504 --- /dev/null +++ b/app.py @@ -0,0 +1,57 @@ +#!/usr/bin/env python +# -*- coding: utf-8 -* + +# +# @author XU Kai(xukai.ken@gmail.com) +# @date 2016-12-04 星期日 +# +# +# +# +# +# +# + +from conf import SERVER + +try: + import RPi.GPIO as GPIO + import atexit + + atexit.register(GPIO.cleanup) +except ImportError: + print('Not the Pi env !') + +import directives +import logging +logging.getLogger('socketIO-client').setLevel(logging.DEBUG) +logging.basicConfig() + +import os, socketIO_client +from socketIO_client import SocketIO, LoggingNamespace + + +def receiveDirectives(args): + directive = args['D'] + if args.has_key('S'): + signal = args['S'] + else: + signal = '' + + global GPIO # Fuck the Global vars cost me severl hours ! :() + try: + GPIO + except NameError: + GPIO = {} + + try: + getattr(__import__('directives.' + directive), directive).run(GPIO, signal) + except ImportError: + print('Directive not found on the CAR !') + +if __name__ == "__main__" : + + socketIO = SocketIO(SERVER['ip'], SERVER['port'], LoggingNamespace, params={'specify': 'CAR'}) + + socketIO.on('directives', receiveDirectives) + socketIO.wait() \ No newline at end of file diff --git a/conf.py b/conf.py new file mode 100644 index 0000000..48fbe76 --- /dev/null +++ b/conf.py @@ -0,0 +1,39 @@ +#!/usr/bin/env python +# -*- coding: utf-8 -* + +# +# @author XU Kai(xukai.ken@gmail.com) +# @date 2016-12-04 星期日 +# +# +# @fileOverview 引脚端口的配置文件 +# +# +# + + +PORTS = { + + 'CarLeft': [12, 13], + + 'CarRight': [15, 16], + + 'CameraVertical': 7, + + 'CameraHorizontal': 32 + +} + +# Camera init degree as global vars. +DEGREE = { + 'vertical': 0, + + 'horizontal' : 0 +} + +# Server side address +SERVER = { + 'ip': '192.168.199.140', + + 'port': '8080' +} \ No newline at end of file diff --git a/directives/__init__.py b/directives/__init__.py new file mode 100644 index 0000000..d18ffe5 --- /dev/null +++ b/directives/__init__.py @@ -0,0 +1,17 @@ +# -*- coding: utf-8 -* + +__all__ = [ + + # 摄像头相关的操作指令 + 'camera_down', + 'camera_left', + 'camera_right', + 'camera_up' + + # 小车相关操作指令 + 'car_back', + 'car_forward', + 'car_left', + 'car_right' + +] diff --git a/directives/camera_down.py b/directives/camera_down.py new file mode 100644 index 0000000..beeda57 --- /dev/null +++ b/directives/camera_down.py @@ -0,0 +1,52 @@ +#!/usr/bin/env python +# -*- coding: utf-8 -* + +# +# @author XU Kai(xukai.ken@gmail.com) +# @date 2016-12-04 星期日 +# +# +# +# +# +# +# + +from conf import DEGREE, PORTS +import time + +reaTime = 0 + + +def run(GPIO, signal): + global reaTime + reaTime = time.time() + + +def notify(GPIO, curTime): + if not reaTime: + pass + else: + action(GPIO, int((curTime - reaTime) / 0.02)) + +def caledCycle(): + return 2.5 + DEGREE['vertical'] / 180 + +def action(GPIO, counter): + GPIO.setmode(GPIO.BOARD) + port = PORTS['CameraVertical'] + GPIO.setup(port, GPIO.OUT) + + p = GPIO.PWM(port, 50) # 50HZ + try: + global DEGREE + DEGREE['vertical'] += counter * 10 + + p.start(caledCycle()) + except ValueError: + pass + time.sleep(0.5) + + p.stop() + global reaTime + reaTime = 0 \ No newline at end of file diff --git a/directives/camera_left.py b/directives/camera_left.py new file mode 100644 index 0000000..7b31a27 --- /dev/null +++ b/directives/camera_left.py @@ -0,0 +1,54 @@ +#!/usr/bin/env python +# -*- coding: utf-8 -* + +# +# @author XU Kai(xukai.ken@gmail.com) +# @date 2016-12-04 星期日 +# +# +# +# +# +# +# + +from conf import DEGREE, PORTS +import time + +reaTime = 0 + + +def run(GPIO, signal): + global reaTime + reaTime = time.time() + + +def notify(GPIO, curTime): + if not reaTime: + pass + else: + action(GPIO, int((curTime - reaTime) / 0.02)) + + +def caledCycle(): + return 2.5 + DEGREE['horizontal'] / 180 + + +def action(GPIO, counter): + GPIO.setmode(GPIO.BOARD) + port = PORTS['CameraHorizontal'] + GPIO.setup(port, GPIO.OUT) + + p = GPIO.PWM(port, 50) # 50HZ + try: + global DEGREE + DEGREE['horizontal'] += counter * 10 + + p.start(caledCycle()) + except ValueError: + pass + time.sleep(0.5) + + p.stop() + global reaTime + reaTime = 0 \ No newline at end of file diff --git a/directives/camera_pause.py b/directives/camera_pause.py new file mode 100644 index 0000000..821c7d6 --- /dev/null +++ b/directives/camera_pause.py @@ -0,0 +1,30 @@ +#!/usr/bin/env python +# -*- coding: utf-8 -* + +# +# @author XU Kai(xukai.ken@gmail.com) +# @date 2016-12-04 星期日 +# +# +# +# +# +# +# + +import camera_down +import camera_up +import camera_left +import camera_right + +import time + + +def run(GPIO, signal): + + curTime = time.time() + + camera_down.notify(GPIO, curTime) + camera_up.notify(GPIO, curTime) + camera_left.notify(GPIO, curTime) + camera_right.notify(GPIO, curTime) \ No newline at end of file diff --git a/directives/camera_right.py b/directives/camera_right.py new file mode 100644 index 0000000..95cefb8 --- /dev/null +++ b/directives/camera_right.py @@ -0,0 +1,52 @@ +#!/usr/bin/env python +# -*- coding: utf-8 -* + +# +# @author XU Kai(xukai.ken@gmail.com) +# @date 2016-12-04 星期日 +# +# +# +# +# +# +# + +from conf import DEGREE, PORTS +import time + +reaTime = 0 + + +def run(GPIO, signal): + global reaTime + reaTime = time.time() + + +def notify(GPIO, curTime): + if not reaTime: + pass + else: + action(GPIO, int((curTime - reaTime) / 0.02)) + +def caledCycle(): + return 2.5 + DEGREE['horizontal'] / 180 + +def action(GPIO, counter): + GPIO.setmode(GPIO.BOARD) + port = PORTS['CameraHorizontal'] + GPIO.setup(port, GPIO.OUT) + + p = GPIO.PWM(port, 50) # 50HZ + try: + global DEGREE + DEGREE['horizontal'] -= counter * 10 + + p.start(caledCycle()) + except ValueError: + pass + time.sleep(0.5) + + p.stop() + global reaTime + reaTime = 0 \ No newline at end of file diff --git a/directives/camera_up.py b/directives/camera_up.py new file mode 100644 index 0000000..de8eca2 --- /dev/null +++ b/directives/camera_up.py @@ -0,0 +1,54 @@ +#!/usr/bin/env python +# -*- coding: utf-8 -* + +# +# @author XU Kai(xukai.ken@gmail.com) +# @date 2016-12-04 星期日 +# +# +# +# +# +# +# + +from conf import DEGREE, PORTS +import time + +reaTime = 0 + + +def run(GPIO, signal): + global reaTime + reaTime = time.time() + + +def notify(GPIO, curTime): + if not reaTime: + pass + else: + action(GPIO, int((curTime - reaTime) / 0.02)) + + +def caledCycle(): + return 2.5 + DEGREE['vertical'] / 180 + + +def action(GPIO, counter): + GPIO.setmode(GPIO.BOARD) + port = PORTS['CameraVertical'] + GPIO.setup(port, GPIO.OUT) + + p = GPIO.PWM(port, 50) # 50HZ + try: + global DEGREE + DEGREE['vertical'] -= counter * 10 + + p.start(caledCycle()) + except ValueError: + pass + time.sleep(0.5) + + p.stop() + global reaTime + reaTime = 0 \ No newline at end of file diff --git a/directives/car_back.py b/directives/car_back.py new file mode 100644 index 0000000..91e22d9 --- /dev/null +++ b/directives/car_back.py @@ -0,0 +1,26 @@ +#!/usr/bin/env python +# -*- coding: utf-8 -* + +# +# @author XU Kai(xukai.ken@gmail.com) +# @date 2016-12-04 星期日 +# +# +# +# +# +# +# + +from conf import PORTS + + +def run(GPIO, signal): + + GPIO.setmode(GPIO.BOARD) + + CarLeft = PORTS['CarLeft'] + CarRight = PORTS['CarRight'] + GPIO.setup(CarLeft + CarRight, GPIO.OUT) + + GPIO.output(CarLeft + CarRight, (True, False, False, True)) diff --git a/directives/car_forward.py b/directives/car_forward.py new file mode 100644 index 0000000..aeeae89 --- /dev/null +++ b/directives/car_forward.py @@ -0,0 +1,26 @@ +#!/usr/bin/env python +# -*- coding: utf-8 -* + +# +# @author XU Kai(xukai.ken@gmail.com) +# @date 2016-12-04 星期日 +# +# +# +# +# +# +# + +from conf import PORTS + + +def run(GPIO, signal): + + GPIO.setmode(GPIO.BOARD) + + CarLeft = PORTS['CarLeft'] + CarRight = PORTS['CarRight'] + GPIO.setup(CarLeft + CarRight, GPIO.OUT) + + GPIO.output(CarLeft + CarRight, (False, True, True, False)) diff --git a/directives/car_left.py b/directives/car_left.py new file mode 100644 index 0000000..3daba52 --- /dev/null +++ b/directives/car_left.py @@ -0,0 +1,26 @@ +#!/usr/bin/env python +# -*- coding: utf-8 -* + +# +# @author XU Kai(xukai.ken@gmail.com) +# @date 2016-12-04 星期日 +# +# +# +# +# +# +# + +from conf import PORTS + + +def run(GPIO, signal): + + GPIO.setmode(GPIO.BOARD) + + CarLeft = PORTS['CarLeft'] + CarRight = PORTS['CarRight'] + GPIO.setup(CarLeft + CarRight, GPIO.OUT) + + GPIO.output(CarLeft + CarRight, (True, False, True, False)) diff --git a/directives/car_pause.py b/directives/car_pause.py new file mode 100644 index 0000000..5f4ab16 --- /dev/null +++ b/directives/car_pause.py @@ -0,0 +1,4 @@ +# -*- coding: utf-8 -* + +def run(GPIO, signal): + GPIO.cleanup() diff --git a/directives/car_right.py b/directives/car_right.py new file mode 100644 index 0000000..118fed7 --- /dev/null +++ b/directives/car_right.py @@ -0,0 +1,26 @@ +#!/usr/bin/env python +# -*- coding: utf-8 -* + +# +# @author XU Kai(xukai.ken@gmail.com) +# @date 2016-12-04 星期日 +# +# +# +# +# +# +# + +from conf import PORTS + + +def run(GPIO, signal): + + GPIO.setmode(GPIO.BOARD) + + CarLeft = PORTS['CarLeft'] + CarRight = PORTS['CarRight'] + GPIO.setup(CarLeft + CarRight, GPIO.OUT) + + GPIO.output(CarLeft + CarRight, (False, True, False, True)) diff --git a/requirements.txt b/requirements.txt new file mode 100644 index 0000000..8e2c878 --- /dev/null +++ b/requirements.txt @@ -0,0 +1,2 @@ +socketIO_client==0.7.1 +hcsr04sensor==1.5.1 \ No newline at end of file diff --git a/tests/camera_pwm.py b/tests/camera_pwm.py new file mode 100644 index 0000000..cfbc753 --- /dev/null +++ b/tests/camera_pwm.py @@ -0,0 +1,49 @@ +#!/usr/bin/env python +# -*- coding: utf-8 -* + +# +# @author XU Kai(xukai.ken@gmail.com) +# @date 2016-12-04 星期日 +# +# +# #fileOverview PMW 调试测试 +# +# +# + +import RPi.GPIO as GPIO +import time +import atexit + +atexit.register(GPIO.cleanup) + +GPIO.setmode(GPIO.BOARD) +GPIO.setup(7, GPIO.OUT, initial=False) + +p = GPIO.PWM(7, 50) #50HZ + +p.start(0) +time.sleep(2) + + +# PWM控制信号周期20ms, 脉宽 0.5ms-2.5ms 对应的角度-90到+90度, 范围180度(3度左右偏差), 当脉宽1.5ms时舵机在中立点(0度); + +while (True): + for i in range(0, 181, 10): + + cycle = 1.2 + 10 * i / 180 + p.ChangeDutyCycle(cycle) #设置转动角度 + + print('高电平脉宽:' + bytes(cycle)) + time.sleep(0.02) #等该20ms周期结束 + p.ChangeDutyCycle(0) #归零信号 + + + + for i in range(181, 0, -10): + + cycle = 1.2 + 10 * i / 180 + p.ChangeDutyCycle(cycle) + print('高电平脉宽:' + bytes(cycle)) + time.sleep(0.02) + p.ChangeDutyCycle(0) diff --git a/tests/camera_pwm_1.py b/tests/camera_pwm_1.py new file mode 100644 index 0000000..04e8966 --- /dev/null +++ b/tests/camera_pwm_1.py @@ -0,0 +1,53 @@ +#!/usr/bin/env python +# -*- coding: utf-8 -* + +# +# @author XU Kai(xukai.ken@gmail.com) +# @date 2016-12-04 星期日 +# +# +# #fileOverview PMW 调试测试 +# +# +# + +import RPi.GPIO as GPIO +import time +import atexit + +atexit.register(GPIO.cleanup) + +GPIO.setmode(GPIO.BOARD) +GPIO.setup(7, GPIO.OUT, initial=False) + +p = GPIO.PWM(7, 50) #50HZ + +p.start(1.5) + +time.sleep(2) +p.ChangeDutyCycle(0) + +p.stop() + +# PWM控制信号周期20ms, 脉宽 0.5ms-2.5ms 对应的角度-90到+90度, 范围180度(3度左右偏差), 当脉宽1.5ms时舵机在中立点(0度); + + +# while (True): +# for i in range(0, 181, 10): + +# cycle = 1.2 + 10 * i / 180 +# p.ChangeDutyCycle(cycle) #设置转动角度 + +# print('高电平脉宽:' + bytes(cycle)) +# time.sleep(0.02) #等该20ms周期结束 +# p.ChangeDutyCycle(0) #归零信号 + + + +# for i in range(181, 0, -10): + +# cycle = 1.2 + 10 * i / 180 +# p.ChangeDutyCycle(cycle) +# print('高电平脉宽:' + bytes(cycle)) +# time.sleep(0.02) +# p.ChangeDutyCycle(0) diff --git a/tests/diode.py b/tests/diode.py new file mode 100644 index 0000000..8b852d4 --- /dev/null +++ b/tests/diode.py @@ -0,0 +1,44 @@ +#!/usr/bin/env python +# -*- coding: utf-8 -* + +# +# @author XU Kai(xukai.ken@gmail.com) +# @date 2016-12-04 星期日 +# +# +# +# +# + +import RPi.GPIO as gpio +import time + +gpio.setmode(gpio.BOARD) + +gpio.setup(38, gpio.OUT) +gpio.setup(40, gpio.OUT) + +gpio.output(38, False) +gpio.output(40, False) + +switch = True +counter = 0 + +try: + while True: + + switch = not switch + gpio.output(40, switch) + + time.sleep(1) + + counter = counter + 1 + + if counter == 200: + break + +except KeyboardInterrupt: + pass + +finally: + gpio.cleanup() diff --git a/tests/diode_1.py b/tests/diode_1.py new file mode 100644 index 0000000..2d41b66 --- /dev/null +++ b/tests/diode_1.py @@ -0,0 +1,41 @@ +#!/usr/bin/env python +# -*- coding: utf-8 -* + +# +# @author XU Kai(xukai.ken@gmail.com) +# @date 2016-12-04 星期日 +# +# +# +# {12, 13}, {15, 16}, {18, 22} +# + +import RPi.GPIO as gpio +import time + +gpio.setmode(gpio.BOARD) + +gpio.setup(38, gpio.OUT) +gpio.setup(40, gpio.OUT) + + + +gpio.output(38, True) +gpio.output(40, True) + + +switch = True +counter = 0 +while True: + + switch = not switch + gpio.output(40, switch) + + time.sleep(0.5) + + counter = counter + 1 + + if counter == 50: + break + +gpio.cleanup() diff --git a/tests/diode_2.py b/tests/diode_2.py new file mode 100644 index 0000000..c33c42b --- /dev/null +++ b/tests/diode_2.py @@ -0,0 +1,40 @@ +#!/usr/bin/env python +# -*- coding: utf-8 -* + +# +# @author XU Kai(xukai.ken@gmail.com) +# @date 2016-12-04 星期日 +# +# @fileOverview 使用 PWM 信号调试红色的发光二极管 +# +# +# + +import RPi.GPIO as GPIO +import time + +GPIO.setmode(GPIO.BOARD) + +GPIO.setup(38, GPIO.OUT) +GPIO.setup(40, GPIO.OUT) + +GPIO.output(38, True) +GPIO.output(40, True) + +p = GPIO.PWM(40, 200) # 通道为 12 频率为 50Hz +p.start(0) +try: + while 1: + for dc in range(0, 101, 1): + p.ChangeDutyCycle(dc) + time.sleep(0.05) + for dc in range(100, -1, -1): + p.ChangeDutyCycle(dc) + time.sleep(0.05) + +except KeyboardInterrupt: + pass + +p.stop() + +GPIO.cleanup() diff --git a/tests/diode_3.py b/tests/diode_3.py new file mode 100644 index 0000000..a49cc1a --- /dev/null +++ b/tests/diode_3.py @@ -0,0 +1,44 @@ +#!/usr/bin/env python +# -*- coding: utf-8 -* + +# +# @author XU Kai(xukai.ken@gmail.com) +# @date 2016-12-04 星期日 +# +# @fileOverview 使用 PWM 信号调试红色的发光二极管 +# +# +# + +import RPi.GPIO as GPIO +import time + +GPIO.setmode(GPIO.BOARD) + +GPIO.setup(38, GPIO.OUT) +GPIO.setup(40, GPIO.OUT) + +GPIO.output(38, False) +GPIO.output(40, False) + +p = GPIO.PWM(40, 200) # 通道为 12 频率为 50Hz + +p.start(20) + + +time.sleep(5) + +p.ChangeDutyCycle(80) + +time.sleep(5) + +p.ChangeDutyCycle(100) +try: + time.sleep(1000) + +except KeyboardInterrupt: + + p.stop() + GPIO.cleanup() + + pass diff --git a/tests/echo.py b/tests/echo.py new file mode 100644 index 0000000..6133732 --- /dev/null +++ b/tests/echo.py @@ -0,0 +1,44 @@ +#!/usr/bin/env python +# -*- coding: utf-8 -* + +# +# @author XU Kai(xukai.ken@gmail.com) +# @date 2016-12-04 星期日 +# +# @fileOverview 回环程序检测,可以根据声波传输和接受的速度,用来测试距离之用。 +# +# +# +# + +import RPi.GPIO as GPIO +import time + +GPIO.setmode(GPIO.BOARD) + +GPIO.setup(11, GPIO.OUT) +GPIO.setup(7, GPIO.IN) + +p = GPIO.PWM(11, 1000) # 100HZ 脉冲周期为 1000 / 1000 = 1 ms +p.start(5) # 高电平的频宽为 1000 / 0.05 = 50 us + + + +GPIO.wait_for_edge(7, GPIO.FALLING) + +try: + while True: + if GPIO.input(7) == GPIO.HIGH: + print('Yes, high .') + else: + print('Not, high .') + + time.sleep(0.01) + +except KeyboardInterrupt: + pass + +finally: + print('\nstop the pwm .') + p.stop() + GPIO.cleanup() diff --git a/tests/echo_1.py b/tests/echo_1.py new file mode 100644 index 0000000..be19dae --- /dev/null +++ b/tests/echo_1.py @@ -0,0 +1,37 @@ +#!/usr/bin/env python +# -*- coding: utf-8 -* + +# +# @author XU Kai(xukai.ken@gmail.com) +# @date 2016-12-04 星期日 +# +# @fileOverview 回环程序检测,可以根据声波传输和接受的速度,用来测试距离之用。 +# +# +# +# + +import RPi.GPIO as GPIO +import time + +GPIO.setmode(GPIO.BOARD) + +GPIO.setup(11, GPIO.OUT) +GPIO.setup(7, GPIO.IN) + +p = GPIO.PWM(11, 1000) # 100HZ 脉冲周期为 1000 / 1000 = 1 ms +p.start(5) # 高电平的频宽为 1000 / 0.05 = 50 us + +# wait for up to 5 seconds for a rising edge (timeout is in milliseconds) +channel = GPIO.wait_for_edge(7, GPIO.FALLING, timeout=5000) +last = time.time() +if channel is None: + print('Timeout occurred') +else: + + current = time.time() + print(last - current) + print('Edge detected on channel', channel) + +p.stop() +GPIO.cleanup() diff --git a/tests/echo_2.py b/tests/echo_2.py new file mode 100644 index 0000000..68b63b1 --- /dev/null +++ b/tests/echo_2.py @@ -0,0 +1,47 @@ +#!/usr/bin/env python +# -*- coding: utf-8 -* + +# +# @author XU Kai(xukai.ken@gmail.com) +# @date 2016-12-04 星期日 +# +# @fileOverview 回环程序检测,可以根据声波传输和接受的速度,用来测试距离之用。 +# +# +# +# + +import RPi.GPIO as GPIO +import time + +from hcsr04sensor import sensor + +GPIO.setmode(GPIO.BCM) + +#set GPIO direction (IN / OUT) +GPIO.setup(17, GPIO.OUT) +GPIO.setup(4, GPIO.IN) + + +def distance(): + value = sensor.Measurement(17, 4) + raw_measurement = value.raw_distance() + + # Calculate the distance in centimeters + metric_distance = value.distance_metric(raw_measurement) + + print("The Distance = {} centimeters".format(metric_distance)) + + +if __name__ == '__main__': + try: + while True: + distance() + time.sleep(1) + + # Reset by pressing CTRL + C + except KeyboardInterrupt: + print("Measurement stopped by User") + GPIO.cleanup() + except ValueError: + GPIO.cleanup() diff --git a/tests/fan.py b/tests/fan.py new file mode 100644 index 0000000..1b6d95a --- /dev/null +++ b/tests/fan.py @@ -0,0 +1,23 @@ +#!/usr/bin/env python +# -*- coding: utf-8 -* + +# +# @author XU Kai(xukai.ken@gmail.com) +# @date 2016-12-04 星期日 +# +# @fileOverview PMW 进行风扇转速的控制。 +# +# +# +# + +import RPi.GPIO as GPIO +import time + +GPIO.setmode(GPIO.BOARD) +GPIO.setup(13, GPIO.OUT) + +GPIO.output(13, False) + +time.sleep(5) +GPIO.cleanup() \ No newline at end of file diff --git a/tests/python.py b/tests/python.py new file mode 100644 index 0000000..9eca966 --- /dev/null +++ b/tests/python.py @@ -0,0 +1,9 @@ +#!/usr/bin/env python +# -*- coding: utf-8 -* + +from conf import DIRECTION + +DIRECTION['vertical'] = 4099 + +print DIRECTION['vertical'] + diff --git a/tests/run.py b/tests/run.py new file mode 100644 index 0000000..df7ca4f --- /dev/null +++ b/tests/run.py @@ -0,0 +1,34 @@ +#!/usr/bin/env python +# -*- coding: utf-8 -* + +# +# @author XU Kai(xukai.ken@gmail.com) +# @date 2016-12-04 星期日 +# +# +# #fileOverview 小车模拟运行测试代码 +# +# +# + +import RPi.GPIO as GPIO +import time + +import atexit + +atexit.register(GPIO.cleanup) + +GPIO.setmode(GPIO.BOARD) +GPIO.setup(12, GPIO.OUT) +GPIO.setup(13, GPIO.OUT) +GPIO.setup(15, GPIO.OUT) +GPIO.setup(16, GPIO.OUT) + + +GPIO.output(12, False) +GPIO.output(13, True) +GPIO.output(15, True) +GPIO.output(16, False) + + +time.sleep(20000) diff --git a/tests/run_1.py b/tests/run_1.py new file mode 100644 index 0000000..3f4f406 --- /dev/null +++ b/tests/run_1.py @@ -0,0 +1,36 @@ +#!/usr/bin/env python +# -*- coding: utf-8 -* + +# +# @author XU Kai(xukai.ken@gmail.com) +# @date 2016-12-04 星期日 +# +# +# #fileOverview 小车模拟运行测试代码 +# +# +# + +import RPi.GPIO as GPIO +import time + + + +GPIO.setwarnings(False) + +GPIO.setmode(GPIO.BOARD) +GPIO.setup(12, GPIO.OUT) +GPIO.setup(13, GPIO.OUT) +GPIO.setup(15, GPIO.OUT) +GPIO.setup(16, GPIO.OUT) + + +GPIO.output(12, True) +GPIO.output(13, False) +GPIO.output(15, False) +GPIO.output(16, True) + + +time.sleep(0.25) +GPIO.cleanup() + diff --git a/tests/run_2.py b/tests/run_2.py new file mode 100644 index 0000000..8c3ddf6 --- /dev/null +++ b/tests/run_2.py @@ -0,0 +1,34 @@ +#!/usr/bin/env python +# -*- coding: utf-8 -* + +# +# @author XU Kai(xukai.ken@gmail.com) +# @date 2016-12-04 星期日 +# +# +# #fileOverview 小车模拟运行测试代码 +# +# +# + +import RPi.GPIO as GPIO +import time + +GPIO.setwarnings(False) + +GPIO.setmode(GPIO.BOARD) +GPIO.setup(12, GPIO.OUT) +GPIO.setup(13, GPIO.OUT) +GPIO.setup(15, GPIO.OUT) +GPIO.setup(16, GPIO.OUT) + + +GPIO.output(12, True) +GPIO.output(13, False) +GPIO.output(15, True) +GPIO.output(16, False) + + +time.sleep(0.25) +GPIO.cleanup() + diff --git a/tests/run_3.py b/tests/run_3.py new file mode 100644 index 0000000..1b59e4c --- /dev/null +++ b/tests/run_3.py @@ -0,0 +1,34 @@ +#!/usr/bin/env python +# -*- coding: utf-8 -* + +# +# @author XU Kai(xukai.ken@gmail.com) +# @date 2016-12-04 星期日 +# +# +# #fileOverview 小车模拟运行测试代码 +# +# +# + +import RPi.GPIO as GPIO +import time + +GPIO.setwarnings(False) + +GPIO.setmode(GPIO.BOARD) +GPIO.setup(12, GPIO.OUT) +GPIO.setup(13, GPIO.OUT) +GPIO.setup(15, GPIO.OUT) +GPIO.setup(16, GPIO.OUT) + + +GPIO.output(12, False) +GPIO.output(13, True) +GPIO.output(15, False) +GPIO.output(16, True) + + +time.sleep(0.25) +GPIO.cleanup() +