㈠ c語言設計機器人運動軌跡程序……期末急需!!!
幾個問題:
1.機器人 電機轉速有反饋嗎?應該可以知道什麼時候碰倒障礙物的吧?
2.是不是必須通過黃線找到紅色區域啊?能不能直接找紅色區域?
3.機器人肯定有兩個motor的把,左右實現轉向對吧?
運動軌跡 應該是找到黃線後,機器人的動作只有兩種 帶速左轉右轉,當前位置在黃線上,那麼右轉,轉到黑色區域左轉;同理左轉轉到黑色區域就右轉,最後實現動作像蛇一樣沿著黃線前進。速度是根據電機前進方向的速度決定的,但是如果光感采樣頻率太低,電機速度太快有可能甩出黃線區域。
這個邏輯用C應該很容易實現,難點是怎麼快速找到紅色區域。
㈡ 機器人 C語言
這樣寫吧,清楚一點。
我覺得我這樣定義的話會讓程序清楚很多。
typedef struct
{
int x;
int y;
} pos_stru;
void main(void)
{
int cmd;
int face[4]={0,1,2,3}; //x+,y+,x-,y- 定義行進方向
int face_start=face[0]; //初始方向
pos_stru start={0,0}; //開始位置
while(scanf("%d",&cmd)!=NULL)
{
if(cmd==0)
break;
if(cmd<-2) //錯誤處理
{
printf("Invide input.");
}
switch(cmd) //先處理方向問題-1/-2
{
case -1: //左轉
if(face_start<3)
face_start=face[face_start+1];
else
face_start=face[0];
break;
case -2: //右轉
if(face_start>0)
face_start=face[face_start-1];
else
face_start=face[3];
break;
default:
break;
}
if(cmd>0) //不是方向的處理
switch(face_start)
{
case 0:
start.x+=cmd;
break;
case 1:
start.y+=cmd;
break;
case 2:
start.x-=cmd;
break;
case 3:
start.y-=cmd;
break;
default:
break;
}
//printf("tmp out:x=%d,y=%d\n",start.x,start.y);
}
printf("result:x=%d,y=%d\n",start.x,start.y);
}
out put:
-1
2
-2
1
0
result:x=1,y=2
Press any key to continue
問題補充:
沒給定義應該是都初始化為0了吧~
答:不對。隨機的這也是我們要求C語言必須初值的原因。下面是試驗
void main(void)
{
int a;
int b=0;
printf("a=%d,b=%d\n",a,b);
}
output:
a=-858993460,b=0
Press any key to continue
㈢ c語言問題,一個機器人只在橫坐標上移動,要求輸入開始坐標,結束坐標,移動的步驟,輸出機器人最終停止
#include <stdio.h>
#include <conio.h>
void main( )
{
int StartPoint, EndPoint, move[1000], CurrentPoint, i;
printf("Enter starting address: ");
scanf("%d", &StartPoint);
printf("Enter ending address: ");
scanf("%d", &EndPoint);
while(EndPoint < StartPoint)
{
printf(" Error! ending address must be >= %d ", StartPoint);
printf("Enter starting address: ");
scanf("%d", &EndPoint);
}
printf("Enter values to move now: ");
i = 0;
scanf("%d", &move[i]);
while(move[i] != 0) scanf("%d", &move[++i]);
for(CurrentPoint = StartPoint, i = 0; move[i]; i++)
{
CurrentPoint += move[i];
if(CurrentPoint > EndPoint) CurrentPoint = EndPoint;
if(CurrentPoint < StartPoint) CurrentPoint = StartPoint;
}
printf(" Final location: %d", CurrentPoint);
getch( ):
}
運行結果
㈣ c語言問題,一個機器人只在橫坐標上移動,要求輸入開始坐標,結束坐標,移動的步驟,輸出機器人最終停止
看到樓主的修改後的代碼,我給出一些修改意見
#include <stdio.h>
/*#include <math.h>*/ /*這行你是將conio.h改為math.h,其實這兩個都可刪去,只要不用getch*/
int main( ) /*將返回類型由void改為int,可以*/
{
int StartPoint, EndPoint, CurrentPoint, move; /*為了可讀性,將你的i改為move了*/
printf("Enter starting address: ");
scanf("%d", &StartPoint);
printf("Enter ending address: ");
scanf("%d", &EndPoint);
while(EndPoint < StartPoint)
{
printf("\nError! ending address must be >= %d\n\n", StartPoint);
printf("Enter ending address: "); /*你的這句改得好*/
scanf("%d", &EndPoint);
}
printf("Enter values to move now: ");
/* i = 0; //這里的連續9行有問題,將其修改後見下面的注。
scanf("%d", &i);
while(i != 0) scanf("%d", &i);
for(CurrentPoint = StartPoint, i = 0; move[i]; i++)
{
CurrentPoint += i;
if(CurrentPoint > EndPoint) CurrentPoint = EndPoint;
if(CurrentPoint < StartPoint) CurrentPoint = StartPoint;
} */
printf("\nFinal location: %d", CurrentPoint);
return (0); /*如果返回類型是int,則需要這行,如果是void,則不需這行*/
}
註:
CurrentPoint = StartPoint;
scanf("%d", &move);
while(move != 0)
{
CurrentPoint += move;
if(CurrentPoint > EndPoint) CurrentPoint = EndPoint;
if(CurrentPoint < StartPoint) CurrentPoint = StartPoint;
scanf("%d", &i);
}
㈤ C語言控制機器人走路的程序
//設置左右馬達參數
void ZYMotor(unsigned char ZState, unsigned char ZSpeed, unsigned char YState, unsigned char YSpeed)
{
SetMotor(_MOTOR_ZuoMotor_, ZState, ZSpeed);
SetMotor(_MOTOR_YouMotor_, YState, YSpeed);
}
//確認各個埠的狀態
void ShiBieXian()
{
n=1;
XJ[1]= GetADScable(_SCABLEAD_HB1_);
XJ[2]= GetADScable(_SCABLEAD_HB2_);
XJ[3]= GetADScable(_SCABLEAD_HB3_);
XJ[4]= GetADScable(_SCABLEAD_HB4_);
XJ[5]= GetADScable(_SCABLEAD_HB5_);
while(n<=5)
{
if(XJ[n]>FZ[n])
{HB[n]=0;}
else
{HB[n]=1;}
n++;
}
}
void GoOnLine()
{
if(HB[2]&&HB[3]&&HB[4])
{ZYMotor(0,60,0,60);}
else
{
if(HB[2]&&HB[3])
{ZYMotor(2,20,0,80);}
else
{
if (HB[2])
{ZYMotor(2,50,0,80);}
else
{
if(HB[3]&&HB[4] )
{ZYMotor(0,80,2,20);}
else
{
if(HB[4])
{ZYMotor(0,80,2,50);}
else
{
if (HB[1]&&!HB[2]&&!HB[3]&&!HB[4]&&!HB[5])
{ZYMotor(2, 80, 0, 80);}
else
{
if (!HB[1]&&!HB[2]&&!HB[3]&&!HB[4]&&HB[5])
{ZYMotor(0, 80, 2, 80);}
else
{ZYMotor(0, 60, 0, 60);}
}
}
}
}
}
}
}