nikoaha / portfolio

0 stars 0 forks source link

Oliot (Objects) -course's robot project code example (Java) #3

Closed nikoaha closed 6 years ago

nikoaha commented 6 years ago

Main class activates the program's case system.

package REXA;

public class Main {

    public static void main(String[] args) {
        Case muuttuja = new Case();
        muuttuja.myCase();
    }

}
nikoaha commented 6 years ago

The case system. The robot does the corresponding action depending on the command the user gives.

package REXA;

import lejos.hardware.Button;
import lejos.hardware.lcd.LCD;
import lejos.hardware.port.SensorPort;
import lejos.hardware.sensor.EV3IRSensor;

public class Case {

    public void myCase() {

    EV3IRSensor infraredSensor = new EV3IRSensor(SensorPort.S1);
    InfraSensor controller = new InfraSensor(infraredSensor);
    Moottori moottori = new Moottori();
    Music musiikki = new Music();
    Ase ase = new Ase(moottori);
    RunAway runAwayy = new RunAway(moottori);

    controller.start();
    musiikki.startUp();

    while (!Button.ESCAPE.isDown()) {

        switch (controller.getCommand()) {
        case 1:
            LCD.clear();
            LCD.drawString("forward", 0, 2);
            moottori.forward();
            break;
        case 2:
            LCD.clear();
            LCD.drawString("turn left", 0, 2);
            moottori.turnLeft();
            break;
        case 3:
            LCD.clear();
            LCD.drawString("backward", 0, 2);
            moottori.backward();
            break;
        case 4:
            LCD.clear();
            LCD.drawString("turn right", 0, 2);
            moottori.turnRight();
            break;
        case 5:
            LCD.clear();
            LCD.drawString("Shoot!", 0, 2);
            ase.shootEr();
            musiikki.laugh();
            break;
        case 7:
            LCD.clear();
            LCD.drawString("Run you fool!", 0, 2);
            musiikki.warning();
            runAwayy.runAway();
            break;
        case 8:
            LCD.clear();
            LCD.drawString("Knife!", 0, 2);
            ase.knife();
            musiikki.laugh();
            break;
        case 0:
            LCD.clear();
            LCD.drawString("stop", 0, 2);
            moottori.stopMotor();
            break;

        }
        LCD.refresh();

    }
    infraredSensor.close();
    moottori.closeMotor();

    }
}
nikoaha commented 6 years ago

For example, here is the program for using the robot's weapons. When giving the command to use either of the weapons, the weapon's motor is activated as long as the user commands to.

package REXA;

import lejos.robotics.RegulatedMotor;

public class Ase {

    private RegulatedMotor mD;
    private RegulatedMotor mC;

    public Ase(Moottori moottori) {
        mD = moottori.getmD();
        mC = moottori.getmC();
    }

    public void shootEr() {
        mD.setSpeed(700);
        mD.rotate(360);
    }

    public void knife() {
        mC.setSpeed(1500);
        mC.backward();
    }
}