diff --git a/src/ATM.ino b/src/ATM.ino index 658aae5..ae929e9 100644 --- a/src/ATM.ino +++ b/src/ATM.ino @@ -5,6 +5,8 @@ #include "commands/TuneMatch.h" #include "commands/Homing.h" #include "commands/SetVoltages.h" +#include "commands/MeasureReflection.h" + #define DEBUG @@ -17,6 +19,7 @@ FrequencySweep frequencySweep; TuneMatch tuneMatch; Homing homing; SetVoltages setVoltages; +MeasureReflection measureReflection; // Frequency Settings #define FREQUENCY_STEP 100000U // 100kHz frequency steps for initial frequency sweep @@ -53,10 +56,10 @@ void setup() commandManager.registerCommand('d', &tuneMatch); commandManager.registerCommand('h', &homing); commandManager.registerCommand('v', &setVoltages); + 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. @@ -149,22 +152,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);