我有一个班级CircleController
,extends
班级VehicleController
,我很困惑,为什么我收到此错误。任何帮助将不胜感激。
CircleController
是......
import java.math.*;
import javax.realtime.*;
public class CircleController extends VehicleController{
public final static int XMiddle = 50;
public final static int YMiddle = 50;
public final static int radius = 25;
//private GroundVehicle v;
public CircleController(Simulator s, GroundVehicle v){
super(s,v);
}
public CircleController(Simulator s, GroundVehicle v, SchedulingParameters sched, ReleaseParameters release){
super(s,v,sched, release);
}
public Control getControl(int sec, int msec){
double position[] = this.newVehic.getPosition();
double dist = Math.sqrt((position[0]-XMiddle)*(position[0]-XMiddle)+(position[1]-YMiddle)*(position[1]-YMiddle));
double angleFromCircleMiddle = Math.atan2(position[1]-YMiddle,position[0]-XMiddle);
double omega=0;
double speed =Simulator.maxTransSpeed;
double angleDelta = position[2]-angleFromCircleMiddle; //angle we need to turn
Control c = new Control(speed, omega);
if (dist<radius+.01){// inside the circle so need to move outwards
//double desiredTheta = angleFromCircleMiddle;
omega = angleDelta/(100.0/1000.0);
//checking omega
if (omega>Math.PI/4)
omega = Math.PI/4;
else if (omega<-Math.PI/4)
omega=-Math.PI/4;
c = new Control(speed, omega);
}
else if (dist+.01>radius){
angleDelta = -angleDelta; //coming from outside inwards
omega = angleDelta/(100.0/1000.0);
//checking omega
if (omega>Math.PI/4)
omega = Math.PI/4;
else if (omega<-Math.PI/4)
omega=-Math.PI/4;
c = new Control(speed, omega);
}
else{
//speed = radius*omega
omega = speed/radius;
c = new Control(speed, omega);
}
c=avoidWalls(this.newVehic.getPosition());
return c;
}
}
VehicleController类是......
import javax.realtime.*;
public class VehicleController extends RealtimeThread{
protected Simulator newSim;
protected GroundVehicle newVehic;
private double prevTime;
private int numSides;
public double turnDuration;
public double edgeTravelDuration;
public boolean isTurning = false;
public boolean controllerInitialized=false;
public double timeOfManoeuverStart;
protected static int numControllers=0;
protected int controllerID = 0;
private static double avoidWallDist = 15;
private static double timeUpdateInterval = .1;
//constants
private final int diameter=50;
public VehicleController(Simulator s, GroundVehicle v){
//error if input is incorrect
if (s==null)
throw new IllegalArgumentException("error, a null Simulator was inputted.");
if (v==null)
throw new IllegalArgumentException("error, a null Ground Vehicle was inputted.");
this.newSim=s;
this.newVehic=v;
setNumSides(5);
synchronized(VehicleController.class){
controllerID=numControllers;
numControllers++;
}
}
public VehicleController(Simulator s, GroundVehicle v, SchedulingParameters schedule, ReleaseParameters release){
//error if input is incorrect
if (s==null)
throw new IllegalArgumentException("error, a null Simulator was inputted.");
if (v==null)
throw new IllegalArgumentException("error, a null Ground Vehicle was inputted.");
this.newSim=s;
this.newVehic=v;
setNumSides(5);
synchronized(VehicleController.class){
controllerID=numControllers;
numControllers++;
}
}
private void initializeController(){
/* The bulk of this method is to determine how long to spend turning at
* each corner of the polygon, and how long to spend driving along each
* edge. We calculate turnDuration and edgeTravelDuration, and then use
* these inside getControl to decide when to switch from turning to
* travelling straight and back again. */
double interiorAngle = (Math.PI/180)*(180+180*(numSides-3))/numSides;
double turningAngle = Math.PI - interiorAngle;
double minTurningRadius = newSim.minTransSpeed/newSim.maxRotSpeed; // v/w=r
double arcLength = (turningAngle)*minTurningRadius; //need to know at what point we need to start turning
turnDuration = arcLength/newSim.minTransSpeed; //shortest length needed to make this turn
double edgeLength = diameter*Math.cos(interiorAngle/2) -(2*minTurningRadius*Math.tan((turningAngle)/2));
edgeTravelDuration =edgeLength/newSim.maxTransSpeed;
isTurning=true; //meaning we are done with the straightaway and need to turn.
timeOfManoeuverStart = -turnDuration/2.0;
controllerInitialized=true;
}
public Control getControl(int sec, int msec) {
double controlTime = sec+msec*1E-3;
Control c = null;
if (isTurning) {
if (controlTime - timeOfManoeuverStart < turnDuration)
c = new Control(newSim.minTransSpeed, newSim.maxRotSpeed);
else {
isTurning = false;
timeOfManoeuverStart = controlTime;
c = new Control(newSim.maxTransSpeed, 0);
}
}
else {
if (controlTime - timeOfManoeuverStart < edgeTravelDuration)
c = new Control(newSim.maxTransSpeed, 0);
else {
isTurning = true;
timeOfManoeuverStart = controlTime;
c = new Control(newSim.minTransSpeed, newSim.maxRotSpeed);
}
}
return c;
}
public void setNumSides(int n){
if (n>10 || n<3)
numSides=numSides;
else
numSides=n;
initializeController();
}
public int getNumSides(){
return numSides;
}
public void run(){
int sec=0, mSec=0;
double currentTime=0;
//prevTime=0;
// The simulation time is called by multiple threads, so we must put it in a simulator block.
double prevTime=0;
while (currentTime<100){
synchronized(newSim){
sec = newSim.getCurrentSec();
mSec = newSim.getCurrentMSec();
currentTime =sec+mSec/1000.0;
if (currentTime>prevTime+ timeUpdateInterval){
prevTime = currentTime;
Control c =getControl(sec,mSec);
if (c!=null){
newVehic.controlVehicle(c);
}
}
newSim.notifyAll();
}
}
}
protected Control avoidWalls(double[] pos) {
if (pos[0] > 100 - avoidWallDist && pos[1] > 100 - avoidWallDist) {
if (pos[2] > -3 * Math.PI / 4.0) {
return new Control(5, -Math.PI/4);
} else {
return new Control(5, Math.PI/4);
}
}
if (pos[0] > 100 - avoidWallDist && pos[1] < 0 + avoidWallDist) {
if (pos[2] > 3 * Math.PI / 4.0) {
return new Control(5, -Math.PI/4);
} else {
return new Control(5, Math.PI/4);
}
}
if (pos[0] < 0 + avoidWallDist && pos[1] > 100 - avoidWallDist) {
if (pos[2] > -Math.PI / 4.0) {
return new Control(5, -Math.PI/4);
} else {
return new Control(5, Math.PI/4);
}
}
if (pos[0] < 0 + avoidWallDist && pos[1] < 0 + avoidWallDist) {
if (pos[2] > Math.PI / 4.0) {
return new Control(5, -Math.PI/4);
} else {
return new Control(5, Math.PI/4);
}
}
if (pos[0] > 100 - avoidWallDist) {
if (pos[2] > 0) {
return new Control(5, Math.PI/4);
} else {
return new Control(5, -Math.PI/4);
}
}
if (pos[0] < 0 + avoidWallDist) {
if (pos[2] > 0) {
return new Control(5, -Math.PI/4);
} else {
return new Control(5, Math.PI/4);
}
}
if (pos[1] < 0 + avoidWallDist) {
if (pos[2] > Math.PI / 2) {
return new Control(5, -Math.PI/4);
} else {
return new Control(5, Math.PI/4);
}
}
if (pos[1] > 100- avoidWallDist) {
if (pos[2] > -Math.PI / 2) {
return new Control(5, -Math.PI/4);
} else {
return new Control(5, Math.PI/4);
}
}
return null;
}
}