forked from straend/SpherebotGUI
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathspherebot.cpp
166 lines (129 loc) · 3.62 KB
/
spherebot.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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
#include "spherebot.h"
SphereBot::SphereBot(QObject *parent) :
QObject(parent)
{
serial = new QSerialPort(this);
state = Spherebot::SB_Disconnected;
connect(serial, SIGNAL(readyRead()),this, SLOT(readData()));
}
bool SphereBot::connectSerial(QString port){
if (state!=Spherebot::SB_Disconnected)
serial->close();
serial->setPortName(port);
if (!serial->open(QIODevice::ReadWrite)){
qDebug()<<"SB: Could not open serialport";
qDebug()<<"\t"<<serial->errorString();
return false;
}
serial->setBaudRate(256000);
serial->setDataBits(QSerialPort::Data8);
serial->setParity(QSerialPort::NoParity);
serial->setStopBits(QSerialPort::OneStop);
serial->setFlowControl(QSerialPort::NoFlowControl);
if (serial->isOpen()){
state=Spherebot::SB_Connected;
qDebug()<<"SB: Connected Serial "<<state;
return true;
} else {
state=Spherebot::SB_Disconnected;
qDebug()<<"SB: Could not open serialport";
qDebug()<<"\t"<<serial->errorString();
return false;
}
}
void SphereBot::disconnectSerial(){
if (state==Spherebot::SB_Disconnected) {
return;
}
serial->close();
state=Spherebot::SB_Disconnected;
qDebug()<<"SB: Disonnected Serial";
}
void SphereBot::disableSteppers(){
if(state!=Spherebot::SB_Connected)
return;
sendCommand("M18");
}
void SphereBot::sendFile(QString path){
file = new QFile(path);
file->open(QIODevice::ReadOnly);
fileStream = new QTextStream(file);
state=Spherebot::SB_Printing;
sendNextLine();
}
void SphereBot::cancelPrint(){
state=Spherebot::SB_Connected;
file->close();
}
void SphereBot::continuePrinting(){
if (state!=Spherebot::SB_WaitUserOk)
return;
state=Spherebot::SB_Printing;
sendNextLine();
}
void SphereBot::setServo(int value){
if (state!=Spherebot::SB_Connected)
return;
QString tmp = ("M300 S" + QString::number((double)value));
sendCommand(tmp);
}
void SphereBot::setRoll(int value){
if (state!=Spherebot::SB_Connected)
return;
QString tmp = ("G1 X" + QString::number((double)value));
sendCommand(tmp);
}
void SphereBot::setPitch(int value){
if (state!=Spherebot::SB_Connected)
return;
QString tmp = ("G1 Y" + QString::number((double)value));
sendCommand(tmp);
}
void SphereBot::gotOkFromPrinter(){
if (state!=Spherebot::SB_WaitPrinterOk)
return;
state=Spherebot::SB_Printing;
sendNextLine();
}
void SphereBot::readData(){
QByteArray data = serial->readAll();
buffer.append(data);
if (buffer.endsWith("ok:\n")){
buffer.clear();
gotOkFromPrinter();
}
}
void SphereBot::sendCommand(QString cmd){
if (state==Spherebot::SB_Connected){
cmd.remove(QRegExp("\\((.*)\\)"));
cmd.append("\n");
serial->write(cmd.toUtf8());
}
}
void SphereBot::sendNextLine(){
if(state!=Spherebot::SB_Printing)
return;
if(fileStream->atEnd()){
state=Spherebot::SB_Idle;
file->close();
fileStream->reset();
disableSteppers();
emit printDone();
return;
}
QString line=fileStream->readLine();
QString stripped = QString(line);
stripped.remove(QRegExp("\\((.*)\\)"));
if (stripped.length()>1){
if (stripped.startsWith("M01")){
state=Spherebot::SB_WaitUserOk;
emit printPause(line);
return;
} else {
state=Spherebot::SB_WaitPrinterOk;
serial->write(stripped.append("\n").toUtf8());
return;
}
}
sendNextLine();
}