Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

UAVCAN ESC: add min and max parameters for ESC raw commands #13372

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 5 additions & 3 deletions src/drivers/uavcan/actuators/esc.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -109,12 +109,14 @@ UavcanEscController::update_outputs(float *outputs, unsigned num_outputs)
actuator_outputs_s actuator_outputs{};
actuator_outputs.noutputs = num_outputs;

static const int cmd_max = uavcan::equipment::esc::RawCommand::FieldTypes::cmd::RawValueType::max();
const float cmd_min = _run_at_idle_throttle_when_armed ? 1.0F : 0.0F;
const float max_raw_value = uavcan::equipment::esc::RawCommand::FieldTypes::cmd::RawValueType::max();
const float cmd_min = _run_at_idle_throttle_when_armed ? _raw_value_min_factor * max_raw_value : 0.0F;
const int cmd_max = _raw_value_max_factor * max_raw_value - cmd_min;

for (unsigned i = 0; i < num_outputs; i++) {
if (_armed_mask & MOTOR_BIT(i)) {
float scaled = (outputs[i] + 1.0F) * 0.5F * cmd_max;
const float delta_cmd = cmd_max - cmd_min;
float scaled = (outputs[i] + 1.0F) * 0.5F * delta_cmd + cmd_min;

// trim negative values back to minimum
if (scaled < cmd_min) {
Expand Down
4 changes: 4 additions & 0 deletions src/drivers/uavcan/actuators/esc.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,8 @@ class UavcanEscController
void arm_single_esc(int num, bool arm);

void enable_idle_throttle_when_armed(bool value) { _run_at_idle_throttle_when_armed = value; }
void set_esc_min_factor(float value) { _raw_value_min_factor = value; }
void set_esc_max_factor(float value) { _raw_value_max_factor = value; }

/**
* Sets the number of rotors
Expand Down Expand Up @@ -102,6 +104,8 @@ class UavcanEscController

bool _armed{false};
bool _run_at_idle_throttle_when_armed{false};
float _raw_value_min_factor{0.0};
float _raw_value_max_factor{1.0};

esc_status_s _esc_status{};

Expand Down
16 changes: 16 additions & 0 deletions src/drivers/uavcan/uavcan_main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -438,6 +438,22 @@ UavcanNode::update_params()
if (param_handle != PARAM_INVALID) {
param_get(param_handle, &_thr_mdl_factor);
}

param_handle = param_find("UAVCAN_ESC_MIN");

if (param_handle != PARAM_INVALID) {
float _esc_min_factor;
param_get(param_handle, &_esc_min_factor);
_esc_controller.set_esc_min_factor(_esc_min_factor);
}

param_handle = param_find("UAVCAN_ESC_MAX");

if (param_handle != PARAM_INVALID) {
float _esc_max_factor;
param_get(param_find("UAVCAN_ESC_MAX"), &_esc_max_factor);
_esc_controller.set_esc_max_factor(_esc_max_factor);
}
}

int
Expand Down
22 changes: 22 additions & 0 deletions src/drivers/uavcan/uavcan_params.c
Original file line number Diff line number Diff line change
Expand Up @@ -88,3 +88,25 @@ PARAM_DEFINE_INT32(UAVCAN_BITRATE, 1000000);
* @group UAVCAN
*/
PARAM_DEFINE_INT32(UAVCAN_ESC_IDLT, 1);

/**
* UAVCAN ESC min RAW value factor
* Specifies the minimum RAW value of the UAVCAN ESC message
* as a function of the maximum value
*
* @min 0.0
* @max 1.0
* @group UAVCAN
*/
PARAM_DEFINE_FLOAT(UAVCAN_ESC_MIN, 0.0);

/**
* UAVCAN ESC max RAW value factor
* Specifies the maximum RAW value of the UAVCAN ESC message
* as a function of the maximum value
*
* @min 0.0
* @max 1.0
* @group UAVCAN
*/
PARAM_DEFINE_FLOAT(UAVCAN_ESC_MAX, 1.0);