Skip to content

Instantly share code, notes, and snippets.

@ajosephau
Last active August 29, 2015 14:20
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 ajosephau/35de84e202390a921c13 to your computer and use it in GitHub Desktop.
Save ajosephau/35de84e202390a921c13 to your computer and use it in GitHub Desktop.
Baseline LeJOS superoptimization experimentation code
import lejos.nxt.*;
public class BenchmarkTest
extends CommonNXTTests
{
public static void main( String[] args )
throws Exception
{
//variables
LightSensor lightSensor = new LightSensor( SensorPort.S1 );
int reading = 0;
int checkValue = ( lightSensor.getHigh() - lightSensor.getLow() ) / 2 + lightSensor.getLow();
//constants
final String appName = "Benchmark App";
calibrateLightSensors( appName, lightSensor );
calibrateMotors( Motor.B, Motor.C );
while ( !Button.LEFT.isPressed() )
{
reading = lightSensor.readNormalizedValue();
displayMainInformation( appName, lightSensor, reading, checkValue );
// superoptimised function start
if ( reading > ( ( lightSensor.getHigh() + lightSensor.getLow() ) / 2 ) ) { Motor.B.forward();Motor.C.forward(); } else { Motor.B.forward();Motor.C.stop(); }
// superoptimized function stop
}
}
}
import lejos.nxt.*;
public class CommonNXTTests
{
protected static int DEFINED_POWER = 67;
private static String EMPTY_STRING = "";
protected static void calibrateMotors( Motor bMotor, Motor cMotor )
{
bMotor.regulateSpeed( false );
cMotor.regulateSpeed( false );
bMotor.setPower( DEFINED_POWER );
cMotor.setPower( DEFINED_POWER );
Motor.B.forward();
Motor.C.forward();
}
protected static void calibrateLightSensors( String appName, LightSensor lightSensor ) throws Exception
{
while ( !Button.LEFT.isPressed() )
{
LCD.drawString( "AJ:" + appName, 0, 0 );
LCD.drawString( "Calibrate Black" + EMPTY_STRING, 0, 1 );
LCD.drawString( lightSensor.readNormalizedValue() + "", 0, 2 );
LCD.drawString( "Press LEFT Key" + EMPTY_STRING, 0, 3 );
LCD.refresh();
}
lightSensor.calibrateHigh();
pauseDisplay();
while ( !Button.LEFT.isPressed() )
{
LCD.drawString( "AJ:" + appName, 0, 0 );
LCD.drawString( "Calibrate White" + EMPTY_STRING, 0, 1 );
LCD.drawString( lightSensor.readNormalizedValue() + "", 0, 2 );
LCD.drawString( "Press LEFT Key" + EMPTY_STRING, 0, 3 );
LCD.refresh();
}
lightSensor.calibrateLow();
pauseDisplay();
}
private static void pauseDisplay()
throws Exception
{
LCD.clear();
LCD.drawString( "Please Wait..." + EMPTY_STRING, 0, 0 );
LCD.refresh();
Thread.sleep( 1000 );
LCD.clear();
}
protected static int calculateCheckValue( LightSensor lightSensor )
{
int checkValue = 0;
checkValue = ( lightSensor.getHigh() - lightSensor.getLow() ) >> 2;
checkValue += lightSensor.getLow();
return checkValue;
}
protected static void displayMainInformation( String appName, LightSensor lightSensor, int reading, int checkValue )
{
LCD.drawString( "AJ:" + appName, 0, 0 );
LCD.drawString( "Value: " + lightSensor.readNormalizedValue(), 0, 1 );
LCD.drawString( "Low: " + lightSensor.getLow(), 0, 2 );
LCD.drawString( "High: " + lightSensor.getHigh(), 0, 3 );
LCD.drawString( "Instructions: " + System.getProgramExecutionsCount(), 0, 4 );
LCD.drawString( "Time: " + (int) System.currentTimeMillis() + " ms", 0, 5 );
LCD.drawString( "Dir: " + ( ( reading > checkValue ) ? ( "Straight" ) : ( "Turning " ) ), 0, 6 );
LCD.drawString( "Push LEFT to exit" + EMPTY_STRING, 0, 7 );
LCD.refresh();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment