|
@@ -0,0 +1,758 @@
|
|
|
|
|
+`timescale 1ns/1ps
|
|
|
|
|
+
|
|
|
|
|
+// Per-axis door percent FSM: cal seek-close → measure-open, then runtime
|
|
|
|
|
+// mid/endpoint motion with latest-wins replan. Position only on gated_step_rise.
|
|
|
|
|
+module door_axis_controller #(
|
|
|
|
|
+ parameter integer AXIS_ID = 0,
|
|
|
|
|
+ parameter integer MAX_CAL_STEPS = 5000,
|
|
|
|
|
+ parameter integer MIN_TRAVEL = 2,
|
|
|
|
|
+ parameter integer ENDPOINT_GUARD_EXTRA = 500,
|
|
|
|
|
+ parameter integer DIR_SETUP_TICKS = 2,
|
|
|
|
|
+ parameter integer PERCENT_MAX = 100
|
|
|
|
|
+) (
|
|
|
|
|
+ input wire clk,
|
|
|
|
|
+ input wire rst_n,
|
|
|
|
|
+ input wire open_limit,
|
|
|
|
|
+ input wire close_limit,
|
|
|
|
|
+ input wire limit_sync_valid,
|
|
|
|
|
+ input wire gated_step,
|
|
|
|
|
+ input wire gated_step_rise,
|
|
|
|
|
+ input wire tick_500us,
|
|
|
|
|
+ input wire cal_enabled,
|
|
|
|
|
+ input wire exec_toggle_pulse,
|
|
|
|
|
+ input wire request_valid,
|
|
|
|
|
+ input wire [7:0] requested_percent_in,
|
|
|
|
|
+ input wire watchdog_stop,
|
|
|
|
|
+ output reg planner_req,
|
|
|
|
|
+ output wire [7:0] planner_percent,
|
|
|
|
|
+ output wire [12:0] planner_travel,
|
|
|
|
|
+ input wire planner_done,
|
|
|
|
|
+ input wire [1:0] planner_done_axis,
|
|
|
|
|
+ input wire [7:0] planner_done_percent,
|
|
|
|
|
+ input wire [12:0] planner_result,
|
|
|
|
|
+ output reg step,
|
|
|
|
|
+ output reg dir_close,
|
|
|
|
|
+ output reg [2:0] status_state,
|
|
|
|
|
+ output reg [7:0] status_flags,
|
|
|
|
|
+ output reg [7:0] status_requested_percent,
|
|
|
|
|
+ output reg [12:0] status_current,
|
|
|
|
|
+ output reg [12:0] status_target,
|
|
|
|
|
+ output reg [12:0] status_travel,
|
|
|
|
|
+ output reg [7:0] status_fault_code,
|
|
|
|
|
+ output wire calibrated,
|
|
|
|
|
+ output wire faulted,
|
|
|
|
|
+ output wire terminal,
|
|
|
|
|
+ // Shared DDA executor observables
|
|
|
|
|
+ output wire dda_cal_active,
|
|
|
|
|
+ output wire dda_run_active,
|
|
|
|
|
+ output wire dda_allow_toggle,
|
|
|
|
|
+ output wire dda_endpoint_crawl,
|
|
|
|
|
+ output wire [12:0] dda_distance,
|
|
|
|
|
+ output wire [12:0] dda_done_steps,
|
|
|
|
|
+ output wire [12:0] dda_ramp_unit,
|
|
|
|
|
+ output wire [12:0] dda_travel_steps
|
|
|
|
|
+);
|
|
|
|
|
+ localparam [2:0] ST_CAL_SEEK_CLOSE = 3'd0;
|
|
|
|
|
+ localparam [2:0] ST_CAL_MEASURE_OPEN = 3'd1;
|
|
|
|
|
+ localparam [2:0] ST_IDLE = 3'd2;
|
|
|
|
|
+ localparam [2:0] ST_MOVE_OPEN = 3'd3;
|
|
|
|
|
+ localparam [2:0] ST_MOVE_CLOSE = 3'd4;
|
|
|
|
|
+ localparam [2:0] ST_WAIT_REPLAN = 3'd5;
|
|
|
|
|
+ localparam [2:0] ST_FAULT = 3'd6;
|
|
|
|
|
+
|
|
|
|
|
+ localparam [2:0] FC_NONE = 3'd0;
|
|
|
|
|
+ localparam [2:0] FC_BOTH_LIMITS = 3'd1;
|
|
|
|
|
+ localparam [2:0] FC_CAL_CLOSE_TIMEOUT = 3'd2;
|
|
|
|
|
+ localparam [2:0] FC_CAL_OPEN_TIMEOUT = 3'd3;
|
|
|
|
|
+ localparam [2:0] FC_TINY_TRAVEL = 3'd4;
|
|
|
|
|
+ localparam [2:0] FC_ENDPOINT_TIMEOUT = 3'd5;
|
|
|
|
|
+
|
|
|
|
|
+ localparam [2:0] PH_NONE = 3'd0;
|
|
|
|
|
+ localparam [2:0] PH_DIR_SETUP = 3'd1;
|
|
|
|
|
+ localparam [2:0] PH_RUN = 3'd2;
|
|
|
|
|
+ localparam [2:0] PH_DRAIN_HIGH = 3'd3;
|
|
|
|
|
+
|
|
|
|
|
+ localparam integer CNT_W = 13;
|
|
|
|
|
+ localparam [CNT_W-1:0] MAX_STEPS_W = MAX_CAL_STEPS[CNT_W-1:0];
|
|
|
|
|
+ localparam [CNT_W-1:0] MIN_TRAVEL_W = MIN_TRAVEL[CNT_W-1:0];
|
|
|
|
|
+ localparam [CNT_W-1:0] CAL_RAMP_UNIT_W = 13'd8;
|
|
|
|
|
+
|
|
|
|
|
+ reg calibrated_r;
|
|
|
|
|
+ reg [2:0] fault_code;
|
|
|
|
|
+ reg [CNT_W-1:0] travel_steps;
|
|
|
|
|
+ reg [CNT_W-1:0] current_position;
|
|
|
|
|
+ reg [CNT_W-1:0] target_position;
|
|
|
|
|
+ reg [7:0] requested_percent;
|
|
|
|
|
+ reg replan_pending;
|
|
|
|
|
+ reg endpoint_mode;
|
|
|
|
|
+ reg target_valid;
|
|
|
|
|
+ reg moving;
|
|
|
|
|
+ reg [2:0] phase;
|
|
|
|
|
+ reg [3:0] setup_count;
|
|
|
|
|
+ reg [CNT_W-1:0] motion_count;
|
|
|
|
|
+ reg allow_rise;
|
|
|
|
|
+ reg stop_after_budget;
|
|
|
|
|
+ reg cal_started;
|
|
|
|
|
+ reg need_planner;
|
|
|
|
|
+ reg wd_stopped;
|
|
|
|
|
+ reg open_limit_armed;
|
|
|
|
|
+ reg close_limit_armed;
|
|
|
|
|
+ reg [CNT_W-1:0] move_distance;
|
|
|
|
|
+ reg [CNT_W-1:0] move_ramp_unit;
|
|
|
|
|
+ reg [CNT_W-1:0] curve_done_count;
|
|
|
|
|
+
|
|
|
|
|
+ assign calibrated = calibrated_r;
|
|
|
|
|
+ assign faulted = (status_state == ST_FAULT);
|
|
|
|
|
+ assign terminal = ((status_state == ST_IDLE) && calibrated_r) || (status_state == ST_FAULT);
|
|
|
|
|
+ assign planner_percent = requested_percent;
|
|
|
|
|
+ assign planner_travel = travel_steps;
|
|
|
|
|
+
|
|
|
|
|
+ wire both_limits = open_limit && close_limit;
|
|
|
|
|
+ wire mid_request = (requested_percent > 8'd0) && (requested_percent < PERCENT_MAX[7:0]);
|
|
|
|
|
+ wire is_open_endpoint = (requested_percent == 8'd0);
|
|
|
|
|
+ wire is_close_endpoint = (requested_percent == PERCENT_MAX[7:0]);
|
|
|
|
|
+ wire planner_accept = planner_done && (planner_done_axis == AXIS_ID[1:0]) &&
|
|
|
|
|
+ (planner_done_percent == requested_percent) && calibrated_r &&
|
|
|
|
|
+ (status_state != ST_FAULT) && mid_request && need_planner;
|
|
|
|
|
+ wire [CNT_W-1:0] guard_limit = travel_steps + ENDPOINT_GUARD_EXTRA[CNT_W-1:0];
|
|
|
|
|
+ wire in_calibration = (status_state == ST_CAL_SEEK_CLOSE) ||
|
|
|
|
|
+ (status_state == ST_CAL_MEASURE_OPEN);
|
|
|
|
|
+ wire open_assert_event = open_limit && open_limit_armed;
|
|
|
|
|
+ wire close_assert_event = close_limit && close_limit_armed;
|
|
|
|
|
+ wire departing_close_source =
|
|
|
|
|
+ calibrated_r && !in_calibration &&
|
|
|
|
|
+ ((status_state == ST_MOVE_OPEN) || (moving && !dir_close));
|
|
|
|
|
+ wire departing_open_source =
|
|
|
|
|
+ calibrated_r && !in_calibration &&
|
|
|
|
|
+ ((status_state == ST_MOVE_CLOSE) || (moving && dir_close));
|
|
|
|
|
+ wire close_snap_accept =
|
|
|
|
|
+ calibrated_r && !in_calibration &&
|
|
|
|
|
+ close_assert_event && !departing_close_source;
|
|
|
|
|
+ wire open_snap_accept =
|
|
|
|
|
+ calibrated_r && !in_calibration &&
|
|
|
|
|
+ open_assert_event && !departing_open_source;
|
|
|
|
|
+ // Pre-NBA view: single-limit coordinate disagrees with accepted snap event.
|
|
|
|
|
+ wire limit_pos_mismatch =
|
|
|
|
|
+ (close_snap_accept && (current_position != travel_steps)) ||
|
|
|
|
|
+ (open_snap_accept && (current_position != {CNT_W{1'b0}}));
|
|
|
|
|
+
|
|
|
|
|
+ // Combinational endpoint-effective coordinate for same-cycle planner decisions.
|
|
|
|
|
+ // Use held source limit (not only one-shot snap): departure may consume arm
|
|
|
|
|
+ // without snapping, so a later mid accept while close_limit stays high must
|
|
|
|
|
+ // still treat the axis as at travel (never MOVE_CLOSE into the close limit).
|
|
|
|
|
+ wire close_source_hold =
|
|
|
|
|
+ calibrated_r && !in_calibration && close_limit && !departing_close_source;
|
|
|
|
|
+ wire open_source_hold =
|
|
|
|
|
+ calibrated_r && !in_calibration && open_limit && !departing_open_source;
|
|
|
|
|
+ wire [CNT_W-1:0] effective_current_position =
|
|
|
|
|
+ close_source_hold ? travel_steps :
|
|
|
|
|
+ open_source_hold ? {CNT_W{1'b0}} :
|
|
|
|
|
+ current_position;
|
|
|
|
|
+
|
|
|
|
|
+ assign dda_travel_steps = travel_steps;
|
|
|
|
|
+ assign dda_cal_active = in_calibration;
|
|
|
|
|
+ assign dda_run_active =
|
|
|
|
|
+ calibrated_r && !in_calibration && moving && (phase == PH_RUN) &&
|
|
|
|
|
+ ((status_state == ST_MOVE_OPEN) || (status_state == ST_MOVE_CLOSE));
|
|
|
|
|
+ assign dda_allow_toggle =
|
|
|
|
|
+ allow_rise && !stop_after_budget &&
|
|
|
|
|
+ (phase == PH_RUN) &&
|
|
|
|
|
+ ((status_state == ST_CAL_SEEK_CLOSE) ||
|
|
|
|
|
+ (status_state == ST_CAL_MEASURE_OPEN) ||
|
|
|
|
|
+ (status_state == ST_MOVE_OPEN) ||
|
|
|
|
|
+ (status_state == ST_MOVE_CLOSE));
|
|
|
|
|
+ assign dda_endpoint_crawl =
|
|
|
|
|
+ endpoint_mode && dda_run_active &&
|
|
|
|
|
+ ((dir_close && !close_limit) || (!dir_close && !open_limit));
|
|
|
|
|
+ assign dda_distance = in_calibration ? MAX_STEPS_W : move_distance;
|
|
|
|
|
+ assign dda_done_steps = in_calibration ? motion_count : curve_done_count;
|
|
|
|
|
+ assign dda_ramp_unit = in_calibration ? CAL_RAMP_UNIT_W : move_ramp_unit;
|
|
|
|
|
+
|
|
|
|
|
+ wire [CNT_W-1:0] plan_dist_abs =
|
|
|
|
|
+ planner_result >= effective_current_position ?
|
|
|
|
|
+ (planner_result - effective_current_position) :
|
|
|
|
|
+ (effective_current_position - planner_result);
|
|
|
|
|
+ wire [CNT_W-1:0] plan_ramp_raw = (plan_dist_abs + 63) >> 6;
|
|
|
|
|
+
|
|
|
|
|
+ // Distinguish request bookkeeping from material replan preempt.
|
|
|
|
|
+ // Idempotent same-percent heartbeat must not occupy the exclusive FSM branch.
|
|
|
|
|
+ wire request_seen = request_valid && calibrated_r;
|
|
|
|
|
+ wire idle_not_satisfied =
|
|
|
|
|
+ (status_state == ST_IDLE) && (
|
|
|
|
|
+ ((requested_percent_in == 8'd0) && !open_limit) ||
|
|
|
|
|
+ ((requested_percent_in == PERCENT_MAX[7:0]) && !close_limit) ||
|
|
|
|
|
+ ((requested_percent_in > 8'd0) &&
|
|
|
|
|
+ (requested_percent_in < PERCENT_MAX[7:0]) &&
|
|
|
|
|
+ (current_position != target_position))
|
|
|
|
|
+ );
|
|
|
|
|
+ wire request_requires_replan = request_seen && (
|
|
|
|
|
+ (requested_percent_in != requested_percent) ||
|
|
|
|
|
+ wd_stopped ||
|
|
|
|
|
+ // Limit snap forces resume only when this cycle cannot accept a matching
|
|
|
|
|
+ // planner result (which uses effective_current_position for DIR).
|
|
|
|
|
+ (limit_pos_mismatch && !planner_accept) ||
|
|
|
|
|
+ idle_not_satisfied
|
|
|
|
|
+ );
|
|
|
|
|
+
|
|
|
|
|
+ always @* begin
|
|
|
|
|
+ status_requested_percent = requested_percent;
|
|
|
|
|
+ status_current = current_position;
|
|
|
|
|
+ status_target = target_position;
|
|
|
|
|
+ status_travel = travel_steps;
|
|
|
|
|
+ status_fault_code = {5'd0, fault_code};
|
|
|
|
|
+ status_flags = {
|
|
|
|
|
+ 1'b0,
|
|
|
|
|
+ replan_pending,
|
|
|
|
|
+ target_valid,
|
|
|
|
|
+ endpoint_mode,
|
|
|
|
|
+ moving,
|
|
|
|
|
+ calibrated_r,
|
|
|
|
|
+ close_limit,
|
|
|
|
|
+ open_limit
|
|
|
|
|
+ };
|
|
|
|
|
+ end
|
|
|
|
|
+
|
|
|
|
|
+ // Shared STEP half-period helper: returns next step level
|
|
|
|
|
+ function automatic step_toggle;
|
|
|
|
|
+ input cur_step;
|
|
|
|
|
+ input do_allow_rise;
|
|
|
|
|
+ begin
|
|
|
|
|
+ if (cur_step)
|
|
|
|
|
+ step_toggle = 1'b0;
|
|
|
|
|
+ else if (do_allow_rise)
|
|
|
|
|
+ step_toggle = 1'b1;
|
|
|
|
|
+ else
|
|
|
|
|
+ step_toggle = 1'b0;
|
|
|
|
|
+ end
|
|
|
|
|
+ endfunction
|
|
|
|
|
+
|
|
|
|
|
+ always @(posedge clk or negedge rst_n) begin
|
|
|
|
|
+ if (!rst_n) begin
|
|
|
|
|
+ status_state <= ST_CAL_SEEK_CLOSE;
|
|
|
|
|
+ calibrated_r <= 1'b0;
|
|
|
|
|
+ fault_code <= FC_NONE;
|
|
|
|
|
+ travel_steps <= {CNT_W{1'b0}};
|
|
|
|
|
+ current_position <= {CNT_W{1'b0}};
|
|
|
|
|
+ target_position <= {CNT_W{1'b0}};
|
|
|
|
|
+ requested_percent <= 8'd0;
|
|
|
|
|
+ replan_pending <= 1'b0;
|
|
|
|
|
+ endpoint_mode <= 1'b0;
|
|
|
|
|
+ target_valid <= 1'b0;
|
|
|
|
|
+ moving <= 1'b0;
|
|
|
|
|
+ phase <= PH_NONE;
|
|
|
|
|
+ setup_count <= 4'd0;
|
|
|
|
|
+ motion_count <= {CNT_W{1'b0}};
|
|
|
|
|
+ allow_rise <= 1'b0;
|
|
|
|
|
+ stop_after_budget <= 1'b0;
|
|
|
|
|
+ cal_started <= 1'b0;
|
|
|
|
|
+ need_planner <= 1'b0;
|
|
|
|
|
+ planner_req <= 1'b0;
|
|
|
|
|
+ step <= 1'b0;
|
|
|
|
|
+ dir_close <= 1'b1;
|
|
|
|
|
+ wd_stopped <= 1'b0;
|
|
|
|
|
+ open_limit_armed <= 1'b1;
|
|
|
|
|
+ close_limit_armed <= 1'b1;
|
|
|
|
|
+ move_distance <= {CNT_W{1'b0}};
|
|
|
|
|
+ move_ramp_unit <= 1'b1;
|
|
|
|
|
+ curve_done_count <= {CNT_W{1'b0}};
|
|
|
|
|
+ end else if (both_limits) begin
|
|
|
|
|
+ status_state <= ST_FAULT;
|
|
|
|
|
+ fault_code <= FC_BOTH_LIMITS;
|
|
|
|
|
+ step <= 1'b0;
|
|
|
|
|
+ phase <= PH_NONE;
|
|
|
|
|
+ moving <= 1'b0;
|
|
|
|
|
+ allow_rise <= 1'b0;
|
|
|
|
|
+ stop_after_budget <= 1'b0;
|
|
|
|
|
+ planner_req <= 1'b0;
|
|
|
|
|
+ need_planner <= 1'b0;
|
|
|
|
|
+ replan_pending <= 1'b0;
|
|
|
|
|
+ if (request_valid)
|
|
|
|
|
+ requested_percent <= requested_percent_in;
|
|
|
|
|
+ end else if (status_state == ST_FAULT) begin
|
|
|
|
|
+ step <= 1'b0;
|
|
|
|
|
+ moving <= 1'b0;
|
|
|
|
|
+ planner_req <= 1'b0;
|
|
|
|
|
+ if (request_valid)
|
|
|
|
|
+ requested_percent <= requested_percent_in;
|
|
|
|
|
+ end else begin
|
|
|
|
|
+ // ----------------------------------------------------------
|
|
|
|
|
+ // 1) Position / cal / guard counts on gated rise (always)
|
|
|
|
|
+ // ----------------------------------------------------------
|
|
|
|
|
+ if (gated_step_rise) begin
|
|
|
|
|
+ if (status_state == ST_CAL_SEEK_CLOSE ||
|
|
|
|
|
+ status_state == ST_CAL_MEASURE_OPEN) begin
|
|
|
|
|
+ motion_count <= motion_count + 1'b1;
|
|
|
|
|
+ end else if (calibrated_r) begin
|
|
|
|
|
+ if (endpoint_mode)
|
|
|
|
|
+ motion_count <= motion_count + 1'b1;
|
|
|
|
|
+ if ((status_state == ST_MOVE_OPEN) || (status_state == ST_MOVE_CLOSE))
|
|
|
|
|
+ curve_done_count <= curve_done_count + 1'b1;
|
|
|
|
|
+ if (dir_close) begin
|
|
|
|
|
+ if (current_position < travel_steps)
|
|
|
|
|
+ current_position <= current_position + 1'b1;
|
|
|
|
|
+ end else begin
|
|
|
|
|
+ if (current_position != {CNT_W{1'b0}})
|
|
|
|
|
+ current_position <= current_position - 1'b1;
|
|
|
|
|
+ end
|
|
|
|
|
+ end
|
|
|
|
|
+ end
|
|
|
|
|
+
|
|
|
|
|
+ // ----------------------------------------------------------
|
|
|
|
|
+ // 1b) One-shot single-limit snap (consumes assertion interval).
|
|
|
|
|
+ // Departure from a consumed source limit must not overwrite §1.
|
|
|
|
|
+ // ----------------------------------------------------------
|
|
|
|
|
+ if (!open_limit)
|
|
|
|
|
+ open_limit_armed <= 1'b1;
|
|
|
|
|
+ if (!close_limit)
|
|
|
|
|
+ close_limit_armed <= 1'b1;
|
|
|
|
|
+ if (open_assert_event)
|
|
|
|
|
+ open_limit_armed <= 1'b0;
|
|
|
|
|
+ if (close_assert_event)
|
|
|
|
|
+ close_limit_armed <= 1'b0;
|
|
|
|
|
+
|
|
|
|
|
+ if (close_snap_accept)
|
|
|
|
|
+ current_position <= travel_steps;
|
|
|
|
|
+ else if (open_snap_accept)
|
|
|
|
|
+ current_position <= {CNT_W{1'b0}};
|
|
|
|
|
+
|
|
|
|
|
+ // ----------------------------------------------------------
|
|
|
|
|
+ // 2a) Non-destructive request bookkeeping (idempotent OK).
|
|
|
|
|
+ // Does not occupy the exclusive FSM branch — heartbeat may
|
|
|
|
|
+ // coexist with planner_accept / tick_500us / limit progression.
|
|
|
|
|
+ // ----------------------------------------------------------
|
|
|
|
|
+ if (request_seen) begin
|
|
|
|
|
+ requested_percent <= requested_percent_in;
|
|
|
|
|
+ wd_stopped <= 1'b0;
|
|
|
|
|
+ end
|
|
|
|
|
+
|
|
|
|
|
+ // ----------------------------------------------------------
|
|
|
|
|
+ // 2b) Material host replan only (latest-wins). Preempts tick.
|
|
|
|
|
+ // Same-percent heartbeat does NOT enter this branch.
|
|
|
|
|
+ // ----------------------------------------------------------
|
|
|
|
|
+ if (request_requires_replan) begin
|
|
|
|
|
+ replan_pending <= 1'b1;
|
|
|
|
|
+ planner_req <= 1'b0;
|
|
|
|
|
+ need_planner <= 1'b0;
|
|
|
|
|
+ allow_rise <= 1'b0;
|
|
|
|
|
+ // Materially new percent or forced replan: old target invalid
|
|
|
|
|
+ target_valid <= 1'b0;
|
|
|
|
|
+ if (gated_step || step) begin
|
|
|
|
|
+ phase <= PH_DRAIN_HIGH;
|
|
|
|
|
+ status_state <= ST_WAIT_REPLAN;
|
|
|
|
|
+ end else begin
|
|
|
|
|
+ step <= 1'b0;
|
|
|
|
|
+ phase <= PH_NONE;
|
|
|
|
|
+ moving <= 1'b0;
|
|
|
|
|
+ status_state <= ST_WAIT_REPLAN;
|
|
|
|
|
+ end
|
|
|
|
|
+
|
|
|
|
|
+ // ----------------------------------------------------------
|
|
|
|
|
+ // 3) Watchdog graceful stop (suppressed by same-cycle SET_ALL)
|
|
|
|
|
+ // Latch drain once; while PH_DRAIN_HIGH, fall through to §8 so
|
|
|
|
|
+ // tick_500us can pull STEP low even if watchdog_stop stays high.
|
|
|
|
|
+ // ----------------------------------------------------------
|
|
|
|
|
+ end else if (watchdog_stop && calibrated_r && !in_calibration &&
|
|
|
|
|
+ !wd_stopped && !request_seen &&
|
|
|
|
|
+ !(status_state == ST_WAIT_REPLAN && phase == PH_DRAIN_HIGH)) begin
|
|
|
|
|
+ allow_rise <= 1'b0;
|
|
|
|
|
+ planner_req <= 1'b0;
|
|
|
|
|
+ need_planner <= 1'b0;
|
|
|
|
|
+ if (gated_step || step) begin
|
|
|
|
|
+ phase <= PH_DRAIN_HIGH;
|
|
|
|
|
+ status_state <= ST_WAIT_REPLAN;
|
|
|
|
|
+ replan_pending <= 1'b0;
|
|
|
|
|
+ end else begin
|
|
|
|
|
+ step <= 1'b0;
|
|
|
|
|
+ phase <= PH_NONE;
|
|
|
|
|
+ moving <= 1'b0;
|
|
|
|
|
+ status_state <= ST_IDLE;
|
|
|
|
|
+ wd_stopped <= 1'b1;
|
|
|
|
|
+ replan_pending <= 1'b0;
|
|
|
|
|
+ end
|
|
|
|
|
+
|
|
|
|
|
+ // ----------------------------------------------------------
|
|
|
|
|
+ // 4) Planner result (uses effective_current_position for DIR)
|
|
|
|
|
+ // ----------------------------------------------------------
|
|
|
|
|
+ end else if (planner_accept) begin
|
|
|
|
|
+ planner_req <= 1'b0;
|
|
|
|
|
+ need_planner <= 1'b0;
|
|
|
|
|
+ replan_pending <= 1'b0;
|
|
|
|
|
+ target_position <= planner_result;
|
|
|
|
|
+ target_valid <= 1'b1;
|
|
|
|
|
+ endpoint_mode <= 1'b0;
|
|
|
|
|
+ motion_count <= {CNT_W{1'b0}};
|
|
|
|
|
+ curve_done_count <= {CNT_W{1'b0}};
|
|
|
|
|
+ move_distance <= plan_dist_abs;
|
|
|
|
|
+ move_ramp_unit <= (plan_ramp_raw == {CNT_W{1'b0}}) ? 1'b1 : plan_ramp_raw;
|
|
|
|
|
+ step <= 1'b0;
|
|
|
|
|
+ setup_count <= 4'd0;
|
|
|
|
|
+ allow_rise <= 1'b0;
|
|
|
|
|
+ stop_after_budget <= 1'b0;
|
|
|
|
|
+ if (planner_result == effective_current_position) begin
|
|
|
|
|
+ status_state <= ST_IDLE;
|
|
|
|
|
+ moving <= 1'b0;
|
|
|
|
|
+ phase <= PH_NONE;
|
|
|
|
|
+ end else if (planner_result > effective_current_position) begin
|
|
|
|
|
+ dir_close <= 1'b1;
|
|
|
|
|
+ phase <= PH_DIR_SETUP;
|
|
|
|
|
+ moving <= 1'b1;
|
|
|
|
|
+ status_state <= ST_MOVE_CLOSE;
|
|
|
|
|
+ end else begin
|
|
|
|
|
+ dir_close <= 1'b0;
|
|
|
|
|
+ phase <= PH_DIR_SETUP;
|
|
|
|
|
+ moving <= 1'b1;
|
|
|
|
|
+ status_state <= ST_MOVE_OPEN;
|
|
|
|
|
+ end
|
|
|
|
|
+
|
|
|
|
|
+ // ----------------------------------------------------------
|
|
|
|
|
+ // 5) Calibration start
|
|
|
|
|
+ // ----------------------------------------------------------
|
|
|
|
|
+ end else if (!cal_started) begin
|
|
|
|
|
+ if (cal_enabled && limit_sync_valid) begin
|
|
|
|
|
+ cal_started <= 1'b1;
|
|
|
|
|
+ motion_count <= {CNT_W{1'b0}};
|
|
|
|
|
+ setup_count <= 4'd0;
|
|
|
|
|
+ allow_rise <= 1'b0;
|
|
|
|
|
+ stop_after_budget <= 1'b0;
|
|
|
|
|
+ step <= 1'b0;
|
|
|
|
|
+ phase <= PH_DIR_SETUP;
|
|
|
|
|
+ moving <= 1'b1;
|
|
|
|
|
+ if (close_limit) begin
|
|
|
|
|
+ dir_close <= 1'b0;
|
|
|
|
|
+ status_state <= ST_CAL_MEASURE_OPEN;
|
|
|
|
|
+ end else begin
|
|
|
|
|
+ dir_close <= 1'b1;
|
|
|
|
|
+ status_state <= ST_CAL_SEEK_CLOSE;
|
|
|
|
|
+ end
|
|
|
|
|
+ end
|
|
|
|
|
+
|
|
|
|
|
+ // ----------------------------------------------------------
|
|
|
|
|
+ // 6) CAL_SEEK_CLOSE
|
|
|
|
|
+ // ----------------------------------------------------------
|
|
|
|
|
+ end else if (status_state == ST_CAL_SEEK_CLOSE) begin
|
|
|
|
|
+ if (close_limit) begin
|
|
|
|
|
+ step <= 1'b0;
|
|
|
|
|
+ allow_rise <= 1'b0;
|
|
|
|
|
+ stop_after_budget <= 1'b0;
|
|
|
|
|
+ motion_count <= {CNT_W{1'b0}};
|
|
|
|
|
+ setup_count <= 4'd0;
|
|
|
|
|
+ dir_close <= 1'b0;
|
|
|
|
|
+ phase <= PH_DIR_SETUP;
|
|
|
|
|
+ moving <= 1'b1;
|
|
|
|
|
+ status_state <= ST_CAL_MEASURE_OPEN;
|
|
|
|
|
+ end else if (stop_after_budget && !gated_step && !step) begin
|
|
|
|
|
+ status_state <= ST_FAULT;
|
|
|
|
|
+ fault_code <= FC_CAL_CLOSE_TIMEOUT;
|
|
|
|
|
+ moving <= 1'b0;
|
|
|
|
|
+ phase <= PH_NONE;
|
|
|
|
|
+ end else begin
|
|
|
|
|
+ // Use post-rise count: if count already at max, stop new rises
|
|
|
|
|
+ if ((motion_count >= MAX_STEPS_W) ||
|
|
|
|
|
+ (gated_step_rise && (motion_count + 1'b1 >= MAX_STEPS_W))) begin
|
|
|
|
|
+ allow_rise <= 1'b0;
|
|
|
|
|
+ stop_after_budget <= 1'b1;
|
|
|
|
|
+ end
|
|
|
|
|
+ if (phase == PH_DIR_SETUP) begin
|
|
|
|
|
+ step <= 1'b0;
|
|
|
|
|
+ if (tick_500us) begin
|
|
|
|
|
+ if (setup_count + 1'b1 >= DIR_SETUP_TICKS[3:0]) begin
|
|
|
|
|
+ phase <= PH_RUN;
|
|
|
|
|
+ setup_count <= 4'd0;
|
|
|
|
|
+ allow_rise <= !stop_after_budget && (motion_count < MAX_STEPS_W);
|
|
|
|
|
+ end else begin
|
|
|
|
|
+ setup_count <= setup_count + 1'b1;
|
|
|
|
|
+ end
|
|
|
|
|
+ end
|
|
|
|
|
+ end else if (exec_toggle_pulse) begin
|
|
|
|
|
+ step <= step_toggle(step, allow_rise && !stop_after_budget &&
|
|
|
|
|
+ (motion_count < MAX_STEPS_W));
|
|
|
|
|
+ end
|
|
|
|
|
+ end
|
|
|
|
|
+
|
|
|
|
|
+ // ----------------------------------------------------------
|
|
|
|
|
+ // 7) CAL_MEASURE_OPEN
|
|
|
|
|
+ // ----------------------------------------------------------
|
|
|
|
|
+ end else if (status_state == ST_CAL_MEASURE_OPEN) begin
|
|
|
|
|
+ if (open_limit) begin
|
|
|
|
|
+ // motion_count already includes this cycle's rise (NBA: see old+1)
|
|
|
|
|
+ // Use effective count: if rise this cycle, +1 on current
|
|
|
|
|
+ if (gated_step_rise) begin
|
|
|
|
|
+ // effective = motion_count+1 (scheduled); compare with that
|
|
|
|
|
+ if (((motion_count + 1'b1) >= MIN_TRAVEL_W) &&
|
|
|
|
|
+ ((motion_count + 1'b1) <= MAX_STEPS_W)) begin
|
|
|
|
|
+ travel_steps <= motion_count + 1'b1;
|
|
|
|
|
+ current_position <= {CNT_W{1'b0}};
|
|
|
|
|
+ target_position <= {CNT_W{1'b0}};
|
|
|
|
|
+ requested_percent <= 8'd0;
|
|
|
|
|
+ calibrated_r <= 1'b1;
|
|
|
|
|
+ fault_code <= FC_NONE;
|
|
|
|
|
+ target_valid <= 1'b1;
|
|
|
|
|
+ endpoint_mode <= 1'b1;
|
|
|
|
|
+ step <= 1'b0;
|
|
|
|
|
+ phase <= PH_NONE;
|
|
|
|
|
+ moving <= 1'b0;
|
|
|
|
|
+ allow_rise <= 1'b0;
|
|
|
|
|
+ stop_after_budget <= 1'b0;
|
|
|
|
|
+ status_state <= ST_IDLE;
|
|
|
|
|
+ end else begin
|
|
|
|
|
+ status_state <= ST_FAULT;
|
|
|
|
|
+ fault_code <= FC_TINY_TRAVEL;
|
|
|
|
|
+ step <= 1'b0;
|
|
|
|
|
+ moving <= 1'b0;
|
|
|
|
|
+ phase <= PH_NONE;
|
|
|
|
|
+ end
|
|
|
|
|
+ end else begin
|
|
|
|
|
+ if ((motion_count >= MIN_TRAVEL_W) &&
|
|
|
|
|
+ (motion_count <= MAX_STEPS_W)) begin
|
|
|
|
|
+ travel_steps <= motion_count;
|
|
|
|
|
+ current_position <= {CNT_W{1'b0}};
|
|
|
|
|
+ target_position <= {CNT_W{1'b0}};
|
|
|
|
|
+ requested_percent <= 8'd0;
|
|
|
|
|
+ calibrated_r <= 1'b1;
|
|
|
|
|
+ fault_code <= FC_NONE;
|
|
|
|
|
+ target_valid <= 1'b1;
|
|
|
|
|
+ endpoint_mode <= 1'b1;
|
|
|
|
|
+ step <= 1'b0;
|
|
|
|
|
+ phase <= PH_NONE;
|
|
|
|
|
+ moving <= 1'b0;
|
|
|
|
|
+ allow_rise <= 1'b0;
|
|
|
|
|
+ stop_after_budget <= 1'b0;
|
|
|
|
|
+ status_state <= ST_IDLE;
|
|
|
|
|
+ end else begin
|
|
|
|
|
+ status_state <= ST_FAULT;
|
|
|
|
|
+ fault_code <= FC_TINY_TRAVEL;
|
|
|
|
|
+ step <= 1'b0;
|
|
|
|
|
+ moving <= 1'b0;
|
|
|
|
|
+ phase <= PH_NONE;
|
|
|
|
|
+ end
|
|
|
|
|
+ end
|
|
|
|
|
+ end else if (stop_after_budget && !gated_step && !step) begin
|
|
|
|
|
+ status_state <= ST_FAULT;
|
|
|
|
|
+ fault_code <= FC_CAL_OPEN_TIMEOUT;
|
|
|
|
|
+ moving <= 1'b0;
|
|
|
|
|
+ phase <= PH_NONE;
|
|
|
|
|
+ end else begin
|
|
|
|
|
+ if ((motion_count >= MAX_STEPS_W) ||
|
|
|
|
|
+ (gated_step_rise && (motion_count + 1'b1 >= MAX_STEPS_W))) begin
|
|
|
|
|
+ allow_rise <= 1'b0;
|
|
|
|
|
+ stop_after_budget <= 1'b1;
|
|
|
|
|
+ end
|
|
|
|
|
+ if (phase == PH_DIR_SETUP) begin
|
|
|
|
|
+ step <= 1'b0;
|
|
|
|
|
+ if (tick_500us) begin
|
|
|
|
|
+ if (setup_count + 1'b1 >= DIR_SETUP_TICKS[3:0]) begin
|
|
|
|
|
+ phase <= PH_RUN;
|
|
|
|
|
+ allow_rise <= !stop_after_budget && (motion_count < MAX_STEPS_W);
|
|
|
|
|
+ end else begin
|
|
|
|
|
+ setup_count <= setup_count + 1'b1;
|
|
|
|
|
+ end
|
|
|
|
|
+ end
|
|
|
|
|
+ end else if (exec_toggle_pulse) begin
|
|
|
|
|
+ step <= step_toggle(step, allow_rise && !stop_after_budget &&
|
|
|
|
|
+ (motion_count < MAX_STEPS_W));
|
|
|
|
|
+ end
|
|
|
|
|
+ end
|
|
|
|
|
+
|
|
|
|
|
+ // ----------------------------------------------------------
|
|
|
|
|
+ // 8) WAIT_REPLAN / drain / decide
|
|
|
|
|
+ // ----------------------------------------------------------
|
|
|
|
|
+ end else if (status_state == ST_WAIT_REPLAN) begin
|
|
|
|
|
+ if (phase == PH_DRAIN_HIGH) begin
|
|
|
|
|
+ if (tick_500us)
|
|
|
|
|
+ step <= 1'b0;
|
|
|
|
|
+ if (!gated_step && (!step || tick_500us)) begin
|
|
|
|
|
+ step <= 1'b0;
|
|
|
|
|
+ phase <= PH_NONE;
|
|
|
|
|
+ moving <= 1'b0;
|
|
|
|
|
+ if (watchdog_stop) begin
|
|
|
|
|
+ status_state <= ST_IDLE;
|
|
|
|
|
+ wd_stopped <= 1'b1;
|
|
|
|
|
+ replan_pending <= 1'b0;
|
|
|
|
|
+ end else begin
|
|
|
|
|
+ replan_pending <= 1'b1;
|
|
|
|
|
+ end
|
|
|
|
|
+ end
|
|
|
|
|
+ end else if (replan_pending) begin
|
|
|
|
|
+ replan_pending <= 1'b0;
|
|
|
|
|
+ if (is_open_endpoint) begin
|
|
|
|
|
+ endpoint_mode <= 1'b1;
|
|
|
|
|
+ target_position <= {CNT_W{1'b0}};
|
|
|
|
|
+ target_valid <= 1'b1;
|
|
|
|
|
+ need_planner <= 1'b0;
|
|
|
|
|
+ planner_req <= 1'b0;
|
|
|
|
|
+ if (open_limit) begin
|
|
|
|
|
+ current_position <= {CNT_W{1'b0}};
|
|
|
|
|
+ status_state <= ST_IDLE;
|
|
|
|
|
+ moving <= 1'b0;
|
|
|
|
|
+ step <= 1'b0;
|
|
|
|
|
+ phase <= PH_NONE;
|
|
|
|
|
+ end else begin
|
|
|
|
|
+ motion_count <= {CNT_W{1'b0}};
|
|
|
|
|
+ dir_close <= 1'b0;
|
|
|
|
|
+ step <= 1'b0;
|
|
|
|
|
+ setup_count <= 4'd0;
|
|
|
|
|
+ allow_rise <= 1'b0;
|
|
|
|
|
+ stop_after_budget <= 1'b0;
|
|
|
|
|
+ phase <= PH_DIR_SETUP;
|
|
|
|
|
+ moving <= 1'b1;
|
|
|
|
|
+ status_state <= ST_MOVE_OPEN;
|
|
|
|
|
+ end
|
|
|
|
|
+ end else if (is_close_endpoint) begin
|
|
|
|
|
+ endpoint_mode <= 1'b1;
|
|
|
|
|
+ target_position <= travel_steps;
|
|
|
|
|
+ target_valid <= 1'b1;
|
|
|
|
|
+ need_planner <= 1'b0;
|
|
|
|
|
+ planner_req <= 1'b0;
|
|
|
|
|
+ if (close_limit) begin
|
|
|
|
|
+ current_position <= travel_steps;
|
|
|
|
|
+ status_state <= ST_IDLE;
|
|
|
|
|
+ moving <= 1'b0;
|
|
|
|
|
+ step <= 1'b0;
|
|
|
|
|
+ phase <= PH_NONE;
|
|
|
|
|
+ end else begin
|
|
|
|
|
+ motion_count <= {CNT_W{1'b0}};
|
|
|
|
|
+ dir_close <= 1'b1;
|
|
|
|
|
+ step <= 1'b0;
|
|
|
|
|
+ setup_count <= 4'd0;
|
|
|
|
|
+ allow_rise <= 1'b0;
|
|
|
|
|
+ stop_after_budget <= 1'b0;
|
|
|
|
|
+ phase <= PH_DIR_SETUP;
|
|
|
|
|
+ moving <= 1'b1;
|
|
|
|
|
+ status_state <= ST_MOVE_CLOSE;
|
|
|
|
|
+ end
|
|
|
|
|
+ end else begin
|
|
|
|
|
+ endpoint_mode <= 1'b0;
|
|
|
|
|
+ need_planner <= 1'b1;
|
|
|
|
|
+ planner_req <= 1'b1;
|
|
|
|
|
+ moving <= 1'b0;
|
|
|
|
|
+ step <= 1'b0;
|
|
|
|
|
+ phase <= PH_NONE;
|
|
|
|
|
+ end
|
|
|
|
|
+ end
|
|
|
|
|
+
|
|
|
|
|
+ // ----------------------------------------------------------
|
|
|
|
|
+ // 9) MOVE_OPEN / MOVE_CLOSE
|
|
|
|
|
+ // ----------------------------------------------------------
|
|
|
|
|
+ end else if ((status_state == ST_MOVE_OPEN) ||
|
|
|
|
|
+ (status_state == ST_MOVE_CLOSE)) begin
|
|
|
|
|
+ if (endpoint_mode) begin
|
|
|
|
|
+ if ((dir_close && close_limit) || (!dir_close && open_limit)) begin
|
|
|
|
|
+ step <= 1'b0;
|
|
|
|
|
+ allow_rise <= 1'b0;
|
|
|
|
|
+ stop_after_budget <= 1'b0;
|
|
|
|
|
+ phase <= PH_NONE;
|
|
|
|
|
+ moving <= 1'b0;
|
|
|
|
|
+ if (dir_close) begin
|
|
|
|
|
+ current_position <= travel_steps;
|
|
|
|
|
+ target_position <= travel_steps;
|
|
|
|
|
+ end else begin
|
|
|
|
|
+ current_position <= {CNT_W{1'b0}};
|
|
|
|
|
+ target_position <= {CNT_W{1'b0}};
|
|
|
|
|
+ end
|
|
|
|
|
+ status_state <= ST_IDLE;
|
|
|
|
|
+ end else if (stop_after_budget && !gated_step && !step) begin
|
|
|
|
|
+ status_state <= ST_FAULT;
|
|
|
|
|
+ fault_code <= FC_ENDPOINT_TIMEOUT;
|
|
|
|
|
+ moving <= 1'b0;
|
|
|
|
|
+ phase <= PH_NONE;
|
|
|
|
|
+ step <= 1'b0;
|
|
|
|
|
+ end else begin
|
|
|
|
|
+ if ((motion_count >= guard_limit) ||
|
|
|
|
|
+ (gated_step_rise && (motion_count + 1'b1 >= guard_limit))) begin
|
|
|
|
|
+ allow_rise <= 1'b0;
|
|
|
|
|
+ stop_after_budget <= 1'b1;
|
|
|
|
|
+ end
|
|
|
|
|
+ if (phase == PH_DIR_SETUP) begin
|
|
|
|
|
+ step <= 1'b0;
|
|
|
|
|
+ if (tick_500us) begin
|
|
|
|
|
+ if (setup_count + 1'b1 >= DIR_SETUP_TICKS[3:0]) begin
|
|
|
|
|
+ phase <= PH_RUN;
|
|
|
|
|
+ allow_rise <= 1'b1;
|
|
|
|
|
+ end else begin
|
|
|
|
|
+ setup_count <= setup_count + 1'b1;
|
|
|
|
|
+ end
|
|
|
|
|
+ end
|
|
|
|
|
+ end else if (phase == PH_DRAIN_HIGH) begin
|
|
|
|
|
+ if (tick_500us)
|
|
|
|
|
+ step <= 1'b0;
|
|
|
|
|
+ end else if (exec_toggle_pulse) begin
|
|
|
|
|
+ step <= step_toggle(step, allow_rise && !stop_after_budget &&
|
|
|
|
|
+ (motion_count < guard_limit));
|
|
|
|
|
+ end
|
|
|
|
|
+ end
|
|
|
|
|
+ end else begin
|
|
|
|
|
+ // Mid-percent motion
|
|
|
|
|
+ if ((dir_close && close_limit) || (!dir_close && open_limit)) begin
|
|
|
|
|
+ step <= 1'b0;
|
|
|
|
|
+ allow_rise <= 1'b0;
|
|
|
|
|
+ if (dir_close)
|
|
|
|
|
+ current_position <= travel_steps;
|
|
|
|
|
+ else
|
|
|
|
|
+ current_position <= {CNT_W{1'b0}};
|
|
|
|
|
+ replan_pending <= 1'b1;
|
|
|
|
|
+ need_planner <= 1'b0;
|
|
|
|
|
+ planner_req <= 1'b0;
|
|
|
|
|
+ phase <= PH_NONE;
|
|
|
|
|
+ moving <= 1'b0;
|
|
|
|
|
+ status_state <= ST_WAIT_REPLAN;
|
|
|
|
|
+ end else begin
|
|
|
|
|
+ // Effective current after this cycle's rise
|
|
|
|
|
+ // Completion: compare using updated position intent
|
|
|
|
|
+ if ((current_position == target_position) ||
|
|
|
|
|
+ (gated_step_rise && dir_close &&
|
|
|
|
|
+ (current_position + 1'b1 == target_position)) ||
|
|
|
|
|
+ (gated_step_rise && !dir_close &&
|
|
|
|
|
+ (current_position - 1'b1 == target_position))) begin
|
|
|
|
|
+ allow_rise <= 1'b0;
|
|
|
|
|
+ if (step || gated_step) begin
|
|
|
|
|
+ phase <= PH_DRAIN_HIGH;
|
|
|
|
|
+ if (tick_500us)
|
|
|
|
|
+ step <= 1'b0;
|
|
|
|
|
+ end else begin
|
|
|
|
|
+ step <= 1'b0;
|
|
|
|
|
+ phase <= PH_NONE;
|
|
|
|
|
+ moving <= 1'b0;
|
|
|
|
|
+ status_state <= ST_IDLE;
|
|
|
|
|
+ end
|
|
|
|
|
+ end else if (phase == PH_DIR_SETUP) begin
|
|
|
|
|
+ step <= 1'b0;
|
|
|
|
|
+ if (tick_500us) begin
|
|
|
|
|
+ if (setup_count + 1'b1 >= DIR_SETUP_TICKS[3:0]) begin
|
|
|
|
|
+ phase <= PH_RUN;
|
|
|
|
|
+ allow_rise <= 1'b1;
|
|
|
|
|
+ end else begin
|
|
|
|
|
+ setup_count <= setup_count + 1'b1;
|
|
|
|
|
+ end
|
|
|
|
|
+ end
|
|
|
|
|
+ end else if (phase == PH_DRAIN_HIGH) begin
|
|
|
|
|
+ if (tick_500us)
|
|
|
|
|
+ step <= 1'b0;
|
|
|
|
|
+ if (!step && !gated_step) begin
|
|
|
|
|
+ moving <= 1'b0;
|
|
|
|
|
+ status_state <= ST_IDLE;
|
|
|
|
|
+ phase <= PH_NONE;
|
|
|
|
|
+ end else if (tick_500us && step) begin
|
|
|
|
|
+ // falling this tick; next cycle check idle
|
|
|
|
|
+ end
|
|
|
|
|
+ end else if (exec_toggle_pulse) begin
|
|
|
|
|
+ step <= step_toggle(step, allow_rise);
|
|
|
|
|
+ end
|
|
|
|
|
+ end
|
|
|
|
|
+ end
|
|
|
|
|
+
|
|
|
|
|
+ // ----------------------------------------------------------
|
|
|
|
|
+ // 10) IDLE
|
|
|
|
|
+ // ----------------------------------------------------------
|
|
|
|
|
+ end else if (status_state == ST_IDLE) begin
|
|
|
|
|
+ step <= 1'b0;
|
|
|
|
|
+ moving <= 1'b0;
|
|
|
|
|
+ if (open_snap_accept)
|
|
|
|
|
+ current_position <= {CNT_W{1'b0}};
|
|
|
|
|
+ if (close_snap_accept)
|
|
|
|
|
+ current_position <= travel_steps;
|
|
|
|
|
+ if (replan_pending)
|
|
|
|
|
+ status_state <= ST_WAIT_REPLAN;
|
|
|
|
|
+ end
|
|
|
|
|
+ end
|
|
|
|
|
+ end
|
|
|
|
|
+endmodule
|