From affbb61284d2ed469ac926e6f9a0870ca29ae3e5 Mon Sep 17 00:00:00 2001 From: jupfi Date: Wed, 16 Aug 2023 12:13:28 +0200 Subject: [PATCH] Implemented MeasureReflection command. --- src/ATM.ino | 20 ++------------ src/commands/MeasureReflection.cpp | 44 ++++++++++++++++++++++++++++++ src/commands/MeasureReflection.h | 21 ++++++++++++++ src/commands/TuneMatch.cpp | 24 ++++++++-------- 4 files changed, 80 insertions(+), 29 deletions(-) create mode 100644 src/commands/MeasureReflection.cpp create mode 100644 src/commands/MeasureReflection.h diff --git a/src/ATM.ino b/src/ATM.ino index 3146d1f..3242ccc 100644 --- a/src/ATM.ino +++ b/src/ATM.ino @@ -4,6 +4,7 @@ #include "commands/FrequencySweep.h" #include "commands/TuneMatch.h" #include "commands/Homing.h" +#include "commands/MeasureReflection.h" #define DEBUG @@ -15,6 +16,7 @@ CommandManager commandManager; FrequencySweep frequencySweep; TuneMatch tuneMatch; Homing homing; +MeasureReflection measureReflection; // Frequency Settings #define FREQUENCY_STEP 100000U // 100kHz frequency steps for initial frequency sweep @@ -48,10 +50,10 @@ void setup() commandManager.registerCommand('f', &frequencySweep); commandManager.registerCommand('d', &tuneMatch); commandManager.registerCommand('h', &homing); + commandManager.registerCommand('m', &measureReflection); pinMode(MISO_PIN, INPUT_PULLUP); // Seems to be necessary for SPI to work - // Setup fo the tuning stepper tuner.DRIVER.begin(); // Initiate pins tuner.DRIVER.rms_current(400); // Set stepper current to 400mA. @@ -142,22 +144,6 @@ void loop() // approximate call // CAREFULL -> if the coil has no proper matching in the frequency range this will not work! Only use this for testing -> otherwise use the automated 'decide' call. /*if (command == 'a') - else if (command == 'r') - { - float frequency_MHz = input_line.substring(1).toFloat(); - uint32_t frequency = validateInput(frequency_MHz); - if (frequency == 0) - return; - - float reflection_loss = calculateRL(frequency); - - printInfo("For frequency:"); - printInfo(frequency); - printInfo("RL is:"); - printInfo(reflection_loss); - - // optimize Matching - } else if (command == 'm') { printInfo("Optimize Matching around frequency:"); diff --git a/src/commands/MeasureReflection.cpp b/src/commands/MeasureReflection.cpp new file mode 100644 index 0000000..0d22cf1 --- /dev/null +++ b/src/commands/MeasureReflection.cpp @@ -0,0 +1,44 @@ +#include "Utilities.h" +#include "MeasureReflection.h" + +void MeasureReflection::execute(String input_line) +{ + int AVERAGES = 16; + return_loss = 0; + phase = 0; + + printInfo("Measure Reflection"); + // Get the float after the r character which is the frequency value where the reflection measurment should be performed + float frequency_MHz = input_line.substring(1).toFloat(); + uint32_t frequency = validateInput(frequency_MHz); + if (frequency == 0) + { + printInfo("Invalid frequency input."); + return; + } + + // First set the frequency + setFrequency(frequency); + + // Measure the reflection at the given frequency + return_loss = readReflection(AVERAGES); + phase = readPhase(AVERAGES); +} + +void MeasureReflection::printResult() +{ + // Print the results which are then read by the autotm module + char identifier = 'r'; + char delimiter = 'p'; + String text = String(identifier) + String(return_loss) + String(delimiter) + String(phase); + + Serial.println(text); +} + +void MeasureReflection::printHelp() +{ + Serial.println("Measure reflection command"); + Serial.println("Syntax: r"); + Serial.println("Example: r100"); + Serial.println("This will measure the reflection at 100 MHz"); +} \ No newline at end of file diff --git a/src/commands/MeasureReflection.h b/src/commands/MeasureReflection.h new file mode 100644 index 0000000..c06b7d4 --- /dev/null +++ b/src/commands/MeasureReflection.h @@ -0,0 +1,21 @@ +#ifndef MEASUREREFLECTION_H +#define MEASUREREFLECTION_H + +#include "Command.h" + +/** + * @brief This class is used to perform a frequency sweep + */ +class MeasureReflection : public Command +{ +public: + void execute(String input_line) override; + void printResult() override; + void printHelp() override; + +private: + int return_loss; + int phase; +}; + +#endif \ No newline at end of file diff --git a/src/commands/TuneMatch.cpp b/src/commands/TuneMatch.cpp index 38988a0..b868d4f 100644 --- a/src/commands/TuneMatch.cpp +++ b/src/commands/TuneMatch.cpp @@ -1,22 +1,22 @@ #include "Utilities.h" #include "TuneMatch.h" -void TuneMatch::execute(String input_line){ - float target_frequency_MHz = input_line.substring(1).toFloat(); - uint32_t target_frequency = validateInput(target_frequency_MHz); - if (target_frequency == 0) - return; +void TuneMatch::execute(String input_line) +{ + float target_frequency_MHz = input_line.substring(1).toFloat(); + uint32_t target_frequency = validateInput(target_frequency_MHz); + if (target_frequency == 0) + return; - uint32_t startf = 35000000U; - uint32_t stopf = 110000000U; - uint32_t stepf = 100000U; - printInfo("Tuning and Matching to target frequency in MHz (automatic mode):"); - printInfo(target_frequency_MHz); + uint32_t startf = 35000000U; + uint32_t stopf = 110000000U; + uint32_t stepf = 100000U; + printInfo("Tuning and Matching to target frequency in MHz (automatic mode):"); + printInfo(target_frequency_MHz); - uint32_t resonance_frequency = automaticTM(target_frequency, startf, stopf, stepf); + uint32_t resonance_frequency = automaticTM(target_frequency, startf, stopf, stepf); } - uint32_t TuneMatch::automaticTM(uint32_t target_frequency, uint32_t start_frequency, uint32_t stop_frequency, uint32_t frequency_step) { uint32_t resonance_frequency = findCurrentResonanceFrequency(start_frequency, stop_frequency, frequency_step);