Class: DrivingPhysics::Car

Inherits:
Object
  • Object
show all
Defined in:
lib/driving_physics/car.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(tire:, powertrain:) {|_self| ... } ⇒ Car

Returns a new instance of Car.

Yields:

  • (_self)

Yield Parameters:



9
10
11
12
13
14
15
16
17
18
19
# File 'lib/driving_physics/car.rb', line 9

def initialize(tire:, powertrain:)
  @num_tires = 4
  @tire = tire
  @env = @tire.env
  @powertrain = powertrain
  @body_mass = 1000.0
  @frontal_area = DrivingPhysics::FRONTAL_AREA
  @cd = DrivingPhysics::DRAG_COF

  yield self if block_given?
end

Instance Attribute Details

#body_massObject

Returns the value of attribute body_mass.



7
8
9
# File 'lib/driving_physics/car.rb', line 7

def body_mass
  @body_mass
end

#cdObject

Returns the value of attribute cd.



7
8
9
# File 'lib/driving_physics/car.rb', line 7

def cd
  @cd
end

#envObject (readonly)

Returns the value of attribute env.



6
7
8
# File 'lib/driving_physics/car.rb', line 6

def env
  @env
end

#frontal_areaObject

Returns the value of attribute frontal_area.



7
8
9
# File 'lib/driving_physics/car.rb', line 7

def frontal_area
  @frontal_area
end

#num_tiresObject

Returns the value of attribute num_tires.



7
8
9
# File 'lib/driving_physics/car.rb', line 7

def num_tires
  @num_tires
end

#powertrainObject (readonly)

Returns the value of attribute powertrain.



6
7
8
# File 'lib/driving_physics/car.rb', line 6

def powertrain
  @powertrain
end

#tireObject (readonly)

Returns the value of attribute tire.



6
7
8
# File 'lib/driving_physics/car.rb', line 6

def tire
  @tire
end

Instance Method Details

#air_force(speed) ⇒ Object

force opposing speed



42
43
44
# File 'lib/driving_physics/car.rb', line 42

def air_force(speed)
  -0.5 * @frontal_area * @cd * @env.air_density * speed ** 2
end

#corner_massObject



105
106
107
# File 'lib/driving_physics/car.rb', line 105

def corner_mass
  self.total_mass / @num_tires
end

#drive_force(rpm) ⇒ Object



89
90
91
# File 'lib/driving_physics/car.rb', line 89

def drive_force(rpm)
  @tire.force(@powertrain.axle_torque(rpm))
end

#gearObject



29
30
31
# File 'lib/driving_physics/car.rb', line 29

def gear
  @powertrain.gearbox.gear
end

#gear=(val) ⇒ Object



33
34
35
# File 'lib/driving_physics/car.rb', line 33

def gear=(val)
  @powertrain.gearbox.gear = val
end

#motor_rpm(tire_speed) ⇒ Object



97
98
99
# File 'lib/driving_physics/car.rb', line 97

def motor_rpm(tire_speed)
  @powertrain.gearbox.crank_rpm(tire_speed / @tire_radius)
end

#normal_forceObject

per tire



110
111
112
# File 'lib/driving_physics/car.rb', line 110

def normal_force
  self.corner_mass * @env.g
end

#throttleObject



21
22
23
# File 'lib/driving_physics/car.rb', line 21

def throttle
  @powertrain.motor.throttle
end

#throttle=(val) ⇒ Object



25
26
27
# File 'lib/driving_physics/car.rb', line 25

def throttle=(val)
  @powertrain.motor.throttle = val
end

#tire_inertial_force(force) ⇒ Object

force of opposite sign to force



61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/driving_physics/car.rb', line 61

def tire_inertial_force(force)
  mag = force.abs
  sign = force / mag
  force_loss = 0
  5.times {
    # use magnitude, so we are subtracting only positive numbers
    acc = DrivingPhysics.acc(mag - force_loss, self.total_mass)
    alpha = acc / @tire.radius
    # this will be a positive number
    force_loss = @num_tires * @tire.implied_torque(alpha) /
                 @tire.radius
  }
  # oppose initial force
  -1 * sign * force_loss
end

#tire_rolling_force(omega) ⇒ Object

force of opposite sign to omega



47
48
49
50
51
# File 'lib/driving_physics/car.rb', line 47

def tire_rolling_force(omega)
  @num_tires *
    @tire.rolling_friction(omega, normal_force: self.normal_force) /
    @tire.radius
end

#tire_rotational_force(omega) ⇒ Object

force of opposite sign to omega



54
55
56
57
58
# File 'lib/driving_physics/car.rb', line 54

def tire_rotational_force(omega)
  @num_tires *
    @tire.rotating_friction(omega, normal_force: self.normal_force) /
    @tire.radius
end

#tire_speed(rpm) ⇒ Object



93
94
95
# File 'lib/driving_physics/car.rb', line 93

def tire_speed(rpm)
  @tire.tangential(@powertrain.axle_omega(rpm))
end

#tire_tractionObject

per tire



115
116
117
# File 'lib/driving_physics/car.rb', line 115

def tire_traction
  @tire.traction(self.normal_force)
end

#to_sObject



77
78
79
80
81
82
83
84
85
86
87
# File 'lib/driving_physics/car.rb', line 77

def to_s
  [[format("Mass: %.1f kg", self.total_mass),
    format("Fr.A: %.2f m^2", @frontal_area),
    format("cD: %.2f", @cd),
   ].join(' | '),
   format("POWERTRAIN:\n%s", @powertrain),
   format("Tires: %s", @tire),
   format("Corner mass: %.1f kg | Normal force: %.1f N",
          self.corner_mass, self.normal_force),
  ].join("\n")
end

#top_gearObject



37
38
39
# File 'lib/driving_physics/car.rb', line 37

def top_gear
  @powertrain.gearbox.top_gear
end

#total_massObject



101
102
103
# File 'lib/driving_physics/car.rb', line 101

def total_mass
  @body_mass + @powertrain.mass + @tire.mass * @num_tires
end

#total_normal_forceObject



119
120
121
# File 'lib/driving_physics/car.rb', line 119

def total_normal_force
  self.total_mass * env.g
end