RobotC - 编程电梯

时间:2013-11-09 21:18:41

标签: c++ c robotics robot

我正在为高中项目设计和编程类似电梯的机器人。我可以做任何事情来使这更简单吗?或更好?我附上了我在AutoCAD Inventor中制作的带有标签的设计图片。

enter image description here

对于那些不熟悉RobotC或VEX(非常类似于C和C ++)的人:限位开关(limit1,limit2,...)和碰撞开关(floor1,floor2,...)是模拟按钮和如果未按下则返回值0,如果按下则返回1。电机(mainMotor)旋转齿轮,使机构在滑块上向上移动。当轴伸出电机机构上下移动时,它按下限位开关并使其返回值为1。

int callup [3];
int calldown [3];
int floorat[3];

    int main ()
    {

        if (SensorValue[limit1] == 1)
        {
            floorat[0] = 1;
        }
        else
        {
            floorat[0] = 0;
        }

        if (SensorValue[limit2] == 1)
        {
            floorat[1] = 1;
        }
        else
        {
            floorat[1] = 0;
        }

        if (SensorValue[limit3] == 1)
        {
            floorat[2] = 1;
        }
        else
        {
            floorat[2] = 0;
        }

        if (SensorValue[floor1] == 1)
        {
            calldown[0] = 1;
            SensorValue[LED1] = 1;
        }

        if (SensorValue[floor2] == 1 && floorat[2] == 1)
        {
            calldown[1] = 1;
            SensorValue[LED2] = 1;
        }

        if (SensorValue[floor2] == 1 && floorat[0] == 1)
        {
            callup[1] = 1;
            SensorValue[LED2] = 1;
        }

        if (SensorValue[floor3])
        {
            callup[2] = 1;
            SensorValue[LED3] = 1;
        }

        motors ();

    }


    void motors ()
    {

        if (callup[2] == 1 && floorat[2] == 1)
        {
            int x = 1;
            while (x < 3)
            {
                SensorValue[LED3] = 1;
                wait(0.5);
                SensorValue[LED3] = 0;
                wait(0.5);
            }
            callup[2] = 0;
            main ();
        }
        else if (callup[1] == 1 && floorat[1] == 1)
        {
            int x = 1;
            while (x < 3)
            {
                SensorValue[LED2] = 1;
                wait(0.5);
                SensorValue[LED2] = 0;
                wait(0.5);
            }
            callup[1] = 0;
            main ();
        }
        else if (callup[0] == 1 && floorat[0] == 1)
        {
            int x = 1;
            while (x < 3)
            {
                SensorValue[LED1] = 1;
                wait(0.5);
                SensorValue[LED1] = 0;
                wait(0.5);
            }
            callup[0] = 0;
            main ();
        }

        if (callup[2] == 1 && floorat[1] == 1 && calldown[0] == 0 || callup[2] == 1 && floorat[0] == 1 && callup[1] == 0)
        {
            startMotor(mainMotor, 60);
            untilTouch(limit3);
            stopMotor(mainMotor);
            callup[2] = 0;
            wait(1);
            main ();
        }

        if (callup[1] == 1 && floorat[0] == 1)
        {
            startMotor(mainMotor, 60);
            untilTouch(limit2);
            stopMotor(mainMotor);
            callup[1] = 0;
            wait(1);
            main();
        }

        if (calldown[1] == 1 && floorat[2] == 1)
        {
            startMotor(mainMotor, -60);
            untilTouch(limit2);
            stopMotor(mainMotor);
            calldown[1] = 0;
            wait(1);
            main();
        }

        if (calldown[0] == 1 && floorat[2] == 1 && calldown[1] == 0 || calldown[0] == 1 && floorat[1] == 1)
        {
            startMotor(mainMotor, -60);
            untilTouch(limit1);
            stopMotor(mainMotor);
            calldown[0] = 0;
            wait(1);
            main();
        }
    }

虽然这个问题不应该引起关注,但startMotor命令中的60是电机的速度,只是为了让它更清晰。

随意提出更多问题。

3 个答案:

答案 0 :(得分:3)

让我们来定义一个特定时刻电梯的状态:

电梯可以向上向下,或空闲

enter image description here

电梯处于给定的楼层,当它触发开关时从一层到另一层:

enter image description here

现在,如果我们将其翻译成一些伪代码(应该很容易翻译成RobotC):

enum elevator_status = { idle, down, up };
int currentfloor; //1, 2, 3 


switch(elevator_status)
{
    case idle:    
        //we check if a button is pressed and possibly go up or down           
        if(SensorValue(floor1))
        {         
             if(currentfloor > 1)
                elevator_status = down;               
        }
        else if(SensorValue(floor2))
        {
              if(currentfloor > 2)
                  elevator_status = down;
              else if(currentfloor < 2)
                  elevator_status = up;               
        }
        else if(SensorValue(floor3))
        {
              if(currentfloor < 3)
                  elevator_status = up;   
        }
        break;

    case up:
    case down:    
        //we check if we trigger a floor switch and stop the elevator
        if(SensorValue(limit1))
        {      
           currentfloor = 1;
           elevator_status = idle;
        }
        else if(SensorValue(limit2))
        {
           currentfloor = 2;
           elevator_status = idle;
        }
        else if(SensorValue(limit3))
        {
           currentfloor = 3;
           elevator_status = idle;
        }
        break;
}


//we set the speed of the motor
if(elevator_status == up)
{
   set_motorstate(cw);
)
else if(elevator_status == down)
{
   set_motorstate(ccw);   
}
else if(elevator_status == idle)
{
   set_motorstate(idle);   
}

注意:在此代码中,电梯只在电梯空闲时处理新的上下楼层呼叫。它在移动时不会存储上下呼叫,而是稍后再去。我不知道这是否是你的要求。

答案 1 :(得分:2)

我可能会离开,因为我只是一个有自己问题的学生,但看起来你的数组大小可能有误。例如,当您声明:

int floorat[2];

这使得数组大小为2.然后在这个数组[0,1,2]中引用3个元素位置。此外,您不能只使用常规整数,并为其赋值1,2或3?

我建议将这些变量重新定义为:

int callup;
int calldown;
int floorat;

然后,您可以避免额外的代码行并将if / else子句简化为:

if (SensorValue[limit1] == 1)
{
    floorat = 1;
}

if (SensorValue[limit2] == 1)
{
    floorat = 2;
}


if (SensorValue[limit3] == 1)
{
    floorat = 3;
}

答案 2 :(得分:2)

我不熟悉RobotC或VEX,但我注意到一定数量的复制操作可以用到自己的函数中。

以下代码片段我将分成不同的函数。因此,在称为电机的大型功能中,您可以进行以下操作:

    int x = 1;
    while (x < 3)
    {
        SensorValue[LED3] = 1;
        wait(0.5);
        SensorValue[LED3] = 0;
        wait(0.5);
    }
    callup[2] = 0;
    main ();

重复此值略有不同。

在这里,我将编写如下函数:

void adjust_sensors( size_t led, size_t level )
{
    int x = 1;
    while (x < 3)
    {
        SensorValue[led] = 1;
        wait(0.5);
        SensorValue[led] = 0;
        wait(0.5);
    }
    callup[level] = 0;
    main ();
}

您也可以对以下代码执行相同的操作:

startMotor(mainMotor, 60);
untilTouch(limit3);
stopMotor(mainMotor);
callup[2] = 0;
wait(1);
main ();

似乎while循环永远不会结束,因为x的值永远不会改变。

当您声明:

时,您的顶部也会出现拼写错误
int callown [2];

我认为你的意思是:

int calldown [2];

为了清晰起见,最好在代码中添加一些注释。

希望这有帮助。