-
Notifications
You must be signed in to change notification settings - Fork 12
/
vehicle.py
executable file
·396 lines (325 loc) · 12.2 KB
/
vehicle.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
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
#############
# vehicle.py
# This file contains classes to define a vehicle,
# including the shape, parameters, control execution and states
#
# Author: Jianyu Chen
# 2016
#############
from panda3d.bullet import BulletVehicle
from panda3d.bullet import ZUp
from panda3d.bullet import BulletBoxShape
from panda3d.core import Vec3
from panda3d.core import Vec4
from panda3d.core import TransformState
from panda3d.core import Point3
from panda3d.bullet import BulletRigidBodyNode
from direct.showbase.InputStateGlobal import inputState
import math
import numpy
# Set the chassis of the vehicle
def setChassis(game,position,velocity,length,width,height):
shape = BulletBoxShape(Vec3(width/2, length/2, height/2)) #The shape of the chassis
ts = TransformState.makePos(Point3(0, 0, 0.5))
np = game.worldNP.attachNewNode(BulletRigidBodyNode('Vehicle'))
np.node().addShape(shape, ts)
np.setPos(position)
np.node().setLinearVelocity(Vec3(0,velocity,0))
np.node().setMass(1000.0) # The mass of the vehicle
np.node().setDeactivationEnabled(False)
game.world.attachRigidBody(np.node())
return np
class basicVehicle(BulletVehicle):
def __init__(self,game,pos,vel,length,width,height,axisDis,wheelDis,radius,wheelH):
self.initCordPos=numpy.array([pos[0],pos[1]])
'''self.length=length
self.width=width
self.height=height
self.axisDis=axisDis
self.wheelDis=wheelDis
self.radius=radius
self.wheelH=wheelH'''
line=game.segLine
lineLen=0
prevPoint=line[0]
self.initNum=0
for point in line:
norm=numpy.linalg.norm(point-prevPoint)
if pos[0]>=lineLen and pos[0]<lineLen+norm:
ldirec=(point-prevPoint)/norm
rdirec=numpy.array([ldirec[1],-ldirec[0]])
post=prevPoint+(pos[0]-lineLen)*ldirec+rdirec*pos[1]
self.initPos=Vec3(post[0],post[1],pos[2])
break
lineLen=lineLen+norm
prevPoint=point
self.initNum=self.initNum+1
chassis=setChassis(game,self.initPos,vel,length,width,height)
BulletVehicle.__init__(self,game.world,chassis.node())
self.setCoordinateSystem(ZUp)
game.world.attachVehicle(self)
#self.yugoNP = loader.loadModel('models/yugo/yugo.egg')
self.yugoNP = loader.loadModel('models/car.egg')
self.yugoNP.reparentTo(chassis)
# Right front wheel
np = loader.loadModel('models/yugo/yugotireR.egg')
np.reparentTo(game.worldNP)
self.addWheel(Point3( wheelDis/2, axisDis/2, wheelH), True, np, radius)
# Left front wheel
np = loader.loadModel('models/yugo/yugotireL.egg')
np.reparentTo(game.worldNP)
self.addWheel(Point3(-wheelDis/2, axisDis/2, wheelH), True, np, radius)
# Right rear wheel
np = loader.loadModel('models/yugo/yugotireR.egg')
np.reparentTo(game.worldNP)
self.addWheel(Point3( wheelDis/2, -axisDis/2, wheelH), False, np, radius)
# Left rear wheel
np = loader.loadModel('models/yugo/yugotireL.egg')
np.reparentTo(game.worldNP)
self.addWheel(Point3(-wheelDis/2, -axisDis/2, wheelH), False, np, radius)
# Steering info
self.steering = 0.0 # degree
self.steeringClamp = 45.0 # degree
self.steeringIncrement = 10.0 # degree per second
def addWheel(self, pos, front, np, radius):
wheel = self.createWheel()
wheel.setNode(np.node())
wheel.setChassisConnectionPointCs(pos)
wheel.setFrontWheel(front)
wheel.setWheelDirectionCs(Vec3(0, 0, -1))
wheel.setWheelAxleCs(Vec3(1, 0, 0))
wheel.setWheelRadius(radius) # the radius of the wheel
wheel.setMaxSuspensionTravelCm(40.0)
wheel.setSuspensionStiffness(40.0)
wheel.setWheelsDampingRelaxation(2.3)
wheel.setWheelsDampingCompression(4.4)
wheel.setFrictionSlip(100.0);
wheel.setRollInfluence(0.1)
# process the input from the keyboard
def processInput(self, dt, up, back, left, right, brake):
engineForce = 0.0
brakeForce = 0.0
#direction=self.getForwardVector()
if inputState.isSet(up):
engineForce = 2000.0
brakeForce = 0.0
if inputState.isSet(back):
engineForce = -1000.0
brakeForce = 0.0
if inputState.isSet(brake):
engineForce = 0.0
brakeForce = 1000.0
if inputState.isSet(left):
self.steering += dt * self.steeringIncrement
self.steering = min(self.steering, self.steeringClamp)
elif inputState.isSet(right):
self.steering -= dt * self.steeringIncrement
self.steering = max(self.steering, -self.steeringClamp)
else:
self.steering=self.steering*0.7
# Apply steering to front wheels
self.setSteeringValue(self.steering, 0)
self.setSteeringValue(self.steering, 1)
# Apply engine and brake to rear wheels
self.applyEngineForce(engineForce, 2)
self.applyEngineForce(engineForce, 3)
self.setBrake(brakeForce, 2)
self.setBrake(brakeForce, 3)
# get the vehicle 3D position
def getPosVector(self):
return self.getChassis().getTransform().getPos()
# get the vehicle x-y position
def getPos(self):
return numpy.array([self.getPosVector()[0],self.getPosVector()[1]])
# get the vehicle heading
def getDirection(self):
return self.getForwardVector()
# get |v|
def getVelocity(self):
V=numpy.array([self.getChassis().getLinearVelocity()[0],self.getChassis().getLinearVelocity()[1]])
return numpy.linalg.norm(V)
# get v(in x-y plane)
def getVelocityVector(self):
return numpy.array([self.getChassis().getLinearVelocity()[0],self.getChassis().getLinearVelocity()[1]])
# get the yaw rate
def getAngleVelocity(self):
return self.getChassis().getAngularVelocity()[2]
# attach sensor to the vehicle
def setSensor(self,sensor):
self.sensor=sensor
# attach agent to the vehicle
def setAgent(self,agent):
self.agent=agent
# execute the control input given by agent
def controlInput(self,agentInput): #the input acc is m/s
engineForce = 0.0
brakeForce = 0.0
accGain=500
engineForce=agentInput[0]*accGain #transfer to engineForce
self.steering=agentInput[1] #In degree not rad
brakeForce=agentInput[2]
#print(brakeForce)
if engineForce>20*accGain:
engineForce=20*accGain
if engineForce<-35*accGain:
engineForce=-35*accGain
steeringLimit=45
if self.steering>steeringLimit:
self.steering=steeringLimit
if self.steering<-steeringLimit:
self.steering=-steeringLimit
# Apply steering to front wheels
self.setSteeringValue(self.steering, 0)
self.setSteeringValue(self.steering, 1)
# Apply engine and brake to rear wheels
self.applyEngineForce(engineForce, 2)
self.applyEngineForce(engineForce, 3)
self.setBrake(brakeForce, 2)
self.setBrake(brakeForce, 3)
class surroundingVehicle(BulletVehicle):
def __init__(self,game,pos,vel,length,width,height,axisDis,wheelDis,radius,wheelH):
self.initCordPos=numpy.array([pos[0],pos[1]])
'''self.length=length
self.width=width
self.height=height
self.axisDis=axisDis
self.wheelDis=wheelDis
self.radius=radius
self.wheelH=wheelH'''
line=game.segLine
lineLen=0
prevPoint=line[0]
self.initNum=0
for point in line:
norm=numpy.linalg.norm(point-prevPoint)
if pos[0]>=lineLen and pos[0]<lineLen+norm:
ldirec=(point-prevPoint)/norm
rdirec=numpy.array([ldirec[1],-ldirec[0]])
post=prevPoint+(pos[0]-lineLen)*ldirec+rdirec*pos[1]
self.initPos=Vec3(post[0],post[1],pos[2])
break
lineLen=lineLen+norm
prevPoint=point
self.initNum=self.initNum+1
chassis=setChassis(game,self.initPos,vel,length,width,height)
BulletVehicle.__init__(self,game.world,chassis.node())
self.setCoordinateSystem(ZUp)
game.world.attachVehicle(self)
self.yugoNP = loader.loadModel('models/yugo/yugo.egg')
#self.yugoNP = loader.loadModel('models/car.egg')
self.yugoNP.reparentTo(chassis)
# Right front wheel
np = loader.loadModel('models/yugo/yugotireR.egg')
np.reparentTo(game.worldNP)
self.addWheel(Point3( wheelDis/2, axisDis/2, wheelH), True, np, radius)
# Left front wheel
np = loader.loadModel('models/yugo/yugotireL.egg')
np.reparentTo(game.worldNP)
self.addWheel(Point3(-wheelDis/2, axisDis/2, wheelH), True, np, radius)
# Right rear wheel
np = loader.loadModel('models/yugo/yugotireR.egg')
np.reparentTo(game.worldNP)
self.addWheel(Point3( wheelDis/2, -axisDis/2, wheelH), False, np, radius)
# Left rear wheel
np = loader.loadModel('models/yugo/yugotireL.egg')
np.reparentTo(game.worldNP)
self.addWheel(Point3(-wheelDis/2, -axisDis/2, wheelH), False, np, radius)
# Steering info
self.steering = 0.0 # degree
self.steeringClamp = 45.0 # degree
self.steeringIncrement = 10.0 # degree per second
def addWheel(self, pos, front, np, radius):
wheel = self.createWheel()
wheel.setNode(np.node())
wheel.setChassisConnectionPointCs(pos)
wheel.setFrontWheel(front)
wheel.setWheelDirectionCs(Vec3(0, 0, -1))
wheel.setWheelAxleCs(Vec3(1, 0, 0))
wheel.setWheelRadius(radius) # the radius of the wheel
wheel.setMaxSuspensionTravelCm(40.0)
wheel.setSuspensionStiffness(40.0)
wheel.setWheelsDampingRelaxation(2.3)
wheel.setWheelsDampingCompression(4.4)
wheel.setFrictionSlip(100.0);
wheel.setRollInfluence(0.1)
# process the input from the keyboard
def processInput(self, dt, up, back, left, right, brake):
engineForce = 0.0
brakeForce = 0.0
#direction=self.getForwardVector()
if inputState.isSet(up):
engineForce = 2000.0
brakeForce = 0.0
if inputState.isSet(back):
engineForce = -1000.0
brakeForce = 0.0
if inputState.isSet(brake):
engineForce = 0.0
brakeForce = 1000.0
if inputState.isSet(left):
self.steering += dt * self.steeringIncrement
self.steering = min(self.steering, self.steeringClamp)
elif inputState.isSet(right):
self.steering -= dt * self.steeringIncrement
self.steering = max(self.steering, -self.steeringClamp)
else:
self.steering=self.steering*0.7
# Apply steering to front wheels
self.setSteeringValue(self.steering, 0)
self.setSteeringValue(self.steering, 1)
# Apply engine and brake to rear wheels
self.applyEngineForce(engineForce, 2)
self.applyEngineForce(engineForce, 3)
self.setBrake(brakeForce, 2)
self.setBrake(brakeForce, 3)
# get the vehicle 3D position
def getPosVector(self):
return self.getChassis().getTransform().getPos()
# get the vehicle x-y position
def getPos(self):
return numpy.array([self.getPosVector()[0],self.getPosVector()[1]])
# get the vehicle heading
def getDirection(self):
return self.getForwardVector()
# get |v|
def getVelocity(self):
V=numpy.array([self.getChassis().getLinearVelocity()[0],self.getChassis().getLinearVelocity()[1]])
return numpy.linalg.norm(V)
# get v(in x-y plane)
def getVelocityVector(self):
return numpy.array([self.getChassis().getLinearVelocity()[0],self.getChassis().getLinearVelocity()[1]])
# get the yaw rate
def getAngleVelocity(self):
return self.getChassis().getAngularVelocity()[2]
# attach sensor to the vehicle
def setSensor(self,sensor):
self.sensor=sensor
# attach agent to the vehicle
def setAgent(self,agent):
self.agent=agent
# execute the control input given by agent
def controlInput(self,agentInput): #the input acc is m/s
engineForce = 0.0
brakeForce = 0.0
accGain=500
engineForce=agentInput[0]*accGain #transfer to engineForce
self.steering=agentInput[1] #In degree not rad
brakeForce=agentInput[2]
#print(brakeForce)
if engineForce>5.5*accGain:
engineForce=5.5*accGain
if engineForce<-35*accGain:
engineForce=-35*accGain
steeringLimit=45
if self.steering>steeringLimit:
self.steering=steeringLimit
if self.steering<-steeringLimit:
self.steering=-steeringLimit
# Apply steering to front wheels
self.setSteeringValue(self.steering, 0)
self.setSteeringValue(self.steering, 1)
# Apply engine and brake to rear wheels
self.applyEngineForce(engineForce, 2)
self.applyEngineForce(engineForce, 3)
self.setBrake(brakeForce, 2)
self.setBrake(brakeForce, 3)