SIM_Motor.h 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. /*
  2. This program is free software: you can redistribute it and/or modify
  3. it under the terms of the GNU General Public License as published by
  4. the Free Software Foundation, either version 3 of the License, or
  5. (at your option) any later version.
  6. This program is distributed in the hope that it will be useful,
  7. but WITHOUT ANY WARRANTY; without even the implied warranty of
  8. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  9. GNU General Public License for more details.
  10. You should have received a copy of the GNU General Public License
  11. along with this program. If not, see <http://www.gnu.org/licenses/>.
  12. */
  13. /*
  14. simple electric motor simulation class
  15. */
  16. #pragma once
  17. #include "SIM_Aircraft.h"
  18. namespace SITL {
  19. /*
  20. class to describe a motor position
  21. */
  22. class Motor {
  23. public:
  24. float angle;
  25. float yaw_factor;
  26. uint8_t servo;
  27. uint8_t display_order;
  28. // support for tilting motors
  29. int8_t roll_servo = -1;
  30. float roll_min, roll_max;
  31. int8_t pitch_servo = -1;
  32. float pitch_min, pitch_max;
  33. // support for servo slew rate
  34. enum {SERVO_NORMAL, SERVO_RETRACT} servo_type;
  35. float servo_rate = 0.24; // seconds per 60 degrees
  36. uint64_t last_change_usec;
  37. float last_roll_value, last_pitch_value;
  38. Motor(uint8_t _servo, float _angle, float _yaw_factor, uint8_t _display_order) :
  39. servo(_servo), // what servo output drives this motor
  40. angle(_angle), // angle in degrees from front
  41. yaw_factor(_yaw_factor), // positive is clockwise
  42. display_order(_display_order) // order for clockwise display
  43. {}
  44. /*
  45. alternative constructor for tiltable motors
  46. */
  47. Motor(uint8_t _servo, float _angle, float _yaw_factor, uint8_t _display_order,
  48. int8_t _roll_servo, float _roll_min, float _roll_max,
  49. int8_t _pitch_servo, float _pitch_min, float _pitch_max) :
  50. servo(_servo), // what servo output drives this motor
  51. angle(_angle), // angle in degrees from front
  52. yaw_factor(_yaw_factor), // positive is clockwise
  53. display_order(_display_order), // order for clockwise display
  54. roll_servo(_roll_servo),
  55. roll_min(_roll_min),
  56. roll_max(_roll_max),
  57. pitch_servo(_pitch_servo),
  58. pitch_min(_pitch_min),
  59. pitch_max(_pitch_max)
  60. {}
  61. void calculate_forces(const struct sitl_input &input,
  62. float thrust_scale,
  63. uint8_t motor_offset,
  64. Vector3f &rot_accel, // rad/sec
  65. Vector3f &body_thrust); // Z is down
  66. uint16_t update_servo(uint16_t demand, uint64_t time_usec, float &last_value);
  67. // calculate current and voltage
  68. void current_and_voltage(const struct sitl_input &input, float &voltage, float &current, uint8_t motor_offset);
  69. };
  70. }