Skip to content

Commit 3892e7c

Browse files
committed
Self driving car working on Linux, not yet tested on windows
1 parent 5a341c4 commit 3892e7c

File tree

4 files changed

+37
-25
lines changed

4 files changed

+37
-25
lines changed

simpylc/engine.py

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -430,12 +430,19 @@ def run (self):
430430
cs.wrapper (self.main)
431431

432432
def main (self, window):
433-
def _print (*args):
433+
def print (*args):
434434
window.addstr (' '.join (str (arg) for arg in args) + '\n')
435+
436+
def getKey ():
437+
try:
438+
return window.getkey ()
439+
except:
440+
return ''
435441

442+
window.nodelay (True)
436443
agentModule = ss.modules [self.Agent.__module__]
437-
agentModule.sp.getKey = window.getkey # No typo
438-
agentModule.print = _print
444+
agentModule.print = print
445+
agentModule.sp.getKey = getKey
439446
self.Agent ()
440447

441448
finity = 1e20

simpylc/simulations/car/keyboard_pilot.py

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,18 @@
1+
import time as tm
2+
13
import simpylc as sp
24

35
class KeyboardPilot:
46
def __init__ (self):
5-
print ('test', 1, 2, 3, 4)
7+
print ('Use arrow keys to control speed and direction')
68

79
while True:
8-
self.readInputs ()
10+
self.input ()
911
self.sweep ()
10-
self.writeOutputs ()
12+
self.output ()
13+
tm.sleep (0.02)
1114

12-
def readInputs (self):
15+
def input (self):
1316
key = sp.getKey ()
1417

1518
self.leftKey = key == 'KEY_LEFT'
@@ -34,7 +37,7 @@ def sweep (self):
3437
self.targetVelocityStep -= 1
3538
print ('Target velocity step: ', self.targetVelocityStep)
3639

37-
def writeOutputs (self):
40+
def output (self):
3841
sp.world.control.steeringAngleStep.set (self.steeringAngleStep)
3942
sp.world.control.targetVelocityStep.set (self.targetVelocityStep)
4043

simpylc/simulations/car/lidar_pilot.py

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -5,34 +5,37 @@
55

66
class LidarPilot:
77
def __init__ (self):
8-
print ('Press enter to start or stop...')
8+
print ('Use up arrow to start, down arrow to stop')
99

10-
'''
1110
self.driveEnabled = False
1211

1312
while True:
1413
self.input ()
1514
self.sweep ()
1615
self.output ()
1716
tm.sleep (0.02)
18-
'''
1917

2018
def input (self):
2119
key = sp.getKey ()
2220

23-
if key == 'KEY_ENTER':
24-
self.driveEnabled = not self.driveEnabled
21+
if key == 'KEY_UP':
22+
self.driveEnabled = True
23+
elif key == 'KEY_DOWN':
24+
self.driveEnabled = False
2525

26+
self.lidarDistances = sp.world.visualisation.lidar.distances
27+
self.lidarHalfApertureAngle = sp.world.visualisation.lidar.halfApertureAngle
28+
29+
30+
def sweep (self):
2631
self.nearestObstacleDistance = sp.finity
2732
self.nearestObstacleAngle = 0
2833

2934
self.nextObstacleDistance = sp.finity
3035
self.nextObstacleAngle = 0
3136

32-
self.lidar = sp.world.visualisation.lidar
33-
34-
for lidarAngle in range (-self.lidar.halfApertureAngle, self.lidar.halfApertureAngle):
35-
lidarDistance = self.lidar.distances [lidarAngle]
37+
for lidarAngle in range (-self.lidarHalfApertureAngle, self.lidarHalfApertureAngle):
38+
lidarDistance = self.lidarDistances [lidarAngle]
3639

3740
if lidarDistance < self.nearestObstacleDistance:
3841
self.nextObstacleDistance = self.nearestObstacleDistance
@@ -48,10 +51,9 @@ def input (self):
4851
self.targetObstacleDistance = (self.nearestObstacleDistance + self.nextObstacleDistance) / 2
4952
self.targetObstacleAngle = (self.nearestObstacleAngle + self.nextObstacleAngle) / 2
5053

51-
def sweep (self):
5254
self.steeringAngle = self.targetObstacleAngle
53-
self.targetVelocity = (sp.abs (90 - self.steeringAngle) / 65) if self.driveEnabled else 0
54-
55+
self.targetVelocity = (sp.abs (90 - self.steeringAngle) / 80) if self.driveEnabled else 0
56+
5557
def output (self):
5658
sp.world.physics.steeringAngle.set (self.steeringAngle)
5759
sp.world.physics.targetVelocity.set (self.targetVelocity)

simpylc/simulations/car/world.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -42,11 +42,11 @@
4242
import timing as tm
4343

4444
sp.World (
45-
ct.Control,
46-
kp.KeyboardPilot,
47-
#lp.LidarPilot,
48-
#ls.LidarPilotSp,
45+
# ct.Control,
46+
# kp.KeyboardPilot,
47+
lp.LidarPilot,
48+
# ls.LidarPilotSp,
4949
ps.Physics,
5050
vs.Visualisation,
51-
tm.Timing
51+
# tm.Timing
5252
)

0 commit comments

Comments
 (0)