-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathphysics_models.py
More file actions
380 lines (246 loc) · 15 KB
/
physics_models.py
File metadata and controls
380 lines (246 loc) · 15 KB
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
import numpy as np
from math import cos, sin, atan2, hypot
from utils import wrapTo2Pi, loadConfigFile
import json
class WindState(object):
def __init__(self, windDirection, windSpeed):
self._dir = windDirection # radian, east north up
self._spd = windSpeed # m/s
def direction(self):
return self._dir
def speed(self):
return self._spd
class PhysicsModel:
# X and y are in UTM coordinates, the heading is in radians
def __init__(self, x=0, y=0, heading=0):
self._x = x
self._y = y
self._heading = heading # [-pi, pi] east north up
self._rotationSpeed = 0
self._speed = 0
def utmCoordinate(self):
return (self._x, self._y)
def heading(self):
return self._heading
def speed(self):
return self._speed
def simulate(self, timeDelta, trueWind):
raise NotImplementedError("Must override simulate method!")
class mainBoatPhysicsModel(PhysicsModel):
def __init__(self, x = 0, y = 0, heading = 0, configPath = 'Janet_config.json' ):
self._x = x
self._y = y
self._heading = heading
self._rotationSpeed = 0
self._speed = 0
self._apparentWind = WindState(0, 0)
def apparentWind(self):
return self._apparentWind
def updateApparentWind(self, trueWind ):
apparentWindVector = [trueWind.speed() * cos( trueWind.direction() - self._heading ) - self._speed, trueWind.speed() * sin( trueWind.direction() - self._heading )]
apparentWindAngle = atan2(apparentWindVector[1], apparentWindVector[0])
apparentWindSpeed = hypot(apparentWindVector[0], apparentWindVector[1])
self._apparentWind = WindState( apparentWindAngle, apparentWindSpeed )
def forceOnRudder(self):
return self._rudderLift * self._speed * sin( self._rudderAngle )
def calculateDrift(self, trueWind ):
x_boatVelocity = self._speed * cos(self._heading)
y_boatVelocity = self._speed * sin(self._heading)
x_windDrift = self._driftCoefficient * trueWind.speed() * cos( trueWind.direction() )
y_windDrift = self._driftCoefficient * trueWind.speed() * sin( trueWind.direction() )
x_dot = x_boatVelocity + x_windDrift
y_dot = y_boatVelocity + y_windDrift
return (x_dot, y_dot)
class SailingPhysicsModel(mainBoatPhysicsModel):
# X and y are in UTM coordinates, the heading is in radians
def __init__(self, x = 0, y = 0, heading = 0, configPath = 'Janet_config.json'):
self._x = np.float64(x)
self._y = np.float64(y)
self._heading = np.float64(heading)
self._rotationSpeed = np.float64(0)
self._speed = 0
config = loadConfigFile(configPath)
# sail parameters
self._sailAngle = 0
self._sailLift = config["sailLift"] # kg s^-1
#rudder parameters
self._rudderAngle = 0
self._rudderLift = config["rudderLift"] # kg s^-1
self._distanceToRudder = config["distanceToRudder"] # m
self._rudderBreakCoefficient = config["rudderBreakCoefficient"]
# wind parameters
self._apparentWind = WindState(0, 0)
# parameters for dynamic
self._driftCoefficient = config["driftCoefficient"]
self._tangentialFriction = config["tangentialFriction"] # kg s^-1
self._angularFriction = config["angularFriction"] # kg m
self._distanceToSailCoE = config["distanceToSailCoE"] # m
self._distanceToMast = config["distanceToMast"] # m
self._boatMass = config["boatMass"] # kg
self._momentOfInertia = config["momentOfInertia"] # kg m^2
def setActuators(self, sail, rudder):
self._sailAngle = sail
self._rudderAngle = rudder
def getActuators(self):
return ( self._sailAngle, self._rudderAngle )
def simulate(self, timeDelta, trueWind):
(x_dot, y_dot) = self.calculateDrift( trueWind )
self.updateApparentWind( trueWind )
self.calculateCorrectSailAngle()
# The acceleration of the boat is affected by three forces, the wind on the sail, a braking force
# from the water on the rudder, and a tangential friction force
sailForce = self.forceOnSails()
rudderForce = self.forceOnRudder()
sailAccelerationForce = sailForce * sin(self._sailAngle)
rudderBrakeForce = self._rudderBreakCoefficient * rudderForce * sin(self._rudderAngle)
tangentialFictionForce = np.sign(self._speed) * (self._tangentialFriction * (self._speed)**2)
acceleration_dot = ( (sailAccelerationForce - rudderBrakeForce) - tangentialFictionForce) / self._boatMass
sailRotationForce = sailForce * ( self._distanceToSailCoE - self._distanceToMast * cos(self._sailAngle ))
rudderRotationForce = self._distanceToRudder * rudderForce * cos(self._rudderAngle)
rotationForce = self._angularFriction * self._rotationSpeed * abs( self._speed )
rotationSpeed_dot = (sailRotationForce - rudderRotationForce - rotationForce) / self._momentOfInertia
self._x += x_dot * timeDelta
self._y += y_dot * timeDelta
self._heading += self._rotationSpeed * timeDelta
self._speed += acceleration_dot * timeDelta
self._rotationSpeed += rotationSpeed_dot * timeDelta
self._heading = wrapTo2Pi(self._heading)
# Ensures the sail is on the correct side of the boat
def calculateCorrectSailAngle(self):
if sin( self._apparentWind.direction() ) == 0:
self._sailAngle = min( abs(wrapTo2Pi(np.pi - self._apparentWind.direction())), abs(wrapTo2Pi(self._sailAngle)) )
else:
self._sailAngle = - np.sign( sin( self._apparentWind.direction() ) ) * min( abs(wrapTo2Pi(np.pi - self._apparentWind.direction())), abs(wrapTo2Pi(self._sailAngle)) )
def forceOnSails(self):
return self._sailLift * self._apparentWind.speed() * sin( self._sailAngle - self._apparentWind.direction() )
# ASPire model has been modified, apparent wind is calculated in world coord., wingsail angle is set manually depending on tail angle. Only MW lift and drag considered, not tail.
# MWAngle and apparentWind kept so as not to need changes in other parts of simulator.
class ASPirePhysicsModel(mainBoatPhysicsModel):
def __init__(self,x = 0, y = 0, heading = 0 , configPath = 'ASPire_config.json' , MWAngleStart = 0):
self._x = np.float64(x)
self._y = np.float64(y)
self._heading = np.float64(heading)
self._rotationSpeed = np.float64(0)
self._speed = 0
self._MWAngleStart = MWAngleStart
self._wingSail_config = 'wingsail_config.json' # default
config = loadConfigFile(configPath)
# rudder parameters
self._rudderAngle = 0
self._rudderLift = config["rudderLift"] # kg s^-1
self._distanceToRudder = config["distanceTorudder"] # m
self._rudderBreakCoefficient = config["rudderBreakCoefficient"]
# Wind parameters
self._apparentWind = WindState(0, 0) # apparent wind in boat coord
self._apparentWindGlobalCoord = WindState(0, 0) # apparent wind in world coord.
# parameters for dynamic
self._driftCoefficient = config["driftCoefficient"]
self._tangentialFriction = config["tangentialFriction"] # kg s^-1
self._angularFriction = config["angularFriction"] # kg m
self._distanceToSailCoE = config["distanceToSailCoE"] # m
self._distanceToMast = config["distanceToMast"] # m
self._boatMass = config["boatMass"] # kg
self._momentOfInertia = config["momentOfInertia"] # kg m^2
if "wingSailConfigPath" in config.keys():
self._wingSail_config = config["wingSailConfigPath"]
self._tailAngle = 0 # angle of tail with regards to main wing center line, + to the right, rad
self._MWAngleG = 0 # angle of main wing in with regards to world coord. syst., east=0 north=pi/2
self._MWAngleB = 0 # angle of main wing with regards to boat centerline, + to the right
self._alpha = 0.30 # chosen angle of attack for simple simulation
# wingsail parameters:
RHO = 1 # density of air
config = loadConfigFile('wingsail_config.json')
# Main Wing parameters
self._MWChord = config["mainWingChord"] # m
self._MWSpan = config["mainWingSpan"] # m
self._MWAspectRatio = self._MWChord/self._MWSpan
self._MWThickness = config["mainWingThickness"] # m
self._MWArea = self._MWThickness*self._MWSpan
self._MWConstPartWindForce = (1/2)*RHO*self._MWArea
self._MWDesignedLiftCoefficient = 2*np.pi*self._MWAspectRatio/(self._MWAspectRatio+2)
self._dontKnowHowToName = (self._MWAspectRatio+1)/(self._MWAspectRatio+2)
# Tail parameters
self._tailAngle = 0
self._tailChord = config["tailChord"] # m
self._tailSpan = config["tailSpan"] # m
self._tailAspectRatio = self._tailChord/self._tailSpan
self._tailThickness = config["tailThickness"] # m
self._tailArea = self._tailThickness*self._tailSpan
self._tailDistanceToMast = config["tailDistanceToMast"] #m
self._tailConstPartWindForce = 1/2*RHO*self._tailArea
self._tailDesignedLiftCoefficient = 2*np.pi*self._tailAspectRatio/(self._tailAspectRatio+2)
def setActuators(self, tail, rudder):
self._tailAngle = tail
self._rudderAngle = rudder
def getActuators(self):
return ( self._tailAngle, self._rudderAngle )
# updates apparent wind ASP (aka apparent wind in world coord)
def updateApparentWindGlobalCoord(self, trueWind ):
Xaw = trueWind.speed()*cos(trueWind.direction()) - self.speed()*cos(self.heading())
Yaw = trueWind.speed()*sin(trueWind.direction()) - self.speed()*sin(self.heading())
apparentWindVector = [Xaw,Yaw]
apparentWindSpeed = np.sqrt(Xaw**2 + Yaw**2)
if Xaw > 0 : #necessary since arctan maps to -pi/2 ; pi/2
apparentWindAngle = wrapTo2Pi(np.arctan(Yaw/Xaw))
elif Xaw < 0 :
apparentWindAngle = wrapTo2Pi(np.arctan(Yaw/Xaw) + np.pi)
elif Xaw == 0 :
if Yaw < 0 :
apparentWindAngle = -np.pi/2
elif Yaw > 0 :
apparentWindAngle = np.pi/2
self._apparentWindGlobalCoord = WindState( apparentWindAngle, apparentWindSpeed )
def apparentWindGlobalCoord(self):
return self._apparentWindGlobalCoord()
def MWAngle(self):
return self._MWAngleB
def simulate(self,timeDelta,trueWind):
(x_dot, y_dot) = self.calculateDrift( trueWind )
self.updateApparentWind( trueWind )
self.updateApparentWindGlobalCoord( trueWind )
liftForceMW = self._MWConstPartWindForce*(self._apparentWindGlobalCoord.speed()**2)*self._MWDesignedLiftCoefficient*abs(wrapTo2Pi(self._alpha))*5.91*10
dragForceMW = self._MWConstPartWindForce*(self._apparentWindGlobalCoord.speed()**2)*self._MWDesignedLiftCoefficient*abs(wrapTo2Pi(self._alpha))**2*10*5.91/2
MWAngleGint = wrapTo2Pi(self._apparentWindGlobalCoord.direction()+np.pi) # initially set wingsail main wing facing into the wind, then set angle of attack depending on tail
if self._tailAngle < 0 :
self._MWAngleG = wrapTo2Pi(MWAngleGint + self._alpha) # lift force is -90dg from drag force
elif self._tailAngle > 0 :
self._MWAngleG = wrapTo2Pi(MWAngleGint - self._alpha) # lift force is 90dg from drag force
liftForceMW = -liftForceMW
else :
self._MWAngleG = MWAngleGint
liftForceMW = 0
# angle of wingsail with regards to boat
self._MWAngleB = wrapTo2Pi(self._MWAngleG -self._heading)
# lift and drag of main wing projected onto x axis of boat (in direction of movement forward)
wingSailForce = -dragForceMW * cos(self._apparentWindGlobalCoord.direction()-self.heading()) + liftForceMW * (sin(self._apparentWindGlobalCoord.direction()-self.heading()))
#print('wignsailforce',wingSailForce)
#print('liftforce',liftForceMW)
#print('dragForceMW',dragForceMW)
#print('app wind direc',self._apparentWindGlobalCoord.direction())
#print('app wind speed',self._apparentWindGlobalCoord.speed())
rudderForce = self.forceOnRudder()
rudderBrakeForce = np.sign(self._speed) * self._rudderBreakCoefficient * rudderForce * np.abs(sin( wrapTo2Pi(self._rudderAngle) )) # always against speed
tangentialFictionForce = np.sign(self._speed) * self._tangentialFriction * (self._speed)**2 # always agaisnt speed
speed_dot = ( wingSailForce - rudderBrakeForce - tangentialFictionForce) / self._boatMass
rudderRotationForce = self._distanceToRudder * rudderForce * cos( wrapTo2Pi(self._rudderAngle) )
rotationForce = self._angularFriction * self._rotationSpeed * abs( self._speed )
rotationSpeed_dot = (- rudderRotationForce - rotationForce) / self._momentOfInertia
self._x += x_dot * timeDelta
self._y += y_dot * timeDelta
self._heading += self._rotationSpeed * timeDelta
self._speed += speed_dot * timeDelta
self._rotationSpeed += rotationSpeed_dot * timeDelta
self._heading = wrapTo2Pi(self._heading)
class SimplePhysicsModel(PhysicsModel):
# X and y are in UTM coordinates, the heading is in radians
def __init__(self, heading=0, speed=0, x=0, y=0):
self._x = x
self._y = y
self._heading = heading
self._rotationSpeed = 0
self._speed = speed
def simulate(self, timeDelta, trueWind):
velocityX = self._speed * cos(self._heading)
velocityY = self._speed * sin(self._heading)
self._x += velocityX * timeDelta
self._y += velocityY * timeDelta