Commit 40969f0c authored by Peder Kronsgaard Detlefsen's avatar Peder Kronsgaard Detlefsen
Browse files

Added initial RampClimber code.

parent edc05fbb
import lejos.nxt.*;
/**
* A locomotion module with methods to drive
* a car with two independent motors. The left motor
* should be connected to port C and the right motor
* to port B.
*
* @author Ole Caprani
* @version 17.4.08
*/
public class Car
{
// Commands for the motors
private final static int forward = 1,
backward = 2,
stop = 3;
private static MotorPort leftMotor = MotorPort.B;
private static MotorPort rightMotor= MotorPort.C;
private Car()
{
}
public static void stop()
{
leftMotor.controlMotor(0,stop);
rightMotor.controlMotor(0,stop);
}
public static void forward(int leftPower, int rightPower)
{
leftMotor.controlMotor(leftPower,forward);
rightMotor.controlMotor(rightPower,forward);
}
public static void backward(int leftPower, int rightPower)
{
leftMotor.controlMotor(leftPower,backward);
rightMotor.controlMotor(rightPower,backward);
}
}
import lejos.nxt.Button;
import lejos.nxt.LCD;
import lejos.nxt.LightSensor;
import lejos.nxt.SensorPort;
public class RampClimber {
public static void main(String[] args) throws Exception {
final int power = 80;
LightSensor lightSensorRight = new LightSensor(SensorPort.S4);
LightSensor lightSensorLeft = new LightSensor(SensorPort.S1);
int blackValue = 397;
int whiteValue = 593;
int edgeLight = ((blackValue + whiteValue) / 2);
LCD.clear();
LCD.drawString("Right light: ", 0, 0);
LCD.drawString("Left light: ", 0, 1);
LCD.drawString("Direction: ", 0, 2);
while (!Button.ESCAPE.isDown()) {
int lightRight = lightSensorRight.readNormalizedValue();
int lightLeft = lightSensorLeft.readNormalizedValue() + 45;
int rightVector = lightRight - edgeLight;
int leftVector = edgeLight - lightLeft;
int direction = (int) ((rightVector * 0.5) + (leftVector * 0.5));
direction /= 6;
int MAXIMUM_POWER = 100;
if (direction < 0) {
Car.backward(MAXIMUM_POWER, MAXIMUM_POWER + direction);
} else if (direction > 0) {
Car.backward(MAXIMUM_POWER - direction, MAXIMUM_POWER);
}
LCD.drawInt(lightRight, 4, 10, 0);
LCD.drawInt(lightLeft, 4, 10, 1);
LCD.drawInt(direction, 4, 10, 2);
LCD.refresh();
//Thread.sleep();
}
Car.stop();
LCD.clear();
LCD.drawString("Program stopped", 0, 0);
LCD.refresh();
}
}
\ No newline at end of file
import lejos.nxt.Button;
import lejos.nxt.LCD;
import lejos.nxt.LightSensor;
import lejos.nxt.SensorPort;
public class RampClimber2 {
public static void main(String[] args) throws Exception {
final int power = 80;
int MAXIMUM_POWER = 100;
LightSensor lightSensorRight = new LightSensor(SensorPort.S4);
LightSensor lightSensorLeft = new LightSensor(SensorPort.S1);
int blackValue = 397;
int whiteValue = 593;
int edgeLight = ((blackValue + whiteValue) / 2);
LCD.clear();
LCD.drawString("Right light: ", 0, 0);
LCD.drawString("Left light: ", 0, 1);
LCD.drawString("Direction: ", 0, 2);
Button.waitForAnyPress();
Car.backward(96, 100);
Thread.sleep(3100);
Car.backward(90, 0);
Thread.sleep(1500);
//Car.backward(96, 100);
//Thread.sleep(3500);
//Car.backward(0, 100);
//Thread.sleep(1000);
Car.stop();
Button.waitForAnyPress();
/*
while (!Button.ESCAPE.isDown()) {
int lightRight = lightSensorRight.readNormalizedValue();
int lightLeft = lightSensorLeft.readNormalizedValue() + 45;
LCD.drawInt(lightRight, 4, 10, 0);
LCD.drawInt(lightLeft, 4, 10, 1);
LCD.drawInt(direction, 4, 10, 2);
LCD.refresh();
//Thread.sleep();
}
*/
Car.stop();
LCD.clear();
LCD.drawString("Program stopped", 0, 0);
LCD.refresh();
}
}
\ No newline at end of file
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment