久久久久久久999_99精品久久精品一区二区爱城_成人欧美一区二区三区在线播放_国产精品日本一区二区不卡视频_国产午夜视频_欧美精品在线观看免费

 找回密碼
 立即注冊

QQ登錄

只需一步,快速開始

搜索
查看: 2804|回復: 1
打印 上一主題 下一主題
收起左側

基于51單片機的機械臂抓取 16路舵機控制程序

[復制鏈接]
跳轉到指定樓層
樓主
ID:721819 發表于 2020-5-2 21:01 | 只看該作者 回帖獎勵 |倒序瀏覽 |閱讀模式
基于51單片機的機械臂抓取

單片機源程序如下:
  1. /***************************************************************************************************************
  2. 文 件 名:main.c  
  3. 功能描述:
  4. 備    注: 16路舵機控制
  5. ****************************************************************************************************************/
  6. #include "STC15Fxxxx.H"  //STC15系列單片機
  7. #include <intrins.h>
  8. #include "UART.H"
  9. #include "timer.h"
  10. #include "util.h"
  11. #include "ps2/ps2.h"
  12. #include "flash/flash.h"//頭文件
  13. #include <string.h>
  14. #include <stdio.h>
  15. #include "LED/led.h"
  16. #include <string.h>
  17. #include <stdlib.h>
  18. #include "adc/adc.h"
  19. extern uint8 flag_p;
  20. extern bit flag_RecFul;
  21. //extern uchar KEY[9];
  22. uint16 pos[7][MOTOR_NUM]={ {1000,1500,1500,1500,1500,1500,1500,1500,1500},
  23.                                          {1000,1500,1500,1500,1500,1500,1500,1500,1500},
  24.                                          {1000,1500,1500,1500,1500,1500,1500,1500,1500},
  25.                                          {1000,500,500,500,500,500,500,500,500},
  26.                                          {1000,1500,1500,1500,1500,1500,1500,1500,1500},
  27.                                          {1000,500,500,500,500,500,500,500,500},
  28.                                          {1000,1500,1500,1500,1500,1500,1500,1500,1500}
  29. };         //位置
  30. uint16 pwm[MOTOR_NUM]=    {1500,1500,1500,1500,1500,1500,1500,1500,1500};
  31. uint16 UartRec[MOTOR_NUM]={1500,1500,1500,1500,1500,1500,1500,1500,1500};
  32. uint8 redata[257] = {0};    // 定義接收數據變量數組
  33. uint8 line=0;                                        //緩存存入口與出口之間的距離,即當前緩存中有多少個沒有執行的數據
  34. uint8 point_now=0;                                //與point_aim一起標記緩存出口位置,即取數位置
  35. uint8 point_aim=1;
  36. uint8 point_in=2;                                //標記緩存入口位置,即上一行數據存放位置
  37. bit flag_connect = 0;
  38. bit flag_stop=1;                                //表示一行執行結束
  39. uint8 flag_vpwm=0;                                //表示到達了該更新pwm[]的時間
  40. bit flag_in=1;                                         //表示緩存中有空閑空間
  41. bit flag_out=0;                                        //表示緩存中有可執行數據的標志位
  42. bit flag_run_ready=0;                        //表示有要要放入緩存的EErom數據
  43. uint16 n=1000;                                        //用來計算需要建立多少個中間數據
  44. uint16 m=1;                                                //用來累計已經執行了多少中間數據
  45. double dp;
  46. double dp0[MOTOR_NUM] = {0};                                        //插補增量
  47. bit flag_download = 0;//判斷是否下載
  48. bit flag_read = 0;// 讀取flash內容,發送上位機
  49. bit flag_connect_run = 0;//連接上位機的執行flash保存的命令
  50. bit flag_stop_download = 0;//停止下載
  51. bit flag_online_run = 0;
  52. bit flag_uart2_rev = 0;
  53. bit flag_uart2_rev_time_out = 0;
  54. bit flag_ps2_rev = 0;
  55. bit flag_read_adc = 0;
  56. unsigned long send_mode = 0;//當前串口接收到命令狀態存儲
  57. MotorData motor_data;//舵機總信息
  58. MotorOneCmd motor_one_cmd;//單個命令
  59. CurrentItem cur_item;
  60. uint16 tuoji_count = 0;//脫機執行次數
  61. bit flag_scan_ps2 = 0;
  62. uint8 error = 0;
  63. uchar file_list[MAX_SUPPORT_FILE_SAVE] = {0};
  64. int file_list_count = 0;
  65. int file_last_num = 0;
  66. char ps2_buf[120] = {0};
  67. char uart2_buf[50] = {0};
  68. uint cur_count = 0;
  69. uchar ad_value = 0;
  70. uchar beep_mode = 1;
  71. uchar key_bak;
  72. uchar ps2_key;
  73. uchar ps2_mode=0;

  74. void updata_file_list()
  75. {
  76.         uchar i = 0;
  77.         uchar j = 0;
  78.         file_last_num = -1;
  79.         ReadMoterInfor();
  80.         for (i = 0; i < motor_data.filecount; i++)
  81.         {
  82.                 if (motor_data.file_flag[i] == 1)
  83.                 {
  84.                         file_list[j] = i;
  85.                         j++;
  86.                         file_last_num = i;
  87.                 }
  88.         }
  89.         file_list_count = j;
  90. }
  91. void InitMotor()
  92. {
  93.         ReadMoterInfor();//讀取舵機控制信息
  94.         updata_file_list();
  95.         memset(&cur_item,0,sizeof(cur_item));
  96.         beep_mode = motor_data.beep_mode;
  97. }

  98.         
  99. void main(void)
  100. {
  101.         uint temp = 0;         
  102.         P0M1=0x00;                                   //設置P0口為強推挽輸出模式
  103.         P0M0=0xFF;
  104.         
  105.         P1M1|=0x80;
  106.         P1M0|=0xc8;
  107.         
  108.         P5M1|=0x00;
  109.   P5M0|=0x2d;
  110.         
  111.         P4M0|=0x20;
  112.         P4M1|=0x00;
  113.         
  114.         Timer_init();          //定時器初始化
  115.         Timer0(31);                  //定時任意值,啟動定時器,進入定時器循環
  116. #if MOTOR_NUM > 9
  117.         Timer1(30);
  118. #endif
  119.         SpiFlashInit();//初始化flash
  120.         while((temp = SpiFlashReadID())!=W25Q64)LED_ALL_ON();//判斷flash有沒有接錯
  121.         LED_ALL_OFF();
  122.         InitMotor();
  123.         UART1_Init();          //串口1初始化
  124.         UART2_Init();  //串口2初始化
  125. #if PS_SUPPORT
  126.         Timer3_init();
  127. #endif        
  128.         InitADC(7);
  129.         BEEP_On_Or_OFF();
  130.         while(1)
  131.         {
  132.                 if(flag_vpwm==1)                  
  133.                 {        
  134.                         vpwm();                                        //更新pwm[]數組
  135.                         flag_vpwm=0;               
  136.                 }
  137.                 if( flag_RecFul==1)                   //串口接受完一條指令
  138.                 {
  139.                         DealRec();                                 //處理串口緩存中的數據
  140.                         flag_RecFul=0;
  141.                 }
  142.                 GetOneMotorCMD();//獲取一個命令
  143.                 SendUartState();//發送狀態信息
  144. #if PS_SUPPORT
  145.                 scan_ps2();
  146. #endif
  147.                 LED_State();
  148.                 Check_Power();
  149.           cur_count++;
  150.         }
  151. }
  152. void Check_Power()
  153. {
  154.         if ((cur_count % 800) == 0)
  155.         {
  156.                 StartADC(7);
  157.                 //cur_count=0;
  158.         }
  159.         if (flag_read_adc)
  160.         {
  161.                 flag_read_adc = 0;
  162.                 //UART_Put_Inf("adc:",ad_value);
  163.                 if (ad_value > 46)//根據電壓粗略估計
  164.                 {
  165.                         //UART_Put_Inf("adc:",ad_value);
  166.                   BEEP_OFF();
  167.                 //        LED2_OFF();
  168.                 }
  169.                 else
  170.                 {
  171.                                 //UART_Put_Inf("adc1111:",ad_value);
  172.                                 BEEP=~BEEP;
  173.                                 LED1=0;
  174.                                 LED2=0;
  175.                                 LED3=0;
  176.                 }
  177.                
  178.         }
  179. }
  180. void LED_State()
  181. {
  182.         uint error_count =  0;
  183.         if (error != 0)
  184.         {
  185.                 if (error & ERROR_FLASH_FULL)
  186.                 {
  187.                         error_count = 100;
  188.                 }
  189.                 if (error & ERROR_FLASH_FILE_FULL)
  190.                 {
  191.                         error_count += 100;
  192.                 }
  193.                 if (error & ERROR_FLASH_WRITE)
  194.                 {
  195.                         error_count += 100;
  196.                 }
  197.                 if (error & ERROR_FLASH_WRITE1)
  198.                 {
  199.                         error_count += 100;
  200.                 }
  201.                 if ((cur_count % error_count) == 0)//判斷flash是否正確
  202.                 {
  203.                         LED1_ON_OR_OFF();
  204.                 }
  205.         }
  206.         
  207.         if (flag_ps2_rev)
  208.         {
  209.                 LED1_ON();
  210.                 if ((cur_count % 100) == 0)
  211.                 {
  212.                         flag_ps2_rev = 0;
  213.                         LED1_OFF();
  214.                 }
  215.         }
  216.         if (flag_connect)
  217.         {
  218.                 LED3_ON();
  219.         }
  220.         else
  221.         {
  222.                 LED3_OFF();
  223.         }
  224. }
  225. void scan_ps2()
  226. {
  227.         int  kind = 0;
  228.         char *p = NULL;
  229.         char buf[15] = {0};
  230.         char i = 0;
  231.         if (flag_scan_ps2)//
  232.         {
  233.                 flag_scan_ps2 = 0;
  234.                 ps2_key=PS2_DataKey();
  235.                 ps2_mode=PS2_RedLight();
  236.         //UART_Put_Inf("mode",ps2_mode);        
  237.                 if(ps2_mode==0)
  238.                 {
  239.                         if(key_bak == ps2_key)return;
  240.                         key_bak=ps2_key;
  241.                         BEEP=~BEEP;
  242.                         switch(ps2_key)
  243.                         {
  244.                                 case PSB_PAD_UP:kind = 1;break;
  245.                                 case PSB_PAD_DOWN:kind = 2;break;
  246.                                 case PSB_PAD_LEFT:kind = 3;break;
  247.                                 case PSB_PAD_RIGHT:kind = 4;break;

  248.                                 case PSB_TRIANGLE:kind = 7;break;
  249.                                 case PSB_CROSS:kind = 8;break;
  250.                                 case PSB_PINK:kind = 9;break;
  251.                                 case PSB_CIRCLE:kind = 10;break;

  252.                                 case PSB_L1:kind = 6;break;
  253.                                 case PSB_L2:kind = 5;break;
  254.                                 case PSB_R1:kind = 12;break;
  255.                                 case PSB_R2:kind = 11;break;
  256.                                 default:break;
  257.                         }
  258.                                 if (kind != 0)
  259.                                 {               
  260.                                         flag_ps2_rev = 1;
  261.                                         flag_connect = 1;
  262.                                         SpiFlashRead(ps2_buf,(PS2_FLASH_ADDR)<<WRITE_BIT_DEPTH,sizeof(ps2_buf));
  263.                                         sprintf(buf,"%dK",kind);
  264.                                         //UART1_SendStr(buf);
  265.                                         p = strstr(ps2_buf,buf);
  266.                                         if (p != NULL)
  267.                                         {
  268.                                                 p = p + strlen(buf);
  269.                                                 while(i < 14 && *p != 0)
  270.                                                 {
  271.                                                         buf[i] = *p++;
  272.                                                         i++;
  273.                                                         if (*p == '#')
  274.                                                                 break;
  275.                                                 }
  276.                                                 if (i < 12)
  277.                                                 {
  278.                                                         buf[i] = '\r';
  279.                                                         buf[i+1] = '\n';
  280.                                                         memcpy(redata,buf,sizeof(buf));
  281.                                                         flag_RecFul = 1;
  282.                                                 }
  283.                                                 UART1_SendStr(redata);
  284.                                         }
  285.                                 }
  286.                         }
  287.                 else if(ps2_mode==1)//綠燈模式
  288.                 {
  289.                         switch(ps2_key)
  290.                                 {
  291.                                         case PSB_PAD_UP:pwm[1]+=10;if(pwm[1]>=2300) pwm[1]=2300;break;
  292.                                         case PSB_PAD_DOWN:pwm[1]-=10;if(pwm[1]<=700) pwm[1]=700;break;
  293.                                         case PSB_PAD_LEFT:pwm[2]+=10;if(pwm[2]>=2300) pwm[2]=2300;break;
  294.                                         case PSB_PAD_RIGHT:pwm[2]-=10;if(pwm[2]<=700) pwm[2]=700;break;
  295.                
  296.                                         case PSB_TRIANGLE:pwm[3]+=10;if(pwm[3]>=2300) pwm[3]=2300;break;
  297.                                         case PSB_CROSS:pwm[3]-=10;if(pwm[3]<=700) pwm[3]=700;break;
  298.                                         case PSB_PINK:pwm[4]+=10;if(pwm[4]>=2300) pwm[4]=2300;break;
  299.                                         case PSB_CIRCLE:pwm[4]-=10;if(pwm[4]<=700) pwm[4]=700;break;

  300.                                         case PSB_L1:pwm[5]+=10;if(pwm[5]>=2300) pwm[5]=2300;break;
  301.                                         case PSB_L2:pwm[5]-=10;if(pwm[5]<=700)  pwm[5]=700;break;
  302.                                         case PSB_R1:pwm[6]+=10;if(pwm[6]>=2300) pwm[6]=2300;break;
  303.                                         case PSB_R2:pwm[6]-=10;if(pwm[6]<=700)  pwm[6]=700;break;
  304.                                         default:break;
  305.                                 }
  306.                 }
  307.         }
  308. }
  309.         
  310. void ReadMoterInfor()
  311. {
  312.         memset(&motor_data,0,sizeof(motor_data));//清 0
  313.         SpiFlashRead((char *)&motor_data,(CMD_FLASH_ADDR)<<WRITE_BIT_DEPTH,sizeof(motor_data));//讀取信息
  314.         if (motor_data.CRC1 != 0x12345678 || motor_data.sum < 0 || motor_data.duoji_count  > MOTOR_NUM)//判斷信息存儲是否有錯
  315.         {
  316.                 memset(&motor_data,0,sizeof(motor_data));
  317.                 //memset(&cur_item,0,sizeof(cur_item));
  318.         }
  319.         else//正常信息
  320.         {
  321.                 //UART1_SendOneChar(motor_data.sum + 0x30);
  322.                 //cur_item.tuoji_count = motor_one_cmd.tuoji_count;//脫機運行次數
  323.                 //cur_item.cur_num = 0;//清 0
  324.         }
  325.                
  326. }
  327. void ReadOneCmdInfor(unsigned int addr)
  328. {
  329.         memset(&motor_one_cmd,0,sizeof(motor_one_cmd));//清 0
  330.         SpiFlashRead((char *)&motor_one_cmd,((((unsigned long)addr)<<4) + FILE_FLASH_ADDR)<<WRITE_BIT_DEPTH,sizeof(motor_one_cmd));//讀取信息
  331.         if (motor_one_cmd.start >= motor_one_cmd.end || motor_one_cmd.cur_file_num != (addr) || motor_data.file_flag[motor_one_cmd.cur_file_num] == 0)//判斷信息存儲是否有錯
  332.         {
  333.                 memset(&motor_one_cmd,0,sizeof(motor_one_cmd));
  334.         }
  335.         else//正常信息,以后留著驗證用的
  336.         {
  337.                
  338.                
  339.                 cur_item.tuoji_count = motor_one_cmd.tuoji_count;//脫機運行次數
  340.                 cur_item.cur_num = motor_one_cmd.start;
  341.                 //UART1_SendOneChar(motor_one_cmd.tuoji_count + 0x30);
  342.                 //cur_item.cur_num = 0;//清 0
  343.         }
  344.                
  345. }

  346. void WriteMoterInfor()
  347. {
  348.         uchar temp = 0;
  349.         motor_data.CRC1 = 0x12345678;//校驗碼
  350.         motor_data.duoji_count = MOTOR_NUM-1;
  351.         temp = motor_data.filecount;
  352.         SpiFlashEraseSector(CMD_FLASH_ADDR >> 4);//擦除以前存儲的信息
  353.         SpiFlashWrite((char *)&motor_data,CMD_FLASH_ADDR<<WRITE_BIT_DEPTH,sizeof(motor_data)); //寫入flash
  354.         ReadMoterInfor();
  355.         if (temp != motor_data.filecount)
  356.         {
  357.                 error |= ERROR_FLASH_WRITE;
  358.         }
  359.         else
  360.         {
  361.                 error &= ~ERROR_FLASH_WRITE;
  362.         }
  363. }
  364. void WriteOneCmdInfor(unsigned int addr)
  365. {
  366.         uchar temp = 0;
  367.         temp = motor_one_cmd.end;
  368.         if (((((unsigned long)addr)<<4) + FILE_FLASH_ADDR) % 16 == 0)
  369.                 SpiFlashEraseSector(((((unsigned long)addr)<<4) + FILE_FLASH_ADDR) >> 4);//擦除以前存儲的信息
  370.         SpiFlashWrite((char *)&motor_one_cmd,((((unsigned long)addr)<<4) + FILE_FLASH_ADDR)<<WRITE_BIT_DEPTH,sizeof(motor_one_cmd)); //寫入flash
  371.         ReadOneCmdInfor(addr);
  372.         if (temp !=  motor_one_cmd.end)
  373.         {
  374.                 error |= ERROR_FLASH_WRITE1;
  375.         }
  376.         else
  377.         {
  378.                 error &= ~ERROR_FLASH_WRITE1;
  379.         }
  380. }
  381.         
  382. void GetOneMotorCMD()
  383. {
  384. #if DEBUG
  385.         uchar buf[20] = {0};
  386. #endif
  387.         if (flag_stop_download)//接收到了上位機的停止下載的命令
  388.         {
  389.                 flag_download = 0;//清楚下載狀態標志位
  390.                 flag_stop_download = 0;
  391.                 flag_read = 0;
  392.                 if (motor_data.filecount < MAX_SUPPORT_FILE_SAVE)
  393.                 {
  394.                         updata_file_list();
  395.                         motor_data.sum = motor_one_cmd.end;
  396.                         motor_data.file_flag[motor_data.filecount] = 1;
  397.                         motor_one_cmd.cur_file_num = file_last_num + 1;
  398.                         motor_data.filecount = motor_one_cmd.cur_file_num + 1;
  399.                         error &= ~MAX_SUPPORT_FILE_SAVE;
  400. #if DEBUG
  401.                         sprintf(buf,"%d %d\r\n",(uint)motor_data.filecount,(uint)motor_data.file_flag[motor_data.filecount-1]);
  402.                         UART1_SendStr(buf);
  403. #endif
  404.                         WriteMoterInfor();
  405.                         WriteOneCmdInfor(motor_one_cmd.cur_file_num);
  406.                         updata_file_list();
  407. #if DEBUG
  408.                         
  409.                         sprintf(buf,"%d %d\r\n",(uint)motor_data.filecount,(uint)motor_data.file_flag[motor_data.filecount-1]);
  410.                         UART1_SendStr(buf);
  411. #endif
  412.                 }
  413.                 else
  414.                 {
  415.                         error |= MAX_SUPPORT_FILE_SAVE;
  416.                 }
  417.                 if        (!(error &(MAX_SUPPORT_FILE_SAVE | ERROR_FLASH_FULL)))
  418.                         send_mode |= SEND_DOWN_OK;//狀態位置為
  419.         }
  420.         if (flag_connect)//如果當前跟上位機聯機狀態
  421.         {
  422.                 if (flag_read)//如果上位機讀取flash內存儲的信息
  423.                 {               
  424.                         if (cur_item.cur_num < motor_one_cmd.end)//判斷是否超過之前存儲的數
  425.                         {
  426.                                 if ((send_mode & SEND_SEND_FILE))//開始接收到讀取命令需要先發送個start
  427.                                 {
  428.                                         UART1_SendStr("#Start\r\n");
  429.                                         send_mode &= ~SEND_READ_FILE;
  430.                                 }
  431.                                 memset(redata,0,WRITE_SIZE);//清 0
  432.                                 SpiFlashRead(redata,(((long)cur_item.cur_num)<<WRITE_BIT_DEPTH),WRITE_SIZE);//讀取信息
  433. #if DEBUG
  434.                                 sprintf(buf,"%d\r\n",cur_item.cur_num);
  435.                                 UART1_SendStr(buf);
  436. #endif
  437.                                 UART1_SendStr(redata);//發送
  438.                                 cur_item.cur_num++;
  439.                         }
  440.                         else//否則
  441.                         {
  442.                                 if (cur_item.cur_num > 0)
  443.                                         UART1_SendStr("#End\r\n");//發送結束字符串
  444.                                   flag_read = 0;
  445.                         }
  446.                         send_mode = 0;//請 0
  447.                 }        
  448.                 if (flag_online_run)
  449.                 {
  450.                         if ((send_mode & SEND_CC) != 0  || cur_item.cur_num == motor_one_cmd.start)//如果當前需要更新舵機命令
  451.                         {
  452.                                 if (cur_item.tuoji_count > 0)//脫機次數沒結束
  453.                                 {
  454.                                         if (cur_item.cur_num < motor_one_cmd.end)//判斷是否讀取結束
  455.                                         {
  456.                                                 SpiFlashRead(redata,((long)cur_item.cur_num)<<WRITE_BIT_DEPTH,WRITE_SIZE);//讀取命令
  457.                                                 flag_RecFul = 1;//標志位為1,
  458.                                                 cur_item.cur_num++;//
  459.                                         }
  460.                                         else//執行玩一遍
  461.                                         {
  462.                                                 cur_item.cur_num = motor_one_cmd.start;
  463.                                                 cur_item.tuoji_count--;//減一
  464.                                         }
  465.                                 }
  466.                                 else//執行完成
  467.                                 {
  468.                                         flag_online_run = 0;
  469.                                         if (flag_connect_run)//如果上位機選擇執行的功能,需要發送 AGF作為結束
  470.                                         {
  471.                                                 UART1_SendStr("#AGF\r\n");
  472.                                                 flag_connect_run = 0;
  473.                                         }
  474.                                 }
  475.                                 //讀取數據
  476.                         }
  477.                 }
  478.         }
  479.         else//脫機
  480.         {
  481.                 if (file_list_count < 0)
  482.                 {
  483.                         return;
  484.                 }
  485.                 if (cur_item.tuoji_count > 0)
  486.                 {
  487.                         if ((send_mode & SEND_CC) != 0  || cur_item.cur_num == motor_one_cmd.start)//如果當前需要更新舵機命令
  488.                         {
  489.                                 if (cur_item.cur_num < motor_one_cmd.end)//判斷是否讀取結束
  490.                                 {
  491.                                         SpiFlashRead(redata,((long)cur_item.cur_num)<<WRITE_BIT_DEPTH,WRITE_SIZE);//讀取命令
  492.                                         flag_RecFul = 1;//標志位為1,
  493.                                         cur_item.cur_num++;//
  494.                                 }
  495.                                 else//執行玩一遍
  496.                                 {
  497.                                         cur_item.cur_num = motor_one_cmd.start;
  498.                                         cur_item.tuoji_count--;//減一
  499.                                 }
  500.                                 //讀取數據
  501.                         }
  502.                 }
  503.                 else
  504.                 {
  505.                         ReadOneCmdInfor(file_list[cur_item.file_num]);
  506.                         file_list_count--;
  507.                         cur_item.file_num++;
  508.                 }
  509.         }
  510.                
  511.         
  512. }
  513.         
  514. void SendUartState()
  515. {
  516.         uchar buf[40] = {0};
  517.         uchar read_motor_num = 0;
  518.         uint i = 0;
  519.         static int count = 0;
  520.         if (send_mode)//如果有狀態需要發送
  521.         {
  522.                 if (send_mode & SEND_A) //發送A
  523.                 {
  524.                         UART1_SendOneChar('A');
  525.                         send_mode &= ~SEND_A;//清狀態
  526.                 }
  527.                 if (send_mode & SEND_CC) //發送CC
  528.                 {
  529.                         UART1_SendStr("#CC\r\n");
  530.                         send_mode &= ~SEND_CC;
  531.                 }
  532.                 if (send_mode & SEND_DOWN_OK)//發送下載ok的狀態字符串
  533.                 {
  534.                         sprintf(buf,"#Down+OK+%d\r\n",(int)motor_data.filecount-1);
  535.                         UART1_SendStr(buf);
  536.                         send_mode &= ~SEND_DOWN_OK;
  537. #if DEBUG
  538.                         sprintf(buf,"%d\r\n",(uint)motor_data.filecount);
  539.                         UART1_SendStr(buf);
  540. #endif
  541.                 }
  542.                 if (send_mode & SEND_START_OK)//發送連接時候的字符串
  543.                 {
  544.                         UART1_SendStr("#Veri+UART+OK+20160906+176\r\n");
  545.                         send_mode &= ~SEND_START_OK;
  546.                 }
  547.                 if (send_mode & SEND_READ_FILE)//發送讀取文件的時候字符串
  548.                 {
  549.                         if (motor_data.filecount > 0)//如果保存的有舵機命令
  550.                         {
  551.                                 //#Name:1.txt--Size:48--Name:2.txt--Size:190--Name:desktop.ini--Size:531--
  552.                                 UART1_SendStr("#");//發送
  553.                                 for (i = 0; i < motor_data.filecount;i++)
  554.                                 {
  555.                                         if (motor_data.file_flag[i] == 1)
  556.                                         {
  557.                                                 ReadOneCmdInfor(i);
  558.                                                 if (motor_one_cmd.end - motor_one_cmd.start <= 0)
  559.                                                 {
  560.                                                         motor_data.file_flag[i] = 0;
  561.                                                         WriteMoterInfor();
  562. #if DEBUG
  563.                                                         sprintf(buf,"E=%d S=%d",motor_one_cmd.end, motor_one_cmd.start);
  564. #endif
  565.                                                 }
  566.                                                 else
  567.                                                 {
  568.                                                         sprintf(buf,"Name:%d.txt--Size:%d--",i,motor_one_cmd.end - motor_one_cmd.start);//獲取命令個數
  569.                                                         UART1_SendStr(buf);//發送
  570.                                                 }
  571.                                         }

  572.                                 }
  573.                                 UART1_SendStr("\r\n");                                
  574.                         }
  575.                         else
  576.                         {
  577.                                 sprintf(buf,"#\r\n",motor_data.sum);
  578.                                 UART1_SendStr(buf);
  579.                         }
  580.                         send_mode &= ~SEND_READ_FILE;
  581.                 }
  582.                 if (send_mode & SEND_SET_OFFLINE_OK)//設置脫機運行次數
  583.                 {
  584.                         WriteOneCmdInfor(motor_one_cmd.cur_file_num);//保存
  585.                         UART1_SendStr("#Enable+OK...\r\n");
  586.                         send_mode &= ~SEND_SET_OFFLINE_OK;
  587.                 }
  588.                 if (send_mode & SEND_SET_DISABLEOFFLINE_OK)//禁止脫機運行
  589.                 {
  590.                         for (i = 0; i < motor_data.filecount;i++)
  591.                         {
  592.                                 if (motor_data.file_flag[i] == 1)
  593.                                 {
  594.                                         ReadOneCmdInfor(i);
  595.                                         motor_one_cmd.tuoji_count = 0;
  596.                                         WriteOneCmdInfor(i);
  597.                                 }

  598.                         }
  599.                         WriteMoterInfor();
  600.                         UART1_SendStr("#Disable+OK...\r\n");
  601.                         send_mode &= ~SEND_SET_DISABLEOFFLINE_OK;
  602.                 }
  603.                 if (send_mode & SEND_SET_ONLINE_OK)//發送聯機運行狀態
  604.                 {
  605.                         UART1_SendStr("#OK\r\n");
  606.                         sprintf(buf,"#%dGC%d\r\n",cur_item.file_num,tuoji_count);
  607.                         UART1_SendStr(buf);
  608.                         UART1_SendStr("#LP=0\r\n");
  609.                         send_mode &= ~SEND_SET_ONLINE_OK;
  610.                         flag_connect_run = 1;
  611.                 }
  612.                 if (send_mode & SEND_SET_DELETE_ONE_FILE_OK)//發送刪除文件命令
  613.                 {
  614.                         //cur_item.tuoji_count = 0;
  615.                         if (cur_item.delete_num < motor_data.filecount)
  616.                         {
  617.                                 motor_data.file_flag[cur_item.delete_num] = 0;
  618.                                 WriteMoterInfor();
  619.                                 updata_file_list();
  620.                                 if (cur_item.delete_num  == motor_data.filecount-1)
  621.                                 {
  622.                                         motor_data.filecount = file_last_num + 1;
  623.                                         if (file_last_num == -1)
  624.                                         {
  625.                                                 motor_data.sum = 0;
  626.                                         }
  627.                                         else
  628.                                         {
  629.                                                 ReadOneCmdInfor(file_last_num);
  630.                                                 motor_data.sum = motor_one_cmd.end;
  631.                                                 motor_data.sum = (((long int)(motor_data.sum) >>4)<<4) + (1<<4);
  632.                                         }
  633.                                         WriteMoterInfor();
  634.                                 }
  635.                                 updata_file_list();               
  636.                                 UART1_SendStr("#FDel+OK\r\n");
  637.                         }
  638.                         send_mode &= ~SEND_SET_DELETE_ONE_FILE_OK;
  639.                         
  640.                 }
  641.                 if (send_mode & SEND_SET_DELETE_ALL_FILE_OK)//發送擦除所有文件的命令
  642.                 {
  643.                         UART1_SendStr("#Format+Start\r\n");
  644.                         SpiFlashEraseChip();
  645.                         cur_item.tuoji_count = 0;
  646.                         motor_data.sum = 0;
  647.                         motor_data.filecount = 0;
  648.                         memset(motor_data.file_flag,0,sizeof(motor_data.file_flag));
  649.                         WriteMoterInfor();
  650.                         UART1_SendStr("#Format+OK\r\n");
  651.                         send_mode &= ~SEND_SET_DELETE_ALL_FILE_OK;
  652.                         updata_file_list();
  653.                 }
  654.                 if (send_mode & SEND_SET_PS2_OK)
  655.                 {
  656.                         UART1_SendStr("#PS2+OK...\r\n");
  657.                         send_mode &= ~SEND_SET_PS2_OK;
  658.                 }
  659.                
  660. #define MATHION_HAND_NUM 20
  661.                 if (send_mode & SEND_SET_READ_UART_MOTOR_ANGLE)
  662.                 {
  663.                         if (cur_item.read_num < MATHION_HAND_NUM)
  664.                         {
  665.                                 if (flag_uart2_rev)
  666.                                 {
  667.                                         read_motor_num = atoi(uart2_buf + 1);
  668.                                         if (read_motor_num == cur_item.read_num)
  669.                                         {
  670.                                                 i = atoi(uart2_buf+5);
  671.                                                 sprintf(uart2_buf,"#%dP%d",(int)read_motor_num,i);
  672.                                                 UART1_SendStr(uart2_buf);
  673.                                                 cur_item.read_num++;
  674.                                                 buf[0] = '#';
  675.                                                 buf[1] = cur_item.read_num / 100 % 10 + 0x30;
  676.                                                 buf[2] = cur_item.read_num / 10 % 10 + 0x30;
  677.                                                 buf[3] = cur_item.read_num % 10 + 0x30;
  678.                                                 buf[4] = 'P';buf[5] = 'R';buf[6] = 'A';buf[7] = 'D';buf[8] = '\r';buf[9] = '\n';
  679.                                                 UART2_SendStr(buf);
  680.                                                 
  681.                                         }
  682.                                         flag_uart2_rev = 0;
  683.                                         count = 0;
  684.                                        
  685.                                 }
  686.                                 else
  687.                                 {
  688.                                         count++;
  689.                                         if (count >= 10)
  690.                                         {
  691.                                                 cur_item.read_num++;
  692.                                                 buf[0] = '#';
  693.                                                 buf[1] = cur_item.read_num / 100 % 10 + 0x30;
  694.                                                 buf[2] = cur_item.read_num / 10 % 10 + 0x30;
  695.                                                 buf[3] = cur_item.read_num % 10 + 0x30;
  696.                                                 buf[4] = 'P';buf[5] = 'R';buf[6] = 'A';buf[7] = 'D';buf[8] = '\r';buf[9] = '\n';
  697.                                                 UART2_SendStr(buf);
  698.                                                 flag_uart2_rev = 0;
  699.                                                 flag_uart2_rev_time_out = 0;
  700.                                                 count = 0;
  701.                                         }
  702.                                 }
  703.                         }
  704.                         else
  705.                         {
  706.                                 send_mode &= ~SEND_SET_READ_UART_MOTOR_ANGLE;
  707.                                 cur_item.read_num= 0;
  708.                                 UART1_SendStr("\r\n");
  709.                         }
  710.                 }
  711.         }
  712.         if (send_mode & SEND_SET_SET_UART_MOTOR_PULK)
  713.         {
  714.                 if (cur_item.pulk_num < MATHION_HAND_NUM)
  715.                 {
  716.                         count++;
  717.                         if (count >= 20)
  718.                         {
  719.                                 sprintf(uart2_buf,"#%dPULK\r\n",(int)cur_item.pulk_num);
  720.                                 UART2_SendStr(uart2_buf);
  721. #if DEBUG
  722.                                 UART1_SendStr(uart2_buf);
  723. #endif
  724.                                 cur_item.pulk_num++;
  725.                                 count = 0;
  726.                         }
  727.                 }
  728.                 else
  729.                 {
  730.                         send_mode &= ~SEND_SET_SET_UART_MOTOR_PULK;
  731.                 }
  732.         }
  733.         if (send_mode & SEND_SET_SET_UART_MOTOR_ANGLE)
  734.         {
  735.                 if (cur_item.pulk_num < MATHION_HAND_NUM)
  736.                 {
  737.                         count++;
  738.                         if (count >= 5)
  739.                         {
  740.                                 sprintf(uart2_buf,"#%dPMOD%d\r\n",(int)cur_item.pulk_num,(int)cur_item.angle_mode);
  741.                                 UART2_SendStr(uart2_buf);
  742. #if DEBUG
  743.                                 UART1_SendStr(uart2_buf);
  744. #endif
  745.                                 cur_item.pulk_num++;
  746.                                 count = 0;
  747.                         }
  748.                 }
  749.                 else
  750.                 {
  751.                         sprintf(buf,"#255PMOD%d+0K...\r\n",(int)cur_item.angle_mode);
  752.                         UART1_SendStr(buf);
  753.                         send_mode &= ~SEND_SET_SET_UART_MOTOR_ANGLE;
  754.                         flag_uart2_rev = 0;
  755.                 }
  756.         }
  757.         if (send_mode & SEND_SET_BEEP_ON)
  758.         {
  759.                 ReadMoterInfor();
  760.                 motor_data.beep_mode = 1;
  761.                 beep_mode = 1;
  762.                 WriteMoterInfor();
  763.                 UART1_SendStr("#FMQENABLE+OK...\r\n");
  764.                 send_mode &= ~SEND_SET_BEEP_ON;
  765.         }
  766.         if (send_mode & SEND_SET_BEEP_OFF)
  767.         {
  768.                 ReadMoterInfor();
  769.                 motor_data.beep_mode = 0;
  770.                 beep_mode = 0;
  771.                 WriteMoterInfor();
  772.                 UART1_SendStr("#FMQDISABLE+OK...\r\n");
  773.                 send_mode &= ~SEND_SET_BEEP_OFF;
  774.         }               
  775. }
  776.         
  777. void change(void)
  778. {        
  779.         uchar s;
  780.         if(line>0)                                   //緩存中有數據
  781.         {  
  782.                 line--;                                   //執行一行
  783.                 if(line<5)                        //緩存允許放入新的數據                        
  784.                         flag_in=1;
  785.                 point_now++;                //取數位置更新
  786.                 point_aim++;
  787.                
  788.                 if(point_aim==7)
  789.                            point_aim=0;
  790.                 if(point_now==7)
  791.                            point_now=0;
  792.                 n=pos[point_aim][0]*4/5;        //計算新的插補次數
  793.                 for(s=1;s<MOTOR_NUM;s++)        //計算新的插補增量
  794.                 {
  795.                  if(pos[point_aim][s]>pos[point_now][s])
  796.                         {
  797.                                    dp=pos[point_aim][s]-pos[point_now][s];
  798.                                    dp0[s]=dp/n;
  799.                         }
  800.                     if(pos[point_aim][s]<=pos[point_now][s])
  801.                         {
  802.                                 dp=pos[point_now][s]-pos[point_aim][s];
  803.                                 dp0[s]=dp/n;
  804.                                 dp0[s]=-dp0[s];
  805.                         }
  806.            }
  807.                 m=0;                                          //m清0
  808.                 flag_stop=0;                          //產生了新的目標位置,停止標志清零
  809.                
  810.         }
  811.         else                                                  //沒有緩存數據,即line==0
  812.         {
  813.                 flag_out=0;                                //緩存中沒有數據
  814.         }
  815. }
  816.         
  817. void vpwm(void)                 
  818. {
  819.         uchar j=0;
  820.         uchar how=0;
  821.   static uchar flag_how;
  822.         static uchar flag_Tover;

  823.         if(flag_stop==1)                                           //一行作業全部完成
  824.         {        
  825.                 if(flag_out==1)                                         //緩沖數組中有數據
  826.                 {
  827.                         change();                                        //更新行
  828.                 }
  829.         }
  830.         else                                                                //不是一行數據全部完成,處于中間插補階段
  831.         {
  832.                 m++;                                                        //用來累加插補過的次數
  833.                 if(m==n)                                                //n是本行作業要插補的總次數
  834.                 {
  835.                   flag_Tover=1;                                //一行數據的執行時間已經完成
  836.                         send_mode |= SEND_CC;
  837.                 }
  838.                 for(j=1;j<MOTOR_NUM;j++)
  839.                 {
  840.                         if(abs(pwm[j]-pos[point_aim][j])<5)
  841.                         {                                                           //檢測靠近終點位置
  842.                            how++;                                           //是,則累加一個
  843.                            pwm[j]=pos[point_aim][j];//并且直接過度到終點位置
  844.                         }        
  845.                         else                                                //不靠近終點,繼續插補
  846.                                 pwm[j]=pos[point_now][j]+m*dp0[j];
  847.                 }         
  848.                 //UART_Put_Inf("pwm",pwm[1]);
  849.                 if(how==MOTOR_NUM-1)
  850.                         flag_how=1;                                                  //16個舵機都到達終點
  851.                 how=0;
  852.                 if((flag_Tover==1)&&(flag_how==1))
  853.                 {                                                                //從插補次數,和脈寬寬度兩方面都到達終點,本作業行完成
  854.                          flag_Tover=0;
  855.                          flag_how=0;
  856.                          flag_stop=1;
  857.                 }
  858.                
  859.          }
  860.         return;

  861. }

  862.                            
復制代碼

以上程序51hei提供下載:
機械臂.zip (700.94 KB, 下載次數: 24)
機械臂1.zip (73.88 KB, 下載次數: 20)


分享到:  QQ好友和群QQ好友和群 QQ空間QQ空間 騰訊微博騰訊微博 騰訊朋友騰訊朋友
收藏收藏4 分享淘帖 頂 踩
回復

使用道具 舉報

沙發
ID:1 發表于 2020-5-2 23:37 | 只看該作者
本帖需要重新編輯補全電路原理圖,源碼,詳細說明與圖片即可獲得100+黑幣(帖子下方有編輯按鈕)
回復

使用道具 舉報

您需要登錄后才可以回帖 登錄 | 立即注冊

本版積分規則

手機版|小黑屋|51黑電子論壇 |51黑電子論壇6群 QQ 管理員QQ:125739409;技術交流QQ群281945664

Powered by 單片機教程網

快速回復 返回頂部 返回列表
主站蜘蛛池模板: h视频在线播放 | 九九热免费看 | 亚洲一区二区免费电影 | 日韩综合在线播放 | 拍真实国产伦偷精品 | 久久久久久久久久一区二区 | 五月婷婷丁香 | 亚洲一区久久 | 人人爽日日躁夜夜躁尤物 | 四虎影院美女 | 国产在线高清 | 99精品视频在线观看免费播放 | 精品欧美一区二区三区精品久久 | 99爱在线 | www.性色 | 久久美女视频 | 亚洲在线日韩 | 亚洲午夜久久久 | 亚洲欧美中文日韩在线v日本 | 亚洲高清在线视频 | 黄色成人av | 亚洲精品欧美 | 国产a级毛片 | 久久久久无码国产精品一区 | 久久久久中文字幕 | 国产精品久久久久久久久图文区 | 韩国精品一区 | 亚洲一区二区三区福利 | 日韩精品免费一区 | 国产精品一区二区不卡 | 精品国产综合 | 久久国产精品久久久久久 | 欧美日韩精品久久久免费观看 | 日韩字幕一区 | 国产免费va | 日韩精品一区二区三区在线观看 | 一区二区三区四区不卡视频 | 精品久久久久国产免费第一页 | 日本成人三级电影 | 欧美日韩不卡合集视频 | 久久国产三级 |