Search in sources :

Example 1 with VisionRay

use of nars.rover.obj.VisionRay in project narchy by automenta.

the class CarefulRover method initMotors.

protected void initMotors(Body torso) {
    nar.on(new NullOperator("vision") {

        @Override
        public List<Task> apply(Operation o) {
            final String command = command(o, o.getMemory());
            String[] sections = command.split(",");
            if (sections.length == 1) {
                // switch (sections[0]) {
                // case "far":
                // dist = 20f;
                // break;
                // case "near":
                // dist = 10f;
                // break;
                // default:
                // return null;
                // }
                String angle = sections[0];
                float dist;
                dist = 5 + 20f * o.getTask().getTruth().getExpectation();
                VisionRay vr = pixels.get(angle);
                if (vr != null)
                    vr.setDistance(dist);
            }
            return null;
        }
    });
    System.out.println(pixels);
    for (String a : pixels.keySet()) {
        randomActions.add("vision(" + a + ")! :!: %1%");
        randomActions.add("vision(" + a + ")! :|: %0%");
    }
    randomActions.add("motor(left)! :|:");
    // randomActions.add("motor(left,left)!");
    randomActions.add("motor(right)! :|:");
    // randomActions.add("motor(right,right)!");
    // randomActions.add("motor(forward,forward)!"); //too much actions are not good,
    // however i would agree if <motor(forward,forward) --> motor(forward)>.
    randomActions.add("motor(forward)! :|:");
    // randomActions.add("motor(forward,forward)!");
    // randomActions.add("motor(forward)!");
    randomActions.add("motor(reverse)! :|:");
    randomActions.add("motor(stop)! :|:");
    // randomActions.add("motor(random)!");
    nar.on(new NullOperator("motor") {

        @Override
        public List<Task> apply(Operation operation) {
            String command = command(operation, operation.getMemory());
            if (command.equals("$1")) {
                // variable causes random movement
                double v = Math.random();
                if (v < 0.25f) {
                    command = "left";
                } else if (v < 0.5f) {
                    command = "right";
                } else if (v < 0.75f) {
                    command = "forward";
                } else {
                    command = "reverse";
                }
            }
            float strength = operation.getTaskExpectation();
            float rotSpeed = 15 * strength;
            float linSpeed = 1 * strength;
            switch(command) {
                case "right":
                    rotateRelative(-rotSpeed);
                    break;
                case "right,right":
                    rotateRelative(-rotSpeed * 2);
                    break;
                case "left":
                    rotateRelative(+rotSpeed);
                    break;
                case "left,left":
                    rotateRelative(+rotSpeed * 2);
                    break;
                case "forward,forward":
                    thrustRelative(+2 * linSpeed);
                    break;
                case "forward":
                    thrustRelative(+linSpeed);
                    break;
                case "reverse":
                    thrustRelative(-linSpeed);
                    break;
                case "stop":
                    stop();
                    break;
                case "random":
                    randomAction();
                    break;
            }
            return null;
        }
    });
}
Also used : VisionRay(nars.rover.obj.VisionRay) ArrayList(java.util.ArrayList) List(java.util.List) NullOperator(nars.nal.nal8.operator.NullOperator)

Example 2 with VisionRay

use of nars.rover.obj.VisionRay in project narchy by automenta.

the class CarefulRover method initSensors.

protected void initSensors(Body torso) {
    Vec2 center = new Vec2(0, 0);
    float da = (float) (Math.PI * 2 / numPixels);
    float a = 0;
    for (int i = 0; i < numPixels; i++) {
        VisionRay v = new VisionRay(this, torso, /*eats ?*/
        center, /*: new Vec2(0,0)*/
        a, da, 3, 10, 8) {

            @Override
            protected float getDistance() {
                return distance * visionDistanceFactor;
            }
        };
        pixels.put(v.angleTerm, v);
        draw.addLayer(v);
        senses.add(v);
        a += da;
    }
}
Also used : VisionRay(nars.rover.obj.VisionRay) Vec2(spacegraph.space2d.phys.common.Vec2)

Example 3 with VisionRay

use of nars.rover.obj.VisionRay in project narchy by automenta.

the class Rover method newTorso.

@Override
protected Body newTorso() {
    PolygonShape shape = new PolygonShape();
    Vec2[] vertices = { new Vec2(3.0f, 0.0f), new Vec2(-1.0f, +2.0f), new Vec2(-1.0f, -2.0f) };
    shape.set(vertices, vertices.length);
    // shape.m_centroid.set(bodyDef.position);
    BodyDef bd = new BodyDef();
    bd.linearDamping = (linearDamping);
    bd.angularDamping = (angularDamping);
    bd.type = BodyType.DYNAMIC;
    bd.position.set(0, 0);
    Body torso = getWorld().createBody(bd);
    Fixture f = torso.createFixture(shape, mass);
    f.setRestitution(restitution);
    f.setFriction(friction);
    // for (int i = -pixels / 2; i <= pixels / 2; i++) {
    for (int i = 0; i < retinaPixels; i++) {
        final int ii = i;
        final float angle = /*MathUtils.PI / 2f*/
        aStep * i;
        final boolean eats = ((angle < mouthArc / 2f) || (angle > (Math.PI * 2f) - mouthArc / 2f));
        // System.out.println(i + " " + angle + " " + eats);
        VisionRay v = new VisionRay(this, torso, /*eats ?*/
        mouthPoint, /*: new Vec2(0,0)*/
        angle, aStep, retinaRaysPerPixel, L, distanceResolution) {

            @Override
            public void onTouch(Body touched, float di) {
                if (touched == null)
                    return;
                if (touched.getUserData() instanceof Sim.Edible) {
                    if (eats) {
                        if (di <= biteDistanceThreshold)
                            eat(touched);
                    /*} else if (di <= tasteDistanceThreshold) {
                                //taste(touched, di );
                            }*/
                    }
                }
            }
        };
        v.sparkColor = new Color3f(0.5f, 0.4f, 0.4f);
        v.normalColor = new Color3f(0.4f, 0.4f, 0.4f);
        ((JoglAbstractDraw) draw).addLayer(v);
        senses.add(v);
    }
    return torso;
}
Also used : PolygonShape(spacegraph.space2d.phys.collision.shapes.PolygonShape) VisionRay(nars.rover.obj.VisionRay) Color3f(spacegraph.space2d.phys.common.Color3f) Vec2(spacegraph.space2d.phys.common.Vec2) Fixture(spacegraph.space2d.phys.dynamics.Fixture) BodyDef(spacegraph.space2d.phys.dynamics.BodyDef) Body(spacegraph.space2d.phys.dynamics.Body) JoglAbstractDraw(nars.rover.physics.gl.JoglAbstractDraw)

Aggregations

VisionRay (nars.rover.obj.VisionRay)3 Vec2 (spacegraph.space2d.phys.common.Vec2)2 ArrayList (java.util.ArrayList)1 List (java.util.List)1 NullOperator (nars.nal.nal8.operator.NullOperator)1 JoglAbstractDraw (nars.rover.physics.gl.JoglAbstractDraw)1 PolygonShape (spacegraph.space2d.phys.collision.shapes.PolygonShape)1 Color3f (spacegraph.space2d.phys.common.Color3f)1 Body (spacegraph.space2d.phys.dynamics.Body)1 BodyDef (spacegraph.space2d.phys.dynamics.BodyDef)1 Fixture (spacegraph.space2d.phys.dynamics.Fixture)1