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: Disengaged

Constant Summary collapse

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

1/3.73

11

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

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

Returns a new instance of Gearbox.

Yields:

  • (_self)

Yield Parameters:



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/driving_physics/gearbox.rb', line 22

def initialize(env)
  @ratios = RATIOS
  @final_drive = FINAL_DRIVE
  @gear = 0 # neutral

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

  yield self if block_given?
end

Instance Attribute Details

#final_driveObject

Returns the value of attribute final_drive.



20
21
22
# File 'lib/driving_physics/gearbox.rb', line 20

def final_drive
  @final_drive
end

#fixed_massObject

Returns the value of attribute fixed_mass.



20
21
22
# File 'lib/driving_physics/gearbox.rb', line 20

def fixed_mass
  @fixed_mass
end

#gearObject

Returns the value of attribute gear.



20
21
22
# File 'lib/driving_physics/gearbox.rb', line 20

def gear
  @gear
end

#ratiosObject

Returns the value of attribute ratios.



20
21
22
# File 'lib/driving_physics/gearbox.rb', line 20

def ratios
  @ratios
end

#spinnerObject

Returns the value of attribute spinner.



20
21
22
# File 'lib/driving_physics/gearbox.rb', line 20

def spinner
  @spinner
end

Instance Method Details

#alpha(torque, omega: 0) ⇒ Object

given torque, apply friction, determine alpha



40
41
42
# File 'lib/driving_physics/gearbox.rb', line 40

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

#axle_omega(crank_rpm) ⇒ Object



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

def axle_omega(crank_rpm)
  DrivingPhysics.omega(crank_rpm) * self.ratio
end

#axle_torque(crank_torque) ⇒ Object



73
74
75
# File 'lib/driving_physics/gearbox.rb', line 73

def axle_torque(crank_torque)
  crank_torque / self.ratio
end

#crank_rpm(axle_omega) ⇒ Object



81
82
83
# File 'lib/driving_physics/gearbox.rb', line 81

def crank_rpm(axle_omega)
  DrivingPhysics.rpm(axle_omega) / self.ratio
end

#implied_torque(alpha) ⇒ Object



48
49
50
# File 'lib/driving_physics/gearbox.rb', line 48

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

#massObject



52
53
54
# File 'lib/driving_physics/gearbox.rb', line 52

def mass
  @fixed_mass + @spinner.mass
end

#match_rpms(old_rpm, new_rpm) ⇒ Object



85
86
87
88
89
90
91
92
93
94
95
96
97
# File 'lib/driving_physics/gearbox.rb', line 85

def match_rpms(old_rpm, new_rpm)
  diff = new_rpm - old_rpm
  proportion = diff.to_f / old_rpm
  if proportion.abs < 0.01
    [:ok, proportion]
  elsif proportion.abs < 0.1
    [:slip, proportion]
  elsif @gear == 1 and new_rpm < old_rpm and old_rpm <= 1500
    [:get_rolling, proportion]
  else
    [:mismatch, proportion]
  end
end

#next_gear(rpm, floor: 2500, ceiling: 6400) ⇒ Object



99
100
101
102
103
104
105
106
107
# File 'lib/driving_physics/gearbox.rb', line 99

def next_gear(rpm, floor: 2500, ceiling: 6400)
  if rpm < floor and @gear > 1
    @gear - 1
  elsif rpm > ceiling and @gear < self.top_gear
    @gear + 1
  else
    @gear
  end
end

#ratio(gear = nil) ⇒ Object

Raises:



67
68
69
70
71
# File 'lib/driving_physics/gearbox.rb', line 67

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

#rotating_friction(omega) ⇒ Object



44
45
46
# File 'lib/driving_physics/gearbox.rb', line 44

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

#to_sObject



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

def to_s
  [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



56
57
58
# File 'lib/driving_physics/gearbox.rb', line 56

def top_gear
  @ratios.length
end