首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >RobotC声纳传感器阵列

RobotC声纳传感器阵列
EN

Stack Overflow用户
提问于 2017-09-27 18:54:30
回答 1查看 292关注 0票数 0

我正在使用声纳传感器创造一个避障机器人。机器人需要能够检查它前面的180度,所以我做了一个“头”,传感器安装在一个带有轴的马达上,轴通过电位器。我已经找到了5套45度间隔的电位器值,总共需要180度,并记录在案。声纳传感器必须能够扫描一段距离,然后移动45度,重复这个过程,直到它达到180度(向右),只有当它到达那个旋转点时,扫描距离才会被放入一个阵列中,以供将来开发的回避任务使用。然而,声纳传感器在头部实际达到指定角度之前存储特定角度的值。避障机器人(中间的头部旋转系统)

代码语言:javascript
复制
  #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度间隔,即使编程似乎正确。是什么原因导致数组在What循环完成将头部定位到适当的角度之前存储值?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-06-13 21:19:20

首先,你的压痕取消了。这不是很好的实践,因为它会导致在循环中无意中放置代码块。

为了回答您的问题,在执行第一种情况后,所有其他while循环都在检查该值是否小于您的上限和下限。第一种情况不是这样的。

下面是您的代码,但是实现了上面提到的更改。

代码语言:javascript
复制
#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);
}

此外,我想指出的是,你的代码是为了转动机器人的头,只有当测量是在期望的范围内。如果在头部超出所需范围时执行任何一种情况,则头部不会移动。如果我理解正确,你想要的是相反的。

下面的代码包含了必要的修改,不仅可以在头部超出所需范围的情况下将头部旋转,而且还可以自动将头部转向所需的方向。

代码语言:javascript
复制
#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的特定问题,可以直接给我发短信。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/46455274

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档