單片機(jī)(8bit)的16路舵機(jī)調(diào)速分析與實(shí)現(xiàn)
[cpp]view plaincopyprint?
- #include
1.H> - #include"ControlRobot.h"
- #include
- #defineDEBUG_PROTOCOL
- typedefunsignedcharUCHAR8;
- typedefunsignedintUINT16;
- #undefTRUE
- #undefFALSE
- #defineTRUE1
- #defineFALSE0
- #defineMEMORY_MODEL
- UINT16MEMORY_MODELdelayVar1;
- UCHAR8MEMORY_MODELdelayVar2;
- #defineBAUD_RATE0xF3
- #defineUSED_PIN2
- #definePIN_GROUP_10
- #definePIN_GROUP_21
- #defineGROUP_1_CONTROL_PINP0
- #defineGROUP_2_CONTROL_PINP2
- #defineSTEERING_ENGINE_CYCLE2000
- #defineSTEERING_ENGINE_INIT_DELAY50
- #defineSTEERING_ENGINE_FULL_CYCLE((STEERING_ENGINE_CYCLE)-(STEERING_ENGINE_INIT_DELAY))
- volatileUCHAR8MEMORY_MODELg_angle[2][ROBOT_JOINT_AMOUNT];
- volatilebitMEMORY_MODELg_fillingBufferIndex=0;
- volatilebitMEMORY_MODELg_readyBufferIndex=1;
- volatilebitMEMORY_MODELg_swapBuffer=FALSE;
- volatileUINT16MEMORY_MODELg_diffAngle[USED_PIN][8];
- volatileUCHAR8MEMORY_MODELg_diffAngleMask[USED_PIN][8]=
- {
- {
- ROBOT_PIN_MASK_LEFT_SHOULDER_VERTICAL,
- ROBOT_PIN_MASK_LEFT_SHOULDER_HORIZEN,
- ROBOT_PIN_MASK_LEFT_ELBOW,
- ROBOT_PIN_MASK_RIGHT_SHOULDER_VERTICAL,
- ROBOT_PIN_MASK_RIGHT_SHOULDER_HORIZEN,
- ROBOT_PIN_MASK_RIGHT_ELBOW,
- ROBOT_PIN_MASK_LEFT_HIP_VERTICAL,
- ROBOT_PIN_MASK_RIGHT_HIP_VERTICAL
- },
- {
- ROBOT_PIN_MASK_LEFT_HIP_HORIZEN,
- ROBOT_PIN_MASK_LEFT_KNEE,
- ROBOT_PIN_MASK_LEFT_ANKLE,
- ROBOT_PIN_MASK_LEFT_FOOT,
- ROBOT_PIN_MASK_RIGHT_HIP_HORIZEN,
- ROBOT_PIN_MASK_RIGHT_KNEE,
- ROBOT_PIN_MASK_RIGHT_ANKLE,
- ROBOT_PIN_MASK_RIGHT_FOOT
- }
- };
- #ifdefDEBUG_PROTOCOL
- sbitP10=P1^0;//正在填充交換區(qū)1
- sbitP11=P1^1;//正在填充交換區(qū)2
- sbitP12=P1^2;//交換區(qū)變換
- sbitP13=P1^3;//協(xié)議是否正確
- #endif
- voidDelay10us(UINT16ntimes)
- {
- for(delayVar1=0;delayVar1
- for(delayVar2=0;delayVar2<21;++delayVar2)
- _nop_();
- }
- voidInitPwmPollint()
- {
- UCHAR8i;
- UCHAR8j;
- UCHAR8k;
- UINT16temp;
- for(i=0;i
- {
- for(j=0;j<7;++j)
- {
- for(k=j;k<8;++k)
- {
- if(g_diffAngle[i][j]>g_diffAngle[i][k])
- {
- temp=g_diffAngle[i][j];
- g_diffAngle[i][j]=g_diffAngle[i][k];
- g_diffAngle[i][k]=temp;
- temp=g_diffAngleMask[i][j];
- g_diffAngleMask[i][j]=g_diffAngleMask[i][k];
- g_diffAngleMask[i][k]=temp;
- }
- }
- }
- }
- for(i=0;i
- {
- for(j=0;j<8;++j)
- {
- if(INVALID_JOINT_VALUE==g_diffAngle[i][j])
- {
- g_diffAngle[i][j]=STEERING_ENGINE_FULL_CYCLE;
- }
- }
- }
- for(i=0;i
- {
- for(j=7;j>=1;--j)
- {
- g_diffAngle[i][j]=g_diffAngle[i][j]-g_diffAngle[i][j-1];
- }
- }
- }
- voidInitSerialPort()
- {
- AUXR=0x00;
- ES=0;
- TMOD=0x20;
- SCON=0x50;
- TH1=BAUD_RATE;
- TL1=TH1;
- PCON=0x80;
- EA=1;
- ES=1;
- TR1=1;
- }
- voidOnSerialPort()interrupt4
- {
- staticUCHAR8previousChar=0;
- staticUCHAR8currentChar=0;
- staticUCHAR8counter=0;
- if(RI)
- {
- RI=0;
- currentChar=SBUF;
- if(PROTOCOL_HEADER[0]==currentChar)//協(xié)議標(biāo)志
- {
- previousChar=currentChar;
- }
- else
- {
- if(PROTOCOL_HEADER[0]==previousChar&&PROTOCOL_HEADER[1]==currentChar)//協(xié)議開始
- {
- counter=0;
- previousChar=currentChar;
- g_swapBuffer=FALSE;
- }
- elseif(PROTOCOL_END[0]==previousChar&&PROTOCOL_END[1]==currentChar)//協(xié)議結(jié)束
- {
- previousChar=currentChar;
- if(ROBOT_JOINT_AMOUNT==counter)//協(xié)議接受正確
- {
- if(0==g_fillingBufferIndex)
- {
- g_readyBufferIndex=0;
- g_fillingBufferIndex=1;
- }
- else
- {
- g_readyBufferIndex=1;
- g_fillingBufferIndex=0;
- }
- g_swapBuffer=TRUE;
- #ifdefDEBUG_PROTOCOL
- P13=0;
- #endif
- }
- else
- {
- g_swapBuffer=FALSE;
- #ifdefDEBUG_PROTOCOL
- P13=1;
- #endif
- }
- counter=0;
- }
- else//接受協(xié)議正文
- {
- g_swapBuffer=FALSE;
- previousChar=currentChar;
- if(counter
- g_angle[g_fillingBufferIndex][counter]=currentChar;
- ++counter;
- }
- }//if(PROTOCOL_HEADER[0]==currentChar)
- SBUF=currentChar;
- while(!TI)
- ;
- TI=0;
- }//(RI)
- }
- voidFillDiffAngle()
- {
- //設(shè)置舵機(jī)要調(diào)整的角度
- g_diffAngle[PIN_GROUP_1][ROBOT_PIN_INDEX_LEFT_SHOULDER_VERTICAL]=g_angle[g_readyBufferIndex][ROBOT_LEFT_SHOULDER_VERTICAL];
- g_diffAngle[PIN_GROUP_1][ROBOT_PIN_INDEX_LEFT_SHOULDER_HORIZEN]=g_angle[g_readyBufferIndex][ROBOT_LEFT_SHOULDER_HORIZEN];
- g_diffAngle[PIN_GROUP_1][ROBOT_PIN_INDEX_LEFT_ELBOW]=g_angle[g_readyBufferIndex][ROBOT_LEFT_ELBOW];
- g_diffAngle[PIN_GROUP_1][ROBOT_PIN_INDEX_RIGHT_SHOULDER_VERTICAL]=g_angle[g_readyBufferIndex][ROBOT_RIGHT_SHOULDER_VERTICAL];
- g_diffAngle[PIN_GROUP_1][ROBOT_PIN_INDEX_RIGHT_SHOULDER_HORIZEN]=g_angle[g_readyBufferIndex][ROBOT_RIGHT_SHOULDER_HORIZEN];
- g_diffAngle[PIN_GROUP_1][ROBOT_PIN_INDEX_RIGHT_ELBOW]=g_angle[g_readyBufferIndex][ROBOT_RIGHT_ELBOW];
- g_diffAngle[PIN_GROUP_1][ROBOT_PIN_INDEX_LEFT_HIP_VERTICAL]=g_angle[g_readyBufferIndex][ROBOT_LEFT_HIP_VERTICAL];
- g_diffAngle[PIN_GROUP_1][ROBOT_PIN_INDEX_RIGHT_HIP_VERTICAL]=g_angle[g_readyBufferIndex][ROBOT_RIGHT_HIP_VERTICAL];
- g_diffAngle[PIN_GROUP_2][ROBOT_PIN_INDEX_LEFT_HIP_HORIZEN]=g_angle[g_readyBufferIndex][ROBOT_LEFT_HIP_HORIZEN];
- g_diffAngle[PIN_GROUP_2][ROBOT_PIN_INDEX_LEFT_KNEE]=g_angle[g_readyBufferIndex][ROBOT_LEFT_KNEE];
- g_diffAngle[PIN_GROUP_2][ROBOT_PIN_INDEX_LEFT_ANKLE]=g_angle[g_readyBufferIndex][ROBOT_LEFT_ANKLE];
- g_diffAngle[PIN_GROUP_2][ROBOT_PIN_INDEX_LEFT_FOOT]=g_angle[g_readyBufferIndex][ROBOT_LEFT_FOOT];
- g_diffAngle[PIN_GROUP_2][ROBOT_PIN_INDEX_RIGHT_HIP_HORIZEN]=g_angle[g_readyBufferIndex][ROBOT_RIGHT_HIP_HORIZEN];
- g_diffAngle[PIN_GROUP_2][ROBOT_PIN_INDEX_RIGHT_KNEE]=g_angle[g_readyBufferIndex][ROBOT_RIGHT_KNEE];
- g_diffAngle[PIN_GROUP_2][ROBOT_PIN_INDEX_RIGHT_ANKLE]=g_angle[g_readyBufferIndex][ROBOT_RIGHT_ANKLE];
- g_diffAngle[PIN_GROUP_2][ROBOT_PIN_INDEX_RIGHT_FOOT]=g_angle[g_readyBufferIndex][ROBOT_RIGHT_FOOT];
- //復(fù)位舵機(jī)管腳索引
- g_diffAngleMask[PIN_GROUP_1][ROBOT_PIN_INDEX_LEFT_SHOULDER_VERTICAL]=ROBOT_PIN_MASK_LEFT_SHOULDER_VERTICAL;
- g_diffAngleMask[PIN_GROUP_1][ROBOT_PIN_INDEX_LEFT_SHOULDER_HORIZEN]=ROBOT_PIN_MASK_LEFT_SHOULDER_HORIZEN;
- g_diffAngleMask[PIN_GROUP_1][ROBOT_PIN_INDEX_LEFT_ELBOW]=ROBOT_PIN_MASK_LEFT_ELBOW;
- g_diffAngleMask[PIN_GROUP_1][ROBOT_PIN_INDEX_RIGHT_SHOULDER_VERTICAL]=ROBOT_PIN_MASK_RIGHT_SHOULDER_VERTICAL;
- g_diffAngleMask[PIN_GROUP_1][ROBOT_PIN_INDEX_RIGHT_SHOULDER_HORIZEN]=ROBOT_PIN_MASK_RIGHT_SHOULDER_HORIZEN;
- g_diffAngleMask[PIN_GROUP_1][ROBOT_PIN_INDEX_RIGHT_ELBOW]=ROBOT_PIN_MASK_RIGHT_ELBOW;
- g_diffAngleMask[PIN_GROUP_1][ROBOT_PIN_INDEX_LEFT_HIP_VERTICAL]=ROBOT_PIN_MASK_LEFT_HIP_VERTICAL;
- g_diffAngleMask[PIN_GROUP_1][ROBOT_PIN_INDEX_RIGHT_HIP_VERTICAL]=ROBOT_PIN_MASK_RIGHT_HIP_VERTICAL;
- g_diffAngleMask[PIN_GROUP_2][ROBOT_PIN_INDEX_LEFT_HIP_HORIZEN]=ROBOT_PIN_MASK_LEFT_HIP_HORIZEN;
- g_diffAngleMask[PIN_GROUP_2][ROBOT_PIN_INDEX_LEFT_KNEE]=ROBOT_PIN_MASK_LEFT_KNEE;
- g_diffAngleMask[PIN_GROUP_2][ROBOT_PIN_INDEX_LEFT_ANKLE]=ROBOT_PIN_MASK_LEFT_ANKLE;
- g_diffAngleMask[PIN_GROUP_2][ROBOT_PIN_INDEX_LEFT_FOOT]=ROBOT_PIN_MASK_LEFT_FOOT;
- g_diffAngleMask[PIN_GROUP_2][ROBOT_PIN_INDEX_RIGHT_HIP_HORIZEN]=ROBOT_PIN_MASK_RIGHT_HIP_HORIZEN;
- g_diffAngleMask[PIN_GROUP_2][ROBOT_PIN_INDEX_RIGHT_KNEE]=ROBOT_PIN_MASK_RIGHT_KNEE;
- g_diffAngleMask[PIN_GROUP_2][ROBOT_PIN_INDEX_RIGHT_ANKLE]=ROBOT_PIN_MASK_RIGHT_ANKLE;
- g_diffAngleMask[PIN_GROUP_2][ROBOT_PIN_INDEX_RIGHT_FOOT]=ROBOT_PIN_MASK_RIGHT_FOOT;
- }
- #definePWM_STEERING_ENGINE(group)
- {
- counter=STEERING_ENGINE_INIT_DELAY;
- for(i=0;i<8;++i)
- counter+=g_diffAngle[PIN_GROUP_##group][i];
- for(i=0;i<30;++i)
- {
- GROUP_##group##_CONTROL_PIN=0xFF;
- Delay10us(STEERING_ENGINE_INIT_DELAY);
- for(j=0;j<8;++j)
- {
- Delay10us(g_diffAngle[PIN_GROUP_##group][j]);
- GROUP_##group##_CONTROL_PIN&=~(g_diffAngleMask[PIN_GROUP_##group][j]);
- }
- Delay10us(STEERING_ENGINE_CYCLE-counter);
- }
- }
- voidmain()
- {
- UCHAR8i;
- UCHAR8j;
- UINT16counter;
- InitSerialPort();
- P1=0xFF;
- //初始化舵機(jī)角度
- for(i=0;i
- {
- g_angle[0][i]=45;
- g_angle[1][i]=45;
- }
- for(i=0;i
- for(j=0;j<8;++j)
- g_diffAngle[i][j]=0;
- FillDiffAngle();
- InitPwmPollint();
- while(1)
- {
- #ifdefDEBUG_PROTOCOL
- if(g_fillingBufferIndex)
- {
- P11=0;
- P10=1;
- }
- else
- {
- P11=1;
- P10=0;
- }
- if(g_swapBuffer)
- P12=0;
- else
- P12=1;
- #endif
- if(g_swapBuffer)
- {
- FillDiffAngle();
- g_swapBuffer=FALSE;
- InitPwmPollint();
- }
- PWM_STEERING_ENGINE(1)
- PWM_STEERING_ENGINE(2)
- }
- }
評(píng)論