Advertisement
Guest User

Untitled

a guest
Jan 18th, 2019
141
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 14.79 KB | None | 0 0
  1. /*##############################################
  2. _ _ _
  3. | | | | |
  4. _ __ ___ ___ __| | | ___ __ _ __ _| |_ ___
  5. | '_ \ / _ \/ _ \/ _` | |/ _ \/ _` |/ _` | __/ _ \
  6. | | | | __/ __/ (_| | | __/ (_| | (_| | || __/
  7. |_| |_|\___|\___|\__,_|_|\___|\__, |\__,_|\__\___|
  8. __/ |
  9. |___/
  10. made by: Needlegate
  11. 2019-01-05
  12.  
  13. for Arduino ATmega 2560
  14. */
  15. #include "Motors.h"
  16. #include "Program.h"
  17. #include <Braccio.h>
  18. #include <Servo.h>
  19. #define CLS "\033[2J"
  20. Servo base;
  21. Servo shoulder;
  22. Servo elbow;
  23. Servo wrist_rot;
  24. Servo wrist_ver;
  25. Servo gripper;
  26. Motors Robot;
  27. Program Prog;
  28. int x_axis = A0; //joystick pin
  29. int y_axis = A1; // joystick pin
  30. int x_pos;
  31. int y_pos;
  32. byte axis_choice = 0; //manual mode
  33. int button1 = 30;
  34. int button2 = 34;
  35. int button3 = 38;
  36. bool bt1_Tick; //high or low every loop
  37. bool bt2_Tick; // --II--
  38. bool bt3_Tick; // --II--
  39. bool lastBt1;
  40. long last_bt1_highFlank;
  41. long last_bt2_highFlank;
  42. long last_bt3_highFlank;
  43. bool lastBt2;
  44. bool lastBt3;
  45. byte lvl_one_ptr = 1;
  46. byte final_lvl_1 = 0;
  47. byte lvl_two_ptr = 0;
  48. byte last_lvl_two_ptr; //special for manual menu
  49. byte final_lvl_2 = 0;
  50. byte lvl_three_ptr = 0;
  51. byte final_lvl_3 = 0;
  52.  
  53. byte program_amount;
  54. byte program_choice;
  55. bool program_running = false;
  56. byte program_nodes;
  57. byte current_node;
  58.  
  59. int lastCheck;
  60. byte tick = 0;
  61. long last_tick = 0;
  62.  
  63.  
  64. void setup() {
  65. Serial.begin(57600);
  66.  
  67.  
  68. Serial.println("##################################################################");
  69. Serial.println("################ NEEDLEGATE ROBOT TOOL 1.0 #######################");
  70. Serial.println("##################################################################");
  71. Serial.println();
  72. Serial.println("robot starting up, stay away..");
  73. // Braccio.begin(-70);
  74. pinMode(button1, INPUT);
  75. pinMode(button2, INPUT);
  76. pinMode(button3, INPUT);
  77. }
  78.  
  79. void loop() {
  80. bt1_Tick = false;
  81. bt2_Tick = false;
  82. bt3_Tick = false;
  83. check_button_highFlank();
  84.  
  85. menus();
  86. if (!program_running) {
  87. if (final_lvl_1 == 1) { // manual mode
  88. manual();
  89. move_robot();
  90. }
  91. if (final_lvl_1 == 2) { //programming
  92. manual();
  93. move_robot();
  94. menu_logic();
  95. }
  96. if (final_lvl_1 == 3) { // load program
  97. menu_logic();
  98.  
  99. }
  100. if (final_lvl_1 == 4) { //Erase memory
  101. print_menu();
  102. menu_logic();
  103. }
  104.  
  105. if (update_screen()) { // only updates if something changed (not perfect)
  106. print_screen();
  107. }
  108.  
  109. } else {
  110. menu_logic();
  111. if (program_running) {
  112. if ( run_program(program_choice, program_nodes)) {
  113. Serial.println("program done");
  114. delay(1000);
  115. } else {
  116. print_screen();
  117. Serial.print("running next node: ");
  118. Serial.println(program_nodes);
  119. }
  120. }
  121.  
  122. }
  123. }
  124. boolean run_program(byte program, byte nodes) {
  125.  
  126. if (current_node > nodes) {
  127. program_running = false;
  128. Serial.println("program completed");
  129. return true;
  130. }
  131.  
  132. bool moving_to_node = true;
  133. byte actual_pos[7];
  134. byte goal_pos[7];
  135. for (int i = 1; i < 7; i++) {
  136. actual_pos[i] = Robot.get_motor(i); // get actual pos
  137. goal_pos[i] = Prog.get_axis(program, current_node, i); //error <<... node 255?
  138.  
  139. }
  140. byte completed = 0;
  141. for (int i = 1; i < 7; i++) {
  142. if (actual_pos[i] > goal_pos[i]) {
  143. Robot.decrement(i);
  144. } else if (actual_pos[i] < goal_pos[i]) {
  145. Robot.increment(i);
  146. } else {
  147. completed++;
  148. }
  149. }
  150. /// just 1 degree per lap
  151.  
  152.  
  153. if (completed >= 6) {
  154. current_node++;
  155. }
  156.  
  157. moving_to_node = false;
  158.  
  159. // move_auto(m1, m2, m3, m4, m5, m6);
  160. return false;
  161. }
  162. boolean update_screen() {
  163. long this_tick = millis();
  164. if (this_tick - last_tick > 1000) {
  165. tick += 1;
  166. last_tick = this_tick;
  167. }
  168. int a = digitalRead(button1);
  169. int b = digitalRead(button2);
  170. int c = digitalRead(button3);
  171. int tempx = analogRead(x_axis);
  172. int tempy = analogRead(y_axis);
  173. tempx = map(tempx, 0, 1023, 0, 100);
  174. tempy = map(tempy, 0, 1023, 0, 100);
  175. int r = 0;
  176. for (int i = 1; i < 7; i++) {
  177. int R = Robot.get_motor(i);
  178. r = r + R;
  179. }
  180. int checkSum = a + b + c + r + tick;
  181. if (checkSum != lastCheck) {
  182. lastCheck = a + b + c + r + tick;
  183. return true;
  184. }
  185. lastCheck = a + b + c + r + tick;
  186. return false;
  187. }
  188. void manual() { /// manually run robot. menu 1 /2
  189.  
  190. x_pos = analogRead(x_axis);
  191. y_pos = analogRead(y_axis);
  192. long currentMillis = millis();
  193.  
  194. if (y_pos > 490 && y_pos < 520) { //return if joystick is untouched
  195. return;
  196. }
  197. boolean increment;
  198.  
  199. if (y_pos > 520) { // determine if we want to increment or not
  200. increment = true;
  201. } else if (y_pos < 490) {
  202. increment = false;
  203. }
  204.  
  205. if (increment) { //increment robot pos for axis of choice
  206. int value = map(y_pos, 521, 1023, 1, 3);
  207. long thisMove = millis();
  208. long lastMove = Robot.get_lastMove(axis_choice);
  209. boolean timeToMove = false;
  210. if (thisMove - lastMove > 50 / value) {
  211. if (Robot.increment(axis_choice)) {
  212. //Serial.println(Robot.get_motor(axis_choice));
  213. }
  214. }
  215. }
  216.  
  217. if (!increment) { //decrement robot pos for axis of choice
  218. int value = map(y_pos, 490, 0, 1, 3);
  219. long thisMove = millis();
  220. long lastMove = Robot.get_lastMove(axis_choice);
  221. boolean timeToMove = false;
  222. if (thisMove - lastMove > 50 / value) {
  223. if (Robot.decrement(axis_choice)) {
  224. ;
  225. }
  226. }
  227. }
  228.  
  229. }
  230. boolean save_node() { //send the axis positions from Robot class to Prog class
  231. byte temp_axis[6];
  232. for (int i = 1; i < 7; i++) {
  233. temp_axis[i - 1] = Robot.get_motor(i);
  234. }
  235. if (Prog.set_node(temp_axis[0], temp_axis[1], temp_axis[2], temp_axis[3],
  236. temp_axis[4], temp_axis[5])) {
  237. return true;
  238. } else {
  239.  
  240. return false;
  241. }
  242. }
  243. boolean save_program() { // let programmer class know that the program is done
  244. Prog.set_end();
  245.  
  246. }
  247.  
  248. void move_robot() {
  249. byte m1 = Robot.get_motor(1);
  250. byte m2 = Robot.get_motor(2);
  251. byte m3 = Robot.get_motor(3);
  252. byte m4 = Robot.get_motor(4);
  253. byte m5 = Robot.get_motor(5);
  254. byte m6 = Robot.get_motor(6);
  255. Braccio.ServoMovement(3, m1, m2, m3, m4, m5, m6);
  256. //delay(5);;
  257.  
  258. }
  259.  
  260. void menus() {
  261. ////////////return button 3
  262. if (bt3_Tick) { //return 1 level
  263. if (lvl_one_ptr > 0 && lvl_two_ptr == 0 && lvl_three_ptr == 0) {
  264. final_lvl_1 = 0;
  265. }
  266. else if (lvl_two_ptr > 0 && lvl_three_ptr == 0) {
  267. lvl_two_ptr = 0; //return to level 1
  268. final_lvl_2 = 0;
  269. } else if (lvl_three_ptr > 0) {
  270. lvl_three_ptr = 0; //return to level 2
  271. final_lvl_3 = 0;
  272. }
  273. }
  274.  
  275. ////////cycle menu level - button 2
  276. if (bt2_Tick) { //cycle menus
  277. if (final_lvl_2 > 0) { //we are in level 3
  278. lvl_three_ptr = (lvl_three_ptr + 1) % 4;
  279. if (lvl_three_ptr == 0) //make sure we dont get zero
  280. lvl_three_ptr = 1;
  281. /////////////////////////////////////////////
  282. } else if (final_lvl_1 > 0 && final_lvl_3 == 0) { // lvl 2
  283. if (final_lvl_1 == 3) {
  284. lvl_two_ptr = (lvl_two_ptr + 1) % 4;
  285. if (lvl_two_ptr == 0) //make sure we dont get zero
  286. lvl_two_ptr = 1;
  287. } else if (final_lvl_1 == 2) { // special - create program
  288. axis_choice = (axis_choice + 1) % 7;
  289. if (axis_choice == 0) {
  290. axis_choice = 1;
  291. }
  292. }
  293. ///////// end special create menu
  294. else if (final_lvl_1 == 1) { //special - manual menu
  295. axis_choice = (axis_choice + 1) % 7;
  296. if (axis_choice == 0) {
  297. axis_choice = 1;
  298. }
  299. }
  300. ////// end special manual menu
  301. } else {
  302. lvl_one_ptr = (lvl_one_ptr + 1) % 5; //lvl 1
  303. if (lvl_one_ptr == 0)
  304. lvl_one_ptr = 1;
  305. }
  306. }
  307. //////chose menu - button 1
  308. if (bt1_Tick) { //select button 1
  309. byte temp;
  310.  
  311. if (lvl_three_ptr > 0) {
  312. final_lvl_3 = lvl_three_ptr;
  313.  
  314. } else if (lvl_two_ptr > 0 && lvl_three_ptr == 0) {
  315. final_lvl_2 = lvl_two_ptr;
  316.  
  317. } else if (lvl_one_ptr > 0 && lvl_two_ptr == 0 && lvl_three_ptr == 0) {
  318. final_lvl_1 = lvl_one_ptr;
  319. if (final_lvl_1 == 3) { //special for loading menu
  320. program_amount = 0;
  321. bool check_program = true;
  322. while (check_program) {
  323. temp = Prog.get_program_list();
  324. if (temp == 254) {
  325. program_amount++;
  326. }
  327. if (temp == 0) {
  328. check_program = false;
  329. }
  330. }
  331. }
  332. }
  333. }
  334.  
  335. }
  336.  
  337.  
  338. void print_screen() {
  339. delay(5);
  340. Serial.print("\033[2J"); // clear screen
  341. // print_logo();
  342. print_axis();
  343. Serial.println("");
  344. Serial.print(lvl_one_ptr); Serial.println(final_lvl_1);
  345. Serial.print(lvl_two_ptr); Serial.println(final_lvl_2);
  346. Serial.print(lvl_three_ptr); Serial.println(final_lvl_3);
  347. print_menu();
  348.  
  349. }
  350. void print_menu() {
  351. char arrow = '<';
  352. char spear = '-';
  353. Serial.println();
  354. ///main menu
  355. if (final_lvl_1 == 0) {
  356. Serial.println("__MAIN MENU__");
  357. Serial.print("* manual drive ");
  358. if (lvl_one_ptr == 1) {
  359. Serial.print(arrow);
  360. Serial.print(spear); Serial.println(spear);
  361. } else {
  362. Serial.println();
  363. }
  364. Serial.print("* create program ");
  365. if (lvl_one_ptr == 2) {
  366. Serial.print(arrow);
  367. Serial.print(spear); Serial.println(spear);
  368. } else {
  369. Serial.println();
  370. }
  371. Serial.print("* load program ");
  372. if (lvl_one_ptr == 3) {
  373. Serial.print(arrow);
  374. Serial.print(spear); Serial.println(spear);
  375. } else {
  376. Serial.println();
  377. }
  378. Serial.print("* format EEPROM ");
  379. if (lvl_one_ptr == 4) {
  380. Serial.print(arrow);
  381. Serial.print(spear); Serial.println(spear);
  382. } else {
  383. Serial.println();
  384. }
  385. }
  386. // level 2 manual menu
  387. //manual menu
  388. if (final_lvl_2 == 0 && final_lvl_1 == 1) {
  389. Serial.println("__MANUAL MENU__");
  390. Serial.println("manual drive");
  391. Serial.print("axis choosed: "); Serial.println(axis_choice);
  392.  
  393. }
  394. // create program menu
  395. if (final_lvl_2 == 0 && final_lvl_1 == 2) {
  396. Serial.println("_Create Program_");
  397. Serial.println("manual drive - save node by clicking bt1");
  398. Serial.println(", hold bt1 2 secs to save program");
  399. Serial.print("axis choosed: "); Serial.println(axis_choice);
  400. }
  401. // list programs om EEPROM
  402. if (final_lvl_2 == 0 && final_lvl_1 == 3) {
  403. Serial.print("number of saved programs: ");
  404. Serial.println(program_amount);
  405. Serial.print("load program: "); Serial.println(program_choice);
  406. }
  407. /// erase EEPROM menu
  408. if (final_lvl_2 == 0 && final_lvl_1 == 4) {
  409. Serial.print(CLS);
  410. Serial.println("WARNING");
  411. Serial.println("this will erase all stored data on EEPROM");
  412. Serial.println("press 1 to ERASE or press 3 to go back to menu");
  413. Serial.println("continue?");
  414. delay(100);
  415. }
  416. }
  417. ////////////////////////
  418. ////LOGIC//////////////
  419. //////////////////////
  420. void menu_logic() {
  421. byte temp = digitalRead(button1); /// for reset for save button
  422. long currentMillis = millis();
  423. /// Erase menu
  424. if (final_lvl_2 == 0 && final_lvl_1 == 4) {
  425. bool choice = false;
  426. while (!choice) {
  427.  
  428. if (get_bt3_highFlank()) {
  429. final_lvl_1 = 0;
  430. choice = true;
  431. }
  432. if (get_bt1_highFlank()) {
  433. final_lvl_1 = 0;
  434. Prog.format_EEPROM();
  435. program_amount = 0;
  436. choice = true;
  437. }
  438. }
  439. }
  440. /// programming menu
  441. if (final_lvl_1 == 2 && lvl_one_ptr == 2) { //prog menu
  442. if (bt1_Tick) { ///set node
  443. if (save_node()) {
  444. Serial.println("saving node in program");
  445. } else {
  446. Serial.println("failed to save node");
  447. }
  448.  
  449. delay(300);
  450. } else if (temp == 1 && currentMillis - last_bt1_highFlank > 2000 && currentMillis - last_bt1_highFlank < 3000) {
  451. Serial.println("Saving program");
  452. Prog.set_end();
  453. last_bt1_highFlank = millis();
  454. final_lvl_1 == 0;
  455. lvl_one_ptr == 0;
  456. }
  457.  
  458. }
  459.  
  460. /// load program
  461. if (final_lvl_1 == 3 && lvl_one_ptr == 3) {
  462. if (bt2_Tick) {
  463. program_choice = (program_choice + 1) % ( program_amount + 1);
  464. if (program_choice == 0) {
  465. program_choice = 1;
  466. }
  467. }
  468. if (bt1_Tick && program_choice != 0) {
  469. program_nodes = Prog.get_program_node_amount(program_choice);
  470.  
  471. program_running = true;
  472. current_node = 2; ///<<<<----------2 because of error in program class
  473.  
  474. }
  475. if (bt3_Tick) {
  476. program_nodes = 0;
  477. program_running = false;
  478. program_choice = 0;
  479. }
  480. }
  481.  
  482. }
  483. void print_axis() {
  484. Serial.println("axis angles:");
  485. Serial.print("M1"); Serial.print(" M2");
  486. Serial.print(" M3"); Serial.print(" M4"); Serial.print(" M5");
  487. Serial.println(" M6");
  488. Serial.print(Robot.get_motor(1)); Serial.print(" ");
  489. Serial.print(Robot.get_motor(2)); Serial.print(" ");
  490. Serial.print(Robot.get_motor(3)); Serial.print(" ");
  491. Serial.print(Robot.get_motor(4)); Serial.print(" ");
  492. Serial.print(Robot.get_motor(5)); Serial.print(" ");
  493. Serial.print(Robot.get_motor(6)); Serial.println(" ");
  494. }
  495. void print_logo() {
  496. Serial.print("\033[2J"); // clear screen
  497. Serial.println("##################################################################");
  498. Serial.println("################ NEEDLEGATE ROBOT TOOL 1.0 #######################");
  499. Serial.println("##################################################################");
  500. Serial.println();
  501. delay(1000);
  502. }
  503. boolean get_bt1_lowFlank() {
  504. bool b1 = digitalRead(button1);
  505. // delay(5);;
  506. if (!b1 && lastBt1) { // low flank
  507. lastBt1 = b1;
  508. return true;
  509. }
  510. lastBt1 = b1;
  511. return false;
  512. }
  513. boolean get_bt1_highFlank() {
  514. bool b1 = digitalRead(button1);
  515. // delay(5);;
  516. if (b1 && !lastBt1) { // high flank
  517. lastBt1 = b1;
  518. last_bt1_highFlank = millis();
  519. return true;
  520. }
  521. lastBt1 = b1;
  522. return false;
  523. }
  524. boolean get_bt2_highFlank() {
  525. bool b2 = digitalRead(button2);
  526. // delay(5);;
  527. if (b2 && !lastBt2) { // high flank
  528. lastBt2 = b2;
  529. return true;
  530. }
  531. lastBt2 = b2;
  532. return false;
  533. }
  534. boolean get_bt3_highFlank() {
  535. bool b3 = digitalRead(button3);
  536. // delay(5);;
  537. if (b3 && !lastBt3) { // high flank
  538. lastBt3 = b3;
  539. return true;
  540. }
  541. lastBt3 = b3;
  542. return false;
  543. }
  544. void check_button_highFlank() {
  545. if (get_bt1_highFlank()) {
  546. last_bt1_highFlank = millis();
  547. bt1_Tick = true;
  548. }
  549. if (get_bt2_highFlank()) {
  550. last_bt2_highFlank = millis();
  551. bt2_Tick = true;
  552. }
  553. if (get_bt3_highFlank()) {
  554. last_bt3_highFlank = millis();
  555. bt3_Tick = true;
  556. }
  557. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement