Advertisement
Guest User

Untitled

a guest
Dec 11th, 2017
62
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.21 KB | None | 0 0
  1. import java.util.ArrayList;
  2. import jssc.*;
  3. import java.util.*;
  4.  
  5. /**
  6. * <h1>IRobot Communication</h1>
  7. * The IRobot class acts as the middle man between
  8. * the main AI and the IRobot. The java simple serial
  9. * connector is used for the communication to make
  10. * the whole thing as simple as possible.
  11. */
  12. public class IRobot{
  13.  
  14. private SerialPort sPort;
  15. private static ArrayList<Integer> opcodes = new ArrayList<Integer>();
  16. private byte[] bytes = new byte[256];
  17. private int[] senserData = new int[51];
  18.  
  19. public static void main(String args[]) {
  20. IRobot bot = new IRobot();
  21. Scanner input = new Scanner(System.in);
  22. int command;
  23. int[] full = {128, 132};
  24. int[] left = {137, 1, 44, 0, 1};
  25. int[] drive = {137, 0, 100, 128, 0};
  26. int[] right = {137, 1, 44, 1, 0};
  27. int[] stop = {173};
  28.  
  29.  
  30. while(true) {
  31. command = input.nextInt();
  32. switch(command) {
  33. case 5:
  34. bot.IO_add(full);
  35. bot.IO_send();
  36. break;
  37. case 4:
  38. bot.IO_add(left);
  39. bot.IO_send();
  40. break;
  41. case 8:
  42. bot.IO_add(drive);
  43. bot.IO_send();
  44. break;
  45. case 6:
  46. bot.IO_add(right);
  47. bot.IO_send();
  48. break;
  49. case 2:
  50. bot.IO_add(stop);
  51. bot.IO_send();
  52. }
  53. }
  54. }
  55.  
  56.  
  57. public IRobot(){
  58.  
  59. //
  60. String[] ports = SerialPortList.getPortNames();
  61. for(int i =0;i<ports.length;i++) {
  62. System.out.println(ports[i]);
  63. }
  64.  
  65. // a small speed up helper
  66. for(int i=0; i<bytes.length; i++) {
  67. bytes[i] = (byte) (i & 0xFF);
  68. }
  69.  
  70. // start the connection
  71. try {
  72. sPort = new SerialPort("COM5");
  73. sPort.openPort();
  74. sPort.setParams(57600, 8, 1, 0);
  75. } catch (SerialPortException e){
  76. e.printStackTrace();
  77. }
  78.  
  79. // start the giving power to the kinect
  80. // IO_start();
  81. // opcodes.add(131);
  82. // opcodes.add(144);
  83. // opcodes.add(0);
  84. // opcodes.add(0);
  85. // opcodes.add(127);
  86. // opcodes.add(0);
  87. // IO_send();
  88. // IO_start();
  89. // opcodes.add(131);
  90. // opcodes.add(144);
  91. // opcodes.add(0);
  92. // opcodes.add(0);
  93. // opcodes.add(127);
  94. // opcodes.add(0);
  95. // IO_send();
  96.  
  97. // add an event listener to the port
  98. try {
  99. sPort.addEventListener(new SerialPortEventListener() {
  100. public void serialEvent(SerialPortEvent event) {
  101. listenerEvent(event);
  102. }
  103. });
  104. } catch (SerialPortException e) {
  105. e.printStackTrace();
  106. }
  107.  
  108. }
  109.  
  110. private void listenerEvent(SerialPortEvent event){
  111. if(event.isRXCHAR()){//If data is available
  112. try {
  113. System.out.println("HexString = " + sPort.readHexString());
  114. // byte[] got = sPort.readBytes();
  115. // System.out.println("HexString = " + sPort.readHexString());
  116. // parseInput(got);
  117. // System.out.println("Distance = " + distance);
  118. } catch (SerialPortException e) {
  119. e.printStackTrace();
  120. }
  121. } else if(event.isCTS()){//If CTS line has changed state
  122. if(event.getEventValue() == 1){//If line is ON
  123. System.out.println("CTS - ON");
  124. } else {
  125. System.out.println("CTS - OFF");
  126. }
  127. } else if(event.isDSR()){///If DSR line has changed state
  128. if(event.getEventValue() == 1){//If line is ON
  129. System.out.println("DSR - ON");
  130. } else {
  131. System.out.println("DSR - OFF");
  132. }
  133. }
  134. }
  135. //
  136. private void parseInput(byte[] b){
  137. byte header = b[0]; // I keep getting 13, I don't really know why...
  138. byte nBytes = b[1]; //the number of bytes between the n-bytes byte and the checksum.
  139.  
  140. if (header != 13) return;
  141.  
  142. // the rest if for debug only
  143. // b[2] = 13
  144. setDistance(b[3], b[4]);
  145. }
  146.  
  147. /**
  148. * This method is used to send the list of commands
  149. * and clear the opcode list.
  150. */
  151. public void IO_send(){
  152. try {
  153. for(int i = 0;i<opcodes.size(); i++) {
  154. sPort.writeByte(bytes[opcodes.get(i)]);
  155. }
  156. } catch (SerialPortException e) {
  157. e.printStackTrace();
  158. }
  159. opcodes.clear();
  160. }
  161.  
  162. public void IO_start(){
  163. opcodes.add(128);
  164. }
  165. public void IO_reset(){
  166. opcodes.add(7);
  167. }
  168. public void IO_stop(){
  169. opcodes.add(173);
  170. }
  171.  
  172. // an easy way to test commands
  173. public void IO_add(int[] buffer){
  174. for(int i = 0;i<buffer.length;i++){
  175. opcodes.add(buffer[i]);
  176. }
  177. }
  178.  
  179. public void setMode(boolean safe){
  180. if (safe)
  181. opcodes.add(131);
  182. else
  183. opcodes.add(132);
  184. }
  185.  
  186. // TODO: the simple methods of just sending bytes
  187. // seekDock()
  188. // Drive(Velocity, Radius)
  189. // DriveDirect(Right velocity, Left velocity)
  190. // DrivePWM(Right PWM, Left PWM)
  191. // LEDs // there are 4
  192. // Digit_LEDs(d1, d2, d3)
  193. public void setSong(int songIndex, int[] song){
  194. opcodes.add(140);
  195. opcodes.add(songIndex);
  196. opcodes.add(song.length/2);
  197. for(int i = 0;i<song.length;i++) {
  198. opcodes.add(song[i]);
  199. }
  200. }
  201. public void playSong(int songIndex){
  202. opcodes.add(141);
  203. opcodes.add(songIndex);
  204. }
  205.  
  206. // TODO: Input Commands
  207. public void sensors_update(){
  208. opcodes.add(148);
  209. opcodes.add(1);
  210. opcodes.add(7);
  211. }
  212.  
  213. // Distance - Packet ID: 19 - Data Bytes: 2, signed
  214. private int distance = 0;
  215. private void setDistance(int lByte, int rByte){
  216. int val = ((lByte & 0xFF) << 8) | (rByte & 0xFF);
  217. distance += val;
  218. }
  219. public int getDistance(){
  220. int dis = distance;
  221. //distance = 0;
  222. return dis;
  223. }
  224. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement