Advertisement
Valderman

modgensetup.cpp

Apr 21st, 2023
613
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 7.55 KB | Haiku | 0 0
  1. #include "modgensetup.h"
  2.  
  3. #if defined(TKPA_V1)
  4.     #include "cdeviceni5450.h"
  5.     #include "ni5450setup.h"
  6. #elif defined(TKPA_V2)
  7.     #include "cdevicemgqs.h"
  8.     #include "mgqssetup.h"
  9. #endif
  10.  
  11. #include "devicemanager.h"
  12. #include "globals.h"
  13. #include "test.h"
  14.  
  15. #include <QDebug>
  16. #include <QElapsedTimer>
  17.  
  18. /* -------------------------------------------------------------------------- */
  19. TestStepBasic* createModGenSetup (const std::vector<ModGenChannelSettings> &ch)
  20. {
  21. #if defined(TKPA_V1)
  22.  
  23.     std::vector<NI5450ChannelSettings> niChannelSettings;
  24.     foreach (const LowFreqGenChannelSettings &s, ch)
  25.     {
  26.         NI5450ChannelSettings niChannel(std::to_string(s.ch), s.amp, s.freq);
  27.         niChannelSettings.push_back(niChannel);
  28.     }
  29.     return new NI5450Setup(niChannelSettings);
  30.  
  31. #elif defined(TKPA_V2)
  32.  
  33.     std::vector<MGQSChannelSettings> mgqsChannelSettings;
  34.     foreach (const ModGenChannelSettings &s, ch)
  35.     {
  36.         MGQSChannelSettings channel(s.ch, s.amp, s.freq);
  37.         mgqsChannelSettings.push_back(channel);
  38.     }
  39.     return new MGQSSetup(mgqsChannelSettings);
  40.  
  41. #endif
  42. }
  43.  
  44. TestStepBasic* createModGenSetup2ch (double freq)
  45. {
  46.     std::vector<ModGenChannelSettings> ch{
  47.         {0, Globals::c_defaultModGenAmp, freq},
  48.         {1, Globals::c_defaultModGenAmp, freq}};
  49.     return createModGenSetup(ch);
  50. }
  51.  
  52. TestStepBasic* createModGenEnableOutput (const std::vector<unsigned short> &ch,
  53.                                          bool bEnable)
  54. {
  55. #if defined(TKPA_V1)
  56.  
  57.     std::vector<std::string> niChannels;
  58.     foreach (unsigned short c, ch)
  59.     {
  60.         niChannels.push_back(std::to_string(c));
  61.     }
  62.     return new NI5450SetOutputEnabled(niChannels, bEnable);
  63.  
  64. #elif defined(TKPA_V2)
  65.  
  66.     return new MGQSEnableOutput(ch, bEnable);
  67.  
  68. #endif
  69. }
  70.  
  71. TestStepBasic* createModGenAbortGeneration ()
  72. {
  73. #if defined(TKPA_V1)
  74.     return new NI5450AbortGeneration;
  75. #elif defined(TKPA_V2)
  76.     return new MGQSAbortGeneration;
  77. #endif
  78. }
  79.  
  80. TestStepBasic*
  81.     createModGenSetFrequency (const std::vector<ModGenChannelSettings> &ch)
  82. {
  83. #if defined(TKPA_V1)
  84.  
  85.     std::vector<std::string> niChannels;
  86.     foreach (const LowFreqGenChannelSettings &c, ch)
  87.     {
  88.         niChannels.push_back(std::to_string(c.ch));
  89.     }
  90.     return new NI5450SetFrequency(niChannels, freq);
  91.  
  92. #elif defined(TKPA_V2)
  93.  
  94.     std::vector<MGQSChannelSettings> channels;
  95.     foreach (const ModGenChannelSettings &c, ch)
  96.     {
  97.         MGQSChannelSettings mgqsChannel(c.ch, c.amp, c.freq);
  98.         channels.push_back(mgqsChannel);
  99.     }
  100.     return new MGQSSetFrequency(channels);
  101.  
  102. #endif
  103. }
  104.  
  105. /* -------------------------------------------------------------------------- */
  106. ModGenFrequencySweep::ModGenFrequencySweep(
  107.     const std::vector<FreqListItem> &freqList,
  108.     const std::vector<unsigned short> &ch,
  109.     double amp,
  110.     Test* parent)
  111.     : TestStep(parent)
  112.     , m_freqList(freqList)
  113.     , m_ch(ch)
  114.     , m_amp(amp)
  115. {
  116. #if defined(TKPA_V1)
  117.     m_devNum = c_NI5450DevNum;
  118. #elif defined(TKPA_V2)
  119.     m_devNum = c_mgqsDevNum;
  120. #endif
  121.  
  122.     Q_ASSERT(freqList.size() > 0);
  123.  
  124.     setName("Frequency sweep " + m_devNum);
  125.     double t(0.0);
  126.     foreach (const FreqListItem &f, m_freqList)
  127.     {
  128.         t += f.time;
  129.     }
  130.     m_time = t;
  131. }
  132.  
  133. bool ModGenFrequencySweep::setFreq(double freq)
  134. {
  135. #if defined(TKPA_V1)
  136.     CDeviceNI5450* dev = dynamic_cast<CDeviceNI5450*>(
  137.         DeviceManager::instance().deviceBySchemeNum(m_devNum).get());
  138.  
  139.     for (unsigned int i = 0; i < m_ch.size(); i++)
  140.     {
  141.         if (dev->setFreqAttribute(std::to_string(m_ch[i]), freq) != 0)
  142.         {
  143.             setErrorString(QString::fromStdString(dev->getLastError()));
  144.             return false;
  145.         }
  146.     }
  147. #elif defined(TKPA_V2)
  148.  
  149.     CDeviceMGQS* dev = dynamic_cast<CDeviceMGQS*>(
  150.         DeviceManager::instance().deviceBySchemeNum(m_devNum).get());
  151.  
  152.     for (unsigned int i = 0; i < m_ch.size(); i++)
  153.     {
  154.         CDeviceMGQS::GenParameters genParams(m_amp, freq, 0);
  155.         if (dev->configAfg(m_ch[i], genParams) != 0)
  156.         {
  157.             setErrorString(QString::fromStdString(dev->getLastError()));
  158.             return false;
  159.         }
  160.     }
  161.  
  162. #endif
  163.     return true;
  164. }
  165.  
  166. bool ModGenFrequencySweep::execute()
  167. {
  168.     // Текущая частота
  169.     double freq{0.0};
  170.     // Прошедшее время в секундах
  171.     double sec{0.0};
  172.  
  173.     // Индекс начального частотного интервала
  174.     unsigned int curIndex{0};
  175.     // Частотный интервал
  176.     FreqListItem fItem = m_freqList[curIndex];
  177.     // Индекс конечного частотного интервала
  178.     FreqListItem fLastItem = *std::prev(m_freqList.end());
  179.     // Изменение частоты между частотными точками в единицу времени
  180.     double df = (fItem.freqEnd - fItem.freqStart) / fItem.time;
  181.     // Время на начале частотного интервала
  182.     double startTime{0.0};
  183.     // Признак - установлена ли частота
  184.     bool bSet{false};
  185.  
  186.     qDebug() << "Total time = " << m_time;
  187.     qDebug() << "df =" << df;
  188.  
  189.     // Запускаем таймер
  190.     QElapsedTimer timer;
  191.     timer.start();
  192.  
  193.     int counter{0};
  194.  
  195.     // Цикл по всем частотным интервалам
  196.     do
  197.     {
  198.         // Расчёт текущей частоты
  199.         freq = fItem.freqStart + (sec - startTime) * df;
  200.  
  201.         if (!bSet)
  202.         {
  203.             // Выдать частоту на генераторе
  204.             if (!setFreq(freq))
  205.             {
  206.                 return false;
  207.             }
  208.  
  209.             qDebug().nospace().noquote()
  210.                 << ++counter << " freq = " << freq << ", msec  = " << sec;
  211.  
  212.             // Если изменеие частоты меньше опредленного значения
  213.             // признак выполнения частоты становится выполнился
  214.             if (df < 0.001)
  215.             {
  216.                 bSet = true;
  217.             }
  218.         }
  219.  
  220.         // Прошедшее время в секундах
  221.         sec = timer.nsecsElapsed() * 1e-6;
  222.  
  223.         // Проверка перехода на следующую частотный интервал по времени
  224.         if (sec > startTime + fItem.time)
  225.         {
  226.             if (curIndex == m_freqList.size() - 1)
  227.             {
  228.                 break;
  229.             }
  230.  
  231.             // Прибавить к начальному времени время перестройки на текущей
  232.             // частотной точке
  233.             startTime += fItem.time;
  234.             // Увеличить индекс частотного интервала
  235.             curIndex++;
  236.  
  237.             // Взять новый частотный интервал
  238.             fItem = m_freqList[curIndex];
  239.  
  240.             // Изменение частоты между частотными точками в единицу времени
  241.             df = (fItem.freqEnd - fItem.freqStart) / fItem.time;
  242.  
  243.             bSet = false;
  244.         }
  245.  
  246.         // Остновить если поступил сигнал Стоп
  247.         if (parent() && parent()->stopCondition())
  248.         {
  249.             break;
  250.         }
  251.  
  252.     } while (sec < m_time);
  253.  
  254.     qDebug().nospace().noquote()
  255.         << ++counter << " freq = " << freq << ", msec  = " << sec;
  256.     // Установить конечную частоту
  257.     setFreq(fLastItem.freqEnd);
  258.  
  259.     return true;
  260. }
  261.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement