问题描述
|
以两种方式遍历INT数组是一个机器人有趣的代码(用C语言编写)。
我有一个这样的职位数组:
int pos[] = {0,45,90,135,180,45};
这些位置用于移动伺服电机。
45 90 135
\\ | /
\\ | /
\\ | /
0 ----------- 180
在主菜单ѭ2中,我检查到障碍物的距离,如果距离为ѭ3,则我的伺服必须旋转到下一个步骤(下一个数组位置),直到找到自由路径(> xx Cm
)为止。
我的主要方法很简单:
int main (int argc,const char * argv[]) { for (;;) find(); }
而我的核心功能(查找)是这样的:
void find() {
for ( i=0; i<sizeof(pos); i++ ) // Traversing position array
{
distance = rand() % 7; // Simulate obstacle distance
move( pos[i] ); // Simulate movements
if (i==sizeof(pos)) { i=1; } // Try to reset the \"i\" counter. PROBLEM!
if ( distance<=5 ) continue; // Is there an obstacle?
sleep(2); // Debug sleep
find(); // Similar recursion
}
}
我不知道这段代码有什么问题,但是我需要移动伺服直到没有障碍。
例:
在位置90
,我发现一个障碍。我想从左到右循环数组,反之亦然,每一步都控制距离。如果我找不到高速公路,way8ѭ,否则print(\"ok\")
。
如何解决此代码以使其正常工作?
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)