使用 Roomba 在玩家舞台中进行迷宫导航

发布于 2024-08-28 08:18:38 字数 2226 浏览 4 评论 0原文

这是我的代码:

/*
Scott Landau
Robot Lab Assignment 1
*/    

// Standard Java Libs

import java.io.*;

// Player/Stage Libs

import javaclient2.*;
import javaclient2.structures.*;
import javaclient2.structures.sonar.*;

// Begin

public class SpinningRobot

{
    public static Position2DInterface pos = null;
    public static LaserInterface laser = null;

    public static void main(String[] args)
    {
        PlayerClient robot = new PlayerClient("localhost", 6665);
        laser = robot.requestInterfaceLaser(0, PlayerConstants.PLAYER_OPEN_MODE);
        pos = robot.requestInterfacePosition2D(0,PlayerConstants.PLAYER_OPEN_MODE);

        robot.runThreaded (-1, -1);

        pos.setSpeed(0.5f, -0.25f);

        // end pos
        float x, y;
        x = 46.0f;
        y = -46.0f;

        boolean done = false;

        while( !done ){

            if(laser.isDataReady()) { 

                float[] laser_data = laser.getData().getRanges();

                System.out.println("== IR Sensor ==");
                System.out.println("Left Wall Distance: "+laser_data[360]);
                System.out.println("Right Wall Distance: " +laser_data[0]);

                // if laser doesn't reach left wall, move to detect it
                // so we can guide using left wall
                if ( laser_data[360] < 0.6f ) {
                    while ( laser_data[360] < 0.6f ) {

                        pos.setSpeed(0.5f, -0.5f);
                    }
            } else if ( laser_data[0] < 0.6f ) {
                                                                                                   while(laser_data[0<0.6f)                                       {                                    pos.setSpeed(0.5f, 0.5f);
                                   }
            }
                pos.setSpeed(0.5f, -0.25f);



// end pos?
done = ( (pos.getX() == x) && (pos.getY() == y) );  
            }

        }

    }





} // End

我试图让 Roomba 连续以轻微的右曲线行驶,如果它用激光识别出它靠近的每堵墙,则快速转身离开。对于这个机器人,我只能使用 Laser_data[360] 和 Laser_data[0]。我认为这最终会穿越迷宫。

但是,我使用的是 Player Stage 平台,当使用此代码 Roomba 靠近墙壁时,Stage 会冻结,我不知道为什么。

另外,如果您能想到更好的迷宫导航算法,请告诉我。

谢谢你!

Here is my code:

/*
Scott Landau
Robot Lab Assignment 1
*/    

// Standard Java Libs

import java.io.*;

// Player/Stage Libs

import javaclient2.*;
import javaclient2.structures.*;
import javaclient2.structures.sonar.*;

// Begin

public class SpinningRobot

{
    public static Position2DInterface pos = null;
    public static LaserInterface laser = null;

    public static void main(String[] args)
    {
        PlayerClient robot = new PlayerClient("localhost", 6665);
        laser = robot.requestInterfaceLaser(0, PlayerConstants.PLAYER_OPEN_MODE);
        pos = robot.requestInterfacePosition2D(0,PlayerConstants.PLAYER_OPEN_MODE);

        robot.runThreaded (-1, -1);

        pos.setSpeed(0.5f, -0.25f);

        // end pos
        float x, y;
        x = 46.0f;
        y = -46.0f;

        boolean done = false;

        while( !done ){

            if(laser.isDataReady()) { 

                float[] laser_data = laser.getData().getRanges();

                System.out.println("== IR Sensor ==");
                System.out.println("Left Wall Distance: "+laser_data[360]);
                System.out.println("Right Wall Distance: " +laser_data[0]);

                // if laser doesn't reach left wall, move to detect it
                // so we can guide using left wall
                if ( laser_data[360] < 0.6f ) {
                    while ( laser_data[360] < 0.6f ) {

                        pos.setSpeed(0.5f, -0.5f);
                    }
            } else if ( laser_data[0] < 0.6f ) {
                                                                                                   while(laser_data[0<0.6f)                                       {                                    pos.setSpeed(0.5f, 0.5f);
                                   }
            }
                pos.setSpeed(0.5f, -0.25f);



// end pos?
done = ( (pos.getX() == x) && (pos.getY() == y) );  
            }

        }

    }





} // End

I was trying to have the Roomba go continuously at a slight right curve, quickly turning away from each wall it came to close to if it recognized it with it's laser. I can only use laser_data[360] and laser_data[0] for this one robot. I think this would eventually navigate the maze.

However, I am using the Player Stage platform, and Stage freezes when the Roomba comes close to a wall using this code, I have no idea why.

Also, if you can think of a better maze navigation algorithm, please let me know.

Thank you!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

云胡 2024-09-04 08:18:38

看起来就像你被困在墙上一样,因为你在距离 0.6 附近振荡。

例如:

  1. 向墙壁方向行驶,最终达到 << 0.6f
  2. 旋转远离墙壁,> 时停止0.6f。换句话说,0.6000001f(例如)
  3. 恢复正常轨迹。
  4. 几乎立即< 0.6f
  5. Goto 2

您将陷入稍微靠近或远离墙壁的角度。

你需要一个缓冲区。一旦距离墙壁 MIN_DIS 距离内,您需要旋转离开,直到距离墙壁达到某个 BUFFER_DIS,其中 BUFFER_DIS > 0。 MIN_DIS 相当大。

编辑:

您似乎遇到了一些问题。首先,您检查 Laser_data 是否靠近墙壁,如果是,则向墙壁旋转。您需要检查它是否不靠近墙壁,然后朝它旋转。

其次,您处于一个非常紧密的 while 循环中,调整轮速。这可能会导致不好的事情。当距离墙壁太远时,您需要调用 trackWall() 函数,该函数将在单独的循环中轮询 Laser.isDataReady() 。要么这样,要么您的主轮询循环中需要一个状态机。如果没有,您将毫无意义地使用陈旧的激光数据来调整轮速。

第三,你能保证墙体始终在0.6f以内吗?否则,如果你的机器人距离墙壁太远,它就会无限旋转。

It seems like you get stuck against a wall because you are oscillating around distance 0.6.

For instance:

  1. Drive angling toward wall, eventually reaching < 0.6f
  2. Rotate away from wall, stop when > 0.6f. In other words 0.6000001f (for instance)
  3. Resume normal trajectory.
  4. Almost immediately < 0.6f
  5. Goto 2

You will get stuck angling slightly toward and slightly away from the wall.

You need a buffer zone. Once you get within MIN_DIS distance from the wall you need to rotate away until you reach some BUFFER_DIS from the wall where BUFFER_DIS > MIN_DIS by a fair amount.

EDIT:

Looks like you have a few issues. First you are checking if the laser_data is close to a wall, and if it is you are rotating toward the wall. You need to check if it is NOT close to a wall, and rotate toward it.

Second, you are in a very tight while loop, adjusting wheel speeds. This will probably result in bad things. You need to call a trackWall() function when you are too far from the wall, which will poll laser.isDataReady() in a separate loop. Either that, or you need a state-machine in your main polling loop. If not, you are pointlessly adjusting wheel speed with stale laser data.

Third, can you guarantee that the wall will always be within 0.6f? If not, your robot will spin infinitely if it gets to far from the wall.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文