Skip to content

Instantly share code, notes, and snippets.

@maxandersen
Created December 24, 2020 00:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save maxandersen/10da67ef2967f2ef071bad385ff10d86 to your computer and use it in GitHub Desktop.
Save maxandersen/10da67ef2967f2ef071bad385ff10d86 to your computer and use it in GitHub Desktop.
///usr/bin/env jbang "$0" "$@" ; exit $?
//REPOS jitpack
//DEPS com.github.ev3dev-lang-java:ev3dev-lang-java:2.4.16
package examples;
import ev3dev.actuators.lego.motors.EV3LargeRegulatedMotor;
import ev3dev.sensors.Battery;
import lejos.hardware.port.MotorPort;
import lejos.utility.Delay;
public class MyFirstRobot {
public static void main(final String[] args){
System.out.println("Creating Motor A & B");
final EV3LargeRegulatedMotor motorLeft = new EV3LargeRegulatedMotor(MotorPort.A);
final EV3LargeRegulatedMotor motorRight = new EV3LargeRegulatedMotor(MotorPort.B);
//To Stop the motor in case of pkill java for example
Runtime.getRuntime().addShutdownHook(new Thread(new Runnable() {
public void run() {
System.out.println("Emergency Stop");
motorLeft.stop();
motorRight.stop();
}
}));
System.out.println("Defining the Stop mode");
motorLeft.brake();
motorRight.brake();
System.out.println("Defining motor speed");
final int motorSpeed = 200;
motorLeft.setSpeed(motorSpeed);
motorRight.setSpeed(motorSpeed);
System.out.println("Go Forward with the motors");
motorLeft.forward();
motorRight.forward();
Delay.msDelay(2000);
System.out.println("Stop motors");
motorLeft.stop();
motorRight.stop();
System.out.println("Go Backward with the motors");
motorLeft.backward();
motorRight.backward();
Delay.msDelay(2000);
System.out.println("Stop motors");
motorLeft.stop();
motorRight.stop();
System.out.println("Checking Battery");
System.out.println("Votage: " + Battery.getInstance().getVoltage());
System.exit(0);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment