1 Star 0 Fork 0

sirli/AxleRobotNew

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
mainwindow1.cpp 8.27 KB
一键复制 编辑 原始数据 按行查看 历史
sirli 提交于 2021-02-24 01:58 . base files
#include "mainwindow1.h"
#include "ui_mainwindow1.h"
MainWindow1::MainWindow1(QWidget *parent) :
m_shareIO(NULL),GT_motor(NULL),m_Digital(NULL),MotorThread(NULL),
DigitalThread(NULL), m_MainWidget(NULL), m_Stepping(NULL),m_PrmWidget(NULL),
m_IOInfo(NULL),Up_IOInfo(NULL),
QMainWindow(parent),ui(new Ui::MainWindow1)
{
ui->setupUi(this);
Up_IOInfo=new QTimer;
m_shareIO=new shareIO;
qDebug()<<"the thread of mainwindow is "<<QThread::currentThreadId();
readPrm();
InitUI();
GT_Init();
connect(Up_IOInfo,&QTimer::timeout,m_IOInfo,&IOInfo::up_IOInfo);
connect(m_PrmWidget,&PrmWidget::SigSetMotorPrm,GT_motor,&Motor::SetMotorPrm);
}
MainWindow1::~MainWindow1()
{
savePrm();
emit SigStopDigitalThread();
QThread::msleep(10);
if(MotorThread!=NULL)
{
MotorThread->exit(0);
MotorThread->wait();
delete GT_motor;
delete MotorThread;
}
if(DigitalThread!=NULL)
{
DigitalThread->exit(0);
DigitalThread->wait();
delete m_Digital;
delete DigitalThread;
}
delete m_Stepping;
delete m_PrmWidget;
delete m_IOInfo;
delete Up_IOInfo;
delete m_shareIO;
delete m_MainWidget;
delete ui;
}
bool MainWindow1::readPrm()
{
EnvmPrm::InitEnvmPrm();
bool readSuccess=false;
QString DateTimer;
QFile fileIn(tr("EnvmPrm.dat"));
if(fileIn.exists())
{
if(fileIn.open(QIODevice::ReadOnly))
{
QDataStream in(&fileIn);
in>>DateTimer;
in>>EnvmPrm::StartFlag;
MotorPrm motorPrmTemp;
for(int i=0;i<4;i++)
{
uint length=sizeof(motorPrmTemp);
char *buf=NULL;
in.readBytes(buf,length);
motorPrmTemp=*(MotorPrm*)buf;
EnvmPrm::m_MotorPrm.insert(i,motorPrmTemp);
}
in>>EnvmPrm::EndFlag;
fileIn.close();
}
if(EnvmPrm::EndFlag!="EndFlag")
return readSuccess;
}
QFile PointIn(tr("Piont.dat")); //读点
if(PointIn.exists())
{
if(PointIn.open(QIODevice::ReadOnly))
{
QDataStream in(&PointIn);
in>>EnvmPrm::PiontCount;
if(EnvmPrm::PiontCount!=100)
return readSuccess;
for(int i=0;i<100;i++)
{
Point PointTemp;
uint length=sizeof(PointTemp);
char *buf=NULL;
in.readBytes(buf,length);
PointTemp=*(Point*)buf;
EnvmPrm::PointMap.insert(i,PointTemp);
if(buf!=NULL)
{
delete buf;
buf=NULL;
}
}
PointIn.close();
}
}
return true;
}
void MainWindow1::savePrm()
{
QFile fileOut(tr("EnvmPrm.dat"));
if(fileOut.open(QIODevice::WriteOnly))
{
QDataStream out(&fileOut);
QString DateTimer=QDateTime::currentDateTime().toString("MM-dd-hh-mm-ss");
out<<DateTimer;
out<<tr("StartFlag");
QMap<int,MotorPrm>::Iterator it=EnvmPrm::m_MotorPrm.begin();
for(;it!=EnvmPrm::m_MotorPrm.end();it++)
{
uint length=sizeof(it.value());
out.writeBytes((char*)&it.value(),length);
}
out<<tr("EndFlag");
fileOut.close();
}
QFile PointOut(tr("Piont.dat")); //保存点位
if(PointOut.open(QIODevice::WriteOnly))
{
QDataStream out(&PointOut);
EnvmPrm::PiontCount=EnvmPrm::PointMap.count();
out<<EnvmPrm::PiontCount;
QMap<int,Point>::iterator it = EnvmPrm::PointMap.begin();
for(; it != EnvmPrm::PointMap.end();it++ )
{
uint length=sizeof(it.value());
out.writeBytes((char*)&it.value(),length);
}
PointOut.close();
}
}
void MainWindow1::InitUI()
{
m_MainWidget =new MainWidget;
m_Stepping=new Stepping;
m_PrmWidget=new PrmWidget;
m_IOInfo=new IOInfo;
ui->stackedWidget->insertWidget(0,m_MainWidget);
ui->stackedWidget->insertWidget(1,m_Stepping);
ui->stackedWidget->insertWidget(2,m_PrmWidget);
ui->stackedWidget->insertWidget(3,m_IOInfo);
ui->stackedWidget->setCurrentIndex(1);
}
void MainWindow1::GT_Init()
{
short sRtn;
char addr[50]="pulse1.cfg"; //配置文件地址
GT_motor=new Motor;
MotorThread=new QThread;
sRtn=GT_motor->LoadSetting(addr);
if(sRtn!=0)
{
InfoShow("LoadSetting fail");
return;
}
for(int i=1;i<5;i++)
{
MotorPrm m_MotorPrm=EnvmPrm::getMotorPrm(i-1);
GT_motor->SetMotorPrm(m_MotorPrm,i);
// GT_motor->SetEnable(true,i);
sRtn=GT_motor->HomeIndex(i);
if(sRtn!=0)
{
InfoShow("HomeIndex fail");
return;
}
GT_motor->SetMovePrm(i);
}
GT_motor->SetCraPrm();
/**********************开启motor运动线程***********************/
GT_motor->moveToThread(MotorThread);
connect(m_Stepping,&Stepping::SigMoveRelative,GT_motor,&Motor::MoveRelative);
connect(m_Stepping,&Stepping::SigMoveJop,GT_motor,&Motor::MoveJop );
connect(m_Stepping,&Stepping::SigMoveABS,GT_motor,&Motor::MoveABS);
connect(m_Stepping,&Stepping::SigMoveLine,GT_motor,&Motor::MoveLine);
connect(m_Stepping,&Stepping::SigSetEnable,GT_motor,&Motor::SetEnable);
connect(m_Stepping,&Stepping::SigHomeIndex,GT_motor,&Motor::HomeIndex);
connect(m_Stepping,&Stepping::SigSetMotorPrm,GT_motor,&Motor::SetMotorPrm);
connect(GT_motor,&Motor::SigCommandHandler,m_MainWidget,&MainWidget::InfoShow);
connect(m_MainWidget,&MainWidget::SigMoveABS,GT_motor,&Motor::MoveABS);
connect(m_MainWidget,&MainWidget::SigMoveLine,GT_motor,&Motor::MoveLine);
connect(MotorThread,&QThread::finished,MotorThread,&QThread::terminate);
MotorThread->start();
// emit SigStartStep();
/******************开启IO硬件状态更新线程***********************/
m_Digital=new Digital;
DigitalThread=new QThread;
m_Digital->moveToThread(DigitalThread);
connect(this,SIGNAL(SigStartDigitalThread()),m_Digital,SLOT(doWork()));
connect(this,SIGNAL(SigStopDigitalThread()),m_Digital,SLOT(quitWork()));
connect(DigitalThread,&QThread::finished,DigitalThread,&QThread::terminate);
DigitalThread->start();
emit SigStartDigitalThread();
}
void MainWindow1::changeIndex()
{
if(currentIndex==3)
{
Up_IOInfo->start(30);
}
else {
Up_IOInfo->stop();
}
ui->stackedWidget->setCurrentIndex(currentIndex);
}
void MainWindow1::on_btn_main_clicked()
{
currentIndex=0;
changeIndex();
}
void MainWindow1::on_btn_Step_clicked()
{
currentIndex=1;
// ui->btn_main->setStyleSheet("background-color: rgb(215, 225,235)");
// ui->btn_Step->setStyleSheet("background-color: rgb(0, 190, 0)");
// ui->btn_parameter->setStyleSheet("background-color: rgb(210, 210,210)");
// ui->btn_digital->setStyleSheet("background-color: rgb(210, 210,210)");
// ui->btn_point->setStyleSheet("background-color: rgb(210, 210,210)");
// ui->btn_other->setStyleSheet("background-color: rgb(210, 210,210)");
changeIndex();
}
void MainWindow1::on_btn_parameter_clicked()
{
currentIndex=2;
// ui->btn_main->setStyleSheet("background-color: rgb(215, 225,235)");
// ui->btn_Step->setStyleSheet("background-color: rgb(210, 210,210)");
// ui->btn_parameter->setStyleSheet("background-color: rgb(0, 190, 0)");
// ui->btn_digital->setStyleSheet("background-color: rgb(210, 210,210)");
// ui->btn_point->setStyleSheet("background-color: rgb(210, 210,210)");
// ui->btn_other->setStyleSheet("background-color: rgb(210, 210,210)");
changeIndex();
}
void MainWindow1::on_btn_digital_clicked()
{
currentIndex=3;
// ui->btn_main->setStyleSheet("background-color: rgb(215, 225,235)");
// ui->btn_Step->setStyleSheet("background-color: rgb(210, 210,210)");
// ui->btn_parameter->setStyleSheet("background-color: rgb(210, 210,210)");
// ui->btn_digital->setStyleSheet("background-color: rgb(0, 190, 0)");
// ui->btn_point->setStyleSheet("background-color: rgb(210, 210,210)");
// ui->btn_other->setStyleSheet("background-color: rgb(210, 210,210)");
changeIndex();
}
void MainWindow1::InfoShow(QString str,int type)
{
m_MainWidget->InfoShow(str,type);
}
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/lixin3216/AxleRobotNew.git
git@gitee.com:lixin3216/AxleRobotNew.git
lixin3216
AxleRobotNew
AxleRobotNew
master

搜索帮助