-
Notifications
You must be signed in to change notification settings - Fork 1
/
sim3.py
197 lines (161 loc) · 6.63 KB
/
sim3.py
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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
import tkinter as tk
import time
import math
import random
from insec import *
#thingy1 dimensions 50x50
WIDTH = 1200
HEIGHT = 1000
# number of past readings to find line best fit
ARANGE = 4
# noise coefficient
NOISE = 3
class thingy1(object):
def __init__(self, canvas, vx, vy, spawnx, spawny):
self.canvas = canvas
self.id = canvas.create_rectangle(spawnx-25, spawny+25, spawnx+25, spawny-25, fill="red")
self.vx = vx
self.vy = vy
self.x = spawnx
self.y = spawny
def move(self):
x1, y1, x2, y2 = self.canvas.bbox(self.id)
if x2 > WIDTH:
self.vx *= -1
if x1 < 0:
self.vx *= -1
if y2 > HEIGHT:
self.vy *= -1
if y1 < 0:
self.vy *= -1
self.canvas.move(self.id,self.vx,self.vy)
def givesides(self):
x1, y1, x2, y2 = self.canvas.bbox(self.id)
return [((x1, y1),(x2, y1)),((x1,y1),(x1,y2)),((x2,y1),(x2,y2)),((x1,y2),(x2,y2))]
class App(object):
def __init__(self,master):
self.master = master
self.canvas = tk.Canvas(root, width = WIDTH, height = HEIGHT)
self.canvas.pack()
self.me = self.canvas.create_rectangle(550,400,650,600, fill="blue")
self.base = (600, 600)
self.distance = []
#ADD LIDAR LINES HERE, left to right
# define line segments, from base to whereever
self.lasers = [(self.base, (0, 600)),
(self.base, (0,650)),
(self.base, (0,700)),
(self.base, (0,750)),
(self.base, (0,800)),
(self.base, (0,850)),
(self.base, (0,900)),
(self.base, (0,950)),
(self.base, (0,1000)),
(self.base, (50,1000)),
(self.base, (100, 1000)),
(self.base, (150, 1000)),
(self.base, (200, 1000)),
(self.base, (250, 1000)),
(self.base, (300, 1000)),
(self.base, (350, 1000)),
(self.base, (400, 1000)),
(self.base, (450, 1000)),
(self.base, (500, 1000)),
(self.base, (550, 1000)),
(self.base, (600, 1000)),
(self.base, (650,1000)),
(self.base, (700, 1000)),
(self.base, (750, 1000)),
(self.base, (800, 1000)),
(self.base, (850, 1000)),
(self.base, (900, 1000)),
(self.base, (950, 1000)),
(self.base, (1000, 1000)),
(self.base, (1050, 1000)),
(self.base, (1100, 1000)),
(self.base, (1150,1000)),
(self.base, (1200, 1000)),
(self.base, (1200, 950)),
(self.base, (1200, 900)),
(self.base, (1200, 850)),
(self.base, (1200, 800)),
(self.base, (1200, 750)),
(self.base, (1200, 700)),
(self.base, (1200, 650)),
(self.base, (1200,600))]
for dims in self.lasers:
self.canvas.create_line(dims[0][0], dims[0][1], dims[1][0], dims[1][1])
#self.distance.append(10000)
for y in range(ARANGE):
tempArr = []
for x in range(20):
tempArr.append(10000)
self.distance.append(tempArr)
#add obstacles here if necessary
self.things = [thingy1(self.canvas, 2,2,300,600)]#, thingy1(self.canvas, 3, 1, 300,300)]
self.canvas.pack()
self.master.after(0,self.animation)
#determines distance from object if there is one in field of view
def scan(self):
disArray = []
for x in range(20):
disArray.append(10000)
for laser in self.lasers:
mindist = 10000
xIntersect = -1
for obj in self.things: #for each object
sides = obj.givesides()
for side in sides: #for each side of the object
ic = intersects(side, laser)
if (ic == (-1,-1)):
tempd = 10000
else:
#tempd = math.sqrt((self.base[0] - ic[0])**2 + (self.base[1]-ic[1])**2)
tempd = ic[1] - self.base[1] + random.random() * NOISE - NOISE/2
if tempd < mindist:
mindist = tempd
xIntersect = ic[0]
#disArray.append(mindist)
if (xIntersect != -1):
arrayX = int(xIntersect / 60)
if disArray[arrayX] > mindist:
disArray[arrayX] = mindist
return disArray
#linreg formula, might need to edit later
def compareDistance(self, curframe):
for numlaser in range(len(curframe)):
n = ARANGE + 1
sumxy = 0
for x in range(ARANGE):
sumxy += self.distance[x][numlaser] * x
sumxy += curframe[numlaser] * ARANGE
sumx = 0
for x in range(ARANGE):
sumx += x
sumx += ARANGE
sumy = 0
for x in range(ARANGE):
sumy += self.distance[x][numlaser]
sumy += curframe[numlaser]
sumx2 = 0
for x in range(ARANGE):
sumx2 += x * x
sumx2 += ARANGE * ARANGE
slope = (n * sumxy - sumx * sumy)/(n * sumx2 - sumx * sumx)
if (slope < 0 and abs(slope) < 100):
self.canvas.itemconfig(self.me, fill='red')
print(slope)
#moves the GUI by one frame and makes calculations, calls itself when it's done
def animation(self):
self.canvas.itemconfig(self.me, fill = 'blue')
for thing in self.things:
thing.move()
currentframe = self.scan()
self.compareDistance(currentframe)
self.distance.append(currentframe)
self.distance.pop(0)
self.master.after(10,self.animation)
if __name__ == "__main__":
root = tk.Tk()
canvas = App(root)
root.mainloop()