Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
rlabbe
GitHub Repository: rlabbe/Kalman-and-Bayesian-Filters-in-Python
Path: blob/master/experiments/slamekf.py
1700 views
1
# -*- coding: utf-8 -*-
2
"""
3
Created on Sun Oct 9 08:25:01 2016
4
5
@author: roger
6
"""
7
8
import numpy as np
9
10
11
class WorldMap(object):
12
13
def __init__(self, N=100):
14
15
self.N = N
16
pass
17
18
19
20
21
def measurements(self, x, theta):
22
""" return array of measurements (range, angle) if robot is in position
23
x"""
24
25
N = 10
26
a = np.linspace(-np.pi, np.pi, self.N)
27
return a
28
29
30
31
def get_line(start, end):
32
"""Bresenham's Line Algorithm
33
Produces a list of tuples from start and end
34
35
>>> points1 = get_line((0, 0), (3, 4))
36
>>> points2 = get_line((3, 4), (0, 0))
37
>>> assert(set(points1) == set(points2))
38
>>> print points1
39
[(0, 0), (1, 1), (1, 2), (2, 3), (3, 4)]
40
>>> print points2
41
[(3, 4), (2, 3), (1, 2), (1, 1), (0, 0)]
42
43
source:
44
http://www.roguebasin.com/index.php?title=Bresenham%27s_Line_Algorithm
45
"""
46
# Setup initial conditions
47
x1, y1 = int(round(start[0])), int(round(start[1]))
48
x2, y2 = int(round(end[0])), int(round(end[1]))
49
dx = x2 - x1
50
dy = y2 - y1
51
52
# Determine how steep the line is
53
is_steep = abs(dy) > abs(dx)
54
55
# Rotate line
56
if is_steep:
57
x1, y1 = y1, x1
58
x2, y2 = y2, x2
59
60
# Swap start and end points if necessary and store swap state
61
swapped = False
62
if x1 > x2:
63
x1, x2 = x2, x1
64
y1, y2 = y2, y1
65
swapped = True
66
# Recalculate differentials
67
dx = x2 - x1
68
dy = y2 - y1
69
70
# Calculate error
71
error = int(dx / 2.0)
72
ystep = 1 if y1 < y2 else -1
73
74
# Iterate over bounding box generating points between start and end
75
y = y1
76
points = []
77
for x in range(x1, x2 + 1):
78
coord = (y, x) if is_steep else (x, y)
79
points.append(coord)
80
error -= abs(dy)
81
if error < 0:
82
y += ystep
83
error += dx
84
85
# Reverse the list if the coordinates were swapped
86
if swapped:
87
points.reverse()
88
return points
89
90
91
world = np.zeros((1000,1000), dtype=bool)
92
93
94
def add_line(p0, p1):
95
pts = get_line(p0, p1)
96
for p in pts:
97
try:
98
world[p[0], p[1]] = True
99
except:
100
pass # ignore out of range
101
102
103
add_line((0,0), (1000, 0))
104
105
def measure(x, theta):
106
107
dx,dy = world.shape
108
h = np.sqrt(2*(dx*dx + dy+dy))
109
p1 = [h*np.cos(theta), h*np.sin(theta)]
110
111
112
hits = get_line(x, p1)
113
114
try:
115
for pt in hits:
116
if world[pt[0], pt[1]]:
117
return pt
118
except:
119
return -1
120
return -2
121
122
123
124
125
measure([100,100], -np.pi/2)
126
127