package org.samcrow.vehicle;
import java.awt.event.KeyEvent;
import java.util.Collection;
import org.samcrow.environment.Obstacle;
import org.samcrow.sensor.SensorData;
import org.samcrow.sensor.SingleRangefinder;
import org.samcrow.sensor.TouchSensor;
import org.samcrow.util.DrawQueue;
/**
* One type of vehicle.
*
* @author Sam Crow
*
*/
public class DefaultVehicle extends Vehicle {
private static final double MAX_STEER = 60;
private TouchSensor rightTouchSensor;
private TouchSensor leftTouchSensor;
private TouchSensor rightRearTouchSensor;
private TouchSensor leftRearTouchSensor;
private SingleRangefinder leftRangefinder;
private SingleRangefinder rightRangefinder;
private double targetSpeed = 0;
public DefaultVehicle(int inX, int inY, DrawQueue queue,
Collection<Obstacle> obstacles) {
super(inX, inY, queue, obstacles);
}
/*
* (non-Javadoc)
*
* @see org.samcrow.vehicle.Vehicle#setup()
*/
@Override
protected void setup() {
addFrontWheel(-10, 30);
addFrontWheel(10, 30);
addRearWheel(-10, 0);
addRearWheel(10, 0);
rightTouchSensor = new TouchSensor(20, 42, obstacles, this);
leftTouchSensor = new TouchSensor(-20, 42, obstacles, this);
addSensor(leftTouchSensor);
addSensor(rightTouchSensor);
rightRearTouchSensor = new TouchSensor(20, -20, obstacles,this);
leftRearTouchSensor = new TouchSensor(-20, -20, obstacles, this);
addSensor(rightRearTouchSensor);
addSensor(leftRearTouchSensor);
final int angle = 25;
leftRangefinder = new SingleRangefinder(0, 40, -angle, this, obstacles);
rightRangefinder = new SingleRangefinder(0, 40, angle, this, obstacles);
addSensor(leftRangefinder);
addSensor(rightRangefinder);
}
/*
* (non-Javadoc)
*
* @see org.samcrow.vehicle.Vehicle#runLoop()
*/
@Override
protected void runLoop() {
targetSpeed = 10;
//runAutothrottle();if(getSpeed() > 20){
// if(getSpeed() > 30){
// setThrottle(0);
// setBrake(0.9);
// }else if(getSpeed() < 20){
// setBrake(0);
setThrottle(1.0);
// }
}
/*
* (non-Javadoc)
*
* @see org.samcrow.vehicle.Vehicle#crashed()
*/
@Override
public void crashed(Obstacle target) {
}
/*
* (non-Javadoc)
*
* @see
* org.samcrow.vehicle.Vehicle#sensorDataReceived(org.samcrow.sensor.SensorData
* )
*/
@Override
public void sensorDataReceived(SensorData data) {
if(data.getSensor().equals(leftTouchSensor) || data.getSensor().equals(rightTouchSensor) && data.getTriggered()){
if(!isReversed()){
setBrake(1);
setThrottle(0);
setIsReversed(true);
setSteeringAngle(0);
}
}
if(data.getSensor().equals(rightRearTouchSensor) || data.getSensor().equals(leftRearTouchSensor)){
if(isReversed()){
setBrake(1);
setThrottle(0);
setIsReversed(false);
setSteeringAngle(0);
}
}
if(data.getSensor() instanceof SingleRangefinder){
double steeringAmount = 150;
double sensitivity = 1.6;
double closeValue = 1.4;
if(data.getSensor().equals(leftRangefinder)){
double distance = data.getSingleDistance() / sensitivity;
steeringAmount /= ((distance / 300d) * closeValue);//relatively discount significance of far readings
double angle = 1/distance * steeringAmount;
if(!isReversed()){
setSteeringAngle(angle);
}else{//reversed, so invert steering
setSteeringAngle(-angle);
}
System.out.println("Setting steering angle to "+ angle);
}
if(data.getSensor().equals(rightRangefinder)){
double distance = data.getSingleDistance() / sensitivity;
steeringAmount /= ((distance / 300d) * closeValue);//relatively discount significance of far readings
double angle = -1/distance * steeringAmount;
if(!isReversed()){
setSteeringAngle(angle);
}else{//reversed, so invert steering
setSteeringAngle(-angle);
}
System.out.println("Setting steering angle to "+ angle);
}
}
}
public void keyPressed(KeyEvent e){
switch(e.getKeyCode()){
case KeyEvent.VK_UP:
setThrottle(1);
break;
case KeyEvent.VK_DOWN:
setBrake(1);
break;
case KeyEvent.VK_LEFT:
setSteeringAngle(getSteeringAngle() - 5);
break;
case KeyEvent.VK_RIGHT:
setSteeringAngle(getSteeringAngle() + 5);
break;
}
}
public void keyReleased(KeyEvent e){
switch(e.getKeyCode()){
case KeyEvent.VK_UP:
setThrottle(0);
break;
case KeyEvent.VK_DOWN:
setBrake(0);
break;
}
}
public void reverse() {
setIsReversed(!isReversed());
}
private void runAutothrottle(){
final double tolerance = 1;
System.out.println("Target: "+targetSpeed+" Actual: "+getSpeed());
if(getSpeed() - targetSpeed > tolerance){
System.out.println("Braking.");
setBrake(1);
}
if(targetSpeed - getSpeed() > tolerance){
System.out.println("Accelerating.");
setThrottle(1);
}
}
}