Compare commits
16
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
507ec8ba59 | ||
|
|
f2918b441d | ||
|
|
150c450210 | ||
|
|
19993ced8e | ||
|
|
2c42352c85 | ||
|
|
0b238ce048 | ||
|
|
2258812fc2 | ||
|
|
31d7e57125 | ||
|
|
af5a6efdb3 | ||
|
|
6b4c891b36 | ||
|
|
c4c5e394af | ||
|
|
77000b525f | ||
|
|
6e10db0e07 | ||
|
|
ca82982a76 | ||
|
|
138f758898 | ||
|
|
53b127232c |
@@ -0,0 +1,59 @@
|
|||||||
|
name: Bug Report
|
||||||
|
about: File a bug report
|
||||||
|
title: "[Bug]: "
|
||||||
|
body:
|
||||||
|
- type: markdown
|
||||||
|
attributes:
|
||||||
|
value: |
|
||||||
|
Thanks for taking the time to fill out this bug report!
|
||||||
|
- type: input
|
||||||
|
id: contact
|
||||||
|
attributes:
|
||||||
|
label: Contact Details
|
||||||
|
description: How can we get in touch with you if we need more info?
|
||||||
|
placeholder: ex. email@example.com
|
||||||
|
validations:
|
||||||
|
required: false
|
||||||
|
- type: textarea
|
||||||
|
id: what-happened
|
||||||
|
attributes:
|
||||||
|
label: What happened?
|
||||||
|
description: Also tell us, what did you expect to happen?
|
||||||
|
placeholder: Tell us what you see!
|
||||||
|
value: "A bug happened!"
|
||||||
|
validations:
|
||||||
|
required: true
|
||||||
|
- type: dropdown
|
||||||
|
id: version
|
||||||
|
attributes:
|
||||||
|
label: Version
|
||||||
|
description: What version of our software are you running?
|
||||||
|
options:
|
||||||
|
- 1.0.2 (Default)
|
||||||
|
- 1.0.3 (Edge)
|
||||||
|
validations:
|
||||||
|
required: true
|
||||||
|
- type: dropdown
|
||||||
|
id: browsers
|
||||||
|
attributes:
|
||||||
|
label: What browsers are you seeing the problem on?
|
||||||
|
multiple: true
|
||||||
|
options:
|
||||||
|
- Firefox
|
||||||
|
- Chrome
|
||||||
|
- Safari
|
||||||
|
- Microsoft Edge
|
||||||
|
- type: textarea
|
||||||
|
id: logs
|
||||||
|
attributes:
|
||||||
|
label: Relevant log output
|
||||||
|
description: Please copy and paste any relevant log output. This will be automatically formatted into code, so no need for backticks.
|
||||||
|
render: shell
|
||||||
|
- type: checkboxes
|
||||||
|
id: terms
|
||||||
|
attributes:
|
||||||
|
label: Code of Conduct
|
||||||
|
description: By submitting this issue, you agree to follow our [Code of Conduct](https://example.com)
|
||||||
|
options:
|
||||||
|
- label: I agree to follow this project's Code of Conduct
|
||||||
|
required: true
|
||||||
+3
-9
@@ -2,7 +2,7 @@ cmake_minimum_required(VERSION 3.15)
|
|||||||
|
|
||||||
project (
|
project (
|
||||||
RoboMasterSDK
|
RoboMasterSDK
|
||||||
VERSION 1.0.0
|
VERSION 2.0.0
|
||||||
LANGUAGES C
|
LANGUAGES C
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -15,8 +15,10 @@ add_library(robomaster
|
|||||||
src/message.c
|
src/message.c
|
||||||
src/modules/sdk.c
|
src/modules/sdk.c
|
||||||
src/modules/chassis.c
|
src/modules/chassis.c
|
||||||
|
src/modules/gimbal.c
|
||||||
src/connection.c
|
src/connection.c
|
||||||
src/robomaster.c
|
src/robomaster.c
|
||||||
|
src/robo.c
|
||||||
)
|
)
|
||||||
|
|
||||||
target_include_directories(robomaster
|
target_include_directories(robomaster
|
||||||
@@ -26,14 +28,6 @@ target_include_directories(robomaster
|
|||||||
include
|
include
|
||||||
)
|
)
|
||||||
|
|
||||||
add_executable(robomastersh
|
|
||||||
src/robomastersh.c
|
|
||||||
)
|
|
||||||
|
|
||||||
target_link_libraries(robomastersh
|
|
||||||
robomaster
|
|
||||||
)
|
|
||||||
|
|
||||||
find_package(SDL2)
|
find_package(SDL2)
|
||||||
|
|
||||||
if(SDL2_FOUND)
|
if(SDL2_FOUND)
|
||||||
|
|||||||
+8
-4
@@ -8,21 +8,25 @@
|
|||||||
static const uint8_t CHASSIS_HOST = 3;
|
static const uint8_t CHASSIS_HOST = 3;
|
||||||
static const uint8_t CHASSIS_INDEX = 6;
|
static const uint8_t CHASSIS_INDEX = 6;
|
||||||
|
|
||||||
static const uint8_t SET_WHEEL_SPEED_CMDID = 0x20;
|
#define SET_WHEEL_SPEED_CMD 0x203F
|
||||||
|
|
||||||
void
|
void
|
||||||
set_wheel_speed (
|
set_wheel_speed (
|
||||||
Client session,
|
union Request* req,
|
||||||
|
uint16_t seq,
|
||||||
|
bool ack,
|
||||||
int16_t w1,
|
int16_t w1,
|
||||||
int16_t w2,
|
int16_t w2,
|
||||||
int16_t w3,
|
int16_t w3,
|
||||||
int16_t w4 );
|
int16_t w4 );
|
||||||
|
|
||||||
static const uint8_t CHASSIS_SPEED_MODE_CMDID = 0x21;
|
#define CHASSIS_SPEED_MODE_CMD 0x213F
|
||||||
|
|
||||||
void
|
void
|
||||||
chassis_speed_mode (
|
chassis_speed_mode (
|
||||||
Client session,
|
union Request* req,
|
||||||
|
uint16_t seq,
|
||||||
|
bool ack,
|
||||||
float x,
|
float x,
|
||||||
float y,
|
float y,
|
||||||
float z );
|
float z );
|
||||||
|
|||||||
@@ -0,0 +1,13 @@
|
|||||||
|
#pragma once
|
||||||
|
|
||||||
|
struct Connection;
|
||||||
|
typedef Connection* Connection;
|
||||||
|
|
||||||
|
struct Connection*
|
||||||
|
conn_new (
|
||||||
|
struct Connection* connection,
|
||||||
|
unsigned int source_port,
|
||||||
|
const char* source_ip,
|
||||||
|
unsigned int dest_port,
|
||||||
|
const char* dest_ip );
|
||||||
|
extern const size_t SIZEOF_CONN;
|
||||||
@@ -0,0 +1,20 @@
|
|||||||
|
#pragma once
|
||||||
|
|
||||||
|
#include "message.h"
|
||||||
|
|
||||||
|
#include <stdbool.h>
|
||||||
|
#include <stdint.h>
|
||||||
|
|
||||||
|
static const uint8_t GIMBAL_HOST = 4;
|
||||||
|
static const uint8_t GIMBAL_INDEX = 0;
|
||||||
|
|
||||||
|
#define GIMBAL_CTRL_SPEED_CMD 0x0C04
|
||||||
|
|
||||||
|
void
|
||||||
|
gimbal_ctrl_speed (
|
||||||
|
union Request* req,
|
||||||
|
uint16_t seq,
|
||||||
|
bool ack,
|
||||||
|
int16_t p,
|
||||||
|
int16_t y,
|
||||||
|
int16_t r );
|
||||||
+39
-9
@@ -5,19 +5,18 @@
|
|||||||
|
|
||||||
#define PACKED __attribute__((__packed__))
|
#define PACKED __attribute__((__packed__))
|
||||||
|
|
||||||
|
static const uint8_t CLIENT_HOST = 9;
|
||||||
|
static const uint8_t CLIENT_INDEX = 6;
|
||||||
|
|
||||||
|
static const uint8_t PREAMBLE = 0x55;
|
||||||
|
|
||||||
struct PACKED Header {
|
struct PACKED Header {
|
||||||
|
|
||||||
// The preamble marks the start of a message and is always 0x55
|
// The preamble marks the start of a message and is always 0x55
|
||||||
uint8_t preamble;
|
uint8_t preamble;
|
||||||
|
|
||||||
// The length of the message includes the preamble and CRC16 at the end
|
// The length of the message includes the preamble and CRC16 at the end
|
||||||
union {
|
uint16_t length;
|
||||||
uint16_t length;
|
|
||||||
struct {
|
|
||||||
uint8_t length_l;
|
|
||||||
uint8_t length_h;
|
|
||||||
};
|
|
||||||
};
|
|
||||||
|
|
||||||
// This is a CRC8 checksum for the preamble and length together
|
// This is a CRC8 checksum for the preamble and length together
|
||||||
uint8_t crc;
|
uint8_t crc;
|
||||||
@@ -46,8 +45,13 @@ struct PACKED Header {
|
|||||||
};
|
};
|
||||||
|
|
||||||
// Each command has a cmdset and cmdid that together make a cmd key
|
// Each command has a cmdset and cmdid that together make a cmd key
|
||||||
uint8_t cmdset;
|
union {
|
||||||
uint8_t cmdid;
|
uint16_t cmd;
|
||||||
|
struct {
|
||||||
|
uint8_t cmdset;
|
||||||
|
uint8_t cmdid;
|
||||||
|
};
|
||||||
|
};
|
||||||
|
|
||||||
};
|
};
|
||||||
|
|
||||||
@@ -164,6 +168,30 @@ struct PACKED SetChassisWheelSpeedResp
|
|||||||
struct Footer footer;
|
struct Footer footer;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
struct PACKED GimbalCtrlSpeedReq
|
||||||
|
{
|
||||||
|
struct Header header;
|
||||||
|
// Values between -360 and 360
|
||||||
|
union {
|
||||||
|
int16_t yrp[3];
|
||||||
|
struct {
|
||||||
|
int16_t yaw;
|
||||||
|
int16_t roll;
|
||||||
|
int16_t pitch;
|
||||||
|
};
|
||||||
|
};
|
||||||
|
// Always 0xDC
|
||||||
|
uint8_t ctrl;
|
||||||
|
struct Footer footer;
|
||||||
|
};
|
||||||
|
|
||||||
|
struct PACKED GimbalCtrlSpeedResp
|
||||||
|
{
|
||||||
|
struct Header header;
|
||||||
|
int8_t retcode;
|
||||||
|
struct Footer footer;
|
||||||
|
};
|
||||||
|
|
||||||
struct PACKED SetWheelSpeedReq
|
struct PACKED SetWheelSpeedReq
|
||||||
{
|
{
|
||||||
struct Header header;
|
struct Header header;
|
||||||
@@ -232,6 +260,7 @@ union Request {
|
|||||||
struct SetRobotModeReq mvmode;
|
struct SetRobotModeReq mvmode;
|
||||||
struct SubNodeResetReq subnodereset;
|
struct SubNodeResetReq subnodereset;
|
||||||
struct SubscribeAddNodeReq subnodeadd;
|
struct SubscribeAddNodeReq subnodeadd;
|
||||||
|
struct GimbalCtrlSpeedReq gimbspeed;
|
||||||
};
|
};
|
||||||
union Response {
|
union Response {
|
||||||
struct Header header;
|
struct Header header;
|
||||||
@@ -245,6 +274,7 @@ union Response {
|
|||||||
struct SetRobotModeResp mvmode;
|
struct SetRobotModeResp mvmode;
|
||||||
struct SubNodeResetResp subnodereset;
|
struct SubNodeResetResp subnodereset;
|
||||||
struct SubscribeAddNodeResp subnodeadd;
|
struct SubscribeAddNodeResp subnodeadd;
|
||||||
|
struct GimbalCtrlSpeedResp gimbspeed;
|
||||||
};
|
};
|
||||||
union Message {
|
union Message {
|
||||||
struct Header header;
|
struct Header header;
|
||||||
|
|||||||
@@ -0,0 +1,93 @@
|
|||||||
|
#pragma once
|
||||||
|
|
||||||
|
// Handle for the high level robot interface
|
||||||
|
struct RobotImp;
|
||||||
|
typedef struct RobotImp* Robot;
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Return a handle to the high level robot interface, or NULL on error.
|
||||||
|
*/
|
||||||
|
Robot robot_new();
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Queue initialization tasks before further commands can be sent to the robot.
|
||||||
|
* The robot is not guaranteed to be ready until a ready message is popped from
|
||||||
|
* the work function.
|
||||||
|
*
|
||||||
|
* robot: The robot to initialize
|
||||||
|
* returns: 0 on success, non-zero on failure
|
||||||
|
*/
|
||||||
|
int robot_init(Robot robot);
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Request the robot to stop. This does not stop the robot immediately. The
|
||||||
|
* robot has stopped once the work function returns that there is no more work
|
||||||
|
* to do.
|
||||||
|
*
|
||||||
|
* robot: The robot to stop
|
||||||
|
* returns: 0 on success, non-zero on failure
|
||||||
|
*/
|
||||||
|
int robot_stop(Robot robot);
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Set the gimble speed. A packet will be sent to the robot on
|
||||||
|
* the next ready tick of the work function.
|
||||||
|
*
|
||||||
|
* robot: The robot to set the velocity of
|
||||||
|
* p: pitch speed
|
||||||
|
* y: yaw speed
|
||||||
|
* returns: 0 on success, non-zero on failure
|
||||||
|
*/
|
||||||
|
int robot_aim(Robot, float p, float y);
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Set the velocity of the robot chassis. A packet will be sent to the robot on
|
||||||
|
* the next ready tick of the work function.
|
||||||
|
*
|
||||||
|
* robot: The robot to set the velocity of
|
||||||
|
* x: strafe velocity
|
||||||
|
* y: forward and backward velocity
|
||||||
|
* r: rotation velocity
|
||||||
|
* returns: 0 on success, non-zero on failure
|
||||||
|
*/
|
||||||
|
int robot_drive(Robot, float x, float y, float r);
|
||||||
|
|
||||||
|
/*
|
||||||
|
* TODO: This is too simplistic for what the lights are capable of.
|
||||||
|
* Set the color of all LEDs on the robot. A packet will be sent to the robot
|
||||||
|
* on the next ready tick of the work function.
|
||||||
|
*
|
||||||
|
* robot: The robot to set LED colors of
|
||||||
|
* r: red
|
||||||
|
* g: green
|
||||||
|
* b: blue
|
||||||
|
* returns: 0 on success, non-zero on failure
|
||||||
|
*/
|
||||||
|
int robot_led(Robot, unsigned char r, unsigned char g, unsigned char b);
|
||||||
|
|
||||||
|
/*
|
||||||
|
* If the robot is in ready mode, this makes the robot send a heartbeat in
|
||||||
|
* the next tick of the work function. The work function will not do anything
|
||||||
|
* else until a heartbeat response is received.
|
||||||
|
*
|
||||||
|
* A heartbeat must be sent periodically or else the robot will become
|
||||||
|
* unresponsive. This function is thread-safe and can be called in a
|
||||||
|
* busy-waiting loop, signal handler, or background thread.
|
||||||
|
*
|
||||||
|
* robot: The robot to send a heartbeat to
|
||||||
|
* returns: 0 on success, non-zero on failure
|
||||||
|
*/
|
||||||
|
int robot_heartbeat(Robot robot);
|
||||||
|
|
||||||
|
/*
|
||||||
|
* This does all of the work in small pieces so it returns as quickly as
|
||||||
|
* possible. Most of the other functions in this API simply set a flag
|
||||||
|
* to signal this function to work. A non-zero value is returned until
|
||||||
|
* no more work is left, which would only happen shortly after the stop
|
||||||
|
* function is called and any remaining tasks have been worked through.
|
||||||
|
*
|
||||||
|
* robot: The robot to do periodic work on
|
||||||
|
* returns: non-zero until stop function is called
|
||||||
|
*/
|
||||||
|
int robot_work(Robot robot);
|
||||||
|
|
||||||
+1
-15
@@ -7,12 +7,7 @@ typedef struct Client* Client;
|
|||||||
#include "message.h"
|
#include "message.h"
|
||||||
#include "chassis.h"
|
#include "chassis.h"
|
||||||
#include "sdk.h"
|
#include "sdk.h"
|
||||||
|
#include "gimbal.h"
|
||||||
// SIMPLE
|
|
||||||
// Create a client
|
|
||||||
// Connect to a drone
|
|
||||||
// Send messages
|
|
||||||
// Poll for responses
|
|
||||||
|
|
||||||
Client client_new();
|
Client client_new();
|
||||||
void client_connect(Client client);
|
void client_connect(Client client);
|
||||||
@@ -27,12 +22,3 @@ static inline void byte2host(uint8_t b, uint8_t* host, uint8_t* index) {
|
|||||||
*index = b >> 5;
|
*index = b >> 5;
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
|
||||||
* Drive the wheels of the robot. Values should be [-1, 1]
|
|
||||||
*
|
|
||||||
* x: positive is forward, negative is backwards
|
|
||||||
* y: positive is right, negative is left
|
|
||||||
* r: positive is clockwise, negative is counterclockwise
|
|
||||||
*/
|
|
||||||
void drive(Client client, float x, float y, float r);
|
|
||||||
|
|
||||||
|
|||||||
+30
-16
@@ -8,7 +8,7 @@
|
|||||||
static const uint8_t SDK_HOST = 9;
|
static const uint8_t SDK_HOST = 9;
|
||||||
static const uint8_t SDK_INDEX = 0;
|
static const uint8_t SDK_INDEX = 0;
|
||||||
|
|
||||||
static const uint8_t SET_SDK_CONNECTION_CMDID = 0xD4;
|
#define SET_SDK_CONNECTION_CMD 0xD43F
|
||||||
|
|
||||||
enum CONNECTION {
|
enum CONNECTION {
|
||||||
CONNECTION_WIFI_AP = 0,
|
CONNECTION_WIFI_AP = 0,
|
||||||
@@ -18,23 +18,31 @@ enum CONNECTION {
|
|||||||
|
|
||||||
void
|
void
|
||||||
set_sdk_connection(
|
set_sdk_connection(
|
||||||
Client session,
|
union Request* req,
|
||||||
|
uint16_t seq,
|
||||||
|
bool ack,
|
||||||
enum CONNECTION connection_type,
|
enum CONNECTION connection_type,
|
||||||
uint32_t ip_address,
|
uint32_t ip_address,
|
||||||
uint16_t port );
|
uint16_t port );
|
||||||
|
|
||||||
static const uint8_t SDK_HEARTBEAT_CMDID = 0xD5;
|
#define SDK_HEARTBEAT_CMD 0xD53F
|
||||||
|
|
||||||
void
|
void
|
||||||
sdk_heartbeat(
|
sdk_heartbeat(
|
||||||
Client session );
|
union Request* req,
|
||||||
|
uint16_t seq,
|
||||||
|
bool ack );
|
||||||
|
|
||||||
static const uint8_t SET_SDK_MODE_CMDID = 0xd1;
|
#define SET_SDK_MODE_CMD 0xD13F
|
||||||
|
|
||||||
void
|
void
|
||||||
set_sdk_mode(Client session, bool enable);
|
set_sdk_mode(
|
||||||
|
union Request* req,
|
||||||
|
uint16_t seq,
|
||||||
|
bool ack,
|
||||||
|
bool enable );
|
||||||
|
|
||||||
static const uint8_t SET_SYSTEM_LED_CMDID = 0x33;
|
#define SET_SYSTEM_LED_CMD 0x333F
|
||||||
|
|
||||||
enum LEDCOMP {
|
enum LEDCOMP {
|
||||||
LEDCOMP_BOTTOM_BACK = 0x1,
|
LEDCOMP_BOTTOM_BACK = 0x1,
|
||||||
@@ -57,7 +65,9 @@ enum LEDEFFECT {
|
|||||||
};
|
};
|
||||||
|
|
||||||
void set_system_led (
|
void set_system_led (
|
||||||
Client session,
|
union Request* req,
|
||||||
|
uint16_t seq,
|
||||||
|
bool ack,
|
||||||
uint8_t red,
|
uint8_t red,
|
||||||
uint8_t green,
|
uint8_t green,
|
||||||
uint8_t blue,
|
uint8_t blue,
|
||||||
@@ -67,7 +77,7 @@ void set_system_led (
|
|||||||
uint16_t t1,
|
uint16_t t1,
|
||||||
uint16_t t2 );
|
uint16_t t2 );
|
||||||
|
|
||||||
static const uint8_t SET_ROBOT_MODE_CMDID = 0x46;
|
#define SET_ROBOT_MODE_CMD 0x463F
|
||||||
|
|
||||||
enum MOVEMENTMODE {
|
enum MOVEMENTMODE {
|
||||||
MOVEMENTMODE_FREE,
|
MOVEMENTMODE_FREE,
|
||||||
@@ -77,20 +87,24 @@ enum MOVEMENTMODE {
|
|||||||
|
|
||||||
void
|
void
|
||||||
set_robot_mode (
|
set_robot_mode (
|
||||||
Client session,
|
union Request* req,
|
||||||
|
uint16_t seq,
|
||||||
|
bool ack,
|
||||||
enum MOVEMENTMODE mode );
|
enum MOVEMENTMODE mode );
|
||||||
|
|
||||||
// cmdset 0x48
|
#define SUBNODE_RESET_CMD 0x0248
|
||||||
static const uint8_t SUBNODE_RESET_CMDID = 0x02;
|
|
||||||
|
|
||||||
void
|
void
|
||||||
subnode_reset (
|
subnode_reset (
|
||||||
Client session );
|
union Request* req,
|
||||||
|
uint16_t seq,
|
||||||
|
bool ack );
|
||||||
|
|
||||||
// cmdset 0x48
|
#define SUBSCRIBE_ADD_NODE_CMD 0x0148
|
||||||
static const uint8_t SUBSCRIBE_ADD_NODE_CMDID = 0x01;
|
|
||||||
|
|
||||||
void
|
void
|
||||||
subscribe_add_node (
|
subscribe_add_node (
|
||||||
Client session );
|
union Request* req,
|
||||||
|
uint16_t seq,
|
||||||
|
bool ack );
|
||||||
|
|
||||||
|
|||||||
+1
-6
@@ -4,9 +4,7 @@
|
|||||||
|
|
||||||
struct Client {
|
struct Client {
|
||||||
|
|
||||||
uint8_t hostbyte;
|
uint16_t seq;
|
||||||
|
|
||||||
int16_t seq;
|
|
||||||
|
|
||||||
union {
|
union {
|
||||||
struct {
|
struct {
|
||||||
@@ -18,6 +16,3 @@ struct Client {
|
|||||||
|
|
||||||
};
|
};
|
||||||
|
|
||||||
static const uint8_t CLIENT_HOST = 9;
|
|
||||||
static const uint8_t CLIENT_INDEX = 6;
|
|
||||||
|
|
||||||
|
|||||||
+65
-14
@@ -1,4 +1,4 @@
|
|||||||
#include "message.h"
|
#include "robomaster.h"
|
||||||
#include "client.h"
|
#include "client.h"
|
||||||
#include "crc.h"
|
#include "crc.h"
|
||||||
#include "connection.h"
|
#include "connection.h"
|
||||||
@@ -12,6 +12,59 @@
|
|||||||
#include <fcntl.h>
|
#include <fcntl.h>
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
|
|
||||||
|
static
|
||||||
|
inline
|
||||||
|
size_t
|
||||||
|
message_length(int cmd) {
|
||||||
|
switch(cmd) {
|
||||||
|
case SET_SDK_CONNECTION_CMD:
|
||||||
|
return sizeof(struct SetSdkConnectionReq);
|
||||||
|
case SDK_HEARTBEAT_CMD:
|
||||||
|
return sizeof(struct SdkHeartbeatReq);
|
||||||
|
case SET_SDK_MODE_CMD:
|
||||||
|
return sizeof(struct SetSdkModeReq);
|
||||||
|
case SET_SYSTEM_LED_CMD:
|
||||||
|
return sizeof(struct SetSystemLedReq);
|
||||||
|
case SET_ROBOT_MODE_CMD:
|
||||||
|
return sizeof(struct SetRobotModeReq);
|
||||||
|
case SUBNODE_RESET_CMD:
|
||||||
|
return sizeof(struct SubNodeResetReq);
|
||||||
|
case SUBSCRIBE_ADD_NODE_CMD:
|
||||||
|
return sizeof(struct SubscribeAddNodeReq);
|
||||||
|
case SET_WHEEL_SPEED_CMD:
|
||||||
|
return sizeof(struct SetWheelSpeedReq);
|
||||||
|
case CHASSIS_SPEED_MODE_CMD:
|
||||||
|
return sizeof(struct ChassisSpeedModeReq);
|
||||||
|
case GIMBAL_CTRL_SPEED_CMD:
|
||||||
|
return sizeof(struct GimbalCtrlSpeedReq);
|
||||||
|
default:
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
static
|
||||||
|
inline
|
||||||
|
uint8_t
|
||||||
|
message_module(int cmd) {
|
||||||
|
switch(cmd) {
|
||||||
|
case SET_SDK_CONNECTION_CMD:
|
||||||
|
case SDK_HEARTBEAT_CMD:
|
||||||
|
case SET_SDK_MODE_CMD:
|
||||||
|
case SET_SYSTEM_LED_CMD:
|
||||||
|
case SET_ROBOT_MODE_CMD:
|
||||||
|
case SUBNODE_RESET_CMD:
|
||||||
|
case SUBSCRIBE_ADD_NODE_CMD:
|
||||||
|
return host2byte(SDK_HOST, SDK_INDEX);
|
||||||
|
case SET_WHEEL_SPEED_CMD:
|
||||||
|
case CHASSIS_SPEED_MODE_CMD:
|
||||||
|
return host2byte(CHASSIS_HOST, CHASSIS_INDEX);
|
||||||
|
case GIMBAL_CTRL_SPEED_CMD:
|
||||||
|
return host2byte(GIMBAL_HOST, GIMBAL_INDEX);
|
||||||
|
default:
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// The greated file descriptor is needed for polling the sockets.
|
// The greated file descriptor is needed for polling the sockets.
|
||||||
// It needs to be global for the whole process.
|
// It needs to be global for the whole process.
|
||||||
int max_fd = -1;
|
int max_fd = -1;
|
||||||
@@ -72,8 +125,8 @@ connection_poll_ready(struct Client* client) {
|
|||||||
if(client->conns[i])
|
if(client->conns[i])
|
||||||
FD_SET(client->conns[i]->sockfd, &read_fds);
|
FD_SET(client->conns[i]->sockfd, &read_fds);
|
||||||
|
|
||||||
//struct timeval timeout = {0, 0};
|
struct timeval timeout = {0, 0};
|
||||||
int result = select(max_fd + 1, &read_fds, NULL, NULL, NULL);
|
int result = select(max_fd + 1, &read_fds, NULL, NULL, &timeout);
|
||||||
|
|
||||||
// Check for socket polling errors
|
// Check for socket polling errors
|
||||||
if(result < 0) {
|
if(result < 0) {
|
||||||
@@ -118,26 +171,24 @@ connection_read(struct Connection* conn, union Message* resp) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
void
|
void
|
||||||
req_send(struct Connection* conn, union Request* req, size_t length) {
|
req_send(struct Connection* conn, union Request* req) {
|
||||||
if(!conn || !req) return;
|
if(!conn || !req) return;
|
||||||
|
size_t length = message_length(req->header.cmd);
|
||||||
sendto(conn->sockfd, req, length, 0, (struct sockaddr*)&conn->remote_addr, conn->addrlen);
|
sendto(conn->sockfd, req, length, 0, (struct sockaddr*)&conn->remote_addr, conn->addrlen);
|
||||||
}
|
}
|
||||||
|
|
||||||
void
|
void
|
||||||
req_finalize(struct Client* client, uint8_t cmdset, uint8_t cmdid, uint8_t hostbyte, bool need_ack, size_t length, union Request* req) {
|
req_finalize(uint16_t seq, uint16_t cmd, bool need_ack, union Request* req) {
|
||||||
|
|
||||||
|
size_t length = message_length(cmd);
|
||||||
req->header.preamble = 0x55;
|
req->header.preamble = 0x55;
|
||||||
req->header.length_l = length & 0xFF;
|
req->header.length = (length & 0x1FFF) | 0x400;
|
||||||
req->header.length_h = ((length >> 8) & 0x3) | 4;
|
|
||||||
req->header.crc = crc8(req, 3);
|
req->header.crc = crc8(req, 3);
|
||||||
req->header.seq_id = client->seq++;
|
req->header.seq_id = seq;
|
||||||
req->header.sender = client->hostbyte;
|
req->header.sender = host2byte(CLIENT_HOST, CLIENT_INDEX);
|
||||||
// TODO: Figure out what this is supposed to be
|
req->header.receiver = message_module(cmd);
|
||||||
//req->header.receiver = host2byte(DEFAULT_CLIENT_HOST, DEFAULT_ROBOT_INDEX);
|
|
||||||
req->header.receiver = hostbyte;
|
|
||||||
req->header.ack_needed = need_ack;
|
req->header.ack_needed = need_ack;
|
||||||
req->header.cmdset = cmdset;
|
req->header.cmd = cmd;
|
||||||
req->header.cmdid = cmdid;
|
|
||||||
|
|
||||||
struct Footer* footer = (void*)req + length - sizeof(struct Footer);
|
struct Footer* footer = (void*)req + length - sizeof(struct Footer);
|
||||||
uint16_t crc = crc16(req, length - sizeof(struct Footer));
|
uint16_t crc = crc16(req, length - sizeof(struct Footer));
|
||||||
|
|||||||
+2
-2
@@ -22,8 +22,8 @@ void
|
|||||||
connection_read(struct Connection* connection, union Message* resp);
|
connection_read(struct Connection* connection, union Message* resp);
|
||||||
|
|
||||||
void
|
void
|
||||||
req_finalize(struct Client* client, uint8_t cmdset, uint8_t cmdid, uint8_t hostbyte, bool need_ack, size_t length, union Request* req);
|
req_finalize(uint16_t seq, uint16_t cmd, bool need_ack, union Request* req);
|
||||||
|
|
||||||
void
|
void
|
||||||
req_send(struct Connection* conn, union Request* req, size_t length);
|
req_send(struct Connection* conn, union Request* req);
|
||||||
|
|
||||||
|
|||||||
+1
-1
@@ -4,7 +4,7 @@
|
|||||||
enum MESSAGEERR
|
enum MESSAGEERR
|
||||||
message_validate(const union Message* message) {
|
message_validate(const union Message* message) {
|
||||||
|
|
||||||
uint16_t length = (message->header.length_h & 0x3) * 0xFF + message->header.length_l;
|
uint16_t length = message->header.length & 0x1FFF & ~0x400;
|
||||||
|
|
||||||
if(message->header.crc != crc8(message, 3))
|
if(message->header.crc != crc8(message, 3))
|
||||||
return MESSAGEERR_HEADERCRC;
|
return MESSAGEERR_HEADERCRC;
|
||||||
|
|||||||
+15
-15
@@ -4,30 +4,30 @@
|
|||||||
|
|
||||||
void
|
void
|
||||||
set_wheel_speed (
|
set_wheel_speed (
|
||||||
Client session,
|
union Request* req,
|
||||||
|
uint16_t seq,
|
||||||
|
bool ack,
|
||||||
int16_t w1,
|
int16_t w1,
|
||||||
int16_t w2,
|
int16_t w2,
|
||||||
int16_t w3,
|
int16_t w3,
|
||||||
int16_t w4 ) {
|
int16_t w4 ) {
|
||||||
union Request req = {0};
|
req->wheel.wheel_speed[0] = w1;
|
||||||
req.wheel.wheel_speed[0] = w1;
|
req->wheel.wheel_speed[1] = w2;
|
||||||
req.wheel.wheel_speed[1] = w2;
|
req->wheel.wheel_speed[2] = w3;
|
||||||
req.wheel.wheel_speed[2] = w3;
|
req->wheel.wheel_speed[3] = w4;
|
||||||
req.wheel.wheel_speed[3] = w4;
|
req_finalize(seq, SET_WHEEL_SPEED_CMD, ack, req);
|
||||||
req_finalize(session, 0x3F, SET_WHEEL_SPEED_CMDID, host2byte(CHASSIS_HOST, CHASSIS_INDEX), false, sizeof(struct SetWheelSpeedReq), &req);
|
|
||||||
req_send(session->dev_conn, &req, sizeof(struct SetWheelSpeedReq));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void
|
void
|
||||||
chassis_speed_mode (
|
chassis_speed_mode (
|
||||||
Client session,
|
union Request* req,
|
||||||
|
uint16_t seq,
|
||||||
|
bool ack,
|
||||||
float x,
|
float x,
|
||||||
float y,
|
float y,
|
||||||
float z ) {
|
float z ) {
|
||||||
union Request req = {0};
|
req->chsspeed.speed[0] = x;
|
||||||
req.chsspeed.speed[0] = x;
|
req->chsspeed.speed[1] = y;
|
||||||
req.chsspeed.speed[1] = y;
|
req->chsspeed.speed[2] = z;
|
||||||
req.chsspeed.speed[2] = z;
|
req_finalize(seq, CHASSIS_SPEED_MODE_CMD, ack, req);
|
||||||
req_finalize(session, 0x3F, CHASSIS_SPEED_MODE_CMDID, host2byte(CHASSIS_HOST, CHASSIS_INDEX), false, sizeof(struct ChassisSpeedModeReq), &req);
|
|
||||||
req_send(session->dev_conn, &req, sizeof(struct ChassisSpeedModeReq));
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -0,0 +1,18 @@
|
|||||||
|
#include "message.h"
|
||||||
|
#include "connection.h"
|
||||||
|
#include "robomaster.h"
|
||||||
|
|
||||||
|
void
|
||||||
|
gimbal_ctrl_speed (
|
||||||
|
union Request* req,
|
||||||
|
uint16_t seq,
|
||||||
|
bool ack,
|
||||||
|
int16_t p,
|
||||||
|
int16_t y,
|
||||||
|
int16_t r ) {
|
||||||
|
req->gimbspeed.yaw = y;
|
||||||
|
req->gimbspeed.roll = r;
|
||||||
|
req->gimbspeed.pitch = p;
|
||||||
|
req->gimbspeed.ctrl = 0xDC;
|
||||||
|
req_finalize(seq, GIMBAL_CTRL_SPEED_CMD, ack, req);
|
||||||
|
}
|
||||||
+49
-49
@@ -4,72 +4,74 @@
|
|||||||
|
|
||||||
void
|
void
|
||||||
set_sdk_connection(
|
set_sdk_connection(
|
||||||
Client session,
|
union Request* req,
|
||||||
|
uint16_t seq,
|
||||||
|
bool ack,
|
||||||
enum CONNECTION connection_type,
|
enum CONNECTION connection_type,
|
||||||
uint32_t ip_address,
|
uint32_t ip_address,
|
||||||
uint16_t port ) {
|
uint16_t port ) {
|
||||||
|
|
||||||
union Request req = {0};
|
req->sdkconn.control = 0;
|
||||||
req.sdkconn.control = 0;
|
req->sdkconn.host = host2byte(CLIENT_HOST, CLIENT_INDEX);
|
||||||
req.sdkconn.host = session->hostbyte;
|
req->sdkconn.connection = connection_type;
|
||||||
req.sdkconn.connection = connection_type;
|
req->sdkconn.protocol = 0;
|
||||||
req.sdkconn.protocol = 0;
|
req->sdkconn.ip_address = ip_address;
|
||||||
req.sdkconn.ip_address = ip_address;
|
req->sdkconn.port = port;
|
||||||
req.sdkconn.port = port;
|
req_finalize(seq, SET_SDK_CONNECTION_CMD, ack, req);
|
||||||
req_finalize(session, 0x3F, SET_SDK_CONNECTION_CMDID, host2byte(SDK_HOST, SDK_INDEX), true, sizeof(struct SetSdkConnectionReq), &req);
|
|
||||||
req_send(session->sdk_conn, &req, sizeof(struct SetSdkConnectionReq));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void
|
void
|
||||||
set_sdk_mode(
|
set_sdk_mode(
|
||||||
Client session,
|
union Request* req,
|
||||||
|
uint16_t seq,
|
||||||
|
bool ack,
|
||||||
bool enable ) {
|
bool enable ) {
|
||||||
union Request req = {0};
|
req->sdkmode.enable = enable;
|
||||||
req.sdkmode.enable = enable;
|
req_finalize(seq, SET_SDK_MODE_CMD, ack, req);
|
||||||
req_finalize(session, 0x3F, SET_SDK_MODE_CMDID, host2byte(SDK_HOST, SDK_INDEX), true, sizeof(struct SetSdkModeReq), &req);
|
|
||||||
req_send(session->dev_conn, &req, sizeof(struct SetSdkModeReq));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void
|
void
|
||||||
sdk_heartbeat(
|
sdk_heartbeat(
|
||||||
Client session ) {
|
union Request* req,
|
||||||
union Request req = {0};
|
uint16_t seq,
|
||||||
req_finalize(session, 0x3F, SDK_HEARTBEAT_CMDID, host2byte(SDK_HOST, SDK_INDEX), false, sizeof(struct SdkHeartbeatReq), &req);
|
bool ack ) {
|
||||||
req_send(session->dev_conn, &req, sizeof(struct SdkHeartbeatReq));
|
req_finalize(seq, SDK_HEARTBEAT_CMD, ack, req);
|
||||||
}
|
}
|
||||||
|
|
||||||
void
|
void
|
||||||
set_robot_mode (
|
set_robot_mode (
|
||||||
Client session,
|
union Request* req,
|
||||||
|
uint16_t seq,
|
||||||
|
bool ack,
|
||||||
enum MOVEMENTMODE mode ) {
|
enum MOVEMENTMODE mode ) {
|
||||||
union Request req = {0};
|
req->mvmode.mode = mode;
|
||||||
req.mvmode.mode = mode;
|
req_finalize(seq, SET_ROBOT_MODE_CMD, ack, req);
|
||||||
req_finalize(session, 0x3F, SET_ROBOT_MODE_CMDID, host2byte(SDK_HOST, SDK_INDEX), true, sizeof(struct SetRobotModeReq), &req);
|
|
||||||
req_send(session->dev_conn, &req, sizeof(struct SetRobotModeReq));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void
|
void
|
||||||
subnode_reset (
|
subnode_reset (
|
||||||
Client session ) {
|
union Request* req,
|
||||||
union Request req = {0};
|
uint16_t seq,
|
||||||
req.subnodereset.hostbyte = session->hostbyte;
|
bool ack ) {
|
||||||
req_finalize(session, 0x48, SUBNODE_RESET_CMDID, host2byte(SDK_HOST, SDK_INDEX), true, sizeof(struct SubNodeResetReq), &req);
|
req->subnodereset.hostbyte = host2byte(CLIENT_HOST, CLIENT_INDEX);
|
||||||
req_send(session->dev_conn, &req, sizeof(struct SubNodeResetReq));
|
req_finalize(seq, SUBNODE_RESET_CMD, ack, req);
|
||||||
}
|
}
|
||||||
|
|
||||||
void
|
void
|
||||||
subscribe_add_node (
|
subscribe_add_node (
|
||||||
Client session ) {
|
union Request* req,
|
||||||
union Request req = {0};
|
uint16_t seq,
|
||||||
req.subnodeadd.hostbyte = session->hostbyte;
|
bool ack ) {
|
||||||
req.subnodeadd.sub_vision = 0x03000000;
|
req->subnodeadd.hostbyte = host2byte(CLIENT_HOST, CLIENT_INDEX);
|
||||||
req_finalize(session, 0x48, SUBSCRIBE_ADD_NODE_CMDID, host2byte(SDK_HOST, SDK_INDEX), true, sizeof(struct SubscribeAddNodeReq), &req);
|
req->subnodeadd.sub_vision = 0x03000000;
|
||||||
req_send(session->dev_conn, &req, sizeof(struct SubscribeAddNodeReq));
|
req_finalize(seq, SUBSCRIBE_ADD_NODE_CMD, ack, req);
|
||||||
}
|
}
|
||||||
|
|
||||||
void
|
void
|
||||||
set_system_led (
|
set_system_led (
|
||||||
Client session,
|
union Request* req,
|
||||||
|
uint16_t seq,
|
||||||
|
bool ack,
|
||||||
uint8_t red,
|
uint8_t red,
|
||||||
uint8_t green,
|
uint8_t green,
|
||||||
uint8_t blue,
|
uint8_t blue,
|
||||||
@@ -79,20 +81,18 @@ set_system_led (
|
|||||||
uint16_t t1,
|
uint16_t t1,
|
||||||
uint16_t t2 ) {
|
uint16_t t2 ) {
|
||||||
|
|
||||||
union Request req = {0};
|
|
||||||
|
|
||||||
req.led.comp_mask = comp;
|
req->led.comp_mask = comp;
|
||||||
req.led.led_mask = led_mask;
|
req->led.led_mask = led_mask;
|
||||||
req.led.effect_mode = effect;
|
req->led.effect_mode = effect;
|
||||||
req.led.control_mode = 7;
|
req->led.control_mode = 7;
|
||||||
req.led.red = red;
|
req->led.red = red;
|
||||||
req.led.green = green;
|
req->led.green = green;
|
||||||
req.led.blue = blue;
|
req->led.blue = blue;
|
||||||
req.led.loop = 0;
|
req->led.loop = 0;
|
||||||
req.led.t1 = t1;
|
req->led.t1 = t1;
|
||||||
req.led.t2 = t2;
|
req->led.t2 = t2;
|
||||||
|
|
||||||
req_finalize(session, 0x3F, SET_SYSTEM_LED_CMDID, host2byte(SDK_HOST, SDK_INDEX), true, sizeof(struct SetSystemLedReq), &req);
|
req_finalize(seq, SET_SYSTEM_LED_CMD, ack, req);
|
||||||
req_send(session->dev_conn, &req, sizeof(struct SetSystemLedReq));
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
+268
@@ -0,0 +1,268 @@
|
|||||||
|
#include "roboeasy.h"
|
||||||
|
#include "robomaster.h"
|
||||||
|
#include "connection.h"
|
||||||
|
|
||||||
|
#include <string.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
|
||||||
|
struct RobotImp {
|
||||||
|
|
||||||
|
struct Client* client;
|
||||||
|
uint16_t seq;
|
||||||
|
|
||||||
|
int16_t wheels[4];
|
||||||
|
bool dirty_wheels;
|
||||||
|
|
||||||
|
uint8_t colors[3];
|
||||||
|
bool dirty_colors;
|
||||||
|
|
||||||
|
int16_t gimbal[2];
|
||||||
|
bool dirty_gimbal;
|
||||||
|
|
||||||
|
bool sdk_mode;
|
||||||
|
|
||||||
|
enum {
|
||||||
|
CONNECTING_TO_PROXY_PORT,
|
||||||
|
SETTING_SDK_CONNECTION,
|
||||||
|
CONNECTING_TO_DEVICE_PORT,
|
||||||
|
SETTING_SDK_MODE,
|
||||||
|
RESETTING_SUBNODE,
|
||||||
|
SUSBCRIBING_SUBNODE,
|
||||||
|
SETTING_MOVEMENT_MODE,
|
||||||
|
SENDING_HEARTBEAT,
|
||||||
|
WAITING,
|
||||||
|
READY,
|
||||||
|
STOPPED
|
||||||
|
} state;
|
||||||
|
|
||||||
|
};
|
||||||
|
|
||||||
|
int
|
||||||
|
robot_drive(Robot robot, float x, float y, float r) {
|
||||||
|
// TODO: move individual wheel speed calculation to work function
|
||||||
|
// This function may be used in a timer handler, so fast as possible
|
||||||
|
robot->wheels[0] = (y + x + r) * 1000;
|
||||||
|
robot->wheels[1] = -(y - x - r) * 1000;
|
||||||
|
robot->wheels[2] = -(y + x - r) * 1000;
|
||||||
|
robot->wheels[3] = (y - x + r) * 1000;
|
||||||
|
robot->dirty_wheels = true;
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
int
|
||||||
|
robot_aim(Robot robot, float p, float y) {
|
||||||
|
robot->gimbal[0] = p * 360;
|
||||||
|
robot->gimbal[1] = y * 360;
|
||||||
|
robot->dirty_gimbal = true;
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
int
|
||||||
|
robot_led(Robot robot, unsigned char r, unsigned char g, unsigned char b) {
|
||||||
|
robot->colors[0] = r;
|
||||||
|
robot->colors[1] = g;
|
||||||
|
robot->colors[2] = b;
|
||||||
|
robot->dirty_colors = true;
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
int
|
||||||
|
robot_heartbeat(Robot robot) {
|
||||||
|
|
||||||
|
if(robot->state != READY)
|
||||||
|
return 0;
|
||||||
|
|
||||||
|
robot->state = SENDING_HEARTBEAT;
|
||||||
|
|
||||||
|
return 1;
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
int
|
||||||
|
robot_work(Robot robot) {
|
||||||
|
|
||||||
|
union Request req = {0};
|
||||||
|
|
||||||
|
switch(robot->state) {
|
||||||
|
|
||||||
|
case WAITING:
|
||||||
|
break;
|
||||||
|
|
||||||
|
case CONNECTING_TO_PROXY_PORT:
|
||||||
|
{
|
||||||
|
robot->client->sdk_conn = connection_new(0, 0, 30030, "192.168.2.1");
|
||||||
|
robot->state = SETTING_SDK_CONNECTION;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
case SETTING_SDK_CONNECTION:
|
||||||
|
{
|
||||||
|
set_sdk_connection(&req, robot->seq++, true, CONNECTION_WIFI_AP, 0, 10010);
|
||||||
|
req_send(robot->client->sdk_conn, &req);
|
||||||
|
robot->state = WAITING;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
case CONNECTING_TO_DEVICE_PORT:
|
||||||
|
{
|
||||||
|
robot->client->dev_conn = connection_new(10010, "192.168.2.24", 20020, "192.168.2.1");
|
||||||
|
robot->state = SETTING_SDK_MODE;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
case SETTING_SDK_MODE:
|
||||||
|
{
|
||||||
|
set_sdk_mode(&req, robot->seq++, true, robot->sdk_mode);
|
||||||
|
req_send(robot->client->dev_conn, &req);
|
||||||
|
robot->state = WAITING;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
case RESETTING_SUBNODE:
|
||||||
|
{
|
||||||
|
subnode_reset(&req, robot->seq++, true);
|
||||||
|
req_send(robot->client->dev_conn, &req);
|
||||||
|
robot->state = WAITING;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
case SUSBCRIBING_SUBNODE:
|
||||||
|
{
|
||||||
|
subscribe_add_node(&req, robot->seq++, true);
|
||||||
|
req_send(robot->client->dev_conn, &req);
|
||||||
|
robot->state = WAITING;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
case SETTING_MOVEMENT_MODE:
|
||||||
|
{
|
||||||
|
set_robot_mode(&req, robot->seq++, true, MOVEMENTMODE_FREE);
|
||||||
|
req_send(robot->client->dev_conn, &req);
|
||||||
|
robot->state = WAITING;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
case SENDING_HEARTBEAT:
|
||||||
|
{
|
||||||
|
sdk_heartbeat(&req, robot->seq++, true);
|
||||||
|
req_send(robot->client->sdk_conn, &req);
|
||||||
|
robot->state = WAITING;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
case READY:
|
||||||
|
{
|
||||||
|
if(robot->dirty_wheels) {
|
||||||
|
set_wheel_speed (
|
||||||
|
&req, robot->seq++, false,
|
||||||
|
robot->wheels[0],
|
||||||
|
robot->wheels[1],
|
||||||
|
robot->wheels[2],
|
||||||
|
robot->wheels[3] );
|
||||||
|
req_send(robot->client->dev_conn, &req);
|
||||||
|
robot->dirty_wheels = false;
|
||||||
|
}
|
||||||
|
if(robot->dirty_gimbal) {
|
||||||
|
gimbal_ctrl_speed (
|
||||||
|
&req, robot->seq++, false,
|
||||||
|
robot->gimbal[0],
|
||||||
|
robot->gimbal[1],
|
||||||
|
0 );
|
||||||
|
req_send(robot->client->dev_conn, &req);
|
||||||
|
robot->dirty_gimbal = false;
|
||||||
|
}
|
||||||
|
if(robot->dirty_colors) {
|
||||||
|
set_system_led (
|
||||||
|
&req, robot->seq++, false,
|
||||||
|
robot->colors[0],
|
||||||
|
robot->colors[1],
|
||||||
|
robot->colors[2],
|
||||||
|
LEDCOMP_ALL,
|
||||||
|
0xFFFF,
|
||||||
|
LEDEFFECT_ON,
|
||||||
|
100,
|
||||||
|
100 );
|
||||||
|
req_send(robot->client->dev_conn, &req);
|
||||||
|
robot->dirty_colors = false;
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
case STOPPED:
|
||||||
|
return 0;
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
union Message resp;
|
||||||
|
poll_message(robot->client, &resp);
|
||||||
|
switch(resp.header.cmd) {
|
||||||
|
case 0:
|
||||||
|
break;
|
||||||
|
case SET_SDK_CONNECTION_CMD:
|
||||||
|
// TODO: Do more with this
|
||||||
|
if(resp.resp.sdkconn.retcode) {
|
||||||
|
robot->state = STOPPED;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
robot->state = CONNECTING_TO_DEVICE_PORT;
|
||||||
|
break;
|
||||||
|
case SET_SDK_MODE_CMD:
|
||||||
|
// TODO: Do more with this
|
||||||
|
if(resp.resp.sdkmode.retcode) {
|
||||||
|
robot->state = STOPPED;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
robot->state = robot->sdk_mode ? RESETTING_SUBNODE : STOPPED;
|
||||||
|
break;
|
||||||
|
case SUBNODE_RESET_CMD:
|
||||||
|
if(resp.resp.subnodereset.retcode) {
|
||||||
|
robot->state = STOPPED;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
robot->state = SUSBCRIBING_SUBNODE;
|
||||||
|
break;
|
||||||
|
case SUBSCRIBE_ADD_NODE_CMD:
|
||||||
|
if(resp.resp.subnodeadd.retcode && resp.resp.subnodeadd.retcode != 0x50) {
|
||||||
|
robot->state = STOPPED;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
robot->state = SETTING_MOVEMENT_MODE;
|
||||||
|
break;
|
||||||
|
case SET_ROBOT_MODE_CMD:
|
||||||
|
if(resp.resp.mvmode.retcode) {
|
||||||
|
robot->state = STOPPED;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
robot->state = SENDING_HEARTBEAT;
|
||||||
|
break;
|
||||||
|
case SDK_HEARTBEAT_CMD:
|
||||||
|
if(resp.resp.heartbeat.retcode) {
|
||||||
|
robot->state = STOPPED;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
robot->state = READY;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
return 1;
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
Robot robot_new() {
|
||||||
|
struct RobotImp* robot = malloc(sizeof(struct RobotImp));
|
||||||
|
memset(robot, 0, sizeof(struct RobotImp));
|
||||||
|
return robot;
|
||||||
|
}
|
||||||
|
|
||||||
|
int robot_init(Robot robot) {
|
||||||
|
robot->client = client_new();
|
||||||
|
robot->sdk_mode = true;
|
||||||
|
robot->state = CONNECTING_TO_PROXY_PORT;
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
int robot_stop(Robot robot) {
|
||||||
|
robot->sdk_mode = false;
|
||||||
|
robot->state = SETTING_SDK_MODE;
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
+3
-4
@@ -11,7 +11,6 @@ Client client_new() {
|
|||||||
struct Client* client = malloc(sizeof(struct Client));
|
struct Client* client = malloc(sizeof(struct Client));
|
||||||
memset(client, 0, sizeof(struct Client));
|
memset(client, 0, sizeof(struct Client));
|
||||||
// TODO: Make this configurable
|
// TODO: Make this configurable
|
||||||
client->hostbyte = host2byte(CLIENT_HOST, CLIENT_INDEX);
|
|
||||||
return client;
|
return client;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -19,8 +18,6 @@ void client_connect(Client client) {
|
|||||||
client->sdk_conn = connection_new(0, 0, 30030, "192.168.2.1");
|
client->sdk_conn = connection_new(0, 0, 30030, "192.168.2.1");
|
||||||
// TODO: This should probably use the source IP in the response body
|
// TODO: This should probably use the source IP in the response body
|
||||||
client->dev_conn = connection_new(10010, "192.168.2.24", 20020, "192.168.2.1");
|
client->dev_conn = connection_new(10010, "192.168.2.24", 20020, "192.168.2.1");
|
||||||
|
|
||||||
set_sdk_connection(client, CONNECTION_WIFI_AP, 0, 10010);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void poll_message(Client client, union Message* resp) {
|
void poll_message(Client client, union Message* resp) {
|
||||||
@@ -32,5 +29,7 @@ void poll_message(Client client, union Message* resp) {
|
|||||||
|
|
||||||
void drive(Client client, float x, float y, float r) {
|
void drive(Client client, float x, float y, float r) {
|
||||||
float w1 = y + x + r, w2 = y - x -r, w3 = y + x -r, w4 = y - x + r;
|
float w1 = y + x + r, w2 = y - x -r, w3 = y + x -r, w4 = y - x + r;
|
||||||
set_wheel_speed(client, w1 * 1000, -w2 * 1000, -w3 * 1000, w4 * 1000);
|
union Request req;
|
||||||
|
set_wheel_speed(&req, client->seq, false, w1 * 1000, -w2 * 1000, -w3 * 1000, w4 * 1000);
|
||||||
|
req_send(client->dev_conn, &req);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,92 +0,0 @@
|
|||||||
#include "robomaster.h"
|
|
||||||
|
|
||||||
#include <stdio.h>
|
|
||||||
#include <stdlib.h>
|
|
||||||
#include <unistd.h>
|
|
||||||
|
|
||||||
int main(int argc, char* argv[])
|
|
||||||
{
|
|
||||||
Client client = client_new();
|
|
||||||
client_connect(client);
|
|
||||||
|
|
||||||
union Message msg;
|
|
||||||
poll_message(client, &msg);
|
|
||||||
if(msg.header.cmdid != SET_SDK_CONNECTION_CMDID || msg.resp.sdkconn.retcode) {
|
|
||||||
fprintf(stderr, "Could not set SDK connection\n");
|
|
||||||
return 1;
|
|
||||||
}
|
|
||||||
|
|
||||||
set_sdk_mode(client, true);
|
|
||||||
poll_message(client, &msg);
|
|
||||||
if(msg.header.cmdid != SET_SDK_MODE_CMDID || msg.resp.sdkmode.retcode) {
|
|
||||||
fprintf(stderr, "Could not set SDK mode\n");
|
|
||||||
return 1;
|
|
||||||
}
|
|
||||||
|
|
||||||
subnode_reset(client);
|
|
||||||
poll_message(client, &msg);
|
|
||||||
if(msg.header.cmdid != SUBNODE_RESET_CMDID || msg.resp.subnodereset.retcode) {
|
|
||||||
fprintf(stderr, "Could not reset subnode subscription\n");
|
|
||||||
return 1;
|
|
||||||
}
|
|
||||||
|
|
||||||
subscribe_add_node(client);
|
|
||||||
poll_message(client, &msg);
|
|
||||||
if(msg.header.cmdid != SUBSCRIBE_ADD_NODE_CMDID || (msg.resp.subnodeadd.retcode && msg.resp.subnodeadd.retcode != 0x50)) {
|
|
||||||
fprintf(stderr, "Could not subscribe node\n");
|
|
||||||
return 1;
|
|
||||||
}
|
|
||||||
|
|
||||||
set_robot_mode(client, MOVEMENTMODE_FREE);
|
|
||||||
poll_message(client, &msg);
|
|
||||||
if(msg.header.cmdid != SET_ROBOT_MODE_CMDID || msg.resp.mvmode.retcode) {
|
|
||||||
fprintf(stderr, "Could not set move mode\n");
|
|
||||||
return 1;
|
|
||||||
}
|
|
||||||
|
|
||||||
sdk_heartbeat(client);
|
|
||||||
//poll_message(client, &msg);
|
|
||||||
//if(msg.header.cmdid != SDK_HEARTBEAT_CMDID || msg.resp.heartbeat.retcode) {
|
|
||||||
// fprintf(stderr, "Did not receive heartbeat\n");
|
|
||||||
// return 1;
|
|
||||||
//}
|
|
||||||
|
|
||||||
int c;
|
|
||||||
while((c = getopt(argc, argv, "hl:::s:::")) != -1) {
|
|
||||||
switch(c) {
|
|
||||||
case 'l':
|
|
||||||
set_system_led (
|
|
||||||
client,
|
|
||||||
strtol(argv[optind + 0], NULL, 0),
|
|
||||||
strtol(argv[optind + 1], NULL, 0),
|
|
||||||
strtol(argv[optind + 2], NULL, 0),
|
|
||||||
LEDCOMP_ALL,
|
|
||||||
0xFFFF,
|
|
||||||
LEDEFFECT_ON,
|
|
||||||
100,
|
|
||||||
100 );
|
|
||||||
poll_message(client, &msg);
|
|
||||||
if(msg.header.cmdid != SET_SYSTEM_LED_CMDID || msg.resp.led.retcode) {
|
|
||||||
fprintf(stderr, "Could not set LED color\n");
|
|
||||||
return 1;
|
|
||||||
}
|
|
||||||
break;
|
|
||||||
case 's':
|
|
||||||
chassis_speed_mode (
|
|
||||||
client,
|
|
||||||
atof(argv[optind + 0]),
|
|
||||||
atof(argv[optind + 1]),
|
|
||||||
atof(argv[optind + 2]) );
|
|
||||||
break;
|
|
||||||
case '?':
|
|
||||||
default:
|
|
||||||
fprintf(stderr, "Unknown argument %c\n", optopt);
|
|
||||||
case 'h':
|
|
||||||
printf("Usage: [-h] [-l r g b] [-w w1 w2 w3 w4]");
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
@@ -1,13 +1,42 @@
|
|||||||
#include "robomaster.h"
|
#include "roboeasy.h"
|
||||||
#include "SDL2/SDL.h"
|
#include "SDL2/SDL.h"
|
||||||
|
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <stdlib.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[]) {
|
int main(int argc, char* argv[]) {
|
||||||
if(SDL_Init(SDL_INIT_VIDEO | SDL_INIT_GAMECONTROLLER) < 0) {
|
if(SDL_Init(SDL_INIT_VIDEO | SDL_INIT_JOYSTICK) < 0) {
|
||||||
fprintf(stderr, "%s", SDL_GetError());
|
fprintf(stderr, "%s", SDL_GetError());
|
||||||
return 1;
|
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(
|
SDL_Window* win = SDL_CreateWindow(
|
||||||
"Robomaster",
|
"Robomaster",
|
||||||
@@ -17,51 +46,18 @@ int main(int argc, char* argv[]) {
|
|||||||
SDL_WINDOW_RESIZABLE );
|
SDL_WINDOW_RESIZABLE );
|
||||||
if(!win) {
|
if(!win) {
|
||||||
fprintf(stderr, "%s", SDL_GetError());
|
fprintf(stderr, "%s", SDL_GetError());
|
||||||
return 1;
|
|
||||||
}
|
|
||||||
|
|
||||||
Client client = client_new();
|
|
||||||
client_connect(client);
|
|
||||||
|
|
||||||
union Message msg;
|
|
||||||
poll_message(client, &msg);
|
|
||||||
if(msg.header.cmdid != SET_SDK_CONNECTION_CMDID || msg.resp.sdkconn.retcode) {
|
|
||||||
fprintf(stderr, "Could not set SDK connection\n");
|
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
set_sdk_mode(client, true);
|
Robot robot = robot_new();
|
||||||
poll_message(client, &msg);
|
robot_init(robot);
|
||||||
if(msg.header.cmdid != SET_SDK_MODE_CMDID || msg.resp.sdkmode.retcode) {
|
SDL_AddTimer(75, drive_timer_handler, robot);
|
||||||
fprintf(stderr, "Could not set SDK mode\n");
|
SDL_AddTimer(1000, heartbeat_timer_handler, robot);
|
||||||
return 1;
|
|
||||||
}
|
|
||||||
|
|
||||||
subnode_reset(client);
|
int h, w;
|
||||||
poll_message(client, &msg);
|
SDL_GetWindowSize(win, &w, &h);
|
||||||
if(msg.header.cmdid != SUBNODE_RESET_CMDID || msg.resp.subnodereset.retcode) {
|
|
||||||
fprintf(stderr, "Could not reset subnode subscription\n");
|
|
||||||
return 1;
|
|
||||||
}
|
|
||||||
|
|
||||||
subscribe_add_node(client);
|
while(robot_work(robot)) {
|
||||||
poll_message(client, &msg);
|
|
||||||
if(msg.header.cmdid != SUBSCRIBE_ADD_NODE_CMDID || (msg.resp.subnodeadd.retcode && msg.resp.subnodeadd.retcode != 0x50)) {
|
|
||||||
fprintf(stderr, "Could not subscribe node\n");
|
|
||||||
return 1;
|
|
||||||
}
|
|
||||||
|
|
||||||
set_robot_mode(client, MOVEMENTMODE_FREE);
|
|
||||||
poll_message(client, &msg);
|
|
||||||
if(msg.header.cmdid != SET_ROBOT_MODE_CMDID || msg.resp.mvmode.retcode) {
|
|
||||||
fprintf(stderr, "Could not set move mode\n");
|
|
||||||
return 1;
|
|
||||||
}
|
|
||||||
|
|
||||||
bool quit = false;
|
|
||||||
Uint32 time = SDL_GetTicks();
|
|
||||||
float x = 0, y = 0, z = 0;
|
|
||||||
while(!quit) {
|
|
||||||
SDL_Event event;
|
SDL_Event event;
|
||||||
while(SDL_PollEvent(&event)) {
|
while(SDL_PollEvent(&event)) {
|
||||||
switch(event.type) {
|
switch(event.type) {
|
||||||
@@ -69,53 +65,70 @@ int main(int argc, char* argv[]) {
|
|||||||
case SDL_KEYDOWN:
|
case SDL_KEYDOWN:
|
||||||
switch(event.key.keysym.scancode) {
|
switch(event.key.keysym.scancode) {
|
||||||
case SDL_SCANCODE_Q:
|
case SDL_SCANCODE_Q:
|
||||||
z = event.type == SDL_KEYUP ? 0 : 250;
|
z = event.type == SDL_KEYUP ? 0 : 0.25;
|
||||||
break;
|
break;
|
||||||
case SDL_SCANCODE_E:
|
case SDL_SCANCODE_E:
|
||||||
z = event.type == SDL_KEYUP ? 0 : -250;
|
z = event.type == SDL_KEYUP ? 0 : -0.25;
|
||||||
break;
|
break;
|
||||||
case SDL_SCANCODE_LEFT:
|
case SDL_SCANCODE_LEFT:
|
||||||
case SDL_SCANCODE_A:
|
case SDL_SCANCODE_A:
|
||||||
x = event.type == SDL_KEYUP ? 0 : 250;
|
x = event.type == SDL_KEYUP ? 0 : 0.25;
|
||||||
break;
|
break;
|
||||||
case SDL_SCANCODE_RIGHT:
|
case SDL_SCANCODE_RIGHT:
|
||||||
case SDL_SCANCODE_D:
|
case SDL_SCANCODE_D:
|
||||||
x = event.type == SDL_KEYUP ? 0 : -250;
|
x = event.type == SDL_KEYUP ? 0 : -0.25;
|
||||||
break;
|
break;
|
||||||
case SDL_SCANCODE_UP:
|
case SDL_SCANCODE_UP:
|
||||||
case SDL_SCANCODE_W:
|
case SDL_SCANCODE_W:
|
||||||
y = event.type == SDL_KEYUP ? 0 : 250;
|
y = event.type == SDL_KEYUP ? 0 : 0.25;
|
||||||
break;
|
break;
|
||||||
case SDL_SCANCODE_DOWN:
|
case SDL_SCANCODE_DOWN:
|
||||||
case SDL_SCANCODE_S:
|
case SDL_SCANCODE_S:
|
||||||
y = event.type == SDL_KEYUP ? 0 : -250;
|
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;
|
break;
|
||||||
default: 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:
|
case SDL_WINDOWEVENT:
|
||||||
if(event.window.event != SDL_WINDOWEVENT_CLOSE) break;
|
if(event.window.event != SDL_WINDOWEVENT_CLOSE) break;
|
||||||
case SDL_QUIT:
|
case SDL_QUIT:
|
||||||
quit = true;
|
robot_stop(robot);
|
||||||
default: break;
|
default: break;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
Uint32 currtime = SDL_GetTicks();
|
|
||||||
if(currtime - time >= 75)
|
|
||||||
{
|
|
||||||
time = currtime;
|
|
||||||
sdk_heartbeat(client);
|
|
||||||
float w1 = y + x + z, w2 = y - x -z, w3 = y + x -z, w4 = y - x + z;
|
|
||||||
set_wheel_speed(client, w1, -w2, -w3, w4);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
set_sdk_mode(client, false);
|
|
||||||
poll_message(client, &msg);
|
|
||||||
if(msg.header.cmdid != SET_SDK_MODE_CMDID || msg.resp.sdkmode.retcode) {
|
|
||||||
fprintf(stderr, "Could not disable SDK mode\n");
|
|
||||||
return 1;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
SDL_JoystickClose(joystick);
|
||||||
SDL_Quit();
|
SDL_Quit();
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user