所以我试图用一个使用CC3000 WiFi盾牌的arduino来控制Parrot AR无人机,因为它是与无人机通信的手段。
我设法连接到无人机的WiFi接入点,通过DHCP等实际连接到控制和通信端口5556,5554。
无人机使用UDP进行通信,我使用Adafruit库为CC3000屏蔽与这些端口建立UDP连接。
然而,我似乎无法让无人机与我交谈或甚至向无人机发送命令:'(无人机接受某种格式的文本字符串来控制各种功能,如起飞,降落等等。我试图通过UDP发送这些字符串,但无济于事。
这是我在Arduino中的代码:
#include <Adafruit_CC3000.h>
#include <ccspi.h>
#include <SPI.h>
#include <String.h>
#include "utility/debug.h"
#define ADAFRUIT_CC3000_IRQ 3
#define ADAFRUIT_CC3000_VBAT 5
#define ADAFRUIT_CC3000_CS 10
#define WLAN_SSID "ardrone_000000"
#define WLAN_PASS ""
#define WLAN_SECURITY WLAN_SEC_UNSEC
#define ARDRONE_NAVDATA_PORT 5554
#define ARDRONE_VIDEO_PORT 5555
#define ARDRONE_AT_PORT 5556
#define ARDRONE_CONFIGDATA_PORT 5559
#define ARDRONE_MAX_AT_COMMAND_LENGTH 300
#define ARDRONE_AT_CMD_INTERVAL 30
Adafruit_CC3000 cc3000 = Adafruit_CC3000(ADAFRUIT_CC3000_CS, ADAFRUIT_CC3000_IRQ,ADAFRUIT_CC3000_VBAT, SPI_CLOCK_DIVIDER);
uint32_t ipAddress, netmask, gateway, dhcpserv, dnsserv;
/*
int longtime = 0;
const unsigned long connectTimeout = 4L * 100L, // Max time to wait for the serverconnection
responseTimeout = 4L * 100L; //Max time to wait for answer from server*/
char cmd[ARDRONE_MAX_AT_COMMAND_LENGTH];
int sequence = 0;
int run_count = 0;
char inBuffer[255];
char outBuffer[] = "acknowledged";
void setup() {
Serial.begin(115200);
Serial.println(F("\nInitialising the CC3000..."));
if (!cc3000.begin()) {
Serial.println(F("Unable to initialise the CC3000! Check your wiring?"));
while(1);
}
Serial.println(F("\nDeleting old connection profiles"));
if (!cc3000.deleteProfiles()) {
Serial.println(F("Failed!"));
while(1);
}
char *ssid = WLAN_SSID;
Serial.print(F("\nAttempting to connect to "));
Serial.print(ssid);
if (!cc3000.connectToAP(WLAN_SSID, WLAN_PASS, WLAN_SECURITY)) {
Serial.println(F("Failed!"));
while(1);
}
Serial.println(F("\nConnected!"));
Serial.println(F("\nRequest DHCP"));
while (!cc3000.checkDHCP()) {
delay(1000);
}
connect_drone();
}
void loop() {
talkToDrone();
}
void connect_drone() {
cc3000.getIPAddress(&ipAddress, &netmask, &gateway, &dhcpserv, &dnsserv);
Adafruit_CC3000_Client drone_config = cc3000.connectTCP(ipAddress, ARDRONE_CONFIGDATA_PORT);
Adafruit_CC3000_Client drone_control = cc3000.connectUDP(ipAddress, ARDRONE_AT_PORT);
Adafruit_CC3000_Client drone_comms = cc3000.connectUDP(ipAddress, ARDRONE_NAVDATA_PORT);
if (drone_comms.connected()) {
Serial.println(F("Drone talking"));
} else {
Serial.println(F("Drone not talking"));
}
if (drone_control.available()) {
Serial.println(F("Drone ready for Commands"));
} else {
Serial.print(F("Drone having none of it..."));
}
if (drone_config.connected()) {
Serial.println(F("Drone Configged"));
} else {
Serial.println(F("Drone not Configged"));
}
}
/*
Serial.println(F("Closing Comms"));
drone_comms.close();
Serial.println(F("-----------------------------------------------------------"));*/
void nav_data_init() {
Adafruit_CC3000_Client drone_comms = cc3000.connectUDP(ipAddress, ARDRONE_NAVDATA_PORT);
if (drone_comms.connected()) {
//drone_comms.print("AT*CONFIG=general:navdata_demo,TRUE\r");
//drone_comms.print("AT*CTRL=0\r");
}
if (drone_comms.connected()) {
if (drone_comms.available()) {
Serial.println(F("Receiving Telemetry Data"));
char c = drone_comms.read();
Serial.print(c);
}
}
}
void takeOff() {
snprintf(cmd , strlen(cmd), "AT*REF=%d,290718208\r", sequence++);//add \r
}
void land() {
snprintf(cmd , strlen(cmd), "AT*REF=%d,290717696\r", sequence++);
}
void hover() {
snprintf(cmd , strlen(cmd), "AT*PCMD=%d,1,0,0,0,0\r", sequence++);
}
void calib() {
snprintf(cmd, strlen(cmd), "AT*FTRIM=%d\r", sequence++);
}
void watchdog() {
snprintf(cmd, strlen(cmd), "AT*COMWDG\r", sequence++);
}
void periodicTask() {
unsigned long current_t = millis();
static unsigned long next_t = 0;
if (current_t > next_t) {
talkToDrone();
next_t = current_t + ARDRONE_AT_CMD_INTERVAL;
}
}
void talkToDrone() {
Adafruit_CC3000_Client drone_control = cc3000.connectUDP(ipAddress, ARDRONE_AT_PORT);
Adafruit_CC3000_Client drone_comms = cc3000.connectUDP(ipAddress, ARDRONE_NAVDATA_PORT);
if (drone_comms.available()) {
drone_comms.write(outBuffer, strlen(outBuffer));
int len = drone_comms.read(inBuffer,255);
if (len > 0) inBuffer[len] = 0;
Serial.print("Received packet");
Serial.println("Contents");
Serial.println(inBuffer);
drone_comms.write(outBuffer, strlen(outBuffer));
}
if (drone_control.connected() && run_count == 0) {
Serial.print(F("Sending Takeoff Sequence."));
calib();
takeOff();
watchdog();
drone_control.print(cmd);
cmd[0] = 0;
Serial.print(F("."));
Serial.println(F(".Done!"));
run_count++;
sequence = 0;
} else if (drone_control.connected() && run_count > 0) {
Serial.print(F("Sending Hover Sequence."));
hover();
watchdog();
drone_control.print(cmd);
cmd[0] = 0;
Serial.print(F("."));
Serial.println(F(".Done!"));
run_count++;
sequence = 0;
}
}