fork download
  1. //Project: V-plotter
  2. //Homepage: www.HomoFaciens.de
  3. //Author Norbert Heinz
  4. //Version: 0.1
  5. //Creation date: 24.06.2015
  6. //This program is free software you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation version 3 of the License.
  7. //This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
  8. //For a copy of the GNU General Public License see http://w...content-available-to-author-only...u.org/licenses/
  9. //
  10. //compile with gcc v-plotter.c -o v-plotter -I/usr/local/include -L/usr/local/lib -lwiringPi -lm
  11. //For details see:
  12. //http://w...content-available-to-author-only...s.de/technics-machines-v-plotter_en_navion.htm
  13.  
  14. #include <stdio.h>
  15. #include <termios.h>
  16. #include <sys/ioctl.h>
  17. #include <stdlib.h>
  18. #include <string.h>
  19. #include <time.h>
  20. #include <sys/time.h>
  21. #include <dirent.h>
  22. #include <math.h>
  23. #include <wiringPi.h>
  24. #include <unistd.h>
  25.  
  26.  
  27. #define PI 3.1415927
  28. #define SERVOUP 10
  29. #define SERVODOWN 20
  30. #define LEFT_STEPPER01 11
  31. #define LEFT_STEPPER02 10
  32.  
  33. #define LEFT_STEPPER03 6
  34. #define LEFT_STEPPER04 14
  35.  
  36.  
  37. #define RIGHT_STEPPER01 2
  38. #define RIGHT_STEPPER02 3
  39.  
  40. #define RIGHT_STEPPER03 12
  41. #define RIGHT_STEPPER04 13
  42.  
  43. #define STEP_PAUSE 1500
  44.  
  45. #define STEP_MAX 220.0
  46.  
  47. #define Z_SERVO 0
  48.  
  49. #define BUFFERSIZE 120
  50.  
  51. //Lengths given in millimiters
  52. #define BASELENGTH 845
  53. #define CORDLENGTH_LEFT 220
  54. #define CORDLENGTH_RIGHT 667
  55.  
  56. //#define BASELENGTH 840
  57. //#define CORDLENGTH_LEFT 266
  58. //#define CORDLENGTH_RIGHT 685
  59.  
  60. //Correction for errors caused by flexibility of the cord 0.0 = OFF (experimental)
  61. #define CORDFLEXFACTOR 0.0f
  62.  
  63. int MaxRows = 24;
  64. int MaxCols = 80;
  65. int MessageX = 1;
  66. int MessageY = 24;
  67. unsigned char MoveBuffer[BUFFERSIZE];
  68. long currentX = 0, currentY = 0;
  69. long CordLengthLeft = 0;
  70. long CordLengthRight = 0;
  71. long BaseLength = 0;
  72. long X0, Y0;
  73. int FilesFound = 0;
  74. int currentPlotDown = 0;
  75. int BoldLineWidth = 0;
  76. int BoldLineGap = 0;
  77. long BoldLineX = 0, BoldLineY = 0;
  78.  
  79. int StepX = 0;
  80. int StepY = 0;
  81.  
  82. //Steps per mm (with gear)
  83. double StepsPermm = 2000.0 / 98.0;
  84.  
  85. //Steps per mm (without gear)
  86. //double StepsPermm = 2000.0 / 199.0;
  87.  
  88. char PicturePath[1000];
  89.  
  90.  
  91. //+++++++++++++++++++++++ Start gotoxy ++++++++++++++++++++++++++
  92. //Thanks to 'Stack Overflow', found on http://w...content-available-to-author-only...b.com/software-development/c/code/216326
  93. int gotoxy(int x, int y) {
  94. char essq[100]; // String variable to hold the escape sequence
  95. char xstr[100]; // Strings to hold the x and y coordinates
  96. char ystr[100]; // Escape sequences must be built with characters
  97.  
  98. //Convert the screen coordinates to strings.
  99. sprintf(xstr, "%d", x);
  100. sprintf(ystr, "%d", y);
  101.  
  102. //Build the escape sequence (vertical move).
  103. essq[0] = '\0';
  104. strcat(essq, "\033[");
  105. strcat(essq, ystr);
  106.  
  107. //Described in man terminfo as vpa=\E[%p1%dd. Vertical position absolute.
  108. strcat(essq, "d");
  109.  
  110. //Horizontal move. Horizontal position absolute
  111. strcat(essq, "\033[");
  112. strcat(essq, xstr);
  113. // Described in man terminfo as hpa=\E[%p1%dG
  114. strcat(essq, "G");
  115.  
  116. //Execute the escape sequence. This will move the cursor to x, y
  117. printf("%s", essq);
  118. return 0;
  119. }
  120. //------------------------ End gotoxy ----------------------------------
  121.  
  122. //+++++++++++++++++++++++ Start clrscr ++++++++++++++++++++++++++
  123. void clrscr(int StartRow, int EndRow) {
  124. int i, i2;
  125.  
  126. if (EndRow < StartRow){
  127. i = EndRow;
  128. EndRow = StartRow;
  129. StartRow = i;
  130. }
  131. gotoxy(1, StartRow);
  132. for (i = 0; i <= EndRow - StartRow; i++){
  133. for(i2 = 0; i2 < MaxCols; i2++){
  134. printf(" ");
  135. }
  136. printf("\n");
  137. }
  138. }
  139. //----------------------- End clrscr ----------------------------
  140.  
  141. //+++++++++++++++++++++++ Start kbhit ++++++++++++++++++++++++++++++++++
  142. //Thanks to Undertech Blog, http://w...content-available-to-author-only...c.de/blog/2009/05/kbhit_und_getch_fur_linux.html
  143. int kbhit(void) {
  144.  
  145. struct termios term, oterm;
  146. int fd = 0;
  147. int c = 0;
  148.  
  149. tcgetattr(fd, &oterm);
  150. memcpy(&term, &oterm, sizeof(term));
  151. term.c_lflag = term.c_lflag & (!ICANON);
  152. term.c_cc[VMIN] = 0;
  153. term.c_cc[VTIME] = 1;
  154. tcsetattr(fd, TCSANOW, &term);
  155. c = getchar();
  156. tcsetattr(fd, TCSANOW, &oterm);
  157. if (c != -1)
  158. ungetc(c, stdin);
  159.  
  160. return ((c != -1) ? 1 : 0);
  161.  
  162. }
  163. //------------------------ End kbhit -----------------------------------
  164.  
  165. //+++++++++++++++++++++++ Start getch ++++++++++++++++++++++++++++++++++
  166. //Thanks to Undertech Blog, http://w...content-available-to-author-only...c.de/blog/2009/05/kbhit_und_getch_fur_linux.html
  167. int getch(){
  168. static int ch = -1, fd = 0;
  169. struct termios new, old;
  170.  
  171. fd = fileno(stdin);
  172. tcgetattr(fd, &old);
  173. new = old;
  174. new.c_lflag &= ~(ICANON|ECHO);
  175. tcsetattr(fd, TCSANOW, &new);
  176. ch = getchar();
  177. tcsetattr(fd, TCSANOW, &old);
  178.  
  179. // printf("ch=%d ", ch);
  180.  
  181. return ch;
  182. }
  183. //------------------------ End getch -----------------------------------
  184.  
  185. //++++++++++++++++++++++ Start MessageText +++++++++++++++++++++++++++++
  186. void MessageText(char *message, int x, int y, int alignment){
  187. int i;
  188. char TextLine[300];
  189.  
  190. clrscr(y, y);
  191. gotoxy (x, y);
  192.  
  193. TextLine[0] = '\0';
  194. if(alignment == 1){
  195. for(i=0; i < (MaxCols - strlen(message)) / 2 ; i++){
  196. strcat(TextLine, " ");
  197. }
  198. }
  199. strcat(TextLine, message);
  200.  
  201. printf("%s\n", TextLine);
  202. }
  203. //-------------------------- End MessageText ---------------------------
  204.  
  205. //++++++++++++++++++++++ Start PrintRow ++++++++++++++++++++++++++++++++
  206. void PrintRow(char character, int y){
  207. int i;
  208. gotoxy (1, y);
  209. for(i=0; i<MaxCols;i++){
  210. printf("%c", character);
  211. }
  212. }
  213. //-------------------------- End PrintRow ------------------------------
  214.  
  215. //+++++++++++++++++++++++++ ErrorText +++++++++++++++++++++++++++++
  216. void ErrorText(char *message){
  217. clrscr(MessageY + 2, MessageY + 2);
  218. gotoxy (1, MessageY + 2);
  219. printf("Last error: %s", message);
  220. }
  221. //----------------------------- ErrorText ---------------------------
  222.  
  223. //+++++++++++++++++++++++++ PrintMenue_01 ++++++++++++++++++++++++++++++
  224. void PrintMenue_01(char * PlotFile, double scale, double width, double height, long MoveLength){
  225. char TextLine[300];
  226.  
  227. clrscr(1, MessageY-2);
  228. MessageText("*** Main menu plotter ***", 1, 1, 1);
  229. sprintf(TextLine, "M - toggle move length, current value = %ld step(s)", MoveLength);
  230. MessageText(TextLine, 10, 3, 0);
  231. MessageText("Cursor right - move plotter in positive X direction", 10, 4, 0);
  232. MessageText("Cursor left - move plotter in negative X direction", 10, 5, 0);
  233. MessageText("Cursor up - move plotter in positive Y direction", 10, 6, 0);
  234. MessageText("Cursor down - move plotter in negative Y direction", 10, 7, 0);
  235. MessageText("Page up - lift pen", 10, 8, 0);
  236. MessageText("Page down - touch down pen", 10, 9, 0);
  237. sprintf(TextLine, "F - choose file. Current file = \"%s\"", PlotFile);
  238. MessageText(TextLine, 10, 10, 0);
  239. MessageText("0 - move plotter to 0/0", 10, 11, 0);
  240. sprintf(TextLine, "S - Scale set to = %0.4f. W = %0.2fcm, H = %0.2fcm", scale, width * scale / 1000.0, height * scale / 1000.0);
  241. MessageText(TextLine, 10, 12, 0);
  242. sprintf(TextLine, "B - Bold line = %d steps", BoldLineWidth);
  243. MessageText(TextLine, 10, 13, 0);
  244. MessageText("P - plot file", 10, 14, 0);
  245.  
  246. MessageText("Esc - leave program", 10, 16, 0);
  247.  
  248. }
  249. //------------------------- PrintMenue_01 ------------------------------
  250.  
  251. //+++++++++++++++++++++++++ PrintMenue_02 ++++++++++++++++++++++++++++++
  252. char *PrintMenue_02(int StartRow, int selected){
  253. char TextLine[300];
  254. char FilePattern[5];
  255. char OpenDirName[1000];
  256. static char FileName[101];
  257. DIR *pDIR;
  258. struct dirent *pDirEnt;
  259. int i = 0;
  260. int Discard = 0;
  261.  
  262. clrscr(1, MessageY-2);
  263. MessageText("*** Choose plotter file ***", 1, 1, 1);
  264.  
  265. strcpy(OpenDirName, PicturePath);
  266.  
  267.  
  268. pDIR = opendir(OpenDirName);
  269. if ( pDIR == NULL ) {
  270. sprintf(TextLine, "Could not open directory '%s'!", OpenDirName);
  271. MessageText(TextLine, 1, 4, 1);
  272. getch();
  273. return( "" );
  274. }
  275.  
  276. FilesFound = 0;
  277. pDirEnt = readdir( pDIR );
  278. while ( pDirEnt != NULL && i < 10) {
  279. if(strlen(pDirEnt->d_name) > 4){
  280. if(memcmp(pDirEnt->d_name + strlen(pDirEnt->d_name)-4, ".svg",4) == 0){
  281. FilesFound++;
  282. if(Discard >= StartRow){
  283. if(i + StartRow == selected){
  284. sprintf(TextLine, ">%s<", pDirEnt->d_name);
  285. strcpy(FileName, pDirEnt->d_name);
  286. }
  287. else{
  288. sprintf(TextLine, " %s ", pDirEnt->d_name);
  289. }
  290. MessageText(TextLine, 1, 3 + i, 0);
  291. i++;
  292. }
  293. Discard++;
  294.  
  295. }
  296. }
  297. pDirEnt = readdir( pDIR );
  298. }
  299.  
  300. gotoxy(MessageX, MessageY + 1);
  301. printf("Choose file using up/down keys and confirm with 'Enter' or press 'Esc' to cancel.");
  302.  
  303.  
  304. return (FileName);
  305. }
  306. //------------------------- PrintMenue_02 ------------------------------
  307.  
  308.  
  309. //+++++++++++++++++++++++++ PrintMenue_03 ++++++++++++++++++++++++++++++
  310. void PrintMenue_03(char *FullFileName, long NumberOfLines, long CurrentLine, long CurrentX, long CurrentY, long StartTime){
  311. char TextLine[300];
  312. long CurrentTime, ProcessHours = 0, ProcessMinutes = 0, ProcessSeconds = 0;
  313.  
  314. CurrentTime = time(0);
  315.  
  316. CurrentTime -= StartTime;
  317.  
  318. while (CurrentTime > 3600){
  319. ProcessHours++;
  320. CurrentTime -= 3600;
  321. }
  322. while (CurrentTime > 60){
  323. ProcessMinutes++;
  324. CurrentTime -= 60;
  325. }
  326. ProcessSeconds = CurrentTime;
  327.  
  328. clrscr(1, MessageY - 2);
  329. MessageText("*** Plotting file ***", 1, 1, 1);
  330.  
  331. sprintf(TextLine, "File name: %s", FullFileName);
  332. MessageText(TextLine, 10, 3, 0);
  333. sprintf(TextLine, "Number of lines: %ld", NumberOfLines);
  334. MessageText(TextLine, 10, 4, 0);
  335. sprintf(TextLine, "Current Position(%ld): X = %ld, Y = %ld ", CurrentLine, CurrentX, CurrentY);
  336. MessageText(TextLine, 10, 5, 0);
  337. sprintf(TextLine, "Process time: %02ld:%02ld:%02ld", ProcessHours, ProcessMinutes, ProcessSeconds);
  338. MessageText(TextLine, 10, 6, 0);
  339.  
  340.  
  341. }
  342. //------------------------- PrintMenue_03 ------------------------------
  343.  
  344.  
  345.  
  346. //++++++++++++++++++++++++++++++ MakeStepLeft ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  347. void MakeStepLeft(int direction){
  348. StepX += direction;
  349.  
  350. if(StepX > 3){
  351. StepX = 0;
  352. }
  353. if(StepX < 0){
  354. StepX = 3;
  355. }
  356.  
  357. if(StepX == 0){
  358. digitalWrite(LEFT_STEPPER01, 1);
  359. usleep(STEP_PAUSE);
  360. digitalWrite(LEFT_STEPPER02, 0);
  361. digitalWrite(LEFT_STEPPER03, 0);
  362. digitalWrite(LEFT_STEPPER04, 0);
  363. }
  364. if(StepX == 1){
  365. digitalWrite(LEFT_STEPPER03, 1);
  366. usleep(STEP_PAUSE);
  367. digitalWrite(LEFT_STEPPER01, 0);
  368. digitalWrite(LEFT_STEPPER02, 0);
  369. digitalWrite(LEFT_STEPPER04, 0);
  370. }
  371. if(StepX == 2){
  372. digitalWrite(LEFT_STEPPER02, 1);
  373. usleep(STEP_PAUSE);
  374. digitalWrite(LEFT_STEPPER01, 0);
  375. digitalWrite(LEFT_STEPPER03, 0);
  376. digitalWrite(LEFT_STEPPER04, 0);
  377. }
  378. if(StepX == 3){
  379. digitalWrite(LEFT_STEPPER04, 1);
  380. usleep(STEP_PAUSE);
  381. digitalWrite(LEFT_STEPPER01, 0);
  382. digitalWrite(LEFT_STEPPER02, 0);
  383. digitalWrite(LEFT_STEPPER03, 0);
  384. }
  385.  
  386. usleep(STEP_PAUSE);
  387. }
  388.  
  389. //++++++++++++++++++++++++++++++ MakeStepRight ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  390. void MakeStepRight(int direction){
  391. StepY += direction;
  392.  
  393. if(StepY > 3){
  394. StepY = 0;
  395. }
  396. if(StepY < 0){
  397. StepY = 3;
  398. }
  399.  
  400.  
  401. if(StepY == 0){
  402. digitalWrite(RIGHT_STEPPER01, 1);
  403. usleep(STEP_PAUSE);
  404. digitalWrite(RIGHT_STEPPER02, 0);
  405. digitalWrite(RIGHT_STEPPER03, 0);
  406. digitalWrite(RIGHT_STEPPER04, 0);
  407. }
  408. if(StepY == 1){
  409. digitalWrite(RIGHT_STEPPER03, 1);
  410. usleep(STEP_PAUSE);
  411. digitalWrite(RIGHT_STEPPER01, 0);
  412. digitalWrite(RIGHT_STEPPER02, 0);
  413. digitalWrite(RIGHT_STEPPER04, 0);
  414. }
  415. if(StepY == 2){
  416. digitalWrite(RIGHT_STEPPER02, 1);
  417. usleep(STEP_PAUSE);
  418. digitalWrite(RIGHT_STEPPER01, 0);
  419. digitalWrite(RIGHT_STEPPER03, 0);
  420. digitalWrite(RIGHT_STEPPER04, 0);
  421. }
  422. if(StepY == 3){
  423. digitalWrite(RIGHT_STEPPER04, 1);
  424. usleep(STEP_PAUSE);
  425. digitalWrite(RIGHT_STEPPER01, 0);
  426. digitalWrite(RIGHT_STEPPER02, 0);
  427. digitalWrite(RIGHT_STEPPER03, 0);
  428. }
  429.  
  430. // printf("StepY\n");
  431. usleep(STEP_PAUSE);
  432. }
  433.  
  434.  
  435. //++++++++++++++++++++++++++++++++++++++ moveXY +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  436. void moveXY(long X, long Y){
  437. long newCordLengthLeft;
  438. long newCordLengthRight;
  439. double forceLeft, forceRight;
  440. double deltaCordLeft, deltaCordRight;
  441. double deltaCordLeft0, deltaCordRight0;
  442. double alpha;
  443. char TextLine[1000];
  444.  
  445. //calculate initial stretch of cords (experimental)
  446. alpha = atan((double)(X0) / (double)(Y0));
  447. forceLeft = 1.0 * cos(alpha);
  448. forceRight = 1.0 * sin(alpha);
  449.  
  450. deltaCordLeft0 = (double)(CORDLENGTH_LEFT * StepsPermm) * forceLeft * CORDFLEXFACTOR;
  451. deltaCordRight0 = (double)(CORDLENGTH_RIGHT * StepsPermm) * forceRight * CORDFLEXFACTOR;
  452.  
  453.  
  454. //forces at current coordinates (experimental)
  455. alpha = atan((double)(X + X0) / (double)(Y + Y0));
  456. forceLeft = 1.0 * cos(alpha);
  457. forceRight = 1.0 * sin(alpha);
  458.  
  459. X += X0;
  460. Y += Y0;
  461.  
  462. newCordLengthLeft = sqrt((double)(X * X) + (double)(Y * Y));
  463. newCordLengthRight = sqrt((double)((BaseLength-X) * (BaseLength-X)) + (double)(Y * Y));
  464.  
  465. deltaCordLeft = (double)(newCordLengthLeft) * forceLeft * CORDFLEXFACTOR - deltaCordLeft0;
  466. deltaCordRight = (double)(newCordLengthRight) * forceRight * CORDFLEXFACTOR - deltaCordRight0;
  467.  
  468.  
  469. newCordLengthLeft -= deltaCordLeft;
  470. newCordLengthRight -= deltaCordRight;
  471.  
  472. while(newCordLengthLeft > CordLengthLeft){
  473. MakeStepLeft(1);
  474. CordLengthLeft++;
  475. }
  476. while(newCordLengthLeft < CordLengthLeft){
  477. MakeStepLeft(-1);
  478. CordLengthLeft--;
  479. }
  480.  
  481. while(newCordLengthRight > CordLengthRight){
  482. MakeStepRight(1);
  483. CordLengthRight++;
  484. }
  485. while(newCordLengthRight < CordLengthRight){
  486. MakeStepRight(-1);
  487. CordLengthRight--;
  488. }
  489.  
  490.  
  491. }
  492.  
  493. //++++++++++++++++++++++++++++++++++++++ BoldLinePattern ++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  494. void BoldLinePattern(long X, long Y){
  495. int i;
  496. long xDiff, yDiff;
  497. double alpha = 0.0;
  498. long xCircle, yCircle;
  499.  
  500. xDiff = BoldLineX - X;
  501. yDiff = BoldLineY - Y;
  502.  
  503. if(BoldLineWidth > 0){
  504. if(currentPlotDown == 1){
  505. if(sqrt(xDiff * xDiff + yDiff * yDiff) > BoldLineGap){
  506. BoldLineX = X;
  507. BoldLineY = Y;
  508.  
  509. for(alpha = 0.0; alpha < 2.0 * PI; alpha += PI / 2500.0){
  510. xCircle = cos(alpha) * BoldLineWidth + X;
  511. yCircle = sin(alpha) * BoldLineWidth + Y;
  512. moveXY(xCircle, yCircle);
  513. }
  514. moveXY(X, Y);
  515. }
  516. }//if(currentPlotDown == 1){
  517. }
  518. }
  519.  
  520. //++++++++++++++++++++++++++++++++++++++ CalculateLine ++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  521. int CalculateLine(long moveToX, long moveToY){
  522. char TextLine[1000] = "";
  523. long tempX = 0, tempY = 0;
  524. int i = 0;
  525.  
  526. sprintf(TextLine, "Moving X: %ld, Moving Y: %ld", moveToX, moveToY);
  527. MessageText(TextLine, MessageX, MessageY, 0);
  528. // getch();
  529.  
  530.  
  531. if(moveToX - currentX != 0 && moveToY - currentY != 0){
  532. tempX = currentX;
  533. tempY = currentY;
  534. if(abs(moveToX - currentX) > abs(moveToY - currentY)){
  535. while(currentX < moveToX){
  536. currentX++;
  537. moveXY(currentX, currentY);
  538. currentY = tempY + (currentX - tempX) * (moveToY - tempY) / (moveToX - tempX);
  539. moveXY(currentX, currentY);
  540. BoldLinePattern(currentX, currentY);
  541. }
  542. while(currentX > moveToX){
  543. currentX--;
  544. moveXY(currentX, currentY);
  545. currentY = tempY + (currentX - tempX) * (moveToY - tempY) / (moveToX - tempX);
  546. moveXY(currentX, currentY);
  547. BoldLinePattern(currentX, currentY);
  548. }
  549. }
  550. else{
  551. while(currentY < moveToY){
  552. currentY++;
  553. moveXY(currentX, currentY);
  554. currentX = tempX + (currentY - tempY) * (moveToX - tempX) / (moveToY - tempY);
  555. moveXY(currentX, currentY);
  556. BoldLinePattern(currentX, currentY);
  557. }
  558. while(currentY > moveToY){
  559. currentY--;
  560. moveXY(currentX, currentY);
  561. currentX = tempX + (currentY - tempY) * (moveToX - tempX) / (moveToY - tempY);
  562. moveXY(currentX, currentY);
  563. BoldLinePattern(currentX, currentY);
  564. }
  565. }
  566. }
  567.  
  568.  
  569. while(moveToY > currentY){
  570. currentY++;
  571. moveXY(currentX, currentY);
  572. BoldLinePattern(currentX, currentY);
  573. }
  574. while(moveToY < currentY){
  575. currentY--;
  576. moveXY(currentX, currentY);
  577. BoldLinePattern(currentX, currentY);
  578. }
  579.  
  580. while(moveToX > currentX){
  581. currentX++;
  582. moveXY(currentX, currentY);
  583. BoldLinePattern(currentX, currentY);
  584. }
  585. while(moveToX < currentX){
  586. currentX--;
  587. moveXY(currentX, currentY);
  588. BoldLinePattern(currentX, currentY);
  589. }
  590.  
  591. return 0;
  592. }
  593. //-------------------------------------- CalculateLine --------------------------------------------------------
  594.  
  595. //######################################################################
  596. //################## Main ##############################################
  597. //######################################################################
  598.  
  599. int main(int argc, char **argv){
  600.  
  601. int MenueLevel = 0;
  602. int KeyHit = 0;
  603. int KeyCode[5];
  604. char FileInfo[3];
  605. char FileName[200] = "";
  606. char FullFileName[200] = "";
  607. char FileNameOld[200] = "";
  608. struct winsize terminal;
  609. double Scale = 1.0;
  610. double OldScale = 1.0;
  611. long MoveLength = 1;
  612. int i;
  613. int SingleKey=0;
  614. long currentPlotX = 0, currentPlotY = 0;
  615. int FileSelected = 0;
  616. int FileStartRow = 0;
  617. char *pEnd;
  618. FILE *PlotFile;
  619. char TextLine[10000];
  620. long xMin = 1000000, xMax = -1000000;
  621. long yMin = 1000000, yMax = -1000000;
  622. long coordinateCount = 0;
  623. char a;
  624. int ReadState = 0;
  625. long xNow = 0, yNow = 0;
  626. long xNow1 = 0, yNow1 = 0;
  627. long xNow2 = 0, yNow2 = 0;
  628. struct timeval StartTime, EndTime;
  629. long coordinatePlot = 0;
  630. int stopPlot = 0;
  631. long PlotStartTime = 0;
  632. int MaxFileRows = 0;
  633.  
  634.  
  635. FileInfo[2]='\0';
  636.  
  637. strcpy(FileName, "noFiLE");
  638.  
  639. getcwd(PicturePath, 1000);
  640. strcat(PicturePath, "/pictures");
  641. printf("PicturePath=>%s<", PicturePath);
  642.  
  643. if (wiringPiSetup () == -1){
  644. printf("Could not run wiringPiSetup!");
  645. exit(1);
  646. }
  647.  
  648. softPwmCreate(Z_SERVO, SERVOUP, 200);
  649. softPwmWrite(Z_SERVO, SERVOUP);
  650. usleep(500000);
  651. softPwmWrite(Z_SERVO, 0);
  652.  
  653.  
  654. pinMode (LEFT_STEPPER01, OUTPUT);
  655. pinMode (LEFT_STEPPER02, OUTPUT);
  656. pinMode (LEFT_STEPPER03, OUTPUT);
  657. pinMode (LEFT_STEPPER04, OUTPUT);
  658.  
  659. digitalWrite(LEFT_STEPPER01, 1);
  660. digitalWrite(LEFT_STEPPER02, 0);
  661. digitalWrite(LEFT_STEPPER03, 0);
  662. digitalWrite(LEFT_STEPPER04, 0);
  663.  
  664. pinMode (RIGHT_STEPPER01, OUTPUT);
  665. pinMode (RIGHT_STEPPER02, OUTPUT);
  666. pinMode (RIGHT_STEPPER03, OUTPUT);
  667. pinMode (RIGHT_STEPPER04, OUTPUT);
  668. digitalWrite(RIGHT_STEPPER01, 1);
  669. digitalWrite(RIGHT_STEPPER02, 0);
  670. digitalWrite(RIGHT_STEPPER03, 0);
  671. digitalWrite(RIGHT_STEPPER04, 0);
  672.  
  673.  
  674.  
  675. if(ioctl(STDOUT_FILENO, TIOCGWINSZ, &terminal)<0){
  676. printf("Can't get size of terminal window");
  677. }
  678. else{
  679. MaxRows = terminal.ws_row;
  680. MaxCols = terminal.ws_col;
  681. MessageY = MaxRows-3;
  682. }
  683.  
  684. MaxFileRows = MaxRows - 10;
  685.  
  686. BaseLength = BASELENGTH * StepsPermm;
  687. CordLengthLeft = CORDLENGTH_LEFT * StepsPermm;
  688. CordLengthRight = CORDLENGTH_RIGHT * StepsPermm;
  689. X0 = (CordLengthLeft * CordLengthLeft - CordLengthRight * CordLengthRight + BaseLength * BaseLength) / (2.0 * BaseLength);
  690. Y0 = sqrt(CordLengthRight * CordLengthRight - (BaseLength - X0) * (BaseLength - X0));
  691.  
  692. printf("X0=%ld, Y0=%ld, CL=%ld, CR=%ld StepsPermm=%lf\n", X0, Y0, CordLengthLeft, CordLengthRight, StepsPermm);
  693.  
  694.  
  695. clrscr(1, MaxRows);
  696. PrintRow('-', MessageY - 1);
  697. PrintMenue_01(FileName, Scale, xMax - xMin, yMax - yMin, MoveLength);
  698.  
  699.  
  700. while (1){
  701. MessageText("Waiting for key press.", MessageX, MessageY, 0);
  702.  
  703. i = 0;
  704. SingleKey = 1;
  705. KeyCode[0] = 0;
  706. KeyCode[1] = 0;
  707. KeyCode[2] = 0;
  708. KeyCode[3] = 0;
  709. KeyCode[4] = 0;
  710. KeyHit = 0;
  711. while (kbhit()){
  712. KeyHit = getch();
  713. KeyCode[i] = KeyHit;
  714. i++;
  715. if(i == 5){
  716. i = 0;
  717. }
  718. if(i > 1){
  719. SingleKey = 0;
  720. }
  721. }
  722. if(SingleKey == 0){
  723. KeyHit = 0;
  724. }
  725.  
  726. if(MenueLevel == 0){
  727.  
  728. //Move left motor
  729. if(KeyCode[0] == 27 && KeyCode[1] == 91 && KeyCode[2] == 68 && KeyCode[3] == 0 && KeyCode[4] == 0){
  730. for(i=0; i<MoveLength; i++){
  731. MakeStepLeft(-1);
  732. }
  733. moveXY(0, 0);
  734. }
  735.  
  736. if(KeyCode[0] == 27 && KeyCode[1] == 91 && KeyCode[2] == 67 && KeyCode[3] == 0 && KeyCode[4] == 0){
  737. for(i=0; i<MoveLength; i++){
  738. MakeStepLeft(1);
  739. }
  740. moveXY(0, 0);
  741. }
  742.  
  743. //Move right motor
  744. if(KeyCode[0] == 27 && KeyCode[1] == 91 && KeyCode[2] == 65 && KeyCode[3] == 0 && KeyCode[4] == 0){
  745. for(i=0; i<MoveLength; i++){
  746. MakeStepRight(-1);
  747. }
  748. moveXY(0, 0);
  749. }
  750.  
  751. if(KeyCode[0] == 27 && KeyCode[1] == 91 && KeyCode[2] == 66 && KeyCode[3] == 0 && KeyCode[4] == 0){
  752. for(i=0; i<MoveLength; i++){
  753. MakeStepRight(1);
  754. }
  755. moveXY(0, 0);
  756. }
  757.  
  758. //Pen UP/DOWN
  759. if(KeyCode[0] == 27 && KeyCode[1] == 91 && KeyCode[2] == 53 && KeyCode[3] == 126 && KeyCode[4] == 0){
  760. softPwmWrite(Z_SERVO, SERVOUP);
  761. usleep(500000);
  762. softPwmWrite(Z_SERVO, 0);
  763. currentPlotDown = 0;
  764. }
  765.  
  766. if(KeyCode[0] == 27 && KeyCode[1] == 91 && KeyCode[2] == 54 && KeyCode[3] == 126 && KeyCode[4] == 0){
  767. softPwmWrite(Z_SERVO, SERVODOWN);
  768. usleep(500000);
  769. softPwmWrite(Z_SERVO, 0);
  770. currentPlotDown = 1;
  771. }
  772.  
  773.  
  774. if(KeyHit == 'm'){
  775. MoveLength *= 10;
  776. if(MoveLength == 10000){
  777. MoveLength = 1;
  778. }
  779. PrintMenue_01(FileName, Scale, xMax - xMin, yMax - yMin, MoveLength);
  780. }
  781.  
  782. if(KeyHit == 'f'){
  783. FileStartRow = 0;
  784. FileSelected = 0;
  785. strcpy(FileNameOld, FileName);
  786. strcpy(FileName, PrintMenue_02(FileStartRow, 0));
  787. MenueLevel = 1;
  788. }
  789.  
  790. if(KeyHit == 's'){
  791. OldScale = Scale;
  792. MessageText("Type new scale value: ", 1, MessageY, 0);
  793. gotoxy(23, MessageY);
  794. scanf("%lf", &Scale);
  795. if(Scale == 0){
  796. Scale = OldScale;
  797. }
  798. else{
  799. PrintMenue_01(FileName, Scale, xMax - xMin, yMax - yMin, MoveLength);
  800. }
  801. }
  802.  
  803. if(KeyHit == 'b'){
  804. OldScale = Scale;
  805. MessageText("Type new bold line value: ", 1, MessageY, 0);
  806. gotoxy(27, MessageY);
  807. scanf("%d", &BoldLineWidth);
  808. BoldLineGap = BoldLineWidth;
  809. PrintMenue_01(FileName, Scale, xMax - xMin, yMax - yMin, MoveLength);
  810. }
  811.  
  812.  
  813. if(KeyHit == 'p'){//Plot file
  814. MessageText("3 seconds until plotting starts !!!!!!!!!!!!!!!!!", 1, 20, 0);
  815. sleep(3);
  816. if(strcmp(FileName, "noFiLE") != 0){
  817. if((PlotFile=fopen(FullFileName,"rb"))==NULL){
  818. sprintf(TextLine, "Can't open file '%s'!\n", FullFileName);
  819. strcpy(FileName, "NoFiLE");
  820. ErrorText(TextLine);
  821. }
  822. }
  823. if(strcmp(FileName, "noFiLE") != 0){
  824. BoldLineX = 0, BoldLineY = 0;
  825. xNow1 = -1;
  826. xNow2 = -1;
  827. yNow1 = -1;
  828. yNow2 = -1;
  829. currentPlotX = 0;
  830. currentPlotY = 0;
  831. PlotStartTime = time(0);
  832. PrintMenue_03(FullFileName, coordinateCount, 0, 0, 0, PlotStartTime);
  833. coordinatePlot = 0;
  834. stopPlot = 0;
  835. if(currentPlotDown == 1){
  836. softPwmWrite(Z_SERVO, SERVOUP);
  837. currentPlotDown = 0;
  838. usleep(500000);
  839. softPwmWrite(Z_SERVO, 0);
  840. }
  841.  
  842. while(!(feof(PlotFile)) && stopPlot == 0){
  843.  
  844. fread(&a, 1, 1, PlotFile);
  845. i=0;
  846. TextLine[0] = '\0';
  847. while(!(feof(PlotFile)) && a !=' ' && a != '<' && a != '>' && a != '\"' && a != '=' && a != ',' && a != ':' && a != 10){
  848. TextLine[i] = a;
  849. TextLine[i+1] = '\0';
  850. i++;
  851. fread(&a, 1, 1, PlotFile);
  852. }
  853. if(a == '<'){//Init
  854. if(xNow2 > -1 && yNow2 > -1 && (xNow2 != xNow1 || yNow2 != yNow1)){
  855. stopPlot = CalculateLine(xNow2, yNow2);
  856. if(currentPlotDown == 0){
  857. softPwmWrite(Z_SERVO, SERVODOWN);
  858. usleep(500000);
  859. softPwmWrite(Z_SERVO, 0);
  860. currentPlotDown = 1;
  861. }
  862. currentPlotX = xNow2;
  863. currentPlotY = yNow2;
  864.  
  865. stopPlot = CalculateLine(xNow1, yNow1);
  866. currentPlotX = xNow1;
  867. currentPlotY = yNow1;
  868.  
  869. stopPlot = CalculateLine(xNow, yNow);
  870. currentPlotX = xNow;
  871. currentPlotY = yNow;
  872. }
  873. ReadState = 0;
  874. xNow1 = -1;
  875. xNow2 = -1;
  876. yNow1 = -1;
  877. yNow2 = -1;
  878. }
  879. if(strcmp(TextLine, "path") == 0){
  880. if(currentPlotDown == 1){
  881. softPwmWrite(Z_SERVO, SERVOUP);
  882. usleep(500000);
  883. softPwmWrite(Z_SERVO, 0);
  884. currentPlotDown = 0;
  885. }
  886. ReadState = 1;//path found
  887. }
  888. if(ReadState == 1 && strcmp(TextLine, "fill") == 0){
  889. ReadState = 2;//fill found
  890. }
  891. if(ReadState == 2 && strcmp(TextLine, "none") == 0){
  892. ReadState = 3;//none found
  893. }
  894. if(ReadState == 2 && strcmp(TextLine, "stroke") == 0){
  895. ReadState = 0;//stroke found, fill isn't "none"
  896. }
  897. if(ReadState == 3 && strcmp(TextLine, "d") == 0 && a == '='){
  898. ReadState = 4;//d= found
  899. }
  900. if(ReadState == 4 && strcmp(TextLine, "M") == 0 && a == ' '){
  901. ReadState = 5;//M found
  902. }
  903.  
  904. if(ReadState == 6){//Y value
  905. yNow = (double)(strtol(TextLine, &pEnd, 10) - yMin) * StepsPermm * Scale / 100.0;
  906. ReadState = 7;
  907. coordinatePlot++;
  908. }
  909. if(ReadState == 5 && a == ','){//X value
  910. //xNow = ((xMax - strtol(TextLine, &pEnd, 10))) * Scale;//swap X
  911. xNow = (double)(strtol(TextLine, &pEnd, 10) - xMin) * StepsPermm * Scale / 100.0;
  912. ReadState = 6;
  913. }
  914. if(ReadState == 7){
  915. if(xNow2 > -1 && yNow2 > -1 && (xNow2 != xNow1 || yNow2 != yNow1)){
  916. stopPlot = CalculateLine(xNow2, yNow2);
  917. if(currentPlotDown == 0){
  918. softPwmWrite(Z_SERVO, SERVODOWN);
  919. usleep(500000);
  920. softPwmWrite(Z_SERVO, 0);
  921. currentPlotDown = 1;
  922. }
  923. currentPlotX = xNow2;
  924. currentPlotY = yNow2;
  925. }
  926. xNow2 = xNow1;
  927. yNow2 = yNow1;
  928. xNow1 = xNow;
  929. yNow1 = yNow;
  930. ReadState = 5;
  931. }
  932. //PrintMenue_03(FullFileName, coordinateCount, coordinatePlot, 0, 0, PlotStartTime);
  933. }//while(!(feof(PlotFile)) && stopPlot == 0){
  934. fclose(PlotFile);
  935. if(currentPlotDown == 1){
  936. softPwmWrite(Z_SERVO, SERVOUP);
  937. usleep(500000);
  938. softPwmWrite(Z_SERVO, 0);
  939. currentPlotDown = 0;
  940. }
  941. PrintMenue_03(FullFileName, coordinateCount, coordinatePlot, 0, 0, PlotStartTime);
  942. CalculateLine(0, 0);
  943. currentPlotX = 0;
  944. currentPlotY = 0;
  945. while(kbhit()){
  946. getch();
  947. }
  948. MessageText("Finished! Press any key to return to main menu.", MessageX, MessageY, 0);
  949. getch();
  950. PrintMenue_01(FileName, Scale, xMax - xMin, yMax - yMin, MoveLength);
  951. }//if(strcmp(FileName, "noFiLE") != 0){
  952. }//if(KeyHit == 'p'){
  953.  
  954.  
  955.  
  956.  
  957. }//if(MenueLevel == 0){
  958.  
  959. if(MenueLevel == 1){//Select file
  960.  
  961. if(KeyCode[0] == 27 && KeyCode[1] == 91 && KeyCode[2] == 66 && KeyCode[3] == 0 && KeyCode[4] == 0){
  962. if(FileSelected < FilesFound - 1){
  963. FileSelected++;
  964. if(FileSelected > MaxFileRows - 2){
  965. FileStartRow = FileSelected - MaxFileRows + 2;
  966. }
  967. strcpy(FileName, PrintMenue_02(FileStartRow, FileSelected));
  968. }
  969. }
  970.  
  971. if(KeyCode[0] == 27 && KeyCode[1] == 91 && KeyCode[2] == 65 && KeyCode[3] == 0 && KeyCode[4] == 0){
  972. if(FileSelected > 0){
  973. if(FileSelected == FileStartRow + 1){
  974. if(FileStartRow > 0){
  975. FileStartRow--;
  976. }
  977. }
  978. FileSelected--;
  979. strcpy(FileName, PrintMenue_02(FileStartRow, FileSelected));
  980. }
  981. }
  982.  
  983. if(KeyHit == 10){//Read file and store values
  984. MenueLevel = 0;
  985. clrscr(MessageY + 1, MessageY + 1);
  986. strcpy(FullFileName, PicturePath);
  987. strcat(FullFileName, "/");
  988. strcat(FullFileName, FileName);
  989. if((PlotFile=fopen(FullFileName,"rb"))==NULL){
  990. sprintf(TextLine, "Can't open file '%s'!\n", FullFileName);
  991. ErrorText(TextLine);
  992. strcpy(FileName, "NoFiLE");
  993. }
  994. else{
  995. xMin=1000000;
  996. xMax=-1000000;
  997. yMin=1000000;
  998. yMax=-1000000;
  999. coordinateCount = 0;
  1000.  
  1001. while(!(feof(PlotFile)) && stopPlot == 0){
  1002.  
  1003. fread(&a, 1, 1, PlotFile);
  1004. i=0;
  1005. TextLine[0] = '\0';
  1006. while(!(feof(PlotFile)) && a !=' ' && a != '<' && a != '>' && a != '\"' && a != '=' && a != ',' && a != ':' && a != 10){
  1007. TextLine[i] = a;
  1008. TextLine[i+1] = '\0';
  1009. i++;
  1010. fread(&a, 1, 1, PlotFile);
  1011. }
  1012. if(a == '<'){//Init
  1013. ReadState = 0;
  1014. }
  1015. if(strcmp(TextLine, "path") == 0){
  1016. ReadState = 1;//path found
  1017. }
  1018. if(ReadState == 1 && strcmp(TextLine, "fill") == 0){
  1019. //ReadState = 2;//fill found
  1020. ReadState = 3;//paths without line are also considered when calculating max values
  1021. }
  1022. if(ReadState == 2 && strcmp(TextLine, "none") == 0){
  1023. ReadState = 3;//none found
  1024. }
  1025. if(ReadState == 2 && strcmp(TextLine, "stroke") == 0){
  1026. ReadState = 0;//stroke found, fill isn't "none"
  1027. }
  1028. if(ReadState == 3 && strcmp(TextLine, "d") == 0 && a == '='){
  1029. ReadState = 4;//d= found
  1030. }
  1031. if(ReadState == 4 && strcmp(TextLine, "M") == 0 && a == ' '){
  1032. ReadState = 5;//M found
  1033. }
  1034.  
  1035. if(ReadState == 5 && strcmp(TextLine, "C") == 0 && a == ' '){
  1036. ReadState = 5;//C found
  1037. }
  1038.  
  1039. if(ReadState == 6){//Y value
  1040. yNow = strtol(TextLine, &pEnd, 10);
  1041. //printf("String='%s' y=%ld\n", TextLine, yNow);
  1042. if(yNow > yMax){
  1043. yMax = yNow;
  1044. }
  1045. if(yNow < yMin){
  1046. yMin = yNow;
  1047. }
  1048. ReadState = 7;
  1049. coordinateCount++;
  1050. }
  1051. if(ReadState == 5 && a == ','){//X value
  1052. xNow = strtol(TextLine, &pEnd, 10);
  1053. if(xNow > xMax){
  1054. xMax = xNow;
  1055. }
  1056. if(xNow < xMin){
  1057. xMin = xNow;
  1058. }
  1059. ReadState = 6;
  1060. }
  1061. if(ReadState == 7){
  1062. //printf("Found coordinates %ld, %ld\n", xNow, yNow);
  1063. ReadState = 5;
  1064. }
  1065. gotoxy(1, MessageY);printf("ReadState=% 3d, xNow=% 10ld, xMin=% 10ld, xMax=% 10ld, yMin=% 10ld, yMax=% 10ld ", ReadState, xNow, xMin, xMax, yMin, yMax);
  1066.  
  1067. }//while(!(feof(PlotFile)) && stopPlot == 0){
  1068. fclose(PlotFile);
  1069. Scale = 1.0;
  1070. }
  1071. PrintMenue_01(FileName, Scale, xMax - xMin, yMax - yMin, MoveLength);
  1072. }//if(KeyHit == 10){
  1073.  
  1074. }//if(MenueLevel == 1){
  1075.  
  1076.  
  1077. if(KeyHit == 27){
  1078. if(MenueLevel == 0){
  1079. clrscr(MessageY + 1, MessageY + 1);
  1080. MessageText("Exit program (y/n)?", MessageX, MessageY + 1, 0);
  1081. while(KeyHit != 'y' && KeyHit != 'n'){
  1082. KeyHit = getch();
  1083. if(KeyHit == 'y'){
  1084. digitalWrite(LEFT_STEPPER01, 0);
  1085. digitalWrite(LEFT_STEPPER02, 0);
  1086. digitalWrite(LEFT_STEPPER03, 0);
  1087. digitalWrite(LEFT_STEPPER04, 0);
  1088.  
  1089. digitalWrite(RIGHT_STEPPER01, 0);
  1090. digitalWrite(RIGHT_STEPPER02, 0);
  1091. digitalWrite(RIGHT_STEPPER03, 0);
  1092. digitalWrite(RIGHT_STEPPER04, 0);
  1093. exit(0);
  1094. }
  1095. }
  1096. }
  1097. if(MenueLevel == 1){
  1098. MenueLevel = 0;
  1099. strcpy(FileName, FileNameOld);
  1100. PrintMenue_01(FileName, Scale, xMax - xMin, yMax - yMin, MoveLength);
  1101. }
  1102. clrscr(MessageY + 1, MessageY + 1);
  1103. }
  1104. }
  1105.  
  1106. return 0;
  1107. }
  1108.  
  1109.  
  1110.  
Compilation error #stdin compilation error #stdout 0s 0KB
stdin
Standard input is empty
compilation info
prog.c:23:22: fatal error: wiringPi.h: No such file or directory
compilation terminated.
stdout
Standard output is empty