gpt4 book ai didi

c - 如何让我的机器人沿着黑色胶带沿着矩形路径移动?

转载 作者:太空狗 更新时间:2023-10-29 15:18:44 25 4
gpt4 key购买 nike

我正在研究一个机器人,它是我们学院夏季机器人研讨会的一部分。我们正在使用 A-WIT 的 C-STAMP 微 Controller 。我能够让它移动,左转,右转,向后移动。我什至设法使用对比传感器让它沿着黑色胶带移动。

我让机器人以 30-45 度角朝向 table 上的黑色胶带,它自行对齐并开始沿着黑色胶带移动。它有点抽搐,可能是由于我下面的编程逻辑,它正在运行一个 while 循环并不断检查 if 语句,所以它最终会每隔几毫秒尝试左右转动一次,这就解释了抽搐的部分。但是没关系,它可以工作,虽然没有我想要的那么流畅但是它可以工作!问题是我不能让我的机器人进入黑色胶带的矩形路径。一旦到达黑色胶带的拐角,它就会继续直行,而不是左转或右转。

我的 2 个传感器位于机器人正下方,靠近前轮,几乎与地板齐平。它具有从 0 到 8 的“索引”值。八是最亮的对比度,零是最暗的对比度。因此,当机器人进入黑带区域时,索引值下降,基于此,我有一个 if 语句告诉我的机器人向左转或向右转。

这是我的尝试。为避免混淆,我没有发布完整的源代码,而只发布了负责我的机器人沿着黑色胶带移动的逻辑部分。

while(1) {

// don't worry about these.
// 10 and 9 represent Sensor's PIN location on the motherboard
V = ANALOGIN(10, 1, 0, 0, 0);
V2 = ANALOGIN(9, 1, 0, 0, 0);

// i got this "formula" from the example in my Manual.
// V stands for voltage of the sensor.
// it gives me the index value of the sensor. 0 = darkest, 8 = lightest.
index = ((-(V - 5) / 5) * 8 + 0.5);
index2 = ((-(V2 - 5) / 5) * 8 + 0.5);

// i've tweaked the position of the sensors so index > 7 is just right number.
// the robot will move anywhere on the table just fine with index > 7.
// as soon as it drops to or below 7 (i.e. finds black tape), the robot will
// either turn left or right and then go forward.

// lp & rp represent left-wheel pin and right-wheel pin, 1 means run forever.
// if i change it from 1 to 100, it will go forward for 100ms.
if (index > 7 && index2 > 7)
goForward(lp, rp, 1);

if (index <= 7) {
turnLeft(lp, rp, 1);
goForward(lp, rp, 1);
// this is the tricky part. i've added this code last minute
// trying to make my robot turn, but i didn't work.
if (index > 4) {
turnLeft(lp, rp, 1);
goForward(lp, rp, 1);
}
}

else if (index2 <= 7) {
turnRight(lp, rp, 1);
goForward(lp, rp, 1);
// this is also the last minute addition. it's same code as above
// but it's for the 2nd sensor.
if (index2 > 4) {
turnRight(lp, rp, 1);
goForward(lp, rp, 1);
}
}
}

我花了一整天的时间试图弄明白。我几乎用尽了所有途径。在 stackoverflow 上寻求解决方案是我现在最后的选择。

提前致谢!如果您对代码有任何疑问,请告诉我,但注释应该是不言自明的。

这是我的 goForward 函数,以防有人疑惑:

void goForward(BYTE lp, BYTE rp, WORD t)
{
WORD i;

for(i = 0; i < t; i = i + 1){
PULSOUT(lp, 400, 1, 1);
PULSOUT(rp, 800, 1, 1);
PAUSE(17);
}
}

更新:这是我到目前为止的想法。我删除了之前发布的所有 if 语句,并决定从头开始编写逻辑:

// if there's enough bright light in both sensors at the same time
// robot will move forward forever.
if (index > 7 && index2 > 7)
goForward(lp, rp, 1);

// but if there's not enough bright light anymore (i.e. reached black tape)
// proceed to the else-statement.
else {
// if left sensor detects the black tape then turn right
// if doesn't detect the black tape then keep going forward
if (index2 <= 7)
turnRight(lp, rp, 1);
else
goForward(lp, rp, 1);

// if right sensor detects the black tape then turn left
// if it doesn't detect the black tape then keep going forward
if (index <= 7)
turnLeft(lp, rp, 1);
else
goForward(lp, rp, 1);
}

// The reason for turnLeft and turnRight is to keep robot re-alligning
// to the black tape. It will happen so fast (every 1ms) that the jerking
// shouldn't even be noticeable.

最佳答案

你需要捕捉突然出现的情况:[cycle n]“我看到磁带”-> [cycle n+1]“我没有看到磁带”当你遇到一个角落时发生。

A state machine是处理此类问题的好方法。用state machine特定状态的代码是隔离的,它仅在状态为真时运行。这种隔离可防止“串扰”并为每个状态提供一个已知代码块。

给定您的示例,流程可能类似于:

:Loop

State == Moving forward and on tape.
read sensors
delta = abs(valueLastCycle - valueThisCycle);
if (delta >= bigChangeValue){
// the tape was just gone.
// change state and handle this situation.
CurrentState = suddenValueChange;
}else
if (index <= 7) {
turnLeft(lp, rp, 1);
goForward(lp, rp, 1);
else if (index2 <= 7) {
turnRight(lp, rp, 1);
goForward(lp, rp, 1);
}
...
State == suddenValueChange
...
code that handles sudden value change event
maybe:
Stop Robot;
Move backwards slowly until on tape or distance x
etc...

Goto Loop

提高扫描速度似乎有帮助……但机器人移动得越快,您的扫描速度就越快……即您可能仍会从磁带上跳转 -> 磁带外,在这种情况下,您当前的代码会出现问题。

关于c - 如何让我的机器人沿着黑色胶带沿着矩形路径移动?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3019535/

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