Class: DrivingPhysics::TorqueCurve

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

Constant Summary collapse

RPMS =
[500, 1000, 1500, 2000, 2500, 3500, 5000, 6000, 7000, 7100]
TORQUES =
[  0,   70,  130,  200,  250,  320,  330,  320,  260,    0]
RPM_IDX =
{
  min: 0,
  idle: 1,
  redline: -2,
  max: -1,
}

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(rpms: RPMS, torques: TORQUES) ⇒ TorqueCurve

Returns a new instance of TorqueCurve.



48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/driving_physics/motor.rb', line 48

def initialize(rpms: RPMS, torques: TORQUES)
  if rpms.size != torques.size
    raise("RPMs size #{rpms.size}; Torques size #{torques.size}")
  end
  @rpms = self.class.validate_rpms! rpms
  @torques = self.class.validate_torques! torques
  peak_torque = 0
  idx = 0
  @torques.each.with_index { |t, i|
    if t > peak_torque
      peak_torque = t
      idx = i
    end
  }
  @peak = idx
end

Class Method Details

.validate_rpms!(rpms) ⇒ Object



31
32
33
34
35
36
37
38
39
# File 'lib/driving_physics/motor.rb', line 31

def self.validate_rpms!(rpms)
  raise("rpms should be positive") if rpms.any? { |r| r < 0 }
  rpms.each.with_index { |r, i|
    if i > 0 and r <= rpms[i-1]
      raise("rpms #{rpms.inspect} should increase")
    end
  }
  rpms
end

.validate_torques!(torques) ⇒ Object



41
42
43
44
45
46
# File 'lib/driving_physics/motor.rb', line 41

def self.validate_torques!(torques)
  raise("first torque should be zero") unless torques.first == 0
  raise("last torque should be zero") unless torques.last == 0
  raise("torques should be positive") if torques.any? { |t| t < 0 }
  torques
end

Instance Method Details

#peakObject



65
66
67
# File 'lib/driving_physics/motor.rb', line 65

def peak
  [@rpms[@peak], @torques[@peak]]
end

#to_sObject



69
70
71
72
73
74
75
# File 'lib/driving_physics/motor.rb', line 69

def to_s
  @rpms.map.with_index { |r, i|
    format("%s RPM %s Nm",
           r.to_s.rjust(5, ' '),
           @torques[i].round(1).to_s.rjust(4, ' '))
  }.join("\n")
end

#torque(rpm) ⇒ Object

interpolate based on torque curve points



80
81
82
# File 'lib/driving_physics/motor.rb', line 80

def torque(rpm)
  DrivingPhysics.interpolate(rpm, xs: @rpms, ys: @torques)
end