Skip to content

Commit 0892108

Browse files
PetervdPerk-NXPmrpollo
authored andcommitted
msp_osd: Add VTX config, stick commands and arming status
- Adds MSPv1 rx parsing to fetch VTX config - Allows to inspect and change VTX channel through CLI - Forward MSP_RC for stick commands osd - Forward MSP_STATUS for arming status for PIT and LP mode
1 parent 39fbca1 commit 0892108

File tree

8 files changed

+394
-16
lines changed

8 files changed

+394
-16
lines changed

src/drivers/osd/msp_osd/MspV1.cpp

Lines changed: 76 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@
3939
#include <float.h>
4040
#include <string.h>
4141
#include <math.h>
42+
#include <stdio.h>
4243

4344
#include <drivers/drv_pwm_output.h>
4445
#include <drivers/drv_hrt.h>
@@ -62,7 +63,7 @@ struct msp_message_descriptor_t {
6263
uint8_t message_size;
6364
};
6465

65-
#define MSP_DESCRIPTOR_COUNT 11
66+
#define MSP_DESCRIPTOR_COUNT 12
6667
const msp_message_descriptor_t msp_message_descriptors[MSP_DESCRIPTOR_COUNT] = {
6768
{MSP_OSD_CONFIG, true, sizeof(msp_osd_config_t)},
6869
{MSP_NAME, true, sizeof(msp_name_t)},
@@ -75,10 +76,9 @@ const msp_message_descriptor_t msp_message_descriptors[MSP_DESCRIPTOR_COUNT] = {
7576
{MSP_COMP_GPS, true, sizeof(msp_comp_gps_t)},
7677
{MSP_ESC_SENSOR_DATA, true, sizeof(msp_esc_sensor_data_dji_t)},
7778
{MSP_MOTOR_TELEMETRY, true, sizeof(msp_motor_telemetry_t)},
79+
{MSP_RC, true, sizeof(msp_rc_t)},
7880
};
7981

80-
#define MSP_FRAME_START_SIZE 5
81-
#define MSP_CRC_SIZE 1
8282
bool MspV1::Send(const uint8_t message_id, const void *payload)
8383
{
8484
uint32_t payload_size = 0;
@@ -149,3 +149,76 @@ bool MspV1::Send(const uint8_t message_id, const void *payload, uint32_t payload
149149
int packet_size = MSP_FRAME_START_SIZE + payload_size + MSP_CRC_SIZE;
150150
return write(_fd, packet, packet_size) == packet_size;
151151
}
152+
153+
154+
int MspV1::Receive(uint8_t *payload, uint8_t *message_id)
155+
{
156+
uint8_t payload_size;
157+
uint8_t crc;
158+
uint8_t calc_crc;
159+
int ret;
160+
161+
while (!has_header) {
162+
int bytes_available = 0;
163+
164+
if (ioctl(_fd, FIONREAD, &bytes_available) < 0) {
165+
return -EIO;
166+
}
167+
168+
if (bytes_available < 5) {
169+
return -EWOULDBLOCK;
170+
}
171+
172+
while (bytes_available > 4) {
173+
if ((ret = read(_fd, header, 1)) != 1) {
174+
return ret;
175+
}
176+
177+
bytes_available--;
178+
179+
if (header[0] == '$') {
180+
break;
181+
}
182+
183+
}
184+
185+
if (header[0] != '$') {
186+
return -EWOULDBLOCK;
187+
}
188+
189+
if ((ret = read(_fd, &header[1], 4)) != 4) {
190+
return ret;
191+
}
192+
193+
if (header[0] == '$' && header[1] == 'M' && header[2] == '<') {
194+
has_header = true;
195+
}
196+
}
197+
198+
payload_size = header[3];
199+
*message_id = header[4];
200+
201+
if ((ret = read(_fd, payload, payload_size + MSP_CRC_SIZE)) != payload_size + MSP_CRC_SIZE) {
202+
if (ret != -EWOULDBLOCK) {
203+
has_header = false;
204+
}
205+
206+
return ret;
207+
}
208+
209+
has_header = false;
210+
211+
crc = payload[payload_size];
212+
213+
calc_crc = payload_size ^ header[4];
214+
215+
for (int i = 0; i < payload_size; i++) {
216+
calc_crc ^= payload[i];
217+
}
218+
219+
if (calc_crc != crc) {
220+
return -EINVAL;
221+
}
222+
223+
return payload_size;
224+
}

src/drivers/osd/msp_osd/MspV1.hpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,14 +33,20 @@
3333

3434
#pragma once
3535

36+
#define MSP_FRAME_START_SIZE 5
37+
#define MSP_CRC_SIZE 1
38+
3639
class MspV1
3740
{
3841
public:
3942
MspV1(int fd);
4043
int GetMessageSize(int message_type);
4144
bool Send(const uint8_t message_id, const void *payload);
4245
bool Send(const uint8_t message_id, const void *payload, uint32_t payload_size);
46+
int Receive(uint8_t *payload, uint8_t *message_id);
4347

4448
private:
4549
int _fd{-1};
50+
uint8_t header[MSP_FRAME_START_SIZE + MSP_CRC_SIZE];
51+
bool has_header{false};
4652
};

src/drivers/osd/msp_osd/module.yaml

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,3 +97,14 @@ parameters:
9797
min: 100
9898
max: 10000
9999
default: 500
100+
101+
# RC Stick input
102+
OSD_RC_STICK:
103+
description:
104+
short: OSD RC Stick commands
105+
long: |
106+
Forward RC stick input to VTX when disarmed
107+
type: int32
108+
min: 0
109+
max: 1
110+
default: 1

src/drivers/osd/msp_osd/msp_defines.h

Lines changed: 60 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,8 @@
4848
#define MSP_ARMING_CONFIG 61
4949
#define MSP_RX_MAP 64 // get channel map (also returns number of channels total)
5050
#define MSP_LOOP_TIME 73 // FC cycle time i.e looptime parameter
51+
#define MSP_GET_VTX_CONFIG 88
52+
#define MSP_SET_VTX_CONFIG 89
5153
#define MSP_STATUS 101
5254
#define MSP_RAW_IMU 102
5355
#define MSP_SERVO 103
@@ -75,10 +77,12 @@
7577
#define MSP_SET_PID 202 // set P I D coeff
7678

7779
// commands
78-
#define MSP_SET_HEAD 211 // define a new heading hold direction
79-
#define MSP_SET_RAW_RC 200 // 8 rc chan
80-
#define MSP_SET_RAW_GPS 201 // fix, numsat, lat, lon, alt, speed
81-
#define MSP_SET_WP 209 // sets a given WP (WP#, lat, lon, alt, flags)
80+
#define MSP_SET_HEAD 211 // define a new heading hold direction
81+
#define MSP_SET_RAW_RC 200 // 8 rc chan
82+
#define MSP_SET_RAW_GPS 201 // fix, numsat, lat, lon, alt, speed
83+
#define MSP_SET_WP 209 // sets a given WP (WP#, lat, lon, alt, flags)
84+
#define MSP_SET_VTXTABLE_BAND 227
85+
#define MSP_SET_VTXTABLE_POWERLEVEL 228
8286

8387
// bits of getActiveModes() return value
8488
#define MSP_MODE_ARM 0
@@ -895,6 +899,58 @@ struct msp_status_BF_t {
895899
uint8_t extra_flags;
896900
} __attribute__((packed));
897901

902+
struct msp_set_vtx_config_t {
903+
uint16_t new_freq; // if setting frequency then full uint16 is the frequency in MHz (ie. 5800)
904+
//if setting band channel than band is high 8 bits and channel is low 8 bits
905+
uint8_t power_level;
906+
uint8_t pit_mode; // 0 = off, 1 = on
907+
uint8_t low_power_disarm;
908+
uint16_t pit_freq;
909+
uint8_t user_band;
910+
uint8_t user_channel;
911+
uint16_t user_freq; // in MHz, 0 if using band & channel
912+
uint8_t band_count;
913+
uint8_t channel_count;
914+
uint8_t power_count;
915+
uint8_t clear_vtxtable; // Bool
916+
} __attribute__((packed));
917+
918+
struct msp_get_vtx_config_t {
919+
uint8_t vtx_type;
920+
uint8_t band;
921+
uint8_t channel;
922+
uint8_t power_index;
923+
uint8_t pit_mode; // 0 = off, 1 = on
924+
uint16_t freq; // in MHz, 0 if using band & channel
925+
uint8_t device_ready;
926+
uint8_t low_power_disarm;
927+
} __attribute__((packed));
928+
929+
struct msp_set_vtxtable_powerlevel_t {
930+
uint8_t index;
931+
uint16_t power_value;
932+
uint8_t power_label_length;
933+
uint8_t power_label_name[3];
934+
} __attribute__((packed));
935+
936+
#define VTX_TABLE_BAND_NAME_LENGTH 8
937+
#define VTXDEV_MSP 5
938+
939+
//29 bytes
940+
941+
struct msp_set_vtxtable_band_t {
942+
uint8_t band;
943+
uint8_t band_name_length;
944+
uint8_t band_label_name[VTX_TABLE_BAND_NAME_LENGTH];
945+
uint8_t band_letter;
946+
uint8_t is_factory_band;
947+
uint8_t channel_count;
948+
uint16_t frequency[8];
949+
} __attribute__((packed));
950+
951+
952+
953+
898954
////ArduPlane
899955
enum arduPlaneModes_e {
900956
MANUAL = 0,

0 commit comments

Comments
 (0)