-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathmainWindow.cpp
More file actions
290 lines (240 loc) · 10.4 KB
/
mainWindow.cpp
File metadata and controls
290 lines (240 loc) · 10.4 KB
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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
#include "mainWindow.h"
#include "motionController.h"
#include <QSerialPortInfo>
#include <QDateTime>
#include <QDebug>
#include <QCoreApplication>
#include <QEventLoop>
mainWindow::mainWindow(QObject *parent)
: QObject(parent)
{
m_pRootItem = parent;
m_pPortsModel = new listModel(new comPortItem, this);
m_pMotorsModule = new motorsModule(m_pRootItem, this);
m_pJoystickModule = new joystickModule(m_pRootItem, this);
m_motorsMoving = false;
m_isOnStartPosition = false;
m_pRootItem->setProperty("availablePorts", QVariant::fromValue<listModel*>(m_pPortsModel));
connect(&controller, SIGNAL(powerSaveStatusFinished(QByteArray)), this, SLOT(powerSaveStatusFinished(QByteArray)));
connect(&controller, SIGNAL(firmwareVersionFinished(QByteArray)), this, SLOT(controllerConnected()));
connect(&controller, SIGNAL(motorsRunningFinished(QByteArray)), this, SLOT(movementCheckFinished(QByteArray)));
connect(&controller, SIGNAL(programProgressFinished(QByteArray)), this, SLOT(programProgressFinished(QByteArray)));
connect(&controller, SIGNAL(testControllerFinished(QByteArray)), this, SLOT(testControllerFinished(QByteArray)));
connect(m_pRootItem, SIGNAL(connectToDevices()), this, SLOT(connectToDevices()));
connect(m_pRootItem, SIGNAL(assignAddressRequest(QString,int)), this, SLOT(assignAddressRequest(QString,int)));
connect(m_pRootItem, SIGNAL(goToProgramStartClicked()), this, SLOT(goToProgramStartClicked()));
connect(m_pRootItem, SIGNAL(startProgramClicked()), this, SLOT(startProgramClicked()));
connect(m_pRootItem, SIGNAL(stopProgramClicked()), this, SLOT(stopProgramClicked()));
connect(m_pRootItem, SIGNAL(pauseProgramClicked()), this, SLOT(pauseProgramClicked()));
connect(m_pRootItem, SIGNAL(portsRescanClicked()), this, SLOT(portsRescanClicked()));
connect(m_pRootItem, SIGNAL(connectToPortClicked(QString,QString)), this, SLOT(connectToPortClicked(QString,QString)));
connect(m_pRootItem, SIGNAL(closePort()), &controller, SLOT(closePort()));
connect(&m_progressTimer, SIGNAL(timeout()), this, SLOT(programProgressRequest()));
connect(&m_movementTimer, SIGNAL(timeout()), this, SLOT(movementCheckRequest()));
m_progressTimer.setInterval(1000);
m_movementTimer.setInterval(1000);
portsRescanClicked();
}
void mainWindow::initController() {
controller.setGraffikModeEnable(true);
controller.setJoystickMode(false);
controller.setWatchdogEnable(false);
controller.enableCamera(true);
controller.setMotorEnable(1, true);
controller.setMotorEnable(2, true);
controller.setMotorEnable(3, true);
controller.setMicroStepValue(1, 4);
controller.setMicroStepValue(2, 4);
controller.setMicroStepValue(3, 4);
controller.setEasingMode(1, 2);
controller.setEasingMode(2, 2);
controller.setEasingMode(3, 2);
controller.setProgramMode(1);
controller.setFocusWithShutter(true);
for(int i = 1; i <= 3; ++i)
controller.setMotorAcceleration((unsigned char)i, (float)25000);
}
void mainWindow::programProgressRequest() {
controller.programProgress();
}
void mainWindow::controllerConnected() {
qDebug()<<"controller connected";
controller.testController();
}
/*
programStatus === 0 - stopped
programStatus === 1 - running
programStatus === 2 - paused
programStatus === 3 - going to start position
*/
void mainWindow::startProgramClicked() {
m_pRootItem->setProperty("progressFormVisible", true);
m_pRootItem->setProperty("programStatus", 1);
unsigned interval = m_pRootItem->property("interval").toReal() * 1000;
unsigned focusTime = m_pRootItem->property("focusTime").toReal() * 1000;
unsigned triggerTime = m_pRootItem->property("triggerTime").toReal() * 1000;
unsigned exposureDelay = m_pRootItem->property("exposureDelay").toReal() * 1000;
unsigned programMode = m_pRootItem->property("programMode").toInt();
bool pingPongFlag = m_pRootItem->property("pingPong").toBool();
float length;
controller.setProgramMode((unsigned char)programMode);
controller.setPingPongFlag(pingPongFlag);
// Determine the overall program length and set general parameters
if(programMode != 2) { //not video mode
controller.setShotsInterval(interval);
controller.setFocusTime(focusTime);
controller.setExposureTime(triggerTime);
controller.setExposureDelay(exposureDelay);
// SMS mode
if(programMode == 0) {
length = m_pRootItem->property("videoFrames").toInt();
}
// Continuous TL mode
else {
int shootingHours = m_pRootItem->property("shootingHours").toInt();
int shootingMinutes = m_pRootItem->property("shootingMinutes").toInt();
int shootingSecs = m_pRootItem->property("shootingSecs").toInt();
length = shootingHours * 3600 + shootingMinutes * 60 + shootingSecs;
length *= 1000;
}
}
// Continuous video mode
else {
float videoLengthSeconds = m_pRootItem->property("videoLengthSeconds").toFloat();
int videoLengthMinutes = m_pRootItem->property("videoLengthMinutes").toInt();
length = videoLengthMinutes * 60 + (int)videoLengthSeconds;
length += (int)(videoLengthSeconds - (int)videoLengthSeconds);
length *= 1000; //to msecs
}
// Assign motor parameters
for(unsigned char i = 1; i <= 3; ++i) {
motion m = m_pMotorsModule->motorMotion(controller.currentDeviceAddress(), i);
unsigned leadIn = unsigned(qRound(length * m.leadIn));
unsigned leadOut = unsigned(qRound(length * m.leadOut));
unsigned accel = unsigned(qRound(length * m.acceleration));
unsigned decel = unsigned(qRound(length * m.deceleration));
// This ensures enforces travel will never be off by a frame in SMS due to rounding
unsigned travel = length - (leadIn + leadOut);
controller.setLeadInShots((unsigned char)i, leadIn);
controller.setLeadOutShots((unsigned char)i, leadOut);
controller.setProgramAcceleration((unsigned char)i, accel);
controller.setProgramDeceleration((unsigned char)i, decel);
controller.setTravelTime((unsigned char)i, travel);
}
controller.startPlannedMove();
m_progressTimer.start();
}
void mainWindow::stopProgramClicked() {
m_progressTimer.stop();
m_movementTimer.stop();
m_pRootItem->setProperty("programStatus", 0);
m_pRootItem->setProperty("progressFormVisible", false);
m_pRootItem->setProperty("motorsOnStartPositions", false);
controller.stopPlannedMove();
for(unsigned char i = 1; i <= 3; ++i)
controller.setMicroStepValue(i, controller.motorMicrostep(i));
}
void mainWindow::pauseProgramClicked() {
m_progressTimer.stop();
m_pRootItem->setProperty("programStatus", 2);
controller.pausePlannedMove();
}
void mainWindow::goToProgramStartClicked() {
controller.setJoystickMode(false);
controller.sendMotorToStartPoint(1);
controller.sendMotorToStartPoint(2);
controller.sendMotorToStartPoint(3);
m_pRootItem->setProperty("progressFormVisible", true);
m_pRootItem->setProperty("programStatus", 3);
m_movementTimer.start();
}
void mainWindow::portsRescanClicked() {
m_pPortsModel->clear();
QList<QSerialPortInfo> ports = QSerialPortInfo::availablePorts();
foreach(QSerialPortInfo port, ports) {
QString portName = port.portName();
m_pPortsModel->appendRow(new comPortItem(portName, this));
}
}
void mainWindow::connectToDevices() {
if(!controller.openPort(m_portName)) {
qDebug()<<"couldn't open device";
return;
}
int controllersCount = m_pRootItem->property("controllersCount").toInt();
for(unsigned char i = 0; i < controllersCount; ++i) {
controller.setDeviceAddress(i + 3);
initController();
controller.powerSaveStatus();
controller.motorsStatus();
}
controller.setAction("init");
}
void mainWindow::movementCheckRequest() {
controller.motorsRunning();
}
void mainWindow::testControllerFinished(const QByteArray &data) {
controller.assignAddress((unsigned char)m_controllerAddress);
controller.requestClosePort();
}
void mainWindow::movementCheckFinished(const QByteArray &data) {
if(!data.size()) {
qDebug()<<"running state data is empty";
return;
}
unsigned char ret = data[1];
m_pRootItem->setProperty("motorsOnStartPositions", ret == 0);
if(ret == 0 && m_pRootItem->property("programStatus").toInt() != 0) {
m_pRootItem->setProperty("progressFormVisible", false);
m_pRootItem->setProperty("programStatus", 0);
m_movementTimer.stop();
}
}
void mainWindow::connectToPortClicked(const QString &portName, const QString &address) {
controller.setDeviceAddress((unsigned char)address.toInt());
if(controller.openPort(portName))
controller.firmwareVersion();
}
void mainWindow::programProgressFinished(const QByteArray &data) {
if(!data.size()) {
qDebug()<<"program progress data is empty";
return;
}
unsigned char progress = data[1];
qDebug()<<"progress:"<<progress;
static unsigned char prev = 0;
if(progress == 100 || prev > progress) {
qDebug()<<"program finished";
m_progressTimer.stop();
m_movementTimer.stop();
m_pRootItem->setProperty("motorsOnStartPositions", false);
m_pRootItem->setProperty("progressFormVisible", false);
m_pRootItem->setProperty("programStatus", 0);
prev = 0;
for(unsigned char i = 1; i <= 3; ++i)
controller.setMicroStepValue(i, controller.motorMicrostep(i));
} else {
prev = progress;
}
qDebug()<<"progress:"<<progress;
m_pRootItem->setProperty("programProgress", progress);
}
void mainWindow::powerSaveStatusFinished(const QByteArray &data) {
if(!data.size()) {
qDebug()<<"power status data is empty";
return;
}
unsigned char ret = data[1];
m_pRootItem->setProperty("motor1PowerSave", (ret & 1) != 0);
m_pRootItem->setProperty("motor2PowerSave", (ret & 2) != 0);
m_pRootItem->setProperty("motor3PowerSave", (ret & 4) != 0);
}
void mainWindow::assignAddressRequest(const QString &portName, int address) {
qDebug()<<"portName:"<<portName<<"address:"<<address;
m_portName = portName;
m_controllerAddress = address;
controller.setDeviceAddress((unsigned char)address);
if(controller.openPort(portName))
controller.testController();
else qDebug()<<"port not opened";
}