RobotC 声纳传感器阵列

RobotC Sonar Sensor Array

我正在使用声纳传感器创建一个避障机器人。机器人需要能够检查它前面的 180 度,所以我制作了一个 "head" 传感器安装在上面,它连接到电机上,电机的轴穿过电位器。我找到了 5 组 45 度间隔的电位器值,总共需要并记录了 180 度。声纳传感器必须能够扫描一段距离,然后移动 45 度,重复该过程,直到达到 180 度(向右),只有在到达那个旋转点后,扫描距离才会被放入一个数组中,以供回避任务将在以后开发。 但是,声纳传感器会在头部实际到达特定角度之前存储特定角度的值。 Obstacle Avoidance Robot (Head rotation system in the middle)

  #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 的特定问题,请随时直接给我发消息。