Skip to content

Commit

Permalink
add can message to handle updating gripper jaw idle value
Browse files Browse the repository at this point in the history
  • Loading branch information
ahiuchingau committed Dec 8, 2023
1 parent de7fbaf commit e641233
Show file tree
Hide file tree
Showing 10 changed files with 88 additions and 11 deletions.
3 changes: 3 additions & 0 deletions include/bootloader/core/ids.h
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,9 @@ typedef enum {
can_messageid_set_gripper_error_tolerance = 0x47,
can_messageid_gripper_jaw_state_request = 0x48,
can_messageid_gripper_jaw_state_response = 0x49,
can_messageid_set_gripper_jaw_holdoff_request = 0x401,
can_messageid_gripper_jaw_holdoff_request = 0x402,
can_messageid_gripper_jaw_holdoff_response = 0x403,
can_messageid_acknowledgement = 0x50,
can_messageid_read_presence_sensing_voltage_request = 0x600,
can_messageid_read_presence_sensing_voltage_response = 0x601,
Expand Down
3 changes: 3 additions & 0 deletions include/can/core/ids.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,9 @@ enum class MessageId {
set_gripper_error_tolerance = 0x47,
gripper_jaw_state_request = 0x48,
gripper_jaw_state_response = 0x49,
set_gripper_jaw_holdoff_request = 0x401,
gripper_jaw_holdoff_request = 0x402,
gripper_jaw_holdoff_response = 0x403,
acknowledgement = 0x50,
read_presence_sensing_voltage_request = 0x600,
read_presence_sensing_voltage_response = 0x601,
Expand Down
3 changes: 2 additions & 1 deletion include/can/core/message_handlers/motion.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,8 @@ class BrushedMotionHandler {
std::variant<std::monostate, DisableMotorRequest, EnableMotorRequest,
ReadLimitSwitchRequest, MotorPositionRequest,
SetGripperErrorToleranceRequest, GetMotorUsageRequest,
GripperJawStateRequest>;
GripperJawStateRequest, SetGripperJawHoldoffRequest,
GripperJawHoldoffRequest>;

BrushedMotionHandler(BrushedMotionTaskClient &motion_client)
: motion_client{motion_client} {}
Expand Down
42 changes: 41 additions & 1 deletion include/can/core/messages.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -1521,6 +1521,46 @@ struct GripperJawStateResponse
-> bool = default;
};

struct SetGripperJawHoldoffRequest
: BaseMessage<MessageId::set_gripper_jaw_holdoff_request> {
uint32_t message_index;
uint32_t holdoff_ticks;

template <bit_utils::ByteIterator Input, typename Limit>
static auto parse(Input body, Limit limit)
-> SetGripperJawHoldoffRequest {
uint32_t holdoff_ticks = 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);
return SetGripperJawHoldoffRequest{
.message_index = msg_ind,
.holdoff_ticks = holdoff_ticks};
}
auto operator==(const SetGripperJawHoldoffRequest& other) const
-> bool = default;
};

using GripperJawHoldoffRequest = Empty<MessageId::gripper_jaw_holdoff_request>;

struct GripperJawHoldoffResponse
: BaseMessage<MessageId::gripper_jaw_holdoff_response> {
uint32_t message_index;
uint32_t holdoff_ticks;

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);
return iter - body;
}

auto operator==(const GripperJawHoldoffResponse& other) const
-> bool = default;
};

/**
* A variant of all message types we might send..
*/
Expand All @@ -1537,6 +1577,6 @@ using ResponseMessageType = std::variant<
PeripheralStatusResponse, BrushedMotorConfResponse,
UpdateMotorPositionEstimationResponse, BaselineSensorResponse,
PushTipPresenceNotification, GetMotorUsageResponse,
GripperJawStateResponse>;
GripperJawStateResponse, GripperJawHoldoffResponse>;

} // namespace can::messages
3 changes: 2 additions & 1 deletion include/gripper/core/can_task.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,8 @@ using BrushedMotionDispatchTarget = can::dispatch::DispatchParseTarget<
can::messages::DisableMotorRequest, can::messages::EnableMotorRequest,
can::messages::ReadLimitSwitchRequest, can::messages::MotorPositionRequest,
can::messages::SetGripperErrorToleranceRequest,
can::messages::GetMotorUsageRequest, can::messages::GripperJawStateRequest>;
can::messages::GetMotorUsageRequest, can::messages::GripperJawStateRequest,
can::messages::SetGripperJawHoldoffRequest, can::messages::GripperJawHoldoffRequest>;
using BrushedMoveGroupDispatchTarget = can::dispatch::DispatchParseTarget<
can::message_handlers::move_group::BrushedMoveGroupHandler<
g_tasks::QueueClient>,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,14 @@ class MotionController {
S15Q16_RADIX));
}

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

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

template <usage_storage_task::TaskClient UsageClient>
void send_usage_data(uint32_t message_index, UsageClient& usage_client) {
usage_messages::GetUsageRequest req = {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,6 @@ using namespace motor_messages;
*
*/

static constexpr uint32_t HOLDOFF_TICKS =
32; // 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

template <template <class> class QueueImpl,
move_status_reporter_task::BrushedTaskClient StatusClient>
requires MessageQueue<QueueImpl<BrushedMove>, BrushedMove>
Expand Down Expand Up @@ -207,7 +202,7 @@ class BrushedMotorInterruptHandler {
}
cancel_and_clear_moves(can::ids::ErrorCode::stop_requested,
can::ids::ErrorSeverity::warning);
} else if (tick < HOLDOFF_TICKS) {
} else if (tick < error_conf.idle_holdoff_ticks) {
tick++;
} else if (_has_active_move) {
execute_active_move();
Expand All @@ -221,7 +216,7 @@ class BrushedMotorInterruptHandler {
* the encoder is idle */
return !(buffered_move.stop_condition ==
MoveStopCondition::limit_switch ||
tick < HOLDOFF_TICKS);
tick < error_conf.idle_holdoff_ticks);
}

void set_enc_idle_state(bool val) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,11 @@ namespace error_tolerance_config {
static constexpr double ACCEPTABLE_DISTANCE_TOLERANCE_MM = 2;
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;

class BrushedMotorErrorTolerance {
public:
BrushedMotorErrorTolerance(
Expand All @@ -19,6 +24,7 @@ class BrushedMotorErrorTolerance {
unwanted_movement_threshold =
int32_t(gear_conf.get_encoder_pulses_per_mm() *
UNWANTED_MOVEMENT_DISTANCE_MM);
idle_holdoff_ticks = uint32_t(IDLE_HOLDOFF_TICKS);
}

void update_tolerance(double pos_error, double unwanted_movement) {
Expand All @@ -27,9 +33,14 @@ class BrushedMotorErrorTolerance {
unwanted_movement_threshold =
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;
}
lms::LinearMotionSystemConfig<lms::GearBoxConfig>& gear_conf;
int32_t acceptable_position_error = 0;
int32_t unwanted_movement_threshold = 0;
uint32_t idle_holdoff_ticks = 0;
};

} // namespace error_tolerance_config
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,20 @@ class MotionControllerMessageHandler {
can_client.send_can_message(can::ids::NodeId::host, msg);
}

void handle(const can::messages::SetGripperJawHoldoffRequest& m) {
controller.set_idle_holdoff(m);
can_client.send_can_message(can::ids::NodeId::host,
can::messages::ack_from_request(m));
}

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};
can_client.send_can_message(can::ids::NodeId::host, msg);
}

brushed_motion_controller::MotionController<MEConfig>& controller;
CanClient& can_client;
UsageClient& usage_client;
Expand Down
3 changes: 2 additions & 1 deletion include/motor-control/core/tasks/messages.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,8 @@ using BrushedMotionControllerTaskMessage = std::variant<
can::messages::AddBrushedLinearMoveRequest, can::messages::StopRequest,
can::messages::ReadLimitSwitchRequest, can::messages::MotorPositionRequest,
can::messages::SetGripperErrorToleranceRequest,
can::messages::GetMotorUsageRequest, can::messages::GripperJawStateRequest>;
can::messages::GetMotorUsageRequest, can::messages::GripperJawStateRequest,
can::messages::SetGripperJawHoldoffRequest, can::messages::GripperJawHoldoffRequest>;

using BrushedMoveGroupTaskMessage = std::variant<
std::monostate, can::messages::ClearAllMoveGroupsRequest,
Expand Down

0 comments on commit e641233

Please sign in to comment.