"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.modesOfOperationOptions = exports.ModesOfOperation = exports.cia402Transitions = exports.createControlwordCommand = exports.controlwordCommandBitmasks = exports.controlwordCommandOptions = exports.controlwordOptions = exports.getCommandFromControlword = exports.ControlwordCommand = exports.isInternalLimitActive = exports.getCia402State = exports.cia402StateMap = exports.Cia402State = void 0;
/**
 * CIA402 refers to the CANopen application profile for drives and motion control.
 * It defines various states that a CiA402-compliant device, such as a motor drive, can be in.
 *
 * @enum
 */
var Cia402State;
(function (Cia402State) {
    /**
     * Not ready to switch on (NRS).
     *
     * This is the initial state where the drive is not ready to be switched on.
     * In this state, the drive is not powered or initialized.
     */
    Cia402State["NOT_READY_TO_SWITCH_ON"] = "NOT_READY_TO_SWITCH_ON";
    /**
     * Switch on disabled (SOD).
     *
     * In this state, the drive is powered and initialized but cannot be switched on.
     * This state is usually used for safety purposes or when certain conditions need to be met before enabling the drive.
     */
    Cia402State["SWITCH_ON_DISABLED"] = "SWITCH_ON_DISABLED";
    /**
     * Ready to switch on (RTS).
     *
     * In this state, the drive is ready to be switched on.
     * It has been powered and initialized, and it is waiting for the switch-on command.
     */
    Cia402State["READY_TO_SWITCH_ON"] = "READY_TO_SWITCH_ON";
    /**
     * Switched on (SWO).
     *
     * When the switch-on command is received, the drive enters this state.
     * In this state, the drive is energized and ready to start motion.
     */
    Cia402State["SWITCHED_ON"] = "SWITCHED_ON";
    /**
     * Operation enabled (OP).
     *
     * This state indicates that the drive is enabled and ready to perform its intended operation.
     * It can receive motion commands and execute them.
     */
    Cia402State["OPERATION_ENABLED"] = "OPERATION_ENABLED";
    /**
     * Quick stop active (QSA).
     *
     * This state is used to bring the drive to an immediate stop.
     * It is typically triggered by an emergency stop or a quick stop command.
     */
    Cia402State["QUICK_STOP_ACTIVE"] = "QUICK_STOP_ACTIVE";
    /**
     * Fault reaction active (FRA).
     *
     * When a fault or error condition occurs, the drive enters this state.
     * It indicates that the drive is responding to the fault by taking appropriate actions,
     * such as shutting down or entering a safe state.
     */
    Cia402State["FAULT_REACTION_ACTIVE"] = "FAULT_REACTION_ACTIVE";
    /**
     * Fault (FLT).
     *
     * This state signifies that a fault condition has been detected and the drive is no longer operational.
     * It requires manual intervention or troubleshooting to resolve the fault
     * and bring the drive back to a functional state.
     */
    Cia402State["FAULT"] = "FAULT";
})(Cia402State = exports.Cia402State || (exports.Cia402State = {}));
exports.cia402StateMap = new Map([
    [0b00000000, Cia402State.NOT_READY_TO_SWITCH_ON],
    [0b01000000, Cia402State.SWITCH_ON_DISABLED],
    [0b00100001, Cia402State.READY_TO_SWITCH_ON],
    [0b00100011, Cia402State.SWITCHED_ON],
    [0b00100111, Cia402State.OPERATION_ENABLED],
    [0b00000111, Cia402State.QUICK_STOP_ACTIVE],
    [0b00001111, Cia402State.FAULT_REACTION_ACTIVE],
    [0b00001000, Cia402State.FAULT],
]);
function getCia402State(value) {
    var _a, _b;
    return ((_b = (_a = exports.cia402StateMap.get(value & 0b01101111)) !== null && _a !== void 0 ? _a : exports.cia402StateMap.get(value & 0b01001111)) !== null && _b !== void 0 ? _b : Cia402State.NOT_READY_TO_SWITCH_ON);
}
exports.getCia402State = getCia402State;
function isInternalLimitActive(statusword) {
    return (0x800 & statusword) === 0x800;
}
exports.isInternalLimitActive = isInternalLimitActive;
/**
 * In the CiA402 standard for drives and motion control, the controlword is a key component
 * used to command and control the operation of a CiA402-compliant device, such as a motor drive.
 * The controlword is typically a 16-bit value,
 * and it consists of several bits that represent different control commands.
 *
 * @enum
 */
var ControlwordCommand;
(function (ControlwordCommand) {
    ControlwordCommand["SHUTDOWN"] = "SHUTDOWN";
    ControlwordCommand["SWITCH_ON"] = "SWITCH_ON";
    ControlwordCommand["SWITCH_ON_ENABLE_OPERATION"] = "SWITCH_ON_ENABLE_OPERATION";
    ControlwordCommand["DISABLE_VOLTAGE"] = "DISABLE_VOLTAGE";
    /**
     * Quick Stop.
     *
     * This command initiates a quick stop of the drive.
     * When this bit is set to 1, the drive enters the "Quick stop active" state,
     * causing the motor to decelerate and come to a stop as quickly as possible.
     */
    ControlwordCommand["QUICK_STOP"] = "QUICK_STOP";
    ControlwordCommand["DISABLE_OPERATION"] = "DISABLE_OPERATION";
    /**
     * Enable Operation.
     *
     * This command enables the drive to perform its intended operation.
     * When this bit is set to 1, the drive transitions from the "Ready to switch on" state to the "Operation enabled" state.
     */
    ControlwordCommand["ENABLE_OPERATION"] = "ENABLE_OPERATION";
    /**
     * Fault Reset.
     *
     * This command resets a fault condition and clears any fault flags or errors that may have occurred.
     * When this bit is set to 1, the drive attempts to recover from a fault and return to an operational state.
     */
    ControlwordCommand["FAULT_RESET"] = "FAULT_RESET";
})(ControlwordCommand = exports.ControlwordCommand || (exports.ControlwordCommand = {}));
/**
 * Return one of the controlword commands based its value.
 *
 * NOTE: DISABLE_OPERATION and ENABLE_OPERATION are never returned.
 * Their values are the same as SWITCH_ON and SWITCH_ON_ENABLE_OPERATION.
 * What controlword is commanded depends on the transition.
 */
function getCommandFromControlword(value) {
    if ((value & 0b1000111) === 0b0000110) {
        return ControlwordCommand.SHUTDOWN;
    }
    if ((value & 0b1001111) === 0b0000111) {
        return ControlwordCommand.SWITCH_ON;
    }
    if ((value & 0b1001111) === 0b0001111) {
        return ControlwordCommand.SWITCH_ON_ENABLE_OPERATION;
    }
    if ((value & 0b1000010) === 0b0000000) {
        return ControlwordCommand.DISABLE_VOLTAGE;
    }
    if ((value & 0b1000110) === 0b0000010) {
        return ControlwordCommand.QUICK_STOP;
    }
    if ((value & 0b1001111) === 0b0000111) {
        return ControlwordCommand.DISABLE_OPERATION;
    }
    if ((value & 0b1001111) === 0b0001111) {
        return ControlwordCommand.ENABLE_OPERATION;
    }
    if ((value & 0b1000000) === 0b1000000) {
        return ControlwordCommand.FAULT_RESET;
    }
    return;
}
exports.getCommandFromControlword = getCommandFromControlword;
exports.controlwordOptions = {
    Shutdown: 0b00000110,
    'Switch on': 0b00000111,
    'Switch on + enable operation': 0b00001111,
    'Disable voltage': 0b00000000,
    'Quick stop': 0b00000010,
    'Disable operation': 0b00000111,
    'Enable operation': 0b00001111,
    'Fault reset': 0b10000000, // fault reset 15
};
exports.controlwordCommandOptions = Object.values(ControlwordCommand);
exports.controlwordCommandBitmasks = {
    [ControlwordCommand.SHUTDOWN]: [0b1111111101111110, 0b0000000000000110],
    [ControlwordCommand.SWITCH_ON]: [0b1111111101110111, 0b0000000000000111],
    [ControlwordCommand.SWITCH_ON_ENABLE_OPERATION]: [0b1111111101111111, 0b0000000000001111],
    [ControlwordCommand.DISABLE_VOLTAGE]: [0b1111111101111101, 0b0000000000000000],
    [ControlwordCommand.QUICK_STOP]: [0b1111111101111011, 0b0000000000000010],
    [ControlwordCommand.DISABLE_OPERATION]: [0b1111111101110111, 0b0000000000000111],
    [ControlwordCommand.ENABLE_OPERATION]: [0b1111111101111111, 0b0000000000001111],
    [ControlwordCommand.FAULT_RESET]: [0b1111111111111111, 0b0000000010000000],
};
function createControlwordCommand(value, command) {
    const bitmasks = exports.controlwordCommandBitmasks[command];
    return (value & bitmasks[0]) | bitmasks[1];
}
exports.createControlwordCommand = createControlwordCommand;
/**
 * CiA402 transitions.
 *
 * List of allowed transitions with controlword command.
 *
 * The following transitions are automatic:
 * - 0: START -> NOT_READY_TO_SWITCH_ON
 * - 1: NOT_READY_TO_SWITCH_ON -> SWITCH_ON_DISABLED
 * - 13: ANY -> FAULT_REACTION_ACTIVE
 * - 14: FAULT_REACTION_ACTIVE -> FAULT
 */
exports.cia402Transitions = [
    {
        from: Cia402State.SWITCH_ON_DISABLED,
        to: Cia402State.READY_TO_SWITCH_ON,
        transitions: [2],
        command: ControlwordCommand.SHUTDOWN,
    },
    {
        from: Cia402State.READY_TO_SWITCH_ON,
        to: Cia402State.SWITCHED_ON,
        transitions: [3],
        command: ControlwordCommand.SWITCH_ON,
    },
    {
        from: Cia402State.READY_TO_SWITCH_ON,
        to: Cia402State.OPERATION_ENABLED,
        transitions: [3, 4],
        command: ControlwordCommand.SWITCH_ON_ENABLE_OPERATION,
    },
    {
        from: Cia402State.SWITCHED_ON,
        to: Cia402State.OPERATION_ENABLED,
        transitions: [4],
        command: ControlwordCommand.ENABLE_OPERATION,
    },
    {
        from: Cia402State.OPERATION_ENABLED,
        to: Cia402State.SWITCHED_ON,
        transitions: [5],
        command: ControlwordCommand.DISABLE_OPERATION,
    },
    {
        from: Cia402State.SWITCHED_ON,
        to: Cia402State.READY_TO_SWITCH_ON,
        transitions: [6],
        command: ControlwordCommand.SHUTDOWN,
    },
    {
        from: Cia402State.READY_TO_SWITCH_ON,
        to: Cia402State.SWITCH_ON_DISABLED,
        transitions: [7],
        command: ControlwordCommand.DISABLE_VOLTAGE,
    },
    {
        from: Cia402State.OPERATION_ENABLED,
        to: Cia402State.READY_TO_SWITCH_ON,
        transitions: [8],
        command: ControlwordCommand.SHUTDOWN,
    },
    {
        from: Cia402State.OPERATION_ENABLED,
        to: Cia402State.SWITCH_ON_DISABLED,
        transitions: [9],
        command: ControlwordCommand.DISABLE_VOLTAGE,
    },
    {
        from: Cia402State.SWITCHED_ON,
        to: Cia402State.SWITCH_ON_DISABLED,
        transitions: [10],
        command: ControlwordCommand.DISABLE_VOLTAGE,
    },
    {
        from: Cia402State.OPERATION_ENABLED,
        to: Cia402State.QUICK_STOP_ACTIVE,
        transitions: [11],
        command: ControlwordCommand.QUICK_STOP,
    },
    {
        from: Cia402State.QUICK_STOP_ACTIVE,
        to: Cia402State.SWITCH_ON_DISABLED,
        transitions: [12],
        command: ControlwordCommand.DISABLE_VOLTAGE,
    },
    {
        from: Cia402State.FAULT,
        to: Cia402State.SWITCH_ON_DISABLED,
        transitions: [15],
        command: ControlwordCommand.FAULT_RESET,
    },
    {
        from: Cia402State.QUICK_STOP_ACTIVE,
        to: Cia402State.OPERATION_ENABLED,
        transitions: [16],
        command: ControlwordCommand.ENABLE_OPERATION,
    },
];
var ModesOfOperation;
(function (ModesOfOperation) {
    ModesOfOperation[ModesOfOperation["CYCLIC_SYNC_OUTPUT_TORQUE_MODE"] = -110] = "CYCLIC_SYNC_OUTPUT_TORQUE_MODE";
    ModesOfOperation[ModesOfOperation["CYCLIC_SYNC_OUTPUT_VELOCITY_MODE"] = -109] = "CYCLIC_SYNC_OUTPUT_VELOCITY_MODE";
    ModesOfOperation[ModesOfOperation["CYCLIC_SYNC_OUTPUT_POSITION_MODE"] = -108] = "CYCLIC_SYNC_OUTPUT_POSITION_MODE";
    ModesOfOperation[ModesOfOperation["PROFILE_OUTPUT_VELOCITY_MODE"] = -103] = "PROFILE_OUTPUT_VELOCITY_MODE";
    ModesOfOperation[ModesOfOperation["PROFILE_OUTPUT_POSITION_MODE"] = -101] = "PROFILE_OUTPUT_POSITION_MODE";
    ModesOfOperation[ModesOfOperation["SYSTEM_IDENTIFICATION_MODE"] = -4] = "SYSTEM_IDENTIFICATION_MODE";
    ModesOfOperation[ModesOfOperation["OPEN_LOOP_FIELD_MODE"] = -3] = "OPEN_LOOP_FIELD_MODE";
    ModesOfOperation[ModesOfOperation["DIAGNOSTICS_MODE"] = -2] = "DIAGNOSTICS_MODE";
    ModesOfOperation[ModesOfOperation["COGGING_COMPENSATION_RECORDING_MODE"] = -1] = "COGGING_COMPENSATION_RECORDING_MODE";
    ModesOfOperation[ModesOfOperation["NO_MODE_ASSIGNED"] = 0] = "NO_MODE_ASSIGNED";
    ModesOfOperation[ModesOfOperation["PROFILE_POSITION_MODE"] = 1] = "PROFILE_POSITION_MODE";
    ModesOfOperation[ModesOfOperation["PROFILE_VELOCITY_MODE"] = 3] = "PROFILE_VELOCITY_MODE";
    ModesOfOperation[ModesOfOperation["TORQUE_PROFILE_MODE"] = 4] = "TORQUE_PROFILE_MODE";
    ModesOfOperation[ModesOfOperation["HOMING_MODE"] = 6] = "HOMING_MODE";
    ModesOfOperation[ModesOfOperation["CYCLIC_SYNC_POSITION_MODE"] = 8] = "CYCLIC_SYNC_POSITION_MODE";
    ModesOfOperation[ModesOfOperation["CYCLIC_SYNC_VELOCITY_MODE"] = 9] = "CYCLIC_SYNC_VELOCITY_MODE";
    ModesOfOperation[ModesOfOperation["CYCLIC_SYNC_TORQUE_MODE"] = 10] = "CYCLIC_SYNC_TORQUE_MODE";
})(ModesOfOperation = exports.ModesOfOperation || (exports.ModesOfOperation = {}));
exports.modesOfOperationOptions = {
    'Cyclic synchronous output torque mode': ModesOfOperation.CYCLIC_SYNC_OUTPUT_TORQUE_MODE,
    'Cyclic synchronous output velocity mode': ModesOfOperation.CYCLIC_SYNC_OUTPUT_VELOCITY_MODE,
    'Cyclic synchronous output position mode': ModesOfOperation.CYCLIC_SYNC_OUTPUT_POSITION_MODE,
    'Profile output velocity mode': ModesOfOperation.PROFILE_OUTPUT_VELOCITY_MODE,
    'Profile output position mode': ModesOfOperation.PROFILE_OUTPUT_POSITION_MODE,
    'System Identification Mode': ModesOfOperation.SYSTEM_IDENTIFICATION_MODE,
    'Open loop field mode': ModesOfOperation.OPEN_LOOP_FIELD_MODE,
    'Diagnostics mode': ModesOfOperation.DIAGNOSTICS_MODE,
    'Cogging compensation recording mode': ModesOfOperation.COGGING_COMPENSATION_RECORDING_MODE,
    'No mode assigned': ModesOfOperation.NO_MODE_ASSIGNED,
    'Profile position mode': ModesOfOperation.PROFILE_POSITION_MODE,
    'Profile velocity mode': ModesOfOperation.PROFILE_VELOCITY_MODE,
    'Torque profile mode': ModesOfOperation.TORQUE_PROFILE_MODE,
    'Homing mode': ModesOfOperation.HOMING_MODE,
    'Cyclic synchronous position mode': ModesOfOperation.CYCLIC_SYNC_POSITION_MODE,
    'Cyclic synchronous velocity mode': ModesOfOperation.CYCLIC_SYNC_VELOCITY_MODE,
    'Cyclic synchronous torque mode': ModesOfOperation.CYCLIC_SYNC_TORQUE_MODE,
};
//# sourceMappingURL=cia402.js.map