|
這是我畢設(shè)的源碼 16路舵機(jī)控制實現(xiàn)6自由度機(jī)械臂控制,采用的是51單片機(jī)。
0.png (49.74 KB, 下載次數(shù): 174)
下載附件
2017-3-24 20:45 上傳
完整源碼下載:
6自由度手臂源代碼.rar
(589.48 KB, 下載次數(shù): 344)
2017-3-24 16:57 上傳
點(diǎn)擊文件名下載附件
6自由度機(jī)械臂源碼 下載積分: 黑幣 -5
單片機(jī)主程序:
- /***************************************************************************************************************
- 文 件 名:main.c
- 功能描述:
- 備 注: 16路舵機(jī)控制
- ****************************************************************************************************************/
- #include "STC15Fxxxx.H" //STC15系列單片機(jī)
- #include <intrins.h>
- #include "UART.H"
- #include "timer.h"
- #include "util.h"
- #include "ps2/ps2.h"
- #include "flash/flash.h"//頭文件
- #include <string.h>
- #include <stdio.h>
- #include "LED/led.h"
- #include <string.h>
- #include <stdlib.h>
- #include "adc/adc.h"
- extern uint8 flag_p;
- extern bit flag_RecFul;
- //extern uchar KEY[9];
- uint16 pos[7][MOTOR_NUM]={ {1000,1500,1500,1500,1500,1500,1500,1500,1500},
- {1000,1500,1500,1500,1500,1500,1500,1500,1500},
- {1000,1500,1500,1500,1500,1500,1500,1500,1500},
- {1000,500,500,500,500,500,500,500,500},
- {1000,1500,1500,1500,1500,1500,1500,1500,1500},
- {1000,500,500,500,500,500,500,500,500},
- {1000,1500,1500,1500,1500,1500,1500,1500,1500}
- }; //位置
- uint16 pwm[MOTOR_NUM]= {1500,1500,1500,1500,1500,1500,1500,1500,1500};
- uint16 UartRec[MOTOR_NUM]={1500,1500,1500,1500,1500,1500,1500,1500,1500};
- uint8 redata[257] = {0}; // 定義接收數(shù)據(jù)變量數(shù)組
- uint8 line=0; //緩存存入口與出口之間的距離,即當(dāng)前緩存中有多少個沒有執(zhí)行的數(shù)據(jù)
- uint8 point_now=0; //與point_aim一起標(biāo)記緩存出口位置,即取數(shù)位置
- uint8 point_aim=1;
- uint8 point_in=2; //標(biāo)記緩存入口位置,即上一行數(shù)據(jù)存放位置
- bit flag_connect = 0;
- bit flag_stop=1; //表示一行執(zhí)行結(jié)束
- uint8 flag_vpwm=0; //表示到達(dá)了該更新pwm[]的時間
- bit flag_in=1; //表示緩存中有空閑空間
- bit flag_out=0; //表示緩存中有可執(zhí)行數(shù)據(jù)的標(biāo)志位
- bit flag_run_ready=0; //表示有要要放入緩存的EErom數(shù)據(jù)
- uint16 n=1000; //用來計算需要建立多少個中間數(shù)據(jù)
- uint16 m=1; //用來累計已經(jīng)執(zhí)行了多少中間數(shù)據(jù)
- double dp;
- double dp0[MOTOR_NUM] = {0}; //插補(bǔ)增量
- bit flag_download = 0;//判斷是否下載
- bit flag_read = 0;// 讀取flash內(nèi)容,發(fā)送上位機(jī)
- bit flag_connect_run = 0;//連接上位機(jī)的執(zhí)行flash保存的命令
- bit flag_stop_download = 0;//停止下載
- bit flag_online_run = 0;
- bit flag_uart2_rev = 0;
- bit flag_uart2_rev_time_out = 0;
- bit flag_ps2_rev = 0;
- bit flag_read_adc = 0;
- unsigned long send_mode = 0;//當(dāng)前串口接收到命令狀態(tài)存儲
- MotorData motor_data;//舵機(jī)總信息
- MotorOneCmd motor_one_cmd;//單個命令
- CurrentItem cur_item;
- uint16 tuoji_count = 0;//脫機(jī)執(zhí)行次數(shù)
- bit flag_scan_ps2 = 0;
- uint8 error = 0;
- uchar file_list[MAX_SUPPORT_FILE_SAVE] = {0};
- int file_list_count = 0;
- int file_last_num = 0;
- char ps2_buf[120] = {0};
- char uart2_buf[50] = {0};
- uint cur_count = 0;
- uchar ad_value = 0;
- uchar beep_mode = 1;
- uchar key_bak;
- uchar ps2_key;
- uchar ps2_mode=0;
- void updata_file_list()
- {
- uchar i = 0;
- uchar j = 0;
- file_last_num = -1;
- ReadMoterInfor();
- for (i = 0; i < motor_data.filecount; i++)
- {
- if (motor_data.file_flag[i] == 1)
- {
- file_list[j] = i;
- j++;
- file_last_num = i;
- }
- }
- file_list_count = j;
- }
- void InitMotor()
- {
- ReadMoterInfor();//讀取舵機(jī)控制信息
- updata_file_list();
- memset(&cur_item,0,sizeof(cur_item));
- beep_mode = motor_data.beep_mode;
- }
- /***************************************************************************************************************
- 函 數(shù) 名:主函數(shù)
- 功能描述:入口函數(shù) ,進(jìn)行各種初始化配置
- 輸入?yún)?shù):無
- 返 回 值:無
- 備 注:
- ****************************************************************************************************************/
- void main(void)
- {
- uint temp = 0;
- P0M1=0x00; //設(shè)置P0口為強(qiáng)推挽輸出模式
- P0M0=0xFF;
-
- P1M1|=0x80;
- P1M0|=0xc8;
-
- P5M1|=0x00;
- P5M0|=0x2d;
-
- P4M0|=0x20;
- P4M1|=0x00;
-
- Timer_init(); //定時器初始化
- Timer0(31); //定時任意值,啟動定時器,進(jìn)入定時器循環(huán)
- #if MOTOR_NUM > 9
- Timer1(30);
- #endif
- SpiFlashInit();//初始化flash
- while((temp = SpiFlashReadID())!=W25Q64)LED_ALL_ON();//判斷flash有沒有接錯
- LED_ALL_OFF();
- InitMotor();
- UART1_Init(); //串口1初始化
- UART2_Init(); //串口2初始化
- #if PS_SUPPORT
- Timer3_init();
- #endif
- InitADC(7);
- BEEP_On_Or_OFF();
- while(1)
- {
- if(flag_vpwm==1)
- {
- vpwm(); //更新pwm[]數(shù)組
- flag_vpwm=0;
- }
- if( flag_RecFul==1) //串口接受完一條指令
- {
- DealRec(); //處理串口緩存中的數(shù)據(jù)
- flag_RecFul=0;
- }
- GetOneMotorCMD();//獲取一個命令
- SendUartState();//發(fā)送狀態(tài)信息
- #if PS_SUPPORT
- scan_ps2();
- #endif
- LED_State();
- Check_Power();
- cur_count++;
- }
- }
- void Check_Power()
- {
- if ((cur_count % 800) == 0)
- {
- StartADC(7);
- //cur_count=0;
- }
- if (flag_read_adc)
- {
- flag_read_adc = 0;
- //UART_Put_Inf("adc:",ad_value);
- if (ad_value > 46)//根據(jù)電壓粗略估計
- {
- //UART_Put_Inf("adc:",ad_value);
- BEEP_OFF();
- // LED2_OFF();
- }
- else
- {
- //UART_Put_Inf("adc1111:",ad_value);
- BEEP=~BEEP;
- LED1=0;
- LED2=0;
- LED3=0;
- }
-
- }
- }
- void LED_State()
- {
- uint error_count = 0;
- if (error != 0)
- {
- if (error & ERROR_FLASH_FULL)
- {
- error_count = 100;
- }
- if (error & ERROR_FLASH_FILE_FULL)
- {
- error_count += 100;
- }
- if (error & ERROR_FLASH_WRITE)
- {
- error_count += 100;
- }
- if (error & ERROR_FLASH_WRITE1)
- {
- error_count += 100;
- }
- if ((cur_count % error_count) == 0)//判斷flash是否正確
- {
- LED1_ON_OR_OFF();
- }
- }
-
- if (flag_ps2_rev)
- {
- LED1_ON();
- if ((cur_count % 100) == 0)
- {
- flag_ps2_rev = 0;
- LED1_OFF();
- }
- }
- if (flag_connect)
- {
- LED3_ON();
- }
- else
- {
- LED3_OFF();
- }
- }
- void scan_ps2()
- {
- int kind = 0;
- char *p = NULL;
- char buf[15] = {0};
- char i = 0;
- if (flag_scan_ps2)//
- {
- flag_scan_ps2 = 0;
- ps2_key=PS2_DataKey();
- ps2_mode=PS2_RedLight();
- //UART_Put_Inf("mode",ps2_mode);
- if(ps2_mode==0)
- {
- if(key_bak == ps2_key)return;
- key_bak=ps2_key;
- BEEP=~BEEP;
- switch(ps2_key)
- {
- case PSB_PAD_UP:kind = 1;break;
- case PSB_PAD_DOWN:kind = 2;break;
- case PSB_PAD_LEFT:kind = 3;break;
- case PSB_PAD_RIGHT:kind = 4;break;
- case PSB_TRIANGLE:kind = 7;break;
- case PSB_CROSS:kind = 8;break;
- case PSB_PINK:kind = 9;break;
- case PSB_CIRCLE:kind = 10;break;
- case PSB_L1:kind = 6;break;
- case PSB_L2:kind = 5;break;
- case PSB_R1:kind = 12;break;
- case PSB_R2:kind = 11;break;
- default:break;
- }
- if (kind != 0)
- {
- flag_ps2_rev = 1;
- flag_connect = 1;
- SpiFlashRead(ps2_buf,(PS2_FLASH_ADDR)<<WRITE_BIT_DEPTH,sizeof(ps2_buf));
- sprintf(buf,"%dK",kind);
- //UART1_SendStr(buf);
- p = strstr(ps2_buf,buf);
- if (p != NULL)
- {
- p = p + strlen(buf);
- while(i < 14 && *p != 0)
- {
- buf[i] = *p++;
- i++;
- if (*p == '#')
- break;
- }
- if (i < 12)
- {
- buf[i] = '\r';
- buf[i+1] = '\n';
- memcpy(redata,buf,sizeof(buf));
- flag_RecFul = 1;
- }
- UART1_SendStr(redata);
- }
- }
- }
- else if(ps2_mode==1)//綠燈模式
- {
- switch(ps2_key)
- {
- case PSB_PAD_UP:pwm[1]+=10;if(pwm[1]>=2300) pwm[1]=2300;break;
- case PSB_PAD_DOWN:pwm[1]-=10;if(pwm[1]<=700) pwm[1]=700;break;
- case PSB_PAD_LEFT:pwm[2]+=10;if(pwm[2]>=2300) pwm[2]=2300;break;
- case PSB_PAD_RIGHT:pwm[2]-=10;if(pwm[2]<=700) pwm[2]=700;break;
-
- case PSB_TRIANGLE:pwm[3]+=10;if(pwm[3]>=2300) pwm[3]=2300;break;
- case PSB_CROSS:pwm[3]-=10;if(pwm[3]<=700) pwm[3]=700;break;
- case PSB_PINK:pwm[4]+=10;if(pwm[4]>=2300) pwm[4]=2300;break;
- case PSB_CIRCLE:pwm[4]-=10;if(pwm[4]<=700) pwm[4]=700;break;
- case PSB_L1:pwm[5]+=10;if(pwm[5]>=2300) pwm[5]=2300;break;
- case PSB_L2:pwm[5]-=10;if(pwm[5]<=700) pwm[5]=700;break;
- case PSB_R1:pwm[6]+=10;if(pwm[6]>=2300) pwm[6]=2300;break;
- case PSB_R2:pwm[6]-=10;if(pwm[6]<=700) pwm[6]=700;break;
- default:break;
- }
- }
- }
- }
- /***************************************************************************************************************
- 函 數(shù) 名:從flash讀取舵機(jī)總的信息
- 功能描述:初始化舵機(jī)控制信息
- 輸入?yún)?shù):無
- 返 回 值:無
- 備 注:
- ****************************************************************************************************************/
- void ReadMoterInfor()
- {
- memset(&motor_data,0,sizeof(motor_data));//清 0
- SpiFlashRead((char *)&motor_data,(CMD_FLASH_ADDR)<<WRITE_BIT_DEPTH,sizeof(motor_data));//讀取信息
- if (motor_data.CRC1 != 0x12345678 || motor_data.sum < 0 || motor_data.duoji_count > MOTOR_NUM)//判斷信息存儲是否有錯
- {
- memset(&motor_data,0,sizeof(motor_data));
- //memset(&cur_item,0,sizeof(cur_item));
- }
- else//正常信息
- {
- //UART1_SendOneChar(motor_data.sum + 0x30);
- //cur_item.tuoji_count = motor_one_cmd.tuoji_count;//脫機(jī)運(yùn)行次數(shù)
- //cur_item.cur_num = 0;//清 0
- }
-
- }
- void ReadOneCmdInfor(unsigned int addr)
- {
- memset(&motor_one_cmd,0,sizeof(motor_one_cmd));//清 0
- SpiFlashRead((char *)&motor_one_cmd,((((unsigned long)addr)<<4) + FILE_FLASH_ADDR)<<WRITE_BIT_DEPTH,sizeof(motor_one_cmd));//讀取信息
- 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)//判斷信息存儲是否有錯
- {
- memset(&motor_one_cmd,0,sizeof(motor_one_cmd));
- }
- else//正常信息,以后留著驗證用的
- {
-
-
- cur_item.tuoji_count = motor_one_cmd.tuoji_count;//脫機(jī)運(yùn)行次數(shù)
- cur_item.cur_num = motor_one_cmd.start;
- //UART1_SendOneChar(motor_one_cmd.tuoji_count + 0x30);
- //cur_item.cur_num = 0;//清 0
- }
-
- }
- /***************************************************************************************************************
- 函 數(shù) 名:把舵機(jī)信息寫到flash中
- 功能描述:把舵機(jī)信息寫到flahs中
- 輸入?yún)?shù):無
- 返 回 值:無
- 備 注:
- ****************************************************************************************************************/
- void WriteMoterInfor()
- {
- uchar temp = 0;
- motor_data.CRC1 = 0x12345678;//校驗碼
- motor_data.duoji_count = MOTOR_NUM-1;
- temp = motor_data.filecount;
- SpiFlashEraseSector(CMD_FLASH_ADDR >> 4);//擦除以前存儲的信息
- SpiFlashWrite((char *)&motor_data,CMD_FLASH_ADDR<<WRITE_BIT_DEPTH,sizeof(motor_data)); //寫入flash
- ReadMoterInfor();
- if (temp != motor_data.filecount)
- {
- error |= ERROR_FLASH_WRITE;
- }
- else
- {
- error &= ~ERROR_FLASH_WRITE;
- }
- }
- void WriteOneCmdInfor(unsigned int addr)
- {
- uchar temp = 0;
- temp = motor_one_cmd.end;
- if (((((unsigned long)addr)<<4) + FILE_FLASH_ADDR) % 16 == 0)
- SpiFlashEraseSector(((((unsigned long)addr)<<4) + FILE_FLASH_ADDR) >> 4);//擦除以前存儲的信息
- SpiFlashWrite((char *)&motor_one_cmd,((((unsigned long)addr)<<4) + FILE_FLASH_ADDR)<<WRITE_BIT_DEPTH,sizeof(motor_one_cmd)); //寫入flash
- ReadOneCmdInfor(addr);
- if (temp != motor_one_cmd.end)
- {
- error |= ERROR_FLASH_WRITE1;
- }
- else
- {
- error &= ~ERROR_FLASH_WRITE1;
- }
- }
- /***************************************************************************************************************
- 函 數(shù) 名:獲取一串舵機(jī)的控制字符串
- 功能描述:處理從flash讀取的舵機(jī)控制字符串處理
- 輸入?yún)?shù):無
- 返 回 值:無
- 備 注:
- ****************************************************************************************************************/
- void GetOneMotorCMD()
- {
- #if DEBUG
- uchar buf[20] = {0};
- #endif
- if (flag_stop_download)//接收到了上位機(jī)的停止下載的命令
- {
- flag_download = 0;//清楚下載狀態(tài)標(biāo)志位
- flag_stop_download = 0;
- flag_read = 0;
- if (motor_data.filecount < MAX_SUPPORT_FILE_SAVE)
- {
- updata_file_list();
- motor_data.sum = motor_one_cmd.end;
- motor_data.file_flag[motor_data.filecount] = 1;
- motor_one_cmd.cur_file_num = file_last_num + 1;
- motor_data.filecount = motor_one_cmd.cur_file_num + 1;
- error &= ~MAX_SUPPORT_FILE_SAVE;
- #if DEBUG
- sprintf(buf,"%d %d\r\n",(uint)motor_data.filecount,(uint)motor_data.file_flag[motor_data.filecount-1]);
- UART1_SendStr(buf);
- #endif
- WriteMoterInfor();
- WriteOneCmdInfor(motor_one_cmd.cur_file_num);
- updata_file_list();
- #if DEBUG
-
- sprintf(buf,"%d %d\r\n",(uint)motor_data.filecount,(uint)motor_data.file_flag[motor_data.filecount-1]);
- UART1_SendStr(buf);
- #endif
- }
- else
- {
- error |= MAX_SUPPORT_FILE_SAVE;
- }
- if (!(error &(MAX_SUPPORT_FILE_SAVE | ERROR_FLASH_FULL)))
- send_mode |= SEND_DOWN_OK;//狀態(tài)位置為
- }
- if (flag_connect)//如果當(dāng)前跟上位機(jī)聯(lián)機(jī)狀態(tài)
- {
- if (flag_read)//如果上位機(jī)讀取flash內(nèi)存儲的信息
- {
- if (cur_item.cur_num < motor_one_cmd.end)//判斷是否超過之前存儲的數(shù)
- {
- if ((send_mode & SEND_SEND_FILE))//開始接收到讀取命令需要先發(fā)送個start
- {
- UART1_SendStr("#Start\r\n");
- send_mode &= ~SEND_READ_FILE;
- }
- memset(redata,0,WRITE_SIZE);//清 0
- SpiFlashRead(redata,(((long)cur_item.cur_num)<<WRITE_BIT_DEPTH),WRITE_SIZE);//讀取信息
- #if DEBUG
- sprintf(buf,"%d\r\n",cur_item.cur_num);
- UART1_SendStr(buf);
- #endif
- UART1_SendStr(redata);//發(fā)送
- cur_item.cur_num++;
- }
- else//否則
- {
- if (cur_item.cur_num > 0)
- UART1_SendStr("#End\r\n");//發(fā)送結(jié)束字符串
- flag_read = 0;
- }
- send_mode = 0;//請 0
- }
- if (flag_online_run)
- {
- if ((send_mode & SEND_CC) != 0 || cur_item.cur_num == motor_one_cmd.start)//如果當(dāng)前需要更新舵機(jī)命令
- {
- if (cur_item.tuoji_count > 0)//脫機(jī)次數(shù)沒結(jié)束
- {
- if (cur_item.cur_num < motor_one_cmd.end)//判斷是否讀取結(jié)束
- {
- SpiFlashRead(redata,((long)cur_item.cur_num)<<WRITE_BIT_DEPTH,WRITE_SIZE);//讀取命令
- flag_RecFul = 1;//標(biāo)志位為1,
- cur_item.cur_num++;//
- }
- else//執(zhí)行玩一遍
- {
- cur_item.cur_num = motor_one_cmd.start;
- cur_item.tuoji_count--;//減一
- }
- }
- else//執(zhí)行完成
- {
- flag_online_run = 0;
- if (flag_connect_run)//如果上位機(jī)選擇執(zhí)行的功能,需要發(fā)送 AGF作為結(jié)束
- {
- UART1_SendStr("#AGF\r\n");
- flag_connect_run = 0;
- }
- }
- //讀取數(shù)據(jù)
- }
- }
- }
- else//脫機(jī)
- {
- if (file_list_count < 0)
- {
- return;
- }
- if (cur_item.tuoji_count > 0)
- {
- if ((send_mode & SEND_CC) != 0 || cur_item.cur_num == motor_one_cmd.start)//如果當(dāng)前需要更新舵機(jī)命令
- {
- if (cur_item.cur_num < motor_one_cmd.end)//判斷是否讀取結(jié)束
- {
- SpiFlashRead(redata,((long)cur_item.cur_num)<<WRITE_BIT_DEPTH,WRITE_SIZE);//讀取命令
- flag_RecFul = 1;//標(biāo)志位為1,
- cur_item.cur_num++;//
- }
- else//執(zhí)行玩一遍
- {
- cur_item.cur_num = motor_one_cmd.start;
- cur_item.tuoji_count--;//減一
- }
- //讀取數(shù)據(jù)
- }
- }
- else
- {
- ReadOneCmdInfor(file_list[cur_item.file_num]);
- file_list_count--;
- cur_item.file_num++;
- }
- }
-
-
- }
- /***************************************************************************************************************
- 函 數(shù) 名:發(fā)送串口狀態(tài)信息
- 功能描述:根據(jù)狀態(tài)標(biāo)志位發(fā)送相應(yīng)的信息
- 輸入?yún)?shù):無
- 返 回 值:無
- 備 注:
- ****************************************************************************************************************/
- void SendUartState()
- {
- uchar buf[40] = {0};
- uchar read_motor_num = 0;
- uint i = 0;
- static int count = 0;
- if (send_mode)//如果有狀態(tài)需要發(fā)送
- {
- if (send_mode & SEND_A) //發(fā)送A
- {
- UART1_SendOneChar('A');
- send_mode &= ~SEND_A;//清狀態(tài)
- }
- if (send_mode & SEND_CC) //發(fā)送CC
- {
- UART1_SendStr("#CC\r\n");
- send_mode &= ~SEND_CC;
- }
- if (send_mode & SEND_DOWN_OK)//發(fā)送下載ok的狀態(tài)字符串
- {
- sprintf(buf,"#Down+OK+%d\r\n",(int)motor_data.filecount-1);
- UART1_SendStr(buf);
- send_mode &= ~SEND_DOWN_OK;
- #if DEBUG
- sprintf(buf,"%d\r\n",(uint)motor_data.filecount);
- UART1_SendStr(buf);
- #endif
- }
- if (send_mode & SEND_START_OK)//發(fā)送連接時候的字符串
- {
- UART1_SendStr("#Veri+UART+OK+20160906+176\r\n");
- send_mode &= ~SEND_START_OK;
- }
- if (send_mode & SEND_READ_FILE)//發(fā)送讀取文件的時候字符串
- {
- if (motor_data.filecount > 0)//如果保存的有舵機(jī)命令
- {
- //#Name:1.txt--Size:48--Name:2.txt--Size:190--Name:desktop.ini--Size:531--
- UART1_SendStr("#");//發(fā)送
- for (i = 0; i < motor_data.filecount;i++)
- {
- if (motor_data.file_flag[i] == 1)
- {
- ReadOneCmdInfor(i);
- if (motor_one_cmd.end - motor_one_cmd.start <= 0)
- {
- motor_data.file_flag[i] = 0;
- WriteMoterInfor();
- #if DEBUG
- sprintf(buf,"E=%d S=%d",motor_one_cmd.end, motor_one_cmd.start);
- #endif
- }
- else
- {
- sprintf(buf,"Name:%d.txt--Size:%d--",i,motor_one_cmd.end - motor_one_cmd.start);//獲取命令個數(shù)
- UART1_SendStr(buf);//發(fā)送
- }
- }
- }
- UART1_SendStr("\r\n");
- }
- else
- {
- sprintf(buf,"#\r\n",motor_data.sum);
- UART1_SendStr(buf);
- }
- send_mode &= ~SEND_READ_FILE;
- }
- if (send_mode & SEND_SET_OFFLINE_OK)//設(shè)置脫機(jī)運(yùn)行次數(shù)
- {
- WriteOneCmdInfor(motor_one_cmd.cur_file_num);//保存
- UART1_SendStr("#Enable+OK...\r\n");
- send_mode &= ~SEND_SET_OFFLINE_OK;
- }
- if (send_mode & SEND_SET_DISABLEOFFLINE_OK)//禁止脫機(jī)運(yùn)行
- {
- for (i = 0; i < motor_data.filecount;i++)
- {
- if (motor_data.file_flag[i] == 1)
- {
- ReadOneCmdInfor(i);
- motor_one_cmd.tuoji_count = 0;
- WriteOneCmdInfor(i);
- }
- }
- WriteMoterInfor();
- UART1_SendStr("#Disable+OK...\r\n");
- send_mode &= ~SEND_SET_DISABLEOFFLINE_OK;
- }
- if (send_mode & SEND_SET_ONLINE_OK)//發(fā)送聯(lián)機(jī)運(yùn)行狀態(tài)
- {
- UART1_SendStr("#OK\r\n");
- sprintf(buf,"#%dGC%d\r\n",cur_item.file_num,tuoji_count);
- UART1_SendStr(buf);
- UART1_SendStr("#LP=0\r\n");
- send_mode &= ~SEND_SET_ONLINE_OK;
- flag_connect_run = 1;
- }
- if (send_mode & SEND_SET_DELETE_ONE_FILE_OK)//發(fā)送刪除文件命令
- {
- //cur_item.tuoji_count = 0;
- if (cur_item.delete_num < motor_data.filecount)
- {
- motor_data.file_flag[cur_item.delete_num] = 0;
- WriteMoterInfor();
- updata_file_list();
- if (cur_item.delete_num == motor_data.filecount-1)
- {
- motor_data.filecount = file_last_num + 1;
- if (file_last_num == -1)
- {
- motor_data.sum = 0;
- }
- else
- {
- ReadOneCmdInfor(file_last_num);
- motor_data.sum = motor_one_cmd.end;
- motor_data.sum = (((long int)(motor_data.sum) >>4)<<4) + (1<<4);
- }
- WriteMoterInfor();
- }
- updata_file_list();
- UART1_SendStr("#FDel+OK\r\n");
- }
- send_mode &= ~SEND_SET_DELETE_ONE_FILE_OK;
-
- }
- if (send_mode & SEND_SET_DELETE_ALL_FILE_OK)//發(fā)送擦除所有文件的命令
- {
- UART1_SendStr("#Format+Start\r\n");
- SpiFlashEraseChip();
- cur_item.tuoji_count = 0;
- motor_data.sum = 0;
- motor_data.filecount = 0;
- memset(motor_data.file_flag,0,sizeof(motor_data.file_flag));
- WriteMoterInfor();
- UART1_SendStr("#Format+OK\r\n");
- send_mode &= ~SEND_SET_DELETE_ALL_FILE_OK;
- updata_file_list();
- }
- if (send_mode & SEND_SET_PS2_OK)
- {
- UART1_SendStr("#PS2+OK...\r\n");
- send_mode &= ~SEND_SET_PS2_OK;
- }
-
- #define MATHION_HAND_NUM 20
- if (send_mode & SEND_SET_READ_UART_MOTOR_ANGLE)
- {
- if (cur_item.read_num < MATHION_HAND_NUM)
- {
- if (flag_uart2_rev)
- {
- read_motor_num = atoi(uart2_buf + 1);
- if (read_motor_num == cur_item.read_num)
- {
- i = atoi(uart2_buf+5);
- sprintf(uart2_buf,"#%dP%d",(int)read_motor_num,i);
- UART1_SendStr(uart2_buf);
- cur_item.read_num++;
- buf[0] = '#';
- buf[1] = cur_item.read_num / 100 % 10 + 0x30;
- buf[2] = cur_item.read_num / 10 % 10 + 0x30;
- buf[3] = cur_item.read_num % 10 + 0x30;
- buf[4] = 'P';buf[5] = 'R';buf[6] = 'A';buf[7] = 'D';buf[8] = '\r';buf[9] = '\n';
- UART2_SendStr(buf);
-
- }
- flag_uart2_rev = 0;
- count = 0;
-
- }
- else
- {
- count++;
- if (count >= 10)
- {
- cur_item.read_num++;
- buf[0] = '#';
- buf[1] = cur_item.read_num / 100 % 10 + 0x30;
- buf[2] = cur_item.read_num / 10 % 10 + 0x30;
- buf[3] = cur_item.read_num % 10 + 0x30;
- buf[4] = 'P';buf[5] = 'R';buf[6] = 'A';buf[7] = 'D';buf[8] = '\r';buf[9] = '\n';
- UART2_SendStr(buf);
- flag_uart2_rev = 0;
- flag_uart2_rev_time_out = 0;
- count = 0;
- }
- }
- }
- else
- {
- send_mode &= ~SEND_SET_READ_UART_MOTOR_ANGLE;
- cur_item.read_num= 0;
- UART1_SendStr("\r\n");
- }
- }
- }
- if (send_mode & SEND_SET_SET_UART_MOTOR_PULK)
- {
- if (cur_item.pulk_num < MATHION_HAND_NUM)
- {
- count++;
- if (count >= 20)
- {
- sprintf(uart2_buf,"#%dPULK\r\n",(int)cur_item.pulk_num);
- UART2_SendStr(uart2_buf);
- #if DEBUG
- UART1_SendStr(uart2_buf);
- #endif
- cur_item.pulk_num++;
- count = 0;
- }
- }
- else
- {
- send_mode &= ~SEND_SET_SET_UART_MOTOR_PULK;
- }
- }
- if (send_mode & SEND_SET_SET_UART_MOTOR_ANGLE)
- {
- if (cur_item.pulk_num < MATHION_HAND_NUM)
- {
- count++;
- if (count >= 5)
- {
- sprintf(uart2_buf,"#%dPMOD%d\r\n",(int)cur_item.pulk_num,(int)cur_item.angle_mode);
- UART2_SendStr(uart2_buf);
- #if DEBUG
- UART1_SendStr(uart2_buf);
- #endif
- cur_item.pulk_num++;
- count = 0;
- }
- }
- else
- {
- sprintf(buf,"#255PMOD%d+0K...\r\n",(int)cur_item.angle_mode);
- UART1_SendStr(buf);
- send_mode &= ~SEND_SET_SET_UART_MOTOR_ANGLE;
- flag_uart2_rev = 0;
- }
- }
- if (send_mode & SEND_SET_BEEP_ON)
- {
- ReadMoterInfor();
- motor_data.beep_mode = 1;
- beep_mode = 1;
- WriteMoterInfor();
- UART1_SendStr("#FMQENABLE+OK...\r\n");
- send_mode &= ~SEND_SET_BEEP_ON;
- }
- if (send_mode & SEND_SET_BEEP_OFF)
- {
- ReadMoterInfor();
- motor_data.beep_mode = 0;
- beep_mode = 0;
- WriteMoterInfor();
- UART1_SendStr("#FMQDISABLE+OK...\r\n");
- send_mode &= ~SEND_SET_BEEP_OFF;
- }
- }
- /***************************************************************************************************************
- 函 數(shù) 名:作業(yè)初位置,末尾置更新函數(shù)
- 功能描述:從緩存中取一個新的目標(biāo)位置替換原來的目標(biāo)位置,原來的目標(biāo)位置變?yōu)樾碌某跷恢茫淮胃?br />
- :有效的數(shù)據(jù)是插補(bǔ)增量,和插補(bǔ)次數(shù),知道這兩個量,和當(dāng)前初位置即可
- 備 注: 先進(jìn)先出,循環(huán)訪問
- ****************************************************************************************************************/
- void change(void)
- {
- uchar s;
- if(line>0) //緩存中有數(shù)據(jù)
- {
- line--; //執(zhí)行一行
- if(line<5) //緩存允許放入新的數(shù)據(jù)
- flag_in=1;
- point_now++; //取數(shù)位置更新
- point_aim++;
-
- if(point_aim==7)
- point_aim=0;
- if(point_now==7)
- point_now=0;
- n=pos[point_aim][0]*4/5; //計算新的插補(bǔ)次數(shù)
- for(s=1;s<MOTOR_NUM;s++) //計算新的插補(bǔ)增量
- {
- if(pos[point_aim][s]>pos[point_now][s])
- {
- dp=pos[point_aim][s]-pos[point_now][s];
- dp0[s]=dp/n;
- }
- if(pos[point_aim][s]<=pos[point_now][s])
- {
- dp=pos[point_now][s]-pos[point_aim][s];
- dp0[s]=dp/n;
- dp0[s]=-dp0[s];
- }
- }
- m=0; //m清0
- flag_stop=0; //產(chǎn)生了新的目標(biāo)位置,停止標(biāo)志清零
-
- }
- else //沒有緩存數(shù)據(jù),即line==0
- {
- flag_out=0; //緩存中沒有數(shù)據(jù)
- }
- }
- /***************************************************************************************************************
- 函 數(shù) 名:vpwm()
- 功能描述:數(shù)據(jù)插補(bǔ),插補(bǔ)時間間隔為20/12ms,由timer0控制,使舵機(jī)平滑實現(xiàn)速度控制
- :另一個功能是執(zhí)行完一行后去更新下一行數(shù)據(jù),即調(diào)用change()
- 備 注:
- ****************************************************************************************************************/
- void vpwm(void)
- {
- uchar j=0;
- uchar how=0;
- static uchar flag_how;
- static uchar flag_Tover;
- if(flag_stop==1) //一行作業(yè)全部完成
- {
- if(flag_out==1) //緩沖數(shù)組中有數(shù)據(jù)
- {
- change(); //更新行
- }
- }
- else //不是一行數(shù)據(jù)全部完成,處于中間插補(bǔ)階段
- {
- m++; //用來累加插補(bǔ)過的次數(shù)
- if(m==n) //n是本行作業(yè)要插補(bǔ)的總次數(shù)
- {
- flag_Tover=1; //一行數(shù)據(jù)的執(zhí)行時間已經(jīng)完成
- send_mode |= SEND_CC;
- }
- for(j=1;j<MOTOR_NUM;j++)
- {
- if(abs(pwm[j]-pos[point_aim][j])<5)
- { //檢測靠近終點(diǎn)位置
- how++; //是,則累加一個
- pwm[j]=pos[point_aim][j];//并且直接過度到終點(diǎn)位置
- }
- else //不靠近終點(diǎn),繼續(xù)插補(bǔ)
- pwm[j]=pos[point_now][j]+m*dp0[j];
- }
- //UART_Put_Inf("pwm",pwm[1]);
- if(how==MOTOR_NUM-1)
- flag_how=1; //16個舵機(jī)都到達(dá)終點(diǎn)
- how=0;
- if((flag_Tover==1)&&(flag_how==1))
- { //從插補(bǔ)次數(shù),和脈寬寬度兩方面都到達(dá)終點(diǎn),本作業(yè)行完成
- flag_Tover=0;
- flag_how=0;
- flag_stop=1;
- }
-
- }
- return;
- }
-
復(fù)制代碼
|
|