Skip to content

Commit 4e47490

Browse files
committed
First part of the regulator GUI and some math
1 parent 4f77e62 commit 4e47490

31 files changed

+1057
-58
lines changed

Control/PIsection.cpp

Lines changed: 146 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,146 @@
1+
#include "PIsection.h"
2+
#include <QDebug>
3+
4+
PISection::PISection(QWidget *parent, int fs):
5+
QWidget(parent)
6+
{
7+
8+
topLayout = new QVBoxLayout(this);
9+
layout = new QBoxLayout(QBoxLayout::TopToBottom,this);
10+
groupBox = new QGroupBox(this);
11+
12+
knob_fc = new Knob(logScale , this);
13+
knob_fc-> setTitle("Fc [Hz]");
14+
knob_fc-> setKnobColor("rgb(255, 127, 127)");
15+
knob_fc->setRange( FMIN , fs, 100);
16+
knob_fc->setDecimals(0);
17+
knob_fc->setSingleStep(1);
18+
knob_fc->setValue(DEFAULT_FC);
19+
knob_gain = new Knob(linScale , this);
20+
knob_gain-> setTitle("Gain [dB]");
21+
knob_gain-> setKnobColor("rgb(127, 127, 255)");
22+
knob_gain->setRange(-20,10,60);
23+
knob_gain->setDecimals(1);
24+
knob_gain->setSingleStep(0.1);
25+
knob_gain->setValue(DEFAULT_GAIN);
26+
27+
layout->addWidget(knob_fc);
28+
layout->addWidget(knob_gain);
29+
layout->setMargin(0);
30+
layout->setSpacing(0);
31+
groupBox->setLayout(layout);
32+
groupBox->setFixedWidth(100);
33+
groupBox->setContentsMargins(0,0,0,0);
34+
QFont box_font = groupBox->font();
35+
box_font.setBold(false);
36+
groupBox->setFont(box_font);
37+
38+
//qDebug() << groupBox->sizeHint().height();
39+
groupBox->setCheckable(false);
40+
groupBox->setChecked(true);
41+
groupBox->setToolTip(tr("PI controller"));
42+
groupBox->setTitle("PI");
43+
44+
connect(knob_gain , SIGNAL(valueChanged(double)) , this , SLOT(slot_gainChanged(double)));
45+
connect(knob_fc , SIGNAL(valueChanged(double)) , this , SLOT(slot_fcChanged(double)));
46+
47+
48+
topLayout->addWidget(groupBox);
49+
topLayout->setContentsMargins(3,10,3,10);
50+
this->setLayout(topLayout);
51+
}
52+
53+
54+
void PISection::setSectionID(int newID){
55+
sectionID=newID;
56+
}
57+
58+
59+
void PISection::setBoxTitle(const QString & title){
60+
groupBox->setTitle(title);
61+
}
62+
63+
64+
double PISection::getFc(){
65+
return knob_fc->Value();
66+
}
67+
68+
void PISection::setFc(double Fc , bool blocked){
69+
if(blocked)
70+
knob_fc->blockSignals(true);
71+
72+
//if(knob_fc->isEnabled()==true)
73+
knob_fc->setValue(Fc);
74+
if(blocked)
75+
knob_fc->blockSignals(false);
76+
}
77+
78+
79+
double PISection::getGain(){
80+
return knob_gain->Value();
81+
}
82+
83+
void PISection::setGain(double gain , bool blocked){
84+
if(blocked)
85+
knob_gain->blockSignals(true);
86+
knob_gain->setValue(gain);
87+
if(blocked)
88+
knob_gain->blockSignals(false);
89+
}
90+
91+
bool PISection::getFilterActive(){
92+
return groupBox->isChecked();
93+
}
94+
95+
void PISection::setFilterActive(bool state , bool blocked){
96+
if(blocked)
97+
groupBox->blockSignals(true);
98+
groupBox->setChecked(state);
99+
if(blocked)
100+
groupBox->blockSignals(false);
101+
}
102+
103+
104+
void::PISection::updateSettingsAndPlot(bool updatePlot , int new_fs){
105+
fs = new_fs;
106+
EQ_section_t EQ;
107+
EQ.Fc = knob_fc->Value();
108+
EQ.Gain = knob_gain->Value();
109+
calc_PI(PIsettings , fs, B , A );
110+
// freqz(B ,A , freq);
111+
if(updatePlot)
112+
emit PIchanged(B,A); //Signal to parent its time to update plot
113+
}
114+
115+
116+
//SLOTS
117+
void PISection::slot_gainChanged(double gain){
118+
PIsettings.Gain = gain;
119+
calc_PI(PIsettings , fs, B , A );
120+
emit PIchanged(B,A);
121+
}
122+
123+
124+
void PISection::slot_fcChanged(double fc){
125+
PIsettings.Fc=fc;
126+
}
127+
128+
void PISection::slot_filtertypeChanged(int type){
129+
if(type == Notch || type == AllPass )
130+
knob_gain->setDisabled(true);
131+
else
132+
knob_gain->setDisabled(false);
133+
134+
}
135+
136+
137+
138+
void PISection::slot_activeEQChanged(bool state){
139+
//
140+
141+
}
142+
143+
PISection::~PISection()
144+
{
145+
}
146+

Control/PIsection.h

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
#ifndef PISECTION_H
2+
#define PISECTION_H
3+
4+
#include <complex>
5+
#include <QWidget>
6+
#include <QGroupBox>
7+
#include <QComboBox>
8+
#include <QCheckBox>
9+
#include "knob.h"
10+
#include "calcfilt.h"
11+
12+
#define DEFAULT_GAIN 0
13+
#define DEFAULT_FILTER PI
14+
15+
class PISection : public QWidget
16+
{
17+
Q_OBJECT
18+
public:
19+
explicit PISection(QWidget *parent = nullptr , int fs=0);
20+
~PISection();
21+
void setSectionID(int newID);
22+
void setBoxTitle(const QString &title);
23+
void updateSettingsAndPlot(bool updatePlot, int new_fs);
24+
25+
void updateLinked();
26+
double getFc();
27+
void setFc(double , bool);
28+
double getGain();
29+
void setGain(double , bool);
30+
bool getFilterActive();
31+
void setFilterActive(bool , bool);
32+
33+
34+
35+
36+
signals:
37+
void PIchanged(double B[2] , double A);
38+
39+
private slots:
40+
void slot_gainChanged(double gain);
41+
void slot_fcChanged(double fc);
42+
void slot_filtertypeChanged(int type);
43+
void slot_activeEQChanged(bool state);
44+
45+
private:
46+
quint16 *port;
47+
int channelID;
48+
int sectionID;
49+
QVBoxLayout *topLayout;
50+
QBoxLayout *layout;
51+
Knob *knob_fc;
52+
Knob *knob_gain;
53+
QGroupBox *groupBox;
54+
float fs;
55+
PI_section_t PIsettings={DEFAULT_FC , DEFAULT_GAIN};
56+
double B[2];
57+
double A;
58+
};
59+
60+
61+
62+
#endif // EQSECTION_H

Control/bodeplot.cpp

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
#include "bodeplot.h"
2+
#include "transform.h"
3+
#include "QDebug"
4+
5+
bodeplot::bodeplot(QWidget *parent) : QWidget(parent)
6+
{
7+
8+
chart = new QChart();
9+
chartview = new QChartView(chart);
10+
int len=256;
11+
freq.reserve(len);
12+
for(int i=0; i<len ; i++){
13+
double f = lin2log(i , len, 150, 0.001);
14+
//qDebug()<<f;
15+
bodeFlux.append(f , 0);
16+
bodeTorque.append(f , 0);
17+
}
18+
freqAxis.setMax(150);
19+
freqAxis.setMin(0.001);
20+
freqAxis.setMinorTickCount(8);
21+
freqAxis.setTitleText("Frequency [kHz]");
22+
freqAxis.setLabelFormat("%G");
23+
24+
levelAxis.setMin(-50);
25+
levelAxis.setMax(10);
26+
levelAxis.setTickCount(7);
27+
levelAxis.setMinorTickCount(1);
28+
levelAxis.setTitleText("Level [dB]");
29+
30+
bodeTorque.setName("Torque");
31+
bodeFlux.setName("Magnetic Flux");
32+
33+
chart->addSeries(&bodeTorque);
34+
chart->addSeries(&bodeFlux);
35+
36+
chart->addAxis(&freqAxis , Qt::AlignBottom);
37+
chart->addAxis(&levelAxis , Qt::AlignLeft);
38+
bodeTorque.attachAxis(&freqAxis);
39+
bodeTorque.attachAxis(&levelAxis);
40+
bodeFlux.attachAxis(&freqAxis);
41+
bodeFlux.attachAxis(&levelAxis);
42+
chartview->setChart(chart);
43+
chartview->setMinimumWidth(350);
44+
top_layout.addWidget(chartview);
45+
this->setLayout(&top_layout);
46+
47+
48+
49+
50+
51+
}

Control/bodeplot.h

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
#ifndef BODEPLOT_H
2+
#define BODEPLOT_H
3+
4+
#include <QWidget>
5+
#include <QtCharts>
6+
#include <QChartView>
7+
#include <QVector>
8+
9+
QT_CHARTS_USE_NAMESPACE
10+
11+
class bodeplot : public QWidget
12+
{
13+
Q_OBJECT
14+
public:
15+
explicit bodeplot(QWidget *parent = nullptr);
16+
17+
signals:
18+
19+
public slots:
20+
21+
private:
22+
QChart* chart;
23+
QChartView* chartview;
24+
QLineSeries bodeTorque;
25+
QLineSeries bodeFlux;
26+
QLogValueAxis freqAxis;
27+
QValueAxis levelAxis;
28+
QVector<float> freq;
29+
QVBoxLayout top_layout;
30+
31+
};
32+
33+
#endif // BODEPLOT_H

0 commit comments

Comments
 (0)