// RobotMonitor.java // // A program that displays robot state on the LCD screen, // based on the original code by Simon Parsons & Davide Grossi, 2013. // // Terry Payne // 6th October 2017 // // This demo samples the different sensors available from the class // SimpleRobot, and provides updates in a separate thread on the LCD // display of the robot. import java.text.DecimalFormat; import lejos.hardware.ev3.LocalEV3; import lejos.hardware.lcd.Font; import lejos.hardware.lcd.GraphicsLCD; public class RobotMonitor extends Thread { private int delay; public SimpleRobot robot; GraphicsLCD lcd = LocalEV3.get().getGraphicsLCD(); // Make the monitor a daemon and set // the robot it monitors and the delay public RobotMonitor(SimpleRobot r, int d){ this.setDaemon(true); delay = d; robot = r; } // The monitor writes various bits of robot state to the screen, then // sleeps. public void run(){ // The decimalformat here is used to round the number to three significant digits DecimalFormat df = new DecimalFormat("####0.000"); while(true){ lcd.clear(); lcd.setFont(Font.getDefaultFont()); lcd.drawString("Robot Monitor", lcd.getWidth()/2, 0, GraphicsLCD.HCENTER); lcd.setFont(Font.getSmallFont()); lcd.drawString("LBump: "+robot.isLeftBumpPressed(), 0, 20, 0); lcd.drawString("RBump: "+robot.isRightBumpPressed(), 0, 30, 0); lcd.drawString("Dist: "+robot.getDistance(), 0, 40, 0); lcd.drawString("Colour: ["+ df.format(robot.getColour()[0]) +" "+ df.format(robot.getColour()[1]) +" "+ df.format(robot.getColour()[2]) +"]", 0, 50, 0); lcd.drawString("Lmotor: "+robot.isLeftMotorOn(), 0, 60, 0); lcd.drawString("Rmotor: "+robot.isRightMotorOn(), 0, 70, 0); try{ sleep(delay); } catch(Exception e){ // We have no exception handling ; } } } }