Skip to content

Commit

Permalink
Merge pull request #9 from raul-ortega/Automatic-protocol-detection
Browse files Browse the repository at this point in the history
Automatic protocol detection: it detects NMEA (GPGGA), LTM (GFRAME), MAVLINK (msg_id 24 and 33), FRXKY_D, FRSKY_X (smartport), FMD, and RVOSD.
  • Loading branch information
raul-ortega authored May 6, 2017
2 parents 55468e0 + 5e551ca commit 29acc00
Show file tree
Hide file tree
Showing 11 changed files with 271 additions and 67 deletions.
3 changes: 2 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -226,7 +226,8 @@ TRACKER_SRC = tracker/main.c \
tracker/ltm.c \
tracker/frskyx.c \
tracker/gps_estimation.c \
tracker/interpolation.c
tracker/interpolation.c \
tracker/protocol_detection.c



Expand Down
6 changes: 6 additions & 0 deletions src/main/io/display.c
Original file line number Diff line number Diff line change
Expand Up @@ -952,6 +952,12 @@ void showDebugPage(void)
}
#endif

void updateDisplayProtocolTitle(uint16_t protocol){
master_telemetry_protocol = protocol;
telemetry_failed_cs = 0;
handlePageChange();
}

void updateDisplay(void)
{
uint32_t now = micros();
Expand Down
1 change: 1 addition & 0 deletions src/main/io/display.h
Original file line number Diff line number Diff line change
Expand Up @@ -137,3 +137,4 @@ void displayEnablePageCycling(void);
void displayDisablePageCycling(void);
void displayResetPageCycling(void);
void displaySetNextPageChangeAt(uint32_t futureMicros);
void updateDisplayProtocolTitle(uint16_t protocol);
152 changes: 89 additions & 63 deletions src/main/tracker/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,7 @@
#include "tracker/servos.h"
#include "tracker/gps_estimation.h"
#include "tracker/interpolation.h"
#include "tracker/protocol_detection.h"

void calcTilt(void);
void getError(void);
Expand Down Expand Up @@ -145,6 +146,9 @@ void updateCalibratePan();
uint16_t calculateDeltaHeading(uint16_t heading1, uint16_t heading2);
void setEpsMode(void);
int16_t getOffset(int16_t offset_master,int8_t offset_trim);
void updateTelemetryProtocol(uint16_t protocol);
void protocolInit(void);
void trackingInit(void);

//EASING
int16_t _lastTilt;
Expand Down Expand Up @@ -299,69 +303,10 @@ extern int32_t telemetry_lon;

void tracker_setup(void)
{
switch(masterConfig.telemetry_protocol)
{
case TP_SERVOTEST:
ENABLE_PROTOCOL(TP_SERVOTEST);
break;
case TP_MFD:
ENABLE_PROTOCOL(TP_MFD);
featureClear(FEATURE_EPS);
featureClear(FEATURE_GPS);
break;
case TP_GPS_TELEMETRY:
ENABLE_PROTOCOL(TP_GPS_TELEMETRY);
break;
case TP_MAVLINK:
ENABLE_PROTOCOL(TP_MAVLINK);
break;
case TP_RVOSD:
ENABLE_PROTOCOL(TP_RVOSD);
break;
case TP_FRSKY_D:
ENABLE_PROTOCOL(TP_FRSKY_D);
telemetry_diy_gps = masterConfig.telemetry_diy_gps;
break;
case TP_FRSKY_X:
ENABLE_PROTOCOL(TP_FRSKY_X);
telemetry_diy_gps = masterConfig.telemetry_diy_gps;
break;
case TP_LTM:
ENABLE_PROTOCOL(TP_LTM);
break;
case TP_LTM_FRSKYD:
ENABLE_PROTOCOL(TP_LTM_FRSKYD);
break;
}

setEpsMode();
updateEPSParams();

OFFSET_TRIM = masterConfig.offset_trim;
OFFSET = getOffset(masterConfig.offset,masterConfig.offset_trim);

gotAlt = false;
gotFix = false;

homeSet = false;
homeReset = false;

trackingStarted = false;

settingHome = false;

previousState = true;
homeButtonPreviousState = true;

currentState = true;
homeButtonCurrentState = true;

gotNewHeading = false;

menuState = 0;
protocolInit();

if(PROTOCOL(TP_MFD))
mfdTestMode = false;
trackingInit();

trackerSerial = openSerialPort(masterConfig.serialConfig.portConfigs[0].identifier, FUNCTION_NONE, NULL, baudRates[masterConfig.serialConfig.portConfigs[0].msp_baudrateIndex], MODE_RXTX, SERIAL_NOT_INVERTED);
setPrintfSerialPort(trackerSerial);
Expand Down Expand Up @@ -398,7 +343,33 @@ void tracker_setup(void)

}

void trackingInit(void){
setEpsMode();
updateEPSParams();

OFFSET_TRIM = masterConfig.offset_trim;
OFFSET = getOffset(masterConfig.offset,masterConfig.offset_trim);

gotAlt = false;
gotFix = false;

homeSet = false;
homeReset = false;

trackingStarted = false;

settingHome = false;

previousState = true;
homeButtonPreviousState = true;

currentState = true;
homeButtonCurrentState = true;

gotNewHeading = false;

menuState = 0;
}

void tracker_loop(void)
{
Expand Down Expand Up @@ -774,16 +745,18 @@ void updateReadTelemetry(void){

evaluateOtherData(trackerSerial,c);

if(!PROTOCOL(TP_SERVOTEST)) encodeTargetData(c);

LED0_ON;

if(!PROTOCOL(TP_SERVOTEST) && !cliMode)
encodeTargetData(c);

if(!PROTOCOL(TP_SERVOTEST)) {
gotTelemetry = true;
lostTelemetry = false;
lostTelemetry_timer = 0;
}


} else {
LED0_OFF;
gotTelemetry = false;
Expand All @@ -804,6 +777,7 @@ void updateTelemetryLost(void){

if(!gotTelemetry && (millis() - lostTelemetry_timer > 3000)){
lostTelemetry = true;
enableProtocolDetection();
}
}

Expand Down Expand Up @@ -1499,3 +1473,55 @@ void updateEPSParams(){
EPS_DISTANCE_GAIN = masterConfig.eps_gain.distance;
EPS_FREQUENCY = masterConfig.eps_frequency;
}

void updateTelemetryProtocol(uint16_t protocol){
if (protocol == 0)
return;

if(protocol != masterConfig.telemetry_protocol) {
masterConfig.telemetry_protocol = protocol;
protocolInit();
trackingInit();
updateDisplayProtocolTitle(protocol);
disableProtocolDetection();
}
}

void protocolInit(void){
DISABLE_PROTOCOL(0b111111111111);
switch(masterConfig.telemetry_protocol)
{
case TP_SERVOTEST:
ENABLE_PROTOCOL(TP_SERVOTEST);
break;
case TP_MFD:
ENABLE_PROTOCOL(TP_MFD);
featureClear(FEATURE_EPS);
featureClear(FEATURE_GPS);
mfdTestMode = false;
break;
case TP_GPS_TELEMETRY:
ENABLE_PROTOCOL(TP_GPS_TELEMETRY);
break;
case TP_MAVLINK:
ENABLE_PROTOCOL(TP_MAVLINK);
break;
case TP_RVOSD:
ENABLE_PROTOCOL(TP_RVOSD);
break;
case TP_FRSKY_D:
ENABLE_PROTOCOL(TP_FRSKY_D);
telemetry_diy_gps = masterConfig.telemetry_diy_gps;
break;
case TP_FRSKY_X:
ENABLE_PROTOCOL(TP_FRSKY_X);
telemetry_diy_gps = masterConfig.telemetry_diy_gps;
break;
case TP_LTM:
ENABLE_PROTOCOL(TP_LTM);
break;
case TP_LTM_FRSKYD:
ENABLE_PROTOCOL(TP_LTM_FRSKYD);
break;
}
}
2 changes: 1 addition & 1 deletion src/main/tracker/mavlink.c
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ void mavlink_encodeTargetData(uint8_t c) {
if (mavlink_parse_char(0, c, &msg, &status)) {
mavlink_handleMessage(&msg);
if (status.parse_error > 0)
telemetry_failed_cs++;
telemetry_failed_cs++;
}
}

120 changes: 120 additions & 0 deletions src/main/tracker/protocol_detection.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
/*
* This file is part of u360gts, aka amv-open360tracker 32bits:
* https://github.com/raul-ortega/amv-open360tracker-32bits
*
* The code below is an adaptation by Raúl Ortega of the original code of Ghettostation antenna tracker
* https://github.com/KipK/Ghettostation
*
* u360gts is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* u360gts is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with u360gts. If not, see <http://www.gnu.org/licenses/>.
*
*/

/*
*/
#include "config.h"
#include <stdint.h>
#include <stdbool.h>
#include "config/runtime_config.h"

uint16_t protocolDetectionParser(uint8_t c);
// machine states
enum protocolDetectionStates {
DETECTION_STATE_IDLE,
DETECTION_STATE_START,
DETECTION_STATE_START_FRXKY,
DETECTION_STATE_START_MAVLINK,
DETECTION_STATE_START_MFD,
DETECTION_STATE_DETECTED
};

static uint8_t detectionState = DETECTION_STATE_IDLE;
static uint8_t detectionPacketIdex=0;
static uint16_t protocolDetected = 0;
static uint16_t lastProtocolDetected = 0;

uint16_t protocolDetectionParser(uint8_t c){

switch(detectionState){
case DETECTION_STATE_IDLE:
protocolDetected = 0;
if (c =='#' || c == 'X') {
detectionState = DETECTION_STATE_START_MFD;
detectionPacketIdex = 0;
} else if (c == 0x7E)
detectionState = DETECTION_STATE_START_FRXKY;
else if (c == 254 && detectionPacketIdex > 10) {
/*detectionState = DETECTION_STATE_START_MAVLINK;*/
protocolDetected = TP_MAVLINK;
detectionState = DETECTION_STATE_DETECTED;
} else if (c == '$')
detectionState = DETECTION_STATE_START;
detectionPacketIdex ++;
break;
case DETECTION_STATE_START_MFD:
if ((c == '#' || c == 'X') && detectionPacketIdex < 3)
detectionPacketIdex++;
else if (detectionPacketIdex > 5 && c == 'D'){
protocolDetected = TP_MFD;
detectionState = DETECTION_STATE_DETECTED;
} else
detectionState = DETECTION_STATE_IDLE;
break;
case DETECTION_STATE_START_FRXKY:
if (c == 0xFD) {
protocolDetected = TP_FRSKY_D;
detectionState = DETECTION_STATE_DETECTED;
} else if (detectionState == DETECTION_STATE_START_FRXKY && c ==0x10){
protocolDetected = TP_FRSKY_X;
detectionState = DETECTION_STATE_DETECTED;
detectionPacketIdex = 0;
} else if (detectionPacketIdex > 10)
detectionState = DETECTION_STATE_IDLE;
break;
case DETECTION_STATE_START_MAVLINK:
if (detectionPacketIdex < 5)
detectionPacketIdex++;
else if (c == 24 || c == 33){
protocolDetected = TP_MAVLINK;
detectionState = DETECTION_STATE_DETECTED;
} else
detectionState = DETECTION_STATE_IDLE;
break;
case DETECTION_STATE_START:
detectionState = DETECTION_STATE_DETECTED;
switch(c){
case 'T':
protocolDetected = TP_LTM;
break;
case 'G':
protocolDetected = TP_GPS_TELEMETRY;
break;
case '1':
case 'R':
case 'V':
protocolDetected = TP_RVOSD;
break;
default:
detectionState = DETECTION_STATE_IDLE;
break;
}
break;
case DETECTION_STATE_DETECTED:
detectionState = DETECTION_STATE_IDLE;
detectionPacketIdex = 0;
break;
}
return protocolDetected;

}
30 changes: 30 additions & 0 deletions src/main/tracker/protocol_detection.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
/*
* This file is part of u360gts, aka amv-open360tracker 32bits:
* https://github.com/raul-ortega/amv-open360tracker-32bits
*
* The code below is an adaptation by Raúl Ortega of the original code of Ghettostation antenna tracker
* https://github.com/KipK/Ghettostation
*
* u360gts is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* u360gts is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with u360gts. If not, see <http://www.gnu.org/licenses/>.
*
*/

/*
*/
#include <stdint.h>

uint16_t protocolDetectionParser(uint8_t c);


Loading

0 comments on commit 29acc00

Please sign in to comment.