- #include <stdio.h>
- #include <stdlib.h>
- typedef unsigned short uint16_t;
- typedef unsigned char uint8_t;
- #define EEP_INFO_CUR_POS 70
- #define EEP_MOTOR_CUR_POS_INIT_NUM 11
- typedef struct
- {
- uint8_t m_current_dir; //电机当前方向
- uint8_t m_locked; //电机堵转
- uint8_t m_anti_pinch; //电机防夹
- uint16_t m_current_pos; //电机当前位置
- uint16_t m_current_speed; //电机当前速度
- uint16_t m_start_move_pos;//用于标记运动前位置
- }_BOTTOM_STATE;
- typedef enum
- {
- /******电机当前状态专用******/
- IDLE, //0空闲状态
- /******电机目标状态专用******/
- STOPING, //1停止状态
- /*********公用部分********/
- SELF_LEARN, //2自学习状态
- ONE_TOUCH_EZE, //3一键EZE
- ONE_TOUCH_EZE_RET, //4一键恢复
- ONE_TOUCH_REL, //5一键释放
- ONE_TOUCH_REL_RET, //6一键释放恢复
- FORWARD_COMFORT, //7向前舒适性调节
- BACKWORD_COMFORT, //8向后舒适性调节
- SAVE_POS_1, //9记忆位置1
- SAVE_POS_2, //10记忆位置1
- SAVE_POS_3, //11记忆位置1
- SAVE_POS_4, //12记忆位置1
- RET_SAVE_POS_1, //13回到位置1
- RET_SAVE_POS_2, //14回到位置2
- RET_SAVE_POS_3, //15回到位置3
- RET_SAVE_POS_4, //16回到位置4
- ONE_TOUCH_RESET, //17复位
- ABOVE_WATER, //18脱困模式
- ANTI_CLAMP, //19防夹
- FORWARD_COMFORT_LOW, //20向前舒适性调节低速
- BACKWORD_COMFORT_LOW, //21向后舒适性调节低速
- SELF_LEARN_DOUBLE, //22双边自学习状态
- /**********公用部分*********/
- }_MOTOR_RUN_MODE;
- //电机控制主结构体
- typedef struct
- {
- uint8_t m_mode; //电机模式,0-正常模式,1-测试模式
- uint8_t self_learn_block_state; //自学习等待标志位
- uint8_t one_touch_state; //EEPROM更新标志位(一键操作使用)
- uint8_t is_over_travel; //超程标志位,0-超程,1-未超程
- uint8_t m_syn_enable; //双机同步开启/关闭状态
- uint16_t time_count; //当前位置保存计时器
- uint16_t time_self_learn; //自学习延迟计时
- uint16_t time_idle; //电机空闲状态计时
- uint8_t ucFaultTypeHold;//故障保持标记1:有故障,0:无,bit[0]:堵转故障指示,bit[7:1]:预留
- _MOTOR_RUN_MODE m_run_target_mode; //电机目标运行状态(来自上位机CAN报文)(应用层)
- _BOTTOM_STATE bottom_state; //当前电机状态(接口层)
- }_MCC_CONTROL;
- typedef struct
- {
- uint16_t learn_s; //自学习状态
- uint16_t fault_grade; //故障等级
- uint16_t motor_cur_pos; //电机当前位置
- uint16_t forward_soft_pos; //前软挡点位置
- uint16_t backward_soft_pos; //后软挡点位置
- uint16_t eze_ret_pos; //一键恢复位置
- uint16_t release_ret_pos; //一键释放恢复位置
- uint16_t mem_1_pos; //记忆位置1
- uint16_t mem_2_pos; //记忆位置2
- uint16_t mem_3_pos; //记忆位置3
- uint16_t mem_4_pos; //记忆位置4
- uint16_t motor_cur_address; //电机位置索引
- }_EEPROM_INFO;
- _MCC_CONTROL Mcc_Control;
- uint16_t m_current_speed;
- uint16_t EEP_MOTOR_CUR_POS_NUM;
-
- void MemCurrentPos( _MCC_CONTROL *const Mcc_Control );
- int main()
- {
-
- printf("请输入当前速度:");
- scanf("%d",&Mcc_Control.bottom_state.m_current_speed);
- printf("请输入当前计时:");
- scanf("%d",&Mcc_Control.time_count);
- printf("请输入当前位置:");
- scanf("%d",&Mcc_Control.bottom_state.m_current_pos);
- printf("请输入当前位置编号:");
- scanf("%d",&EEP_MOTOR_CUR_POS_NUM);
- MemCurrentPos(Mcc_Control);
-
- while(1);
- return 0;
- }
- void MemCurrentPos( _MCC_CONTROL *const Mcc_Control )
- {
- static uint16_t EEP_MOTOR_LAST_POS_NUM;
- //当电机停下来后进行
- if( Mcc_Control->bottom_state.m_current_speed != 0 )
- {
- Mcc_Control->time_count = 0;
-
- ///
- printf("速度不为0,不记忆了,退出喽");
- return;
- }
- else
- {
- //开始进行计时
- Mcc_Control->time_count ++;
- //3s记录一次当前位置
- if( (Mcc_Control->time_count) < 600)
- {
- printf("间隔太短,无需记忆");
- return;
- }
- //时间到达
- else
- {
- Mcc_Control->time_count = 0;
- //读取当前EEPROM保存的位置
- uint16_t eep_info_cur_pos = EEP_INFO_CUR_POS;
- //当前位置和EEPROM位置不一致
- if( Mcc_Control->bottom_state.m_current_pos != eep_info_cur_pos )
- {
- if(20U <= EEP_MOTOR_CUR_POS_NUM)
- {
- EEP_MOTOR_CUR_POS_NUM = EEP_MOTOR_CUR_POS_INIT_NUM;
- printf("重定义初始位置");
-
- }
- eep_info_cur_pos = Mcc_Control->bottom_state.m_current_pos;
-
- }
- //位置相同
- else
- {
- //do nothing
- }
- if(EEP_MOTOR_LAST_POS_NUM != EEP_MOTOR_CUR_POS_NUM)
- {
- EEP_MOTOR_LAST_POS_NUM = EEP_MOTOR_CUR_POS_NUM;
- }
- }
- }
- }
复制代码 |