-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmainwindow.cpp
136 lines (114 loc) · 2.85 KB
/
mainwindow.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <math.h>
#include <iostream>
#define PI 3.14159265
double constrainAngle(double x){
x = fmod(x,360);
if (x < 0)
x += 360;
return x;
}
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
OpenSCAD = new org::openscad::OpenSCAD("org.openscad.OpenSCAD", "/org/openscad/OpenSCAD/Application",
QDBusConnection::sessionBus(), this);
m_serial=(new QSerialPort(this));
m_serial->setPortName("/dev/ttyACM0");
m_serial->setBaudRate(QSerialPort::Baud115200);
m_serial->setDataBits(QSerialPort::Data8);
m_serial->setParity(QSerialPort::NoParity);
m_serial->setStopBits(QSerialPort::OneStop);
m_serial->setFlowControl(QSerialPort::NoFlowControl);
if (m_serial->open(QIODevice::ReadWrite)) {
ui->labelDBus->setText("connected");
} else {
ui->labelDBus->setText("could not connect");
}
x=0;
y=0;
z=0;
phi=0;
phiOff=0;
startTimer(100);
aButton = false;
bButton = false;
QTimer *timer = new QTimer(this);
connect(timer, SIGNAL(timeout()), this, SLOT(timerEventSerial()));
timer->start(25);
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::timerEvent(QTimerEvent *event)
{
Q_UNUSED(event);
if(OpenSCAD->isValid()){
ui->labelDBus->setText("connected");
}else{
ui->labelDBus->setText("disconnected");
}
if(aButton){
ui->labelAButton->setText("A Button\nPressed");
}else{
ui->labelAButton->setText("A Button\n");
}
if(bButton){
ui->labelBButton->setText("B Button\nPressed");
}else{
ui->labelBButton->setText("B Button\n");
}
ui->progressBarPhi->setValue(phiRaw);
ui->progressBarX->setValue(x);
ui->progressBarY->setValue(y);
ui->progressBarZ->setValue(z);
ui->progressBarA->setValue(a);
ui->progressBarB->setValue(b);
ui->progressBarC->setValue(c);
}
void MainWindow::timerEventSerial(){
char buf[1024];
while(m_serial->canReadLine()){
m_serial->readLine(buf, sizeof(buf));
QString line = QString(buf);
QStringList elemts = line.split(":");
QString el = elemts[0];
if(el=="phi"){
phiRaw=elemts[1].toInt();
}else if(el=="x"){
x=elemts[1].toInt();
}else if(el=="y"){
y=elemts[1].toInt();
}else if(el=="z"){
z=elemts[1].toInt();
}else if(el=="A"){
aButton = (elemts[1].contains("true",Qt::CaseInsensitive));
}else if(el=="B"){
bButton = (elemts[1].contains("true",Qt::CaseInsensitive));
}
}
if(aButton){
phiOff = phiRaw;
}
phi = phiRaw - phiOff;
if(z>0){
phi = 180-phi;
}
double xs = cos(phi*PI/180) * x + sin(phi*PI/180) * y;
double ys = sin(phi*PI/180) * x + cos(phi*PI/180) * y;
//https://www.nxp.com/docs/en/application-note/AN3461.pdf
a = atan2(+ys, -z)/PI*180.0;
b = atan2(+xs, sqrt(pow(ys,2.0)+pow(z,2.0)))/PI*180.0;
c = -phi;
if(z>0){
b =-b;
}
a = constrainAngle(a);
b = constrainAngle(b);
c = constrainAngle(c);
OpenSCAD->rotateTo(a,b,c);
}