-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathSimulator.py
209 lines (165 loc) · 4.95 KB
/
Simulator.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
198
199
200
201
202
203
204
205
206
207
208
# Import all libraries
import sys
import serial
from pandac.PandaModules import *
loadPrcFileData("", "window-title FlySim: The Flight Simulator")
loadPrcFileData("", "fullscreen 0")
import direct.directbase.DirectStart
from panda3d.core import Fog
from direct.showbase.DirectObject import DirectObject
from direct.interval.MetaInterval import Sequence
from direct.interval.LerpInterval import LerpFunc
from direct.interval.FunctionInterval import Func
from direct.task import Task
# Define constants
TUNNEL_SEGMENT_LENGTH = 50
TUNNEL_TIME = 15
X_STABLE = 320.0
Y_STABLE = 260.0
MAX_TILT = 30
MAX_LIFT = 20
# The World class
class World(DirectObject):
# Initialize the world class
def __init__(self):
# A few window settings
base.disableMouse()
camera.setPosHpr(0, 0.5, 10, 0, -100, 0) #Vary this
base.setBackgroundColor(0, 0.5, 1)
# Load the Boeing707
self.scale = 0.04
self.xPos = 0.0
self.yPos = 0.0
self.tilt = 0.0
self.lift = 0.0
self.plane = loader.loadModel('./models/plane/boeing707')
self.plane.reparentTo(render)
self.plane.setScale(self.scale, self.scale, self.scale)
self.plane.setPosHpr(self.xPos, -0.7 + self.yPos, 0, 0, 270 + self.lift, self.tilt)
# Load fog into the view
self.fog = Fog('distanceFog')
self.fog.setColor(0, 0.5, 1)
self.fog.setExpDensity(.08)
render.setFog(self.fog)
# Load the tunnel and keep it running infinitely
self.initTunnel()
self.contTunnel()
# Key mappings
self.accept('escape', sys.exit)
self.accept('+', self.scaleUp)
self.accept('-', self.scaleDown)
try:
self.ser = serial.Serial('/dev/ttyUSB0', 9600)
taskMgr.add(self.serialTask, "serialTask")
except:
print("Could not open Serial port")
# The serial task
def serialTask(self, task):
reading = self.ser.readline()
if(reading != ""):
try:
if(reading[0] == 'y'):
x = float(reading[1:])
devX = (X_STABLE - x)
if(devX < -25):
self.moveLeft()
elif(devX > 25):
self.moveRight()
else:
self.stabilizeTilt()
if(reading[0] == 'x'):
y = float(reading[1:])
devY = (Y_STABLE - y)
if(devY < -25):
self.liftDown()
elif(devY > 25):
self.liftUp()
else:
self.stabilizeLift()
except:
pass
return Task.cont
# Make the Boeing stable on the tilt
def stabilizeTilt(self):
if(self.tilt > 0):
if(self.tilt != 0.0):
self.tilt = self.tilt - 0.25
else:
if(self.tilt != 0.0):
self.tilt = self.tilt + 0.25
self.plane.setPosHpr(self.xPos, -0.7 + self.yPos, 0, 0, 270 + self.lift, self.tilt)
# Make the Boeing stable on the lift
def stabilizeLift(self):
if(self.lift > 0):
if(self.lift != 0.0):
self.lift = self.lift - 0.25
else:
if(self.lift != 0.0):
self.lift = self.lift + 0.25
self.lift.setPosHpr(self.xPos, -0.7 + self.yPos, 0, 0, 270 + self.lift, self.tilt)
# Zoom into the plane
def scaleUp(self):
self.scale = self.scale + 0.005
self.plane.setScale(self.scale, self.scale, self.scale)
# Zoom out of the plane
def scaleDown(self):
self.scale = self.scale - 0.005
self.plane.setScale(self.scale, self.scale, self.scale)
# Move the plane right
def moveRight(self):
if(self.tilt >= MAX_TILT):
self.tilt = MAX_TILT
self.xPos = self.xPos + 0.01
self.tilt = self.tilt + 0.25
self.plane.setPosHpr(self.xPos, -0.7 + self.yPos, 0, 0, 270 + self.lift, self.tilt)
# Move the plane left
def moveLeft(self):
if(self.tilt <= -MAX_TILT):
self.tilt = -MAX_TILT
self.tilt = self.tilt - 0.25
self.xPos = self.xPos - 0.01
self.plane.setPosHpr(self.xPos, -0.7 + self.yPos, 0, 0, 270 + self.lift, self.tilt)
# Lift the plane up
def liftUp(self):
if(self.lift >= MAX_LIFT):
self.lift = MAX_LIFT
self.lift = self.lift + 0.25
self.yPos = self.yPos + 0.01
self.plane.setPosHpr(self.xPos, -0.7 + self.yPos, 0, 0, 270 + self.lift, self.tilt)
# Lift the plane down
def liftDown(self):
if(self.lift <= -MAX_LIFT):
self.lift = -MAX_LIFT
self.lift = self.lift - 0.25
self.yPos = self.yPos - 0.01
self.plane.setPosHpr(self.xPos, -0.7 + self.yPos, 0, 0, 270 + self.lift, self.tilt)
# The tunnel initialization function
def initTunnel(self):
self.tunnel = [None for i in range(4)]
for x in range(4):
self.tunnel[x] = loader.loadModel('models/terrain/tunnel')
if x == 0:
self.tunnel[x].reparentTo(render)
else:
self.tunnel[x].reparentTo(self.tunnel[x-1])
self.tunnel[x].setPos(0, 0, -TUNNEL_SEGMENT_LENGTH)
# Function to keep the tunnel running infinite
def contTunnel(self):
self.tunnel = self.tunnel[1:]+ self.tunnel[0:1]
self.tunnel[0].setZ(0)
self.tunnel[0].reparentTo(render)
self.tunnel[0].setScale(.455, .455, .605)
self.tunnel[3].reparentTo(self.tunnel[2])
self.tunnel[3].setZ(-TUNNEL_SEGMENT_LENGTH)
self.tunnel[3].setScale(1)
self.tunnelMove = Sequence(
LerpFunc(self.tunnel[0].setZ,
duration = TUNNEL_TIME,
fromData = 0,
toData = TUNNEL_SEGMENT_LENGTH*.305),
Func(self.contTunnel)
)
self.tunnelMove.start()
# Load the world
w = World()
run()