From 6b93806568a34d0c7924feb6cc919ace33102c98 Mon Sep 17 00:00:00 2001 From: Silver Kuusik Date: Fri, 10 Nov 2017 17:46:30 +0100 Subject: [PATCH 1/5] Initial commit --- .gitignore | 101 +++++++++++++++++++++++++++++++++++++++++++++++++++++ LICENSE | 21 +++++++++++ README.md | 2 ++ 3 files changed, 124 insertions(+) create mode 100644 .gitignore create mode 100644 LICENSE create mode 100644 README.md diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..7bbc71c --- /dev/null +++ b/.gitignore @@ -0,0 +1,101 @@ +# 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/ +wheels/ +*.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/ + +# Jupyter Notebook +.ipynb_checkpoints + +# pyenv +.python-version + +# celery beat schedule file +celerybeat-schedule + +# SageMath parsed files +*.sage.py + +# dotenv +.env + +# virtualenv +.venv +venv/ +ENV/ + +# Spyder project settings +.spyderproject +.spyproject + +# Rope project settings +.ropeproject + +# mkdocs documentation +/site + +# mypy +.mypy_cache/ diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..7b6d80b --- /dev/null +++ b/LICENSE @@ -0,0 +1,21 @@ +MIT License + +Copyright (c) 2017 RoboKoding + +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..2ee96f2 --- /dev/null +++ b/README.md @@ -0,0 +1,2 @@ +# sumorobot-firmware +The software that is running on the SumoRobots From 8744b96a9af51dce270ca27efa436a46232d7b1e Mon Sep 17 00:00:00 2001 From: Silver Kuusik Date: Sun, 17 Dec 2017 14:39:15 +0100 Subject: [PATCH 2/5] Update README.md --- README.md | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/README.md b/README.md index 2ee96f2..f56f517 100644 --- a/README.md +++ b/README.md @@ -1,2 +1,11 @@ # sumorobot-firmware The software that is running on the SumoRobots + +# Instructions +* Downlad the [SumoRobot firmware](https://github.com/eik-robo/sumoesp/tree/master/soft) +* Replace the hal.py with the one in this repository +* Install [MicroPython](http://micropython.org/download#esp32) on your ESP32 +* Upload the SumoRobot firmware on your ESP32 with [ampy](https://github.com/adafruit/ampy) + +# Credits +* [K-SPACE MTÜ](https://k-space.ee/) From 0cb60051bb048c4bf50bbfb08340fb7763f365d3 Mon Sep 17 00:00:00 2001 From: Silver Kuusik Date: Sun, 17 Dec 2017 14:59:52 +0100 Subject: [PATCH 3/5] Create hal.py --- hal.py | 132 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 132 insertions(+) create mode 100644 hal.py diff --git a/hal.py b/hal.py new file mode 100644 index 0000000..9c67b89 --- /dev/null +++ b/hal.py @@ -0,0 +1,132 @@ +from utime import sleep_us +from machine import Pin, PWM, ADC, time_pulse_us + +WIFIS = dict({ +"": ""}) + +# directions +STOP = 0 +LEFT = 1 +RIGHT = 2 +FORWARD = 3 +BACKWARD = 4 + +# Battery resistor ladder ratio +BATTERY_COEFF = 2.25 + +# Ultrasonic sensor calibration +ULTRASONIC_OFFSET = 800 + +# Servo timing +MOTOR_LEFT_TUNING = 33 +MOTOR_RIGHT_TUNING = 33 + +# Ultrasonic distance sensor +echo = Pin(14, Pin.IN) +trigger = Pin(27, Pin.OUT) + +# Motor PWM-s +pwm_left = PWM(Pin(15), freq=50, duty=0) +pwm_right = PWM(Pin(4), freq=50, duty=0) + +# bottom LED +bottom_led = Pin(5, Pin.OUT) +# bottom LED is in reverse polarity +bottom_led.value(1) +# sensor LEDs +enemy_led = PWM(Pin(16), freq=50, duty=0) +left_line_led = PWM(Pin(17), freq=50, duty=0) +right_line_led = PWM(Pin(12), freq=50, duty=0) + +# Battery gauge +adc_battery = ADC(Pin(32)) + +# Optek sensors +adc_line_left = ADC(Pin(34)) +adc_line_right = ADC(Pin(33)) + +# Set reference voltage to 3.3V +adc_battery.atten(ADC.ATTN_11DB) +adc_line_left.atten(ADC.ATTN_11DB) +adc_line_right.atten(ADC.ATTN_11DB) + +# Calibrate line sensors +LINE_LEFT_THRESHOLD = adc_line_left.read() +LINE_RIGHT_THRESHOLD = adc_line_right.read() + +def status_led(state): + bottom_led.value(0 if state else 1) + +def battery_voltage(): + return BATTERY_COEFF * (adc_battery.read() * 3.3 / 4096) + +enemy_score = 0 +def enemy_distance(): + global enemy_score + + trigger.value(0) + sleep_us(5) + trigger.value(1) + sleep_us(10) + trigger.value(0) + # wait for the pulse and calculate the distance + enemy_distance = (machine.time_pulse_us(echo, 1, 30000) / 2) / 29.1 + + if enemy_distance < 60 and enemy_distance > 0: + if enemy_score < 5: + enemy_score += 1 + else: + if enemy_score > 0: + enemy_score -= 1 + + # indicate enemy LED + enemy_led.duty(255 if enemy_score > 2 else 0) + + return True if enemy_score > 2 else False + +def line_left(): + line = abs(adc_line_left.read() - LINE_LEFT_THRESHOLD) > 1000 + if line: + left_line_led.duty(255) + else: + left_line_led.duty(0) + return line + +def line_right(): + line = abs(adc_line_right.read() - LINE_RIGHT_THRESHOLD) > 1000 + if line: + right_line_led.duty(255) + else: + right_line_led.duty(0) + return line + +def detach_servos(): + motor_left(0) + motor_right(0) + +prev_left_speed = 0 +def motor_left(speed): + global prev_left_speed + if speed == prev_left_speed: + return + prev_left_speed = speed + assert speed >= -100 + assert speed <= 100 + pwm_left.duty(int(33 + MOTOR_LEFT_TUNING + speed * 33 / 100)) # -100 ... 100 to 33 .. 102 + if speed == 0: + pwm_left.duty(0) + +prev_right_speed = 0 +def motor_right(speed): + global prev_right_speed + if speed == prev_right_speed: + return + prev_right_speed = speed + assert speed >= -100 + assert speed <= 100 + pwm_right.duty(int(33 + MOTOR_RIGHT_TUNING + speed * 33 / 100)) # -100 ... 100 to 33 .. 102 + if speed == 0: + pwm_right.duty(0) + +print("Battery voltage: %.2fV" % battery_voltage()) +print("Line sensor thresholds:", LINE_LEFT_THRESHOLD, LINE_RIGHT_THRESHOLD) From dddea42b35fd22434f1cee5b7145ac8844d7af34 Mon Sep 17 00:00:00 2001 From: Silver Kuusik Date: Sun, 17 Dec 2017 15:01:20 +0100 Subject: [PATCH 4/5] Update README.md --- README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README.md b/README.md index f56f517..593a1ea 100644 --- a/README.md +++ b/README.md @@ -4,6 +4,7 @@ The software that is running on the SumoRobots # Instructions * Downlad the [SumoRobot firmware](https://github.com/eik-robo/sumoesp/tree/master/soft) * Replace the hal.py with the one in this repository +* Add your WiFi networks to the hal.py WIFIS dict * Install [MicroPython](http://micropython.org/download#esp32) on your ESP32 * Upload the SumoRobot firmware on your ESP32 with [ampy](https://github.com/adafruit/ampy) From 1d434b228304c61670651d25f769dbb1a82da2e0 Mon Sep 17 00:00:00 2001 From: Silver Kuusik Date: Sun, 17 Dec 2017 15:01:47 +0100 Subject: [PATCH 5/5] Update hal.py --- hal.py | 1 + 1 file changed, 1 insertion(+) diff --git a/hal.py b/hal.py index 9c67b89..450d32e 100644 --- a/hal.py +++ b/hal.py @@ -1,6 +1,7 @@ from utime import sleep_us from machine import Pin, PWM, ADC, time_pulse_us +# add your WiFi SSID and password WIFIS = dict({ "": ""})