|
楼主 |
发表于 2011-2-8 21:53:28
|
显示全部楼层
小车端的程序。比较简单,就判断按键,然后计算速度和偏离值,然后控制B/C两轴。- task main()
- {
- string in;
- char c[2];
- float sSpeed = 0; // Motor speed
- float sTurn = 0; // 0 for strait, negtive for left turn, positive for right turn
- float sSpeedStep = 5;
- float sTurnStep = 0.1;
-
- float sSpeedLeft = 0;
- float sSpeedRight = 0;
-
- while(true)
- {
- ReceiveRemoteString(MAILBOX1, true, in);
- if (strlen(in)>1)
- {
- StrToByteArray(in, c);
- switch (c[0])
- {
- case 'X':
- sSpeed = 0;
- sTurn = 0;
- break;
- case 'W':
- sSpeed = sSpeed + sSpeedStep;
- if (sSpeed > 100) sSpeed = 100;
- break;
- case 'S':
- sSpeed = sSpeed - sSpeedStep;
- if (sSpeed < -100) sSpeed = -100;
- break;
- case 'D':
- if (sTurn < 0) sTurn = 0;
- sTurn = sTurn + sTurnStep;
- if (sTurn > 1) sTurn = 1;
- break;
- case 'A':
- if (sTurn > 0) sTurn = 0;
- sTurn = sTurn - sTurnStep;
- if (sTurn < -1) sTurn = -1;
- break;
- default:
- break;
- }
- if (sSpeed > 0)
- {
- sSpeedLeft = sSpeed + sSpeed*sTurn;
- sSpeedRight = sSpeed - sSpeed*sTurn;
- if (sSpeedLeft > 100) sSpeedLeft = 100;
- if (sSpeedLeft < 0) sSpeedLeft = 0;
- if (sSpeedRight > 100) sSpeedRight = 100;
- if (sSpeedRight < 0) sSpeedRight = 0;
- OnFwd(OUT_B, sSpeedLeft);
- OnFwd(OUT_C, sSpeedRight);
- }
- else
- {
- sSpeedLeft = -1*(sSpeed + sSpeed*sTurn);
- sSpeedRight = -1*(sSpeed - sSpeed*sTurn);
- if (sSpeedLeft > 100) sSpeedLeft = 100;
- if (sSpeedLeft < 0) sSpeedLeft = 0;
- if (sSpeedRight > 100) sSpeedRight = 100;
- if (sSpeedRight < 0) sSpeedRight = 0;
- OnRev(OUT_B, sSpeedLeft);
- OnRev(OUT_C, sSpeedRight);
- }
- }
- // Erase the information on screen...
- TextOut(10, LCD_LINE2, " ");
- TextOut(10, LCD_LINE3, " ");
- TextOut(10, LCD_LINE2, in);
- NumOut(10, LCD_LINE3, sSpeed);
- Wait(25);
- }
- }
复制代码 |
|