| 1 | #ifndef _GEAR_HPP |
| 2 | #define _GEAR_HPP |
| 3 | #include "Math.hpp" |
| 4 | |
| 5 | #include <functional> |
| 6 | |
| 7 | |
| 8 | namespace simgear { |
| 9 | class BVHMaterial; |
| 10 | } |
| 11 | |
| 12 | namespace yasim { |
| 13 | |
| 14 | class Ground; |
| 15 | class RigidBody; |
| 16 | struct State; |
| 17 | |
| 18 | |
| 19 | /* Contains a vector and pre-generates unit and magnitude values. */ |
| 20 | struct GearVector |
| 21 | { |
| 22 | GearVector( float v0=0, float v1=0, float v2=0); |
| 23 | void set( float v0, float v1, float v2); |
| 24 | float v[3]; |
| 25 | float unit[3]; |
| 26 | float magnitude; |
| 27 | }; |
| 28 | |
| 29 | // A landing gear has the following parameters: |
| 30 | // |
| 31 | // position: a point in the aircraft's local coordinate system where the |
| 32 | // fully-extended wheel will be found. |
| 33 | // compression: a vector from position where a fully-compressed wheel |
| 34 | // will be, also in aircraft-local coordinates. |
| 35 | // spring coefficient: force coefficient along the compression axis, in |
| 36 | // Newtons per meter. |
| 37 | // damping coefficient: force per compression speed, in Ns/m |
| 38 | // static coefficient of friction: force along the ground plane exerted |
| 39 | // by a motionless wheel. A unitless scalar. |
| 40 | // dynamic coefficient of friction: force along the ground plane exerted |
| 41 | // by a sliding/skidding wheel. |
| 42 | // braking fraction: fraction of the dynamic friction that will be |
| 43 | // actually exerted by a rolling wheel |
| 44 | // rotation: the angle from "forward" by which the wheel is rotated |
| 45 | // around its compression axis. In radians. |
| 46 | // |
| 47 | class Gear { |
| 48 | public: |
| 49 | Gear(); |
| 50 | |
| 51 | // Externally set values |
| 52 | void setPosition(float* position) { Math::set3(position, _pos); } |
| 53 | void getPosition(float* out) { Math::set3(_pos, out); } |
| 54 | void setCompression(float* compression); |
| 55 | void getCompression(float* out) { Math::set3(_cmpr.v, out); } |
| 56 | |
| 57 | void setWheelAxle(float* wheelAxle); |
| 58 | void getWheelAxle(float* out) { Math::set3(_wheelAxle.v, out); } |
| 59 | |
| 60 | void setWheelRadius(float wheelRadius) { _wheelRadius = wheelRadius; } |
| 61 | float getWheelRadius() { return _wheelRadius; } |
| 62 | |
| 63 | void setTyreRadius(float tyreRadius) { _tyreRadius = tyreRadius; } |
| 64 | float getTyreRadius() { return _tyreRadius; } |
| 65 | |
| 66 | void getContact(float* contact) { Math::set3( _contact, contact); } |
| 67 | |
| 68 | void setSpring(float spring) { _spring = spring; } |
| 69 | float getSpring() { return _spring; } |
| 70 | void setSpring2(float spring2) { _spring2 = spring2; } |
| 71 | void setDamping(float damping) { _damp = damping; } |
| 72 | float getDamping() {return _damp; } |
| 73 | void setStaticFriction(float sfric) { _sfric = sfric; } |
| 74 | float getStaticFriction() { return _sfric; } |
| 75 | void setDynamicFriction(float dfric) { _dfric = dfric; } |
| 76 | float getDynamicFriction() { return _dfric; } |
| 77 | void setBrake(float brake) { _brake = Math::clamp(brake, 0, 1); } |
| 78 | float getBrake() { return _brake; } |
| 79 | void setRotation(float rotation) { _rot = rotation; } |
| 80 | float getRotation() { return _rot; } |
| 81 | void setExtension(float extension) { _extension = Math::clamp(extension, 0, 1); } |
| 82 | float getExtension() { return _extension; } |
| 83 | void setCastering(bool c) { _castering = c; } |
| 84 | bool getCastering() { return _castering; } |
| 85 | void setOnWater(bool c) { _onWater = c; } |
| 86 | void setOnSolid(bool c) { _onSolid = c; } |
| 87 | void setSpringFactorNotPlaning(float f) { _spring_factor_not_planing = f; } |
| 88 | void setSpeedPlaning(float s) { _speed_planing = s; } |
| 89 | void setReduceFrictionByExtension(float s) { _reduceFrictionByExtension = s; } |
| 90 | void setInitialLoad(float l) { _initialLoad = l; } |
| 91 | float getInitialLoad() {return _initialLoad; } |
| 92 | void setIgnoreWhileSolving(bool c) { _ignoreWhileSolving = c; } |
| 93 | void setStiction(bool s) { _stiction = s; } |
| 94 | bool getStiction() { return _stiction; } |
| 95 | void setStictionABS(bool a) { _stiction_abs = a; } |
| 96 | bool getStictionABS() { return _stiction_abs; } |
| 97 | |
| 98 | void setGlobalGround(double* global_ground, float* global_vel, |
| 99 | double globalX, double globalY, |
| 100 | const simgear::BVHMaterial *material, unsigned int body); |
| 101 | void getGlobalGround(double* global_ground); |
| 102 | float getCasterAngle() { return _casterAngle; } |
| 103 | float getRollSpeed() { return _rollSpeed; } |
| 104 | float getBumpAltitude(); |
| 105 | bool getGroundIsSolid() { return _ground_isSolid; } |
| 106 | float getGroundFrictionFactor() { return (float)_ground_frictionFactor; } |
| 107 | void integrate(float dt); |
| 108 | |
| 109 | // Takes a velocity of the aircraft relative to ground, a rotation |
| 110 | // vector, and a ground plane (all specified in local coordinates) |
| 111 | // and make a force and point of application (i.e. ground contact) |
| 112 | // available via getForce(). |
| 113 | void calcForce(Ground *g_cb, RigidBody* body, State* s, float* v, float* rot); |
| 114 | void calcForceWithoutStiction(float wgt, float steer[3], float skid[3], float *cv, float *force); |
| 115 | void calcForceWithStiction(Ground *g_cb, State *s, float wgt, float steer[3], float skid[3], float *cv, float *force); |
| 116 | |
| 117 | // Computed values: total force, weight-on-wheels (force normal to |
| 118 | // ground) and compression fraction. |
| 119 | void getForce(float* force, float* contact); |
| 120 | float getWoW() { return _wow; } |
| 121 | float getCompressFraction() { return _frac; } |
| 122 | float getCompressDist() { return _compressDist; } |
| 123 | bool getSubmergable() {return (!_ground_isSolid)&&(!_isContactPoint); } |
| 124 | bool getIgnoreWhileSolving() {return _ignoreWhileSolving; } |
| 125 | void setContactPoint(bool c) { _isContactPoint=c; } |
| 126 | |
| 127 | // Update the stuck point after all integrator's iterations |
| 128 | void updateStuckPoint(State* s); |
| 129 | void getStuckPoint(double *out); |
| 130 | void setStuckPoint(double *in); |
| 131 | |
| 132 | private: |
| 133 | float calcFriction(float wgt, float v); |
| 134 | float calcFrictionFluid(float wgt, float v); |
| 135 | void calcFriction(float *gpos, float *cv, float *steer, float *skid, float wgt, float *force); |
| 136 | void calcFrictionFluid(float *cv, float *steer, float *skid, float wgt, float *force); |
| 137 | |
| 138 | float _fric_spring = 0; |
| 139 | bool _castering = false; |
| 140 | bool _rolling = false; |
| 141 | bool _slipping = false; |
| 142 | float _pos[3] = {0}; |
| 143 | double _stuck[3] = {0}; |
| 144 | GearVector _cmpr; |
| 145 | float _spring = 0; |
| 146 | float _spring2 = 0; |
| 147 | float _damp = 0; |
| 148 | float _sfric = 0; |
| 149 | float _dfric = 0; |
| 150 | float _brake = 0; |
| 151 | float _rot = 0; |
| 152 | float _extension = 0; |
| 153 | float _force[3] = {0}; |
| 154 | float _contact[3] = {0}; |
| 155 | float _wow = 0; |
| 156 | float _frac = 0; /* Compression as fraction of maximum. */ |
| 157 | float _initialLoad = 0; |
| 158 | float _compressDist = 0; /* Vertical compression distance. */ |
| 159 | double _global_ground[4] = {0}; |
| 160 | float _global_vel[3] = {0}; |
| 161 | float _casterAngle = 0; |
| 162 | float _rollSpeed = 0; |
| 163 | bool _isContactPoint = false; |
| 164 | bool _onWater = false; |
| 165 | bool _onSolid = false; |
| 166 | float _spring_factor_not_planing = 0; |
| 167 | float _speed_planing = 0; |
| 168 | float _reduceFrictionByExtension = 0; |
| 169 | bool _ignoreWhileSolving = false; |
| 170 | bool _stiction = false; |
| 171 | bool _stiction_abs = false; |
| 172 | |
| 173 | double _ground_frictionFactor = 0; |
| 174 | double _ground_rollingFriction = 0; |
| 175 | double _ground_loadCapacity = 0; |
| 176 | double _ground_loadResistance = 0; |
| 177 | double _ground_bumpiness = 0; |
| 178 | bool _ground_isSolid = false; |
| 179 | double _global_x = 0; |
| 180 | double _global_y = 0; |
| 181 | unsigned int _body_id = 0; |
| 182 | double _ground_rot[9] = {0}; |
| 183 | double _ground_trans[3] = {0}; |
| 184 | |
| 185 | GearVector _wheelAxle; |
| 186 | float _wheelRadius = 0; |
| 187 | float _tyreRadius = 0; |
| 188 | }; |
| 189 | |
| 190 | |
| 191 | /* [This is internal implementation for Gear(), exposed here to allow unit |
| 192 | testing.] |
| 193 | |
| 194 | Finds compression of gear needed to make outside of tyre rest on ground. |
| 195 | |
| 196 | ground |
| 197 | Definition of ground plane; points on ground satisfy p.ground[:3] == |
| 198 | ground[4]. Is assumed that for points below ground, p.ground[:3] is |
| 199 | positive. |
| 200 | compression |
| 201 | Movement of wheel centre at maximum compression. |
| 202 | wheel_pos |
| 203 | Centre of wheel at full extension. |
| 204 | wheel_axle |
| 205 | Direction of wheel axle; defines orientation of wheel. |
| 206 | wheel_radius |
| 207 | Distance from <wheel_pos> to centre of tubular tyre. |
| 208 | tyre_radius |
| 209 | Radius of tubular tyre. |
| 210 | bump_fn |
| 211 | Callable that returns bump value. |
| 212 | o_contact |
| 213 | Out-param for position of tyre contact after compression has been |
| 214 | applied. |
| 215 | o_compression_distance_vertical |
| 216 | Out-param vertical compression distance in metres. |
| 217 | o_compression_norm |
| 218 | Out-param compression as a fraction of maximum compression. |
| 219 | |
| 220 | If on ground, sets out-params for ground contact and returns true. Otherwise |
| 221 | sets out-params for maximum extension of the gear and returns false. |
| 222 | */ |
| 223 | bool gearCompression( |
| 224 | const float (&ground)[4], |
| 225 | const GearVector& compression, |
| 226 | const float (&wheel_pos)[3], |
| 227 | const GearVector& wheel_axle, |
| 228 | float wheel_radius, |
| 229 | float tyre_radius, |
| 230 | std::function< float ()> bump_fn, |
| 231 | float (&o_contact)[3], |
| 232 | float& o_compression_distance_vertical, |
| 233 | float& o_compression_norm |
| 234 | ); |
| 235 | |
| 236 | /* Old calculation for simple contact point. |
| 237 | |
| 238 | ground |
| 239 | Definition of ground plane; points on ground satisfy p.ground[:3] == |
| 240 | ground[4]. Is assumed that for points below ground, p.ground[:3] is |
| 241 | positive. |
| 242 | pos |
| 243 | Position of contact point. |
| 244 | compression |
| 245 | Movement of contact point at maximum compression. |
| 246 | bump_fn |
| 247 | Callable that returns bump value. |
| 248 | o_contact |
| 249 | Position of tyre contact after our compression has been applied. |
| 250 | o_compressDist |
| 251 | Compression distance in metres. |
| 252 | o_compressNorm |
| 253 | Compression as a fraction of maximum compression. |
| 254 | bump_altitude_override |
| 255 | For testing; if >= 0, we use this instead of getting a live value from |
| 256 | getBumpAltitude(). |
| 257 | */ |
| 258 | bool gearCompressionOld( |
| 259 | const float (&ground)[4], |
| 260 | const GearVector& compression, |
| 261 | const float (&pos)[3], |
| 262 | std::function< float ()> bump_fn, |
| 263 | float (&o_contact)[3], |
| 264 | float& o_compression_distance_vertical, |
| 265 | float& o_compression_norm |
| 266 | ); |
| 267 | |
| 268 | }; // namespace yasim |
| 269 | #endif // _GEAR_HPP |