Skip to content

Instantly share code, notes, and snippets.

@jmeridth
Forked from PatrickHallek/dwm1001-localization.py
Last active April 21, 2024 16:55
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save jmeridth/0a45e86f7d234041759a9e33dd492638 to your computer and use it in GitHub Desktop.
Save jmeridth/0a45e86f7d234041759a9e33dd492638 to your computer and use it in GitHub Desktop.
code and setup to test 3 anchors and 1 tag decawave boards. Used Raspberry Pi 3B+
import json
import redis
import serial
import sys
import time
from art import text2art
if len(sys.argv) == 1:
print("Please provide a numeric value for the first down")
print("Example: python first_down.py 0.18")
sys.exit()
try:
first_down = float(sys.argv[1])
print("Initial First Down is at {}".format(first_down))
except ValueError:
print("Please provide a float value for the argument (first down)")
print("Example: python first_down.py 0.18")
sys.exit()
first_down_art = text2art(text="FIRST DOWN!", font="3d-diagonal")
# connect to redis on port 7001
r = redis.Redis(host='localhost', port=7001, db=0)
# connect to Decawave tag
DWM = serial.Serial(port="/dev/ttyACM0", baudrate=115200)
print("Connected to " + DWM.name)
# two carriage returns to initiate communication with Decawave tag
DWM.write("\r\r".encode())
print("Encode")
time.sleep(1)
# tell Decawave board to start sending data
# API referecen page 99 https://www.decawave.com/wp-content/uploads/2019/01/DWM1001-API-Guide-2.2.pdf
DWM.write("lec\r".encode())
print("Encode")
time.sleep(1)
try:
while True:
data = DWM.readline()
if(data):
print(data)
# if the data includes locations for each anchor parse it
if ("DIST".encode() in data and "AN0".encode() in data and "AN1".encode() in data and "AN2".encode() in data):
data = data.replace("\r\n".encode(), "".encode())
data = data.decode().split(",")
if("DIST" in data):
anchor_number = int(data[data.index("DIST")+1])
for i in range(anchor_nummber):
pos_AN = {"id": data[data.index("AN"+str(i))+1],
"x": data[data.index("AN"+str(i))+2],
"y": data[data.index("AN"+str(i))+3],
"z": data[data.index("AN"+str(i))+4],
"dist": data[data.index("AN"+str(i))+5]}
pos_AN = json.dumps(pos_AN)
print(pos_AN)
r.set('AN'+str(i), pos_AN)
# if there is position data for the tag
if("POS" in data):
x_pos = data[data.index("POS")+1]
y_pos = data[data.index("POS")+2]
z_pos = data[data.index("POS")+3]
pos = {"x": x_pos,
"y": y_pos,
"z": z_pos}
pos = json.dumps(pos)
print(pos)
if float(x_pos) > first_down:
print(first_down_art)
first_down += 0.2
print("New first down is at {}".format(first_down))
r.set("pos", pos)
DWM.write("\r".encode())
DWM.close()
except KeyboardInterrupt:
print("Stop")
art==4.6
pkg-resource==0..0.0
pyserial==3.4
redis==3.4.1
#!/bin/bash
docker run --name redis -p 7001:6379 -d redis
#!/bin/bash
# Used Hypriot OS 1.12.0 from
# https://github.com/hypriot/image-builder-rpi/releases/download/v1.12.0/hypriotos-rpi-v1.12.0.img.zip
# which includes python 3.8 by default and docker
# install vim editor and python 3 virtual environment tool
sudo apt-get install vim python3-env
# get pip so we can install python packages
curl https://bootstrap.pypa.io/get-pip.py -o get-pip.py
# install pip
python get-pip.py
# create virtual environment for python packages (pyserial and redis)
python -m venv venv
# start python virtual environment
source venv/bin/activate
# install python dependencies/packages (pyserial and redis)
pip install -r requirements.txt
# pull redis docker image from dockerhub and start container exposing
# port 7001 on host to container's 6379 port (redis default)
./run_redis_container.sh
# start the script to listen to the Decawave tag
# ensure to pass a float (decimal) value for the inital first down location
python first_down.py 0.0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment