Class: DrivingPhysics::Gearbox

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

Overview

The gear ratio (above 1) multiplies speed and divides torque 5000 RPM is around 375 MPH with a standard wheel/tire 3.73 final drive reduces that to around 100 mph in e.g. 5th gear (1.0) Likewise, 1st gear is a smaller gear ratio than 3rd

Defined Under Namespace

Classes: ClutchDisengage, Disengaged

Constant Summary collapse

RATIOS =
[1/5r, 2/5r, 5/9r, 5/7r, 1r, 5/4r]
FINAL_DRIVE =

1/3.73

11
REVERSE =
-1
REVERSE_RATIO =
-1/

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(env) {|_self| ... } ⇒ Gearbox

Returns a new instance of Gearbox.

Yields:

  • (_self)

Yield Parameters:



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/driving_physics/gearbox.rb', line 35

def initialize(env)
  @ratios = RATIOS
  @final_drive = FINAL_DRIVE
  @gear = 0     # neutral
  @clutch = 1.0 # fully engaged (clutch pedal out)

  # represent all rotating mass
  @spinner = Disk.new(env) { |m|
    m.radius = 0.15
    m.base_friction = 5.0/1000
    m.omega_friction = 15.0/100_000
    m.mass = 15
  }
  @fixed_mass = 30 # kg

  yield self if block_given?
end

Instance Attribute Details

#clutchObject

Returns the value of attribute clutch.



24
25
26
# File 'lib/driving_physics/gearbox.rb', line 24

def clutch
  @clutch
end

#final_driveObject

Returns the value of attribute final_drive.



23
24
25
# File 'lib/driving_physics/gearbox.rb', line 23

def final_drive
  @final_drive
end

#fixed_massObject

Returns the value of attribute fixed_mass.



23
24
25
# File 'lib/driving_physics/gearbox.rb', line 23

def fixed_mass
  @fixed_mass
end

#gearObject

Returns the value of attribute gear.



24
25
26
# File 'lib/driving_physics/gearbox.rb', line 24

def gear
  @gear
end

#ratiosObject

Returns the value of attribute ratios.



23
24
25
# File 'lib/driving_physics/gearbox.rb', line 23

def ratios
  @ratios
end

#spinnerObject

Returns the value of attribute spinner.



23
24
25
# File 'lib/driving_physics/gearbox.rb', line 23

def spinner
  @spinner
end

Class Method Details

.gear_interval!(gear, min: REVERSE, max:) ⇒ Object

Raises:

  • (ArgumentError)


26
27
28
29
30
31
32
33
# File 'lib/driving_physics/gearbox.rb', line 26

def self.gear_interval!(gear, min: REVERSE, max:)
  if gear < min or gear > max
    raise(ArgumentError, format("gear %s should be between %d and %d",
                                gear.inspect, min, max))
  end
  raise(ArgumentError, "gear should be an integer") if !gear.is_a? Integer
  gear
end

Instance Method Details

#alpha(torque, omega: 0) ⇒ Object

given torque, apply friction, determine alpha



58
59
60
# File 'lib/driving_physics/gearbox.rb', line 58

def alpha(torque, omega: 0)
  @spinner.alpha(torque + @spinner.rotating_friction(omega))
end

#axle_omega(crank_rpm, axle_omega: nil) ⇒ Object

take into account the old axle_omega and @clutch



118
119
120
121
122
123
124
125
126
# File 'lib/driving_physics/gearbox.rb', line 118

def axle_omega(crank_rpm, axle_omega: nil)
  new_axle_omega = DrivingPhysics.omega(crank_rpm) * self.ratio
  if axle_omega.nil?
    raise(ClutchDisengage, "cannot determine axle omega") if @clutch != 1.0
    return new_axle_omega
  end
  diff = new_axle_omega - axle_omega
  axle_omega + diff * @clutch
end

#axle_torque(crank_torque) ⇒ Object



106
107
108
# File 'lib/driving_physics/gearbox.rb', line 106

def axle_torque(crank_torque)
  crank_torque * @clutch / self.ratio
end

#crank_rpm(axle_omega, crank_rpm: nil) ⇒ Object

take into account the old crank_rpm and @clutch crank will tolerate mismatch more than axle



130
131
132
133
134
135
136
137
# File 'lib/driving_physics/gearbox.rb', line 130

def crank_rpm(axle_omega, crank_rpm: nil)
  new_crank_rpm = DrivingPhysics.rpm(axle_omega) / self.ratio
  if crank_rpm.nil?
    raise(ClutchDisengage, "cannot determine crank rpm") if @clutch != 1.0
    return new_crank_rpm
  end
  crank_rpm + (new_crank_rpm - crank_rpm) * @clutch
end

#implied_torque(alpha) ⇒ Object



66
67
68
# File 'lib/driving_physics/gearbox.rb', line 66

def implied_torque(alpha)
  @spinner.implied_torque(alpha)
end

#inputsObject



90
91
92
# File 'lib/driving_physics/gearbox.rb', line 90

def inputs
  format("Gear: %d  Clutch: %.1f%%", @gear, @clutch * 100)
end

#massObject



70
71
72
# File 'lib/driving_physics/gearbox.rb', line 70

def mass
  @fixed_mass + @spinner.mass
end

#output_torque(crank_torque, crank_rpm, axle_omega: nil) ⇒ Object



110
111
112
113
114
115
# File 'lib/driving_physics/gearbox.rb', line 110

def output_torque(crank_torque, crank_rpm, axle_omega: nil)
  axle_alpha = self.alpha(self.axle_torque(crank_torque),
                          omega: self.axle_omega(crank_rpm,
                                                 axle_omega: axle_omega))
  self.implied_torque(axle_alpha)
end

#ratio(gear = nil) ⇒ Object



94
95
96
97
98
99
100
101
102
103
104
# File 'lib/driving_physics/gearbox.rb', line 94

def ratio(gear = nil)
  gear ||= @gear
  case gear
  when REVERSE
    REVERSE_RATIO * @final_drive
  when 0
    raise(Disengaged, "Cannot determine gear ratio")
  else
    @ratios.fetch(gear - 1) * @final_drive
  end
end

#rotating_friction(omega) ⇒ Object



62
63
64
# File 'lib/driving_physics/gearbox.rb', line 62

def rotating_friction(omega)
  @spinner.rotating_friction(omega)
end

#to_sObject



82
83
84
85
86
87
88
# File 'lib/driving_physics/gearbox.rb', line 82

def to_s
  [self.inputs,
   format("Ratios: %s", @ratios.inspect),
   format(" Final: %s  Mass: %.1f kg  Rotating: %.1f kg",
          @final_drive.inspect, self.mass, @spinner.mass),
  ].join("\n")
end

#top_gearObject



78
79
80
# File 'lib/driving_physics/gearbox.rb', line 78

def top_gear
  @ratios.length
end