我正在使用声纳传感器来创建一个避障机器人。机器人需要能够检查它前面的 180 度,所以我做了一个“头”,传感器安装在这个“头”上,连接到一个电机上,电机的轴穿过一个电位计。我找到了 5 组 45 度间隔的电位计值,总共 180 度需要并记录在案。声纳传感器必须能够扫描一段距离然后移动 45 度,重复该过程直到它到达 180 度(向右),只有当它到达那个旋转点时,扫描距离被放入一个阵列中以供使用稍后将开发的回避任务。但是,声纳传感器会在头部实际达到特定角度之前存储特定角度的值。 避障机器人(中间头部旋转系统)
#pragma config(Sensor, in1, headrot, sensorPotentiometer)
#pragma config(Sensor, dgtl1, fsonar, sensorSONAR_inch)
#pragma config(Sensor, dgtl3, bsonar, sensorSONAR_inch)
#pragma config(Sensor, dgtl5, steerrot, sensorQuadEncoder)
#pragma config(Sensor, dgtl7, wheelrot, sensorQuadEncoder)
#pragma config(Motor, port2, head, tmotorVex393_MC29, openLoop)
#pragma config(Motor, port4, drivem, tmotorVex393_MC29, openLoop)
#pragma config(Motor, port5, steer, tmotorVex393_MC29, openLoop)
int headrotationspeed = 25;
int frontscandistance[5]; //Array that hold each of the 5 angles at 45 degree intervals
int headangle =0;
task avoidance()
{
}
task frontscanner()
{
//0 Degrees = 3500
//45 Degrees = 2800
//90 Degrees = 1900
//135 Degrees = 1200
//180 Degrees = 530
while(true)
{
switch(headangle)
{
case 0:
while(SensorValue[headrot]<3500 && SensorValue[headrot]>3400)
{
motor[head]=-headrotationspeed;
}
motor[head]=0;
frontscandistance[0] = SensorValue[fsonar];
headangle+=45;
break;
case 45:
while(SensorValue[headrot]<2800 && SensorValue[headrot]<2700)
{
motor[head]=headrotationspeed;
}
motor[head]=0;
frontscandistance[1] = SensorValue[fsonar];
headangle+=45;
break;
case 90:
while(SensorValue[headrot]<1900 && SensorValue[headrot]<1800)
{
motor[head]=headrotationspeed;
}
motor[head]=0;
frontscandistance[2] = SensorValue[fsonar];
headangle+=45;
break;
case 135:
while(SensorValue[headrot]<1200 && SensorValue[headrot]<1100)
{
motor[head]=headrotationspeed;
}
motor[head]=0;
frontscandistance[3] = SensorValue[fsonar];
headangle+=45;
break;
case 180:
while(SensorValue[headrot]<550 && SensorValue[headrot]<440)
{
motor[head]=headrotationspeed;
}
motor[head]=0;
frontscandistance[4] = SensorValue[fsonar];
headangle=0;
break;
}
}
}
task main()
{
startTask(frontscanner);
}
即使编程看起来正确,一旦头部分别达到每个 45 度间隔,声纳就不会扫描。是什么导致数组在 while 循环完成将头部定位到正确角度之前存储值?