我正在使用声纳传感器来创建避障机器人。机器人需要能够检查它前面的 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 循环完成将头部定位到正确角度之前存储值?
首先,你的缩进已经关闭。这不是一个好的做法,因为它可能会导致无意中将代码块放置在循环内。
回答你的问题,在执行第一个情况后,所有其他 while 循环都会检查该值是否小于你的上限和下限。第一种情况不会这样做。
以下是您的代码,但已实施上述更改。
#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);
}
另外,我想指出,您的代码编写为仅当测量值处于所需范围内时才转动机器人的头部。如果任何情况在头部超出所需范围时执行,头部将不会移动。如果我理解正确的话,你想要的恰恰相反。
下面的代码包含必要的修改,不仅可以在超出所需范围时转动头部,还可以自动将头部转向所需方向。
#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;
int reverse = 1 //Set this to -1 if the motor is turning in the wrong direction.
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*reverse*(3500-SensorValue[headrot]/abs(3500-SensorValue[headrot]));
}
motor[head]=0;
frontscandistance[0] = SensorValue[fsonar];
headangle+=45;
break;
case 45:
while(SensorValue[headrot]<2800 && SensorValue[headrot]>2700)
{
motor[head]=headrotationspeed*reverse*(2800-SensorValue[headrot]/abs(3500-SensorValue[headrot]));
}
motor[head]=0;
frontscandistance[1] = SensorValue[fsonar];
headangle+=45;
break;
case 90:
while(SensorValue[headrot]<1900 && SensorValue[headrot]>1800)
{
motor[head]=headrotationspeed*reverse*(1900-SensorValue[headrot]/abs(3500-SensorValue[headrot]));
}
motor[head]=0;
frontscandistance[2] = SensorValue[fsonar];
headangle+=45;
break;
case 135:
while(SensorValue[headrot]<1200 && SensorValue[headrot]>1100)
{
motor[head]=headrotationspeed*reverse*(1200-SensorValue[headrot]/abs(3500-SensorValue[headrot]));
}
motor[head]=0;
frontscandistance[3] = SensorValue[fsonar];
headangle+=45;
break;
case 180:
while(SensorValue[headrot]<550 && SensorValue[headrot]>440)
{
motor[head]=headrotationspeed*reverse*(550-SensorValue[headrot]/abs(3500-SensorValue[headrot]));
}
motor[head]=0;
frontscandistance[4] = SensorValue[fsonar];
headangle=0;
break;
}
}
}
task main()
{
startTask(frontscanner);
}
此外,我强烈推荐 VEX 论坛(可在此处找到:https://www.vexforum.com/)以解决未来涉及 vex 的问题。它更适合您的需求。我的用户名是Tark1492。如果您遇到有关 RobotC 的具体问题,请随时直接给我发消息。