我正在尝试在我的项目中实现javadrone(AR.Drone Java API)。但是,当我编译我的java代码并尝试飞行时,它发生了状态更改错误。我能够向AR.Drone发送命令并第一次飞行。在第一次成功起飞后,由于“状态改变”错误,它不会再次起飞。我不知道我的代码出了什么问题。请帮帮我。谢谢!此项目中需要3个文件。我的java文件(arDroneFrame.java),NavData.java(来自javadrone)和ARDrone.java(来自javadrone)。主java文件:arDroneFrame.java。当我按下TakeOff按钮时,它应该让AR.Drone飞行并降落。
在我的arDroneFrame.java中,
private void jButtonTakeOffActionPerformed(java.awt.event.ActionEvent evt) {
com.codeminders.ardrone.ARDrone drone;
try{
drone = new com.codeminders.ardrone.ARDrone();
drone.connect();
drone.clearEmergencySignal();
// Wait until drone is ready
drone.waitForReady(CONNECT_TIMEOUT);
// do TRIM operation
drone.trim();
// Take off
System.err.println("Taking off");
drone.takeOff();
// Fly a little :)
Thread.sleep(5000);
//Land
System.err.println("Landing");
drone.land();
// Give it some time to land
Thread.sleep(2000);
// Disconnect from the done
drone.disconnect();
} catch (UnknownHostException ex) {
Logger.getLogger(arDroneFrame.class.getName()).log(Level.SEVERE, null, ex);
} catch (IOException ex) {
Logger.getLogger(arDroneFrame.class.getName()).log(Level.SEVERE, null, ex);
} catch (InterruptedException ex) {
Logger.getLogger(arDroneFrame.class.getName()).log(Level.SEVERE, null, ex);
}
}
在ARDrone.java中,我已经注释掉了一些java编码。这是因为只有当我注释掉那些java代码时,它才有效。如果我取消注释它们,我的程序将被卡在那里(根本没有响应)。
public void waitForReady(long how_long) throws IOException
{
/*long since = System.currentTimeMillis();
synchronized(state_mutex)
{
while(true)
{
if((System.currentTimeMillis() - since) >= how_long)
{
try
{
disconnect();
} catch(IOException e)
{
}
// Timeout, too late
throw new IOException("Timeout connecting to ARDrone");
} else if(state == State.DEMO)
{
return; // OK! We are now connected
} else if(state == State.ERROR || state == State.DISCONNECTED)
{
throw new IOException("Connection Error");
}
long p = Math.min(how_long - (System.currentTimeMillis() - since), how_long);
if(p > 0)
{
try
{
state_mutex.wait(p);
} catch(InterruptedException e)
{
// Ignore
}
}
}
}*/
while(state == State.DEMO)
{
System.out.println("Changed to DEMO !");
return; // OK! We are now connected
}
}
节目输出:
126 [Thread-7] DEBUG ardrone.ARDrone - State changed from TAKING_OFF to ERROR with exception
java.lang.NullPointerException
at com.codeminders.ardrone.NavData$FlyingState.fromControlState(NavData.java:58)
at com.codeminders.ardrone.NavData.getFlyingState(NavData.java:622)
at com.codeminders.ardrone.ARDrone.navDataReceived(ARDrone.java:431)
at com.codeminders.ardrone.NavDataReader.run(NavDataReader.java:92)
at java.lang.Thread.run(Thread.java:745)