-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsteering.py
66 lines (45 loc) · 1.52 KB
/
steering.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
import math
#follows cartesian system
#iDirAngle - Initial movement angle
#iSpeedX - Initial Speed along X
#iSpeedY - Inital Speed along Y (|speed| < Limit? )
#abPedal - Acceleration and Break Pedal (-5 to +4)
#stAngle - Steering Angle (-60 to 60)
#Globals
NORM = 3
abMAX = 4
speedMAX = 50;
# timeInterval = 0.1 seconds by default
def getTotalSpeed(xSpeed, ySpeed):
return math.sqrt( xSpeed**2 + ySpeed**2 )
def getAcDc(multiplier):
return multiplier*abMAX
def addSpeeds(a, b):
toReturn = a + b
#to set a limit on speed
# if toReturn > speedMAX:
# return speedMAX
if toReturn < 0:
return 0
else:
return toReturn
def sgn(x):
if (x == 0):
return 0
else:
return x/abs(x)
def getSpeedComps(spd, theta):
return spd * math.cos( math.radians(theta) ), spd * math.sin( math.radians(theta) )
def getNewSpeed(iDirAngle, iSpeedX, iSpeedY, abPedal, stAngle, timeInterval = 0.1):
acc = getAcDc(abPedal)
deltaSpeed = acc * timeInterval / NORM
oldSpeed = getTotalSpeed(iSpeedX, iSpeedY)
newSpeed = addSpeeds(oldSpeed, deltaSpeed)
turnDirection = sgn(stAngle)
# perpAngle = ( iDirAngle + (turnDirection * 90) ) % 360 #angle perp to iDirAngle
newDirAngle = iDirAngle - (stAngle/NORM)
newSpeedX, newSpeedY = getSpeedComps(newSpeed, newDirAngle)
return newDirAngle, newSpeedX, newSpeedY
def rotateCenter(obj, rotatedImage):
r = rotatedImage.get_rect().center
return obj.x + 15 - r[0], obj.y + 30 - r[1]