BatteryMonitorConfig.cc 9.73 KB
Newer Older
1
#include "BatteryMonitorConfig.h"
2
#include <QMessageBox>
3

4
BatteryMonitorConfig::BatteryMonitorConfig(QWidget *parent) : AP2ConfigWidget(parent)
5 6
{
    ui.setupUi(this);
7 8 9
    ui.monitorComboBox->addItem(tr("0: Disabled"));
    ui.monitorComboBox->addItem(tr("3: Battery Volts"));
    ui.monitorComboBox->addItem(tr("4: Voltage and Current"));
10

11
    ui.sensorComboBox->addItem(tr("0: Other"));
12 13 14 15 16 17 18 19 20
    ui.sensorComboBox->addItem("1: AttoPilot 45A");
    ui.sensorComboBox->addItem("2: AttoPilot 90A");
    ui.sensorComboBox->addItem("3: AttoPilot 180A");
    ui.sensorComboBox->addItem("4: 3DR Power Module");

    ui.apmVerComboBox->addItem("0: APM1");
    ui.apmVerComboBox->addItem("1: APM2 - 2.5 non 3DR");
    ui.apmVerComboBox->addItem("2: APM2.5 - 3DR Power Module");
    ui.apmVerComboBox->addItem("3: PX4");
21 22

    ui.alertOnLowCheckBox->setVisible(false); //Unimpelemented, but TODO.
23

24 25 26 27 28 29 30 31 32

    connect(ui.monitorComboBox,SIGNAL(currentIndexChanged(int)),this,SLOT(monitorCurrentIndexChanged(int)));
    connect(ui.sensorComboBox,SIGNAL(currentIndexChanged(int)),this,SLOT(sensorCurrentIndexChanged(int)));
    connect(ui.apmVerComboBox,SIGNAL(currentIndexChanged(int)),this,SLOT(apmVerCurrentIndexChanged(int)));

    connect(ui.calcDividerLineEdit,SIGNAL(editingFinished()),this,SLOT(calcDividerSet()));
    connect(ui.ampsPerVoltsLineEdit,SIGNAL(editingFinished()),this,SLOT(ampsPerVoltSet()));
    connect(ui.battCapacityLineEdit,SIGNAL(editingFinished()),this,SLOT(batteryCapacitySet()));

33

34
    initConnections();
35
}
36 37
void BatteryMonitorConfig::activeUASSet(UASInterface *uas)
{
38 39 40 41 42
    if (m_uas)
    {
        disconnect(m_uas,SIGNAL(batteryChanged(UASInterface*,double,double,double,int)),this,SLOT(batteryChanged(UASInterface*,double,double,double,int)));
    }
    AP2ConfigWidget::activeUASSet(uas);
43 44 45 46 47
    if (!uas)
    {
        return;
    }
    connect(uas,SIGNAL(batteryChanged(UASInterface*,double,double,double,int)),this,SLOT(batteryChanged(UASInterface*,double,double,double,int)));
48

49
}
50 51
void BatteryMonitorConfig::alertOnLowClicked(bool checked)
{
52
    Q_UNUSED(checked);
53
}
54

55 56
void BatteryMonitorConfig::calcDividerSet()
{
57 58
    if (!m_uas)
    {
59
        showNullMAVErrorMessageBox();
60 61
        return;
    }
62 63 64 65 66 67 68 69
    bool ok = false;
    float newval = ui.calcDividerLineEdit->text().toFloat(&ok);
    if (!ok)
    {
        //Error parsing;
        QMessageBox::information(0,"Error","Invalid number entered for voltage divider. Please try again");
        return;
    }
70
    m_uas->getParamManager()->setParameter(1,"VOLT_DIVIDER",newval);
71 72 73
}
void BatteryMonitorConfig::ampsPerVoltSet()
{
74 75
    if (!m_uas)
    {
76
        showNullMAVErrorMessageBox();
77 78
        return;
    }
79 80 81 82 83 84 85 86
    bool ok = false;
    float newval = ui.ampsPerVoltsLineEdit->text().toFloat(&ok);
    if (!ok)
    {
        //Error parsing;
        QMessageBox::information(0,"Error","Invalid number entered for amps per volts. Please try again");
        return;
    }
87
    m_uas->getParamManager()->setParameter(1,"AMPS_PER_VOLT",newval);
88 89 90
}
void BatteryMonitorConfig::batteryCapacitySet()
{
91 92
    if (!m_uas)
    {
93
        showNullMAVErrorMessageBox();
94 95
        return;
    }
96 97 98 99 100 101 102 103
    bool ok = false;
    float newval = ui.battCapacityLineEdit->text().toFloat(&ok);
    if (!ok)
    {
        //Error parsing;
        QMessageBox::information(0,"Error","Invalid number entered for amps per volts. Please try again");
        return;
    }
104
    m_uas->getParamManager()->setParameter(1,"BATT_CAPACITY",newval);
105 106 107 108 109 110
}

void BatteryMonitorConfig::monitorCurrentIndexChanged(int index)
{
    if (!m_uas)
    {
111
        showNullMAVErrorMessageBox();
112 113
        return;
    }
114
    if (index == 0) //Battery Monitor Disabled
115
    {
116 117 118
        m_uas->getParamManager()->setParameter(1,"BATT_VOLT_PIN",-1);
        m_uas->getParamManager()->setParameter(1,"BATT_CURR_PIN",-1);
        m_uas->getParamManager()->setParameter(1,"BATT_MONITOR",0);
119 120 121 122 123 124 125 126
        ui.sensorComboBox->setEnabled(false);
        ui.apmVerComboBox->setEnabled(false);
        ui.measuredVoltsLineEdit->setEnabled(false);
        ui.measuredVoltsLineEdit->setEnabled(false);
        ui.calcDividerLineEdit->setEnabled(false);
        ui.calcVoltsLineEdit->setEnabled(false);
        ui.ampsPerVoltsLineEdit->setEnabled(false);
    }
127
    else if (index == 1) //Monitor voltage only
128
    {
129
        m_uas->getParamManager()->setParameter(1,"BATT_MONITOR",3);
130 131 132 133 134 135 136
        ui.sensorComboBox->setEnabled(false);
        ui.apmVerComboBox->setEnabled(true);
        ui.measuredVoltsLineEdit->setEnabled(true);
        ui.calcDividerLineEdit->setEnabled(true);
        ui.calcVoltsLineEdit->setEnabled(false);
        ui.ampsPerVoltsLineEdit->setEnabled(false);
    }
137
    else if (index == 2) //Monitor voltage and current
138
    {
139
        m_uas->getParamManager()->setParameter(1,"BATT_MONITOR",4);
140 141 142 143 144 145 146 147 148 149 150 151
        ui.sensorComboBox->setEnabled(true);
        ui.apmVerComboBox->setEnabled(true);
        ui.measuredVoltsLineEdit->setEnabled(true);
        ui.calcDividerLineEdit->setEnabled(true);
        ui.calcVoltsLineEdit->setEnabled(false);
        ui.ampsPerVoltsLineEdit->setEnabled(true);
    }


}
void BatteryMonitorConfig::sensorCurrentIndexChanged(int index)
{
152 153 154 155 156 157
    float maxvolt = 0.0f;
    float maxamps = 0.0f;
    float mvpervolt = 0.0f;
    float mvperamp = 0.0f;
    float topvolt = 0.0f;
    float topamps = 0.0f;
158 159 160

    if (index == 1)
    {
161
        //atto 45 see https://www.sparkfun.com/products/10643
162 163
        maxvolt = 13.6f;
        maxamps = 44.7f;
164 165 166
    }
    else if (index == 2)
    {
167
        //atto 90 see https://www.sparkfun.com/products/9028
168 169
        maxvolt = 51.8f;
        maxamps = 89.4f;
170 171 172
    }
    else if (index == 3)
    {
173
        //atto 180 see https://www.sparkfun.com/products/10644
174 175
        maxvolt = 51.8f;
        maxamps = 178.8f;
176 177 178 179
    }
    else if (index == 4)
    {
        //3dr
180 181
        maxvolt = 50.0f;
        maxamps = 90.0f;
182
    }
183 184
    mvpervolt = calculatemVPerVolt(3.3f, maxvolt);
    mvperamp = calculatemVPerAmp(3.3f, maxamps);
185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203
    if (index == 0)
    {
        //Other
        ui.ampsPerVoltsLineEdit->setEnabled(true);
        ui.calcDividerLineEdit->setEnabled(true);
        ui.measuredVoltsLineEdit->setEnabled(true);
    }
    else
    {
        topvolt = (maxvolt * mvpervolt) / 1000.0;
        topamps = (maxamps * mvperamp) / 1000.0;

        ui.calcDividerLineEdit->setText(QString::number(maxvolt/topvolt));
        ui.ampsPerVoltsLineEdit->setText(QString::number(maxamps / topamps));
        ui.ampsPerVoltsLineEdit->setEnabled(false);
        ui.calcDividerLineEdit->setEnabled(false);
        ui.measuredVoltsLineEdit->setEnabled(false);
    }
}
204 205 206 207 208 209 210 211 212
float BatteryMonitorConfig::calculatemVPerAmp(float maxvoltsout,float maxamps)
{
    return (1000.0 * (maxvoltsout/maxamps));
}

float BatteryMonitorConfig::calculatemVPerVolt(float maxvoltsout,float maxvolts)
{
    return (1000.0 * (maxvoltsout/maxvolts));
}
213 214 215

void BatteryMonitorConfig::apmVerCurrentIndexChanged(int index)
{
216 217
    if (!m_uas)
    {
218
        showNullMAVErrorMessageBox();
219 220 221 222
        return;
    }
    if (index == 0) //APM1
    {
223 224
        m_uas->getParamManager()->setParameter(1,"BATT_VOLT_PIN",0);
        m_uas->getParamManager()->setParameter(1,"BATT_CURR_PIN",1);
225 226 227
    }
    else if (index == 1) //APM2
    {
228 229
        m_uas->getParamManager()->setParameter(1,"BATT_VOLT_PIN",1);
        m_uas->getParamManager()->setParameter(1,"BATT_CURR_PIN",2);
230 231 232
    }
    else if (index == 2) //APM2.5
    {
233 234
        m_uas->getParamManager()->setParameter(1,"BATT_VOLT_PIN",13);
        m_uas->getParamManager()->setParameter(1,"BATT_CURR_PIN",12);
235 236 237
    }
    else if (index == 3) //PX4
    {
238 239 240
        m_uas->getParamManager()->setParameter(1,"BATT_VOLT_PIN",100);
        m_uas->getParamManager()->setParameter(1,"BATT_CURR_PIN",101);
        m_uas->getParamManager()->setParameter(1,"VOLT_DIVIDER",1);
241 242
        ui.calcDividerLineEdit->setText("1");
    }
243 244 245 246 247
}

BatteryMonitorConfig::~BatteryMonitorConfig()
{
}
248 249
void BatteryMonitorConfig::batteryChanged(UASInterface* uas, double voltage, double current, double percent, int seconds)
{
250 251 252 253 254
    Q_UNUSED(uas);
    Q_UNUSED(current);
    Q_UNUSED(percent);
    Q_UNUSED(seconds);
    
255 256 257 258 259 260
    ui.calcVoltsLineEdit->setText(QString::number(voltage,'f',2));
    if (ui.measuredVoltsLineEdit->text() == "")
    {
        ui.measuredVoltsLineEdit->setText(ui.calcVoltsLineEdit->text());
    }
}
261 262 263

void BatteryMonitorConfig::parameterChanged(int uas, int component, QString parameterName, QVariant value)
{
264 265 266
    Q_UNUSED(uas);
    Q_UNUSED(component);
    
267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320
    if (parameterName == "VOLT_DIVIDER")
    {
        ui.calcDividerLineEdit->setText(QString::number(value.toFloat(),'f',4));
    }
    else if (parameterName == "AMP_PER_VOLT")
    {
        ui.ampsPerVoltsLineEdit->setText(QString::number(value.toFloat(),'g',2));

    }
    else if (parameterName == "BATT_MONITOR")
    {
        if (value.toInt() == 0) //0: Disable
        {
            ui.monitorComboBox->setCurrentIndex(0);
        }
        else if (value.toInt() == 3) //3: Battery volts
        {
            ui.monitorComboBox->setCurrentIndex(1);
        }
        else if (value.toInt() == 4) //4: Voltage and Current
        {
            ui.monitorComboBox->setCurrentIndex(2);
        }

    }
    else if (parameterName == "BATT_CAPACITY")
    {
        ui.battCapacityLineEdit->setText(QString::number(value.toFloat()));
    }
    else if (parameterName == "BATT_VOLT_PIN")
    {
        int ivalue = value.toInt();
        if (ivalue == 0) //APM1
        {
            ui.apmVerComboBox->setCurrentIndex(0);
        }
        else if (ivalue == 1) //APM2
        {
            ui.apmVerComboBox->setCurrentIndex(1);
        }
        else if (ivalue == 13) //APM2.5
        {
            ui.apmVerComboBox->setCurrentIndex(2);
        }
        else if (ivalue == 100) //PX4
        {
            ui.apmVerComboBox->setCurrentIndex(3);
        }
    }
    else if (parameterName == "BATT_CURR_PIN")
    {
        //Unused at the moment, everything is off BATT_VOLT_PIN
    }
}