#pragma once
#include "ecrt.h"
#include "ethercat_control/motor_control.hpp" // 电机控制库 

#define ZER_Device0        0,0    /*EtherCAT address on the bus 别名，位置*/
  
#define VID_PID          0x00202288,0x00000384   /*Vendor ID, product code	厂商ID和产品代码*/

typedef struct {
    unsigned int ctrl_word;                   // 0x6040:00  1
    unsigned int target_position;             // 0x607A:00  1
    unsigned int target_velocity;             // 0x60FF:00  1
    unsigned int operation_mode;              // 0x6060:00  1
    unsigned int status_word;                 // 0x6041:00  1
    unsigned int position_actual_value;       // 0x6064:00  1
    unsigned int velocity_actual_value;       // 0x606C:00  1
    unsigned int error_code;                  // 0x603F:00  1
    unsigned int modes_of_operation_display;  // 0x6061:00  1
} zer_offset_t;

static zer_offset_t zer_offsets[NUM_SLAVES];

// ------------------- PDO 定义（CSV/CSP/CST），对应 0x1600/0x1A00 -------------------
// 下列结构由IGH主站 cstruct 自动生成
const static ec_pdo_entry_reg_t zer_domain1_regs[] = {
    {ZER_Device0, VID_PID, 0x6040, 0, &zer_offsets[0].ctrl_word},                   // 控制字
    {ZER_Device0, VID_PID, 0x607A, 0, &zer_offsets[0].target_position},             // 目标位置
    {ZER_Device0, VID_PID, 0x60FF, 0, &zer_offsets[0].target_velocity},             // 目标速度
    {ZER_Device0, VID_PID, 0x6060, 0, &zer_offsets[0].operation_mode},              // 运行模式
    {ZER_Device0, VID_PID, 0x6041, 0, &zer_offsets[0].status_word},                 // 状态字
    {ZER_Device0, VID_PID, 0x6064, 0, &zer_offsets[0].position_actual_value},       // 实际位置
    {ZER_Device0, VID_PID, 0x606C, 0, &zer_offsets[0].velocity_actual_value},       // 实际速度
    {ZER_Device0, VID_PID, 0x603F, 0, &zer_offsets[0].error_code},                  // 错误代码
    {ZER_Device0, VID_PID, 0x6061, 0, &zer_offsets[0].modes_of_operation_display},  // 模式显示
    {} // 结束标记
};

static ec_pdo_entry_info_t zer_device_pdo_entries[] = {
    /*RxPdo 0x1600*/
    {0x6040, 0x00, 16}, /* control word */
    {0x607a, 0x00, 32}, /* target position */
    {0x60ff, 0x00, 32}, /* target velocity */
    {0x6060, 0x00, 8}, /* modes of operation */
    /*TxPdo 0x1A00*/
    {0x6041, 0x00, 16}, /* status word */
    {0x6064, 0x00, 32}, /* position actual value */
    {0x606c, 0x00, 32}, /* velocity actual value */
    {0x603f, 0x00, 16}, /* error code */
    {0x6061, 0x00, 8}, /* modes of operation display */
};

static ec_pdo_info_t zer_device_pdos[] = {
    //RxPdo
    {0x1600, 4, zer_device_pdo_entries + 0 },
    //TxPdo
    {0x1A00, 5, zer_device_pdo_entries + 4 }
};

static ec_sync_info_t zer_device_syncs[] = {
    {0, EC_DIR_OUTPUT, 0, NULL, EC_WD_DISABLE},
    {1, EC_DIR_INPUT, 0, NULL, EC_WD_DISABLE},
    {2, EC_DIR_OUTPUT, 1, zer_device_pdos + 0, EC_WD_ENABLE},
    {3, EC_DIR_INPUT, 1, zer_device_pdos + 1, EC_WD_DISABLE},
    {0xff}
};