Debian Codegraph @

Plotter.cpp

raw symbols SCIP
1 /***************************************************************************
2 * This file is part of Robocut. *
3 * Copyright (C) 2010 Tim Hutt <[email protected]> *
4 * Copyright (C) 2010 Markus Schulz <[email protected]> *
5 * *
6 * This program is free software; you can redistribute it and/or modify *
7 * it under the terms of the GNU General Public License as published by *
8 * the Free Software Foundation; either version 3 of the License, or *
9 * (at your option) any later version. *
10 * *
11 * This program is distributed in the hope that it will be useful, *
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
14 * GNU General Public License for more details. *
15 * *
16 * You should have received a copy of the GNU General Public License *
17 * along with this program; if not, write to the *
18 * Free Software Foundation, Inc., *
19 * 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA. *
20 ***************************************************************************/
21
22 #include "Plotter.h"
23 #include "ProgramOptions.h"
24
25 #include <libusb-1.0/libusb.h>
26
27 int VENDOR_ID = ProgramOptions::Instance().getVendorUSB_ID();
28 int PRODUCT_ID = ProgramOptions::Instance().getProductUSB_ID();
29
30 #include <iostream>
31 #include <cmath>
32
33
34 string UsbError(int e)
35 {
36 switch (e)
37 {
38 case LIBUSB_SUCCESS:
39 return "Success (internal error)";
40 case LIBUSB_ERROR_IO:
41 return "I/O error";
42 case LIBUSB_ERROR_INVALID_PARAM:
43 return "Invalid parameter";
44 case LIBUSB_ERROR_ACCESS:
45 return "Access denied";
46 case LIBUSB_ERROR_NO_DEVICE:
47 return "No device";
48 case LIBUSB_ERROR_NOT_FOUND:
49 return "Entity not found";
50 case LIBUSB_ERROR_BUSY:
51 return "Resource busy";
52 case LIBUSB_ERROR_TIMEOUT:
53 return "Timeout";
54 case LIBUSB_ERROR_OVERFLOW:
55 return "Overflow";
56 case LIBUSB_ERROR_PIPE:
57 return "Pipe error";
58 case LIBUSB_ERROR_INTERRUPTED:
59 return "Syscall interrupted";
60 case LIBUSB_ERROR_NO_MEM:
61 return "Insufficient memory";
62 case LIBUSB_ERROR_NOT_SUPPORTED:
63 return "Operation not supported";
64 case LIBUSB_ERROR_OTHER:
65 return "Other error";
66 }
67 return "Unknown error";
68 }
69
70 // Send some data to the USB device.
71 Error UsbSend(libusb_device_handle* handle, const string& s, int timeout = 0)
72 {
73 // Send in 4096 byte chunks. Not sure where I got this from, I'm not sure it is actually necessary.
74 for (unsigned int i = 0; i < s.length(); i += 4096)
75 {
76 string data = s.substr(i, 4096);
77 int transferred;
78 unsigned char endpoint = '\x01';
79 int ret = libusb_bulk_transfer(handle, endpoint, reinterpret_cast<unsigned char*>(const_cast<char*>(data.c_str())),
80 data.length(), &transferred, timeout);
81 if (ret != 0)
82 {
83 cerr << "Error writing to device: " << UsbError(ret) << endl;
84 return Error("Error writing to device: " + UsbError(ret));
85 }
86 if ((unsigned int)transferred != data.length())
87 {
88 cerr << "Warning, some data not transferred correctly." << endl;
89 return Error("Some data not transferred. Attempted: " + ItoS(data.length()) + " Transferred: " + ItoS(transferred));
90 }
91 }
92 return Success;
93 }
94
95 // Receive some data up to the maximum packet size of 64. Because we use the packet size
96 // there can never be overflows, and 64 is ok because none of the craft robo responses are
97 // anywhere near that long.
98 Error UsbReceive(libusb_device_handle* handle, string& s, int timeout = 0)
99 {
100 s.clear();
101
102 // A buffer that is one packet long.
103 const int PacketSize = 64;
104 unsigned char buffer[PacketSize];
105 int transferred = 0;
106 unsigned char endpoint = '\x82';
107 int ret = libusb_bulk_transfer(handle, endpoint, buffer, PacketSize, &transferred, timeout);
108 if (ret != 0) // But it could be a timout.
109 {
110 cerr << "Error reading from device: " << UsbError(ret) << endl;
111 return Error("Error reading from device: " + UsbError(ret));
112 }
113
114 s = string(reinterpret_cast<char*>(buffer), transferred);
115 return Success;
116 }
117
118 // Send a control message
119 //Error UsbControl(libusb_device_handle* handle, ...)
120 //{
121 // libusb_control_transfer(handle,
122 // uint8_t request_type, uint8_t request, uint16_t value, uint16_t index,
123 // unsigned char *data, uint16_t length, unsigned int timeout);
124 //}
125
126 libusb_device_handle *UsbOpen(struct cutter_id *id)
127 {
128 libusb_device** list;
129 ssize_t cnt = libusb_get_device_list(NULL, &list);
130 if (cnt < 1)
131 {
132 id->msg = Error("Couldn't get usb device list.");
133 return NULL;
134 }
135
136 int err = 0;
137
138 libusb_device* found = NULL;
139 for (ssize_t i = 0; i < cnt; ++i)
140 {
141 libusb_device* device = list[i];
142 libusb_device_descriptor desc;
143 libusb_get_device_descriptor(device, &desc);
144 // I don't want to be more specific than this really.
145 if ((desc.idVendor == VENDOR_ID || desc.idVendor == VENDOR_ID_GRAPHTEC) &&
146 (desc.idProduct == PRODUCT_ID ||
147 desc.idProduct == PRODUCT_ID_CC200_20 ||
148 desc.idProduct == PRODUCT_ID_CC300_20 ||
149 desc.idProduct == PRODUCT_ID_SILHOUETTE_SD_1 ||
150 desc.idProduct == PRODUCT_ID_SILHOUETTE_SD_2 ||
151 desc.idProduct == PRODUCT_ID_SILHOUETTE_CAMEO ||
152 desc.idProduct == PRODUCT_ID_SILHOUETTE_PORTRAIT
153 )
154 )
155 {
156 // Just use the first one. Who has two?!
157 found = device;
158 id->usb_product_id = desc.idProduct;
159 id->usb_vendor_id = desc.idVendor;
160 break;
161 }
162 }
163
164 if (!found)
165 {
166 libusb_free_device_list(list, 1);
167 id->msg = Error("Couldn't find Craft Robo 2 (USB device with Vendor ID with USB VendorID 0b4d and ProductID 110a). Is it connected to the system and powered on?");
168 return NULL;
169 }
170
171 libusb_device_handle* handle;
172
173 err = libusb_open(found, &handle);
174 if (err != 0)
175 {
176 libusb_free_device_list(list, 1);
177 id->msg = Error("Error accessing Craft Robo 2: " + UsbError(err) + ". Do you have permission (on Linux make sure you are in the group 'lp').");
178 return NULL;
179 }
180
181 libusb_free_device_list(list, 1);
182 id->msg = "Ready";
183 return handle;
184 }
185
186 // caller can use UsbSend() afterwards, and should
187 // finally do libusb_release_interface(handle, 0); libusb_close(handle);
188 libusb_device_handle *UsbInit(struct cutter_id *id)
189 {
190 // Now do stuff with the handle.
191 int r = 0;
192
193 libusb_device_handle* handle = UsbOpen(id);
194 if (!handle) return NULL;
195
196 if (libusb_kernel_driver_active(handle, 0) == 1)
197 {
198 r = libusb_detach_kernel_driver(handle, 0);
199 if (r != 0)
200 {
201 libusb_close(handle);
202 id->msg = Error("Error detaching kernel USB driver: " + UsbError(r));
203 return NULL;
204 }
205 }
206
207 r = libusb_reset_device(handle);
208 if (r != 0)
209 {
210 libusb_close(handle);
211 id->msg = Error("Error resetting device: " + UsbError(r));
212 return NULL;
213 }
214
215 cout << "Selecting configuration." << endl;
216 r = libusb_set_configuration(handle, 1);
217 if (r < 0)
218 {
219 libusb_close(handle);
220 id->msg = Error("Error setting USB configuration: " + UsbError(r));
221 return NULL;
222 }
223
224
225 cout << "Claiming main control interface." << endl;
226 r = libusb_claim_interface(handle, 0);
227 if (r < 0)
228 {
229 libusb_close(handle);
230 id->msg = Error("Error claiming USB interface: " + UsbError(r));
231 return NULL;
232 }
233
234 cout << "Setting alt interface." << endl;
235 r = libusb_set_interface_alt_setting(handle, 0, 0); // Probably not really necessary.
236 if (r < 0)
237 {
238 libusb_close(handle);
239 id->msg = Error("Error setting alternate USB interface: " + UsbError(r));
240 return NULL;
241 }
242
243 cout << "Initialisation successful." << endl;
244 return handle;
245 }
246
247 struct cutter_id *Identify()
248 {
249 static struct cutter_id id = { "?", 0, 0 };
250 libusb_device_handle *handle;
251
252 if (1)
253 {
254 handle = UsbOpen(&id);
255 if (handle)
256 libusb_close(handle);
257 else
258 id.msg = "no device found";
259 }
260 else
261 {
262 id.msg = Error("Cannot Identify while cut thread is running");
263 }
264 return &id;
265 }
266
267
268 QList<QPolygonF> Transform_Silhouette_Cameo(QList<QPolygonF> cuts, double *mediawidth, double *mediaheight)
269 {
270 const double devicewidth = 300; // cameo is 12inch aka 300mm wide
271 double w;
272 double h = *mediaheight;
273
274 cout << "Transform_Silhouette_Cameo " << *mediawidth << "," << *mediaheight << endl;
275 if (0)
276 {
277 w = *mediawidth;
278 cout << "Paper right aligned" << endl;
279 }
280 else
281 {
282 w = devicewidth;
283 // adjust the used media area, so that the hardware does not clip.
284 *mediawidth = devicewidth;
285 cout << "Paper left aligned" << endl;
286 }
287
288
289 // flip it around 180 deg, and go backwards through the path list.
290 QList<QPolygonF> paths;
291 for (int i = cuts.size()-1; i >= 0; i--)
292 {
293 QPolygonF poly;
294 for (int j = 0; j < cuts[i].size(); j++)
295 {
296 // QPolygonF is a QList<QPointF>
297 double x = cuts[i][j].x();
298 double y = cuts[i][j].y();
299 poly << QPointF(w-x, h-y);
300 }
301 paths << poly;
302 }
303 return paths;
304 }
305
306 Error Cut(CutParams p)
307 {
308 VENDOR_ID = ProgramOptions::Instance().getVendorUSB_ID();
309 PRODUCT_ID = ProgramOptions::Instance().getProductUSB_ID();
310
311 cout << "Cutting... VENDOR_ID : " << VENDOR_ID << " PRODUCT_ID: " << PRODUCT_ID
312 << " mediawidth: " << p.mediawidth << " mediaheight: " << p.mediaheight
313 << "media: " << p.media << " speed: " << p.speed << " pressure: " << p.pressure
314 << " trackenhancing: " << p.trackenhancing << " regmark: " << p.regmark
315 << " regsearch:" << p.regsearch <<" regwidth:" << p.regwidth << " reglength: " << p.regheight << endl;
316
317 if (p.media < 100 || p.media > 300)
318 p.media = 300;
319 if (p.speed < 1)
320 p.speed = 1;
321 if (p.speed > 10)
322 p.speed = 10;
323 if (p.pressure < 1)
324 p.pressure = 1;
325 if (p.pressure > 33)
326 p.pressure = 33;
327
328 // how can VENDOR_ID / PRODUCT_ID report the correct values abve???
329 struct cutter_id id = { "?", 0, 0 };
330 libusb_device_handle* handle = UsbInit(&id);
331 if (id.usb_vendor_id == VENDOR_ID_GRAPHTEC &&
332 id.usb_product_id == PRODUCT_ID_SILHOUETTE_CAMEO)
333 {
334 // should this also transform the regwidth regheigth or not?
335 p.cuts = Transform_Silhouette_Cameo(p.cuts, &p.mediawidth, &p.mediaheight);
336 }
337
338 // TODO: Use exceptions.
339
340 Error e = Success;
341 string resp;
342
343 // Initialise plotter.
344 e = UsbSend(handle, "\x1b\x04");
345 if (!e) goto error;
346
347 // Status request.
348 e = UsbSend(handle, "\x1b\x05");
349 if (!e) goto error;
350
351 e = UsbReceive(handle, resp, 5000);
352 if (!e) goto error;
353
354 if (resp != "0\x03") // 0 = Ready. 1 = Moving. 2 = Nothing loaded. " " = ??
355 {
356 if (resp == "1\x03")
357 e = Error("Moving, please try again.");
358 else if (resp == "2\x03")
359 e = Error("Empty tray, please load media."); // Silhouette Cameo
360 else
361 e = Error("Invalid response from plotter: " + resp);
362 goto error;
363 }
364
365 // Home the cutter.
366 e = UsbSend(handle, "TT\x03");
367 if (!e) goto error;
368
369 // Query version.
370 e = UsbSend(handle, "FG\x03");
371 if (!e) goto error;
372
373 // Receive the firmware version.
374 e = UsbReceive(handle, resp, 10000); // Large timeout because the plotter moves.
375 if (!e) goto error;
376 // Don't really care about this.
377 // if (resp.length() != 10)
378 // {
379 // e = Error("Version error: " + version);
380 // goto error;
381 // }
382
383 e = UsbSend(handle, "FW" + ItoS(p.media) + "\x03");
384 if (!e) goto error;
385
386 e = UsbSend(handle, "!" + ItoS(p.speed) + "\x03");
387 if (!e) goto error;
388
389 e = UsbSend(handle, "FX" + ItoS(p.pressure) + "\x03");
390 if (!e) goto error;
391
392 // I think this sets the distance from the position of the plotter
393 // head to the actual cutting point, maybe in 0.1 mms (todo: Measure blade).
394 // It is 0 for the pen, 18 for cutting.
395 // C possible stands for curvature. Not that any of the other letters make sense...
396 e = UsbSend(handle, "FC" + ItoS(p.media == 113 ? 0 : 18) + "\x03");
397 if (!e) goto error;
398
399 e = UsbSend(handle, "FY" + ItoS(p.trackenhancing ? 0 : 1) + "\x03");
400 if (!e) goto error;
401
402 // Set to portrait. FN1 does landscape but it's easier just to rotate the image.
403 e = UsbSend(handle, "FN0\x03");
404 if (!e) goto error;
405
406 e = UsbSend(handle, "FE0\x03"); // No idea what this does.
407 if (!e) goto error;
408
409 e = UsbSend(handle, "TB71\x03"); // Again, no idea. Maybe something to do with registration marks?
410 if (!e) goto error;
411
412 e = UsbReceive(handle, resp, 10000); // Allow 10s. Seems reasonable.
413 if (!e) goto error;
414
415 if (resp != " 0, 0\x03")
416 {
417 e = Error("Invalid response from plotter.");
418 goto error;
419 }
420
421
422 // Begin page definition.
423 e = UsbSend(handle, "FA\x03");
424 if (!e) goto error;
425
426
427 // Block for all the "jump to error crosses initialization" errors. Really need to use exceptions!
428 {
429
430 // Page size: height,width in 20ths of a mm minus a margin. This is for A4. TODO: Find maximum and use that.
431 stringstream page;
432
433 int width = lroundl(p.mediawidth * 20.0);
434 int height = lroundl(p.mediaheight * 20.0);
435
436 int margintop = ProgramOptions::Instance().getMarginTop();
437 int marginright = ProgramOptions::Instance().getMarginRight();
438
439 e = UsbSend(handle, "FU" + ItoS(height - margintop) + "," + ItoS(width - marginright) + "\x03");
440 if (!e) goto error;
441 e = UsbSend(handle, "FM1\x03"); // ?
442 if (!e) goto error;
443
444 if (p.regmark)
445 {
446 stringstream regmarkstr;
447 regmarkstr.precision(0);
448 string searchregchar = "23,";
449 int regw = lroundl(p.regwidth * 20.0);
450 int regl = lroundl(p.regheight * 20.0);
451 e = UsbSend(handle, "TB50,381\x03"); //only with registration (it was TB50,1) ???
452 if (!e) goto error;
453
454 if (p.regsearch)
455 searchregchar ="123,";
456
457 regmarkstr << "TB99\x03TB55,1\x03TB" + searchregchar + ItoS(regw) + "," + ItoS(regl) + "\x03";
458
459 cout << "Registration mark string: " << regmarkstr.str() << endl;
460
461 e = UsbSend(handle, regmarkstr.str()); //registration mark test /1-2: 180.0mm / 1-3: 230.0mm (origin 15mmx20mm)
462 if (!e) goto error;
463
464 e = UsbSend(handle, "FQ5\x03"); // only with registration ???
465 if (!e) goto error;
466
467 e = UsbReceive(handle, resp, 40000); // Allow 20s for reply...
468 if (!e) goto error;
469 if (resp != " 0, 0\x03")
470 {
471 cout << resp << endl;
472 e = Error("Couldn't find registration marks.");
473 goto error;
474 }
475 // Looks like if the reg marks work it gets 3 messages back (if it fails it times out because it only gets the first message)
476 e = UsbReceive(handle, resp, 40000); // Allow 20s for reply...
477 if (!e) goto error;
478 if (resp != " 0\x03")
479 {
480 cout << resp << endl;
481 e = Error("Couldn't find registration marks.");
482 goto error;
483 }
484
485 e = UsbReceive(handle, resp, 40000); // Allow 20s for reply...
486 if (!e) goto error;
487 if (resp != " 1\x03")
488 {
489 cout << resp << endl;
490 e = Error("Couldn't find registration marks.");
491 goto error;
492 }
493 }
494 else
495 {
496 e = UsbSend(handle, "TB50,1\x03"); // ???
497 if (!e) goto error;
498 }
499
500 if (!e) goto error;
501 // I think this is the feed command. Sometimes it is 5588 - maybe a maximum?
502 e = UsbSend(handle, "FO" + ItoS(height - margintop) + "\x03");
503 if (!e) goto error;
504
505 page.flags(ios::fixed);
506 page.precision(0);
507 page << "&100,100,100,\\0,0,Z" << ItoS(width) << "," << ItoS(height) << ",L0";
508 for (int i = 0; i < p.cuts.size(); ++i)
509 {
510 if (p.cuts[i].size() < 2)
511 continue;
512
513 double x = p.cuts[i][0].x()*20.0;
514 double y = p.cuts[i][0].y()*20.0;
515
516 double xorigin = ProgramOptions::Instance().getRegOriginWidthMM();
517 double yorigin = ProgramOptions::Instance().getRegOriginHeightMM();
518
519 if (p.regmark)
520 {
521 x = x - (xorigin*20.0);
522 y = y + (yorigin*20.0);
523 }
524
525 // TODO: Also do this in the UI and warn the user about it.
526 if (x < 0.0) x = 0.0;
527 if (x > width) x = width;
528
529 if (y < 0.0) y = 0.0;
530 if (y > height) y = height;
531
532 page << ",M" << x << "," << height-y;
533 for (int j = 1; j < p.cuts[i].size(); ++j)
534 {
535 x = p.cuts[i][j].x()*20.0;
536 y = p.cuts[i][j].y()*20.0;
537
538 if (p.regmark)
539 {
540 x = x - (xorigin*20.0);
541 y = y + (yorigin*20.0);
542 }
543
544 bool draw = true;
545 if (x <= 0.0)
546 {
547 x = 0.0;
548 draw = false;
549 }
550 if (x >= width)
551 {
552 x = width;
553 draw = false;
554 }
555 if (y <= 0.0)
556 {
557 y = 0.0;
558 draw = false;
559 }
560 if (y >= height)
561 {
562 y = height;
563 draw = false;
564 }
565
566 if (draw) page << ",D" << x << "," << height-y;
567 else page << ",M" << x << "," << height-y; // if outside the range just move
568 }
569 }
570
571
572 page << "&1,1,1,TB50,0\x03"; // TB maybe .. ah I dunno. Need to experiment. No idea what &1,1,1 does either.
573
574 // cout << page.str() << endl;
575
576 e = UsbSend(handle, page.str());
577 if (!e) goto error;
578 }
579
580 // Feed the page out.
581 e = UsbSend(handle, "FO0\x03");
582 if (!e) goto error;
583
584 // Halt?
585 e = UsbSend(handle, "H,");
586 if (!e) goto error;
587
588
589 // Don't really care about the results of these!
590 libusb_release_interface(handle, 0);
591 libusb_close(handle);
592
593 return Success;
594
595 error: // Hey, this is basically C and I can't be bothered to properly C++-ify it. TODO: Use exceptions.
596 cout << "Error: " << e << endl;
597 libusb_release_interface(handle, 0);
598 libusb_close(handle);
599 return e;
600
601 }
602