0

我正在使用声纳传感器来创建一个避障机器人。机器人需要能够检查它前面的 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 循环完成将头部定位到正确角度之前存储值?

4

1 回答 1

0

首先,您的缩进是关闭的。这不是好的做法,因为它可能会导致无意中将代码块放入循环中。

要回答您的问题,在第一个案例执行后,所有其他 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 的特定问题,请随时直接给我发消息。

于 2018-06-13T21:19:20.477 回答