读书人

The robot API amp;amp;How to add and use

发布时间: 2013-01-23 10:44:50 作者: rapoo

The robot API &&How to add and use sensor.

The robot API

Robot的状态API

Robot的状态可以可通过访问以下继承于Agentclas的方法得到:

*boolean collisionDetected():如果agent碰撞到物体,返回true.

*int getCounter():返回模拟器已经执行难过的次数.

*double getOdometer():返回agents里程表的数据,即agents走了多少米.

*double getLifeTime():返回agent的存在多少”虚拟秒”

*void getCoords(Point3d coord):返回坐标,米为单位.


Robot的运动API

可以通过指定旋转速度和平均速度来控制robot的运动.

* void setRotationalVelocity(double rv):设置旋转速度,单位是弧度/秒.

* void setTranslationalVelocity(double tv):设置平均速度,单位是米/秒.

*double getRotationalVelocity():获取旋转速度,单位是弧度/秒.

*double getTranslationalVelocity():获取平均速度,单位是米/秒.

*void moveToStartPosition():把robot放置回它开始的位置


传感器

你能为robot装备以下传感器:

*声纳定位器(Sonars)

*缓冲器(Bumpers)

*摄像头(Camera)

*光线传感器(Lightsensor)


RobotFactoryclass提供的方法能容易地添加上述的传感器.


添加和使用缓冲器传感器

在使用缓冲器之前必须先添加进robot.可以在robot的构造函数里面使用RobotFactory类绑定RangeSensorBelt对象.下面的例子为robot添加8个声纳传感器.每个声纳传感器一定范围内的距离信息以及碰撞状态,当在声纳范围内检测到物体返回true.能通过方法hasHit和getMeasurement单独访问每个声纳传感器.下面的例子每20帧打印出所有声纳传感器的状态.


建立robot类MyRobot.java(也可是在之前的MyRobot基础上添加缓冲器)

代码如下:


public class MyRobot extends Agent {     RangeSensorBelt sonars;     public Robot(Vector3d position, String name) {         super(position, name);         sonars = RobotFactory.addSonarBeltSensor(this,8);  //添加8个声纳传感器     }    public void performBehavior() {        //every 20 frames        if (getCounter()%20==0){             //每20帧输出声纳传感器的状态            // print each sonars measurement            for (int i=0;i< sonars.getNumSensors();i++) {                double range = sonars.getMeasurement(i);                 double angle = sonars.getSonarAngle(i);                boolean hit = sonars.hasHit(i);                System.out.println("Sonar at angle "+ angle +                "measured range ="+range+ " has hit something:"+hit);             }        }    }}
编译:javac -classpath simbad.jar  MyProg.java MyEnv.java  MyRobot
运行:java -classpath simbad.jar:. MyProg
截图如下:
The robot API &&How to add and use sensor
The robot API &&How to add and use sensor
The robot API &&How to add and use sensor
 

读书人网 >其他相关

热点推荐