Skip to content

Commit

Permalink
use ms as unit instead of ticks for can message
Browse files Browse the repository at this point in the history
  • Loading branch information
ahiuchingau committed Dec 14, 2023
1 parent 80180c4 commit 17e62f1
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 17 deletions.
2 changes: 1 addition & 1 deletion gripper/firmware/interfaces_grip_motor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ static lms::LinearMotionSystemConfig<lms::GearBoxConfig> gear_config{
.encoder_pulses_per_rev = 512};

static error_tolerance_config::BrushedMotorErrorTolerance error_conf(
gear_config);
gear_config, double(GRIPPER_JAW_PWM_FREQ_HZ));

static brushed_motor::BrushedMotor grip_motor(gear_config,
brushed_motor_hardware_iface,
Expand Down
14 changes: 7 additions & 7 deletions include/can/core/messages.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -1524,17 +1524,17 @@ struct GripperJawStateResponse
struct SetGripperJawHoldoffRequest
: BaseMessage<MessageId::set_gripper_jaw_holdoff_request> {
uint32_t message_index;
uint32_t holdoff_ticks;
uint32_t holdoff_ms;

template <bit_utils::ByteIterator Input, typename Limit>
static auto parse(Input body, Limit limit) -> SetGripperJawHoldoffRequest {
uint32_t holdoff_ticks = 0;
uint32_t holdoff_ms = 0;
uint32_t msg_ind = 0;

body = bit_utils::bytes_to_int(body, limit, msg_ind);
body = bit_utils::bytes_to_int(body, limit, holdoff_ticks);
body = bit_utils::bytes_to_int(body, limit, holdoff_ms);
return SetGripperJawHoldoffRequest{.message_index = msg_ind,
.holdoff_ticks = holdoff_ticks};
.holdoff_ms = holdoff_ms};
}
auto operator==(const SetGripperJawHoldoffRequest& other) const
-> bool = default;
Expand All @@ -1545,13 +1545,13 @@ using GripperJawHoldoffRequest = Empty<MessageId::gripper_jaw_holdoff_request>;
struct GripperJawHoldoffResponse
: BaseMessage<MessageId::gripper_jaw_holdoff_response> {
uint32_t message_index;
uint32_t holdoff_ticks;
uint32_t holdoff_ms;

template <bit_utils::ByteIterator Output, typename Limit>
auto serialize(Output body, Limit limit) const -> uint8_t {
auto iter = bit_utils::int_to_bytes(message_index, body, limit);
iter = bit_utils::int_to_bytes(static_cast<uint32_t>(holdoff_ticks),
iter, limit);
iter = bit_utils::int_to_bytes(static_cast<uint32_t>(holdoff_ms), iter,
limit);
return iter - body;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -133,11 +133,13 @@ class MotionController {

void set_idle_holdoff(
const can::messages::SetGripperJawHoldoffRequest& can_msg) {
error_config.update_idle_holdoff_ticks(can_msg.holdoff_ticks);
error_config.update_idle_holdoff_ticks(
fixed_point_to_float(can_msg.holdoff_ms, S15Q16_RADIX));
}

auto get_idle_holdoff() -> uint32_t {
return error_config.idle_holdoff_ticks;
return convert_to_fixed_point(error_config.idle_holdoff_ticks,
S15Q16_RADIX);
}

template <usage_storage_task::TaskClient UsageClient>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,20 +11,21 @@ static constexpr double UNWANTED_MOVEMENT_DISTANCE_MM = 2;
// hold off for 1 ms (with a 32k Hz timer)
// using the logic analyzer it takes about 0.2-0.3 ms for the output
// to stablize after changing directions of the PWM
static constexpr uint32_t IDLE_HOLDOFF_TICKS = 32;
static constexpr double IDLE_HOLDOFF_MS = 1;

class BrushedMotorErrorTolerance {
public:
BrushedMotorErrorTolerance(
lms::LinearMotionSystemConfig<lms::GearBoxConfig>& gearbox_config)
: gear_conf(gearbox_config) {
lms::LinearMotionSystemConfig<lms::GearBoxConfig>& gearbox_config,
double pwm_timer_freq)
: gear_conf(gearbox_config), pwm_freq(pwm_timer_freq) {
acceptable_position_error =
int32_t(gear_conf.get_encoder_pulses_per_mm() *
ACCEPTABLE_DISTANCE_TOLERANCE_MM);
unwanted_movement_threshold =
int32_t(gear_conf.get_encoder_pulses_per_mm() *
UNWANTED_MOVEMENT_DISTANCE_MM);
idle_holdoff_ticks = uint32_t(IDLE_HOLDOFF_TICKS);
idle_holdoff_ticks = uint32_t(pwm_freq / 1000 * IDLE_HOLDOFF_MS);
}

void update_tolerance(double pos_error, double unwanted_movement) {
Expand All @@ -34,10 +35,11 @@ class BrushedMotorErrorTolerance {
int32_t(gear_conf.get_encoder_pulses_per_mm() * unwanted_movement);
}

void update_idle_holdoff_ticks(uint32_t holdoff_ticks) {
idle_holdoff_ticks = holdoff_ticks;
void update_idle_holdoff_ticks(double holdoff_ms) {
idle_holdoff_ticks = uint32_t(pwm_freq / 1000 * holdoff_ms);
}
lms::LinearMotionSystemConfig<lms::GearBoxConfig>& gear_conf;
double pwm_freq;
int32_t acceptable_position_error = 0;
int32_t unwanted_movement_threshold = 0;
uint32_t idle_holdoff_ticks = 0;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ class MotionControllerMessageHandler {
void handle(const can::messages::GripperJawHoldoffRequest& m) {
auto ticks = controller.get_idle_holdoff();
can::messages::GripperJawHoldoffResponse msg{
.message_index = m.message_index, .holdoff_ticks = ticks};
.message_index = m.message_index, .holdoff_ms = ticks};
can_client.send_can_message(can::ids::NodeId::host, msg);
}

Expand Down

0 comments on commit 17e62f1

Please sign in to comment.