Advertisement
Guest User

Untitled

a guest
May 2nd, 2014
170
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.05 KB | None | 0 0
  1. import g4p_controls.*;
  2. import processing.serial.*;
  3.  
  4. class IMU {
  5. public int accX, accY, accZ;
  6. public int gyroX, gyroY, gyroZ;
  7. public int tempRaw;
  8. public int compAngleX, compAngleY, tempCal;
  9. public int serialCount = 0; // A count of how many bytes we receive
  10. public boolean firstContact = false; // Whether we've heard from the microcontroller
  11. }
  12.  
  13. IMU myIMU1;
  14. IMU myIMU2;
  15.  
  16. Serial[] myPorts = new Serial[2];
  17.  
  18. float[][] dataIn = new float[2][10]; // a list to hold data from the serial ports
  19. boolean communicationStarted = false;
  20. float value;
  21.  
  22. void setup()
  23. {
  24. // Take care of the GUI
  25. size(600, 400);
  26. createGUI();
  27. customGUI();
  28.  
  29. // Create IMU objects
  30. myIMU1 = new IMU();
  31. myIMU2 = new IMU();
  32.  
  33. // initial parameters for 2D/3D animation
  34. initAnimation();
  35. }
  36.  
  37. void draw() {
  38. background(160, 150, 150);
  39. if (communicationStarted == true) {
  40. printValues();
  41. }
  42. // Every 10th frame update the sketchpad graphic
  43. if (frameCount % 10 == 0) {
  44. clearGraphic();
  45. updateGraphic();
  46. }
  47. }
  48.  
  49. void printValues() {
  50. }
  51.  
  52. void startCommunication() {
  53. communicationStarted = true;
  54.  
  55. textarea_console.appendText("Communication Started");
  56. }
  57.  
  58. void stopCommunication() {
  59. communicationStarted = false;
  60.  
  61. textarea_console.appendText("Communication Stopped");
  62. }
  63.  
  64. void findSerials() {
  65. String SerialList[] = Serial.list();
  66. String s;
  67.  
  68. s = join(SerialList, "\n");
  69. // Print a list of the serial ports, for debugging purposes:
  70. printArray(Serial.list());
  71.  
  72. textarea_console.appendText("Available Serial Ports:");
  73. textarea_console.appendText(s);
  74. textarea_console.appendText(" ");
  75. }
  76.  
  77. void autoConnect() {
  78. String portOne = Serial.list()[0];
  79. String portTwo = Serial.list()[1];
  80. myPorts[0] = new Serial(this, portOne, 9600);
  81. myPorts[1] = new Serial(this, portTwo, 9600);
  82.  
  83. textarea_console.appendText("Auto-connecting.");
  84. textarea_console.appendText("IMU #1 Connected.");
  85. textarea_console.appendText(" Port: " + portOne + " BaudRate: " + 9600);
  86. textarea_console.appendText("IMU #2 Connected.");
  87. textarea_console.appendText(" Port: " + portTwo + " BaudRate: " + 9600);
  88. textarea_console.appendText(" ");
  89. }
  90.  
  91. void connectIMU1() {
  92. String port = dropList_com1.getSelectedText();
  93. //int baudRate = dropList_com1.getSelectedText();
  94. int baudRate = 9600;
  95. myPorts[0] = new Serial(this, port, baudRate);
  96.  
  97. textarea_console.appendText("IMU #1 Connected.");
  98. textarea_console.appendText(" Port: " + port + " BaudRate: " + baudRate);
  99. }
  100.  
  101. void connectIMU2() {
  102. String port = dropList_com2.getSelectedText();
  103. //int baudRate = dropList_com1.getSelectedText();
  104. int baudRate = 9600;
  105. myPorts[1] = new Serial(this, port, baudRate);
  106.  
  107. textarea_console.appendText("IMU #2 Connected.");
  108. textarea_console.appendText(" Port: " + port + " BaudRate: " + baudRate);
  109. }
  110.  
  111. /**
  112. * When SerialEvent is generated, it'll also give you
  113. * the port that generated it. Check that against a list
  114. * of the ports you know you opened to find out where
  115. * the data came from
  116. */
  117.  
  118. void serialEvent(Serial thisPort) {
  119. // variable to hold the number of the port:
  120. int portNumber = -1;
  121.  
  122. // iterate over the list of ports opened, and match the
  123. // one that generated this event:
  124. for (int p = 0; p < myPorts.length; p++) {
  125. if (thisPort == myPorts[p]) {
  126. portNumber = p;
  127. }
  128. }
  129.  
  130. // read a byte from the port:
  131. int inByte = thisPort.read();
  132.  
  133. if (portNumber == 0) {
  134. if (myIMU1.firstContact == false) {
  135. if (inByte == 'A') {
  136. myPorts[0].clear(); // clear the serial port buffer
  137. myIMU1.firstContact = true; // you've had first contact from the microcontroller
  138. myPorts[0].write('A'); // ask for more
  139. }
  140. }
  141. if (myIMU1.firstContact == true ) {
  142. String message = myPorts[0].readStringUntil('\n');
  143. if (message != null) {
  144. value = float(message);
  145. println(value);
  146. }
  147.  
  148. // Add the latest byte from the serial port to array:
  149. dataIn[0][myIMU1.serialCount] = value;
  150. myIMU1.serialCount++;
  151.  
  152. if (myIMU1.serialCount > 9) {
  153. // Send a capital A to request new sensor readings:
  154. thisPort.write('A');
  155. // Reset serialCount:
  156. myIMU1.serialCount = 0;
  157. }
  158.  
  159. /*
  160. // Add the latest byte from the serial port to array:
  161. dataIn[0][myIMU1.serialCount] = inByte;
  162. myIMU1.serialCount++;
  163.  
  164. // If we have 17 bytes:
  165. if (myIMU1.serialCount > 16) {
  166. myIMU1.accX = ((dataIn[0][0] << 8) | dataIn[0][1]);
  167. myIMU1.accY = ((dataIn[0][2] << 8) | dataIn[0][3]);
  168. myIMU1.accZ = ((dataIn[0][4] << 8) | dataIn[0][5]);
  169. myIMU1.tempRaw = ((dataIn[0][6] << 8) | dataIn[0][7]);
  170. myIMU1.gyroX = ((dataIn[0][8] << 8) | dataIn[0][9]);
  171. myIMU1.gyroY = ((dataIn[0][10] << 8) | dataIn[0][11]);
  172. myIMU1.gyroZ = ((dataIn[0][12] << 8) | dataIn[0][13]);
  173. myIMU1.compAngleX = dataIn[0][14];
  174. myIMU1.compAngleY = dataIn[0][15];
  175. myIMU1.tempCal = dataIn[0][16];
  176.  
  177. // Send a capital A to request new sensor readings:
  178. thisPort.write('A');
  179. // Reset serialCount:
  180. myIMU1.serialCount = 0;
  181. }
  182. */
  183. }
  184. }
  185.  
  186. // tell us who sent what:
  187. //println("Got " + inByte + " from serial port " + portNumber);
  188. }
  189.  
  190. /**
  191. * Use this method to add additional statements
  192. * to customise the GUI controls
  193. */
  194.  
  195. void customGUI() {
  196. textarea_console.setText ("##############################");
  197. textarea_console.appendText("GUI for xMotionPlus");
  198. textarea_console.appendText("IMU developed by PXL");
  199. textarea_console.appendText("Version 1.0, May 2014");
  200. textarea_console.appendText("");
  201. textarea_console.appendText("Glenn Kerselaers & Devlin Voets");
  202. textarea_console.appendText("##############################");
  203. textarea_console.appendText(" ");
  204.  
  205. window_graph.setVisible(false);
  206. window_animation.setVisible(false);
  207. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement