123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320 |
- #ifndef __axis_H__
- #define __axis_H__
- #define AXIS_USING_FLOAT //使用浮点运算功能
- //#define AXIS_USING_DEBUG //使用调试
- //#define AXIS_IGNORE_REPEAT_STEP //去除重复步
- //#define AXIS_USING_ACC_TABLE_ONLY //使用加速表同时当减速表来减少内存,使能该选项将会减少内存占用,
- //但是减时间设置将无效化
- #define AXIS_MAX_ACCDEC_STEP 500 //加减速表的缓冲大小
- #define AXIS_TASK_NUMBER 5 //任务缓冲个数
- #define AXIS_CALC_ROUND(reg) 0
- #define PP_Stop 0
- #define PP_NoStop 1
- //任务数量必须大于1,不然就没有作用
- #if defined(AXIS_TASK_NUMBER) && (AXIS_TASK_NUMBER < 1)
- #error "axis tasks data buffer number must bigger 1!"
- #endif
- /*
- #define axis_MODE_PP 1 //位置模式
- #define axis_MODE_PV 2 //速度模式
- #define axis_MODE_STOP 3 //减速停止
- #define axis_MODE_EMGSTOP 4 //急停
- */
- typedef struct
- {
- unsigned short steps; //脉冲需要加减速度时保证加减周期需要的脉冲个数
- unsigned short period; //脉宽
- } axis_accdec_t;
- typedef enum
- {
- AXIS_OUT_ENABLE,
- AXIS_OUT_ERRCLEAR
- }axis_output_name;
- typedef enum
- {
- AXIS_MODE_PP, //PP定位模式
- AXIS_MODE_PV, //连续运动模式
- AXIS_MODE_PP_TASKS, //多段定位模式
- AXIS_MODE_STOP, //停止模式
- AXIS_MODE_EMGSTOP, //急停模式
- AXIS_MODE_HOLD, //保持状态
- AXIS_MODE_PLUS_STOP //运行指定脉冲数停止模式
- }axis_mode;
- typedef enum
- {
- AXIS_ALARM_NONE, //没有报警
- AXIS_ALARM_ERROR, //报警信号
- AXIS_ALARM_HW_POSITIVE, //硬件正限位信号
- AXIS_ALARM_HW_NEGATIVE, //硬件负限位信号
- AXIS_ALARM_SW_POSITIVE, //软件正限位信号
- AXIS_ALARM_SW_NEGATIVE //软件负限位信号
- }axis_alarm_code_t;
- typedef enum
- {
- AXIS_ACCDEC_STATUS_ACC,
- AXIS_ACCDEC_STATUS_DEC
- }axis_accdec_status;
- typedef struct
- {
- int position;
- int speed;
- unsigned short dst_speed_index;
- unsigned short dec_speed_index;
- unsigned short reg_value;
- int target_position;
- int dec_position;
- } axis_tasks_data_t;
- typedef enum
- {
- AXIS_AXIS_NO, //设置轴号
- AXIS_POSITION, //设置脉冲位置计数寄存器
- AXIS_CLOCK, //设置时钟
- AXIS_PERIOD, //加减速周期
- AXIS_MIN_STEP, //设置最小步进
- AXIS_START_SPEED, //设置最小速度
- AXIS_ACCDEC_MODE, //设置加减速模式
- AXIS_AAC_LMT, //设置加加速度限制
- AXIS_ACC_LMT, //设置加速度限制
- AXIS_SPEED_UNIT, //设置速度倍数
- AXIS_ACC_TIME, //加速时间
- AXIS_DEC_TIME, //减速时间
- AXIS_PP_STOP_SELECT, //数控到位是否停止
- AXIS_HOME_SPACE, //零点限位的距离
- AXIS_HOME_OFFSET, //零点偏移
- AXIS_HOME_FIND_SPEED, //零点查找速度
- AXIS_HOME_HIGH_SPEED, //高速回零速度
- AXIS_HOME_LOW_SPEED, //低速回零速度
-
- AXIS_DIR_REVERSE, //方向电平取反
- AXIS_EN_REVERSE, //使能电平取反
- AXIS_ALARM_REVERSE, //伺服告警极性转换
- AXIS_USE_ENCODE //使能使用编码器位置
-
- } axis_paramter_t;
- //轴对象的数据结构
- typedef struct
- {
- unsigned char axis_no; //轴号,也是硬件脉冲号
- unsigned char accdec_mode; //加减速模式,0是直线加减速,1是S加减速
- axis_mode run_mode; //当前模式
- //输入信号
- struct axis_input_struct {
- unsigned short alarm_sgn : 1; //报警信号 1表示有信号
- unsigned short fin_sgn: 1; //脉冲发送完后到位信号
- unsigned short dir_reverse:1; //伺服方向调转,该位能直接让硬件正反方向调换,但是不会影响软件
- unsigned short en_reverse:1; //使能方向调转,该位能直接让硬件使能电平反转,但是不会影响软件
- unsigned short alarm_reverse:1; //伺服告警极性设置,0位常开,1为常闭
- unsigned short hw_positive_lmt : 1; //正极限位 1表示有信号
- unsigned short hw_home_sgn : 1; //零点 1表示有信号
- unsigned short hw_negative_lmt : 1; //负极限位 1表示有信号
- unsigned short hw_slow_lmt:1; //减速信号 1表示有信号
- unsigned short sw_lmt_enable:1; //软件限位功能使能 1表示有信号,这时候软件限位功能才有效
- unsigned short home_high_speed_enable:1; //回零高速使能
- unsigned short manual_cw:1; //手动正转信号
- unsigned short manual_ccw:1; //手动反转信号
- } inputs;
- //输出运算信号,用户不应该直接修改该区域,由软件自动判断
- struct axis_out_struct {
- unsigned short enable:1; //使能信号
- unsigned short error_clear:1; //报警清除信号
- unsigned short error_alarm : 1; //故障报警
- unsigned short hw_positive_alarm : 1; //硬件正极限位报警
- unsigned short hw_negative_alarm : 1; //硬件负极限位报警
- unsigned short sw_positive_alarm : 1; //软件正极限位报警
- unsigned short sw_negative_alarm : 1; //软件负极限位报警
- unsigned short dir:1; //方向 1时是正方向,0时是反方向
- unsigned short on:1; //开关状态,0时是关,1时是开
- unsigned short homing:1; //回零标志位
- unsigned short manual_action : 1; //手动动作标志,当手动有动作的时候,该标志软件会置1,急停时强制清0
- } outputs;
- long sw_positive_limit; //正极软件限位
- long sw_negative_limit; //负极软件限位
- void (* handle)(); //回调事件
- //加减速配置
- unsigned short period; //时间单位,ms
- int clock; //轴的基础频率
- unsigned short max_aac; //最大加加速
- unsigned short max_acc; //最大加速值
- unsigned short max_dec; //最大减速值
- unsigned short min_steps; //最小步限制,保证有最小执行计数
- unsigned short acc_time; //加速时间单位个数
- unsigned short dec_time; //减速时间单位个数
- unsigned short accdec_count; //加减速的计数寄存器
- unsigned short PP_Stop_Select; //数控模式下到数停止选择,0正常停止,到数后继续直低速
- //速度配置
- unsigned short speed_unit; //速度单位,真实设置速度必须乘以该单位才是真正的频率
- unsigned short start_speed; //启动速度
- unsigned short stop_speed; //停止速度
- unsigned short target_speed_reg; //目标速度寄存器值
- //编码器配置
- unsigned char use_encode; //0表示不使用编码器位置,1表示使用编码器位置
- long plus_stop_position; //运行指定脉冲数停止位置
- //加减速度表
- int acc_table_size; //加速表大小
- int dec_table_size; //减速表大小
- unsigned short acc_number_table[AXIS_MAX_ACCDEC_STEP]; //当前速度满足周期的脉冲数
- unsigned short acc_reg_table[AXIS_MAX_ACCDEC_STEP]; //当前速度对应的寄存器值
- #ifndef AXIS_USING_ACC_TABLE_ONLY
- unsigned short dec_number_table[AXIS_MAX_ACCDEC_STEP]; //当前速度满足周期的脉冲数
- unsigned short dec_reg_table[AXIS_MAX_ACCDEC_STEP]; //当前速度对应的寄存器值
- #endif
- #ifdef AXIS_USING_DEBUG
- unsigned int acc_speed_table[AXIS_MAX_ACCDEC_STEP]; //当前速度满足周期的脉冲数
- #ifndef AXIS_USING_ACC_TABLE_ONLY
- unsigned int dec_speed_table[AXIS_MAX_ACCDEC_STEP]; //当前速度对应的寄存器值
- #endif
- #endif
- //软件自动计算,用户一般情况不应该去修改
- long dec_position; //开始减速度的脉冲位置
- long acc_position; //加速脉冲数量
- long position; //当前的脉冲位置
- long target_position; //目标的脉冲位置
- long feed; //只能是-1或者1,用于去掉读取方向的复杂运算
- long encoder; //编码器输入值
- unsigned short cur_speed_index; //当前速度编号,针对的是脉冲表
- unsigned short acc_speed_index; //目标速度编号,针对的是脉冲表
- unsigned short dec_speed_index; //目标速度编号,针对的是脉冲表
- unsigned short dst_period; //目标速度的寄存器值
- unsigned char cur_accdec_status; //当前加减速状态,0表示正在加速,1表示正在减速
- unsigned short cur_speed;//当前速度
- unsigned short cur_reg_value;//当前寄出器的值。
- //回零参数配置
- unsigned char home_method; //回零模式,为0是默认的常规回零模式,后续可能根据需要进行添加
- unsigned char home_step; //回零状态步
- unsigned home_high_speed; //回零速度
- unsigned home_low_speed; //回零低速,一般当有碰到减速信号时才会用到
- unsigned home_find_speed; //查找零点的速度
- int home_offset; //原点偏移,回零可以根据该参数再进行一次移动
- int home_space; //离开零点需要的至少空间
- long negative_space; //离开限位需要的至少空间
- #ifdef AXIS_TASK_NUMBER
- char cur_task_index; //当前任务位置
- char task_number; //任务数
- axis_tasks_data_t tasks[AXIS_TASK_NUMBER]; //任务缓冲池
- #endif
- } axis_object_t;
- typedef struct
- {
- char index; //编号,辅助识别用,意义不大
- int posi; //阶段位置
- unsigned short speed; //该阶段期望的速度
- //计算变量
- unsigned short dst_speed_index; //目标速查表编号
- unsigned short reg; //该阶段速度对应的寄存器值
- int decposition; //减速位置
- } axis_stage_task_t;
- //硬件正极限信号
- #define AXIS_HW_POSITIVE_SGN(axis) axis->inputs.hw_positive_lmt
- //硬件负极限信号
- #define AXIS_HW_NEGATIVE_SGN(axis) axis->inputs.hw_negative_lmt
- //方向反向信号
- #define AXIS_REVERSE_SGN(axis) axis->inputs.dir_reverse
- //软件极限保护使能信号
- #define AXIS_SW_PROTECCT_SGN(axis) axis->inputs.sw_lmt_enable
- //减速信号
- #define AXIS_SLOW_SGN(axis) axis->inputs.hw_slow_lmt
- //报警信号
- #define AXIS_ALARM_SGN(axis) axis->inputs.alarm_sgn
- //回零时使能高速,当有减速信号开关时才使用
- #define AXIS_HOMING_HIGH_SPEED_ENABLE(axis) axis->inputs.home_high_speed_enable
- //是否正在回零点
- #define AXIS_IS_HOMING(axis) (axis->outputs.homing)
- //是否正在自动运行
- #define AXIS_IS_RUNNING(axis) (axis->outputs.on)
- //是否停止状态
- #define AXIS_IS_STOP(axis) (axis->outputs.on == 0)
- //是否出现错误
- #define AXIS_IS_ERROR(axis) (axis->outputs.error_alarm)
- //硬件正极限信号
- #define AXIS_EXCEED_HW_POSITIVE(axis) (axis->outputs.hw_positive_alarm)
- //硬件负极限信号
- #define AXIS_EXCEED_HW_NEGATIVE(axis) (axis->outputs.hw_negative_alarm)
- //软件正极限信号
- #define AXIS_EXCEED_SW_POSITIVE(axis) (axis->outputs.sw_positive_alarm)
- //软件负极限信号
- #define AXIS_EXCEED_SW_NEGATIVE(axis) (axis->outputs.sw_negative_alarm)
- //硬件使能信号
- #define AXIS_IS_ENABLE(axis) (axis->outputs.enable)
- //硬件清除报警信号
- #define AXIS_IS_ERRCLEAR(axis) (axis->outputs.error_clear)
- extern long axispostion;
- void axis_run(axis_object_t *axis);
- void axis_start(axis_object_t *axis);
- void axis_stop(axis_object_t *axis);
- void axis_emgstop(axis_object_t *axis);
- void axis_pp(axis_object_t *axis, int rel, int position, int speed,long slowpos);
- void axis_pv(axis_object_t *axis, int dir, int speed);
- void axis_pv_change_speed(axis_object_t *axis, int speed);
- void axis_stop_at(axis_object_t *axis, int postion);
- void axis_init_task(axis_object_t *axis, int dir);
- void axis_add_pp_task(axis_object_t *axis, int feed, int speed);
- void axis_start_pp_task(axis_object_t *axis);
- void axis_ccw(axis_object_t *axis);
- void axis_cw(axis_object_t *axis);
- void axis_home_run(axis_object_t *axis);
- void axis_home(axis_object_t *axis, int method, int home_speed, int find_speed);
- void axis_home_enable_high_speed(axis_object_t *axis, int high_speed);
- void axis_stop_all(axis_object_t *axis);
- void axis_set_ouput(axis_object_t *axis, axis_output_name type, int value);
- void axis_it_handle(void *handle);
- int axis_cw_lmt(axis_object_t *axis);
- int axis_ccw_lmt(axis_object_t *axis);
- axis_alarm_code_t axis_alarm_code(axis_object_t *axis);
- void axis_enable(axis_object_t *axis);
- void axis_disable(axis_object_t *axis);
- void axis_set_parameter(axis_object_t *axis, axis_paramter_t cmd, int argv);
- int axis_get_parameter(axis_object_t *axis, axis_paramter_t cmd);
- void axis_enable_software_lmt(axis_object_t *axis, int positive, int negative);
- void axis_disable_software_lmt(axis_object_t *axis);
- void axis_enable_irq_callback(axis_object_t * axis, void(*handle)());
- void axis_disable_irq_callback(axis_object_t * axis);
- void axis_pv_change_speed_table(axis_object_t *axis, int target_speed);
- void axis_set_accdec(axis_object_t *axis, int accdec_mode, int acc_time, int dec_time);
- void axis_pp_change_speed (axis_object_t *axis, int rel,
- int position, int target_speed,long slowpos);
- void axis_change_speed_direct(axis_object_t *axis, int target_speed);
- void axis_plus_stop(axis_object_t *axis, unsigned short num);
- #endif
|