Compare commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
c0f2b11db5 | ||
|
|
11c3ac43fb | ||
|
|
23a07f99d9 | ||
|
|
19861ca234 | ||
|
|
4d11e5b66f | ||
|
|
72b9a2ba92 |
+11
-19
@@ -3,7 +3,7 @@ cmake_minimum_required(VERSION 3.15)
|
|||||||
project (
|
project (
|
||||||
RoboMasterSDK
|
RoboMasterSDK
|
||||||
VERSION 2.0.0
|
VERSION 2.0.0
|
||||||
LANGUAGES C CXX
|
LANGUAGES C
|
||||||
)
|
)
|
||||||
|
|
||||||
set_property(GLOBAL PROPERTY C_STANDARD 17)
|
set_property(GLOBAL PROPERTY C_STANDARD 17)
|
||||||
@@ -30,27 +30,19 @@ target_include_directories(robomaster
|
|||||||
include
|
include
|
||||||
)
|
)
|
||||||
|
|
||||||
find_package(SDL2 REQUIRED)
|
include(FetchContent)
|
||||||
find_package(OpenCV REQUIRED)
|
FetchContent_Declare (
|
||||||
find_package(PkgConfig REQUIRED)
|
SDL2
|
||||||
|
GIT_REPOSITORY https://github.com/libsdl-org/SDL
|
||||||
pkg_check_modules(FFMPEG REQUIRED IMPORTED_TARGET libavcodec libavformat libavutil libswscale)
|
GIT_TAG release-2.0.20
|
||||||
|
GIT_SHALLOW TRUE
|
||||||
|
)
|
||||||
|
FetchContent_MakeAvailable(SDL2)
|
||||||
|
|
||||||
add_executable(robomasterapp
|
add_executable(robomasterapp
|
||||||
src/sdl.cpp
|
src/sdl.c
|
||||||
)
|
)
|
||||||
target_link_libraries(robomasterapp
|
target_link_libraries(robomasterapp
|
||||||
robomaster
|
robomaster
|
||||||
SDL2::SDL2
|
SDL2-static
|
||||||
${OpenCV_LIBS}
|
|
||||||
PkgConfig::FFMPEG
|
|
||||||
)
|
|
||||||
|
|
||||||
add_executable(stream
|
|
||||||
src/stream.cpp
|
|
||||||
)
|
|
||||||
target_link_libraries(stream
|
|
||||||
SDL2::SDL2
|
|
||||||
${OpenCV_LIBS}
|
|
||||||
PkgConfig::FFMPEG
|
|
||||||
)
|
)
|
||||||
|
|||||||
@@ -38,28 +38,3 @@ stream_ctrl (
|
|||||||
enum STREAMRESOLUTION res,
|
enum STREAMRESOLUTION res,
|
||||||
enum STREAMCTRL ctrl );
|
enum STREAMCTRL ctrl );
|
||||||
|
|
||||||
static const uint8_t VISION_HOST = 17;
|
|
||||||
static const uint8_t VISION_INDEX = 7;
|
|
||||||
|
|
||||||
#define VISION_DETECT_ENABLE_CMD 0xA30A
|
|
||||||
|
|
||||||
#define VISION_DETECT_ENABLE_PERSON 2
|
|
||||||
#define VISION_DETECT_ENABLE_GESTURE 4
|
|
||||||
#define VISION_DETECT_ENABLE_LINE 16
|
|
||||||
#define VISION_DETECT_ENABLE_MARKER 32
|
|
||||||
#define VISION_DETECT_ENABLE_ROBOT 128
|
|
||||||
void
|
|
||||||
vision_enable (
|
|
||||||
union Request* req,
|
|
||||||
uint16_t seq,
|
|
||||||
bool ack );
|
|
||||||
|
|
||||||
#define VISION_DETECT_INFO_CMD 0xA40A
|
|
||||||
enum VISIONTYPE {
|
|
||||||
VISIONTYPE_SHOULDER,
|
|
||||||
VISIONTYPE_PERSON,
|
|
||||||
VISIONTYPE_GESTURE,
|
|
||||||
VISIONTYPE_LINE,
|
|
||||||
VISIONTYPE_MARKER,
|
|
||||||
VISIONTYPE_ROBOT
|
|
||||||
};
|
|
||||||
|
|||||||
@@ -18,14 +18,3 @@ gimbal_ctrl_speed (
|
|||||||
int16_t p,
|
int16_t p,
|
||||||
int16_t y,
|
int16_t y,
|
||||||
int16_t r );
|
int16_t r );
|
||||||
|
|
||||||
#define GIMBAL_ROTATE_CMD 0xb03f
|
|
||||||
void
|
|
||||||
gimbal_rotate (
|
|
||||||
union Request* req,
|
|
||||||
uint16_t seq,
|
|
||||||
bool ack,
|
|
||||||
int16_t p,
|
|
||||||
int16_t y );
|
|
||||||
|
|
||||||
#define GIMBAL_ACTION_PUSH_CMD 0xb13f
|
|
||||||
|
|||||||
+35
-120
@@ -280,119 +280,49 @@ struct PACKED StreamCtrlResp {
|
|||||||
struct Footer footer;
|
struct Footer footer;
|
||||||
};
|
};
|
||||||
|
|
||||||
struct PACKED VisionRect {
|
struct PACKED AddSubMsgReq {
|
||||||
union {
|
|
||||||
struct {
|
|
||||||
float x;
|
|
||||||
float y;
|
|
||||||
float w;
|
|
||||||
float h;
|
|
||||||
uint32_t info;
|
|
||||||
} rect;
|
|
||||||
struct {
|
|
||||||
float x;
|
|
||||||
float y;
|
|
||||||
float theta;
|
|
||||||
float c;
|
|
||||||
uint32_t info;
|
|
||||||
} line;
|
|
||||||
struct {
|
|
||||||
float x;
|
|
||||||
float y;
|
|
||||||
float w;
|
|
||||||
float h;
|
|
||||||
uint16_t info;
|
|
||||||
uint16_t distance;
|
|
||||||
} marker;
|
|
||||||
};
|
|
||||||
};
|
|
||||||
|
|
||||||
struct PACKED VisionDetectInfo {
|
|
||||||
struct Header header;
|
struct Header header;
|
||||||
uint8_t type;
|
uint8_t node_id;
|
||||||
uint8_t status;
|
uint8_t msg_id;
|
||||||
uint16_t errcode;
|
uint8_t reserved;
|
||||||
uint8_t count;
|
uint8_t sub_mode;
|
||||||
struct VisionRect rects[];
|
uint8_t subject_count;
|
||||||
};
|
uint64_t subject_uuid;
|
||||||
|
uint16_t freq;
|
||||||
struct PACKED GimbalActionPush {
|
|
||||||
struct Header header;
|
|
||||||
uint8_t id;
|
|
||||||
uint8_t progress;
|
|
||||||
uint8_t state;
|
|
||||||
int16_t yaw;
|
|
||||||
int16_t roll;
|
|
||||||
int16_t pitch;
|
|
||||||
};
|
|
||||||
|
|
||||||
struct PACKED GimbalRotateReq {
|
|
||||||
struct Header header;
|
|
||||||
uint8_t id;
|
|
||||||
|
|
||||||
//union {
|
|
||||||
uint8_t control_bitfield;
|
|
||||||
// struct {
|
|
||||||
// // always 0 for start
|
|
||||||
// uint8_t control : 1;
|
|
||||||
// uint8_t frequency : 2;
|
|
||||||
// };
|
|
||||||
//};
|
|
||||||
|
|
||||||
//union {
|
|
||||||
uint8_t coord_bitfield;
|
|
||||||
// struct {
|
|
||||||
// uint8_t coordinate : 3;
|
|
||||||
// // always true
|
|
||||||
// uint8_t pitch_valid : 1;
|
|
||||||
// // always false
|
|
||||||
// uint8_t roll_valid : 1;
|
|
||||||
// // always true
|
|
||||||
// uint8_t yaw_valid : 1;
|
|
||||||
// };
|
|
||||||
//};
|
|
||||||
|
|
||||||
union {
|
|
||||||
int16_t yaw;
|
|
||||||
struct {
|
|
||||||
int8_t l_yaw;
|
|
||||||
int8_t h_yaw;
|
|
||||||
};
|
|
||||||
};
|
|
||||||
int16_t roll;
|
|
||||||
union {
|
|
||||||
int16_t pitch;
|
|
||||||
struct {
|
|
||||||
int8_t l_pitch;
|
|
||||||
int8_t h_pitch;
|
|
||||||
};
|
|
||||||
};
|
|
||||||
|
|
||||||
uint16_t error;
|
|
||||||
uint16_t yaw_speed;
|
|
||||||
uint16_t roll_speed;
|
|
||||||
uint16_t pitch_speed;
|
|
||||||
|
|
||||||
struct Footer footer;
|
struct Footer footer;
|
||||||
};
|
};
|
||||||
|
|
||||||
struct PACKED GimbalRotateResp {
|
struct PACKED AddSubMsgResp {
|
||||||
struct Header header;
|
struct Header header;
|
||||||
uint8_t retcode;
|
uint8_t retcode;
|
||||||
uint8_t accept;
|
uint8_t pub_node_id;
|
||||||
|
uint8_t ack_sub_mode;
|
||||||
|
uint8_t ack_msg_id;
|
||||||
|
uint16_t ack_err_uid_data;
|
||||||
struct Footer footer;
|
struct Footer footer;
|
||||||
};
|
};
|
||||||
|
|
||||||
struct PACKED VisionDetectEnableReq {
|
struct PACKED PushPeriodMsg {
|
||||||
struct Header header;
|
struct Header header;
|
||||||
uint16_t type;
|
uint8_t sub_mode;
|
||||||
struct Footer footer;
|
uint8_t msg_id;
|
||||||
|
union {
|
||||||
|
struct {
|
||||||
|
float x;
|
||||||
|
float y;
|
||||||
|
float z;
|
||||||
|
} 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 PACKED VisionDetectEnableResp {
|
|
||||||
struct Header header;
|
|
||||||
uint8_t retcode;
|
|
||||||
uint16_t error;
|
|
||||||
struct Footer footer;
|
struct Footer footer;
|
||||||
};
|
};
|
||||||
|
|
||||||
@@ -411,8 +341,7 @@ union Request {
|
|||||||
struct GimbalCtrlSpeedReq gimbspeed;
|
struct GimbalCtrlSpeedReq gimbspeed;
|
||||||
struct BlasterFireReq blaster;
|
struct BlasterFireReq blaster;
|
||||||
struct StreamCtrlReq stream;
|
struct StreamCtrlReq stream;
|
||||||
struct VisionDetectEnableReq enablevision;
|
struct AddSubMsgReq sub;
|
||||||
struct GimbalRotateReq gimbalrot;
|
|
||||||
};
|
};
|
||||||
union Response {
|
union Response {
|
||||||
struct Header header;
|
struct Header header;
|
||||||
@@ -429,27 +358,13 @@ union Response {
|
|||||||
struct GimbalCtrlSpeedResp gimbspeed;
|
struct GimbalCtrlSpeedResp gimbspeed;
|
||||||
struct BlasterFireResp blaster;
|
struct BlasterFireResp blaster;
|
||||||
struct StreamCtrlResp stream;
|
struct StreamCtrlResp stream;
|
||||||
struct VisionDetectEnableResp enablevision;
|
struct AddSubMsgResp sub;
|
||||||
struct GimbalRotateResp gimbalrot;
|
struct PushPeriodMsg push;
|
||||||
};
|
};
|
||||||
|
|
||||||
union Push {
|
|
||||||
struct GimbalActionPush gimbalaction;
|
|
||||||
struct VisionDetectInfo vision;
|
|
||||||
};
|
|
||||||
|
|
||||||
union Message {
|
union Message {
|
||||||
struct Header header;
|
struct Header header;
|
||||||
union Request req;
|
union Request req;
|
||||||
union Response resp;
|
union Response resp;
|
||||||
union Push push;
|
|
||||||
};
|
|
||||||
|
|
||||||
enum ACTIONSTATE {
|
|
||||||
ACTION_RUNNING,
|
|
||||||
ACTION_SUCCEEDED,
|
|
||||||
ACTION_FAILED,
|
|
||||||
ACTION_STARTED
|
|
||||||
};
|
};
|
||||||
|
|
||||||
enum MESSAGEERR {
|
enum MESSAGEERR {
|
||||||
|
|||||||
@@ -1,28 +1,11 @@
|
|||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
#include "message.h"
|
|
||||||
|
|
||||||
#include <stdbool.h>
|
#include <stdbool.h>
|
||||||
|
|
||||||
// Handle for the high level robot interface
|
// Handle for the high level robot interface
|
||||||
struct RobotImp;
|
struct RobotImp;
|
||||||
typedef struct RobotImp* Robot;
|
typedef struct RobotImp* Robot;
|
||||||
|
|
||||||
#define FRAGMENT_MESSAGE 1
|
|
||||||
#define FRAGMENT_RECTANGLE 2
|
|
||||||
#define FRAGMENT_LINE 3
|
|
||||||
#define FRAGMENT_MARKER 4
|
|
||||||
|
|
||||||
struct Fragment {
|
|
||||||
int type;
|
|
||||||
union {
|
|
||||||
union Message message;
|
|
||||||
struct VisionRect rect;
|
|
||||||
};
|
|
||||||
};
|
|
||||||
|
|
||||||
int robot_poll(Robot robot, struct Fragment* fragment);
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* Return a handle to the high level robot interface, or NULL on error.
|
* Return a handle to the high level robot interface, or NULL on error.
|
||||||
*/
|
*/
|
||||||
@@ -59,8 +42,6 @@ int robot_stop(Robot robot);
|
|||||||
*/
|
*/
|
||||||
int robot_aim(Robot, float p, float y);
|
int robot_aim(Robot, float p, float y);
|
||||||
|
|
||||||
int robot_target(Robot robot, float pitch, float yaw);
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* Set the velocity of the robot chassis. A packet will be sent to the robot on
|
* Set the velocity of the robot chassis. A packet will be sent to the robot on
|
||||||
* the next ready tick of the work function.
|
* the next ready tick of the work function.
|
||||||
|
|||||||
@@ -1,8 +1,8 @@
|
|||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
// Public stuff
|
// Public stuff
|
||||||
struct ClientImp;
|
struct Client;
|
||||||
typedef struct ClientImp* Client;
|
typedef struct Client* Client;
|
||||||
|
|
||||||
#include "message.h"
|
#include "message.h"
|
||||||
#include "chassis.h"
|
#include "chassis.h"
|
||||||
|
|||||||
@@ -108,3 +108,34 @@ subscribe_add_node (
|
|||||||
uint16_t seq,
|
uint16_t seq,
|
||||||
bool ack );
|
bool ack );
|
||||||
|
|
||||||
|
#define ADD_SUB_MSG_CMD 0x0348
|
||||||
|
|
||||||
|
#define PUSH_PERIOD_MSG_CMD 0x0848
|
||||||
|
|
||||||
|
#define DDS_BATTERY 0x000200096862229f
|
||||||
|
#define DDS_GIMBAL_BASE 0x00020009f5882874
|
||||||
|
#define DDS_VELOCITY 0x0002000949a4009c
|
||||||
|
#define DDS_ESC 0x00020009c14cb7c5
|
||||||
|
#define DDS_ATTITUDE 0x000200096b986306
|
||||||
|
#define DDS_IMU 0x00020009a7985b8d
|
||||||
|
#define DDS_POSITION 0x00020009eeb7cece
|
||||||
|
#define DDS_SA_STATUS 0x000200094a2c6d55
|
||||||
|
#define DDS_CHASSIS_MODE 0x000200094fcb1146
|
||||||
|
#define DDS_SBUS 0x0002000988223568
|
||||||
|
#define DDS_SERVO 0x000200095f0059e7
|
||||||
|
#define DDS_ARM 0x0002000926abd64d
|
||||||
|
#define DDS_GRIPPER 0x00020009124d156a
|
||||||
|
#define DDS_GIMBAL_POS 0x00020009f79b3c97
|
||||||
|
#define DDS_STICK 0x0002000955e9a0fa
|
||||||
|
#define DDS_MOVE_MODE 0x00020009784c7bfd
|
||||||
|
#define DDS_TOF 0x0002000986e4c05a
|
||||||
|
#define DDS_PINBOARD 0x00020009eebb9ffc
|
||||||
|
|
||||||
|
void
|
||||||
|
add_sub_msg (
|
||||||
|
union Request* req,
|
||||||
|
uint16_t seq,
|
||||||
|
bool ack,
|
||||||
|
uint8_t msg_id,
|
||||||
|
uint8_t freq,
|
||||||
|
uint64_t subject_uuid );
|
||||||
|
|||||||
+1
-1
@@ -2,7 +2,7 @@
|
|||||||
|
|
||||||
#include <stdint.h>
|
#include <stdint.h>
|
||||||
|
|
||||||
struct ClientImp {
|
struct Client {
|
||||||
|
|
||||||
uint16_t seq;
|
uint16_t seq;
|
||||||
|
|
||||||
|
|||||||
+28
-28
@@ -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>
|
||||||
@@ -41,10 +40,8 @@ message_length(int cmd) {
|
|||||||
return sizeof(struct BlasterFireReq);
|
return sizeof(struct BlasterFireReq);
|
||||||
case STREAM_CTRL_CMD:
|
case STREAM_CTRL_CMD:
|
||||||
return sizeof(struct StreamCtrlReq);
|
return sizeof(struct StreamCtrlReq);
|
||||||
case VISION_DETECT_ENABLE_CMD:
|
case ADD_SUB_MSG_CMD:
|
||||||
return sizeof(struct VisionDetectEnableReq);
|
return sizeof(struct AddSubMsgReq);
|
||||||
case GIMBAL_ROTATE_CMD:
|
|
||||||
return sizeof(struct GimbalRotateReq);
|
|
||||||
default:
|
default:
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
@@ -62,19 +59,17 @@ message_module(int cmd) {
|
|||||||
case SET_ROBOT_MODE_CMD:
|
case SET_ROBOT_MODE_CMD:
|
||||||
case SUBNODE_RESET_CMD:
|
case SUBNODE_RESET_CMD:
|
||||||
case SUBSCRIBE_ADD_NODE_CMD:
|
case SUBSCRIBE_ADD_NODE_CMD:
|
||||||
|
case ADD_SUB_MSG_CMD:
|
||||||
return host2byte(SDK_HOST, SDK_INDEX);
|
return host2byte(SDK_HOST, SDK_INDEX);
|
||||||
case SET_WHEEL_SPEED_CMD:
|
case SET_WHEEL_SPEED_CMD:
|
||||||
case CHASSIS_SPEED_MODE_CMD:
|
case CHASSIS_SPEED_MODE_CMD:
|
||||||
return host2byte(CHASSIS_HOST, CHASSIS_INDEX);
|
return host2byte(CHASSIS_HOST, CHASSIS_INDEX);
|
||||||
case GIMBAL_CTRL_SPEED_CMD:
|
case GIMBAL_CTRL_SPEED_CMD:
|
||||||
case GIMBAL_ROTATE_CMD:
|
|
||||||
return host2byte(GIMBAL_HOST, GIMBAL_INDEX);
|
return host2byte(GIMBAL_HOST, GIMBAL_INDEX);
|
||||||
case BLASTER_FIRE_CMD:
|
case BLASTER_FIRE_CMD:
|
||||||
return host2byte(BLASTER_HOST, BLASTER_INDEX);
|
return host2byte(BLASTER_HOST, BLASTER_INDEX);
|
||||||
case STREAM_CTRL_CMD:
|
case STREAM_CTRL_CMD:
|
||||||
return host2byte(STREAM_HOST, STREAM_INDEX);
|
return host2byte(STREAM_HOST, STREAM_INDEX);
|
||||||
case VISION_DETECT_ENABLE_CMD:
|
|
||||||
return host2byte(VISION_HOST, VISION_INDEX);
|
|
||||||
default:
|
default:
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
@@ -86,48 +81,53 @@ 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*
|
||||||
connection_poll_ready(struct ClientImp* client) {
|
connection_poll_ready(struct Client* client) {
|
||||||
|
|
||||||
// Return a null connection if no sockets have been opened
|
// Return a null connection if no sockets have been opened
|
||||||
if(max_fd < 0)
|
if(max_fd < 0)
|
||||||
|
|||||||
+13
-2
@@ -6,17 +6,28 @@
|
|||||||
#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 ClientImp* client);
|
connection_poll_ready(struct Client* client);
|
||||||
|
|
||||||
void
|
void
|
||||||
connection_read(struct Connection* connection, union Message* resp);
|
connection_read(struct Connection* connection, union Message* resp);
|
||||||
|
|||||||
@@ -1,14 +1,9 @@
|
|||||||
#include "message.h"
|
#include "message.h"
|
||||||
#include "camera.h" // FIXME: hack
|
|
||||||
#include "crc.h"
|
#include "crc.h"
|
||||||
|
|
||||||
enum MESSAGEERR
|
enum MESSAGEERR
|
||||||
message_validate(const union Message* message) {
|
message_validate(const union Message* message) {
|
||||||
|
|
||||||
// FIXME: hack
|
|
||||||
if(message->header.cmd == VISION_DETECT_INFO_CMD)
|
|
||||||
return MESSAGEERR_NONE;
|
|
||||||
|
|
||||||
uint16_t length = message->header.length & 0x1FFF & ~0x400;
|
uint16_t length = message->header.length & 0x1FFF & ~0x400;
|
||||||
|
|
||||||
if(message->header.crc != crc8(message, 3))
|
if(message->header.crc != crc8(message, 3))
|
||||||
|
|||||||
@@ -17,12 +17,4 @@ stream_ctrl (
|
|||||||
req_finalize(seq, STREAM_CTRL_CMD, ack, req);
|
req_finalize(seq, STREAM_CTRL_CMD, ack, req);
|
||||||
}
|
}
|
||||||
|
|
||||||
void
|
|
||||||
vision_enable (
|
|
||||||
union Request* req,
|
|
||||||
uint16_t seq,
|
|
||||||
bool ack ) {
|
|
||||||
req->enablevision.type = VISION_DETECT_ENABLE_PERSON;
|
|
||||||
req_finalize(seq, VISION_DETECT_ENABLE_CMD, ack, req);
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|||||||
@@ -16,35 +16,3 @@ gimbal_ctrl_speed (
|
|||||||
req->gimbspeed.ctrl = 0xDC;
|
req->gimbspeed.ctrl = 0xDC;
|
||||||
req_finalize(seq, GIMBAL_CTRL_SPEED_CMD, ack, req);
|
req_finalize(seq, GIMBAL_CTRL_SPEED_CMD, ack, req);
|
||||||
}
|
}
|
||||||
|
|
||||||
void
|
|
||||||
gimbal_rotate (
|
|
||||||
union Request* req,
|
|
||||||
uint16_t seq,
|
|
||||||
bool ack,
|
|
||||||
int16_t p,
|
|
||||||
int16_t y ) {
|
|
||||||
//req->gimbalrot.coordinate = 1;
|
|
||||||
//req->gimbalrot.yaw_valid = 1;
|
|
||||||
//req->gimbalrot.pitch_valid = 1;
|
|
||||||
//req->gimbalrot.roll_valid = 0;
|
|
||||||
req->gimbalrot.coord_bitfield = 0;
|
|
||||||
req->gimbalrot.coord_bitfield = 1 | 0 << 1 | 1 << 2 | 1 << 3;
|
|
||||||
|
|
||||||
//req->gimbalrot.frequency = 2;
|
|
||||||
//req->gimbalrot.control = 0;
|
|
||||||
req->gimbalrot.control_bitfield = 0;
|
|
||||||
req->gimbalrot.control_bitfield = 0 | 1 << 2;
|
|
||||||
|
|
||||||
req->gimbalrot.id = 33;
|
|
||||||
req->gimbalrot.yaw = 0;
|
|
||||||
req->gimbalrot.yaw = y;// >> 8;//(y >> 8 | y << 8);
|
|
||||||
req->gimbalrot.roll = 0;
|
|
||||||
req->gimbalrot.pitch = 0;
|
|
||||||
req->gimbalrot.pitch = p;// >> 8;//(p >> 8 | y << 8);
|
|
||||||
req->gimbalrot.error = 0;
|
|
||||||
req->gimbalrot.yaw_speed = 15;
|
|
||||||
req->gimbalrot.roll_speed = 0;
|
|
||||||
req->gimbalrot.pitch_speed = 15;
|
|
||||||
req_finalize(seq, GIMBAL_ROTATE_CMD, ack, req);
|
|
||||||
}
|
|
||||||
|
|||||||
@@ -96,3 +96,24 @@ set_system_led (
|
|||||||
req_finalize(seq, SET_SYSTEM_LED_CMD, ack, req);
|
req_finalize(seq, SET_SYSTEM_LED_CMD, ack, req);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void
|
||||||
|
add_sub_msg (
|
||||||
|
union Request* req,
|
||||||
|
uint16_t seq,
|
||||||
|
bool ack,
|
||||||
|
uint8_t msg_id,
|
||||||
|
uint8_t freq,
|
||||||
|
uint64_t subject_uuid ) {
|
||||||
|
|
||||||
|
req->sub.node_id = host2byte(CLIENT_HOST, CLIENT_INDEX);
|
||||||
|
req->sub.msg_id = msg_id;
|
||||||
|
req->sub.reserved = 0; // NOTE: Always 0
|
||||||
|
req->sub.sub_mode = 0; // NOTE: Always 0
|
||||||
|
req->sub.subject_count = 1; // TODO: Add support for multiple
|
||||||
|
req->sub.subject_uuid = subject_uuid; // TODO: Add support for multiple
|
||||||
|
req->sub.freq = freq;
|
||||||
|
|
||||||
|
req_finalize(seq, ADD_SUB_MSG_CMD, ack, req);
|
||||||
|
|
||||||
|
}
|
||||||
|
|||||||
+55
-129
@@ -4,23 +4,13 @@
|
|||||||
|
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include <math.h>
|
#include <arpa/inet.h>
|
||||||
|
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
|
|
||||||
struct FragmentLink {
|
|
||||||
struct Fragment fragment;
|
|
||||||
struct FragmentLink *next;
|
|
||||||
};
|
|
||||||
|
|
||||||
struct RobotImp {
|
struct RobotImp {
|
||||||
|
|
||||||
struct {
|
struct Client* client;
|
||||||
struct FragmentLink *head;
|
|
||||||
struct FragmentLink *free;
|
|
||||||
} frag_queue;
|
|
||||||
|
|
||||||
struct ClientImp* client;
|
|
||||||
uint16_t seq;
|
uint16_t seq;
|
||||||
|
|
||||||
int16_t wheels[4];
|
int16_t wheels[4];
|
||||||
@@ -32,10 +22,6 @@ struct RobotImp {
|
|||||||
int16_t gimbal[2];
|
int16_t gimbal[2];
|
||||||
bool dirty_gimbal;
|
bool dirty_gimbal;
|
||||||
|
|
||||||
int16_t pitch;
|
|
||||||
int16_t yaw;
|
|
||||||
bool dirty_target;
|
|
||||||
|
|
||||||
bool dirty_blaster;
|
bool dirty_blaster;
|
||||||
|
|
||||||
bool stream_state;
|
bool stream_state;
|
||||||
@@ -43,6 +29,13 @@ struct RobotImp {
|
|||||||
|
|
||||||
bool sdk_mode;
|
bool sdk_mode;
|
||||||
|
|
||||||
|
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,
|
||||||
@@ -51,6 +44,8 @@ struct RobotImp {
|
|||||||
RESETTING_SUBNODE,
|
RESETTING_SUBNODE,
|
||||||
SUSBCRIBING_SUBNODE,
|
SUSBCRIBING_SUBNODE,
|
||||||
SETTING_MOVEMENT_MODE,
|
SETTING_MOVEMENT_MODE,
|
||||||
|
SUBSCRIBING_POSITION_STATUS,
|
||||||
|
SUBSCRIBING_GIMBAL_STATUS,
|
||||||
SENDING_HEARTBEAT,
|
SENDING_HEARTBEAT,
|
||||||
WAITING,
|
WAITING,
|
||||||
READY,
|
READY,
|
||||||
@@ -92,25 +87,6 @@ robot_aim(Robot robot, float p, float y) {
|
|||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
int
|
|
||||||
robot_target(Robot robot, float pitch, float yaw) {
|
|
||||||
// Get the FOV angle of the point in radians
|
|
||||||
float FOV = 120 * (M_PI / 180);
|
|
||||||
yaw = yaw * (FOV / 2);
|
|
||||||
pitch = pitch * (FOV / 2);
|
|
||||||
|
|
||||||
// Convert to degrees
|
|
||||||
yaw = yaw * (180 / M_PI);
|
|
||||||
pitch = pitch * (180 / M_PI);
|
|
||||||
|
|
||||||
// Convert for Robomaster
|
|
||||||
robot->yaw = yaw * 10;
|
|
||||||
robot->pitch = -pitch * 10;
|
|
||||||
|
|
||||||
robot->dirty_target = true;
|
|
||||||
return 1;
|
|
||||||
};
|
|
||||||
|
|
||||||
int
|
int
|
||||||
robot_led(Robot robot, unsigned char r, unsigned char g, unsigned char b) {
|
robot_led(Robot robot, unsigned char r, unsigned char g, unsigned char b) {
|
||||||
robot->colors[0] = r;
|
robot->colors[0] = r;
|
||||||
@@ -132,61 +108,6 @@ robot_heartbeat(Robot robot) {
|
|||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
inline
|
|
||||||
static
|
|
||||||
void
|
|
||||||
enqueue_fragment (
|
|
||||||
Robot robot,
|
|
||||||
const struct Fragment fragment ) {
|
|
||||||
|
|
||||||
struct FragmentLink *link;
|
|
||||||
if(robot->frag_queue.free) {
|
|
||||||
link = robot->frag_queue.free;
|
|
||||||
robot->frag_queue.free = link->next;
|
|
||||||
} else {
|
|
||||||
link = malloc(sizeof(struct FragmentLink));
|
|
||||||
link->next = NULL;
|
|
||||||
}
|
|
||||||
|
|
||||||
link->fragment = fragment;
|
|
||||||
|
|
||||||
if(robot->frag_queue.head) {
|
|
||||||
robot->frag_queue.head->next = link;
|
|
||||||
} else {
|
|
||||||
robot->frag_queue.head = link;
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
inline
|
|
||||||
static
|
|
||||||
void
|
|
||||||
dequeue_fragment (
|
|
||||||
Robot robot,
|
|
||||||
struct Fragment *fragment ) {
|
|
||||||
|
|
||||||
fragment->type = 0;
|
|
||||||
if(!robot->frag_queue.head)
|
|
||||||
return;
|
|
||||||
|
|
||||||
struct FragmentLink *link = robot->frag_queue.head;
|
|
||||||
*fragment = link->fragment;
|
|
||||||
|
|
||||||
robot->frag_queue.head = link->next;
|
|
||||||
link->next = NULL;
|
|
||||||
if(robot->frag_queue.free) {
|
|
||||||
robot->frag_queue.free->next = link;
|
|
||||||
} else {
|
|
||||||
robot->frag_queue.free = link;
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
int robot_poll(Robot robot, struct Fragment* fragment) {
|
|
||||||
dequeue_fragment(robot, fragment);
|
|
||||||
return fragment->type;
|
|
||||||
}
|
|
||||||
|
|
||||||
int
|
int
|
||||||
robot_work(Robot robot) {
|
robot_work(Robot robot) {
|
||||||
|
|
||||||
@@ -199,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;
|
||||||
}
|
}
|
||||||
@@ -214,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;
|
||||||
}
|
}
|
||||||
@@ -245,7 +168,7 @@ robot_work(Robot robot) {
|
|||||||
|
|
||||||
case SETTING_MOVEMENT_MODE:
|
case SETTING_MOVEMENT_MODE:
|
||||||
{
|
{
|
||||||
set_robot_mode(&req, robot->seq++, true, MOVEMENTMODE_GIMBAL_LEAD);
|
set_robot_mode(&req, robot->seq++, true, MOVEMENTMODE_FREE);
|
||||||
req_send(robot->client->dev_conn, &req);
|
req_send(robot->client->dev_conn, &req);
|
||||||
robot->state = WAITING;
|
robot->state = WAITING;
|
||||||
break;
|
break;
|
||||||
@@ -259,6 +182,22 @@ robot_work(Robot robot) {
|
|||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
case SUBSCRIBING_POSITION_STATUS:
|
||||||
|
{
|
||||||
|
add_sub_msg ( &req, robot->seq++, true, 20, 1, DDS_POSITION );
|
||||||
|
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;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
case READY:
|
case READY:
|
||||||
{
|
{
|
||||||
if(robot->dirty_wheels) {
|
if(robot->dirty_wheels) {
|
||||||
@@ -280,14 +219,6 @@ robot_work(Robot robot) {
|
|||||||
req_send(robot->client->dev_conn, &req);
|
req_send(robot->client->dev_conn, &req);
|
||||||
robot->dirty_gimbal = false;
|
robot->dirty_gimbal = false;
|
||||||
}
|
}
|
||||||
if(robot->dirty_target) {
|
|
||||||
gimbal_rotate (
|
|
||||||
&req, robot->seq++, true,
|
|
||||||
robot->pitch,
|
|
||||||
robot->yaw );
|
|
||||||
req_send(robot->client->dev_conn, &req);
|
|
||||||
robot->dirty_target = false;
|
|
||||||
}
|
|
||||||
if(robot->dirty_colors) {
|
if(robot->dirty_colors) {
|
||||||
set_system_led (
|
set_system_led (
|
||||||
&req, robot->seq++, false,
|
&req, robot->seq++, false,
|
||||||
@@ -338,37 +269,10 @@ robot_work(Robot robot) {
|
|||||||
|
|
||||||
union Message resp;
|
union Message resp;
|
||||||
poll_message(robot->client, &resp);
|
poll_message(robot->client, &resp);
|
||||||
if(resp.header.cmd) {
|
|
||||||
const struct Fragment fragment = {
|
|
||||||
.type = FRAGMENT_MESSAGE,
|
|
||||||
.message = resp
|
|
||||||
};
|
|
||||||
enqueue_fragment(robot, fragment);
|
|
||||||
}
|
|
||||||
switch(resp.header.cmd) {
|
switch(resp.header.cmd) {
|
||||||
case 0:
|
case 0:
|
||||||
break;
|
break;
|
||||||
case VISION_DETECT_INFO_CMD:
|
default:
|
||||||
// enqueue rectangles
|
|
||||||
for(int i = 0; i < resp.push.vision.count; i++) {
|
|
||||||
int type;
|
|
||||||
switch(resp.push.vision.type) {
|
|
||||||
case VISIONTYPE_SHOULDER:
|
|
||||||
case VISIONTYPE_PERSON:
|
|
||||||
case VISIONTYPE_GESTURE:
|
|
||||||
case VISIONTYPE_ROBOT:
|
|
||||||
type = FRAGMENT_RECTANGLE;
|
|
||||||
case VISIONTYPE_LINE:
|
|
||||||
type = FRAGMENT_LINE;
|
|
||||||
case VISIONTYPE_MARKER:
|
|
||||||
type = FRAGMENT_MARKER;
|
|
||||||
}
|
|
||||||
const struct Fragment fragment = {
|
|
||||||
.type = type,
|
|
||||||
.rect = resp.push.vision.rects[i]
|
|
||||||
};
|
|
||||||
enqueue_fragment(robot, fragment);
|
|
||||||
}
|
|
||||||
break;
|
break;
|
||||||
case SET_SDK_CONNECTION_CMD:
|
case SET_SDK_CONNECTION_CMD:
|
||||||
// TODO: Do more with this
|
// TODO: Do more with this
|
||||||
@@ -376,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:
|
||||||
@@ -405,6 +310,13 @@ robot_work(Robot robot) {
|
|||||||
robot->state = STOPPED;
|
robot->state = STOPPED;
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
robot->state = SUBSCRIBING_POSITION_STATUS;
|
||||||
|
break;
|
||||||
|
case ADD_SUB_MSG_CMD:
|
||||||
|
if(resp.resp.sub.retcode) {
|
||||||
|
robot->state = STOPPED;
|
||||||
|
break;
|
||||||
|
}
|
||||||
robot->state = SENDING_HEARTBEAT;
|
robot->state = SENDING_HEARTBEAT;
|
||||||
break;
|
break;
|
||||||
case SDK_HEARTBEAT_CMD:
|
case SDK_HEARTBEAT_CMD:
|
||||||
@@ -414,6 +326,20 @@ robot_work(Robot robot) {
|
|||||||
}
|
}
|
||||||
robot->state = READY;
|
robot->state = READY;
|
||||||
break;
|
break;
|
||||||
|
case PUSH_PERIOD_MSG_CMD:
|
||||||
|
switch(resp.resp.push.msg_id) {
|
||||||
|
case 20:
|
||||||
|
robot->x = resp.resp.push.position.x;
|
||||||
|
robot->y = resp.resp.push.position.y;
|
||||||
|
robot->z = resp.resp.push.position.z;
|
||||||
|
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;
|
||||||
|
|||||||
+2
-2
@@ -8,8 +8,8 @@
|
|||||||
#include <string.h>
|
#include <string.h>
|
||||||
|
|
||||||
Client client_new() {
|
Client client_new() {
|
||||||
struct ClientImp* client = malloc(sizeof(struct ClientImp));
|
struct Client* client = malloc(sizeof(struct Client));
|
||||||
memset(client, 0, sizeof(struct ClientImp));
|
memset(client, 0, sizeof(struct Client));
|
||||||
// TODO: Make this configurable
|
// TODO: Make this configurable
|
||||||
return client;
|
return client;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -0,0 +1,140 @@
|
|||||||
|
#include "roboeasy.h"
|
||||||
|
#include "SDL2/SDL.h"
|
||||||
|
|
||||||
|
#include <stdio.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include <stdbool.h>
|
||||||
|
|
||||||
|
struct {
|
||||||
|
unsigned char r;
|
||||||
|
unsigned char g;
|
||||||
|
unsigned char b;
|
||||||
|
} const colors[] = {
|
||||||
|
{0xFF, 0x00, 0x00},
|
||||||
|
{0x00, 0xFF, 0x00},
|
||||||
|
{0x00, 0x00, 0xFF}
|
||||||
|
};
|
||||||
|
int color = 0;
|
||||||
|
|
||||||
|
float pitch = 0, yaw = 0;
|
||||||
|
float x = 0, y = 0, z = 0;
|
||||||
|
static Uint32 drive_timer_handler(Uint32 interval, void* param) {
|
||||||
|
robot_drive((Robot)param, x, y, z);
|
||||||
|
robot_aim((Robot)param, pitch, yaw);
|
||||||
|
return 75;
|
||||||
|
}
|
||||||
|
static Uint32 heartbeat_timer_handler(Uint32 interval, void* param) {
|
||||||
|
robot_heartbeat((Robot)param);
|
||||||
|
return 1000;
|
||||||
|
}
|
||||||
|
|
||||||
|
int main(int argc, char* argv[]) {
|
||||||
|
if(SDL_Init(SDL_INIT_VIDEO | SDL_INIT_JOYSTICK) < 0) {
|
||||||
|
fprintf(stderr, "%s", SDL_GetError());
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
printf("Detected %d joysticks\n", SDL_NumJoysticks());
|
||||||
|
SDL_Joystick* joystick = NULL;
|
||||||
|
if(SDL_NumJoysticks() > 0) {
|
||||||
|
joystick = SDL_JoystickOpen(0);
|
||||||
|
}
|
||||||
|
SDL_Window* win = SDL_CreateWindow(
|
||||||
|
"Robomaster",
|
||||||
|
SDL_WINDOWPOS_UNDEFINED,
|
||||||
|
SDL_WINDOWPOS_UNDEFINED,
|
||||||
|
800, 300,
|
||||||
|
SDL_WINDOW_RESIZABLE );
|
||||||
|
if(!win) {
|
||||||
|
fprintf(stderr, "%s", SDL_GetError());
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
Robot robot = robot_new();
|
||||||
|
robot_init(robot);
|
||||||
|
robot_stream(robot, true);
|
||||||
|
|
||||||
|
SDL_AddTimer(75, drive_timer_handler, robot);
|
||||||
|
SDL_AddTimer(1000, heartbeat_timer_handler, robot);
|
||||||
|
|
||||||
|
int h, w;
|
||||||
|
SDL_GetWindowSize(win, &w, &h);
|
||||||
|
|
||||||
|
while(robot_work(robot)) {
|
||||||
|
SDL_Event event;
|
||||||
|
while(SDL_PollEvent(&event)) {
|
||||||
|
switch(event.type) {
|
||||||
|
case SDL_KEYUP:
|
||||||
|
case SDL_KEYDOWN:
|
||||||
|
switch(event.key.keysym.scancode) {
|
||||||
|
case SDL_SCANCODE_Q:
|
||||||
|
z = event.type == SDL_KEYUP ? 0 : 0.25;
|
||||||
|
break;
|
||||||
|
case SDL_SCANCODE_E:
|
||||||
|
z = event.type == SDL_KEYUP ? 0 : -0.25;
|
||||||
|
break;
|
||||||
|
case SDL_SCANCODE_LEFT:
|
||||||
|
case SDL_SCANCODE_A:
|
||||||
|
x = event.type == SDL_KEYUP ? 0 : 0.25;
|
||||||
|
break;
|
||||||
|
case SDL_SCANCODE_RIGHT:
|
||||||
|
case SDL_SCANCODE_D:
|
||||||
|
x = event.type == SDL_KEYUP ? 0 : -0.25;
|
||||||
|
break;
|
||||||
|
case SDL_SCANCODE_UP:
|
||||||
|
case SDL_SCANCODE_W:
|
||||||
|
y = event.type == SDL_KEYUP ? 0 : 0.25;
|
||||||
|
break;
|
||||||
|
case SDL_SCANCODE_DOWN:
|
||||||
|
case SDL_SCANCODE_S:
|
||||||
|
y = event.type == SDL_KEYUP ? 0 : -0.25;
|
||||||
|
break;
|
||||||
|
case SDL_SCANCODE_SPACE:
|
||||||
|
robot_led(robot, colors[color].r, colors[color].g, colors[color].b);
|
||||||
|
color = (color + 1) % 3;
|
||||||
|
break;
|
||||||
|
case SDL_SCANCODE_RETURN:
|
||||||
|
robot_blast(robot);
|
||||||
|
break;
|
||||||
|
default: break;
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
case SDL_MOUSEMOTION:
|
||||||
|
yaw = (float)event.motion.xrel;
|
||||||
|
pitch = (float)event.motion.yrel;
|
||||||
|
break;
|
||||||
|
case SDL_JOYAXISMOTION:
|
||||||
|
switch(event.jaxis.axis) {
|
||||||
|
case 0:
|
||||||
|
x = (float)event.jaxis.value / 32767;
|
||||||
|
break;
|
||||||
|
case 1:
|
||||||
|
y = (float)event.jaxis.value / 32767;
|
||||||
|
break;
|
||||||
|
case 4:
|
||||||
|
z = (float)event.jaxis.value / 32767 / 2;
|
||||||
|
break;
|
||||||
|
case 2:
|
||||||
|
yaw = (float)event.jaxis.value / 32767;
|
||||||
|
break;
|
||||||
|
case 3:
|
||||||
|
pitch = (float)event.jaxis.value / 32767;
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
printf("axis: %d\n", event.jaxis.axis);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
case SDL_WINDOWEVENT:
|
||||||
|
if(event.window.event != SDL_WINDOWEVENT_CLOSE) break;
|
||||||
|
case SDL_QUIT:
|
||||||
|
robot_stop(robot);
|
||||||
|
default: break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
SDL_JoystickClose(joystick);
|
||||||
|
SDL_Quit();
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
-417
@@ -1,417 +0,0 @@
|
|||||||
extern "C" {
|
|
||||||
#include "robomaster.h"
|
|
||||||
#include "roboeasy.h"
|
|
||||||
#include <libavcodec/avcodec.h>
|
|
||||||
#include <libavformat/avformat.h>
|
|
||||||
#include <libswscale/swscale.h>
|
|
||||||
#include <libavutil/imgutils.h>
|
|
||||||
}
|
|
||||||
|
|
||||||
#include <SDL2/SDL.h>
|
|
||||||
#include <opencv2/opencv.hpp>
|
|
||||||
#include <opencv2/dnn/dnn.hpp>
|
|
||||||
|
|
||||||
#include <stdio.h>
|
|
||||||
#include <stdlib.h>
|
|
||||||
#include <math.h>
|
|
||||||
#include <thread>
|
|
||||||
#include <mutex>
|
|
||||||
|
|
||||||
struct {
|
|
||||||
unsigned char r;
|
|
||||||
unsigned char g;
|
|
||||||
unsigned char b;
|
|
||||||
} const colors[] = {
|
|
||||||
{0xFF, 0x00, 0x00},
|
|
||||||
{0x00, 0xFF, 0x00},
|
|
||||||
{0x00, 0x00, 0xFF}
|
|
||||||
};
|
|
||||||
int color = 0;
|
|
||||||
|
|
||||||
float x = 0, y = 0, z = 0;
|
|
||||||
int target_x = 0, target_y = 0;
|
|
||||||
int yaw_cur = 0, pitch_cur = 0;
|
|
||||||
static Uint32 drive_timer_handler(Uint32 interval, void* param) {
|
|
||||||
robot_drive((Robot)param, x, y, z);
|
|
||||||
return 75;
|
|
||||||
}
|
|
||||||
static Uint32 heartbeat_timer_handler(Uint32 interval, void* param) {
|
|
||||||
robot_heartbeat((Robot)param);
|
|
||||||
return 1000;
|
|
||||||
}
|
|
||||||
|
|
||||||
std::mutex mtx;
|
|
||||||
cv::Mat img;
|
|
||||||
std::vector<cv::Rect> found;
|
|
||||||
std::vector<double> weights;
|
|
||||||
unsigned target;
|
|
||||||
|
|
||||||
bool stop = false;
|
|
||||||
bool track = false;
|
|
||||||
|
|
||||||
static void processFrameThread(Robot robot) {
|
|
||||||
cv::dnn::Net net = cv::dnn::readNet("yolov3-tiny.weights", "yolov3-tiny.cfg");
|
|
||||||
|
|
||||||
while(!stop) {
|
|
||||||
std::unique_lock<std::mutex> lock(mtx);
|
|
||||||
|
|
||||||
if(!img.empty()) {
|
|
||||||
found.clear();
|
|
||||||
weights.clear();
|
|
||||||
cv::Mat blob = cv::dnn::blobFromImage(img, 1/255.0, cv::Size(416, 416), cv::Scalar(0, 0, 0), true, false);
|
|
||||||
net.setInput(blob);
|
|
||||||
std::vector<cv::Mat> outs;
|
|
||||||
net.forward(outs, net.getUnconnectedOutLayersNames());
|
|
||||||
float *data = (float*)outs[0].data;
|
|
||||||
for(int i = 0; i < outs[0].rows; ++i) {
|
|
||||||
float final_score = data[4] * data[5];
|
|
||||||
if(final_score >= 0.0075)
|
|
||||||
{
|
|
||||||
weights.push_back(final_score);
|
|
||||||
int cx = data[0] * img.cols;
|
|
||||||
int cy = data[1] * img.rows;
|
|
||||||
int width = data[2] * img.cols;
|
|
||||||
int height = data[3] * img.rows;
|
|
||||||
int left = cx - width / 2;
|
|
||||||
int top = cy - height / 2;
|
|
||||||
found.push_back(cv::Rect(left, top, width, height));
|
|
||||||
}
|
|
||||||
data += 85;
|
|
||||||
}
|
|
||||||
|
|
||||||
target = 0;
|
|
||||||
for(unsigned i = 0; i < weights.size(); i++) {
|
|
||||||
if(weights[i] > weights[target])
|
|
||||||
target = i;
|
|
||||||
}
|
|
||||||
|
|
||||||
if(track && !weights.empty()) {
|
|
||||||
float yaw = target_x = found[target].x;
|
|
||||||
float pitch = target_y = found[target].y;
|
|
||||||
|
|
||||||
// Normalize the coordinates
|
|
||||||
yaw = 2 * (yaw - img.cols / 2) / img.cols;
|
|
||||||
pitch = 2 * (pitch - img.rows / 2) / img.rows;
|
|
||||||
|
|
||||||
robot_target(robot, pitch, yaw);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
lock.unlock();
|
|
||||||
|
|
||||||
SDL_Delay(250);
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
static void captureFrameThread(SDL_Window* window, const char* fname) {
|
|
||||||
|
|
||||||
SDL_Delay(750);
|
|
||||||
|
|
||||||
av_register_all();
|
|
||||||
avcodec_register_all();
|
|
||||||
AVFormatContext* pFormatCtx = avformat_alloc_context();
|
|
||||||
|
|
||||||
if (avformat_open_input(&pFormatCtx, fname, NULL, NULL) != 0) {
|
|
||||||
std::cerr << "Couldn't open stream\n";
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (avformat_find_stream_info(pFormatCtx, NULL) < 0) {
|
|
||||||
std::cerr << "Couldn't find stream information\n";
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
int videoStream = -1;
|
|
||||||
for (unsigned int i = 0; i < pFormatCtx->nb_streams; i++) {
|
|
||||||
if (pFormatCtx->streams[i]->codecpar->codec_type == AVMEDIA_TYPE_VIDEO) {
|
|
||||||
videoStream = i;
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if (videoStream == -1) {
|
|
||||||
std::cerr << "Didn't find a video stream\n";
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
AVCodecParameters* pCodecParameters = pFormatCtx->streams[videoStream]->codecpar;
|
|
||||||
AVCodec* pCodec = avcodec_find_decoder(pCodecParameters->codec_id);
|
|
||||||
|
|
||||||
if (pCodec == NULL) {
|
|
||||||
std::cerr << "Unsupported codec\n";
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
AVCodecContext* pCodecCtx = avcodec_alloc_context3(pCodec);
|
|
||||||
pCodecCtx->pix_fmt = AV_PIX_FMT_GRAY8;
|
|
||||||
avcodec_parameters_to_context(pCodecCtx, pCodecParameters);
|
|
||||||
|
|
||||||
if (avcodec_open2(pCodecCtx, pCodec, NULL) < 0) {
|
|
||||||
std::cerr << "Could not open codec\n";
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
AVFrame* pFrame = av_frame_alloc();
|
|
||||||
AVFrame* pFrameRGB = av_frame_alloc();
|
|
||||||
|
|
||||||
int numBytes = av_image_get_buffer_size(AV_PIX_FMT_RGB24, pCodecCtx->width, pCodecCtx->height, 1);
|
|
||||||
uint8_t* buffer = (uint8_t*)av_malloc(numBytes * sizeof(uint8_t));
|
|
||||||
av_image_fill_arrays(pFrameRGB->data, pFrameRGB->linesize, buffer, AV_PIX_FMT_BGR24, pCodecCtx->width, pCodecCtx->height, 1);
|
|
||||||
|
|
||||||
SwsContext* sws_ctx = sws_getContext(
|
|
||||||
pCodecCtx->width,
|
|
||||||
pCodecCtx->height,
|
|
||||||
pCodecCtx->pix_fmt,
|
|
||||||
pCodecCtx->width,
|
|
||||||
pCodecCtx->height,
|
|
||||||
AV_PIX_FMT_BGR24,
|
|
||||||
SWS_BILINEAR,
|
|
||||||
NULL,
|
|
||||||
NULL,
|
|
||||||
NULL);
|
|
||||||
|
|
||||||
SDL_Renderer* renderer = SDL_CreateRenderer(window, -1, 0);
|
|
||||||
SDL_Texture* texture = SDL_CreateTexture(renderer, SDL_PIXELFORMAT_BGR24, SDL_TEXTUREACCESS_STATIC, pCodecCtx->width, pCodecCtx->height);
|
|
||||||
|
|
||||||
while (!stop) {
|
|
||||||
AVPacket packet;
|
|
||||||
if(av_read_frame(pFormatCtx, &packet) < 0) {
|
|
||||||
std::cerr << "Error while reading a frame\n";
|
|
||||||
stop = true;
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (packet.stream_index == videoStream) {
|
|
||||||
int response = avcodec_send_packet(pCodecCtx, &packet);
|
|
||||||
if (response < 0) {
|
|
||||||
std::cerr << "Error while sending a packet to the decoder: " << response << '\n';
|
|
||||||
stop = true;
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
|
|
||||||
while (response >= 0) {
|
|
||||||
response = avcodec_receive_frame(pCodecCtx, pFrame);
|
|
||||||
if (response == AVERROR(EAGAIN) || response == AVERROR_EOF) {
|
|
||||||
break;
|
|
||||||
} else if (response < 0) {
|
|
||||||
std::cerr << "Error while receiving a frame from the decoder: " << response << '\n';
|
|
||||||
stop = true;
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (response >= 0) {
|
|
||||||
sws_scale(sws_ctx, (uint8_t const* const*)pFrame->data, pFrame->linesize, 0, pCodecCtx->height, pFrameRGB->data, pFrameRGB->linesize);
|
|
||||||
|
|
||||||
std::unique_lock<std::mutex> lock(mtx);
|
|
||||||
|
|
||||||
img = cv::Mat(pCodecCtx->height, pCodecCtx->width, CV_8UC3, pFrameRGB->data[0], pFrameRGB->linesize[0]);
|
|
||||||
for (unsigned i = 0; i < found.size(); i++) {
|
|
||||||
rectangle(img, found[i], cv::Scalar(255 - weights[i] * 255, 0, weights[i] * 255), 3);
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
SDL_UpdateTexture(texture, NULL, img.data, img.cols * 3);
|
|
||||||
SDL_RenderDrawLine(renderer, 1280 / 2, 720 / 2, target_x, target_y);
|
|
||||||
|
|
||||||
SDL_RenderClear(renderer);
|
|
||||||
SDL_RenderCopy(renderer, texture, NULL, NULL);
|
|
||||||
SDL_RenderPresent(renderer);
|
|
||||||
|
|
||||||
lock.unlock();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
av_packet_unref(&packet);
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
av_free(buffer);
|
|
||||||
av_frame_free(&pFrameRGB);
|
|
||||||
av_frame_free(&pFrame);
|
|
||||||
avcodec_close(pCodecCtx);
|
|
||||||
avformat_close_input(&pFormatCtx);
|
|
||||||
|
|
||||||
SDL_DestroyTexture(texture);
|
|
||||||
SDL_DestroyRenderer(renderer);
|
|
||||||
}
|
|
||||||
|
|
||||||
int main(int argc, char* argv[]) {
|
|
||||||
if(SDL_Init(SDL_INIT_VIDEO | SDL_INIT_JOYSTICK) < 0) {
|
|
||||||
fprintf(stderr, "%s", SDL_GetError());
|
|
||||||
return 1;
|
|
||||||
}
|
|
||||||
printf("Detected %d joysticks\n", SDL_NumJoysticks());
|
|
||||||
SDL_Joystick* joystick = NULL;
|
|
||||||
if(SDL_NumJoysticks() > 0) {
|
|
||||||
joystick = SDL_JoystickOpen(0);
|
|
||||||
}
|
|
||||||
SDL_Window* win = SDL_CreateWindow(
|
|
||||||
"Robomaster",
|
|
||||||
SDL_WINDOWPOS_UNDEFINED,
|
|
||||||
SDL_WINDOWPOS_UNDEFINED,
|
|
||||||
1280, 720,
|
|
||||||
SDL_WINDOW_RESIZABLE );
|
|
||||||
if(!win) {
|
|
||||||
fprintf(stderr, "%s", SDL_GetError());
|
|
||||||
return 1;
|
|
||||||
}
|
|
||||||
|
|
||||||
Robot robot = robot_new();
|
|
||||||
robot_init(robot);
|
|
||||||
robot_stream(robot, true);
|
|
||||||
|
|
||||||
SDL_AddTimer(75, drive_timer_handler, robot);
|
|
||||||
SDL_AddTimer(1000, heartbeat_timer_handler, robot);
|
|
||||||
|
|
||||||
int streamcount = 0;
|
|
||||||
std::thread *captureThread = nullptr;
|
|
||||||
std::thread *processThread = nullptr;
|
|
||||||
|
|
||||||
target_x = 720 / 2;
|
|
||||||
target_y = 1280 / 2;
|
|
||||||
|
|
||||||
while(robot_work(robot)) {
|
|
||||||
|
|
||||||
int h = 720, w = 1280;
|
|
||||||
|
|
||||||
struct Fragment fragment;
|
|
||||||
while(robot_poll(robot, &fragment)) {
|
|
||||||
switch(fragment.type) {
|
|
||||||
case FRAGMENT_RECTANGLE:
|
|
||||||
case FRAGMENT_LINE:
|
|
||||||
case FRAGMENT_MARKER:
|
|
||||||
printf("Rect(%f,%f,%f,%f)\n",
|
|
||||||
fragment.rect.rect.x,
|
|
||||||
fragment.rect.rect.y,
|
|
||||||
fragment.rect.rect.w,
|
|
||||||
fragment.rect.rect.h );
|
|
||||||
break;
|
|
||||||
case FRAGMENT_MESSAGE:
|
|
||||||
switch(fragment.message.header.cmd) {
|
|
||||||
case GIMBAL_ACTION_PUSH_CMD:
|
|
||||||
if(fragment.message.push.gimbalaction.state != 1)
|
|
||||||
break;
|
|
||||||
yaw_cur = fragment.message.push.gimbalaction.yaw;
|
|
||||||
pitch_cur = fragment.message.push.gimbalaction.pitch;
|
|
||||||
printf("Gimbal action %d\n", fragment.message.push.gimbalaction.id);
|
|
||||||
printf("\tProgress %d\n", fragment.message.push.gimbalaction.progress);
|
|
||||||
printf("\tYaw %d, Pitch %d\n", yaw_cur, pitch_cur);
|
|
||||||
break;
|
|
||||||
case GIMBAL_ROTATE_CMD:
|
|
||||||
if(fragment.message.resp.gimbalrot.retcode) {
|
|
||||||
printf("ERROR: Gimbal rotate message failure\n");
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
break;
|
|
||||||
case STREAM_CTRL_CMD:
|
|
||||||
if(fragment.message.resp.stream.retcode) {
|
|
||||||
printf("ERROR: Stream not enabled\n");
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
printf("Stream enabled\n");
|
|
||||||
if(++streamcount >= 2) {
|
|
||||||
captureThread = new std::thread(captureFrameThread, win, "tcp://192.168.2.1:40921");
|
|
||||||
processThread = new std::thread(processFrameThread, robot);
|
|
||||||
}
|
|
||||||
break;
|
|
||||||
case VISION_DETECT_ENABLE_CMD:
|
|
||||||
if(fragment.message.resp.enablevision.retcode) {
|
|
||||||
printf("ERROR: Vision not enabled\n");
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
if(fragment.message.resp.enablevision.error) {
|
|
||||||
printf("ERROR: Vision not enabled %d\n",
|
|
||||||
fragment.message.resp.enablevision.error);
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
printf("Vision enabled\n");
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
break;
|
|
||||||
default:
|
|
||||||
printf("Unhandled fragment type\n");
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
SDL_Event event;
|
|
||||||
while(SDL_PollEvent(&event)) {
|
|
||||||
switch(event.type) {
|
|
||||||
case SDL_KEYUP:
|
|
||||||
case SDL_KEYDOWN:
|
|
||||||
switch(event.key.keysym.scancode) {
|
|
||||||
case SDL_SCANCODE_Q:
|
|
||||||
z = event.type == SDL_KEYUP ? 0 : 0.25;
|
|
||||||
break;
|
|
||||||
case SDL_SCANCODE_E:
|
|
||||||
z = event.type == SDL_KEYUP ? 0 : -0.25;
|
|
||||||
break;
|
|
||||||
case SDL_SCANCODE_LEFT:
|
|
||||||
case SDL_SCANCODE_A:
|
|
||||||
x = event.type == SDL_KEYUP ? 0 : 0.25;
|
|
||||||
break;
|
|
||||||
case SDL_SCANCODE_RIGHT:
|
|
||||||
case SDL_SCANCODE_D:
|
|
||||||
x = event.type == SDL_KEYUP ? 0 : -0.25;
|
|
||||||
break;
|
|
||||||
case SDL_SCANCODE_UP:
|
|
||||||
case SDL_SCANCODE_W:
|
|
||||||
y = event.type == SDL_KEYUP ? 0 : 0.25;
|
|
||||||
break;
|
|
||||||
case SDL_SCANCODE_DOWN:
|
|
||||||
case SDL_SCANCODE_S:
|
|
||||||
y = event.type == SDL_KEYUP ? 0 : -0.25;
|
|
||||||
break;
|
|
||||||
case SDL_SCANCODE_SPACE:
|
|
||||||
robot_led(robot, colors[color].r, colors[color].g, colors[color].b);
|
|
||||||
color = (color + 1) % 3;
|
|
||||||
break;
|
|
||||||
case SDL_SCANCODE_RETURN:
|
|
||||||
robot_blast(robot);
|
|
||||||
break;
|
|
||||||
case SDL_SCANCODE_T:
|
|
||||||
track = true;
|
|
||||||
break;
|
|
||||||
default: break;
|
|
||||||
}
|
|
||||||
break;
|
|
||||||
case SDL_MOUSEBUTTONDOWN:
|
|
||||||
{
|
|
||||||
// Get window coordinates
|
|
||||||
SDL_GetMouseState(&target_x, &target_y);
|
|
||||||
|
|
||||||
float yaw = target_x;
|
|
||||||
float pitch = target_y;
|
|
||||||
|
|
||||||
// Normalize the coordinates
|
|
||||||
yaw = 2 * (yaw - w / 2) / w;
|
|
||||||
pitch = 2 * (pitch - h / 2) / h;
|
|
||||||
|
|
||||||
robot_target(robot, pitch, yaw);
|
|
||||||
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
case SDL_WINDOWEVENT:
|
|
||||||
if(event.window.event != SDL_WINDOWEVENT_CLOSE) break;
|
|
||||||
case SDL_QUIT:
|
|
||||||
robot_stop(robot);
|
|
||||||
stop = true;
|
|
||||||
default: break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if(captureThread != nullptr) {
|
|
||||||
captureThread->join();
|
|
||||||
delete captureThread;
|
|
||||||
}
|
|
||||||
if(processThread != nullptr) {
|
|
||||||
processThread->join();
|
|
||||||
delete processThread;
|
|
||||||
}
|
|
||||||
SDL_JoystickClose(joystick);
|
|
||||||
SDL_Quit();
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
-233
@@ -1,233 +0,0 @@
|
|||||||
#include <iostream>
|
|
||||||
#include <thread>
|
|
||||||
#include <mutex>
|
|
||||||
#include <cmath>
|
|
||||||
#include <opencv2/opencv.hpp>
|
|
||||||
#include <opencv2/dnn/dnn.hpp>
|
|
||||||
#include <SDL2/SDL.h>
|
|
||||||
|
|
||||||
extern "C" {
|
|
||||||
#include <libavcodec/avcodec.h>
|
|
||||||
#include <libavformat/avformat.h>
|
|
||||||
#include <libswscale/swscale.h>
|
|
||||||
#include <libavutil/imgutils.h>
|
|
||||||
}
|
|
||||||
|
|
||||||
std::mutex mtx;
|
|
||||||
cv::Mat img;
|
|
||||||
std::vector<cv::Rect> found;
|
|
||||||
std::vector<double> weights;
|
|
||||||
unsigned target;
|
|
||||||
|
|
||||||
bool stop = false;
|
|
||||||
|
|
||||||
static void processFrameThread() {
|
|
||||||
cv::dnn::Net net = cv::dnn::readNet("yolov3-tiny.weights", "yolov3-tiny.cfg");
|
|
||||||
|
|
||||||
while(!stop) {
|
|
||||||
std::unique_lock<std::mutex> lock(mtx);
|
|
||||||
|
|
||||||
if(!img.empty()) {
|
|
||||||
found.clear();
|
|
||||||
weights.clear();
|
|
||||||
cv::Mat blob = cv::dnn::blobFromImage(img, 1/255.0, cv::Size(416, 416), cv::Scalar(0, 0, 0), true, false);
|
|
||||||
net.setInput(blob);
|
|
||||||
std::vector<cv::Mat> outs;
|
|
||||||
net.forward(outs, net.getUnconnectedOutLayersNames());
|
|
||||||
float *data = (float*)outs[0].data;
|
|
||||||
for(int i = 0; i < outs[0].rows; ++i) {
|
|
||||||
float final_score = data[4] * data[5];
|
|
||||||
if(final_score >= 0.0075)
|
|
||||||
{
|
|
||||||
weights.push_back(final_score);
|
|
||||||
int cx = data[0] * img.cols;
|
|
||||||
int cy = data[1] * img.rows;
|
|
||||||
int width = data[2] * img.cols;
|
|
||||||
int height = data[3] * img.rows;
|
|
||||||
int left = cx - width / 2;
|
|
||||||
int top = cy - height / 2;
|
|
||||||
found.push_back(cv::Rect(left, top, width, height));
|
|
||||||
}
|
|
||||||
data += 85;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
lock.unlock();
|
|
||||||
|
|
||||||
SDL_Delay(250);
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
static void captureFrameThread(SDL_Window* window, const char* fname) {
|
|
||||||
|
|
||||||
av_register_all();
|
|
||||||
avcodec_register_all();
|
|
||||||
AVFormatContext* pFormatCtx = avformat_alloc_context();
|
|
||||||
|
|
||||||
if (avformat_open_input(&pFormatCtx, fname, NULL, NULL) != 0) {
|
|
||||||
std::cerr << "Couldn't open stream\n";
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (avformat_find_stream_info(pFormatCtx, NULL) < 0) {
|
|
||||||
std::cerr << "Couldn't find stream information\n";
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
int videoStream = -1;
|
|
||||||
for (unsigned int i = 0; i < pFormatCtx->nb_streams; i++) {
|
|
||||||
if (pFormatCtx->streams[i]->codecpar->codec_type == AVMEDIA_TYPE_VIDEO) {
|
|
||||||
videoStream = i;
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if (videoStream == -1) {
|
|
||||||
std::cerr << "Didn't find a video stream\n";
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
AVCodecParameters* pCodecParameters = pFormatCtx->streams[videoStream]->codecpar;
|
|
||||||
AVCodec* pCodec = avcodec_find_decoder(pCodecParameters->codec_id);
|
|
||||||
|
|
||||||
if (pCodec == NULL) {
|
|
||||||
std::cerr << "Unsupported codec\n";
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
AVCodecContext* pCodecCtx = avcodec_alloc_context3(pCodec);
|
|
||||||
pCodecCtx->pix_fmt = AV_PIX_FMT_GRAY8;
|
|
||||||
avcodec_parameters_to_context(pCodecCtx, pCodecParameters);
|
|
||||||
|
|
||||||
if (avcodec_open2(pCodecCtx, pCodec, NULL) < 0) {
|
|
||||||
std::cerr << "Could not open codec\n";
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
AVFrame* pFrame = av_frame_alloc();
|
|
||||||
AVFrame* pFrameRGB = av_frame_alloc();
|
|
||||||
|
|
||||||
int numBytes = av_image_get_buffer_size(AV_PIX_FMT_RGB24, pCodecCtx->width, pCodecCtx->height, 1);
|
|
||||||
uint8_t* buffer = (uint8_t*)av_malloc(numBytes * sizeof(uint8_t));
|
|
||||||
av_image_fill_arrays(pFrameRGB->data, pFrameRGB->linesize, buffer, AV_PIX_FMT_BGR24, pCodecCtx->width, pCodecCtx->height, 1);
|
|
||||||
|
|
||||||
SwsContext* sws_ctx = sws_getContext(
|
|
||||||
pCodecCtx->width,
|
|
||||||
pCodecCtx->height,
|
|
||||||
pCodecCtx->pix_fmt,
|
|
||||||
pCodecCtx->width,
|
|
||||||
pCodecCtx->height,
|
|
||||||
AV_PIX_FMT_BGR24,
|
|
||||||
SWS_BILINEAR,
|
|
||||||
NULL,
|
|
||||||
NULL,
|
|
||||||
NULL);
|
|
||||||
|
|
||||||
//AVRational time_base = pFormatCtx->streams[videoStream]->time_base;
|
|
||||||
//AVRational frame_rate = av_guess_frame_rate(pFormatCtx, pFormatCtx->streams[videoStream], NULL);
|
|
||||||
//uint32_t delay = (av_rescale_q(1, av_inv_q(frame_rate), time_base) / AV_TIME_BASE) * 1000;
|
|
||||||
//printf("delay: %u\n", delay);
|
|
||||||
|
|
||||||
SDL_SetWindowSize(window, pCodecCtx->width, pCodecCtx->height);
|
|
||||||
SDL_Renderer* renderer = SDL_CreateRenderer(window, -1, 0);
|
|
||||||
SDL_Texture* texture = SDL_CreateTexture(renderer, SDL_PIXELFORMAT_BGR24, SDL_TEXTUREACCESS_STATIC, pCodecCtx->width, pCodecCtx->height);
|
|
||||||
|
|
||||||
while (!stop) {
|
|
||||||
AVPacket packet;
|
|
||||||
if(av_read_frame(pFormatCtx, &packet) < 0) {
|
|
||||||
stop = true;
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (packet.stream_index == videoStream) {
|
|
||||||
int response = avcodec_send_packet(pCodecCtx, &packet);
|
|
||||||
if (response < 0) {
|
|
||||||
std::cerr << "Error while sending a packet to the decoder: " << response << '\n';
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
while (response >= 0) {
|
|
||||||
response = avcodec_receive_frame(pCodecCtx, pFrame);
|
|
||||||
if (response == AVERROR(EAGAIN) || response == AVERROR_EOF) {
|
|
||||||
break;
|
|
||||||
} else if (response < 0) {
|
|
||||||
std::cerr << "Error while receiving a frame from the decoder: " << response << '\n';
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (response >= 0) {
|
|
||||||
sws_scale(sws_ctx, (uint8_t const* const*)pFrame->data, pFrame->linesize, 0, pCodecCtx->height, pFrameRGB->data, pFrameRGB->linesize);
|
|
||||||
|
|
||||||
std::unique_lock<std::mutex> lock(mtx);
|
|
||||||
|
|
||||||
img = cv::Mat(pCodecCtx->height, pCodecCtx->width, CV_8UC3, pFrameRGB->data[0], pFrameRGB->linesize[0]);
|
|
||||||
|
|
||||||
for (unsigned i = 0; i < found.size(); i++) {
|
|
||||||
rectangle(img, found[i], cv::Scalar(255 - weights[i] * 255, 0, weights[i] * 255), 3);
|
|
||||||
}
|
|
||||||
|
|
||||||
lock.unlock();
|
|
||||||
|
|
||||||
{
|
|
||||||
SDL_UpdateTexture(texture, NULL, img.data, img.cols * 3);
|
|
||||||
SDL_RenderClear(renderer);
|
|
||||||
SDL_RenderCopy(renderer, texture, NULL, NULL);
|
|
||||||
SDL_RenderPresent(renderer);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
av_packet_unref(&packet);
|
|
||||||
|
|
||||||
SDL_Event event;
|
|
||||||
while(SDL_PollEvent(&event)) {
|
|
||||||
switch(event.type) {
|
|
||||||
case SDL_QUIT:
|
|
||||||
stop = true;
|
|
||||||
default: break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
SDL_Delay(33);
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
av_free(buffer);
|
|
||||||
av_frame_free(&pFrameRGB);
|
|
||||||
av_frame_free(&pFrame);
|
|
||||||
avcodec_close(pCodecCtx);
|
|
||||||
avformat_close_input(&pFormatCtx);
|
|
||||||
|
|
||||||
SDL_DestroyTexture(texture);
|
|
||||||
SDL_DestroyRenderer(renderer);
|
|
||||||
}
|
|
||||||
|
|
||||||
int main(int argc, char* argv[]) {
|
|
||||||
if(SDL_Init(SDL_INIT_VIDEO) < 0) {
|
|
||||||
fprintf(stderr, "%s", SDL_GetError());
|
|
||||||
return 1;
|
|
||||||
}
|
|
||||||
|
|
||||||
SDL_Window* win = SDL_CreateWindow(
|
|
||||||
"Robomaster",
|
|
||||||
SDL_WINDOWPOS_UNDEFINED,
|
|
||||||
SDL_WINDOWPOS_UNDEFINED,
|
|
||||||
800, 300,
|
|
||||||
SDL_WINDOW_RESIZABLE );
|
|
||||||
if(!win) {
|
|
||||||
fprintf(stderr, "%s", SDL_GetError());
|
|
||||||
return 1;
|
|
||||||
}
|
|
||||||
|
|
||||||
std::thread captureThread(captureFrameThread, win, argv[1]);
|
|
||||||
std::thread processThread(processFrameThread);
|
|
||||||
|
|
||||||
captureThread.join();
|
|
||||||
processThread.join();
|
|
||||||
|
|
||||||
SDL_Quit();
|
|
||||||
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
Reference in New Issue
Block a user