【发布时间】:2011-03-02 11:07:16
【问题描述】:
我正在研究一个机器人,它是我们学院暑期机器人研讨会的一部分。我们正在使用 A-WIT 的 C-STAMP 微控制器。我能够让它移动,左转,右转,后退。我什至设法使用对比度传感器让它沿着黑色胶带移动。
我将机器人以 30-45 度角朝向桌子上的黑色胶带,它会自行对齐并开始沿着黑色胶带移动。它有点晃动,可能是由于我下面的编程逻辑,它正在运行一个 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 上寻求解决方案是我现在最后的选择。
提前致谢! 如果您对代码有任何疑问,请告诉我,但 cmets 应该是不言自明的。
这是我的 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.
【问题讨论】:
-
你试过chiphacker.com吗?