[go: up one dir, main page]

Menu

[8286ad]: / body / Engine.cpp  Maximize  Restore  History

Download this file

114 lines (99 with data), 4.0 kB

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
// An engine for the drivetrain.
//
// Copyright (C) 2001-2019 Sam Varner
//
// This file is part of Vamos Automotive Simulator.
//
// Vamos is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Vamos is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Vamos. If not, see <http://www.gnu.org/licenses/>.
#include "Engine.hpp"
#include "../geometry/Constants.hpp"
using namespace Vamos_Body;
using namespace Vamos_Geometry;
Engine::Engine(double mass, const Three_Vector& position, double max_power, double peak_engine_rpm,
double rpm_limit, double inertia, double idle_throttle, double start_rpm,
double stall_rpm, double fuel_consumption)
: Particle(mass, position),
m_max_power(max_power),
m_peak_engine_speed(rpm_to_rad_s(peak_engine_rpm)),
m_engine_speed_limit(rpm_to_rad_s(rpm_limit)),
m_inertia(inertia),
m_idle_throttle(idle_throttle),
m_start_speed(rpm_to_rad_s(start_rpm)),
m_stall_speed(rpm_to_rad_s(stall_rpm)),
m_fuel_consumption(fuel_consumption),
// See "Motor Vehicle Dynamics" Genta, Section 4.2.2
m_friction(m_max_power / pow(m_peak_engine_speed, 3))
{}
void Engine::set_torque_curve(const std::vector<Two_Vector>& torque_points)
{
m_torque_curve.clear();
m_torque_curve.load(torque_points);
m_torque_curve.scale(rpm_to_rad_s(1.0));
}
void Engine::input(double gas, double drag, double transmission_speed, bool engaged)
{
m_gas = gas;
m_drag = drag;
m_transmission_speed = transmission_speed;
m_engaged = engaged;
}
double Engine::power(double gas, double rotational_speed)
{
return rotational_speed * torque_map(gas, rotational_speed);
}
void Engine::propagate(double time)
{
// Find the engine's torque with the current conditions.
m_drive_torque = torque_map(m_gas, m_rotational_speed) - m_drag;
m_torque = Three_Vector::X * -m_drive_torque;
// The engine should change its own speed only when the clutch is disengaged.
// Otherwise, the engine speed is matched to the transmission, which changes speed due
// to the applied engine torque. If the clutch is engaged, the engine speed is locked
// to the transmission speed.
m_rotational_speed = m_engaged
? m_transmission_speed : m_rotational_speed + time * m_drive_torque / m_inertia;
// Keep engine speed from going negative when changing from forward to reverse (or
// vice versa) without using the clutch.
if (m_rotational_speed < m_stall_speed)
m_rotational_speed = 0.0;
}
double Engine::torque_map(double gas, double rot_speed)
{
if (m_out_of_gas || m_rotational_speed < m_stall_speed
|| m_rotational_speed > m_engine_speed_limit)
m_gas = 0.0;
else
m_gas = std::max(gas, m_idle_throttle);
return m_torque_curve.empty()
// See "Motor Vehicle Dynamics" Genta, Section 4.2.2
? m_max_power * m_gas * (1.0 + rot_speed / m_peak_engine_speed) / m_peak_engine_speed
- m_friction * rot_speed * rot_speed
: m_gas * m_torque_curve.interpolate(rot_speed)
- m_friction * rot_speed * rot_speed * (1.0 - m_gas);
}
void Engine::speed(double speed_in)
{
double rot_speed = m_rotational_speed;
m_rotational_speed = speed_in > m_stall_speed ? speed_in : 0.0;
// Record the change in angular momentum.
m_drive_impulse = m_inertia * (m_rotational_speed - rot_speed);
}
void Engine::start()
{
speed(m_start_speed);
}
double Engine::fuel_rate() const
{
return m_fuel_consumption * m_rotational_speed * m_gas;
}