Advertisement
Guest User

Untitled

a guest
Dec 15th, 2017
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.95 KB | None | 0 0
  1. import java.util.ArrayList;
  2. import jssc.*;
  3. import java.util.*;
  4. import java.sql.*;
  5.  
  6. /**
  7. * <h1>IRobot Communication</h1>
  8. * The IRobot class acts as the middle man between
  9. * the main AI and the IRobot. The java simple serial
  10. * connector is used for the communication to make
  11. * the whole thing as simple as possible.
  12. */
  13. public class IRobot{
  14.  
  15. private SerialPort sPort;
  16. private static ArrayList<Integer> opcodes = new ArrayList<Integer>();
  17. private byte[] bytes = new byte[256];
  18. public static String[] sensorName = {"Bump Sensor: ", "Wall Sensor: ", "Cliff Left: ", "Cliff Front Left: ", "Cliff Front Right: ", "Cliff Right: ", "Virtual Wall Sensor: " };
  19.  
  20. public static void main(String args[]) {
  21. IRobot bot = new IRobot();
  22. Scanner input = new Scanner(System.in);
  23. int command;
  24. bot.full();
  25. System.out.println("The commands correspond to the arrows on your computer's number pad.\n");
  26. System.out.println("Move forward\t\t\t8");
  27. System.out.println("Turn counter-clockwise\t\t4");
  28. System.out.println("Turn clockwise\t\t\t6");
  29. System.out.println("Stop\t\t\t\t5");
  30. System.out.println("Reverse\t\t\t\t2");
  31. System.out.println("Send sensor data to server\t1");
  32. System.out.println("Print data from server\t\t7");
  33. while(true) {
  34. command = input.nextInt();
  35. switch(command) {
  36. case 4:
  37. bot.left();
  38. break;
  39. case 8:
  40. bot.drive();
  41. break;
  42. case 6:
  43. bot.right();
  44. break;
  45. case 5:
  46. bot.stop();
  47. break;
  48. case 2:
  49. bot.reverse();
  50. break;
  51. //sensor input
  52. case 1:
  53. bot.inputSensorData();
  54. break;
  55. case 7:
  56. bot.printSensorData();
  57. break;
  58. }
  59. }
  60. }
  61.  
  62. public void full() {
  63. int[] full = {128, 132};
  64. IO_add(full);
  65. IO_send();
  66. }
  67.  
  68. public void left() {
  69. int[] left = {137, 254, 12, 255, 255};
  70. IO_add(left);
  71. IO_send();
  72. }
  73.  
  74. public void drive() {
  75. int[] drive = {137, 0, 100, 128, 0};
  76. IO_add(drive);
  77. IO_send();
  78. }
  79.  
  80. public void reverse() {
  81. int[] reverse = {137, 255, 56, 128, 0};
  82. IO_add(reverse);
  83. IO_send();
  84. }
  85.  
  86. public void right() {
  87. int[] right = {137, 254, 12, 0, 1};
  88. IO_add(right);
  89. IO_send();
  90. }
  91.  
  92. public void stop() {
  93. int[] stop = {137, 0, 0, 0, 0};
  94. IO_add(stop);
  95. IO_send();
  96. }
  97.  
  98. public void inputSensorData() {
  99. int[] inputSensorData = {149, 7, 7, 8, 9, 10, 11, 12, 13}; //bump, wall, cliff L, cliff FL, cliff FR, cliff R, Virtual Wall
  100. IO_add(inputSensorData);
  101. IO_send();
  102. }
  103.  
  104. public void printSensorData() {
  105. printSensorData(sensorName);
  106. }
  107.  
  108. public IRobot(){
  109.  
  110. // puts the bytes into a byte array
  111. for(int i=0; i<bytes.length; i++) {
  112. bytes[i] = (byte) (i & 0xFF);
  113. }
  114.  
  115. // start the connection to COM Number
  116. try {
  117. sPort = new SerialPort("COM5");
  118. sPort.openPort();
  119. sPort.setParams(57600, 8, 1, 0);
  120. } catch (SerialPortException e){
  121. e.printStackTrace();
  122. }
  123.  
  124. // add an event listener to the port
  125. try {
  126. sPort.addEventListener(new SerialPortEventListener() {
  127. public void serialEvent(SerialPortEvent event) {
  128. listenerEvent(event);
  129. }
  130. });
  131. } catch (SerialPortException e) {
  132. e.printStackTrace();
  133. }
  134.  
  135. }
  136.  
  137. private void listenerEvent(SerialPortEvent event){
  138. if(event.isRXCHAR()){//If data is available
  139. try {
  140. String[] data = new String[7];
  141. String inString = sPort.readHexString(); //puts the input into a string
  142. String[] sepString = inString.split(" "); //splits the input string into a string array
  143.  
  144.  
  145. for(int i = 0; i < sepString.length; i++) { //outputs the sensor detection
  146. if (sepString[i].equals("00")) {
  147. data[i] = "No Detection";
  148. }
  149. else {
  150. data[i] = "Detection";
  151. }
  152. }
  153. addSensorData(data);
  154. } catch (SerialPortException e) {
  155. e.printStackTrace();
  156. }
  157. } else if(event.isCTS()){//If CTS line has changed state
  158. if(event.getEventValue() == 1){//If line is ON
  159. System.out.println("CTS - ON");
  160. } else {
  161. System.out.println("CTS - OFF");
  162. }
  163. } else if(event.isDSR()){///If DSR line has changed state
  164. if(event.getEventValue() == 1){//If line is ON
  165. System.out.println("DSR - ON");
  166. } else {
  167. System.out.println("DSR - OFF");
  168. }
  169. }
  170. }
  171.  
  172.  
  173. //this method is used to send the command array opcodes to the iRobot
  174. public void IO_send(){
  175. try {
  176. for(int i = 0;i<opcodes.size(); i++) {
  177. sPort.writeByte(bytes[opcodes.get(i)]);
  178. }
  179. } catch (SerialPortException e) {
  180. e.printStackTrace();
  181. }
  182. opcodes.clear();
  183. }
  184.  
  185.  
  186. // adds commands to the command array opcodes
  187. public void IO_add(int[] buffer){
  188. for(int i = 0;i<buffer.length;i++){
  189. opcodes.add(buffer[i]);
  190. }
  191. }
  192.  
  193. public void addSensorData(String[] s) {
  194. Connection connection = null;
  195. try {
  196. Class.forName("com.mysql.jdbc.Driver").newInstance();
  197. } catch (Exception e) {
  198. System.err.println(e.toString());
  199. }
  200. String url = "jdbc:mysql://cs2043.cs.unb.ca:3306/cs2043team14";
  201. try {
  202. connection = DriverManager.getConnection(url, "cs2043team14", "j9HOVQ9O");
  203. Statement st = connection.createStatement();
  204. st.executeUpdate("DELETE FROM RealData;");
  205. st.executeUpdate("INSERT INTO RealData " +
  206. "VALUES ('" + s[0] + "', '" + s[1] + "', '" + s[2] + "', '" + s[3]+ "', '" + s[4] + "', '" + s[5] + "', '" + s[6] + "');");
  207. System.out.println("Data was recieved");
  208. } catch (SQLException e) {
  209. System.err.println(e.toString());
  210. }
  211.  
  212. }
  213.  
  214. public static void printSensorData(String[] sensorName) {
  215. Connection connection = null;
  216. try {
  217. Class.forName("com.mysql.jdbc.Driver").newInstance();
  218. } catch (Exception e) {
  219. System.err.println(e.toString());
  220. }
  221. String url = "jdbc:mysql://cs2043.cs.unb.ca:3306/cs2043team14";
  222. try {
  223. connection = DriverManager.getConnection(url, "cs2043team14", "j9HOVQ9O");
  224. Statement st = connection.createStatement();
  225. ResultSet rs = st.executeQuery("SELECT * FROM RealData;");
  226. while(rs.next()) {
  227. System.out.println(sensorName[0] + rs.getString(1));
  228. System.out.println(sensorName[1] + rs.getString(2));
  229. System.out.println(sensorName[2] + rs.getString(3));
  230. System.out.println(sensorName[3] + rs.getString(4));
  231. System.out.println(sensorName[4] + rs.getString(5));
  232. System.out.println(sensorName[5] + rs.getString(6));
  233. System.out.println(sensorName[6] + rs.getString(7));
  234. }
  235. } catch (SQLException e) {
  236. System.err.println(e.toString());
  237. }
  238.  
  239. }
  240.  
  241. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement