From 677334773924937e626284e5b42290ded2aeb33f Mon Sep 17 00:00:00 2001 From: Chris Esposito Date: Mon, 28 Jan 2019 13:39:48 +1100 Subject: [PATCH] Edge detection --- Desktop_Interface/desktop_settings.h | 1 - Desktop_Interface/isobuffer.cpp | 43 ++++++++++++++++++++++--- Desktop_Interface/isobuffer.h | 22 ++++++++++++- Desktop_Interface/isodriver.cpp | 48 +++++++++++++++++++++++++++- Desktop_Interface/isodriver.h | 11 ++----- 5 files changed, 109 insertions(+), 16 deletions(-) diff --git a/Desktop_Interface/desktop_settings.h b/Desktop_Interface/desktop_settings.h index 1c34ec5e..5b8ad610 100644 --- a/Desktop_Interface/desktop_settings.h +++ b/Desktop_Interface/desktop_settings.h @@ -17,7 +17,6 @@ extern int TIMER_PERIOD; extern int ISO_RECOVERY_TIME; extern int MAX_WINDOW_SIZE; extern int TICK_SEPARATION; -#define TRIGGER_COUNT_THRESH (7 + std::max(0, log10(window))) //Is this the right number? //Multimeter settings extern int MULTIMETER_PERIOD; diff --git a/Desktop_Interface/isobuffer.cpp b/Desktop_Interface/isobuffer.cpp index bd0946b5..adac9c93 100644 --- a/Desktop_Interface/isobuffer.cpp +++ b/Desktop_Interface/isobuffer.cpp @@ -23,6 +23,7 @@ namespace #endif constexpr auto kTopMultimeter = 2048; + constexpr double kTriggerSensitivityMultiplier = 4; } isoBuffer::isoBuffer(QWidget* parent, int bufferLen, isoDriver* caller, unsigned char channel_value) @@ -41,24 +42,26 @@ void isoBuffer::insertIntoBuffer(short item) { m_buffer[m_back] = item; m_buffer[m_back+m_bufferEnd+1] = item; - m_back++; - m_insertedCount++; + m_back++; + m_insertedCount++; if (m_insertedCount > m_bufferEnd) { m_insertedCount = m_bufferEnd+1; } - if (m_back > m_bufferEnd) + if (m_back > m_bufferEnd) { m_back = 0; } + + checkTriggered(); } short isoBuffer::bufferAt(int idx) const { // NOTE: this is only correct if idx < m_insertedCount - return m_buffer[m_back + (m_bufferEnd+1) - idx]; + return m_buffer[(m_back-1) + (m_bufferEnd+1) - idx]; } void isoBuffer::outputSampleToFile(double averageSample) @@ -336,3 +339,35 @@ void isoBuffer::serialManage(double baudRate, UartParity parity) m_decoder->serialDecode(baudRate); } +void isoBuffer::setTriggerType(TriggerType newType) +{ + qDebug() << "Trigger Type: " << (uint8_t)newType; + m_triggerType = newType; +} + +void isoBuffer::setTriggerLevel(double voltageLevel, uint16_t top, bool acCoupled) +{ + m_triggerLevel = inverseSampleConvert(voltageLevel, top, acCoupled); + m_triggerSensitivity = static_cast(1 + abs(voltageLevel * kTriggerSensitivityMultiplier * static_cast(top) / 128.)); + qDebug() << "Trigger Level: " << m_triggerLevel; + qDebug() << "Trigger sensitivity:" << m_triggerSensitivity; +} + +// TODO: Clear trigger +// FIXME: AC changes will not be reflected here +void isoBuffer::checkTriggered() +{ + if (m_triggerType == TriggerType::Disabled) + return; + + if ((bufferAt(0) >= (m_triggerLevel + m_triggerSensitivity)) && (m_triggerSeekState == TriggerSeekState::BelowTriggerLevel)) + { + // Rising Edge + m_triggerSeekState = TriggerSeekState::AboveTriggerLevel; + } + else if ((bufferAt(0) < (m_triggerLevel - m_triggerSensitivity)) && (m_triggerSeekState == TriggerSeekState::AboveTriggerLevel)) + { + // Falling Edge + m_triggerSeekState = TriggerSeekState::BelowTriggerLevel; + } +} diff --git a/Desktop_Interface/isobuffer.h b/Desktop_Interface/isobuffer.h index a2c555ae..9369db3e 100644 --- a/Desktop_Interface/isobuffer.h +++ b/Desktop_Interface/isobuffer.h @@ -21,6 +21,19 @@ class isoDriver; class uartStyleDecoder; enum class UartParity : uint8_t; +enum class TriggerType : uint8_t +{ + Disabled, + Rising, + Falling +}; + +enum class TriggerSeekState : uint8_t +{ + BelowTriggerLevel, + AboveTriggerLevel +}; + // isoBuffer is a generic class that enables O(1) read times (!!!) on all // read/write operations, while maintaining a huge buffer size. // Imagine it as a circular buffer, but with access functions specifically @@ -64,11 +77,14 @@ public: private: template int capSample(int offset, int target, double seconds, double value, Function comp); + void checkTriggered(); public: int cap_x0fromLast(double seconds, double vbot); int cap_x1fromLast(double seconds, int x0, double vbot); int cap_x2fromLast(double seconds, int x1, double vtop); void serialManage(double baudRate, UartParity parity); + void setTriggerType(TriggerType newType); + void setTriggerLevel(double voltageLevel, uint16_t top, bool acCoupled); // ---- MEMBER VARIABLES ---- @@ -91,7 +107,11 @@ public: double m_frontendGain = (R4 / (R3 + R4)); int m_samplesPerSecond; int m_sampleRate_bit; - + TriggerType m_triggerType = TriggerType::Disabled; + TriggerSeekState m_triggerSeekState = TriggerSeekState::BelowTriggerLevel; + short m_triggerLevel = 0; + short m_triggerSensitivity = 0; + std::vector m_triggerPositionList = {}; // UARTS decoding uartStyleDecoder* m_decoder = NULL; bool m_isDecoding = true; diff --git a/Desktop_Interface/isodriver.cpp b/Desktop_Interface/isodriver.cpp index 63f4fd46..02f1793e 100644 --- a/Desktop_Interface/isodriver.cpp +++ b/Desktop_Interface/isodriver.cpp @@ -624,20 +624,27 @@ short isoDriver::reverseFrontEnd(double voltage){ void isoDriver::setTriggerEnabled(bool enabled) { triggerEnabled = enabled; + triggerStateChanged(); } void isoDriver::setTriggerLevel(double level) { + internalBuffer375_CH1->setTriggerLevel(level, 128, AC_CH1); + internalBuffer375_CH2->setTriggerLevel(level, 128, AC_CH2); + internalBuffer750->setTriggerLevel(level, 2048, AC_CH1); + triggerStateChanged(); } void isoDriver::setSingleShotEnabled(bool enabled) { singleShotEnabled = enabled; + triggerStateChanged(); } void isoDriver::setTriggerMode(int newMode) { - triggerMode = (TriggerType)newMode; + triggerMode = newMode; + triggerStateChanged(); } void isoDriver::frameActionGeneric(char CH1_mode, char CH2_mode) //0 for off, 1 for ana, 2 for dig, -1 for ana750, -2 for file @@ -1403,3 +1410,42 @@ void isoDriver::hideCH2(bool enable) { axes->graph(1)->setVisible(!enable); } + +void isoDriver::triggerStateChanged() +{ + qDebug() << "triggerStateChanged()"; + switch(triggerMode) + { + case 0: + { + internalBuffer375_CH1->setTriggerType(TriggerType::Rising); + internalBuffer375_CH2->setTriggerType(TriggerType::Disabled); + internalBuffer750->setTriggerType(TriggerType::Rising); + break; + } + case 1: + { + internalBuffer375_CH1->setTriggerType(TriggerType::Falling); + internalBuffer375_CH2->setTriggerType(TriggerType::Disabled); + internalBuffer750->setTriggerType(TriggerType::Falling); + break; + } + case 2: + { + internalBuffer375_CH1->setTriggerType(TriggerType::Disabled); + internalBuffer375_CH2->setTriggerType(TriggerType::Rising); + internalBuffer750->setTriggerType(TriggerType::Disabled); + break; + + } + case 3: + { + internalBuffer375_CH1->setTriggerType(TriggerType::Disabled); + internalBuffer375_CH2->setTriggerType(TriggerType::Falling); + internalBuffer750->setTriggerType(TriggerType::Disabled); + break; + } + } + +} + diff --git a/Desktop_Interface/isodriver.h b/Desktop_Interface/isodriver.h index 4524bea4..81e27ea1 100644 --- a/Desktop_Interface/isodriver.h +++ b/Desktop_Interface/isodriver.h @@ -56,14 +56,6 @@ public: bool fileModeEnabled = false; double daq_maxWindowSize; private: - enum class TriggerType : int - { - rising_ch1 = 0, - falling_ch1 = 1, - rising_ch2 = 2, - falling_ch2 = 3 - }; - //Those bloody bools that just Enable/Disable a single property bool paused_CH1 = false, paused_CH2 = false, paused_multimeter = false; bool autoGainEnabled = true; @@ -99,6 +91,7 @@ private: void multimeterAction(); void broadcastStats(bool CH2); void frameActionGeneric(char CH1_mode, char CH2_mode); + void triggerStateChanged(); //Variables that are just pointers to other classes/vars QCustomPlot *axes; std::unique_ptr readData375_CH1; @@ -110,11 +103,11 @@ private: siprint *v0, *v1, *dv, *t0, *t1, *dt, *f; //Scope/MM++ related variables double currentVmax, currentVmin, currentVRMS; - TriggerType triggerMode; double multi = 0; double xmin = 20, xmax = -20, ymin = 20, ymax = -20; double estimated_resistance = 0; int multimeterRsource = 0; + int triggerMode = 0; //Pure MM++ related variables enum multimeterType_enum {V = 0, I = 1, R = 2, C = 3}; multimeterType_enum multimeterType = V;