我正在研究Parrot AR.无人机项目。这些库是从JavaDrone网站(https://code.google.com/p/javadrone/downloads/list)下载并实现的。但是,尽管我确实包含了所有正确的库,并进行了正确的类调用来获取该方法,但它仍然无法返回正确的信息。返回的所有结果似乎都是"假的"。知道这段代码上发生了什么吗?请帮我:(
所以我所做的是我有 2 个按钮:(i) 连接 (ii) 起飞按钮。连接按钮功能用于建立与无人机的连接,而起飞按钮用于使无人机飞行移动一点并返回无人机的 NAV 导航数据。可悲的是,所有返回的NAV数据似乎都不起作用。
注意:此代码在代码编译时工作正常。但它无法从无人机返回正确有效的 NAV 数据。
private void jButtonConnectActionPerformed(java.awt.event.ActionEvent evt) {
System.out.println("Connect?");
drone = new ARDrone();
data = new NavData();
drone.playLED(10,10,10);
drone.connect();
drone.clearEmergencySignal();
System.err.println("Ready to connect!!");
// Wait until drone is ready
drone.waitForReady(CONNECT_TIMEOUT);
System.err.println("Drone State: " + drone.getState());
// do TRIM operation
drone.trim();
System.err.println("Congratulation! You have connected to Drone!");
System.out.println("You can issue flight commands now!");
batteryStatus.setText("0" + "%");
batteryStatus.setForeground(Color.ORANGE);
batteryStatus.setText("" + data.getBattery());
}
private void jButtonTakeOffActionPerformed(java.awt.event.ActionEvent evt) {
System.err.println("Current Drone State : " + drone.getState().toString());
System.err.println("Taking off");
drone.takeOff();
Thread.sleep(4000);
System.err.println("**********nMOVEn**********");
drone.move(0.0f, 150.5f, 500.0f, 0.0f);
Thread.sleep(1000);
System.err.println("******************************************");
System.err.println("Drone Infomation");
System.err.println("Battery Too High ? " + data.isBatteryTooHigh());
System.err.println("Battery Too Low ? " + data.isBatteryTooLow());
System.err.println("Drone Flying ? " + data.isFlying());
System.err.println("Control Received ? " + data.isControlReceived());
System.err.println("Motor Down ? " + data.isMotorsDown());
System.err.println("Not Enough Power ?" + data.isNotEnoughPower());
System.err.println("Trim Received ? " + data.isTrimReceived());
System.err.println("Trim Running? " + data.isTrimRunning());
System.err.println("Trim succeded? " + data.isTrimSucceeded());
System.err.println("PIC Number OK? "+ data.isPICVersionNumberOK());
System.err.println("******************************************");
Thread.sleep(5000);
drone.sendAllNavigationData();
drone.land();
}
输出:
******************************************
Drone Infomation
Battery Life: 0.0%
Battery Too High ? false
Battery Too Low ? false
Drone Flying ? false
Control Received ? false
Motor Down ? false
Not Enough Power ?false
Trim Received ? false
Trim Running? false
Trim succeded? false
PIC Number OK? false
********************************************
更新:我所做的是听从约翰的建议。我确实实现了所有必要的方法和 NavDataListener 来从无人机获取 NavData。
import com.codeminders.ardrone.ARDrone;
import com.codeminders.ardrone.ARDrone.VideoChannel;
import com.codeminders.ardrone.NavData;
import com.codeminders.ardrone.NavDataListener;
public class arDrone extends javax.swing.JFrame implements Runnable, NavDataListener{
public ARDrone drone;
public NavData data = new NavData();
public arDrone(String text) {
//FreeTTS speech text
this.text=text;
}
public arDrone() {
initComponents();
setIcon();
initDrone();
}
private void initDrone() {
try {
drone = new ARDrone();
data = new NavData();
drone.addNavDataListener(this);
} catch (UnknownHostException ex) {
System.err.println(ex);
return;
}
}
public void navDataReceived(NavData nd) {
System.err.println("Testing navDataReceived is running...");
updateBatteryStatus(nd.getBattery());
this.flying.set(nd.isFlying());
}
private void updateBatteryStatus(final int value) {
java.awt.EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
batteryStatus.setText(value + "%");
if (value < 15) {
batteryStatus.setForeground(Color.RED);
} else if (value < 50) {
batteryStatus.setForeground(Color.ORANGE);
} else {
batteryStatus.setForeground(Color.GREEN);
}
}
});
}
问题是你没有做任何事情来实际获取导航数据。 你不能只创建一个NavData
对象,并希望它被填充到有效的数据中 - 它不会。
您需要使用com.codeminders.ardrone.NavDataListener
界面。
- 实现
NavDataListener
接口,以及navDataReceived
方法。 - 使用
ARDrone
添加侦听器方法addNavDataListener
. - 在您的
navDataRecieved
方法你将收到一个包含有效遥测数据的 NavData 对象。
你设置无人机IP地址吗?根据消息来源,无人机的默认 IP 是 192.168.1.1。
可以调用另一个构造函数来设置 IP:
drone = new ARDrone(InetAddress.getByName("xxx.xxx.xxx.xxx"));
将 xxx.xxx.xxx.xxx 替换为实际的无人机 IP。