| 1 | // gyro.cxx - simple implementation of a spinning gyro model. |
| 2 | |
| 3 | // Todo: this should be really modelled physically correctly and initialize its own properties etc. |
| 4 | // this way, instruments using a gyro can just refer to it and read the data from the gyro. |
| 5 | |
| 6 | #include "gyro.hxx" |
| 7 | |
| 8 | Gyro::Gyro () |
| 9 | : _serviceable(true), |
| 10 | _power_norm(0.0), |
| 11 | _spin_norm(0.0) |
| 12 | { |
| 13 | } |
| 14 | |
| 15 | Gyro::~Gyro () |
| 16 | { |
| 17 | } |
| 18 | |
| 19 | void Gyro::reinit(void) |
| 20 | { |
| 21 | _power_norm = 0.0; |
| 22 | _spin_norm = 0.0; |
| 23 | _spin_down = 180.0; // about 3 minutes from full spin |
| 24 | _spin_up = 4.0; // up to power-norm; about 4 seconds to full spin |
| 25 | } |
| 26 | |
| 27 | void |
| 28 | Gyro::update (double delta_time_sec) |
| 29 | { |
| 30 | // spin decays 0.5% every second |
| 31 | double spin_decay = (1.0 / _spin_down) * delta_time_sec; |
| 32 | _spin_norm -= spin_decay; |
| 33 | |
| 34 | // power can increase spin by 25% |
| 35 | // every second, but only up to the |
| 36 | // level of power available |
| 37 | if (_serviceable) { |
| 38 | double step = spin_decay + (1.0 / _spin_up) * _power_norm * delta_time_sec; |
| 39 | if ((_spin_norm + step) <= _power_norm) |
| 40 | _spin_norm += step; |
| 41 | } else { |
| 42 | _spin_norm = 0; // stop right away if the gyro breaks |
| 43 | } |
| 44 | |
| 45 | // clamp the spin to 0.0:1.0 |
| 46 | if (_spin_norm < 0.0) |
| 47 | _spin_norm = 0.0; |
| 48 | else if (_spin_norm > 1.0) |
| 49 | _spin_norm = 1.0; |
| 50 | } |
| 51 | |
| 52 | void |
| 53 | Gyro::set_power_norm (double power_norm) |
| 54 | { |
| 55 | _power_norm = power_norm; |
| 56 | } |
| 57 | |
| 58 | double |
| 59 | Gyro::get_spin_norm () const |
| 60 | { |
| 61 | return _spin_norm; |
| 62 | } |
| 63 | |
| 64 | void |
| 65 | Gyro::set_spin_norm (double spin_norm) |
| 66 | { |
| 67 | _spin_norm = spin_norm; |
| 68 | } |
| 69 | |
| 70 | bool |
| 71 | Gyro::is_serviceable () const |
| 72 | { |
| 73 | return _serviceable; |
| 74 | } |
| 75 | |
| 76 | void |
| 77 | Gyro::set_serviceable (bool serviceable) |
| 78 | { |
| 79 | _serviceable = serviceable; |
| 80 | } |
| 81 | |
| 82 | void Gyro::set_spin_up(double spin_up) |
| 83 | { |
| 84 | _spin_up = spin_up; |
| 85 | } |
| 86 | |
| 87 | void Gyro::set_spin_down(double spin_down) |
| 88 | { |
| 89 | _spin_down = spin_down; |
| 90 | } |
| 91 | |
| 92 | |
| 93 | // end of gyro.cxx |
| 94 |