Skip to content

Commit fe334c0

Browse files
committed
self -> sp in KeyboardPilot
1 parent 32f9fb3 commit fe334c0

File tree

12 files changed

+125
-153
lines changed

12 files changed

+125
-153
lines changed

simpylc/engine.py

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
import math
3232
import builtins
3333
import curses as cs
34+
import sys as ss
3435

3536
from .base import *
3637
from .gui import *
@@ -429,8 +430,7 @@ def run (self):
429430
cs.wrapper (self.main)
430431

431432
def main (self, window):
432-
self.Agent.world = world
433-
self.Agent.getKey = window.getkey # No typo
433+
ss.modules [self.Agent.__module__] .sp.getKey = window.getkey # No typo
434434
self.Agent ()
435435

436436
finity = 1e20
@@ -496,11 +496,9 @@ def snap (anObject, target, margin):
496496
def digit (anObject, index):
497497
return int (('000000000000' + str (int (evaluate (anObject)))) [-evaluate (index + 1)])
498498

499-
'''
500499
_print = print
501500

502501
def cursesCompatiblePrint (*args, **kwargs):
503502
_print (*args, **kwargs, end = '\n\r')
504503

505504
builtins.print = cursesCompatiblePrint
506-
'''

simpylc/simulations/car/default.track

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
- - - - - - - - - - - - - - - - - * - * - - - - - - * - * - - -
2121
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
2222
- - - - - - - - - * - * - - - - - - - - - - - - - - - - - - - -
23-
* - * - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
23+
- - * - * - - - - - - - - - - - - - - - - - - - - - - - - - - -
2424
- - - - - - - - - - - - - - - - - * - * - - - - - - * - * - - -
2525
- - - - - - - - - * - * - - - - - - - - - - - - - - - - - - - -
2626
- - - * - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
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+
- - - - - - - - - - - - - - - - - - - - - - - - * - - - - - - -
Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import simpylc as sp
2+
13
class KeyboardPilot:
24
def __init__ (self):
35
while True:
@@ -6,31 +8,31 @@ def __init__ (self):
68
self.writeOutputs ()
79

810
def readInputs (self):
9-
key = self.getKey ()
11+
key = sp.getKey ()
1012

11-
self.upKey = key == 'KEY_UP'
12-
self.downKey = key == 'KEY_DOWN'
1313
self.leftKey = key == 'KEY_LEFT'
1414
self.rightKey = key == 'KEY_RIGHT'
15+
self.upKey = key == 'KEY_UP'
16+
self.downKey = key == 'KEY_DOWN'
1517

16-
self.targetVelocityStep = self.world.control.targetVelocityStep
17-
self.steeringAngleStep = self.world.control.steeringAngleStep
18+
self.targetVelocityStep = sp.world.control.targetVelocityStep
19+
self.steeringAngleStep = sp.world.control.steeringAngleStep
1820

1921
def sweep (self):
20-
if self.upKey:
21-
self.targetVelocityStep += 1
22-
print ('Target velocity step: ', self.targetVelocityStep)
23-
elif self.downKey:
24-
self.targetVelocityStep -= 1
25-
print ('Target velocity step: ', self.targetVelocityStep)
26-
elif self.leftKey:
22+
if self.leftKey:
2723
self.steeringAngleStep += 1
2824
print ('Steering angle step: ', self.steeringAngleStep)
2925
elif self.rightKey:
3026
self.steeringAngleStep -= 1
3127
print ('Steering angle step: ', self.steeringAngleStep)
28+
elif self.upKey:
29+
self.targetVelocityStep += 1
30+
print ('Target velocity step: ', self.targetVelocityStep)
31+
elif self.downKey:
32+
self.targetVelocityStep -= 1
33+
print ('Target velocity step: ', self.targetVelocityStep)
3234

3335
def writeOutputs (self):
34-
self.world.control.targetVelocityStep.set (self.targetVelocityStep)
35-
self.world.control.steeringAngleStep.set (self.steeringAngleStep)
36+
sp.world.control.steeringAngleStep.set (self.steeringAngleStep)
37+
sp.world.control.targetVelocityStep.set (self.targetVelocityStep)
3638

simpylc/simulations/car/keyboard_world.py

Lines changed: 0 additions & 41 deletions
This file was deleted.
Lines changed: 43 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,50 +1,58 @@
1-
'''
2-
Inputs:
3-
4-
- A lidar distance array with a resolution of 1 degree
5-
- An x, y position and course angle of the car
6-
7-
Outpus:
8-
9-
- Acceleration stepper output
10-
11-
'''
12-
131
import time as tm
2+
import traceback as tb
143

154
import simpylc as sp
165

176
class LidarPilot:
18-
noObstacle = (sp.finity, 0)
19-
207
def __init__ (self):
21-
self.lidar = self.world.visualisation.lidar
8+
print ('Press enter to start or stop...')
9+
10+
'''
11+
self.driveEnabled = False
2212
2313
while True:
24-
self.readInputs ()
14+
self.input ()
2515
self.sweep ()
26-
self.writeOutputs ()
16+
self.output ()
2717
tm.sleep (0.02)
28-
29-
30-
def readInputs (self):
31-
self.nearestObstacles = [self.noObstacle, self.noObstacle]
18+
'''
19+
20+
def input (self):
21+
key = sp.getKey ()
22+
23+
if key == 'KEY_ENTER':
24+
self.driveEnabled = not self.driveEnabled
25+
26+
self.nearestObstacleDistance = sp.finity
27+
self.nearestObstacleAngle = 0
28+
29+
self.nextObstacleDistance = sp.finity
30+
self.nextObstacleAngle = 0
31+
32+
self.lidar = sp.world.visualisation.lidar
3233

3334
for lidarAngle in range (-self.lidar.halfApertureAngle, self.lidar.halfApertureAngle):
3435
lidarDistance = self.lidar.distances [lidarAngle]
35-
if lidarDistance < self.nearestObstacles [0][0]:
36-
self.nearestObstacles [0] = (lidarDistance, lidarAngle)
37-
elif lidarDistance < self.nearestObstacles [1][0]:
38-
self.nearestObstacles [1] = (lidarDistance, lidarAngle)
39-
40-
self.targetObstacle = sp.tsDiv (sp.tAdd (*self.nearestObstacles), 2)
41-
42-
self.velocity = self.world.physics.velocity
43-
self.steeringAngle = self.world.physics.steeringAngle
44-
36+
37+
if lidarDistance < self.nearestObstacleDistance:
38+
self.nextObstacleDistance = self.nearestObstacleDistance
39+
self.nextObstacleAngle = self.nearestObstacleAngle
40+
41+
self.nearestObstacleDistance = lidarDistance
42+
self.nearestObstacleAngle = lidarAngle
43+
44+
elif lidarDistance < self.nextObstacleDistance:
45+
self.nextObstacleDistance = lidarDistance
46+
self.nextObstacleAngle = lidarAngle
47+
48+
self.targetObstacleDistance = (self.nearestObstacleDistance + self.nextObstacleDistance) / 2
49+
self.targetObstacleAngle = (self.nearestObstacleAngle + self.nextObstacleAngle) / 2
50+
4551
def sweep (self):
46-
self.steeringAngle = self.targetObstacle [1]
47-
48-
def writeOutputs (self):
49-
self.world.physics.steeringAngle.set (self.steeringAngle)
52+
self.steeringAngle = self.targetObstacleAngle
53+
self.targetVelocity = (sp.abs (90 - self.steeringAngle) / 65) if self.driveEnabled else 0
54+
55+
def output (self):
56+
sp.world.physics.steeringAngle.set (self.steeringAngle)
57+
sp.world.physics.targetVelocity.set (self.targetVelocity)
5058

simpylc/simulations/car/lidar_pilot_sp.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,8 @@ def __init__ (self):
3636

3737
self.group ('inputs', True)
3838

39+
self.driveEnabled = sp.Marker ()
40+
3941
self.nearestObstacleDistance = sp.Register (sp.finity)
4042
self.nearestObstacleAngle = sp.Register (0)
4143

@@ -81,9 +83,10 @@ def input (self):
8183
self.velocity.set (sp.world.physics.velocity)
8284

8385
def sweep (self):
84-
self.steeringAngle = self.targetObstacleAngle
86+
self.steeringAngle.set (self.targetObstacleAngle)
87+
self.targetVelocity.set (sp.abs (90 - self.steeringAngle) / 65, self.driveEnabled, 0)
8588

8689
def output (self):
87-
sp.world.physics.targetVelocity.set (self.targetVelocity)
8890
sp.world.physics.steeringAngle.set (self.steeringAngle)
91+
sp.world.physics.targetVelocity.set (self.targetVelocity)
8992

simpylc/simulations/car/lidar_sp_world.py

Lines changed: 0 additions & 41 deletions
This file was deleted.

simpylc/simulations/car/physics.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ def __init__ (self):
5858
self.positionX = sp.Register ()
5959
self.positionY = sp.Register ()
6060

61-
self.radialAccelleration = sp.Register ()
61+
self.radialAcceleration = sp.Register ()
6262
self.slipping = sp.Marker ()
6363
self.radialVelocity = sp.Register ()
6464

@@ -79,9 +79,9 @@ def sweep (self):
7979
self.attitudeAngle.set (self.attitudeAngle + self.midAngularVelocity * sp.world.period)
8080
self.courseAngle.set (self.attitudeAngle + self.midSteeringAngle)
8181

82-
self.radialAccelleration.set (sp.max (abs (self.tangentialVelocity * self.tangentialVelocity * self.inverseMidCurveRadius) - 0.5, 0))
83-
self.slipping.mark (sp.abs (self.radialAccelleration) > 0.5)
84-
self.radialVelocity.set (self.radialVelocity + self.radialAccelleration * sp.world.period, self.slipping, 0)
82+
self.radialAcceleration.set (sp.max (abs (self.tangentialVelocity * self.tangentialVelocity * self.inverseMidCurveRadius) - 0.5, 0))
83+
self.slipping.mark (sp.abs (self.radialAcceleration) > 0.55)
84+
self.radialVelocity.set (self.radialVelocity + self.radialAcceleration * sp.world.period, self.slipping, 0)
8585

8686
self.velocityX.set (self.tangentialVelocity * sp.cos (self.courseAngle) + self.radialVelocity * sp.sin (self.courseAngle))
8787
self.velocityY.set (self.tangentialVelocity * sp.sin (self.courseAngle) + self.radialVelocity * sp.cos (self.courseAngle))

simpylc/simulations/car/timing.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,11 @@ def __init__ (self):
3131
sp.Chart.__init__ (self)
3232

3333
def define (self):
34-
pass
34+
self.channel (sp.world.physics.steeringAngle, sp.aqua, -90, 90, 100)
35+
self.channel (sp.world.physics.targetVelocity, sp.white, -1, 2, 100)
36+
self.channel (sp.world.physics.velocity, sp.yellow, -1, 2, 100)
37+
self.channel (sp.world.physics.radialAcceleration, sp.lime, -4, 4, 100)
38+
self.channel (sp.world.physics.slipping, sp.red)
39+
3540

3641

0 commit comments

Comments
 (0)