找回密码
 立即注册

QQ登录

只需一步,快速开始

搜索
查看: 2644|回复: 4
打印 上一主题 下一主题
收起左侧

stm32控制两相电机源程序

[复制链接]
跳转到指定楼层
楼主
ID:530349 发表于 2019-5-8 10:54 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
这是我设计的stm32F103控制两相电机程序
测试好用
附件有keil工程文件

单片机源程序如下:
  1. #include "Bsp.h"
  2. #include "stm32f10x.h"
  3. #include <stdio.h>
  4. #include <stdlib.h>
  5. #include <string.h>

  6. #ifdef __GNUC__
  7.   /* With GCC/RAISONANCE, small printf (option LD Linker->Libraries->Small printf
  8.      set to 'Yes') calls __io_putchar() */
  9.   #define PUTCHAR_PROTOTYPE int __io_putchar(int ch)
  10. #else
  11.   #define PUTCHAR_PROTOTYPE int fputc(int ch, FILE *f)
  12. #endif /* __GNUC__ */

  13. #define DEBUG        1

  14. char                        TestWord[USARTBUFLEN];
  15. char                        Cmd_buf[USARTBUFLEN];
  16. uint8_t                Cmd_LockFlag        =        0;
  17. uint8_t                ResetCmd_Flag        =        0;
  18. uint8_t                Cmd_FinishFlag=        0;
  19. #define         ACKSTR                "OK_\r\n"
  20. #define   NACKSTR                "BADCMD_\r\n"
  21. #define         BUSYSTR                "WAIT_\r\n"

  22. char                          startchar[USARTBUFLEN];
  23. const char CMD_LIST[8][PARASTRLEN]={"RESET","MOVE","STOP","POWER","SET","READ","ZERO"};        //允许的命令
  24. const        char HelpMsg[8][USARTBUFLEN]={{"Command List: MOVE STOP SET READ POWER ZERO;\r\n"},
  25.                                                                                                                                                 {"MOVE:         e.g. :FF MOVE VERTICAL/HORIZONT CW/ANCW xxx.xx(0-360,sOLUTION: 0.01);\r\n"},
  26.                                                                                                                                                 {"STOP:                e.g. :FF STOP VERTICAL/HORIZONT;\r\n"},
  27.                                                                                                                                                 {"POWER:        e.g. :FF POWER VERTICAL/HORIZONT ON/OFF;\r\n"},
  28.                                                                                                                                                 {"SET:                e.g. :FF SET VERTICAL/HORIZONT ;\r\n"},
  29.                                                                                                                                                 {"READ:                e.g. :FF READ VERTICAL/HORIZONT;\r\n"},
  30.                                                                                                                                                 {"ZERO:                e.g. :FF ZERO VERTICAL/HORIZONT;\r\n"},
  31.                                                                                                                                                 {"RESET:        e.g. :FF RESET;\r\n"}};
  32. char                         CmdRespondStr[50];
  33. char                          para_list[CMDPARANUM][PARASTRLEN];                                                                                                                                                        //保存命令中的参数
  34. SYSTEM_STA        Sys_Sta;
  35.                                                                                                                                                
  36. void CMD_Analyse(const char *cmd,uint8_t        cmdlen);
  37. uint8_t str2float(const char *str,float *result);
  38. u8 Flag=0;                                      //指示输出角度是否到达360度或-360,如果到了,讲360置为0度                                                                                                               

  39. /****************************************************************************
  40. * 名    称:                                 main
  41. * 功    能:                 串口占用GPIO初始化
  42. * 入口参数:                 
  43. * 出口参数:
  44. * 说    明:
  45. * 调用方法:
  46. * 修改日志:  
  47. ****************************************************************************/
  48. int  main(void)
  49. {
  50.         uint16_t                temp[4],temp1=0,HVTemp1;
  51.         uint16_t                data_tmp;
  52.         int32_t                        step_num        = 0;
  53.         uint32_t                devison_tmp,step_speed;
  54.         uint32_t                sendbytes=0;
  55.         const        char        *usart_sendaddr;
  56.         float                                angle_tmp,speed_tmp,limit_tmp;
  57.         uint8_t                        error;
  58.         uint8_t                 tmp                                          = 0;
  59.         uint8_t                        fault_try                        =        0;
  60.         STEPMOTOR_INDEX                ver_hor;
  61.         MOTIONDIR                                        cw_ncw;
  62.         uint8_t                                                cycle_cnt,char_num;;
  63.         Cmd_LockFlag                                                        =        0;
  64.         Bsp_Init();
  65.        
  66. //         AT24CXX_Write(0,HelpMsg[0],strlen(HelpMsg[0]));
  67.         usart_sendaddr                                                =        CmdRespondStr;

  68.         cycle_cnt                                                                         =        0;
  69.         temp[0]                                                                                =        0;
  70.         ResetCmd_Flag                                                        =        0;
  71.         #ifdef DEBUG
  72.                 //打印识别信息
  73.                 char_num        = sprintf(startchar,"\r\n 134mm-Diameter Step-Motor Driver.\r\nCorp.AGGC,By LK,140\r\n");
  74.                 UsartDMASend(startchar,char_num);
  75.         #endif
  76.        
  77.                
  78.         //上电首先检查振镜位置是否超出允许
  79. /*       
  80.         Delay_ms(5000);
  81.         if((RESET == GPIO_ReadInputDataBit(ANCW_LIMIT))|(RESET == GPIO_ReadInputDataBit(CW_LIMIT)))       
  82.         {
  83.                 Hor_Info.motion_sta                        =        OutOfRange;       
  84.                
  85.                 if(RESET == GPIO_ReadInputDataBit(ANCW_LIMIT))
  86.                 {
  87.                         Hor_Info.motion_dir                =        Motion_CW;
  88.                         Hor_Info.dir_now                        = Hor_Info.dir_range_ancw;
  89.                 }
  90.                 else
  91.                 {
  92.                         Hor_Info.motion_dir                =        Motion_ANCW;
  93.                         Hor_Info.dir_now                        = Hor_Info.dir_range_cw;
  94.                 }
  95.         }
  96.        
  97. */
  98.         while(1)
  99.         {
  100.         //程序运行指示灯
  101.                 cycle_cnt++;
  102.                 if(cycle_cnt > 1)
  103.                 {
  104.                         LEDToggle();
  105.                         cycle_cnt        =0;
  106.                 }
  107.        
  108.                 //垂直电机位置超出允许范围,尝试反向转动3次,失败后,定义为振镜异常
  109. /*               
  110.                 if(OutOfRange == Hor_Info.motion_sta)
  111.                 {
  112.                         Delay_ms(1000);
  113.                         if((RESET == GPIO_ReadInputDataBit(ANCW_LIMIT))|(RESET == GPIO_ReadInputDataBit(CW_LIMIT)))
  114.                         {
  115.                                 //
  116.                                 if(fault_try<3)
  117.                                 {
  118.                                         if(Stop== Hor_Info.motor_sta)
  119.                                         {
  120.                                                 MOTOR_DISEN_HOR;
  121.                                                 Hor_Info.motor_sta                =        Move;
  122.                                                 Hor_Info.motor_fb                        =        FB_Enable;
  123.                                                 Move_Angles(Horizontal,Hor_Info.motion_dir,5);        //尝试反向转5度,离开限位开关位置
  124.                                                 MOTOR_START_HOR;
  125.                                                 fault_try++;
  126.                                         }
  127.                                 }
  128.                                 else
  129.                                 {
  130.                                         Hor_Info.motion_sta                        =        Fault;
  131.                                         MOTOR_EN_HOR;
  132.                                 }
  133.                         }
  134.                 }
  135.                 else
  136.                                 fault_try                                                                        =        0;       
  137.                
  138. */               
  139.                
  140.                
  141.                 //收到一条命令
  142.                 if(1 == Cmd_FinishFlag)
  143.                 {
  144.                         Cmd_LockFlag                =0;                        //串口命令解锁
  145.                         Cmd_FinishFlag        =0;
  146.                         CMD_Analyse(Cmd_buf+1,30);
  147.                         memset(Cmd_buf,0,USARTBUFLEN);  //USARTBUFLEN=100 串口命令最大长度
  148.                   //void *memset(void *s,int c,size_t n)  将已开辟内存空间S的首个n字节的值设置为C
  149.                         sendbytes        =        sprintf(CmdRespondStr,NACKSTR);  //sprintf字符串格式化命令,主要将格式化的数据写入字符串中

  150.                         if(0 == strncmp(para_list[0],"MOVE",sizeof("MOVE")))                        //电机运动命令,sizeof返回字节大小
  151.                         {                        //strncmp ( const char * str1, const char * str2, size_t n );str1,str2为需要比较的字符串,n为比较数量
  152.                                                        //str1第一个字符减去str2第一个字符,若差值为0,则继续比较下一个字符
  153.                                 if(0 == strncmp(para_list[1],"VERTICAL",sizeof("VERTICAL")))
  154.                                         ver_hor                        =        Vertical; //垂直
  155.                                 else if(0 == strncmp(para_list[1],"HORIZONT",sizeof("HORIZONT")))
  156.                                         ver_hor                        =        Horizontal;  //水平
  157.                                 else
  158.                                         ver_hor                        = Unknow;
  159.                                
  160.                                 if(Unknow != ver_hor)
  161.                                 {
  162.                                         if(0 == str2float(para_list[2],&angle_tmp))
  163.                                         {
  164.                                                 float end_angle=0;
  165.                                                 end_angle= Motor_Info[ver_hor].angle_now+angle_tmp;
  166.                                                 //当前位置不能超过设定范围
  167.                                                 if((end_angle >= -1*Motor_Info[ver_hor].limit_angle)&&(end_angle <= Motor_Info[ver_hor].limit_angle))
  168.                                                 {
  169.                                                         if(angle_tmp >0)
  170.                                                         {
  171.                                                                 cw_ncw                        = Motion_CW;  //步进电机方向
  172.                                                         }
  173.                                                         else if(angle_tmp <0)
  174.                                                         {
  175.                                                                 cw_ncw                        =        Motion_ANCW;
  176.                                                                 angle_tmp                *= -1;
  177.                                                         }
  178.                                                         else
  179.                                                                 cw_ncw                        =        Hold;
  180.                                                 }
  181.                                                 else if(end_angle < -1*Motor_Info[ver_hor].limit_angle)        //超过逆时针转动范围
  182.                                                 {
  183.                                                                 cw_ncw                        = Motion_ANCW;
  184.                                                                 angle_tmp                = Motor_Info[ver_hor].angle_now + Motor_Info[ver_hor].limit_angle;
  185.                                                        
  186.                                                 }
  187.                                                 else         if(end_angle > Motor_Info[ver_hor].limit_angle)        //超过顺时针转动范围
  188.                                                 {
  189.                                                                 cw_ncw                        = Motion_CW;
  190.                                                                 angle_tmp                = Motor_Info[ver_hor].limit_angle - Motor_Info[ver_hor].angle_now;
  191.                                                 }
  192.                                                 else
  193.                                                         cw_ncw                        =        Hold;
  194.                                                        
  195.                                                         //电机停止状态下,才能允许转动
  196.                                                         if((Hold != cw_ncw)&&(Stop == Motor_Info[ver_hor].motor_sta))
  197.                                                         {
  198. //                                                                 Motor_Info[ver_hor].move_pulsenum        =(uint32_t)(angle_tmp*CODER_NUM);
  199.                                                                 Move_Angles(ver_hor,cw_ncw,angle_tmp);
  200.                                                                 sendbytes        =        sprintf(CmdRespondStr,ACKSTR);
  201.                                                         }
  202.                                                         else
  203.                                                                 sendbytes        =        sprintf(CmdRespondStr,BUSYSTR);
  204. //                                                 }
  205. //                                                 else
  206. //                                                         sendbytes        =        sprintf(CmdRespondStr,"AngleWrong_\r\n");
  207.                                                        
  208.                                         }
  209.                                 else sendbytes        =        sprintf(CmdRespondStr,NACKSTR);
  210.                                 }
  211.                         }
  212.                         else if(0 == strncmp(para_list[0],"CHECK",sizeof("CHECK")))        //查询当前电机状态(是否在转)
  213.                         {
  214.                                 if(0 == strncmp(para_list[1],"VERTICAL",sizeof("VERTICAL")))
  215.                                 {
  216.                                         ver_hor                        =        Vertical;
  217.                                 }
  218.                                 else if(0 == strncmp(para_list[1],"HORIZONT",sizeof("HORIZONT")))
  219.                                 {
  220.                                         ver_hor                        =        Horizontal;
  221.                                 }
  222.                                 else
  223.                                         ver_hor                        = Unknow;
  224.                                
  225.                                 if(Unknow != ver_hor)
  226.                                 {
  227.                                         if(Move == Motor_Info[ver_hor].motor_sta)
  228.                                                 sendbytes        =        sprintf(CmdRespondStr,"MOVE_\r\n");
  229.                                         else
  230.                                                 sendbytes        =        sprintf(CmdRespondStr,"HOLD_\r\n");
  231.                                 }
  232.                         }
  233.                         else if(0 == strncmp(para_list[0],"STATUS",sizeof("STATUS")))        //查询当前电机状态(是否在转)
  234.                         {
  235.                                 if(0 == strncmp(para_list[1],"VERTICAL",sizeof("VERTICAL")))
  236.                                 {
  237.                                         ver_hor                        =        Vertical;
  238.                                 }
  239.                                 else if(0 == strncmp(para_list[1],"HORIZONT",sizeof("HORIZONT")))
  240.                                 {
  241.                                         ver_hor                        =        Horizontal;
  242.                                 }
  243.                                 else
  244.                                         ver_hor                        = Unknow;
  245.                                
  246.                                 if(Unknow != ver_hor)
  247.                                 {
  248.                                         if(Stepslost == Motor_Info[ver_hor].motion_sta)
  249.                                                 sendbytes        =        sprintf(CmdRespondStr,"STEPSLOST_\r\n");
  250.                                         else if(OutOfRange == Motor_Info[ver_hor].motion_sta)
  251.                                                 sendbytes        =        sprintf(CmdRespondStr,"OVERRANGE_\r\n");
  252.                                         else if(Zero == Motor_Info[ver_hor].motion_sta)
  253.                                                 sendbytes        =        sprintf(CmdRespondStr,"ZERO_\r\n");
  254.                                         else if(Fault == Motor_Info[ver_hor].motion_sta)
  255.                                                 sendbytes        =        sprintf(CmdRespondStr,"FAULT_\r\n");
  256.                                         else
  257.                                                 sendbytes        =        sprintf(CmdRespondStr,"NORMAL_\r\n");
  258.                                 }
  259.                         }
  260.                         else if(0 == strncmp(para_list[0],"STOP",sizeof("STOP")))                //运动停止命令
  261.                         {
  262.                                 if(0 == strncmp(para_list[1],"VERTICAL",sizeof("VERTICAL")))
  263.                                         ver_hor                        =        Vertical;
  264.                                 else if(0 == strncmp(para_list[1],"HORIZONT",sizeof("HORIZONT")))
  265.                                         ver_hor                        =        Horizontal;
  266.                                 else
  267.                                         ver_hor                        = Unknow;
  268.                                
  269.                                 if(Unknow != ver_hor)
  270.                                 {
  271.                                         if(Vertical == ver_hor)
  272.                                         {
  273.                                                 Ver_Info.motor_sta                =        Stop;
  274.                                                 Ver_Info.motor_fb                        =        FB_Disable;
  275.                                                 MOTOR_STOP_VER;
  276.                                         }
  277.                                         else
  278.                                         {
  279.                                                 Hor_Info.motor_sta                =        Stop;
  280.                                                 Hor_Info.motor_fb                        =        FB_Disable;
  281.                                                 MOTOR_STOP_HOR;
  282.                                         }
  283.                                         sendbytes        =        sprintf(CmdRespondStr,ACKSTR);
  284.                                 }
  285.                         }
  286.                         else if(0 == strncmp(para_list[0],"SET",sizeof("SET")))                                                //参数设置命令
  287.                         {
  288.                                 if(0 == strncmp(para_list[1],"VERTICAL",sizeof("VERTICAL")))
  289.                                         ver_hor                        =        Vertical;
  290.                                 else if(0 == strncmp(para_list[1],"HORIZONT",sizeof("HORIZONT")))
  291.                                         ver_hor                        =        Horizontal;
  292.                                 else
  293.                                         ver_hor                        = Unknow;
  294.                                
  295.                                 //电机停止时,允许进行参数设定
  296.                                 if(Stop == Motor_Info[ver_hor].motor_sta)
  297.                                 {
  298.                                         if(Unknow != ver_hor)
  299.                                         {
  300.                                                 if(0 == strncmp(para_list[2],"DEVISION",sizeof("DEVISION")))        //设置细分比例
  301.                                                 {
  302.                                                         devison_tmp        =        atoi(para_list[3]);
  303.                                                         if((devide_1 == devison_tmp)|(devide_2 == devison_tmp)|(devide_8 == devison_tmp)|(devide_16 == devison_tmp))
  304.                                                         {
  305.                                                                 if(Vertical == ver_hor)
  306.                                                                 {
  307.                                                                         Ver_Info.subdevision                =        (DEVISIONVALUE)devison_tmp;
  308.                                                                         Ver_Info.step_resolution=        MOTOR_RESOLUTION/(RATIO_GEAR*devison_tmp);
  309.                                                                         Ver_Info.step_speed        =        360.0f*Ver_Info.speed/(60.0f*Ver_Info.step_resolution);
  310.                                                                         SPEEDSET_VER(1000000/Ver_Info.step_speed);
  311.                                                                         Ver_Info.dir_range_cw                        =        (int32_t)RATIO_GEAR*Ver_Info.subdevision*180/MOTOR_RESOLUTION;        //顺时针电机方位角范围
  312.                                                                         Ver_Info.dir_range_ancw                =        -1*(int32_t)RATIO_GEAR*Ver_Info.subdevision*180/MOTOR_RESOLUTION;        //逆时针电机方位角范围
  313.                                                                         //更新EEPROM中保存的参数
  314.                                                                         UPDATE_DEVISIONH_VER;
  315.                                                                         UPDATE_STEPRE_VER;
  316.                                                                         UPDATE_SPEED_VER;
  317.                                                                         UPDATE_DIRRANGE_VER;
  318.                                                                 }
  319.                                                                 else if(Horizontal == ver_hor)
  320.                                                                 {
  321.                                                                         Hor_Info.subdevision                =(DEVISIONVALUE)devison_tmp;
  322.                                                                         Hor_Info.step_resolution=        MOTOR_RESOLUTION/(RATIO_GEAR*devison_tmp);
  323.                                                                         Hor_Info.step_speed        =        360.0f*Hor_Info.speed/(60.0f*Hor_Info.step_resolution);
  324.                                                                         SPEEDSET_HOR(1000000/Hor_Info.step_speed);
  325.                                                                         Hor_Info.dir_range_cw                        =        (int32_t)RATIO_GEAR*Hor_Info.subdevision*360/MOTOR_RESOLUTION;        //顺时针电机方位角范围
  326.                         Hor_Info.dir_range_ancw                =        -1*(int32_t)RATIO_GEAR*Hor_Info.subdevision*360/MOTOR_RESOLUTION;        //逆时针电机方位角范围
  327.                                                                         AT24CXX_Write(HORINFO_ADDR,(const uint8_t *)&Hor_Info,sizeof(MOTORINFO));
  328.                                                                         //更新EEPROM中保存的参数
  329.                                                                         UPDATE_DEVISIONH_HOR;
  330.                                                                         UPDATE_STEPRE_HOR;
  331.                                                                         UPDATE_SPEED_HOR;
  332.                                                                         UPDATE_DIRRANGE_HOR;
  333.                                                                 }
  334.                                                                 sendbytes        =        sprintf(CmdRespondStr,ACKSTR);
  335.                                                         }
  336.                                                 }
  337.                                                 else if(0 == strncmp(para_list[2],"LOCATION",sizeof("LOCATION")))        //设置当前位置对应角度,手动归零时使用该命令
  338.                                                 {
  339.                                                         if(0 == str2float(para_list[3],&angle_tmp))
  340.                                                         {
  341.                                                                 if((angle_tmp >= -185)&&(angle_tmp <= 185))
  342.                                                                 {
  343.                                                                         if(Vertical == ver_hor)
  344.                                                                         {
  345.                                                                                 Ver_Info.angle_now                =        angle_tmp;
  346.                                                                                 Ver_Info.dir_now                        =        angle_tmp/360.0f*Ver_Info.dir_range_cw*2;
  347.                                                                                 //更新EEPROM中保存的参数
  348.                                                                                 UPDATE_ANGEL_VER;
  349.                                                                                 UPDATE_DIRNOW_VER;
  350.                                                                         }
  351.                                                                         else if(Horizontal == ver_hor)
  352.                                                                         {
  353.                                                                                 Hor_Info.angle_now                =        angle_tmp;
  354.                                                                                 Hor_Info.dir_now                        =        angle_tmp/360.0f*Hor_Info.dir_range_cw*2;
  355.                                                                                 //更新EEPROM中保存的参数
  356.                                                                                 UPDATE_ANGEL_HOR;
  357.                                                                                 UPDATE_DIRNOW_HOR;
  358.                                                                         }
  359.                                                                         sendbytes        =        sprintf(CmdRespondStr,ACKSTR);
  360.                                                                 }
  361.                                                         }
  362.                                                 }
  363.                                                 else if(0 == strncmp(para_list[2],"SPEED",sizeof("SPEED")))        //设置转动速度
  364.                                                 {
  365.                                                         speed_tmp        = atof(para_list[3]);
  366.                                                         if((speed_tmp > 0)&&(speed_tmp <= 2000))                //分钟转速小于2000r/min
  367.                                                         {
  368.                                                                 if(Vertical == ver_hor)
  369.                                                                 {
  370.                                                                         Ver_Info.speed                        =        speed_tmp;
  371.                                                                         Ver_Info.step_speed        =        360.0f*Ver_Info.speed/(60.0f*Ver_Info.step_resolution);
  372.                                                                         SPEEDSET_VER(1000000/Ver_Info.step_speed);
  373.                                                                         //更新EEPROM中保存的参数
  374.                                                                         UPDATE_SPEED_VER;
  375.                                                                         UPDATE_SPEEDSTEPS_VER;
  376. //                                                                         AT24CXX_Write(VERINFO_ADDR,(const uint8_t *)&Ver_Info,sizeof(MOTORINFO));
  377.                                                                 }
  378.                                                                 else if(Horizontal == ver_hor)
  379.                                                                 {
  380.                                                                         Hor_Info.speed                        =        speed_tmp;
  381.                                                                         Hor_Info.step_speed        =        360.0f*Hor_Info.speed/(60.0f*Hor_Info.step_resolution);
  382.                                                                         SPEEDSET_HOR(1000000/Hor_Info.step_speed);
  383.         //                                                                 AT24CXX_Write(HORINFO_ADDR,(const uint8_t *)&Hor_Info,sizeof(MOTORINFO));
  384.                                                                         //更新EEPROM中保存的参数
  385.                                                                         UPDATE_SPEED_HOR;
  386.                                                                         UPDATE_SPEEDSTEPS_HOR;
  387.                                                                 }
  388.                                                                 sendbytes        =        sprintf(CmdRespondStr,ACKSTR);
  389.                                                         }
  390.                                                 }
  391.                                                 else if(0 == strncmp(para_list[2],"LIMIT",sizeof("LIMIT")))        //设置转动范围
  392.                                                 {
  393.                                                         if(0 == str2float(para_list[3],&limit_tmp))
  394.                                                         {
  395.                                                                 if((limit_tmp > 0)&&(limit_tmp <= 361))
  396.                                                                 {
  397.                                                                         Motor_Info[ver_hor].limit_angle                                =        limit_tmp;
  398.                                                                         if(Vertical == ver_hor)
  399.                                                                                 UPDATE_LIMITANGEL_VER;
  400.                                                                         else
  401.                                                                                 UPDATE_LIMITANGEL_HOR;
  402.                                                                                
  403.                                                                         sendbytes        =        sprintf(CmdRespondStr,ACKSTR);
  404.                                                                 }
  405.                                                         }
  406.                                                 }
  407.                                         }
  408.                                 }
  409.                                 else
  410.                                                 sendbytes        =        sprintf(CmdRespondStr,BUSYSTR);
  411.                         }
  412.                         else if(0 == strncmp(para_list[0],"READ",sizeof("READ")))                                //读取细分参数命令
  413.                         {
  414.                                 if(0 == strncmp(para_list[1],"VERTICAL",sizeof("VERTICAL")))
  415.                                         ver_hor                        =        Vertical;
  416.                                 else if(0 == strncmp(para_list[1],"HORIZONT",sizeof("HORIZONT")))
  417.                                         ver_hor                        =        Horizontal;
  418.                                 else
  419.                                         ver_hor                        = Unknow;
  420.                                
  421.                                 if(Unknow != ver_hor)
  422.                                 {
  423.                                         if(0 == strncmp(para_list[2],"DEVISION",sizeof("DEVISION")))        //读取细分比例
  424.                                         {
  425.                                                 if(Vertical == ver_hor)
  426.                                                         sendbytes        =        sprintf(CmdRespondStr,"Vertical_Devision_%d_\r\n",Ver_Info.subdevision);
  427.                                                 else if(Horizontal == ver_hor)
  428.                                                         sendbytes        =        sprintf(CmdRespondStr,"Horizontal_Devision_%d_\r\n",Hor_Info.subdevision);
  429.                                         }
  430.                                         else if(0 == strncmp(para_list[2],"LOCATION",sizeof("LOCATION")))        //读取当前位置对应角度
  431.                                         {
  432.                                                 if(Vertical == ver_hor)
  433.                                                         sendbytes        =        sprintf(CmdRespondStr,"%.2f_\r\n",Ver_Info.angle_now);
  434.                                                 else if(Horizontal == ver_hor)
  435.                                                         sendbytes        =        sprintf(CmdRespondStr,"%.2f_\r\n",Hor_Info.angle_now);
  436.                                         }
  437.                                         else if(0 == strncmp(para_list[2],"SPEED",sizeof("SPEED")))        //读取当前位置对应步进速度
  438.                                         {
  439.                                                 if(Vertical == ver_hor)
  440.                                                         sendbytes        =        sprintf(CmdRespondStr,"%.2d_\r\n",Ver_Info.step_speed);
  441.                                                 else if(Horizontal == ver_hor)
  442.                                                         sendbytes        =        sprintf(CmdRespondStr,"%.2d_\r\n",Hor_Info.step_speed);
  443.                                         }
  444.                                         else if(0 == strncmp(para_list[2],"LIMIT",sizeof("LIMIT")))        //设置转动范围
  445.                                         {
  446.                                                 if(Vertical == ver_hor)
  447.                                                         sendbytes        =        sprintf(CmdRespondStr,"%.1f_\r\n",Ver_Info.limit_angle);
  448.                                                 else if(Horizontal == ver_hor)
  449.                                                         sendbytes        =        sprintf(CmdRespondStr,"%.1f_\r\n",Hor_Info.limit_angle);
  450.                                         }
  451.                                 }
  452.                         }
  453.                         else if(0 == strncmp(para_list[0],"ZERO",sizeof("ZERO")))                //角度归零,待完善
  454.                         {
  455.                                 if(0 == strncmp(para_list[1],"VERTICAL",sizeof("VERTICAL")))
  456.                                         ver_hor                        =        Vertical;
  457.                                 else if(0 == strncmp(para_list[1],"HORIZONT",sizeof("HORIZONT")))
  458.                                         ver_hor                        =        Horizontal;
  459.                                 else
  460.                                         ver_hor                        = Unknow;
  461.                                
  462.                                 if(Unknow != ver_hor)
  463.                                 {
  464.                                         ZeroMotor(ver_hor);
  465.                                         sendbytes        =        sprintf(CmdRespondStr,ACKSTR);
  466.                                 }
  467.                         }
  468.                         else if(0 == strncmp(para_list[0],"HELP",sizeof("HELP")))                //命令使用信息查询
  469.                         {
  470.                                 usart_sendaddr                =HelpMsg[0];
  471.                                 sendbytes                                        =strlen(HelpMsg[0]);
  472.                         }
  473.                         else if(0 == strncmp(para_list[0],"RESET",sizeof("RESET")))                        //系统复位命令
  474.                         {
  475.                                 //复位前,停止电机转动
  476.                                 MOTOR_STOP_HOR;
  477.                                 MOTOR_STOP_VER;
  478.                                 Sys_Soft_Reset();
  479.                         }
  480.                         else
  481.                                 sendbytes        =        sprintf(CmdRespondStr,NACKSTR);
  482.                                
  483.                         for(tmp=0;tmp<CMDPARANUM;tmp++)
  484.                                 memset(para_list[tmp],0,PARASTRLEN);
  485.                 }
  486.                
  487.                 //DMA空闲时,将数据发送出去
  488.                 if((0 == DMA_GetCurrDataCounter(PC_DMA_CHANNEL))&&(sendbytes > 0))       
  489.                 {
  490.                         UsartDMASend(usart_sendaddr,sendbytes);
  491.                         usart_sendaddr                                                =        CmdRespondStr;
  492.                         sendbytes                =0;
  493.                 }
  494.                 //将方位更新到角度,并判断是否超过设定范围
  495.                 Ver_Info.angle_now        =        (float)Ver_Info.dir_now*Hor_Info.step_resolution;
  496.                 Hor_Info.angle_now        =        (float)Hor_Info.dir_now*Hor_Info.step_resolution;      //当前的角度为实际脉冲数计算得到,angle_now=360*dir_now/(2*200*140)      (细分比率*200*次轮比)
  497.                 UPDATE_ANGEL_HOR;
  498.                 UPDATE_DIRNOW_HOR;
  499.                
  500.                 UPDATE_ANGEL_VER;
  501.                 UPDATE_DIRNOW_VER;
  502.                 WG_Feed();
  503.                
  504.         }
  505. }
  506. /****************************************************************************
  507. * 名    称: str2float()
  508. * 功    能:收到的角度是否合法
  509. * 入口参数:
  510. * 出口参数:
  511. * 说    明:
  512. * 调用方法:
  513. * 修改日志:
  514. ****************************************************************************/
  515. uint8_t str2float(const char *str,float *result)
  516. {
  517.         uint8_t        charcnt;
  518.         volatile uint8_t        isok=1;
  519.         uint8_t        paracnt                        =0;
  520.         uint8_t        paracharcnt        =0;
  521.         uint8_t        point_flag        =0;
  522.         uint8_t        num_flag                =0;
  523.         uint8_t        sym_flag                =0;
  524.         uint8_t        sym_loc                        =0;
  525.         for(charcnt=0;charcnt<PARASTRLEN;charcnt++)
  526.         {
  527.                 if(('-' == *(str+charcnt))|('-' == *(str+charcnt)))
  528.                 {
  529.                         sym_flag        = charcnt+1;
  530.                         if(sym_flag>1)
  531.                                 break;        //符号位超过一个
  532.                 }
  533. ……………………

  534. …………限于本文篇幅 余下代码请从51黑下载附件…………
复制代码

所有资料51hei提供下载:
StepMotor_LV4(134口径振镜源代码).7z (180.66 KB, 下载次数: 31)


分享到:  QQ好友和群QQ好友和群 QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友
收藏收藏2 分享淘帖 顶 踩
回复

使用道具 举报

沙发
ID:530349 发表于 2019-5-8 13:35 | 只看该作者
不错不错哟
回复

使用道具 举报

板凳
ID:1 发表于 2019-5-8 20:51 | 只看该作者
本帖需要重新编辑补全电路原理图,源码,详细说明与图片即可获得100+黑币(帖子下方有编辑按钮)
回复

使用道具 举报

地板
ID:777142 发表于 2020-12-31 10:06 | 只看该作者
目前在了解这个,感谢楼主的分享
回复

使用道具 举报

5#
ID:597820 发表于 2021-1-1 20:17 | 只看该作者
不错不错,感谢楼主的分享!
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

手机版|小黑屋|51黑电子论坛 |51黑电子论坛6群 QQ 管理员QQ:125739409;技术交流QQ群281945664

Powered by 单片机教程网

快速回复 返回顶部 返回列表