2016-04-21 06:56:44 -03:00
|
|
|
/*
|
|
|
|
This program is free software: you can redistribute it and/or modify
|
|
|
|
it under the terms of the GNU General Public License as published by
|
|
|
|
the Free Software Foundation, either version 3 of the License, or
|
|
|
|
(at your option) any later version.
|
|
|
|
|
|
|
|
This program is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
GNU General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
/*
|
|
|
|
simple electric motor simulator class
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "SIM_Motor.h"
|
|
|
|
#include <AP_Motors/AP_Motors.h>
|
|
|
|
|
|
|
|
using namespace SITL;
|
|
|
|
|
|
|
|
// calculate rotational accel and thrust for a motor
|
2018-07-31 09:33:23 -03:00
|
|
|
void Motor::calculate_forces(const struct sitl_input &input,
|
2016-04-21 06:56:44 -03:00
|
|
|
const float thrust_scale,
|
|
|
|
uint8_t motor_offset,
|
|
|
|
Vector3f &rot_accel,
|
2016-07-03 09:02:09 -03:00
|
|
|
Vector3f &thrust)
|
2016-04-21 06:56:44 -03:00
|
|
|
{
|
2016-05-01 05:06:49 -03:00
|
|
|
// fudge factors
|
|
|
|
const float arm_scale = radians(5000);
|
|
|
|
const float yaw_scale = radians(400);
|
|
|
|
|
|
|
|
// get motor speed from 0 to 1
|
2016-04-21 06:56:44 -03:00
|
|
|
float motor_speed = constrain_float((input.servos[motor_offset+servo]-1100)/900.0, 0, 1);
|
2016-05-01 05:06:49 -03:00
|
|
|
|
|
|
|
// the yaw torque of the motor
|
|
|
|
Vector3f rotor_torque(0, 0, yaw_factor * motor_speed * yaw_scale);
|
|
|
|
|
|
|
|
// get thrust for untilted motor
|
2020-06-04 02:54:29 -03:00
|
|
|
thrust = {0, 0, -motor_speed};
|
2016-05-01 05:06:49 -03:00
|
|
|
|
|
|
|
// define the arm position relative to center of mass
|
|
|
|
Vector3f arm(arm_scale * cosf(radians(angle)), arm_scale * sinf(radians(angle)), 0);
|
|
|
|
|
|
|
|
// work out roll and pitch of motor relative to it pointing straight up
|
|
|
|
float roll = 0, pitch = 0;
|
2016-07-03 09:02:09 -03:00
|
|
|
|
|
|
|
uint64_t now = AP_HAL::micros64();
|
2016-05-01 05:06:49 -03:00
|
|
|
|
|
|
|
// possibly roll and/or pitch the motor
|
2016-04-21 07:29:49 -03:00
|
|
|
if (roll_servo >= 0) {
|
2016-07-03 09:02:09 -03:00
|
|
|
uint16_t servoval = update_servo(input.servos[roll_servo+motor_offset], now, last_roll_value);
|
2016-04-21 20:55:22 -03:00
|
|
|
if (roll_min < roll_max) {
|
2016-04-21 21:26:37 -03:00
|
|
|
roll = constrain_float(roll_min + (servoval-1000)*0.001*(roll_max-roll_min), roll_min, roll_max);
|
2016-04-21 20:55:22 -03:00
|
|
|
} else {
|
2016-04-21 21:26:37 -03:00
|
|
|
roll = constrain_float(roll_max + (2000-servoval)*0.001*(roll_min-roll_max), roll_max, roll_min);
|
2016-04-21 20:55:22 -03:00
|
|
|
}
|
2016-04-21 07:29:49 -03:00
|
|
|
}
|
|
|
|
if (pitch_servo >= 0) {
|
2016-07-03 09:02:09 -03:00
|
|
|
uint16_t servoval = update_servo(input.servos[pitch_servo+motor_offset], now, last_pitch_value);
|
2016-04-21 20:55:22 -03:00
|
|
|
if (pitch_min < pitch_max) {
|
2016-04-21 21:26:37 -03:00
|
|
|
pitch = constrain_float(pitch_min + (servoval-1000)*0.001*(pitch_max-pitch_min), pitch_min, pitch_max);
|
2016-04-21 20:55:22 -03:00
|
|
|
} else {
|
2016-04-21 21:26:37 -03:00
|
|
|
pitch = constrain_float(pitch_max + (2000-servoval)*0.001*(pitch_min-pitch_max), pitch_max, pitch_min);
|
2016-04-21 20:55:22 -03:00
|
|
|
}
|
2016-05-01 05:06:49 -03:00
|
|
|
}
|
2016-07-03 09:02:09 -03:00
|
|
|
last_change_usec = now;
|
2016-05-01 05:06:49 -03:00
|
|
|
|
|
|
|
// possibly rotate the thrust vector and the rotor torque
|
|
|
|
if (!is_zero(roll) || !is_zero(pitch)) {
|
2016-04-21 07:29:49 -03:00
|
|
|
Matrix3f rotation;
|
2016-05-01 05:06:49 -03:00
|
|
|
rotation.from_euler(radians(roll), radians(pitch), 0);
|
2016-04-21 07:29:49 -03:00
|
|
|
thrust = rotation * thrust;
|
2016-05-01 05:06:49 -03:00
|
|
|
rotor_torque = rotation * rotor_torque;
|
2016-04-21 07:29:49 -03:00
|
|
|
}
|
2016-05-01 05:06:49 -03:00
|
|
|
|
|
|
|
// calculate total rotational acceleration
|
|
|
|
rot_accel = (arm % thrust) + rotor_torque;
|
|
|
|
|
|
|
|
// scale the thrust
|
|
|
|
thrust = thrust * thrust_scale;
|
2016-04-21 06:56:44 -03:00
|
|
|
}
|
|
|
|
|
2016-07-03 09:02:09 -03:00
|
|
|
/*
|
|
|
|
update and return current value of a servo. Calculated as 1000..2000
|
|
|
|
*/
|
|
|
|
uint16_t Motor::update_servo(uint16_t demand, uint64_t time_usec, float &last_value)
|
|
|
|
{
|
|
|
|
if (servo_rate <= 0) {
|
|
|
|
return demand;
|
|
|
|
}
|
|
|
|
if (servo_type == SERVO_RETRACT) {
|
|
|
|
// handle retract servos
|
|
|
|
if (demand > 1700) {
|
|
|
|
demand = 2000;
|
|
|
|
} else if (demand < 1300) {
|
|
|
|
demand = 1000;
|
|
|
|
} else {
|
|
|
|
demand = last_value;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
demand = constrain_int16(demand, 1000, 2000);
|
|
|
|
float dt = (time_usec - last_change_usec) * 1.0e-6f;
|
|
|
|
// assume servo moves through 90 degrees over 1000 to 2000
|
|
|
|
float max_change = 1000 * (dt / servo_rate) * 60.0f / 90.0f;
|
|
|
|
last_value = constrain_float(demand, last_value-max_change, last_value+max_change);
|
|
|
|
return uint16_t(last_value+0.5);
|
|
|
|
}
|
2019-01-12 23:07:36 -04:00
|
|
|
|
|
|
|
|
|
|
|
// calculate current and voltage
|
|
|
|
void Motor::current_and_voltage(const struct sitl_input &input, float &voltage, float ¤t,
|
|
|
|
uint8_t motor_offset)
|
|
|
|
{
|
|
|
|
// get motor speed from 0 to 1
|
|
|
|
float motor_speed = constrain_float((input.servos[motor_offset+servo]-1100)/900.0, 0, 1);
|
|
|
|
|
|
|
|
// assume 10A per motor at full speed
|
|
|
|
current = 10 * motor_speed;
|
|
|
|
|
|
|
|
// assume 3S, and full throttle drops voltage by 0.7V
|
2019-03-01 07:06:47 -04:00
|
|
|
if (AP::sitl()) {
|
|
|
|
voltage = AP::sitl()->batt_voltage - motor_speed * 0.7;
|
|
|
|
}
|
2019-01-12 23:07:36 -04:00
|
|
|
}
|