RigsofRods
Soft-body Physics Simulation
All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
CruiseControl.cpp
Go to the documentation of this file.
1 /*
2  This source file is part of Rigs of Rods
3  Copyright 2005-2012 Pierre-Michel Ricordel
4  Copyright 2007-2012 Thomas Fischer
5  Copyright 2015-2020 Petr Ohlidal
6 
7  For more information, see http://www.rigsofrods.org/
8 
9  Rigs of Rods is free software: you can redistribute it and/or modify
10  it under the terms of the GNU General Public License version 3, as
11  published by the Free Software Foundation.
12 
13  Rigs of Rods is distributed in the hope that it will be useful,
14  but WITHOUT ANY WARRANTY; without even the implied warranty of
15  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16  GNU General Public License for more details.
17 
18  You should have received a copy of the GNU General Public License
19  along with Rigs of Rods. If not, see <http://www.gnu.org/licenses/>.
20 */
21 
22 #include "Actor.h"
23 
24 #include "Engine.h"
25 #include "InputEngine.h"
26 
27 #include <Ogre.h>
28 
29 using namespace RoR;
30 
32 {
33  cc_mode = !cc_mode;
34 
35  if (cc_mode)
36  {
39  }
40  else
41  {
42  cc_target_speed = 0;
43  cc_target_rpm = 0;
44  cc_accs.clear();
45  }
46 }
47 
49 {
52  (ar_engine->getGear() > 0 && ar_parking_brake) ||
53  (ar_engine->getGear() < 0) ||
54  !ar_engine->isRunning() ||
55  !ar_engine->hasContact())
56  {
57  this->cruisecontrolToggle();
58  return;
59  }
60 
62  return;
63 
64  float acc = ar_engine->getAccToHoldRPM();
65 
66  if (ar_engine->getGear() > 0)
67  {
68  // Try to maintain the target speed
69  float power_weight_ratio = getTotalMass(true) / ar_engine->getEnginePower();
70  acc += (cc_target_speed - ar_wheel_speed) * power_weight_ratio * 0.25;
71  }
72  else if (ar_engine->getGear() == 0) // out of gear
73  {
74  // Try to maintain the target rpm
75  float speed_range = (ar_engine->getShiftUpRPM() - ar_engine->getShiftDownRPM()) / 50.0f;
76  acc += ar_engine->getEngineInertia() * (cc_target_rpm - ar_engine->getRPM()) / speed_range;
77  }
78 
79  cc_accs.push_front(Ogre::Math::Clamp(acc, -1.0f, +1.0f));
80  if (cc_accs.size() > 30)
81  {
82  cc_accs.pop_back();
83  }
84 
85  float avg_acc = 0.0f;
86  for (unsigned int i = 0; i < cc_accs.size(); i++)
87  {
88  avg_acc += cc_accs[i];
89  }
90  avg_acc /= cc_accs.size();
91 
92  ar_engine->autoSetAcc(Ogre::Math::Clamp(avg_acc, ar_engine->getAcc(), 1.0f));
93 
95  {
96  if (ar_engine->getGear() > 0)
97  {
98  cc_target_speed *= pow(2.0f, dt / 5.0f);
100  if (sl_enabled)
101  {
103  }
104  }
105  else if (ar_engine->getGear() == 0) // out of gear
106  {
107  cc_target_rpm *= pow(2.0f, dt / 5.0f);
108  cc_target_rpm = std::min(cc_target_rpm, ar_engine->getShiftUpRPM());
109  }
110  }
111  if (App::GetInputEngine()->getEventBoolValue(EV_TRUCK_CRUISE_CONTROL_DECL))
112  {
113  if (ar_engine->getGear() > 0)
114  {
115  cc_target_speed *= pow(0.5f, dt / 5.0f);
117  }
118  else if (ar_engine->getGear() == 0) // out of gear
119  {
120  cc_target_rpm *= pow(0.5f, dt / 5.0f);
121  cc_target_rpm = std::max(ar_engine->getShiftDownRPM(), cc_target_rpm);
122  }
123  }
124  if (App::GetInputEngine()->getEventBoolValue(EV_TRUCK_CRUISE_CONTROL_READJUST))
125  {
127  if (sl_enabled)
128  {
130  }
132  }
133 
134  if (cc_can_brake)
135  {
137  {
138  float brake = (ar_avg_wheel_speed - cc_target_speed) * 0.5f;
139  ar_brake = std::min(brake, 1.0f);
140  }
141  }
142 }
RoR::Actor::ar_avg_wheel_speed
float ar_avg_wheel_speed
Physics state; avg wheel speed in m/s.
Definition: Actor.h:426
RoR::Actor::cc_mode
bool cc_mode
Cruise Control.
Definition: Actor.h:388
RoR::Actor::ar_parking_brake
bool ar_parking_brake
Definition: Actor.h:438
RoR::Actor::cc_target_rpm
float cc_target_rpm
Cruise Control.
Definition: Actor.h:390
RoR::Actor::ar_brake
Ogre::Real ar_brake
Physics state; braking intensity.
Definition: Actor.h:423
RoR::Actor::sl_enabled
bool sl_enabled
Speed limiter;.
Definition: Actor.h:394
RoR::EV_TRUCK_CRUISE_CONTROL_READJUST
@ EV_TRUCK_CRUISE_CONTROL_READJUST
match target speed / rpm with current truck speed / rpm
Definition: InputEngine.h:311
RoR::Actor::sl_speed_limit
float sl_speed_limit
Speed limiter;.
Definition: Actor.h:395
RoR::Actor::cc_target_speed_lower_limit
float cc_target_speed_lower_limit
Cruise Control.
Definition: Actor.h:392
Engine.h
RoR::InputEngine::getEventValue
float getEventValue(int eventID, bool pure=false, InputSourceType valueSource=InputSourceType::IST_ANY)
valueSource: IST_ANY=digital and analog devices, IST_DIGITAL=only digital, IST_ANALOG=only analog
Definition: InputEngine.cpp:965
Actor.h
RoR::Actor::ar_wheel_speed
float ar_wheel_speed
Physics state; wheel speed in m/s.
Definition: Actor.h:424
RoR::Engine::isRunning
bool isRunning()
Definition: Engine.h:101
RoR::Actor::ar_engine
EnginePtr ar_engine
Definition: Actor.h:403
RoR::Engine::getRPM
float getRPM()
Definition: Engine.h:94
RoR::Actor::cc_target_speed
float cc_target_speed
Cruise Control.
Definition: Actor.h:391
RoR::Engine::autoSetAcc
void autoSetAcc(float val)
Definition: Engine.cpp:1075
RoR::InputEngine::getEventBoolValue
bool getEventBoolValue(int eventID)
Definition: InputEngine.cpp:764
RoR::Actor::cc_accs
std::deque< float > cc_accs
Cruise Control.
Definition: Actor.h:393
RoR::Actor::UpdateCruiseControl
void UpdateCruiseControl(float dt)
Defined in 'gameplay/CruiseControl.cpp'.
Definition: CruiseControl.cpp:48
RoR::Actor::getTotalMass
float getTotalMass(bool withLocked=true)
Definition: Actor.cpp:832
RoR::Actor::cc_can_brake
bool cc_can_brake
Cruise Control.
Definition: Actor.h:389
RoR::EV_TRUCK_ACCELERATE
@ EV_TRUCK_ACCELERATE
accelerate the truck
Definition: InputEngine.h:296
RoR::EV_TRUCK_MANUAL_CLUTCH
@ EV_TRUCK_MANUAL_CLUTCH
manual clutch (for manual transmission)
Definition: InputEngine.h:325
RoR::Engine::getAcc
float getAcc()
Definition: Engine.cpp:880
RoR::App::GetInputEngine
InputEngine * GetInputEngine()
Definition: Application.cpp:287
RoR::Engine::getAccToHoldRPM
float getAccToHoldRPM()
estimate required throttle input to hold the current rpm
Definition: Engine.cpp:1069
RoR::Engine::getEnginePower
float getEnginePower()
Definition: Engine.h:106
InputEngine.h
Handles controller inputs from player. Defines input events and binding mechanism,...
RoR::EV_TRUCK_CRUISE_CONTROL_DECL
@ EV_TRUCK_CRUISE_CONTROL_DECL
decrease target speed / rpm
Definition: InputEngine.h:310
RoR::EV_TRUCK_CRUISE_CONTROL_ACCL
@ EV_TRUCK_CRUISE_CONTROL_ACCL
increase target speed / rpm
Definition: InputEngine.h:309
RoR::Engine::getGear
int getGear()
Definition: Engine.cpp:1038
RoR::Actor::cruisecontrolToggle
void cruisecontrolToggle()
Defined in 'gameplay/CruiseControl.cpp'.
Definition: CruiseControl.cpp:31
RoR
Definition: AppContext.h:36
RoR::EV_TRUCK_BRAKE
@ EV_TRUCK_BRAKE
brake
Definition: InputEngine.h:305