Page Menu
Home
c4science
Search
Configure Global Search
Log In
Files
F120644093
mainwindow.cpp
No One
Temporary
Actions
Download File
Edit File
Delete File
View Transforms
Subscribe
Mute Notifications
Award Token
Subscribers
None
File Metadata
Details
File Info
Storage
Attached
Created
Sat, Jul 5, 21:47
Size
3 KB
Mime Type
text/x-c
Expires
Mon, Jul 7, 21:47 (1 d, 23 h)
Engine
blob
Format
Raw Data
Handle
27199256
Attached To
R2671 HHRI-software
mainwindow.cpp
View Options
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QMessageBox>
#include <QDebug>
#include <QInputDialog>
#include <QList>
#include "hriboard.h"
/**
* @brief Constructor
* Setups the GUI, and try to connect to the HRI board.
* @param parent parent of this widget.
*/
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()));
}
/**
* @brief Destructor.
*/
MainWindow::~MainWindow()
{
delete ui;
}
/**
* @brief Displays the SyncVars list, and starts the streaming.
* @param syncVars SyncVars list.
* @remark This slot function is called automatically by the HriBoard object, as
* soon as the list is received from the board.
*/
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 relevant 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});
}
/**
* @brief Updates the GUI indicators with the updated SyncVars values.
* @remark This slot function is called automatically by the HriBoard object,
* every time a streaming packet is received.
*/
void MainWindow::onVarsUpdated()
{
// Update the UI widgets.
ui->hallVoltageBar->setValue((int)(*hallVoltage/3.3f*100.0f));
}
/**
* @brief Sets the motor torque, from the GUI control.
*/
void MainWindow::setMotorTorque()
{
// Get the UI control value, and affect it to the SyncVar.
*motorTorque = (float)ui->torqueSpin->value();
motorTorque.writeValue();
}
Event Timeline
Log In to Comment