Skip to content
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

jitter reduction #4

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions src/CRSF.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ void ICACHE_RAM_ATTR CRSF::handleUARTin() //RTOS task to read and write CRSF pac
{
volatile uint8_t *SerialInBuffer = CRSF::inBuffer.asUint8_t;

if (this->_dev->available())
while (this->_dev->available())
{
char inChar = this->_dev->read();

Expand Down Expand Up @@ -116,6 +116,8 @@ void ICACHE_RAM_ATTR CRSF::handleUARTin() //RTOS task to read and write CRSF pac

bool ICACHE_RAM_ATTR CRSF::ProcessPacket()
{
uint32_t now = micros();

if (CRSFstate == false)
{
CRSFstate = true;
Expand All @@ -127,7 +129,7 @@ bool ICACHE_RAM_ATTR CRSF::ProcessPacket()

if (packetType == CRSF_FRAMETYPE_RC_CHANNELS_PACKED)
{
CRSF::RCdataLastRecv = micros();
CRSF::RCdataLastRecv = now;
GetChannelDataIn();
(RCdataCallback)(); // run new RC data callback
return true;
Expand All @@ -143,4 +145,5 @@ void ICACHE_RAM_ATTR CRSF::GetChannelDataIn() // data is packed as 11 bits per c
ChannelDataIn[1] = (rcChannels->ch1);
ChannelDataIn[2] = (rcChannels->ch2);
ChannelDataIn[3] = (rcChannels->ch3);
ChannelDataIn[4] = (rcChannels->ch4); // Arm aux is sent every packet
}
1 change: 1 addition & 0 deletions src/CRSF.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
#define PACKED __attribute__((packed))

#define CRSF_RX_BAUDRATE 420000
// #define CRSF_RX_BAUDRATE 1000000 // Required for testing 1kHz
#define CRSF_OPENTX_FAST_BAUDRATE 400000
#define CRSF_OPENTX_SLOW_BAUDRATE 115200 // Used for QX7 not supporting 400kbps
#define CRSF_NUM_CHANNELS 16
Expand Down
69 changes: 38 additions & 31 deletions src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ auto &testSerial = Serial;
//#define USE_IBUS
////#define USE_SRXL2

// #define USE_ARM_AUX

bool testRunning = false;
#define NumOfTests 500
uint32_t testCount = 0;
Expand Down Expand Up @@ -82,8 +84,7 @@ void userProvidedHandleVtxData(SrxlVtxData *pVtxData)

#endif

#define TRIGGER_WAIT_RAND_MIN_MS 200000
#define TRIGGER_WAIR_RAND_MAX_MS 350000
uint32_t packetInterval = 350000;
uint32_t TriggerBeginTime;

#define GPIO_OUTPUT_PIN D0
Expand Down Expand Up @@ -114,10 +115,15 @@ double ICACHE_RAM_ATTR average(uint32_t *array, uint32_t len)

void ICACHE_RAM_ATTR RCcallback(volatile uint16_t *data)
{
uint32_t now = micros();
uint32_t now = CRSF::RCdataLastRecv;

if (CurrState == 2)
{
#ifdef USE_ARM_AUX
if (data[4] > 1000)
#else
if (data[2] > 1000)
#endif
{
digitalWrite(D0, LOW);
CurrState = 0;
Expand All @@ -131,18 +137,15 @@ void ICACHE_RAM_ATTR RCcallback(volatile uint16_t *data)
usbSerial.print(",");
usbSerial.print(result);
usbSerial.print(",");
usbSerial.println(now - lastRCdataMicros);
packetInterval = now - lastRCdataMicros;
usbSerial.println(packetInterval);
}

if (testCount >= NumOfTests && testRunning)
{
testRunning = false;
testCount = 0;
usbSerial.println("===== FINISHED =====");
usbSerial.print("AVERAGE: ");
usbSerial.print(average(latencyResult, NumOfTests));
usbSerial.println(" microSeconds");
clear_array(latencyResult, sizeof(latencyResult));
}
else
{
Expand All @@ -166,7 +169,7 @@ void inline CRSF_GHST_RC_CALLBACK()

void inline PreTrigger()
{
TriggerBeginTime = random(TRIGGER_WAIT_RAND_MIN_MS, TRIGGER_WAIR_RAND_MAX_MS) + micros();
TriggerBeginTime = random(1.5 * packetInterval, 3.5 * packetInterval) + micros();
CurrState = 1;
}

Expand All @@ -177,6 +180,19 @@ void inline DoTrigger()
CurrState = 2;
}

// Could also be put on a pin ISR. But hitting the rst button is also easy.
void startTest()
{
usbSerial.println("Begin Test");
usbSerial.print("Test will run:");
usbSerial.print(NumOfTests);
usbSerial.println(" times");
testRunning = true;
testCount = 0;
clear_array(latencyResult, NumOfTests);
usbSerial.println("===== BEGIN =====");
}

void setup()
{
pinMode(D0, OUTPUT);
Expand Down Expand Up @@ -205,7 +221,8 @@ void setup()
usbSerial.begin(19200, SWSERIAL_8N1, 3, 1, false, 256);
usbSerial.enableIntTx(false);
usbSerial.println("Softserial Mon Started");
usbSerial.println("Press Button to Begin Test");

startTest();
}

#ifdef USE_SBUS
Expand Down Expand Up @@ -267,18 +284,7 @@ void loop_srxl2()
#endif

void loop()
{
if (CurrState == 0)
{
PreTrigger();
}
else if (CurrState == 1)
{
if (micros() > TriggerBeginTime)
{
DoTrigger();
}
}
{

#if defined(USE_CRSF)
crsf.handleUARTin();
Expand All @@ -292,17 +298,18 @@ void loop()
ibus.loop();
#endif

if (digitalRead(0) == 0)
if (CurrState < 2)
{
if (CurrState == 0 && testRunning)
{
usbSerial.println("Begin Test");
usbSerial.print("Test will run:");
usbSerial.print(NumOfTests);
usbSerial.println(" times");
testRunning = true;
testCount = 0;
clear_array(latencyResult, NumOfTests);
usbSerial.println("===== BEGIN =====");
PreTrigger();
}
else if (CurrState == 1)
{
if (micros() > TriggerBeginTime)
{
DoTrigger();
}
}
}
}