Skip to content
Snippets Groups Projects

Gyro Kode

The snippet can be accessed without any authentication.
Authored by Christian Østergaard Laursen
Edited
GyroTest.java 1.58 KiB
import lejos.nxt.BasicMotorPort;
import lejos.nxt.Button;
import lejos.nxt.LCD;
import lejos.nxt.Motor;
import lejos.nxt.MotorPort;
import lejos.nxt.SensorPort;
import lejos.nxt.addon.GyroSensor;

/**
 * Test of Gyro Sensor
 * Records the minimum, maximum and current values
 * 
 * @author Lawrie Griffiths
 */

public class GyroTest {
	
    static DataLogger dl;
	private static int power = 100;
	
	public static void main(String[] args) throws Exception
	{
		GyroSensor gyro = new GyroSensor(SensorPort.S1);
		float minValue = 1023, maxValue = 0;
		int sampleInterval = 5; // ms
		dl = new DataLogger("G-pow.txt");
		long startTime = System.currentTimeMillis();
		
		LCD.drawString("Gyro Test:", 0, 0);
		Button.waitForAnyPress();
		
		LCD.drawString("Min:", 0, 2);
		LCD.drawString("Max:", 0, 3);
		LCD.drawString("Current:", 0, 4);
		
		while(!Button.ESCAPE.isDown()) 
		{
			float value = gyro.readValue();
			
			dl.writeFloatSample(value);
			
			minValue = Math.min(minValue, value);
			maxValue = Math.max(maxValue, value);
			
			LCD.drawInt((int) minValue, 6, 5, 2);
			LCD.drawInt((int) maxValue, 6, 5, 3);
			LCD.drawInt((int)(value), 6, 9, 4);
			
			long currentTime = System.currentTimeMillis();
			
			if ( currentTime - startTime > 4000 && currentTime - startTime < 8000)	{
				MotorPort.A.controlMotor(power , BasicMotorPort.FORWARD);
                MotorPort.C.controlMotor(power, BasicMotorPort.FORWARD);
			} else {
		    	Motor.A.flt();
		        Motor.C.flt();
			}
			
			Thread.sleep(sampleInterval);
			
		}
		dl.close();
	}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment