zl程序教程

您现在的位置是:首页 >  硬件

当前栏目

写字机器人程序

2023-06-13 09:12:26 时间

大家好,又见面了,我是你们的朋友全栈君。

#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QPainter>
#include <QtCore>
#include <QDebug>
#include "math.h"
#include <sys/ioctl.h>
#include <fcntl.h>
#include <QFile>
#include <QDataStream>    //引入头文件
/*定义常量*/
#define DIS_SMALL 278;     //定义小臂长度 单位cm
#define DIS_BIG 213.5;     //定义臂长度 单位cm
#define PI 3.1415926;      //定义PI
/*定义用到的全局变量*/
int angfix_big=0;             //定义大臂
int angfix_small=0;
int current_big=560-117;=433/667-550=117//553-433=120  大臂
int current_small=650-(-71);//=695//553-624=-71623-695=-72

int current_page=1;
int page_size=1;
QString save_index="dat0.dat";
int motor_speed=1500;//delay time

int flag_writing=0;//writing标志
int flag_lock[10];//0 当前页锁定标志,锁定后不可修改
int cont_replay[10];//0 屏幕复写计数
int temp_replay[10];//0 屏幕复写用到临时变量
int timems[10];//延时计数变量,每页单独一个数组元素由计数器1修改数值
int xx[10][100][1000];//记录x坐标 当前页-笔画数-点数
int yy[10][100][1000];//记录y坐标 当前页-笔画数-点数
int angle_big[10][100][1000];  //记录当前点的大臂角度值 当前页-笔画数-点数
int angle_small[10][100][1000];//记录当前点的小臂角度值 当前页-笔画数-点数
int dian_delay[10][100][1000];//记录每个点之间到延时时间 当前页-笔画数-点数
int step[10][100];//记录每个笔画到点数 当前页-笔画数
int bihua_num[10];//-1 用前先加1所以初始值为-1,记录当前页的总笔画数 当前页
int dian_num[10];//记录当前页的总点数 当前页
int flag_go[10];//0 用于go标记,每次go之后用此标记清除画板

int bihua_delay[10][100];//记录笔画间隔时间 当前页-笔画数
double A1,A2,A,B,C,a,b,c;//坐标与电机角度转换用到变量、常量

/********************************************************************************
* 函数名称:主函数
* 功    能:主函数
*********************************************************************************/
MainWindow::MainWindow(QWidget *parent) :QMainWindow(parent),ui(new Ui::MainWindow)//主函数入口
{
    /*初始化变量*/
    ui->setupUi(this);
    ui->radioButton_2->setEnabled(0);
    ui->radioButton_3->setEnabled(0);
    ui->radioButton_4->setEnabled(0);
    ui->radioButton_5->setEnabled(0);
    ui->radioButton_6->setEnabled(0);
    ui->radioButton_7->setEnabled(0);
    ui->radioButton_8->setEnabled(0);
    ui->radioButton_9->setEnabled(0);
    for(int con1=0;con1<10;con1++)
    {
        flag_lock[con1]=0;
        cont_replay[con1]=0;
        bihua_num[con1]=-1;
        dian_num[con1]=0;
        flag_go[con1]=0;
        temp_replay[con1]=0;
        timems[con1]=1;
    }
    a=DIS_SMALL;
    c=DIS_BIG;

    /*初始化GPIO状态*/
    int fd=::open("/dev/leds",0);
    int fd2=::open("/dev/step",0);
    ::ioctl(fd2,0,0);//  con6 PIN9  CK1
    ::ioctl(fd2,0,1);//  con6 PIN10 CK2
    ::ioctl(fd2,0,2);//  con6 PIN11 CW1
    ::ioctl(fd2,0,3);//  con6 PIN12 CW2
    ::ioctl(fd2,1,6);//  con6 PIN15 HAND
    ::close(fd2);
    ::ioctl(fd,0,0);//
    ::ioctl(fd,0,1);//
    ::ioctl(fd,0,2);//
    ::ioctl(fd,0,3);//
    ::close(fd);

    /*初始化画板*/
    pix = QPixmap(200,200);
    pix.fill(Qt::gray);

    /*初始化定时器*/
    timer1 = new QTimer(this);
    timer4 = new QTimer(this);
    timer2 = new QTimer(this);
    timer3 = new QTimer(this);
    connect(timer1,SIGNAL(timeout()),this,SLOT(timer1UpDate())); //
    connect(timer2,SIGNAL(timeout()),this,SLOT(timer2UpDate()));
    connect(timer3,SIGNAL(timeout()),this,SLOT(timer3UpDate()));
    connect(timer4,SIGNAL(timeout()),this,SLOT(timer4UpDate()));
    timer1->start(1);
    timer4->start(10);//启动遥控接收器监视定时器,每10ms检测一次是否有按键被按下
}

/********************************************************************************
* 函数名称:退出函数
* 功    能:退出应用
*********************************************************************************/
MainWindow::~MainWindow() //主退出函数
{
    delete ui;
}

/********************************************************************************
* 函数名称:鼠标按下事件
* 功    能:完成坐标记录、角度转换、延时数据记录、绘制起始点等功能
* 参    数:事件
* 返 回 值:无
*********************************************************************************/
void MainWindow::mousePressEvent(QMouseEvent *event)
{
    if(event->button()==Qt::LeftButton)
    {
        x = event->x()-20;//获得当前x坐标并修正
        y = event->y()-10;//获得当前y坐标并修正
        if(x>=0&&x<=200&&y>=0&&y<=200&&!flag_lock[current_page]) //判断是否符合绘画条件
        {
            bihua_num[current_page]++;//当前页到笔画数加1
            xx[current_page][bihua_num[current_page]][dian_num[current_page]]=x;//记录当前x坐标
            yy[current_page][bihua_num[current_page]][dian_num[current_page]]=y;//记录当前y坐标

            /*绘制当前坐标*/
            QPainter pp11(&pix);
            pp11.drawLine(x,y,x,y);
            update();

            /*屏幕坐标换算成电机角度并记录*/
            x1=320-x;//当前坐标转化为机械臂坐标
            y1=290-y;//当前坐标转化为机械臂坐标
            b=sqrt(x1*x1+y1*y1);
            B=acos((c*c+a*a-b*b)/(2*a*c))*180/PI;
            A1=atan(y1/x1)*180/PI;;
            A2=acos((c*c+b*b-a*a)/(2*c*b))*180/PI;
            A=A1+A2;
            angle_big[current_page][bihua_num[current_page]][dian_num[current_page]]=(int)(A/0.1286)+angfix_big;    //记录当前坐标的大臂角度数
            angle_small[current_page][bihua_num[current_page]][dian_num[current_page]]=(int)(B/0.1286)+angfix_small;//记录当前坐标的小臂角度数
            dian_num[current_page]++;//点数加1
            path1.moveTo(x,y);//将绘图坐标系原点转换到当前坐标
            bihua_delay[current_page][bihua_num[current_page]]=timems[current_page];//记录笔画间隔时间
            timems[current_page]=1;//重置计时变量
            flag_go[current_page]=0;//清除go标记
        }
    }
}

/********************************************************************************
* 函数名称:鼠标按下并移动事件
* 功    能:完成坐标记录、角度转换、延时数据记录、绘制相邻两点构成的直线等功能
* 参    数:事件
* 返 回 值:无
*********************************************************************************/
void MainWindow::mouseMoveEvent(QMouseEvent *event)
{
    if(event->buttons()&Qt::LeftButton) //注释同“鼠标按下事件函数”
    {
        x = event->x()-20;
        y = event->y()-10;
        if(x>=0&&x<=200&&y>=0&&y<=200&&!flag_lock[current_page])
        {
            QPainter pp11(&pix);
            xx[current_page][bihua_num[current_page]][dian_num[current_page]]=x;
            yy[current_page][bihua_num[current_page]][dian_num[current_page]]=y;
            pp11.drawLine(xx[current_page][bihua_num[current_page]][dian_num[current_page]-1],yy[current_page][bihua_num[current_page]][dian_num[current_page]-1],xx[current_page][bihua_num[current_page]][dian_num[current_page]],yy[current_page][bihua_num[current_page]][dian_num[current_page]]);//绘制上一坐标到本次坐标的直线
            update();
            dian_delay[current_page][bihua_num[current_page]][dian_num[current_page]]=timems[current_page];

            x1=320-x;
            y1=290-y;

            b=sqrt(x1*x1+y1*y1);
            B=acos((c*c+a*a-b*b)/(2*a*c))*180/PI;
            A1=atan(y1/x1)*180/PI;;
            A2=acos((c*c+b*b-a*a)/(2*c*b))*180/PI;
            A=A1+A2;

            angle_big[current_page][bihua_num[current_page]][dian_num[current_page]]=(int)(A/0.1286)+angfix_big;
            angle_small[current_page][bihua_num[current_page]][dian_num[current_page]]=(int)(B/0.1286)+angfix_small;;
            dian_num[current_page]++;
            timems[current_page]=1;
        }
    }
}

/********************************************************************************
* 函数名称:鼠标按下事件
* 功    能:完成坐标记录、角度转换、延时数据记录、绘制结束点点功能
* 参    数:事件
* 返 回 值:无
*********************************************************************************/
void MainWindow::mouseReleaseEvent(QMouseEvent *event)
{
    if(event->button() == Qt::LeftButton) //注释同“鼠标按下事件函数”
    {
        x = event->x()-20;
        y = event->y()-10;

        if(x>=0&&x<=200&&y>=0&&y<=200&&!flag_lock[current_page])
        {
            xx[current_page][bihua_num[current_page]][dian_num[current_page]]=x;
            yy[current_page][bihua_num[current_page]][dian_num[current_page]]=y;
            dian_delay[current_page][bihua_num[current_page]][dian_num[current_page]]=timems[current_page];

            x1=320-x;
            y1=290-y;

            b=sqrt(x1*x1+y1*y1);
            B=acos((c*c+a*a-b*b)/(2*a*c))*180/PI;
            A1=atan(y1/x1)*180/PI;;
            A2=acos((c*c+b*b-a*a)/(2*c*b))*180/PI;
            A=A1+A2;

            angle_big[current_page][bihua_num[current_page]][dian_num[current_page]]=(int)(A/0.1286)+angfix_big;
            angle_small[current_page][bihua_num[current_page]][dian_num[current_page]]=(int)(B/0.1286)+angfix_small;
            step[current_page][bihua_num[current_page]]=dian_num[current_page];
            dian_num[current_page]=0;

            timems[current_page]=1;
        }
    }
}

/********************************************************************************
* 函数名称:go按钮点击函数
* 功    能:完成当前页的绘制机械臂动作并且打开timer2完成在屏幕上复写的功能
* 参    数:无
* 返 回 值:无
*********************************************************************************/
void MainWindow::on_pushButton_clicked()
{
    /*为变量、标志赋值*/
    flag_go[current_page]=1;
    cont_replay[current_page]=0;
    flag_lock[current_page]=1;
    /*打开step步进电机驱动,并进行相应操作*/
    int fd2=::open("/dev/step",0);
    for(int b11=0;b11<=bihua_num[current_page];b11++)
    {
        go(angle_big[current_page][b11][0],angle_small[current_page][b11][0]);//使机械臂走到指定的角度坐标
        usleep(50000);
        ::ioctl(fd2,0,6);
        for(int b22=1;b22<=step[current_page][b11];b22++)
        {
            usleep(1000*dian_delay[current_page][b11][b22]);
            move(angle_big[current_page][b11][b22]-angle_big[current_page][b11][b22-1],angle_small[current_page][b11][b22]-angle_small[current_page][b11][b22-1]);//按照记录数据依次向后一点坐标移动
        }
        usleep(50000);
        ::ioctl(fd2,1,6);
        if(b11<bihua_num[current_page]){usleep(1000*bihua_delay[current_page][b11+1]);}//笔画延时
    }
    ::close(fd2);
    timer2->start(100);//go复写定时器打开
}

/********************************************************************************
* 函数名称:屏幕复写函数
* 功    能:利用定时器绘制,能够准确复现绘制点之间、笔画之间的延时效果
* 参    数:无
* 返 回 值:无
*********************************************************************************/
void MainWindow::timer2UpDate()//屏幕复写定时器
{
    QPainter pp11(&pix);
    if(temp_replay[current_page]<=bihua_num[current_page])
    {
        if(cont_replay[current_page]<step[current_page][temp_replay[current_page]])
        {
            pp11.drawLine(xx[current_page][temp_replay[current_page]][cont_replay[current_page]],yy[current_page][temp_replay[current_page]][cont_replay[current_page]],xx[current_page][temp_replay[current_page]][cont_replay[current_page]+1],yy[current_page][temp_replay[current_page]][cont_replay[current_page]+1]);
            update();
            timer2->start(dian_delay[current_page][temp_replay[current_page]][cont_replay[current_page]+1]);
            cont_replay[current_page]++;
        }
        else
        {
            temp_replay[current_page]++;
            cont_replay[current_page]=0;
            timer2->start(bihua_delay[current_page][temp_replay[current_page]]);
        }
    }
    else
    {
        timer2->stop();//绘制完成关闭定时器
        update();
        /*恢复复写变量的初值*/
        cont_replay[current_page]=0;
        temp_replay[current_page]=0;
    }
}
/********************************************************************************
* 函数名称:画板更新事件函数
* 功    能:更新画板
* 参    数:绘画事件
* 返 回 值:无
*********************************************************************************/
void MainWindow::paintEvent(QPaintEvent *)
{
    QPainter pp(this);
    pp.drawPixmap(20,10,200,200,pix); //将全局画布pix绘制在画板上
    /*根据go标记清除画板*/
    if(flag_go[current_page])
    {
        pix.fill(Qt::gray);
        flag_go[current_page]=0;
    }
}

/********************************************************************************
* 函数名称:延时计数定时器1更新事件
* 功    能:将延时计数变量自加
* 参    数:无
* 返 回 值:无
*********************************************************************************/
void MainWindow::timer1UpDate()
{
    timems[current_page]++;
}

/********************************************************************************
* 函数名称:机械臂移动函数
* 功    能:将机械臂按照参数运动指定步数
* 参    数:move_temp_big,move_temp_small 分别为大臂与小臂运动的步数,若为正直则向正方向
*           运动对应步数反之则向相反方向运动对应步数
* 返 回 值:无
*********************************************************************************/
void MainWindow::move(int move_temp_big,int move_temp_small)
{
    current_big+=move_temp_big;
    current_small+=move_temp_small;
    int fd=::open("/dev/leds",0);
    int fd2=::open("/dev/step",0);//con6 低电平有效 是!cmd 所以1代表低电平
    int direction1,direction2;

    if(move_temp_big==0&&move_temp_small==0)
    {
        ::close(fd);
        return;
    }

    if(move_temp_big<0)
    {
        direction1=0;
        move_temp_big=-move_temp_big;
        ::ioctl(fd,1,2);
        ::ioctl(fd2,1,2);
    }
    else
    {
        direction1=1;
        ::ioctl(fd,0,2);
        ::ioctl(fd2,0,2);
    }

    if(move_temp_small<0)
    {
        direction2=0;
        move_temp_small=-move_temp_small;
        ::ioctl(fd,1,3);
        ::ioctl(fd2,1,3);
    }
    else
    {
        direction2=1;
        ::ioctl(fd,0,3);
        ::ioctl(fd2,0,3);
    }

    if(move_temp_big==0)
    {
        for(int tmp=0;tmp<move_temp_small;tmp++)
        {
            ::ioctl(fd,1,1);
            ::ioctl(fd2,1,1);
            usleep(10);
            ::ioctl(fd,0,1);
            ::ioctl(fd2,0,1);
            usleep(motor_speed);
        }
        ::close(fd);
        ::close(fd2);
        return;
    }

    if(move_temp_small==0)
    {
        for(int tmp=0;tmp<move_temp_big;tmp++)
        {
            ::ioctl(fd,1,0);
            ::ioctl(fd2,1,0);
            usleep(10);
            ::ioctl(fd,0,0);
            ::ioctl(fd2,0,0);
            usleep(motor_speed);
        }
        ::close(fd);
        ::close(fd2);
        return;
    }

    int go_loop=move_temp_big*move_temp_small;

    for (int go_temp=1;go_temp<=go_loop;go_temp++)
    {
        if((go_temp%move_temp_big==0)&&(go_temp%move_temp_small==0))
        {
            ::ioctl(fd,1,1);
            ::ioctl(fd,1,0);
            ::ioctl(fd2,1,1);
            ::ioctl(fd2,1,0);
            usleep(10);
            ::ioctl(fd,0,1);
            ::ioctl(fd,0,0);
            ::ioctl(fd2,0,1);
            ::ioctl(fd2,0,0);
            usleep(motor_speed);
        }
        else
        {
            if(go_temp%move_temp_big==0)
            {
                ::ioctl(fd,1,1);
                ::ioctl(fd2,1,1);
                usleep(10);
                ::ioctl(fd,0,1);
                ::ioctl(fd2,0,1);
                usleep(motor_speed);
            }
            if(go_temp%move_temp_small==0)
            {
                ::ioctl(fd,1,0);
                ::ioctl(fd2,1,0);
                usleep(10);
                ::ioctl(fd,0,0);
                ::ioctl(fd2,0,0);
                usleep(motor_speed);
            }
        }
    }

    ::close(fd);
    ::close(fd2);
}

/********************************************************************************
* 函数名称:机械臂移动到指定角度函数
* 功    能:将机械臂移动到指定步数
* 参    数:go_temp_big,go_temp_small 分别为大臂与小臂的目的角度值,经过与当前角度比较,得出
*           运动对应步数反和方向,并移动
* 返 回 值:无
*********************************************************************************/
void MainWindow::go(int go_temp_big,int go_temp_small)
{
    go_temp_big=go_temp_big-current_big;      //计算大臂当前角度与指定的角度之间的差值
    go_temp_small=go_temp_small-current_small;//计算小臂当前角度与指定的角度之间的差值

    current_big+=go_temp_big;    //更新当前角度
    current_small+=go_temp_small;//更新当前角度

    int fd=::open("/dev/leds",0);//打开led驱动,便于观察程序运行状态
    int fd2=::open("/dev/step",0);//打开step驱动
    /*判断如果两个电机需要运动的角度均为0则退出函数*/
    if(go_temp_big==0&&go_temp_small==0)
    {
        ::close(fd);return;
    }
    /*判断如果大臂需要运动的角度为负数,则将大臂运行方向置为反并且将负数转为正数;否则大臂运行方向为正*/
    if(go_temp_big<0)
    {
        go_temp_big=-go_temp_big;
        ::ioctl(fd,1,2);
        ::ioctl(fd2,1,2);
    }
    else
    {
        ::ioctl(fd,0,2);
        ::ioctl(fd2,0,2);
    }
    /*判断如果小臂需要运动的角度为负数,则将小臂运行方向置为反并且将负数转为正数;否则小臂运行方向为正*/
    if(go_temp_small<0)
    {
        go_temp_small=-go_temp_small;
        ::ioctl(fd,1,3);
        ::ioctl(fd2,1,3);
    }
    else
    {
        ::ioctl(fd,0,3);
        ::ioctl(fd2,0,3);
    }

    /*判断如果大臂需要运动的角度大于等于小臂需要的,则用大臂度数作为循环计数值,
      这里需要说明以下,本函数是两个电机同时动作的所以需要用运动数大的作为计数。*/
    if(go_temp_big>=go_temp_small)
    {
        for(int tmp=0;tmp<go_temp_big;tmp++)
        {
            ::ioctl(fd,1,0);
            ::ioctl(fd2,1,0);

            /*判断小臂是否运行结束,若没结束则小臂运动*/
            if(tmp<go_temp_small)
            {
                ::ioctl(fd,1,1);
                ::ioctl(fd2,1,1);
            }

            usleep(10);

            /*判断小臂是否运行结束,若没结束则小臂运动*/
            if(tmp<go_temp_small)
            {
                ::ioctl(fd,0,1);
                ::ioctl(fd2,0,1);
            }

            ::ioctl(fd,0,0);
            ::ioctl(fd2,0,0);
            usleep(motor_speed);//调整脉冲周期以调节电机转速
        }
    }
    else //否则与上述相反
    {
        for(int tmp=0;tmp<go_temp_small;tmp++)
        {
            ::ioctl(fd,1,1);
            ::ioctl(fd2,1,1);

            if(tmp<go_temp_big)
            {
                ::ioctl(fd,1,0);
                ::ioctl(fd2,1,0);
            }

            usleep(10);

            if(tmp<go_temp_big)
            {
                ::ioctl(fd,0,0);
                ::ioctl(fd2,0,0);
            }

            ::ioctl(fd,0,1);
            ::ioctl(fd2,0,1);

            usleep(motor_speed);
        }
    }
    ::close(fd);
    ::close(fd2);
}

/********************************************************************************
* 函数名称:clear按钮函数
* 功    能:清除当前页的画板信息,并解除当前页的锁定可以重新绘制
* 参    数:无
* 返 回 值:无
*********************************************************************************/
void MainWindow::on_pushButton_2_clicked()
{
    bihua_num[current_page]=-1;
    timer1->start(1);
    flag_lock[current_page]=0;
    pix.fill(Qt::gray);
    update();
}

/********************************************************************************
* 函数名称:ADD按钮点击函数
* 功    能:增加绘制页面
* 参    数:无
* 返 回 值:无
*********************************************************************************/
void MainWindow::on_ADD_clicked()
{
    switch (current_page)
    {
    case 1:
        current_page=2;
        ui->radioButton_2->setChecked(1);
        ui->radioButton_2->setEnabled(1);
        page_size=2;
        pix.fill(Qt::gray);
        update();
        flag_lock[current_page-1]=1;
        flag_lock[current_page]=0;
        timems[current_page]=1;
        break;

    case 2:
        current_page=3;
        ui->radioButton_3->setChecked(1);
        ui->radioButton_3->setEnabled(1);
        page_size=3;
        pix.fill(Qt::gray);
        update();
        flag_lock[current_page-1]=1;
        flag_lock[current_page]=0;
        timems[current_page]=1;
        break;

    case 3:
        current_page=4;
        ui->radioButton_4->setChecked(1);
        ui->radioButton_4->setEnabled(1);
        flag_lock[current_page]=0;
        page_size=4;
        pix.fill(Qt::gray);
        update();
        flag_lock[current_page-1]=1;
        timems[current_page]=1;
        break;

    case 4:
        current_page=5;
        ui->radioButton_5->setChecked(1);
        ui->radioButton_5->setEnabled(1);
        flag_lock[current_page]=0;
        page_size=5;
        pix.fill(Qt::gray);
        update();
        flag_lock[current_page-1]=1;
        timems[current_page]=1;
        break;

    case 5:
        current_page=6;
        ui->radioButton_6->setChecked(1);
        ui->radioButton_6->setEnabled(1);
        flag_lock[current_page]=0;
        page_size=6;
        pix.fill(Qt::gray);
        update();
        flag_lock[current_page-1]=1;
        timems[current_page]=1;
        break;

    case 6:
        current_page=7;
        ui->radioButton_7->setChecked(1);
        ui->radioButton_7->setEnabled(1);
        flag_lock[current_page]=0;
        page_size=7;
        pix.fill(Qt::gray);
        update();
        flag_lock[current_page-1]=1;
        timems[current_page]=1;
        break;

    case 7:
        current_page=8;
        ui->radioButton_8->setChecked(1);
        ui->radioButton_8->setEnabled(1);
        flag_lock[current_page]=0;
        page_size=8;
        pix.fill(Qt::gray);
        update();
        flag_lock[current_page-1]=1;
        timems[current_page]=1;
        break;

    case 8:
        current_page=9;
        ui->radioButton_9->setChecked(1);
        ui->radioButton_9->setEnabled(1);
        flag_lock[current_page]=0;
        page_size=9;
        pix.fill(Qt::gray);
        update();
        flag_lock[current_page-1]=1;
        timems[current_page]=1;
        break;
    case 9:flag_lock[current_page]=1;break;
    }
}

/********************************************************************************
* 函数名称:RM按钮点击函数
* 功    能:从后向前删除页面,同时删除页面上所储存的信息
* 参    数:无
* 返 回 值:无
*********************************************************************************/
void MainWindow::on_RM_clicked()
{
    switch (page_size)
    {
    case 1:timer2->start(1);update();break;
    case 2:
        current_page=1;
        ui->radioButton_1->setChecked(1);
        ui->radioButton_2->setEnabled(0);
        page_size=1;
        bihua_num[current_page+1]=-1;
        timer1->start(1);
        flag_lock[current_page+1]=0;
        pix.fill(Qt::gray);
        timer2->start(1);
        update();
        break;

    case 3:
        current_page=2;
        ui->radioButton_2->setChecked(1);
        ui->radioButton_3->setEnabled(0);
        page_size=2;
        bihua_num[current_page+1]=-1;
        timer1->start(1);
        flag_lock[current_page+1]=0;
        pix.fill(Qt::gray);
        timer2->start(1);
        update();
        break;
    case 4:
        current_page=3;
        ui->radioButton_3->setChecked(1);
        ui->radioButton_4->setEnabled(0);
        page_size=3;
        bihua_num[current_page+1]=-1;
        timer1->start(1);
        flag_lock[current_page+1]=0;
        pix.fill(Qt::gray);
        timer2->start(1);
        update();
        break;
    case 5:
        current_page=4;
        ui->radioButton_4->setChecked(1);
        ui->radioButton_5->setEnabled(0);
        page_size=4;
        bihua_num[current_page+1]=-1;
        timer1->start(1);
        flag_lock[current_page+1]=0;
        pix.fill(Qt::gray);
        timer2->start(1);
        update();
        break;
    case 6:
        current_page=5;
        ui->radioButton_5->setChecked(1);
        ui->radioButton_6->setEnabled(0);
        page_size=5;
        bihua_num[current_page+1]=-1;
        timer1->start(1);
        flag_lock[current_page]=0;
        pix.fill(Qt::gray);
        timer2->start(1);
        update();
        break;

    case 7:
        current_page=6;
        ui->radioButton_6->setChecked(1);
        ui->radioButton_7->setEnabled(0);
        page_size=6;
        bihua_num[current_page+1]=-1;
        timer1->start(1);
        flag_lock[current_page]=0;
        pix.fill(Qt::gray);
        timer2->start(1);
        update();
        break;

    case 8:
        current_page=7;
        ui->radioButton_7->setChecked(1);
        ui->radioButton_8->setEnabled(0);
        page_size=7;
        bihua_num[current_page+1]=-1;
        timer1->start(1);
        flag_lock[current_page]=0;
        pix.fill(Qt::gray);
        timer2->start(1);
        update();
        break;

    case 9:
        current_page=8;
        ui->radioButton_8->setChecked(1);
        ui->radioButton_9->setEnabled(0);
        page_size=8;
        bihua_num[current_page+1]=-1;
        timer1->start(1);
        flag_lock[current_page]=0;
        pix.fill(Qt::gray);
        timer2->start(1);
        update();
        break;
    }
}

/********************************************************************************
* 函数名称:radioButton点击函数
* 功    能:根据被点击的radioButton更改当前页面,并且在点击的时候调用复写函数完成本页信息的预览
* 参    数:checked 代表是否被选中
* 返 回 值:无
*********************************************************************************/
void MainWindow::on_radioButton_1_clicked(bool checked)
{
    if(checked)current_page=1;
    pix.fill(Qt::gray);
    timer2->start(1);
    update();
}

void MainWindow::on_radioButton_2_clicked(bool checked)
{
    if(checked)current_page=2;
    pix.fill(Qt::gray);
    timer2->start(1);
    update();
}

void MainWindow::on_radioButton_3_clicked(bool checked)
{
    if(checked)current_page=3;
    pix.fill(Qt::gray);
    timer2->start(1);
    update();
}

void MainWindow::on_radioButton_4_clicked(bool checked)
{
    if(checked)current_page=4;
    pix.fill(Qt::gray);
    timer2->start(1);
    update();
}

void MainWindow::on_radioButton_5_clicked(bool checked)
{
    if(checked)current_page=5;
    pix.fill(Qt::gray);
    timer2->start(1);
    update();
}

void MainWindow::on_radioButton_6_clicked(bool checked)
{
    if(checked)current_page=6;
    pix.fill(Qt::gray);
    timer2->start(1);
    update();
}


void MainWindow::on_radioButton_7_clicked(bool checked)
{
    if(checked)current_page=7;
    pix.fill(Qt::gray);
    timer2->start(1);
    update();
}

void MainWindow::on_radioButton_8_clicked(bool checked)
{
    if(checked)current_page=8;
    pix.fill(Qt::gray);
    timer2->start(1);
    update();
}

void MainWindow::on_radioButton_9_clicked(bool checked)
{
    if(checked)current_page=9;
    pix.fill(Qt::gray);
    timer2->start(1);
    update();
}

/********************************************************************************
* 函数名称:quit按钮点击函数
* 功    能:退出程序
* 参    数:无
* 返 回 值:无
*********************************************************************************/
void MainWindow::on_pushButton_3_clicked()
{
    close();
}

/********************************************************************************
* 函数名称:SV按钮点击函数
* 功    能:在当前储存位置储存当前的一切数据
* 参    数:无
* 返 回 值:无
*********************************************************************************/
void MainWindow::on_pushButton_4_clicked()
{

    QFile file(save_index);
    if(!file.open(QIODevice::WriteOnly))
    {
        qDebug()<<"Can't open the file!"<<endl;
    }
    QDataStream in(&file);
    for(int con3=0;con3<10;con3++)
    {
        for(int con1=0;con1<=bihua_num[con3];con1++)
        {
            for(int con2=0;con2<=step[con3][con1];con2++)
            {
                in
                        <<1
                        <<con3
                        <<con1
                        <<con2
                        <<xx[con3][con1][con2]
                        <<yy[con3][con1][con2]
                        <<angle_big[con3][con1][con2]
                        <<angle_small[con3][con1][con2]
                        <<step[con3][con1]
                        <<bihua_delay[con3][con1]
                        <<0
                        <<0;
            }
        }
    }
    for(int con1=0;con1<10;con1++)
    {
        in
                <<2
                <<con1
                <<con1
                <<flag_lock[con1]
                <<cont_replay[con1]
                <<bihua_num[con1]
                <<dian_num[con1]
                <<flag_go[con1]
                <<temp_replay[con1]
                <<timems[con1]
                <<current_page
                <<page_size;
    }

    file.close();
}

/********************************************************************************
* 函数名称:LD按钮点击函数
* 功    能:读取当前储存位置的数据作为当前数据
* 参    数:无
* 返 回 值:无
*********************************************************************************/
void MainWindow::on_pushButton_5_clicked()
{
    return;
    QFile file(save_index);
    if(!file.open(QIODevice::ReadOnly))
    {
        qDebug()<<"Can't open the file!"<<endl;
    }

    QDataStream in(&file);
    int t_xx,t_yy,t_axx,t_ayy,con1,con2,fn,curpage,noth1,noth2,noth3,noth4;

    while(!in.atEnd())
    {
        in
                >>fn
                >>curpage
                >>con1
                >>con2
                >>t_xx
                >>t_yy
                >>t_axx
                >>t_ayy
                >>noth1
                >>noth2
                >>noth3
                >>noth4;
        if(fn==1)
        {
            xx[curpage][con1][con2]=t_xx;
            yy[curpage][con1][con2]=t_yy;
            angle_big[curpage][con1][con2]=t_axx;
            angle_small[curpage][con1][con2]=t_ayy;
            step[curpage][con1]=noth1;
            bihua_delay[curpage][con1]=noth2;
        }
        if(fn==2)
        {
            flag_lock[curpage]=con2;
            cont_replay[curpage]=t_xx;
            bihua_num[curpage]=t_yy;
            dian_num[curpage]=t_axx;
            flag_go[curpage]=t_ayy;
            temp_replay[curpage]=noth1;
            timems[curpage]=noth2;
            current_page=noth3;
            page_size=noth4;
        }
    }

    file.close();

    switch(current_page)
    {
    case 1:
        ui->radioButton_1->setEnabled(1);
        ui->radioButton_2->setEnabled(0);
        ui->radioButton_3->setEnabled(0);
        ui->radioButton_4->setEnabled(0);
        ui->radioButton_5->setEnabled(0);
        ui->radioButton_6->setEnabled(0);
        ui->radioButton_7->setEnabled(0);
        ui->radioButton_8->setEnabled(0);
        ui->radioButton_9->setEnabled(0);
        ui->radioButton_1->setChecked(1);
        break;

    case 2:
        ui->radioButton_1->setEnabled(1);
        ui->radioButton_2->setEnabled(1);
        ui->radioButton_3->setEnabled(0);
        ui->radioButton_4->setEnabled(0);
        ui->radioButton_5->setEnabled(0);
        ui->radioButton_6->setEnabled(0);
        ui->radioButton_7->setEnabled(0);
        ui->radioButton_8->setEnabled(0);
        ui->radioButton_9->setEnabled(0);
        ui->radioButton_2->setChecked(1);
        break;

    case 3:
        ui->radioButton_1->setEnabled(1);
        ui->radioButton_2->setEnabled(1);
        ui->radioButton_3->setEnabled(1);
        ui->radioButton_4->setEnabled(0);
        ui->radioButton_5->setEnabled(0);
        ui->radioButton_6->setEnabled(0);
        ui->radioButton_7->setEnabled(0);
        ui->radioButton_8->setEnabled(0);
        ui->radioButton_9->setEnabled(0);
        ui->radioButton_3->setChecked(1);
        break;

    case 4:
        ui->radioButton_1->setEnabled(1);
        ui->radioButton_2->setEnabled(1);
        ui->radioButton_3->setEnabled(1);
        ui->radioButton_4->setEnabled(1);
        ui->radioButton_5->setEnabled(0);
        ui->radioButton_6->setEnabled(0);
        ui->radioButton_7->setEnabled(0);
        ui->radioButton_8->setEnabled(0);
        ui->radioButton_9->setEnabled(0);
        ui->radioButton_4->setChecked(1);
        break;

    case 5:
        ui->radioButton_1->setEnabled(1);
        ui->radioButton_2->setEnabled(1);
        ui->radioButton_3->setEnabled(1);
        ui->radioButton_4->setEnabled(1);
        ui->radioButton_5->setEnabled(1);
        ui->radioButton_6->setEnabled(0);
        ui->radioButton_7->setEnabled(0);
        ui->radioButton_8->setEnabled(0);
        ui->radioButton_9->setEnabled(0);
        ui->radioButton_5->setChecked(1);
        break;

    case 6:
        ui->radioButton_1->setEnabled(1);
        ui->radioButton_2->setEnabled(1);
        ui->radioButton_3->setEnabled(1);
        ui->radioButton_4->setEnabled(1);
        ui->radioButton_5->setEnabled(1);
        ui->radioButton_6->setEnabled(1);
        ui->radioButton_7->setEnabled(0);
        ui->radioButton_8->setEnabled(0);
        ui->radioButton_9->setEnabled(0);
        ui->radioButton_6->setChecked(1);
        break;

    case 7:
        ui->radioButton_1->setEnabled(1);
        ui->radioButton_2->setEnabled(1);
        ui->radioButton_3->setEnabled(1);
        ui->radioButton_4->setEnabled(1);
        ui->radioButton_5->setEnabled(1);
        ui->radioButton_6->setEnabled(1);
        ui->radioButton_7->setEnabled(1);
        ui->radioButton_8->setEnabled(0);
        ui->radioButton_9->setEnabled(0);
        ui->radioButton_7->setChecked(1);
        break;

    case 8:
        ui->radioButton_1->setEnabled(1);
        ui->radioButton_2->setEnabled(1);
        ui->radioButton_3->setEnabled(1);
        ui->radioButton_4->setEnabled(1);
        ui->radioButton_5->setEnabled(1);
        ui->radioButton_6->setEnabled(1);
        ui->radioButton_7->setEnabled(1);
        ui->radioButton_8->setEnabled(1);
        ui->radioButton_9->setEnabled(0);
        ui->radioButton_8->setChecked(1);
        break;

    case 9:
        ui->radioButton_1->setEnabled(1);
        ui->radioButton_2->setEnabled(1);
        ui->radioButton_3->setEnabled(1);
        ui->radioButton_4->setEnabled(1);
        ui->radioButton_5->setEnabled(1);
        ui->radioButton_6->setEnabled(1);
        ui->radioButton_7->setEnabled(1);
        ui->radioButton_8->setEnabled(1);
        ui->radioButton_9->setEnabled(1);
        ui->radioButton_9->setChecked(1);
        break;
    }
}

/********************************************************************************
* 函数名称:run all按钮点击函数
* 功    能:利用timer3定时器准确的完成从页面前向后的写作动作
* 参    数:无
* 返 回 值:无
*********************************************************************************/
void MainWindow::on_pushButton_6_clicked()
{
    int fd=::open("/dev/step",0);//con6 低电平有效 是!cmd 所以1代表低电平
    ::ioctl(fd,1,11);//  con6 PIN19 LED L
    ::close(fd);
    ui->pushButton_6->setText("Running");
    timer3->start(100);
    current_page=0;
    ui->radioButton_1->setChecked(1);
}

/********************************************************************************
* 函数名称:timer3更新函数
* 功    能:依次完成每个页面的书写动作,并且没写完一页都调用lefthand()进行换纸
* 参    数:无
* 返 回 值:无
*********************************************************************************/
void MainWindow::timer3UpDate()
{
    timer3->stop();
    pix.fill(Qt::gray);
    update();
    current_page++;
    on_pushButton_clicked();
    usleep(100000);
    lefthand();

    switch(current_page)
    {
        case 1:ui->radioButton_1->setChecked(1);break;
        case 2:ui->radioButton_2->setChecked(1);break;
        case 3:ui->radioButton_3->setChecked(1);break;
        case 4:ui->radioButton_4->setChecked(1);break;
        case 5:ui->radioButton_5->setChecked(1);break;
        case 6:ui->radioButton_6->setChecked(1);break;
        case 7:ui->radioButton_7->setChecked(1);break;
        case 8:ui->radioButton_8->setChecked(1);break;
        case 9:ui->radioButton_9->setChecked(1);break;
    }

    if(current_page!=page_size)
    {
        timer3->start(100);
        switch(current_page)
        {
            case 1:ui->radioButton_2->setChecked(1);break;
            case 2:ui->radioButton_3->setChecked(1);break;
            case 3:ui->radioButton_4->setChecked(1);break;
            case 4:ui->radioButton_5->setChecked(1);break;
            case 5:ui->radioButton_6->setChecked(1);break;
            case 6:ui->radioButton_7->setChecked(1);break;
            case 7:ui->radioButton_8->setChecked(1);break;
            case 8:ui->radioButton_9->setChecked(1);break;
            case 9:;break;
        }
    }
    else
    {
        ui->pushButton_6->setText("RUN ALL");
        int fd=::open("/dev/step",0);//con6 低电平有效 是!cmd 所以1代表低电平
        ::ioctl(fd,0,11);//  con6 PIN19 LED M
        ::close(fd);
        flag_writing=0;
        timer4->start(10);
    }
}

/********************************************************************************
* 函数名称:存储位置变化函数
* 功    能:根据spinBox的值修改当前的存储文件名称
* 参    数:cont spinBox的值
* 返 回 值:无
*********************************************************************************/
void MainWindow::on_spinBox_valueChanged(QString cont)
{
    save_index="dat"+cont+".dat";
}

/********************************************************************************
* 函数名称:lefthand辅助机械臂运动函数
* 功    能:完成一系列的辅助机械臂动作,完成更换纸张的动作
* 参    数:无
* 返 回 值:无
*********************************************************************************/
void MainWindow::lefthand()
{
    return; //test
    go(1000,1000);
    usleep(10000);
    int fd2=::open("/dev/step",0);//con6 低电平有效 是!cmd 所以1代表低电平

    ::ioctl(fd2,1,4);//  con6 PIN13 M3T K
    ::ioctl(fd2,1,7);//  con6 PIN16 M1W K
    usleep(100000);
    ::ioctl(fd2,1,10);//  con6 PIN19 M2L K
    usleep(1000000);
    ::ioctl(fd2,0,7);//  con6 PIN16 M1W G
    ::ioctl(fd2,0,10);//  con6 PIN19 M2L G
    usleep(10000);
    ::ioctl(fd2,0,4);//  con6 PIN13 M3T G
    usleep(10000);
    ::ioctl(fd2,1,5);//  con6 PIN14 M3L K//准备
    usleep(100000);
    ::ioctl(fd2,1,8);//  con6 PIN17 M1L k//开始
    ::ioctl(fd2,1,9);//  con6 PIN18 M2W k 小臂 -6000
    usleep(100000);
     ::ioctl(fd2,0,9);//  con6 PIN18 M2W g 小臂 -6000
    usleep(100000);
    ::ioctl(fd2,1,9);//  con6 PIN18 M2W k 小臂 -6000
    usleep(100000);
    usleep(100000);
    usleep(100000);
    usleep(100000);
    usleep(100000);
    usleep(100000);
    usleep(100000);
    usleep(100000);
    usleep(100000);
    usleep(650000);
    ::ioctl(fd2,0,8);//  con6 PIN17 M1L g//结束
    ::ioctl(fd2,0,9);//  con6 PIN18 M2W g 小臂
    usleep(100000);
    ::ioctl(fd2,0,5);//  con6 PIN14 M3L g
    usleep(10000);
    ::ioctl(fd2,1,4);//  con6 PIN13 M3T K
    usleep(800000);
    ::ioctl(fd2,1,7);//  con6 PIN16 M1W 大臂 k
    ::ioctl(fd2,1,10);//  con6 PIN19 M2L K
    usleep(150000);
    ::ioctl(fd2,0,10);//  con6 PIN19 M2L g
    usleep(1400000);
    ::ioctl(fd2,0,7);//  con6 PIN16 M1W 大臂 g
    ::ioctl(fd2,0,4);//  con6 PIN13 M3T g
    ::close(fd2);
}

/********************************************************************************
* 函数名称:left按钮点击函数
* 功    能:调用lefthand()函数
* 参    数:无
* 返 回 值:无
*********************************************************************************/
void MainWindow::on_pushButton_7_clicked()//left hand
{
    lefthand();
}

/********************************************************************************
* 函数名称:遥控输入监视函数
* 功    能:利用timer4定时触发事件来监视遥控接收器是否有按键被按下,若有则读取对应存储位置的数据
*           并且调用run all函数完成整套动作
* 参    数:无
* 返 回 值:无
*********************************************************************************/
void MainWindow::timer4UpDate()
{
    timer4->stop();
    char current_buttons[6];

    int fd = ::open("/dev/buttons", 0);

    ::read(fd, current_buttons, 6);
    ::close(fd);

    if(!flag_writing)
    {
        timer4->start(10);
    }
}

发布者:全栈程序员栈长,转载请注明出处:https://javaforall.cn/160487.html原文链接:https://javaforall.cn