新聞中心

EEPW首頁 > 嵌入式系統(tǒng) > 設(shè)計(jì)應(yīng)用 > 單片機(jī)(8bit)的16路舵機(jī)調(diào)速分析與實(shí)現(xiàn)

單片機(jī)(8bit)的16路舵機(jī)調(diào)速分析與實(shí)現(xiàn)

作者: 時(shí)間:2016-11-18 來源:網(wǎng)絡(luò) 收藏
// main.c
[cpp]view plaincopyprint?
  1. #include1.H>
  2. #include"ControlRobot.h"
  3. #include
  4. #defineDEBUG_PROTOCOL
  5. typedefunsignedcharUCHAR8;
  6. typedefunsignedintUINT16;
  7. #undefTRUE
  8. #undefFALSE
  9. #defineTRUE1
  10. #defineFALSE0
  11. #defineMEMORY_MODEL
  12. UINT16MEMORY_MODELdelayVar1;
  13. UCHAR8MEMORY_MODELdelayVar2;
  14. #defineBAUD_RATE0xF3
  15. #defineUSED_PIN2
  16. #definePIN_GROUP_10
  17. #definePIN_GROUP_21
  18. #defineGROUP_1_CONTROL_PINP0
  19. #defineGROUP_2_CONTROL_PINP2
  20. #defineSTEERING_ENGINE_CYCLE2000
  21. #defineSTEERING_ENGINE_INIT_DELAY50
  22. #defineSTEERING_ENGINE_FULL_CYCLE((STEERING_ENGINE_CYCLE)-(STEERING_ENGINE_INIT_DELAY))
  23. volatileUCHAR8MEMORY_MODELg_angle[2][ROBOT_JOINT_AMOUNT];
  24. volatilebitMEMORY_MODELg_fillingBufferIndex=0;
  25. volatilebitMEMORY_MODELg_readyBufferIndex=1;
  26. volatilebitMEMORY_MODELg_swapBuffer=FALSE;
  27. volatileUINT16MEMORY_MODELg_diffAngle[USED_PIN][8];
  28. volatileUCHAR8MEMORY_MODELg_diffAngleMask[USED_PIN][8]=
  29. {
  30. {
  31. ROBOT_PIN_MASK_LEFT_SHOULDER_VERTICAL,
  32. ROBOT_PIN_MASK_LEFT_SHOULDER_HORIZEN,
  33. ROBOT_PIN_MASK_LEFT_ELBOW,
  34. ROBOT_PIN_MASK_RIGHT_SHOULDER_VERTICAL,
  35. ROBOT_PIN_MASK_RIGHT_SHOULDER_HORIZEN,
  36. ROBOT_PIN_MASK_RIGHT_ELBOW,
  37. ROBOT_PIN_MASK_LEFT_HIP_VERTICAL,
  38. ROBOT_PIN_MASK_RIGHT_HIP_VERTICAL
  39. },
  40. {
  41. ROBOT_PIN_MASK_LEFT_HIP_HORIZEN,
  42. ROBOT_PIN_MASK_LEFT_KNEE,
  43. ROBOT_PIN_MASK_LEFT_ANKLE,
  44. ROBOT_PIN_MASK_LEFT_FOOT,
  45. ROBOT_PIN_MASK_RIGHT_HIP_HORIZEN,
  46. ROBOT_PIN_MASK_RIGHT_KNEE,
  47. ROBOT_PIN_MASK_RIGHT_ANKLE,
  48. ROBOT_PIN_MASK_RIGHT_FOOT
  49. }
  50. };
  51. #ifdefDEBUG_PROTOCOL
  52. sbitP10=P1^0;//正在填充交換區(qū)1
  53. sbitP11=P1^1;//正在填充交換區(qū)2
  54. sbitP12=P1^2;//交換區(qū)變換
  55. sbitP13=P1^3;//協(xié)議是否正確
  56. #endif
  57. voidDelay10us(UINT16ntimes)
  58. {
  59. for(delayVar1=0;delayVar1
  60. for(delayVar2=0;delayVar2<21;++delayVar2)
  61. _nop_();
  62. }
  63. voidInitPwmPollint()
  64. {
  65. UCHAR8i;
  66. UCHAR8j;
  67. UCHAR8k;
  68. UINT16temp;
  69. for(i=0;i
  70. {
  71. for(j=0;j<7;++j)
  72. {
  73. for(k=j;k<8;++k)
  74. {
  75. if(g_diffAngle[i][j]>g_diffAngle[i][k])
  76. {
  77. temp=g_diffAngle[i][j];
  78. g_diffAngle[i][j]=g_diffAngle[i][k];
  79. g_diffAngle[i][k]=temp;
  80. temp=g_diffAngleMask[i][j];
  81. g_diffAngleMask[i][j]=g_diffAngleMask[i][k];
  82. g_diffAngleMask[i][k]=temp;
  83. }
  84. }
  85. }
  86. }
  87. for(i=0;i
  88. {
  89. for(j=0;j<8;++j)
  90. {
  91. if(INVALID_JOINT_VALUE==g_diffAngle[i][j])
  92. {
  93. g_diffAngle[i][j]=STEERING_ENGINE_FULL_CYCLE;
  94. }
  95. }
  96. }
  97. for(i=0;i
  98. {
  99. for(j=7;j>=1;--j)
  100. {
  101. g_diffAngle[i][j]=g_diffAngle[i][j]-g_diffAngle[i][j-1];
  102. }
  103. }
  104. }
  105. voidInitSerialPort()
  106. {
  107. AUXR=0x00;
  108. ES=0;
  109. TMOD=0x20;
  110. SCON=0x50;
  111. TH1=BAUD_RATE;
  112. TL1=TH1;
  113. PCON=0x80;
  114. EA=1;
  115. ES=1;
  116. TR1=1;
  117. }
  118. voidOnSerialPort()interrupt4
  119. {
  120. staticUCHAR8previousChar=0;
  121. staticUCHAR8currentChar=0;
  122. staticUCHAR8counter=0;
  123. if(RI)
  124. {
  125. RI=0;
  126. currentChar=SBUF;
  127. if(PROTOCOL_HEADER[0]==currentChar)//協(xié)議標(biāo)志
  128. {
  129. previousChar=currentChar;
  130. }
  131. else
  132. {
  133. if(PROTOCOL_HEADER[0]==previousChar&&PROTOCOL_HEADER[1]==currentChar)//協(xié)議開始
  134. {
  135. counter=0;
  136. previousChar=currentChar;
  137. g_swapBuffer=FALSE;
  138. }
  139. elseif(PROTOCOL_END[0]==previousChar&&PROTOCOL_END[1]==currentChar)//協(xié)議結(jié)束
  140. {
  141. previousChar=currentChar;
  142. if(ROBOT_JOINT_AMOUNT==counter)//協(xié)議接受正確
  143. {
  144. if(0==g_fillingBufferIndex)
  145. {
  146. g_readyBufferIndex=0;
  147. g_fillingBufferIndex=1;
  148. }
  149. else
  150. {
  151. g_readyBufferIndex=1;
  152. g_fillingBufferIndex=0;
  153. }
  154. g_swapBuffer=TRUE;
  155. #ifdefDEBUG_PROTOCOL
  156. P13=0;
  157. #endif
  158. }
  159. else
  160. {
  161. g_swapBuffer=FALSE;
  162. #ifdefDEBUG_PROTOCOL
  163. P13=1;
  164. #endif
  165. }
  166. counter=0;
  167. }
  168. else//接受協(xié)議正文
  169. {
  170. g_swapBuffer=FALSE;
  171. previousChar=currentChar;
  172. if(counter
  173. g_angle[g_fillingBufferIndex][counter]=currentChar;
  174. ++counter;
  175. }
  176. }//if(PROTOCOL_HEADER[0]==currentChar)
  177. SBUF=currentChar;
  178. while(!TI)
  179. ;
  180. TI=0;
  181. }//(RI)
  182. }
  183. voidFillDiffAngle()
  184. {
  185. //設(shè)置舵機(jī)要調(diào)整的角度
  186. g_diffAngle[PIN_GROUP_1][ROBOT_PIN_INDEX_LEFT_SHOULDER_VERTICAL]=g_angle[g_readyBufferIndex][ROBOT_LEFT_SHOULDER_VERTICAL];
  187. g_diffAngle[PIN_GROUP_1][ROBOT_PIN_INDEX_LEFT_SHOULDER_HORIZEN]=g_angle[g_readyBufferIndex][ROBOT_LEFT_SHOULDER_HORIZEN];
  188. g_diffAngle[PIN_GROUP_1][ROBOT_PIN_INDEX_LEFT_ELBOW]=g_angle[g_readyBufferIndex][ROBOT_LEFT_ELBOW];
  189. g_diffAngle[PIN_GROUP_1][ROBOT_PIN_INDEX_RIGHT_SHOULDER_VERTICAL]=g_angle[g_readyBufferIndex][ROBOT_RIGHT_SHOULDER_VERTICAL];
  190. g_diffAngle[PIN_GROUP_1][ROBOT_PIN_INDEX_RIGHT_SHOULDER_HORIZEN]=g_angle[g_readyBufferIndex][ROBOT_RIGHT_SHOULDER_HORIZEN];
  191. g_diffAngle[PIN_GROUP_1][ROBOT_PIN_INDEX_RIGHT_ELBOW]=g_angle[g_readyBufferIndex][ROBOT_RIGHT_ELBOW];
  192. g_diffAngle[PIN_GROUP_1][ROBOT_PIN_INDEX_LEFT_HIP_VERTICAL]=g_angle[g_readyBufferIndex][ROBOT_LEFT_HIP_VERTICAL];
  193. g_diffAngle[PIN_GROUP_1][ROBOT_PIN_INDEX_RIGHT_HIP_VERTICAL]=g_angle[g_readyBufferIndex][ROBOT_RIGHT_HIP_VERTICAL];
  194. g_diffAngle[PIN_GROUP_2][ROBOT_PIN_INDEX_LEFT_HIP_HORIZEN]=g_angle[g_readyBufferIndex][ROBOT_LEFT_HIP_HORIZEN];
  195. g_diffAngle[PIN_GROUP_2][ROBOT_PIN_INDEX_LEFT_KNEE]=g_angle[g_readyBufferIndex][ROBOT_LEFT_KNEE];
  196. g_diffAngle[PIN_GROUP_2][ROBOT_PIN_INDEX_LEFT_ANKLE]=g_angle[g_readyBufferIndex][ROBOT_LEFT_ANKLE];
  197. g_diffAngle[PIN_GROUP_2][ROBOT_PIN_INDEX_LEFT_FOOT]=g_angle[g_readyBufferIndex][ROBOT_LEFT_FOOT];
  198. g_diffAngle[PIN_GROUP_2][ROBOT_PIN_INDEX_RIGHT_HIP_HORIZEN]=g_angle[g_readyBufferIndex][ROBOT_RIGHT_HIP_HORIZEN];
  199. g_diffAngle[PIN_GROUP_2][ROBOT_PIN_INDEX_RIGHT_KNEE]=g_angle[g_readyBufferIndex][ROBOT_RIGHT_KNEE];
  200. g_diffAngle[PIN_GROUP_2][ROBOT_PIN_INDEX_RIGHT_ANKLE]=g_angle[g_readyBufferIndex][ROBOT_RIGHT_ANKLE];
  201. g_diffAngle[PIN_GROUP_2][ROBOT_PIN_INDEX_RIGHT_FOOT]=g_angle[g_readyBufferIndex][ROBOT_RIGHT_FOOT];
  202. //復(fù)位舵機(jī)管腳索引
  203. g_diffAngleMask[PIN_GROUP_1][ROBOT_PIN_INDEX_LEFT_SHOULDER_VERTICAL]=ROBOT_PIN_MASK_LEFT_SHOULDER_VERTICAL;
  204. g_diffAngleMask[PIN_GROUP_1][ROBOT_PIN_INDEX_LEFT_SHOULDER_HORIZEN]=ROBOT_PIN_MASK_LEFT_SHOULDER_HORIZEN;
  205. g_diffAngleMask[PIN_GROUP_1][ROBOT_PIN_INDEX_LEFT_ELBOW]=ROBOT_PIN_MASK_LEFT_ELBOW;
  206. g_diffAngleMask[PIN_GROUP_1][ROBOT_PIN_INDEX_RIGHT_SHOULDER_VERTICAL]=ROBOT_PIN_MASK_RIGHT_SHOULDER_VERTICAL;
  207. g_diffAngleMask[PIN_GROUP_1][ROBOT_PIN_INDEX_RIGHT_SHOULDER_HORIZEN]=ROBOT_PIN_MASK_RIGHT_SHOULDER_HORIZEN;
  208. g_diffAngleMask[PIN_GROUP_1][ROBOT_PIN_INDEX_RIGHT_ELBOW]=ROBOT_PIN_MASK_RIGHT_ELBOW;
  209. g_diffAngleMask[PIN_GROUP_1][ROBOT_PIN_INDEX_LEFT_HIP_VERTICAL]=ROBOT_PIN_MASK_LEFT_HIP_VERTICAL;
  210. g_diffAngleMask[PIN_GROUP_1][ROBOT_PIN_INDEX_RIGHT_HIP_VERTICAL]=ROBOT_PIN_MASK_RIGHT_HIP_VERTICAL;
  211. g_diffAngleMask[PIN_GROUP_2][ROBOT_PIN_INDEX_LEFT_HIP_HORIZEN]=ROBOT_PIN_MASK_LEFT_HIP_HORIZEN;
  212. g_diffAngleMask[PIN_GROUP_2][ROBOT_PIN_INDEX_LEFT_KNEE]=ROBOT_PIN_MASK_LEFT_KNEE;
  213. g_diffAngleMask[PIN_GROUP_2][ROBOT_PIN_INDEX_LEFT_ANKLE]=ROBOT_PIN_MASK_LEFT_ANKLE;
  214. g_diffAngleMask[PIN_GROUP_2][ROBOT_PIN_INDEX_LEFT_FOOT]=ROBOT_PIN_MASK_LEFT_FOOT;
  215. g_diffAngleMask[PIN_GROUP_2][ROBOT_PIN_INDEX_RIGHT_HIP_HORIZEN]=ROBOT_PIN_MASK_RIGHT_HIP_HORIZEN;
  216. g_diffAngleMask[PIN_GROUP_2][ROBOT_PIN_INDEX_RIGHT_KNEE]=ROBOT_PIN_MASK_RIGHT_KNEE;
  217. g_diffAngleMask[PIN_GROUP_2][ROBOT_PIN_INDEX_RIGHT_ANKLE]=ROBOT_PIN_MASK_RIGHT_ANKLE;
  218. g_diffAngleMask[PIN_GROUP_2][ROBOT_PIN_INDEX_RIGHT_FOOT]=ROBOT_PIN_MASK_RIGHT_FOOT;
  219. }
  220. #definePWM_STEERING_ENGINE(group)
  221. {
  222. counter=STEERING_ENGINE_INIT_DELAY;
  223. for(i=0;i<8;++i)
  224. counter+=g_diffAngle[PIN_GROUP_##group][i];
  225. for(i=0;i<30;++i)
  226. {
  227. GROUP_##group##_CONTROL_PIN=0xFF;
  228. Delay10us(STEERING_ENGINE_INIT_DELAY);
  229. for(j=0;j<8;++j)
  230. {
  231. Delay10us(g_diffAngle[PIN_GROUP_##group][j]);
  232. GROUP_##group##_CONTROL_PIN&=~(g_diffAngleMask[PIN_GROUP_##group][j]);
  233. }
  234. Delay10us(STEERING_ENGINE_CYCLE-counter);
  235. }
  236. }
  237. voidmain()
  238. {
  239. UCHAR8i;
  240. UCHAR8j;
  241. UINT16counter;
  242. InitSerialPort();
  243. P1=0xFF;
  244. //初始化舵機(jī)角度
  245. for(i=0;i
  246. {
  247. g_angle[0][i]=45;
  248. g_angle[1][i]=45;
  249. }
  250. for(i=0;i
  251. for(j=0;j<8;++j)
  252. g_diffAngle[i][j]=0;
  253. FillDiffAngle();
  254. InitPwmPollint();
  255. while(1)
  256. {
  257. #ifdefDEBUG_PROTOCOL
  258. if(g_fillingBufferIndex)
  259. {
  260. P11=0;
  261. P10=1;
  262. }
  263. else
  264. {
  265. P11=1;
  266. P10=0;
  267. }
  268. if(g_swapBuffer)
  269. P12=0;
  270. else
  271. P12=1;
  272. #endif
  273. if(g_swapBuffer)
  274. {
  275. FillDiffAngle();
  276. g_swapBuffer=FALSE;
  277. InitPwmPollint();
  278. }
  279. PWM_STEERING_ENGINE(1)
  280. PWM_STEERING_ENGINE(2)
  281. }
  282. }



評(píng)論


技術(shù)專區(qū)

關(guān)閉