-
-
Notifications
You must be signed in to change notification settings - Fork 344
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Enable serial trainer for foreign protocols (SBUS improvements / CRSF / Ibus / SumD) #4102
Draft
gagarinlg
wants to merge
41
commits into
main
Choose a base branch
from
wmibus
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
Show all changes
41 commits
Select commit
Hold shift + click to select a range
03b56fb
feat: process CRSF telemetry by frame
3djc a2b4693
WiP: test with USART prio 0
raphaelcoeffic 54fd68a
WiP: test with PPM as debug pin on Boxer
raphaelcoeffic 4d04605
fix: CRSF frame parser
raphaelcoeffic 8d0a5e7
Remove debug pin
raphaelcoeffic 7c7d5a9
Fixed frame processing on internal module
raphaelcoeffic 07ee40c
Simu fixes
raphaelcoeffic 9ed189c
Some more fixes
raphaelcoeffic c57dcfa
fix: check pkg len against buffer len
raphaelcoeffic cbfbaf2
fix(radio): Better iteration through mixers on display, sanitize inva…
raphaelcoeffic 6d2034f
fix: Deactivate logs and close log file if USB is connected (#4024)
mha1 6a22fca
first version
wimalopaan 7df187e
WIP: intermediate commit
wimalopaan 4485e14
fix taranis
wimalopaan d21435d
fix translations
wimalopaan 0e92e22
fixed use of 1ms tick
wimalopaan 843fef0
do not call SBus::tick1ms() if not SBUS enabled
wimalopaan 9ebe01e
restored switches.cpp
wimalopaan c1b84f5
- cleaned code
wimalopaan 7687200
wip: just compiles after rebase
wimalopaan 3588335
only allow one trainer-input via serial at a time
wimalopaan d08c206
* removed IBUS trainer via ext module bay
wimalopaan 07dfd94
after serial refactor get it working again
wimalopaan 2819448
added CRSF trainer input
wimalopaan 40e4759
changed baudrate to 420KB for CRSF trainer input
wimalopaan 1731c6b
fixed silly mistakes decoding CRSF
wimalopaan 8f41c01
fixes the freeze problem on startup
wimalopaan 47f894e
WIP for SumDV1/V3
wimalopaan b0aa9c3
refactored protocol adapters (sbus, ibus, crsf, sumd)
wimalopaan 68fd791
fixed taranis problems
wimalopaan 9909d23
Now also SumDV3 fully functional and working
wimalopaan 77fd02d
* refactored sbus/crsf/ibus/sumd v1/v3 trainer input
wimalopaan 49b4e93
refactor class templates
wimalopaan 251ca94
fixed lua api
wimalopaan e9b0dc0
improved LS switch handling
wimalopaan 562f55b
added EXTENDED_TARINER for X9e
wimalopaan d578d4c
* little cleanup
wimalopaan b01fda6
chore: fix rebase issues
gagarinlg 2fd5882
chore: remove unused defines
gagarinlg 28a6118
chore: de-templatify the CRSF trainer code and fix a potential bit sh…
gagarinlg 36ad3c1
chore: WIP: convert serial trainer to idle callback
gagarinlg File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
#include "crsf.h" | ||
|
||
static CRSF::Servo crsfTrainer; | ||
|
||
void crsfTrainerPauseCheck() { | ||
#if !defined(SIMU) | ||
# if defined(AUX_SERIAL) || defined(AUX2_SERIAL) | ||
if (hasSerialMode(UART_MODE_CRSF_TRAINER) >= 0) { | ||
crsfTrainer.tick1ms(); | ||
processCrsfInput(); | ||
} | ||
# endif | ||
#endif | ||
} | ||
|
||
void processCrsfInput() { | ||
#if !defined(SIMU) | ||
uint8_t rxchar; | ||
|
||
while (sbusAuxGetByte(&rxchar)) { | ||
crsfTrainer.process(rxchar, [&](){ | ||
crsfTrainer.convert(trainerInput); | ||
trainerInputValidityTimer = TRAINER_IN_VALID_TIMEOUT; | ||
}); | ||
} | ||
#endif | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,143 @@ | ||
#pragma once | ||
|
||
#include "opentx.h" | ||
#include "trainer.h" | ||
#include "crc.h" | ||
|
||
#include <algorithm> | ||
#include <limits> | ||
|
||
void processCrsfInput(); | ||
void crsfTrainerPauseCheck(); | ||
|
||
#ifdef __GNUC__ | ||
# pragma GCC diagnostic push | ||
# pragma GCC diagnostic error "-Wswitch" // unfortunately the project uses -Wnoswitch | ||
#endif | ||
|
||
namespace CRSF { | ||
static constexpr uint32_t baudrate{420000}; | ||
void crsfTrainerOnIdle(); | ||
struct Servo { | ||
using Crsf = Trainer::Protocol::Crsf; | ||
using MesgType = Crsf::MesgType; | ||
|
||
static constexpr uint8_t mPauseCount{2}; // 2ms | ||
|
||
static constexpr uint8_t CRSFChannels{16}; | ||
|
||
enum class State : uint8_t {Undefined, GotFCAddress, GotLength, Channels, Data, AwaitCRC, AwaitCRCAndDecode}; | ||
|
||
static inline int16_t convertCrsfToPuls(uint16_t const value) { | ||
const int16_t centered = value - Crsf::CenterValue; | ||
return Trainer::clamp((centered * 5) / 8); | ||
} | ||
|
||
inline void tick1ms() { | ||
if (mPauseCounter > 0) { | ||
--mPauseCounter; | ||
} | ||
else { | ||
mState = State::Undefined; | ||
} | ||
} | ||
|
||
inline void process(const uint8_t b, const std::function<void()> f) { | ||
mPauseCounter = mPauseCount; | ||
++mBytesCounter; | ||
switch(mState) { // enum-switch -> no default (intentional) | ||
case State::Undefined: | ||
csum.reset(); | ||
if (b == Crsf::FcAddress) { | ||
mState = State::GotFCAddress; | ||
} | ||
break; | ||
case State::GotFCAddress: | ||
if ((b > 2) && (b <= mData.size())) { | ||
mLength = b - 2; // only payload (not including type and crc) | ||
mIndex = 0; | ||
mState = State::GotLength; | ||
} | ||
else { | ||
mState = State::Undefined; | ||
} | ||
break; | ||
case State::GotLength: | ||
csum += b; | ||
if ((b == Crsf::FrametypeChannels) && (mLength == 22)) { | ||
mState = State::Channels; | ||
} | ||
else { | ||
mState = State::Data; | ||
} | ||
break; | ||
case State::Data: | ||
csum += b; | ||
if (++mIndex >= mLength) { | ||
mState = State::AwaitCRC; | ||
} | ||
break; | ||
case State::Channels: | ||
csum += b; | ||
mData[mIndex] = b; | ||
if (++mIndex >= mLength) { | ||
mState = State::AwaitCRCAndDecode; | ||
} | ||
break; | ||
case State::AwaitCRC: | ||
if (csum == b) { | ||
// only channel data is decoded, nothing todo here | ||
} | ||
mState = State::Undefined; | ||
break; | ||
case State::AwaitCRCAndDecode: | ||
if (csum == b) { | ||
++mPackagesCounter; | ||
f(); | ||
} | ||
mState = State::Undefined; | ||
break; | ||
} | ||
} | ||
inline void convert(int16_t pulses[]) { | ||
pulses[0] = (uint16_t) ((mData[0] | (uint16_t)mData[1] <<8) & Crsf::ValueMask); | ||
pulses[1] = (uint16_t) ((mData[1] >>3 | (uint16_t)mData[2] <<5) & Crsf::ValueMask); | ||
pulses[2] = (uint16_t) ((mData[2] >>6 | (uint16_t)mData[3] <<2 | (uint16_t)mData[4]<<10) & Crsf::ValueMask); | ||
pulses[3] = (uint16_t) ((mData[4] >>1 | (uint16_t)mData[5] <<7) & Crsf::ValueMask); | ||
pulses[4] = (uint16_t) ((mData[5] >>4 | (uint16_t)mData[6] <<4) & Crsf::ValueMask); | ||
pulses[5] = (uint16_t) ((mData[6] >>7 | (uint16_t)mData[7] <<1 | (uint16_t)mData[8]<<9) & Crsf::ValueMask); | ||
pulses[6] = (uint16_t) ((mData[8] >>2 | (uint16_t)mData[9] <<6) & Crsf::ValueMask); | ||
pulses[7] = (uint16_t) ((mData[9] >>5 | (uint16_t)mData[10]<<3) & Crsf::ValueMask); | ||
pulses[8] = (uint16_t) ((mData[11] | (uint16_t)mData[12]<<8) & Crsf::ValueMask); | ||
pulses[9] = (uint16_t) ((mData[12]>>3 | (uint16_t)mData[13]<<5) & Crsf::ValueMask); | ||
pulses[10] = (uint16_t) ((mData[13]>>6 | (uint16_t)mData[14]<<2 | (uint16_t)mData[15]<<10) & Crsf::ValueMask); | ||
pulses[11] = (uint16_t) ((mData[15]>>1 | (uint16_t)mData[16]<<7) & Crsf::ValueMask); | ||
pulses[12] = (uint16_t) ((mData[16]>>4 | (uint16_t)mData[17]<<4) & Crsf::ValueMask); | ||
pulses[13] = (uint16_t) ((mData[17]>>7 | (uint16_t)mData[18]<<1 | (uint16_t)mData[19]<<9) & Crsf::ValueMask); | ||
pulses[14] = (uint16_t) ((mData[19]>>2 | (uint16_t)mData[20]<<6) & Crsf::ValueMask); | ||
pulses[15] = (uint16_t) ((mData[20]>>5 | (uint16_t)mData[21]<<3) & Crsf::ValueMask); | ||
|
||
for(size_t i = 0; i < MAX_TRAINER_CHANNELS; ++i) { | ||
pulses[i] = convertCrsfToPuls(pulses[i]); | ||
} | ||
} | ||
inline uint16_t packages() { | ||
return mPackagesCounter; | ||
} | ||
inline uint16_t getBytes() { | ||
return mBytesCounter; | ||
} | ||
private: | ||
CRC8 csum; | ||
State mState = State::Undefined; | ||
MesgType mData; | ||
uint8_t mIndex = 0; | ||
uint8_t mLength = 0; | ||
uint16_t mPackagesCounter = 0; | ||
uint16_t mBytesCounter = 0; | ||
uint8_t mPauseCounter = mPauseCount; | ||
}; | ||
} | ||
#ifdef __GNUC__ | ||
# pragma GCC diagnostic pop | ||
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -20,6 +20,7 @@ | |
*/ | ||
|
||
#include "opentx.h" | ||
#include "mixes.h" | ||
|
||
enum MixFields { | ||
MIX_FIELD_NAME, | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -20,6 +20,7 @@ | |
*/ | ||
|
||
#include "opentx.h" | ||
#include "mixes.h" | ||
|
||
enum MixFields { | ||
MIX_FIELD_NAME, | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please note that although ExpressLRS receivers currently output this as their first byte, it is incorrect CRSF and will be changed at some point. Correct CRSF starts with 0xC8 always (
UART_SYNC
in the Crossfire units). Betaflight, iNav, ExpressLRS, EdgeTX and many other systems don't check the first byte or check it against a variety of values to validate the frame.Also note there's a perfectly good CRSF frame parser already in pulses/Crossfire.cpp so it would be nice to not duplicate the code, and with different behavior between them.
I think ideally ExpressLRS would love to be able to send CHANNELSPACKED/SUBSETPACKED on its telemetry connection as well for use as a trainer or to forward headtracking data directly from the TX module's backpack into EdgeTX.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That last part sounds interesting