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

Refactor pulse width calculation for kick into its own function #3351

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
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
32 changes: 24 additions & 8 deletions src/proto/message_translation/power_frame_msg.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -112,13 +112,30 @@ TbotsProto_PowerStatus inline createNanoPbPowerStatus(
return status;
}

/**
* Calculates width of the pulse for chicker
* @param kick_constant The constant to use in the kick speed to pulse width conversion
* @param kick_coefficient The coefficient to use in the kick speed to pulse width
* conversion
* @param speed Speed of the desired kick in m/s
* @return Width of pulse
*/
inline uint32_t calculateChickerPulseWidth(int kick_constant, double kick_coefficient,
float speed)
{
return static_cast<uint32_t>(kick_constant * std::exp(kick_coefficient * speed));
}

// These functions are only used in power service so they are excluded from platformio
// Protobuf needs additional support to run on an esp32
#ifndef PLATFORMIO_BUILD
/**
* Converts a google protobuf power control msg to its nanopb representation
*
* @param google_control protobuf message to convert
* @param kick_coeff The coefficient to use for the chicker pulse width calculation
* @param kick_constant The constant to use in the chicker pulse width calculation
* @param chip_pulse_width The width of the pulse to use for the chipper
* @return a nanopb power control msg matching provided protobuf
*/

Expand All @@ -139,9 +156,8 @@ TbotsProto_PowerPulseControl inline createNanoPbPowerPulseControl(
nanopb_control.chicker.which_chicker_command =
TbotsProto_PowerPulseControl_ChickerControl_kick_pulse_width_tag;
nanopb_control.chicker.chicker_command.kick_pulse_width =
static_cast<uint32_t>(
kick_constant *
std::exp(kick_coeff * google_control.chicker().kick_speed_m_per_s()));
calculateChickerPulseWidth(kick_constant, kick_coeff,
google_control.chicker().kick_speed_m_per_s());
break;
case TbotsProto::PowerControl::ChickerControl::kChipDistanceMeters:
nanopb_control.chicker.which_chicker_command =
Expand All @@ -158,11 +174,11 @@ TbotsProto_PowerPulseControl inline createNanoPbPowerPulseControl(
.which_auto_chip_or_kick =
TbotsProto_PowerPulseControl_AutoChipOrKick_autokick_pulse_width_tag;
nanopb_control.chicker.chicker_command.auto_chip_or_kick
.auto_chip_or_kick.autokick_pulse_width = static_cast<uint32_t>(
kick_constant *
std::exp(kick_coeff * google_control.chicker()
.auto_chip_or_kick()
.autokick_speed_m_per_s()));
.auto_chip_or_kick.autokick_pulse_width =
calculateChickerPulseWidth(kick_constant, kick_coeff,
google_control.chicker()
.auto_chip_or_kick()
.autokick_speed_m_per_s());
break;
case TbotsProto::AutoChipOrKick::kAutochipDistanceMeters:
nanopb_control.chicker.chicker_command.auto_chip_or_kick
Expand Down
Loading