Skip to content

Commit 3af3482

Browse files
committed
feat: inital commit; proximity-alert module
0 parents  commit 3af3482

File tree

9 files changed

+541
-0
lines changed

9 files changed

+541
-0
lines changed

.github/workflows/deploy.yml

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
# see https://github.com/viamrobotics/build-action for help
2+
on:
3+
push:
4+
tags:
5+
- "[0-9]+.[0-9]+.[0-9]+" # the build-action will trigger on tags with the format 1.0.0
6+
7+
jobs:
8+
publish:
9+
runs-on: ubuntu-latest
10+
steps:
11+
- uses: actions/checkout@v4
12+
- uses: viamrobotics/build-action@v1
13+
with:
14+
# note: you can replace this line with 'version: ""' if
15+
# you want to test the build process without deploying
16+
version: ${{ github.ref_name }}
17+
ref: ${{ github.sha }}
18+
key-id: ${{ secrets.viam_key_id }}
19+
key-value: ${{ secrets.viam_key_value }}
20+
token: ${{ github.token }} # only required for private git repos

.gitignore

Lines changed: 168 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,168 @@
1+
# https://github.com/github/gitignore/blob/main/Python.gitignore
2+
# Byte-compiled / optimized / DLL files
3+
__pycache__/
4+
*.py[cod]
5+
*$py.class
6+
7+
# C extensions
8+
*.so
9+
10+
# Distribution / packaging
11+
.Python
12+
build/
13+
develop-eggs/
14+
dist/
15+
downloads/
16+
eggs/
17+
.eggs/
18+
lib/
19+
lib64/
20+
parts/
21+
sdist/
22+
var/
23+
wheels/
24+
share/python-wheels/
25+
*.egg-info/
26+
.installed.cfg
27+
*.egg
28+
MANIFEST
29+
30+
# PyInstaller
31+
# Usually these files are written by a python script from a template
32+
# before PyInstaller builds the exe, so as to inject date/other infos into it.
33+
*.manifest
34+
*.spec
35+
36+
# Installer logs
37+
pip-log.txt
38+
pip-delete-this-directory.txt
39+
40+
# Unit test / coverage reports
41+
htmlcov/
42+
.tox/
43+
.nox/
44+
.coverage
45+
.coverage.*
46+
.cache
47+
nosetests.xml
48+
coverage.xml
49+
*.cover
50+
*.py,cover
51+
.hypothesis/
52+
.pytest_cache/
53+
cover/
54+
55+
# Translations
56+
*.mo
57+
*.pot
58+
59+
# Django stuff:
60+
*.log
61+
local_settings.py
62+
db.sqlite3
63+
db.sqlite3-journal
64+
65+
# Flask stuff:
66+
instance/
67+
.webassets-cache
68+
69+
# Scrapy stuff:
70+
.scrapy
71+
72+
# Sphinx documentation
73+
docs/_build/
74+
75+
# PyBuilder
76+
.pybuilder/
77+
target/
78+
79+
# Jupyter Notebook
80+
.ipynb_checkpoints
81+
82+
# IPython
83+
profile_default/
84+
ipython_config.py
85+
86+
# pyenv
87+
# For a library or package, you might want to ignore these files since the code is
88+
# intended to run in multiple environments; otherwise, check them in:
89+
# .python-version
90+
91+
# pipenv
92+
# According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control.
93+
# However, in case of collaboration, if having platform-specific dependencies or dependencies
94+
# having no cross-platform support, pipenv may install dependencies that don't work, or not
95+
# install all needed dependencies.
96+
#Pipfile.lock
97+
98+
# poetry
99+
# Similar to Pipfile.lock, it is generally recommended to include poetry.lock in version control.
100+
# This is especially recommended for binary packages to ensure reproducibility, and is more
101+
# commonly ignored for libraries.
102+
# https://python-poetry.org/docs/basic-usage/#commit-your-poetrylock-file-to-version-control
103+
#poetry.lock
104+
105+
# pdm
106+
# Similar to Pipfile.lock, it is generally recommended to include pdm.lock in version control.
107+
#pdm.lock
108+
# pdm stores project-wide configurations in .pdm.toml, but it is recommended to not include it
109+
# in version control.
110+
# https://pdm.fming.dev/latest/usage/project/#working-with-version-control
111+
.pdm.toml
112+
.pdm-python
113+
.pdm-build/
114+
115+
# PEP 582; used by e.g. github.com/David-OConnor/pyflow and github.com/pdm-project/pdm
116+
__pypackages__/
117+
118+
# Celery stuff
119+
celerybeat-schedule
120+
celerybeat.pid
121+
122+
# SageMath parsed files
123+
*.sage.py
124+
125+
# Environments
126+
.env
127+
.venv
128+
env/
129+
venv/
130+
ENV/
131+
env.bak/
132+
venv.bak/
133+
.installed
134+
135+
# Spyder project settings
136+
.spyderproject
137+
.spyproject
138+
139+
# Rope project settings
140+
.ropeproject
141+
142+
# mkdocs documentation
143+
/site
144+
145+
# mypy
146+
.mypy_cache/
147+
.dmypy.json
148+
dmypy.json
149+
150+
# Pyre type checker
151+
.pyre/
152+
153+
# pytype static type analyzer
154+
.pytype/
155+
156+
# Cython debug symbols
157+
cython_debug/
158+
159+
# PyCharm
160+
# JetBrains specific template is maintained in a separate JetBrains.gitignore that can
161+
# be found at https://github.com/github/gitignore/blob/main/Global/JetBrains.gitignore
162+
# and can be added to the global gitignore or merged into this file. For a more nuclear
163+
# option (not recommended) you can uncomment the following to ignore the entire idea folder.
164+
#.idea/
165+
166+
# Other ignores
167+
.DS_Store
168+
reload.sh

.viam-gen-info

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
{
2+
"module_name": "proximity-alert",
3+
"namespace": "atacke",
4+
"language": "python",
5+
"resource_type": "service",
6+
"resource_subtype": "generic",
7+
"model_name": "hc-sr04_rgb-led",
8+
"enable_cloud_build": true,
9+
"initialize_git": false,
10+
"generator_version": "0.1.0",
11+
"generated_on": "2025-02-20T21:27:45.919959Z"
12+
}

README.md

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
# `proximity-alert` module
2+
3+
This module implements the [`rdk:service:generic` API](https://docs.viam.com/appendix/apis/services/generic/) to use with the proximity alert workshop.
4+
5+
With this model, you can automatically start sensing distances with the ultrasonic sensor and turn an RGB LED red or green if it passes a specified "safe" threshold.
6+
7+
## Requirements
8+
This module assumes you have a Viam machine configured with a board component that is connected to an ultrasonic sensor and an RGB LED. (See the proximity-alert workshop to get started)
9+
10+
## Model atacke:proximity-alert:hc-sr04_rgb-led
11+
Using the ultrasonic sensor's readings and the configured `safe_distance`, the proximity alert will change the color of the RGB LED as follows:
12+
13+
| Detected distance | Meaning | RGB LED Color |
14+
|---------------|--------|-----------|
15+
| Greater than or equal to `safe_distance` (default is `0.2`) | Object is within a safe distance or farther | Green |
16+
| Less than `safe_distance` (default is `0.2`) | Object is too close, surpassing the safe distance threshold | Red |
17+
18+
If you wish to customize these colors or write your own logic, you can [create your own module](https://docs.viam.com/operate/get-started/other-hardware/hello-world-module/) using this repository as a starting point.
19+
20+
### Configuration
21+
The following attribute template can be used to configure this model:
22+
23+
```json
24+
{
25+
"board": <string>,
26+
"sensor": <string>,
27+
"red_pin": <string>,
28+
"green_pin": <string>,
29+
"blue_pin": <string>,
30+
"safe_distance": <float>
31+
}
32+
```
33+
34+
#### Attributes
35+
36+
The following attributes are available for this model:
37+
38+
| Name | Type | Inclusion | Description |
39+
|---------------|--------|-----------|----------------------------|
40+
| `board` | string | Required | The name of the board component (which is the same as the Raspberry Pi board in the Viam app). This will be used to control the GPIO pins for the ultrasonic sensor and the RGB LED. |
41+
| `sensor` | string | Required | The name of the HC-SR04 ultrasonic sensor component in the Viam app. |
42+
| `red_pin` | string | Required | A string representing the physical pin on your board connected to the red lead of a common cathode RGB LED. The string must contain only digits, such as "33" |
43+
| `green_pin` | string | Required | A string representing the physical pin on your board connected to the green lead of a common cathode RGB LED. The string must contain only digits, such as "32" |
44+
| `blue_pin` | string | Required | A string representing the physical pin on your board connected to the blue lead of a common cathode RGB LED. The string must contain only digits, such as "12" |
45+
| `safe_distance` | float | Optional | The designated threshold that determines when to turn the RGB LED a safe (green) or unsafe (red) color. If an object is detected to be at or greater than this distance, it is considered a "safe" distance and will light accordingly. If an object is detected to be less than this distance, it is considered an "unsafe" distance and will light accordingly. Default is `0.2` |
46+
47+
#### Example Configuration
48+
49+
```json
50+
{
51+
"board": "board-1",
52+
"sensor": "sensor-x",
53+
"red_pin": "33",
54+
"green_pin": "32",
55+
"blue_pin": "12",
56+
"safe_distance": "0.3"
57+
}
58+
```
59+

build.sh

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
#!/bin/sh
2+
cd `dirname $0`
3+
4+
# Create a virtual environment to run our code
5+
VENV_NAME="venv"
6+
PYTHON="$VENV_NAME/bin/python"
7+
8+
if ! $PYTHON -m pip install pyinstaller -Uqq; then
9+
exit 1
10+
fi
11+
12+
$PYTHON -m PyInstaller --onefile --hidden-import="googleapiclient" src/main.py
13+
tar -czvf dist/archive.tar.gz ./dist/main meta.json

meta.json

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
{
2+
"$schema": "https://dl.viam.dev/module.schema.json",
3+
"module_id": "atacke:proximity-alert",
4+
"visibility": "public",
5+
"url": "https://github.com/viam-devrel/proximity-controller",
6+
"description": "Modular generic service: hc-sr04_rgb-led",
7+
"models": [
8+
{
9+
"api": "rdk:service:generic",
10+
"model": "atacke:proximity-alert:hc-sr04_rgb-led",
11+
"short_description": "Use an ultrasonic sensor (HC-SR04) to detect distance and display a visual indicator with an RGB LED.",
12+
"markdown_link": "README.md#model-atackeproximity-alerthc-sr04_rgb-led"
13+
}
14+
],
15+
"entrypoint": "dist/main",
16+
"first_run": "",
17+
"build": {
18+
"build": "./build.sh",
19+
"setup": "./setup.sh",
20+
"path": "dist/archive.tar.gz",
21+
"arch": [
22+
"linux/amd64",
23+
"linux/arm64"
24+
]
25+
}
26+
}

requirements.txt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
2+
viam-sdk==0.41.0
3+
4+
typing-extensions

setup.sh

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
#!/bin/sh
2+
cd `dirname $0`
3+
4+
# Create a virtual environment to run our code
5+
VENV_NAME="venv"
6+
PYTHON="$VENV_NAME/bin/python"
7+
ENV_ERROR="This module requires Python >=3.8, pip, and virtualenv to be installed."
8+
9+
if ! python3 -m venv $VENV_NAME >/dev/null 2>&1; then
10+
echo "Failed to create virtualenv."
11+
if command -v apt-get >/dev/null; then
12+
echo "Detected Debian/Ubuntu, attempting to install python3-venv automatically."
13+
SUDO="sudo"
14+
if ! command -v $SUDO >/dev/null; then
15+
SUDO=""
16+
fi
17+
if ! apt info python3-venv >/dev/null 2>&1; then
18+
echo "Package info not found, trying apt update"
19+
$SUDO apt -qq update >/dev/null
20+
fi
21+
$SUDO apt install -qqy python3-venv >/dev/null 2>&1
22+
if ! python3 -m venv $VENV_NAME >/dev/null 2>&1; then
23+
echo $ENV_ERROR >&2
24+
exit 1
25+
fi
26+
else
27+
echo $ENV_ERROR >&2
28+
exit 1
29+
fi
30+
fi
31+
32+
# remove -U if viam-sdk should not be upgraded whenever possible
33+
# -qq suppresses extraneous output from pip
34+
echo "Virtualenv found/created. Installing/upgrading Python packages..."
35+
$PYTHON -m pip install -r requirements.txt

0 commit comments

Comments
 (0)