I'm trying to code model for calculating input rpms for two servo drives. The model consists of four equations with eight variables.
mean speed: v = (vd+vb)/ 2
slide to roll ratio: srr = (vd-vb)/ v
disc speed: vd = 2* pi* Rd* nd
ball speed: vb = 2* pi* Rb* nb
Inside application (QT gui), the model work like a table in which each cell represent variable. Some of the cells user fill with values and the rest is calculated based on the equations. The solution is done after each input - if user inserts "vd" and "vb", application solve "v" and "srr", then wait for the next input.
Now the model is solved by a bunch of ifs conditions defined for most likely possible combinations of inputs. This is however not a very elegant solution since the code is messy and hard to edit/extend.
Main problem/question is how to put these equations into a matrix and solve for every combination of inputs(input should be the definition of 4 variables).
//Class of the model
class Prediction : public QObject
{
Q_OBJECT
public:
Prediction(QObject* parent = nullptr);
PhysicalQuantity constant(double value);
//called to solve the model
void evaluate();
signals:
void callDataChanged();
private:
const double pi = 3.141592653589793;
int m_row;
PhysicalQuantity m_meanSpeed = PhysicalQuantity("v");
PhysicalQuantity m_ballRPM = PhysicalQuantity("bRPM");
PhysicalQuantity m_discRPM = PhysicalQuantity("dRPM");
PhysicalQuantity m_ballSpeed = PhysicalQuantity("vb");
PhysicalQuantity m_discSpeed = PhysicalQuantity("vd");
PhysicalQuantity m_slideToRoll = PhysicalQuantity("srr");
PhysicalQuantity m_discRadius = PhysicalQuantity("rd");
PhysicalQuantity m_ballRadius = PhysicalQuantity("rb");
};
//current solution
void Prediction::evaluate()
{
if(this->m_meanSpeed.isEntered() && this->m_slideToRoll.isEntered() && m_discRadius.isEntered() && m_ballRadius.isEntered())
{
m_discSpeed.computedValue(((m_slideToRoll * m_meanSpeed + 2 * m_meanSpeed)/m_slideToRoll).value());
m_ballSpeed.computedValue((2*m_meanSpeed - m_discSpeed).value());
m_discRPM.computedValue((m_discSpeed / (2 * pi * m_discRadius)).value());
m_ballRPM.computedValue((m_ballSpeed / (2 * pi * m_ballRadius)).value());
}
...
};
//class of the variables in the model
class PhysicalQuantity
{
public:
PhysicalQuantity();
PhysicalQuantity(QString id);
PhysicalQuantity(double value);
PhysicalQuantity(const PhysicalQuantity &obj);
//read functions
bool isEntered() const;
bool isConstant() const;
double value() const;
double matInt() const;
QString name() const;
//write functions
bool setEntered(bool isEnetered);
QString name(QString id);
double value(double value);
double computedValue(double value);
//Operators
PhysicalQuantity operator+(const PhysicalQuantity &obj);
PhysicalQuantity operator-(const PhysicalQuantity &obj);
PhysicalQuantity operator/(const PhysicalQuantity &obj);
PhysicalQuantity operator=(const PhysicalQuantity &obj);
PhysicalQuantity operator-();
protected:
bool m_isEntered;
bool m_isConstant;
bool m_isComputed;
double m_value = 1234;
double m_defaultVal;
QString m_id;