1 // =============================================================================
2 // PROJECT CHRONO - http://projectchrono.org
3 //
4 // Copyright (c) 2014 projectchrono.org
5 // All rights reserved.
6 //
7 // Use of this source code is governed by a BSD-style license that can be found
8 // in the LICENSE file at the top level of the distribution and at
9 // http://projectchrono.org/license-chrono.txt.
10 //
11 // =============================================================================
12 // Authors: Radu Serban
13 // =============================================================================
14 //
15 // Base class for a tracked vehicle system.
16 //
17 // The reference frame for a vehicle follows the ISO standard: Z-axis up, X-axis
18 // pointing forward, and Y-axis towards the left of the vehicle.
19 //
20 // =============================================================================
21 
22 #ifndef CH_TRACKED_VEHICLE_H
23 #define CH_TRACKED_VEHICLE_H
24 
25 #include <vector>
26 
27 #include "chrono_vehicle/ChVehicle.h"
28 #include "chrono_vehicle/ChDriver.h"
29 #include "chrono_vehicle/tracked_vehicle/ChDrivelineTV.h"
30 #include "chrono_vehicle/tracked_vehicle/ChTrackAssembly.h"
31 #include "chrono_vehicle/tracked_vehicle/ChTrackContactManager.h"
32 
33 namespace chrono {
34 namespace vehicle {
35 
36 /// @addtogroup vehicle_tracked
37 /// @{
38 
39 /// Base class for chrono tracked vehicle systems.
40 /// This class provides the interface between the vehicle system and other
41 /// systems (terrain, driver, etc.)
42 class CH_VEHICLE_API ChTrackedVehicle : public ChVehicle {
43   public:
44     /// Construct a vehicle system with a default ChSystem.
45     ChTrackedVehicle(const std::string& name,                               ///< [in] vehicle name
46                      ChContactMethod contact_method = ChContactMethod::NSC  ///< [in] contact method
47     );
48 
49     /// Construct a vehicle system using the specified ChSystem.
50     ChTrackedVehicle(const std::string& name,  ///< [in] vehicle name
51                      ChSystem* system          ///< [in] containing mechanical system
52                      );
53 
54     /// Destructor.
55     virtual ~ChTrackedVehicle();
56 
57     /// Get the name of the vehicle system template.
GetTemplateName()58     virtual std::string GetTemplateName() const override { return "TrackedVehicle"; }
59 
60     /// Get the vehicle total mass.
61     /// This includes the mass of the chassis and all vehicle subsystems.
62     virtual double GetVehicleMass() const override;
63 
64     /// Get the current global vehicle COM location.
65     /// (Currently not implemented)
66     virtual ChVector<> GetVehicleCOMPos() const override;
67 
68     /// Get the powertrain attached to this vehicle.
GetPowertrain()69     virtual std::shared_ptr<ChPowertrain> GetPowertrain() const override { return m_powertrain; }
70 
71     /// Get the specified suspension subsystem.
GetTrackAssembly(VehicleSide side)72     std::shared_ptr<ChTrackAssembly> GetTrackAssembly(VehicleSide side) const { return m_tracks[side]; }
73 
74     /// Get a handle to the vehicle's driveline subsystem.
GetDriveline()75     std::shared_ptr<ChDrivelineTV> GetDriveline() const { return m_driveline; }
76 
77     /// Get a handle to the vehicle's driveshaft body.
GetDriveshaft()78     virtual std::shared_ptr<ChShaft> GetDriveshaft() const override { return m_driveline->GetDriveshaft(); }
79 
80     /// Get the number of suspensions in the specified track assembly.
GetNumRoadWheelAssemblies(VehicleSide side)81     size_t GetNumRoadWheelAssemblies(VehicleSide side) const { return m_tracks[side]->GetNumRoadWheelAssemblies(); }
82 
83     /// Get the number of shoes in the specified track assembly.
GetNumTrackShoes(VehicleSide side)84     size_t GetNumTrackShoes(VehicleSide side) const { return m_tracks[side]->GetNumTrackShoes(); }
85 
86     /// Get a handle to the specified track shoe.
GetTrackShoe(VehicleSide side,size_t id)87     std::shared_ptr<ChTrackShoe> GetTrackShoe(VehicleSide side, size_t id) const {
88         return m_tracks[side]->GetTrackShoe(id);
89     }
90 
91     /// Get the complete state for the specified track shoe.
92     /// This includes the location, orientation, linear and angular velocities,
93     /// all expressed in the global reference frame.
GetTrackShoeState(VehicleSide side,size_t shoe_id)94     BodyState GetTrackShoeState(VehicleSide side, size_t shoe_id) const {
95         return m_tracks[side]->GetTrackShoeState(shoe_id);
96     }
97 
98     /// Get the complete states for all track shoes of the specified track assembly.
99     /// It is assumed that the vector of body states was properly sized.
GetTrackShoeStates(VehicleSide side,BodyStates & states)100     void GetTrackShoeStates(VehicleSide side, BodyStates& states) const {
101         m_tracks[side]->GetTrackShoeStates(states);
102     }
103 
104     /// Set visualization type for the sprocket subsystem.
105     void SetSprocketVisualizationType(VisualizationType vis);
106 
107     /// Set visualization type for the idler subsystem.
108     void SetIdlerVisualizationType(VisualizationType vis);
109 
110     /// Set visualization type for the suspension subsystems.
111     void SetRoadWheelAssemblyVisualizationType(VisualizationType vis);
112 
113     /// Set visualization type for the road-wheel subsystems.
114     void SetRoadWheelVisualizationType(VisualizationType vis);
115 
116     /// Set visualization type for the roller subsystems.
117     void SetRollerVisualizationType(VisualizationType vis);
118 
119     /// Set visualization type for the track shoe subsystems.
120     void SetTrackShoeVisualizationType(VisualizationType vis);
121 
122     /// Enable/disable collision for the sprocket subsystem.
123     void SetSprocketCollide(bool state);
124 
125     /// Enable/disable collision for the idler subsystem.
126     void SetIdlerCollide(bool state);
127 
128     /// Enable/disable collision for the road-wheel subsystems.
129     void SetRoadWheelCollide(bool state);
130 
131     /// Enable/disable collision for the roller subsystems.
132     void SetRollerCollide(bool state);
133 
134     /// Enable/disable collision for the track shoe subsystems.
135     void SetTrackShoeCollide(bool state);
136 
137     /// Set collision flags for the various subsystems.
138     /// By default, collision is enabled for chassis, sprocket, idler, road wheels, and
139     /// track shoes. To override these default settings, this function must be called
140     /// called after the call to Initialize(). The 'flags' argument can be any of the
141     /// TrackedCollisionFlag enums, or a combination thereof (using bit-wise operators).
142     void SetCollide(int flags);
143 
144     /// Enable/disable collision between the chassis and all other vehicle
145     /// subsystems. This only controls collisions between the chassis and the
146     /// track shoes.  All other internal collisions involving the chassis are
147     /// always ignored.
148     virtual void SetChassisVehicleCollide(bool state) override;
149 
150     /// Enable user-defined contact forces between track shoes and idlers and/or road-wheels and/or ground. By default,
151     /// contact forces are generated by the underlying Chrono contact processing. If enabled, no contact forces are
152     /// applied automatically for specified collision types. Instead, these collisions are cached and passed to the
153     /// user-supplied callback which must compute the contact force for each individual collision.
154     void EnableCustomContact(std::shared_ptr<ChTrackCustomContact> callback);
155 
156     /// Set contacts to be monitored.
157     /// Contact information will be tracked for the specified subsystems.
MonitorContacts(int flags)158     void MonitorContacts(int flags) { m_contact_manager->MonitorContacts(flags); }
159 
160     /// Render normals of all monitored contacts.
SetRenderContactNormals(bool val)161     void SetRenderContactNormals(bool val) { m_contact_manager->SetRenderNormals(val); }
162 
163     /// Render forces of all monitored contacts.
SetRenderContactForces(bool val,double scale)164     void SetRenderContactForces(bool val, double scale) { m_contact_manager->SetRenderForces(val, scale); }
165 
166     /// Turn on/off contact data collection.
167     /// If enabled, contact information will be collected for all monitored subsystems.
SetContactCollection(bool val)168     void SetContactCollection(bool val) { m_contact_manager->SetContactCollection(val); }
169 
170     /// Return true if the specified vehicle part is currently experiencing a collision.
IsPartInContact(TrackedCollisionFlag::Enum part)171     bool IsPartInContact(TrackedCollisionFlag::Enum part) const { return m_contact_manager->InContact(part); }
172 
173     /// Return estimated resistive torque on the specified sprocket.
174     /// This torque is available only if monitoring of contacts for that sprocket is enabled.
GetSprocketResistiveTorque(VehicleSide side)175     ChVector<> GetSprocketResistiveTorque(VehicleSide side) const {
176         return m_contact_manager->GetSprocketResistiveTorque(side);
177     }
178 
179     /// Write contact information to file.
180     /// If data collection was enabled and at least one subsystem is monitored,
181     /// contact information is written (in CSV format) to the specified file.
WriteContacts(const std::string & filename)182     void WriteContacts(const std::string& filename) { m_contact_manager->WriteContacts(filename); }
183 
184     /// Enable/disable output for the track assemblies.
185     /// See also ChVehicle::SetOuput.
186     void SetTrackAssemblyOutput(VehicleSide side, bool state);
187 
188     /// Initialize this vehicle at the specified global location and orientation.
189     /// This base class implementation only initializes the main chassis subsystem.
190     /// Derived classes must extend this function to initialize all other tracked
191     /// vehicle subsystems (the two track assemblies and the driveline).
192     virtual void Initialize(const ChCoordsys<>& chassisPos,  ///< [in] initial global position and orientation
193                             double chassisFwdVel = 0         ///< [in] initial chassis forward velocity
194                             ) override;
195 
196     /// Initialize the given powertrain system and associate it to this vehicle.
197     /// The powertrain is initialized by connecting it to this vehicle's chassis and driveline shaft.
198     void InitializePowertrain(std::shared_ptr<ChPowertrain> powertrain);
199 
200     /// Update the state of this vehicle at the current time.
201     /// The vehicle system is provided the current driver inputs (throttle between 0 and 1, steering between -1 and +1,
202     /// braking between 0 and 1) and terrain forces on the track shoes (expressed in the global reference frame).
203     void Synchronize(double time,                            ///< [in] current time
204                      const ChDriver::Inputs& driver_inputs,  ///< [in] current driver inputs
205                      const TerrainForces& shoe_forces_left,  ///< [in] vector of track shoe forces (left side)
206                      const TerrainForces& shoe_forces_right  ///< [in] vector of track shoe forces (left side)
207     );
208 
209     /// Advance the state of this vehicle by the specified time step.
210     /// In addition to advancing the state of the multibody system (if the vehicle owns the underlying system), this
211     /// function also advances the state of the associated powertrain.
212     virtual void Advance(double step) override final;
213 
214     /// Log current constraint violations.
215     virtual void LogConstraintViolations() override;
216 
217     /// Return a JSON string with information on all modeling components in the vehicle system.
218     /// These include bodies, shafts, joints, spring-damper elements, markers, etc.
219     virtual std::string ExportComponentList() const override;
220 
221     /// Write a JSON-format file with information on all modeling components in the vehicle system.
222     /// These include bodies, shafts, joints, spring-damper elements, markers, etc.
223     virtual void ExportComponentList(const std::string& filename) const override;
224 
225   protected:
226     /// Output data for all modeling components in the vehicle system.
227     virtual void Output(int frame, ChVehicleOutput& database) const override;
228 
229     std::shared_ptr<ChTrackAssembly> m_tracks[2];  ///< track assemblies (left/right)
230     std::shared_ptr<ChDrivelineTV> m_driveline;    ///< driveline subsystem
231     std::shared_ptr<ChPowertrain> m_powertrain;    ///< associated powertrain system
232 
233     std::shared_ptr<ChTrackCollisionManager> m_collision_manager;  ///< manager for internal collisions
234     std::shared_ptr<ChTrackContactManager> m_contact_manager;      ///< manager for internal contacts
235 
236     friend class ChTrackedVehicleIrrApp;
237 };
238 
239 /// @} vehicle_tracked
240 
241 }  // end namespace vehicle
242 }  // end namespace chrono
243 
244 #endif
245