1 //  Engine.h - an engine for the drivetrain.
2 //
3 //  Copyright (C) 2001--2003 Sam Varner
4 //
5 //  This file is part of Vamos Automotive Simulator.
6 //
7 //  This program is free software; you can redistribute it and/or modify
8 //  it under the terms of the GNU General Public License as published by
9 //  the Free Software Foundation; either version 2 of the License, or
10 //  (at your option) any later version.
11 //
12 //  This program is distributed in the hope that it will be useful,
13 //  but WITHOUT ANY WARRANTY; without even the implied warranty of
14 //  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 //  GNU General Public License for more details.
16 //
17 //  You should have received a copy of the GNU General Public License
18 //  along with this program; if not, write to the Free Software
19 //  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
20 
21 #ifndef _ENGINE_H_
22 #define _ENGINE_H_
23 
24 #include "../geometry/Conversions.h"
25 #include "../geometry/Spline.h"
26 #include "../geometry/Two_Vector.h"
27 #include "Particle.h"
28 
29 namespace Vamos_Body
30 {
31   //* An engine for the drivetrain.  Although it produces a torque,
32   // Engine is not derived from Component because the Engine's torque
33   // is a scalar, not a vector.
34   class Engine : public Particle
35   {
36 	// Used to calculate torque in torque_map ().
37 	double m_max_power;
38 
39 	// Used to calculate torque in torque_map ().
40 	double m_peak_engine_speed;
41 
42 	// The highest allowed engine speed (rev limit).
43 	double m_engine_speed_limit;
44 
45 	// The rotational inertia of the engine.
46 	double m_inertia;
47 
48 	// The fraction of throttle used when idling.
49 	double m_idle_throttle;
50 
51 	// The rotational speed that the engine is set to when starting.
52 	double m_start_speed;
53 
54 	// The engine shuts off if the rotational speed goes below this
55 	// value.
56 	double m_stall_speed;
57 
58 	// The rate of fuel consumption.
59 	double m_fuel_consumption;
60 
61 	// The rotational speed of the engine.
62 	double m_rotational_speed;
63 
64 	double m_last_rotational_speed;
65 
66 	// The throttle position.
67 	double m_gas;
68 
69 	// The load on the engine from the clutch.
70 	double m_drag;
71 
72 	double m_transmission_speed;
73 
74 	// true if the gas tank is empty, false otherwise.
75 	bool m_out_of_gas;
76 
77 	// The minimum throttle position.
78 	double m_idle;
79 
80 	// The current torque produced by the engine.
81 	double m_drive_torque;
82 
83 	// The impulse calculated in the last call to torque ().
84 	double m_drive_impulse;
85 
86 	// true if the clutch is fully engaged, false otherwise.
87 	bool m_engaged;
88 
89 	// Set the engine speed to SPEED_IN and calculate the resulting
90 	// impulse.
91 	void speed (double speed_in);
92 
93     Vamos_Geometry::Spline m_torque_curve;
94 
95     double m_friction;
96 
97   public:
98 	//** Constructor
99 	// MAX_POWER is in the correct derived units (Watts in SI),
100 	// PEAK_ENGINE_RPM is in rotations per minute.
101 	Engine (double mass, const Vamos_Geometry::Three_Vector& position,
102 			double max_power,
103 			double peak_engine_rpm,
104 			double rpm_limit,
105 			double inertia,
106 			double idle_throttle,
107 			double start_rpm,
108 			double stall_rpm,
109 			double fuel_consumption,
110             const Frame* parent = 0);
111 
112     void set_torque_curve (const std::vector <Vamos_Geometry::Two_Vector>&
113                            torque_points);
114 
set_friction(double friction)115     void set_friction (double friction) { m_friction = friction; }
116 
117 	// Handle the input parameters.  GAS is the throttle position.
118 	// TRANSMISSION_SPEED is the rotational speed of the transmission
119 	// side of the clutch.  DRAG is the torque due to friction when
120 	// the clutch is not fully engaged.  ENGAGED is true when the
121 	// clutch is fully engaged, false otherwise.
122 	void input (double gas, double drag, double transmission_speed,
123 				bool engaged);
124 
125 	void find_forces ();
126 
127 	// Advance the engine in time by TIME.
128 	void propagate (double time);
129 
130 	void rewind ();
131 
132 	// Return the current rotational speed in radians per second.
rotational_speed()133 	double rotational_speed () const { return m_rotational_speed; }
134 
135 	// Return the engine speed where the rev limiter kicks in.
max_rotational_speed()136 	double max_rotational_speed () const { return m_engine_speed_limit; }
137 
peak_engine_speed()138     double peak_engine_speed () const { return m_peak_engine_speed; }
139 
stall_speed()140     double stall_speed () const { return m_stall_speed; }
141 
142 	// Return the current torque.
drive_torque()143 	double drive_torque () const { return m_drive_torque; }
144 
drive_impulse()145 	double drive_impulse () const { return m_drive_impulse; }
146 
147 	// Return the torque for a given throttle setting, GAS, and engine
148 	// speed ROTATIONAL_SPEED.
149 	double torque_map (double gas, double rotational_speed);
150 
151     double power (double gas, double rotational_speed);
152 
throttle()153 	double throttle () const { return m_gas; }
154 
155 	// Return the current rate of fuel consumption.
fuel_rate()156 	double fuel_rate () const
157 	{ return m_fuel_consumption * m_rotational_speed * m_gas; }
158 
159 	// Tell the engine if we're out of gas.
out_of_gas(bool out)160 	void out_of_gas (bool out) { m_out_of_gas = out; }
is_out_of_gas()161     bool is_out_of_gas () const { return m_out_of_gas; }
162 
163 	// Start the engine.
start()164 	void start () { speed (m_start_speed); }
165   };
166 }
167 
168 #endif // !_ENGINE_H_
169