2020-08-06 22:37:36 -03:00
|
|
|
#include "AP_Winch_PWM.h"
|
2022-03-03 23:55:36 -04:00
|
|
|
|
2023-03-02 20:48:38 -04:00
|
|
|
#if AP_WINCH_PWM_ENABLED
|
|
|
|
|
2022-03-03 23:55:36 -04:00
|
|
|
#include <AP_Logger/AP_Logger.h>
|
2020-07-24 05:29:25 -03:00
|
|
|
#include <GCS_MAVLink/GCS.h>
|
2023-01-24 17:39:44 -04:00
|
|
|
#include <SRV_Channel/SRV_Channel.h>
|
2017-10-04 23:19:22 -03:00
|
|
|
|
|
|
|
extern const AP_HAL::HAL& hal;
|
|
|
|
|
2020-07-24 05:29:25 -03:00
|
|
|
// true if winch is healthy
|
2020-08-06 22:37:36 -03:00
|
|
|
bool AP_Winch_PWM::healthy() const
|
2017-10-04 23:19:22 -03:00
|
|
|
{
|
2020-07-24 05:29:25 -03:00
|
|
|
// return immediately if no servo is assigned to control the winch
|
|
|
|
if (!SRV_Channels::function_assigned(SRV_Channel::k_winch)) {
|
|
|
|
return false;
|
|
|
|
}
|
2017-10-04 23:19:22 -03:00
|
|
|
|
2020-07-24 05:29:25 -03:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2020-08-06 22:37:36 -03:00
|
|
|
void AP_Winch_PWM::update()
|
2017-10-04 23:19:22 -03:00
|
|
|
{
|
|
|
|
// return immediately if no servo is assigned to control the winch
|
|
|
|
if (!SRV_Channels::function_assigned(SRV_Channel::k_winch)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-07-24 05:29:25 -03:00
|
|
|
// read pilot input
|
|
|
|
read_pilot_desired_rate();
|
2017-10-04 23:19:22 -03:00
|
|
|
|
2020-07-24 05:29:25 -03:00
|
|
|
// send outputs to winch
|
|
|
|
control_winch();
|
|
|
|
}
|
2017-10-04 23:19:22 -03:00
|
|
|
|
2020-07-24 05:29:25 -03:00
|
|
|
// update pwm outputs to control winch
|
2020-08-06 22:37:36 -03:00
|
|
|
void AP_Winch_PWM::control_winch()
|
2020-07-24 05:29:25 -03:00
|
|
|
{
|
2020-08-06 22:23:27 -03:00
|
|
|
const uint32_t now_ms = AP_HAL::millis();
|
2022-03-11 13:35:51 -04:00
|
|
|
float dt = (now_ms - control_update_ms) * 0.001f;
|
2017-10-04 23:19:22 -03:00
|
|
|
if (dt > 1.0f) {
|
|
|
|
dt = 0.0f;
|
|
|
|
}
|
2020-07-24 05:29:25 -03:00
|
|
|
control_update_ms = now_ms;
|
2017-10-04 23:19:22 -03:00
|
|
|
|
2020-07-24 05:29:25 -03:00
|
|
|
// if relaxed stop outputting pwm signals
|
|
|
|
if (config.control_mode == AP_Winch::ControlMode::RELAXED) {
|
|
|
|
// if not doing any control stop sending pwm to winch
|
|
|
|
SRV_Channels::set_output_pwm(SRV_Channel::k_winch, 0);
|
2017-10-04 23:19:22 -03:00
|
|
|
|
2020-07-24 05:29:25 -03:00
|
|
|
// rate used for acceleration limiting reset to zero
|
|
|
|
set_previous_rate(0.0f);
|
|
|
|
return;
|
|
|
|
}
|
2017-10-04 23:19:22 -03:00
|
|
|
|
|
|
|
// if doing position control, calculate position error to desired rate
|
2020-07-24 05:29:25 -03:00
|
|
|
if ((config.control_mode == AP_Winch::ControlMode::POSITION) && healthy()) {
|
|
|
|
float position_error = config.length_desired - line_length;
|
|
|
|
config.rate_desired = constrain_float(position_error * config.pos_p, -config.rate_max, config.rate_max);
|
2017-10-04 23:19:22 -03:00
|
|
|
}
|
|
|
|
|
2020-07-24 05:29:25 -03:00
|
|
|
// apply acceleration limited to rate
|
|
|
|
const float rate_limited = get_rate_limited_by_accel(config.rate_desired, dt);
|
|
|
|
|
|
|
|
// use linear interpolation to calculate output to move winch at desired rate
|
2021-09-18 14:57:26 -03:00
|
|
|
const float scaled_output = linear_interpolate(-1000, 1000, rate_limited, -config.rate_max, config.rate_max);
|
2020-07-24 05:29:25 -03:00
|
|
|
SRV_Channels::set_output_scaled(SRV_Channel::k_winch, scaled_output);
|
2017-10-04 23:19:22 -03:00
|
|
|
|
2020-07-24 05:29:25 -03:00
|
|
|
// update distance estimate assuming winch will move exactly as requested
|
|
|
|
line_length += config.rate_desired * dt;
|
|
|
|
}
|
|
|
|
|
|
|
|
//send generator status
|
2020-08-06 22:37:36 -03:00
|
|
|
void AP_Winch_PWM::send_status(const GCS_MAVLINK &channel)
|
2020-07-24 05:29:25 -03:00
|
|
|
{
|
|
|
|
// prepare status bitmask
|
|
|
|
uint32_t status_bitmask = 0;
|
|
|
|
if (healthy()) {
|
|
|
|
status_bitmask |= MAV_WINCH_STATUS_HEALTHY;
|
2017-10-04 23:19:22 -03:00
|
|
|
}
|
|
|
|
|
2020-07-24 05:29:25 -03:00
|
|
|
// send status
|
|
|
|
mavlink_msg_winch_status_send(
|
|
|
|
channel.get_chan(),
|
|
|
|
AP_HAL::micros64(), // time_usec
|
|
|
|
line_length, // line_length
|
|
|
|
config.rate_desired, // speed
|
|
|
|
std::numeric_limits<double>::quiet_NaN(), // tension
|
|
|
|
std::numeric_limits<double>::quiet_NaN(), // voltage
|
|
|
|
std::numeric_limits<double>::quiet_NaN(), // current
|
|
|
|
INT16_MAX, // temperature
|
|
|
|
status_bitmask); // status flags
|
|
|
|
}
|
2017-10-04 23:19:22 -03:00
|
|
|
|
2020-07-24 05:29:25 -03:00
|
|
|
// write log
|
2020-08-06 22:37:36 -03:00
|
|
|
void AP_Winch_PWM::write_log()
|
2020-07-24 05:29:25 -03:00
|
|
|
{
|
|
|
|
AP::logger().Write_Winch(
|
|
|
|
healthy(),
|
|
|
|
0, // thread end (unsupported)
|
|
|
|
0, // moving (unsupported)
|
|
|
|
0, // clutch (unsupported)
|
|
|
|
(uint8_t)config.control_mode,
|
|
|
|
config.length_desired,
|
|
|
|
get_current_length(),
|
|
|
|
config.rate_desired,
|
|
|
|
0, // tension (unsupported)
|
|
|
|
0, // voltage (unsupported)
|
|
|
|
0); // temp (unsupported)
|
2017-10-04 23:19:22 -03:00
|
|
|
}
|
2023-03-02 20:48:38 -04:00
|
|
|
|
|
|
|
#endif // AP_WINCH_PWM_ENABLED
|