Advertisement
Guest User

Untitled

a guest
Jan 28th, 2018
263
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.75 KB | None | 0 0
  1. // simple tool for testing the PID response using some of the features of new firmware (S command returns the last encoder positions from last X command
  2. // by misan 2015
  3. // it is a loop of moves X100 .. X0 .. X100 .. X0 ...
  4. // "P", "I" or "D" keys increase existing PID values
  5. // p i d keys decrease respective values
  6. // it draws "almost" in real time motor response
  7. // modified for longer target values than 100
  8.  
  9. import java.util.*;
  10. import processing.serial.*;
  11.  
  12. Serial myPort; // The serial port
  13.  
  14. int targetVal = 220; // use this to set the target value
  15. //please note the warparound effect on th graph if using values > 255 as Arduino only recording 8 bits location values :-(
  16.  
  17. float kp=19, ki=0.5, kd=0.17;
  18. void setup() {
  19. size(800, 400);
  20. // create a font with the third font available to the system:
  21. PFont myFont = createFont(PFont.list()[2], 14);
  22. textFont(myFont);
  23. frameRate(1);
  24. // List all the available serial ports:
  25. println(Serial.list());
  26. String portName = Serial.list()[3];
  27. myPort = new Serial(this, portName, 115200);
  28. }
  29.  
  30. void draw() {
  31. strokeWeight(1);
  32. background(255,100); stroke(255,0,0);
  33. line(0,400-333,600,400-333);
  34. strokeWeight(.1);
  35. for(int i=1;i<6;i++) line(i*100,0,i*100,400);
  36. delay(500 + targetVal / 10);
  37. stroke(0,0,255); strokeWeight(2);
  38. while(myPort.available()>0) print(char(myPort.read()));
  39.  
  40.  
  41. myPort.write('X');
  42. String out = "" + targetVal;
  43. for(int i=0; i<out.length();i++) myPort.write(out.charAt(i));
  44. myPort.write('\r');
  45. myPort.write('\n');
  46. delay(500 + targetVal / 10);
  47. myPort.write('S');
  48. myPort.write('\r');
  49. myPort.write('\n');
  50. delay(500 + targetVal / 10);
  51. String s="";
  52. while(myPort.available()>0) s+=char(myPort.read());
  53.  
  54. //println(s);
  55.  
  56. Scanner sc = new Scanner(s);
  57.  
  58. int x=0;
  59. while(sc.hasNextInt()) {
  60. float y = sc.nextInt();
  61. float c = map (y,0,targetVal<255 ? targetVal : 255,0,300);
  62. point(x++, 400-c);
  63. }
  64.  
  65. myPort.write('X');
  66. myPort.write('0');
  67. myPort.write('\r');
  68. myPort.write('\n');
  69. delay(500 + targetVal / 10);
  70. }
  71.  
  72. void keyPressed() {
  73. if(key=='P') {
  74. kp+=1;
  75. myPort.write('P');
  76. String out=""+kp; for(int i=0; i<out.length();i++) myPort.write(out.charAt(i));
  77. myPort.write('\r');
  78. myPort.write('\n');
  79. println("P="+kp);
  80. }
  81. if(key=='p') {
  82. kp-=1; kp=max(0,kp);
  83. myPort.write('P');
  84. String out=""+kp; for(int i=0; i<out.length();i++) myPort.write(out.charAt(i));
  85. myPort.write('\r');
  86. myPort.write('\n');
  87. println("P="+kp);
  88. }
  89. if(key=='D')
  90. {
  91. kd+=0.01;
  92. myPort.write('D');
  93. String out=""+kd; for(int i=0; i<out.length();i++) myPort.write(out.charAt(i));
  94. myPort.write('\r');
  95. myPort.write('\n');
  96. println("D="+kd);
  97. }
  98. if(key=='d') {
  99. kd-=0.01; kd=max(0,kd);
  100. myPort.write('D');
  101. String out=""+kd; for(int i=0; i<out.length();i++) myPort.write(out.charAt(i));
  102. myPort.write('\r');
  103. myPort.write('\n');
  104. println("D="+kd);
  105. }
  106. if(key=='I') {
  107. ki*=2;
  108. myPort.write('I');
  109. String out=""+ki; for(int i=0; i<out.length();i++) myPort.write(out.charAt(i));
  110. myPort.write('\r');
  111. myPort.write('\n');
  112. println("I="+ki);
  113. }
  114. if(key=='i') {
  115. ki/=2;
  116. myPort.write('I');
  117. String out=""+ki; for(int i=0; i<out.length();i++) myPort.write(out.charAt(i));
  118. myPort.write('\r');
  119. myPort.write('\n');
  120. println("I="+ki);
  121. }
  122. if(key=='W') {
  123. myPort.write('W');
  124. myPort.write('\r');
  125. myPort.write('\n');
  126. println("Values stored.");
  127. }
  128. if(key=='Q') {
  129. myPort.write('Q');
  130. myPort.write('\r');
  131. myPort.write('\n');
  132. println("Values requested.");
  133. }
  134. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement