Page MenuHomec4science

mainwindow.cpp
No OneTemporary

File Metadata

Created
Tue, May 7, 08:06

mainwindow.cpp

#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QMessageBox>
#include <QDebug>
#include <QInputDialog>
#include <QList>
#include "hriboard.h"
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
//
ui->setupUi(this);
// Connect the GUI signals to the slots.
connect(ui->torqueSpin, SIGNAL(editingFinished()),
this, SLOT(setMotorTorque()));
// Ask for the COM port.
QString comPortName;
QStringList ports = HriBoard::getComPorts();
if(ports.size() == 0)
{
QMessageBox::critical(this, qApp->applicationName(), "No COM port.");
exit(0);
}
else if(ports.size() == 1)
comPortName = ports.first();
else
{
bool ok;
QString portChoice = QInputDialog::getItem(this, "HRI2_PC_Controller",
"Serial port:", ports, 0,
false, &ok);
int portIndex = ports.indexOf(portChoice);
if(portIndex == -1 || !ok)
exit(0);
comPortName = ports[portIndex];
}
// Establish the link with the HRI board.
hriBoard.openLink(comPortName);
connect(&hriBoard, SIGNAL(syncVarsListReceived(QList<SyncVar>)),
this, SLOT(onVarsListReceived(QList<SyncVar>)));
connect(&hriBoard, SIGNAL(syncVarsUpdated()), this, SLOT(onVarsUpdated()));
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::onVarsListReceived(const QList<SyncVar> &syncVars)
{
// Display the list of all the SyncVars.
qDebug() << "SyncVars list:";
for(const SyncVar &sv : syncVars)
qDebug() << sv.getName();
// Get the variables from the list.
hriBoard.associate(hallVoltage, "hall_voltage [V]");
hriBoard.associate(motorTorque, "motor_torque [N.m]");
if(!hallVoltage.isValid() || !motorTorque.isValid())
{
qDebug() << "The expected SyncVars were not found in the list." << endl;
return;
}
// Start the streaming.
hriBoard.setStreamedVars({&hallVoltage, &motorTorque});
}
void MainWindow::onVarsUpdated()
{
// Update the UI widgets.
ui->hallVoltageBar->setValue((int)(*hallVoltage/3.3f*100.0f));
}
void MainWindow::setMotorTorque()
{
// Get the UI control value, and affect it to the SyncVar.
*motorTorque = (float)ui->torqueSpin->value();
motorTorque.writeValue();
}

Event Timeline