2 Commits
4 changed files with 78 additions and 27 deletions
+10
View File
@@ -312,6 +312,16 @@ struct PACKED PushPeriodMsg {
float y; float y;
float z; float z;
} position; } position;
struct {
uint16_t yaw_ground;
uint16_t pitch_ground;
uint16_t yaw;
uint16_t pitch;
struct {
uint8_t option_mode : 2;
uint8_t return_center : 1;
};
} gimbal;
}; };
struct Footer footer; struct Footer footer;
}; };
+24 -20
View File
@@ -7,7 +7,6 @@
#include <sys/socket.h> #include <sys/socket.h>
#include <sys/select.h> #include <sys/select.h>
#include <netinet/in.h> #include <netinet/in.h>
#include <arpa/inet.h>
#include <string.h> #include <string.h>
#include <fcntl.h> #include <fcntl.h>
#include <stdio.h> #include <stdio.h>
@@ -82,44 +81,49 @@ int max_fd = -1;
// TODO: Close the socket and return NULL on error // TODO: Close the socket and return NULL on error
struct Connection* struct Connection*
connection_new(unsigned int source_port, const char* source_ip, unsigned int dest_port, const char* dest_ip) connection_new(uint16_t source_port, uint32_t source_ip, uint16_t dest_port, uint32_t dest_ip)
{ {
struct Connection* conn = malloc(sizeof(struct Connection)); struct Connection* conn = malloc(sizeof(struct Connection));
memset(conn, 0, sizeof(struct Connection)); memset(conn, 0, sizeof(struct Connection));
// Request a UDP socket
conn->sockfd = socket(AF_INET, SOCK_DGRAM, 0);
// Set the source address and port if they are provided // Set the source address and port if they are provided
if(source_port && source_ip) { if(source_port && source_ip) {
struct sockaddr_in loc_addr; conn->local_addr.sin_family = AF_INET;
loc_addr.sin_family = AF_INET; conn->local_addr.sin_port = htons(source_port);
loc_addr.sin_port = htons(source_port); conn->local_addr.sin_addr.s_addr = source_ip;
loc_addr.sin_addr.s_addr = inet_addr(source_ip);
if(bind(conn->sockfd, (struct sockaddr*)&loc_addr, sizeof(loc_addr)) < 0)
{
perror("unable to bind local port");
exit(EXIT_FAILURE);
} }
}
// Make the socket non-blocking
int flags = fcntl(conn->sockfd, F_GETFL);
fcntl(conn->sockfd, F_SETFL, flags | O_NONBLOCK);
// Set the address of the drone // Set the address of the drone
memset(&conn->remote_addr, 0, sizeof(conn->remote_addr)); memset(&conn->remote_addr, 0, sizeof(conn->remote_addr));
conn->addrlen = sizeof(conn->remote_addr); conn->addrlen = sizeof(conn->remote_addr);
conn->remote_addr.sin_family = AF_INET; conn->remote_addr.sin_family = AF_INET;
conn->remote_addr.sin_port = htons(dest_port); conn->remote_addr.sin_port = htons(dest_port);
conn->remote_addr.sin_addr.s_addr = inet_addr(dest_ip); conn->remote_addr.sin_addr.s_addr = dest_ip;
return conn;
}
enum connection_error
connection_connect(struct Connection* conn) {
// Request a UDP socket
conn->sockfd = socket(AF_INET, SOCK_DGRAM, 0);
if(conn->local_addr.sin_addr.s_addr
&& bind(conn->sockfd, (struct sockaddr*)&conn->local_addr, sizeof(conn->local_addr)) < 0)
return CONNECTION_LOCAL_BIND;
// Make the socket non-blocking
int flags = fcntl(conn->sockfd, F_GETFL);
fcntl(conn->sockfd, F_SETFL, flags | O_NONBLOCK);
// File descriptors are numbers that count up sequentially, // File descriptors are numbers that count up sequentially,
// so save the last one as the greatest file descriptor. // so save the last one as the greatest file descriptor.
// This is needed for polling the sockets later. // This is needed for polling the sockets later.
max_fd = conn->sockfd; max_fd = conn->sockfd;
return conn; return CONNECTION_NO_ERROR;
} }
struct Connection* struct Connection*
+12 -1
View File
@@ -6,14 +6,25 @@
#include <stddef.h> #include <stddef.h>
#include <netinet/in.h> #include <netinet/in.h>
#define ROBOT_DEFAULT_WIFI_ADDR 0X102A8C0
struct Connection { struct Connection {
int sockfd; int sockfd;
socklen_t addrlen; socklen_t addrlen;
struct sockaddr_in remote_addr; struct sockaddr_in remote_addr;
struct sockaddr_in local_addr;
}; };
struct Connection* struct Connection*
connection_new(unsigned int source_port, const char* source_ip, unsigned int dest_port, const char* dest_ip); connection_new(uint16_t source_port, uint32_t source_ip, uint16_t dest_port, uint32_t dest_ip);
enum connection_error {
CONNECTION_NO_ERROR,
CONNECTION_LOCAL_BIND
};
enum connection_error
connection_connect();
struct Connection* struct Connection*
connection_poll_ready(struct Client* client); connection_poll_ready(struct Client* client);
+29 -3
View File
@@ -4,6 +4,7 @@
#include <string.h> #include <string.h>
#include <stdlib.h> #include <stdlib.h>
#include <arpa/inet.h>
#include <stdio.h> #include <stdio.h>
@@ -30,6 +31,11 @@ struct RobotImp {
float x, y, z; float x, y, z;
uint16_t yaw_ground;
uint16_t pitch_ground;
uint16_t yaw;
uint16_t pitch;
enum { enum {
CONNECTING_TO_PROXY_PORT, CONNECTING_TO_PROXY_PORT,
SETTING_SDK_CONNECTION, SETTING_SDK_CONNECTION,
@@ -39,6 +45,7 @@ struct RobotImp {
SUSBCRIBING_SUBNODE, SUSBCRIBING_SUBNODE,
SETTING_MOVEMENT_MODE, SETTING_MOVEMENT_MODE,
SUBSCRIBING_POSITION_STATUS, SUBSCRIBING_POSITION_STATUS,
SUBSCRIBING_GIMBAL_STATUS,
SENDING_HEARTBEAT, SENDING_HEARTBEAT,
WAITING, WAITING,
READY, READY,
@@ -113,7 +120,8 @@ robot_work(Robot robot) {
case CONNECTING_TO_PROXY_PORT: case CONNECTING_TO_PROXY_PORT:
{ {
robot->client->sdk_conn = connection_new(0, 0, 30030, "192.168.2.1"); robot->client->sdk_conn = connection_new(0, 0, 30030, ROBOT_DEFAULT_WIFI_ADDR);
connection_connect(robot->client->sdk_conn);
robot->state = SETTING_SDK_CONNECTION; robot->state = SETTING_SDK_CONNECTION;
break; break;
} }
@@ -128,7 +136,8 @@ robot_work(Robot robot) {
case CONNECTING_TO_DEVICE_PORT: case CONNECTING_TO_DEVICE_PORT:
{ {
robot->client->dev_conn = connection_new(10010, "192.168.2.24", 20020, "192.168.2.1"); if(connection_connect(robot->client->dev_conn))
robot->state = STOPPED;
robot->state = SETTING_SDK_MODE; robot->state = SETTING_SDK_MODE;
break; break;
} }
@@ -177,6 +186,14 @@ robot_work(Robot robot) {
{ {
add_sub_msg ( &req, robot->seq++, true, 20, 1, DDS_POSITION ); add_sub_msg ( &req, robot->seq++, true, 20, 1, DDS_POSITION );
req_send(robot->client->dev_conn, &req); req_send(robot->client->dev_conn, &req);
robot->state = SUBSCRIBING_GIMBAL_STATUS;
break;
}
case SUBSCRIBING_GIMBAL_STATUS:
{
add_sub_msg ( &req, robot->seq++, true, 21, 1, DDS_GIMBAL_POS );
req_send(robot->client->dev_conn, &req);
robot->state = WAITING; robot->state = WAITING;
break; break;
} }
@@ -263,6 +280,7 @@ robot_work(Robot robot) {
robot->state = STOPPED; robot->state = STOPPED;
break; break;
} }
robot->client->dev_conn = connection_new(10010, resp.resp.sdkconn.config_ip, 20020, ROBOT_DEFAULT_WIFI_ADDR);
robot->state = CONNECTING_TO_DEVICE_PORT; robot->state = CONNECTING_TO_DEVICE_PORT;
break; break;
case SET_SDK_MODE_CMD: case SET_SDK_MODE_CMD:
@@ -309,11 +327,19 @@ robot_work(Robot robot) {
robot->state = READY; robot->state = READY;
break; break;
case PUSH_PERIOD_MSG_CMD: case PUSH_PERIOD_MSG_CMD:
// assume it is a position status for now switch(resp.resp.push.msg_id) {
case 20:
robot->x = resp.resp.push.position.x; robot->x = resp.resp.push.position.x;
robot->y = resp.resp.push.position.y; robot->y = resp.resp.push.position.y;
robot->z = resp.resp.push.position.z; robot->z = resp.resp.push.position.z;
break; break;
case 21:
robot->yaw_ground = resp.resp.push.gimbal.yaw_ground;
robot->pitch_ground = resp.resp.push.gimbal.pitch_ground;
robot->yaw = resp.resp.push.gimbal.yaw;
robot->pitch = resp.resp.push.gimbal.pitch;
}
break;
} }
return 1; return 1;