← Back to ratslair.com
aboutsummaryrefslogtreecommitdiff
path: root/DWM.py
blob: 39c599befb0048c72f8cde7fd560dd75c916afd2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
import serial
import os
from time import sleep
from filterpy.kalman import UnscentedKalmanFilter
from filterpy.kalman import MerweScaledSigmaPoints
import numpy as np
import re
from collections import deque

def fx(x, t):
    return x

def hx(x):
    return x

x_mu = -0.02163601775523146
x_std = 0.07074315964054628
y_mu = 0.02645106742760512
y_std = 0.07415316805017082

points = MerweScaledSigmaPoints(n=2, alpha=1, beta=2, kappa=0)

kf = UnscentedKalmanFilter(dim_x=2, dim_z=2, dt=0.1, fx=fx, hx=hx, points=points)

# Initial position
kf.x = np.array([0., 0.])

# Initial error
kf.P *= 1000

# Noise matrix
kf.R = np.diag([25, 25])

kf.Q = np.eye(2)

position = deque([(0, 0)])

updRate = "1 1" # Active Idle

with serial.Serial('/dev/ttyACM0', 115200, timeout = 1) as s:

    # print(f"Opened serial port {s.name}")

    sleep(1)
    s.write(b"\r")
    sleep(0.1)
    s.write(b"\r")

    s.write(b'nis ')
    sleep(0.1)
    s.write(b'0x1234')
    sleep(0.1)
    s.write(b'\r')
    # print(f"Set PAN ID to 0x1234")

    sleep(1)
    
    s.write(b'nmt')
    sleep(0.1)
    s.write(b'\r')
    # print("Configured node as tag")

    sleep(1)

    s.write(b"\r")
    sleep(0.1)
    s.write(b"\r")

    sleep(2)

    s.write(b"aurs ")
    sleep(0.1)
    s.write(updRate.encode())
    sleep(0.1)
    s.write(b'\r')
    # print("Set update rate to " + updRate)

    sleep(0.1)
    s.write(b"lep")
    sleep(0.1)
    s.write(b"\r")
    sleep(0.1)

    for i in range(30):
        s.readline()

    for i in range(50):
        position.append((0, 0))
    
    while True:
        dstr = s.readline().decode('utf-8').strip('\n')

        if "POS," in dstr:
            dstr = re.sub('[^0-9,.]', '', dstr)
            _, x, y, z, qf = dstr.split(",")
            x = int(float(x) * 1000)
            y = int(float(y) * 1000)
            kf.predict()
            kf.update(np.array([x, y]))

            kfx = int(kf.x[0])
            kfy = int(kf.x[1])

            position.popleft()
            position.append((x, y))

            xmean = 0
            ymean = 0
            
            for p_i in position:
                xmean += p_i[0]
                ymean += p_i[1]

            xmean = int(xmean / 50)
            ymean = int(ymean / 50)

            with open("position_buf.txt", "w") as f:
                print(f"{kfx},{kfy}")
                f.write(f"{kfx},{kfy}\n")
            os.rename("position_buf.txt", "position.txt")

            with open("position_mean_buf.txt", "w") as f:
                #print(f"xmean: {xmean}, ymean: {ymean}")
                f.write(f"{xmean},{ymean}\n")
            os.rename("position_mean_buf.txt", "position_mean.txt")

print("Shutting down serial communication")