Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Stop the robots when the stop receiving packages for 1 sec #168

Merged
merged 4 commits into from
Jan 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions include/sslworld.h
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,10 @@ class SSLWorld : public QObject
QUdpSocket *simControlSocket;
QUdpSocket *blueControlSocket;
QUdpSocket *yellowControlSocket;

QElapsedTimer elapsedLastPackageBlue;
QElapsedTimer elapsedLastPackageYellow;

bool updatedCursor;
Robot* robots[MAX_ROBOT_COUNT*2]{};
int sendGeomCount;
Expand Down
32 changes: 31 additions & 1 deletion src/mainwindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@
QObject::connect(simControlSocket,SIGNAL(readyRead()),this,SLOT(simControlSocketReady()));
QObject::connect(blueControlSocket,SIGNAL(readyRead()),this,SLOT(blueControlSocketReady()));
QObject::connect(yellowControlSocket,SIGNAL(readyRead()),this,SLOT(yellowControlSocketReady()));

glwidget->ssl->visionServer = visionServer;
glwidget->ssl->commandSocket = commandSocket;
glwidget->ssl->blueStatusSocket = blueStatusSocket;
Expand Down Expand Up @@ -362,56 +362,86 @@
return s;
}

void MainWindow::update()
{
if (glwidget->ssl->isGLEnabled) {
glwidget->ssl->g->enableGraphics();
glwidget->updateGL();
} else {
glwidget->ssl->g->disableGraphics();
glwidget->step();
}

int R = robotIndex(glwidget->Current_robot,glwidget->Current_team);

if(0 <= R)
{
const dReal* vv = dBodyGetLinearVel(glwidget->ssl->robots[R]->chassis->body);
static dVector3 lvv;
dVector3 aa;
aa[0]=(vv[0]-lvv[0])/configwidget->DeltaTime();
aa[1]=(vv[1]-lvv[1])/configwidget->DeltaTime();
aa[2]=(vv[2]-lvv[2])/configwidget->DeltaTime();
robotwidget->vellabel->setText(QString::number(sqrt(vv[0]*vv[0]+vv[1]*vv[1]+vv[2]*vv[2]),'f',3));
robotwidget->acclabel->setText(QString::number(sqrt(aa[0]*aa[0]+aa[1]*aa[1]+aa[2]*aa[2]),'f',3));
lvv[0]=vv[0];
lvv[1]=vv[1];
lvv[2]=vv[2];
}

fpslabel->setText(QString("Frame rate: %1 fps").arg(QString::asprintf("%06.2f",glwidget->getFPS())));
if (glwidget->ssl->selected!=-1)
{
selectinglabel->setVisible(true);
if (glwidget->ssl->selected==-2)
{
selectinglabel->setText("Ball");
}
else
{
int R = glwidget->ssl->selected%configwidget->Robots_Count();
int T = glwidget->ssl->selected/configwidget->Robots_Count();
if (T==0) selectinglabel->setText(QString("%1:Blue").arg(R));
else selectinglabel->setText(QString("%1:Yellow").arg(R));
}
}
else selectinglabel->setVisible(false);
vanishlabel->setVisible(configwidget->vanishing());
noiselabel->setVisible(configwidget->noise());
cursorlabel->setText(QString("Cursor: [X=%1;Y=%2;Z=%3]").arg(dRealToStr(glwidget->ssl->cursor_x)).arg(dRealToStr(glwidget->ssl->cursor_y)).arg(dRealToStr(glwidget->ssl->cursor_z)));
statusWidget->update();

if(glwidget != nullptr && glwidget->ssl != nullptr)
{
// Stops blue robots from moving if no package has been received for 1 second
if(glwidget->ssl->elapsedLastPackageBlue.nsecsElapsed()*1e-9 > 1)
{
for(int i=0; i < glwidget->cfg->Robots_Count(); ++i)
{
const int index = glwidget->ssl->robotIndex(i, BLUE-1);

if(index == -1 || glwidget->ssl->robots[index] == nullptr)
continue;
Bollos00 marked this conversation as resolved.
Show resolved Hide resolved

glwidget->ssl->robots[index]->resetSpeeds();
}
}
// Stops yellow robots from moving if no package has been received for 1 second
if(glwidget->ssl->elapsedLastPackageYellow.nsecsElapsed()*1e-9 > 1)
{
for(int i=0; i < glwidget->cfg->Robots_Count(); ++i)
{
const int index = glwidget->ssl->robotIndex(i, YELLOW-1);

if(index == -1 || glwidget->ssl->robots[index] == nullptr)
continue;

glwidget->ssl->robots[index]->resetSpeeds();
}
}
}
}

Check notice on line 444 in src/mainwindow.cpp

View check run for this annotation

codefactor.io / CodeFactor

src/mainwindow.cpp#L365-L444

Complex Method
void MainWindow::updateRobotLabel()
{
robotwidget->teamCombo->setCurrentIndex(glwidget->Current_team);
Expand Down
7 changes: 7 additions & 0 deletions src/sslworld.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -313,6 +313,9 @@ SSLWorld::SSLWorld(QGLWidget* parent, ConfigWidget* _cfg, RobotsFormation *form1
}

restartRequired = false;

elapsedLastPackageBlue.start();
elapsedLastPackageYellow.start();
}

int SSLWorld::robotIndex(int robot,int team) {
Expand Down Expand Up @@ -705,6 +708,8 @@ void SSLWorld::blueControlSocketReady() {
robotControlResponse.SerializeToArray(buffer.data(), buffer.size());
blueControlSocket->writeDatagram(buffer.data(), buffer.size(), datagram.senderAddress(), datagram.senderPort());
}

elapsedLastPackageBlue.start();
}

void SSLWorld::yellowControlSocketReady() {
Expand All @@ -723,6 +728,8 @@ void SSLWorld::yellowControlSocketReady() {
robotControlResponse.SerializeToArray(buffer.data(), buffer.size());
yellowControlSocket->writeDatagram(buffer.data(), buffer.size(), datagram.senderAddress(), datagram.senderPort());
}

elapsedLastPackageYellow.start();
}


Expand Down
Loading