|
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 | | - |
13 | 1 | import time as tm |
| 2 | +import traceback as tb |
14 | 3 |
|
15 | 4 | import simpylc as sp |
16 | 5 |
|
17 | 6 | class LidarPilot: |
18 | | - noObstacle = (sp.finity, 0) |
19 | | - |
20 | 7 | def __init__ (self): |
21 | | - self.lidar = self.world.visualisation.lidar |
| 8 | + print ('Press enter to start or stop...') |
| 9 | + |
| 10 | + ''' |
| 11 | + self.driveEnabled = False |
22 | 12 | |
23 | 13 | while True: |
24 | | - self.readInputs () |
| 14 | + self.input () |
25 | 15 | self.sweep () |
26 | | - self.writeOutputs () |
| 16 | + self.output () |
27 | 17 | 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 |
32 | 33 |
|
33 | 34 | for lidarAngle in range (-self.lidar.halfApertureAngle, self.lidar.halfApertureAngle): |
34 | 35 | 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 | + |
45 | 51 | 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) |
50 | 58 |
|
0 commit comments