gpt4 book ai didi

c++ - RobotC - 电梯编程

转载 作者:塔克拉玛干 更新时间:2023-11-03 01:29:30 28 4
gpt4 key购买 nike

我正在为一个高中项目设计和编程一个类似电梯的机器人。我能做点什么让这更简单吗?或更好?我附上了我在 AutoCAD Inventor 中制作的带有标签的设计图片。

enter image description here

对于那些不熟悉 RobotC 或 VEX(它与 C 和 C++ 非常相似)的人:限位开关(limit1、limit2、...)和碰撞开关(floor1、floor2、...)是模拟按钮和如果未按下则返回值 0,如果按下则返回 1。电机(主电机)旋转齿轮,使机构在 slider 上向上移动。当伸出电机机构的轴上下移动时,它会按下限位开关并使其返回值 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是电机的转速,只是为了说的清楚一点。

如有任何问题,请随时提出。

最佳答案

让我们定义电梯在给定时刻的状态:

电梯可以上行下行空闲

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);
}

注意:在这段代码中,电梯只会在电梯空闲时处理新的上下楼层调用。它在移动时不存储上行和下行调用,稍后再去那里。我不知道这是否是您的要求。

关于c++ - RobotC - 电梯编程,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19883220/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com