Class: DrivingPhysics::Cockpit

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

Constant Summary collapse

CLUTCH_MIN =
0.1
REV_MATCH =
0.02
REV_SLIP =
0.1
MODE =
{
  normal: 0,
  sport: 1,
  sport_plus: 2,
}
MODE_LABELS =
MODE.invert

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(car) {|_self| ... } ⇒ Cockpit

Returns a new instance of Cockpit.

Yields:

  • (_self)

Yield Parameters:



46
47
48
49
50
51
52
53
54
55
56
# File 'lib/driving_physics/cockpit.rb', line 46

def initialize(car)
  @car = car
  @brake_pedal = 0.0    # to 1.0
  @steering_wheel = 0.0 # -1.0 to +1.0
  @min_rpm = @car.powertrain.motor.idle + 1000
  @max_rpm = @car.powertrain.motor.redline

  self.mode = 0             # normal

  yield self if block_given?
end

Instance Attribute Details

#brake_pedalObject

Returns the value of attribute brake_pedal.



43
44
45
# File 'lib/driving_physics/cockpit.rb', line 43

def brake_pedal
  @brake_pedal
end

#carObject (readonly)

Returns the value of attribute car.



43
44
45
# File 'lib/driving_physics/cockpit.rb', line 43

def car
  @car
end

#max_rpmObject

Returns the value of attribute max_rpm.



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

def max_rpm
  @max_rpm
end

#min_rpmObject

Returns the value of attribute min_rpm.



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

def min_rpm
  @min_rpm
end

#modeObject

Returns the value of attribute mode.



43
44
45
# File 'lib/driving_physics/cockpit.rb', line 43

def mode
  @mode
end

#mode_labelObject (readonly)

Returns the value of attribute mode_label.



43
44
45
# File 'lib/driving_physics/cockpit.rb', line 43

def mode_label
  @mode_label
end

#steering_wheelObject

Returns the value of attribute steering_wheel.



43
44
45
# File 'lib/driving_physics/cockpit.rb', line 43

def steering_wheel
  @steering_wheel
end

Class Method Details

.generateObject



33
34
35
36
37
38
39
40
41
# File 'lib/driving_physics/cockpit.rb', line 33

def self.generate
  e = Environment.new
  t = Tire.new(e)
  m = Motor.new(e)
  g = Gearbox.new(e)
  p = Powertrain.new(motor: m, gearbox: g)
  c = Car.new(tire: t, powertrain: p)
  self.new(c)
end

.mode_label(idx) ⇒ Object



15
16
17
# File 'lib/driving_physics/cockpit.rb', line 15

def self.mode_label(idx)
  MODE_LABELS.fetch(idx)
end

.rev_match(old_rpm, new_rpm) ⇒ Object

return [:status, recommended clutch, proportional difference]



20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/driving_physics/cockpit.rb', line 20

def self.rev_match(old_rpm, new_rpm)
  diff = new_rpm - old_rpm
  proportion = diff.to_f / old_rpm
  clutch = [1.0 - proportion.abs, CLUTCH_MIN].max
  if proportion.abs <= REV_MATCH
    [:match, 1.0, proportion]
  elsif proportion.abs <= REV_SLIP
    [:slip, clutch, proportion]
  else
    [:mismatch, clutch, proportion]
  end
end

Instance Method Details

#brake_pctObject



72
73
74
# File 'lib/driving_physics/cockpit.rb', line 72

def brake_pct
  self.brake_pedal * 100
end

#choose_gear(rpm) ⇒ Object



154
155
156
157
158
159
160
161
162
163
164
165
# File 'lib/driving_physics/cockpit.rb', line 154

def choose_gear(rpm)
  min, max = *self.rpm_range
  gear = self.gear

  if rpm < min and gear > 1
    gear - 1
  elsif rpm > max and gear < @car.top_gear
    gear + 1
  else
    gear
  end
end

#clutch_pctObject



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

def clutch_pct
  self.clutch_pedal * 100
end

#clutch_pedalObject



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

def clutch_pedal
  1.0 - @car.clutch
end

#clutch_pedal=(val) ⇒ Object

clutch pedal has inverse relationship with the clutch itself clutch pedal DISENGAGES the clutch; pedal at 0.0 means clutch at 1.0



68
69
70
# File 'lib/driving_physics/cockpit.rb', line 68

def clutch_pedal=(val)
  @car.clutch = 1.0 - DrivingPhysics.unit_interval!(val)
end

#gearObject



103
104
105
# File 'lib/driving_physics/cockpit.rb', line 103

def gear
  @car.gear
end

#gear=(val) ⇒ Object



107
108
109
# File 'lib/driving_physics/cockpit.rb', line 107

def gear=(val)
  @car.gear = val
end

#pedal_modeObject

return :normal, :sport, :sport_plus based on pedal positions



125
126
127
128
129
130
131
132
133
# File 'lib/driving_physics/cockpit.rb', line 125

def pedal_mode
  if self.brake_pedal < 0.4 and self.throttle_pedal < 0.5
    MODE[:normal]
  elsif self.brake_pedal < 0.7 and self.throttle_pedal < 0.8
    MODE[:sport]
  else
    MODE[:sport_plus]
  end
end

#rpm_rangeObject



135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
# File 'lib/driving_physics/cockpit.rb', line 135

def rpm_range
  if @mode < MODE[:sport_plus] and self.pedal_mode == MODE[:sport_plus]
    mode = @mode + 1
  elsif @mode == MODE[:sport_plus] and self.pedal_mode == MODE[:normal]
    mode = MODE[:sport]
  else
    mode = @mode
  end

  case mode
  when MODE[:normal]
    [@min_rpm, [@min_rpm + 3000, @max_rpm - 1000].min]
  when MODE[:sport]
    [@min_rpm + 1000, @max_rpm - 1000]
  when MODE[:sport_plus]
    [@min_rpm + 1500, @max_rpm]
  end
end

#steering_pctObject



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

def steering_pct
  self.steering_wheel * 100
end

#throttle_pctObject



85
86
87
# File 'lib/driving_physics/cockpit.rb', line 85

def throttle_pct
  self.throttle_pedal * 100
end

#throttle_pedalObject



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

def throttle_pedal
  @car.throttle
end

#throttle_pedal=(val) ⇒ Object



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

def throttle_pedal=(val)
  @car.throttle = DrivingPhysics.unit_interval!(val)
end

#to_sObject



116
117
118
119
120
121
122
# File 'lib/driving_physics/cockpit.rb', line 116

def to_s
  [format("Clutch: %d%%  Brake: %d%%  Throttle: %d%%",
          self.clutch_pct, self.brake_pct, self.throttle_pct),
   format("Wheel: %d%%  Gear: %d  Mode: [%d] %s",
          self.steering_pct, self.gear, self.mode, self.mode_label)
  ].join("\n")
end