Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.ArrayList;
- import jssc.*;
- import java.util.*;
- import java.sql.*;
- /**
- * <h1>IRobot Communication</h1>
- * The IRobot class acts as the middle man between
- * the main AI and the IRobot. The java simple serial
- * connector is used for the communication to make
- * the whole thing as simple as possible.
- */
- public class IRobot{
- private SerialPort sPort;
- private static ArrayList<Integer> opcodes = new ArrayList<Integer>();
- private byte[] bytes = new byte[256];
- public static String[] sensorName = {"Bump Sensor: ", "Wall Sensor: ", "Cliff Left: ", "Cliff Front Left: ", "Cliff Front Right: ", "Cliff Right: ", "Virtual Wall Sensor: " };
- public static void main(String args[]) {
- IRobot bot = new IRobot();
- Scanner input = new Scanner(System.in);
- int command;
- bot.full();
- System.out.println("The commands correspond to the arrows on your computer's number pad.\n");
- System.out.println("Move forward\t\t\t8");
- System.out.println("Turn counter-clockwise\t\t4");
- System.out.println("Turn clockwise\t\t\t6");
- System.out.println("Stop\t\t\t\t5");
- System.out.println("Reverse\t\t\t\t2");
- System.out.println("Send sensor data to server\t1");
- System.out.println("Print data from server\t\t7");
- while(true) {
- command = input.nextInt();
- switch(command) {
- case 4:
- bot.left();
- break;
- case 8:
- bot.drive();
- break;
- case 6:
- bot.right();
- break;
- case 5:
- bot.stop();
- break;
- case 2:
- bot.reverse();
- break;
- //sensor input
- case 1:
- bot.inputSensorData();
- break;
- case 7:
- bot.printSensorData();
- break;
- }
- }
- }
- public void full() {
- int[] full = {128, 132};
- IO_add(full);
- IO_send();
- }
- public void left() {
- int[] left = {137, 254, 12, 255, 255};
- IO_add(left);
- IO_send();
- }
- public void drive() {
- int[] drive = {137, 0, 100, 128, 0};
- IO_add(drive);
- IO_send();
- }
- public void reverse() {
- int[] reverse = {137, 255, 56, 128, 0};
- IO_add(reverse);
- IO_send();
- }
- public void right() {
- int[] right = {137, 254, 12, 0, 1};
- IO_add(right);
- IO_send();
- }
- public void stop() {
- int[] stop = {137, 0, 0, 0, 0};
- IO_add(stop);
- IO_send();
- }
- public void inputSensorData() {
- int[] inputSensorData = {149, 7, 7, 8, 9, 10, 11, 12, 13}; //bump, wall, cliff L, cliff FL, cliff FR, cliff R, Virtual Wall
- IO_add(inputSensorData);
- IO_send();
- }
- public void printSensorData() {
- printSensorData(sensorName);
- }
- public IRobot(){
- // puts the bytes into a byte array
- for(int i=0; i<bytes.length; i++) {
- bytes[i] = (byte) (i & 0xFF);
- }
- // start the connection to COM Number
- try {
- sPort = new SerialPort("COM5");
- sPort.openPort();
- sPort.setParams(57600, 8, 1, 0);
- } catch (SerialPortException e){
- e.printStackTrace();
- }
- // add an event listener to the port
- try {
- sPort.addEventListener(new SerialPortEventListener() {
- public void serialEvent(SerialPortEvent event) {
- listenerEvent(event);
- }
- });
- } catch (SerialPortException e) {
- e.printStackTrace();
- }
- }
- private void listenerEvent(SerialPortEvent event){
- if(event.isRXCHAR()){//If data is available
- try {
- String[] data = new String[7];
- String inString = sPort.readHexString(); //puts the input into a string
- String[] sepString = inString.split(" "); //splits the input string into a string array
- for(int i = 0; i < sepString.length; i++) { //outputs the sensor detection
- if (sepString[i].equals("00")) {
- data[i] = "No Detection";
- }
- else {
- data[i] = "Detection";
- }
- }
- addSensorData(data);
- } catch (SerialPortException e) {
- e.printStackTrace();
- }
- } else if(event.isCTS()){//If CTS line has changed state
- if(event.getEventValue() == 1){//If line is ON
- System.out.println("CTS - ON");
- } else {
- System.out.println("CTS - OFF");
- }
- } else if(event.isDSR()){///If DSR line has changed state
- if(event.getEventValue() == 1){//If line is ON
- System.out.println("DSR - ON");
- } else {
- System.out.println("DSR - OFF");
- }
- }
- }
- //this method is used to send the command array opcodes to the iRobot
- public void IO_send(){
- try {
- for(int i = 0;i<opcodes.size(); i++) {
- sPort.writeByte(bytes[opcodes.get(i)]);
- }
- } catch (SerialPortException e) {
- e.printStackTrace();
- }
- opcodes.clear();
- }
- // adds commands to the command array opcodes
- public void IO_add(int[] buffer){
- for(int i = 0;i<buffer.length;i++){
- opcodes.add(buffer[i]);
- }
- }
- public void addSensorData(String[] s) {
- Connection connection = null;
- try {
- Class.forName("com.mysql.jdbc.Driver").newInstance();
- } catch (Exception e) {
- System.err.println(e.toString());
- }
- String url = "jdbc:mysql://cs2043.cs.unb.ca:3306/cs2043team14";
- try {
- connection = DriverManager.getConnection(url, "cs2043team14", "j9HOVQ9O");
- Statement st = connection.createStatement();
- st.executeUpdate("DELETE FROM RealData;");
- st.executeUpdate("INSERT INTO RealData " +
- "VALUES ('" + s[0] + "', '" + s[1] + "', '" + s[2] + "', '" + s[3]+ "', '" + s[4] + "', '" + s[5] + "', '" + s[6] + "');");
- System.out.println("Data was recieved");
- } catch (SQLException e) {
- System.err.println(e.toString());
- }
- }
- public static void printSensorData(String[] sensorName) {
- Connection connection = null;
- try {
- Class.forName("com.mysql.jdbc.Driver").newInstance();
- } catch (Exception e) {
- System.err.println(e.toString());
- }
- String url = "jdbc:mysql://cs2043.cs.unb.ca:3306/cs2043team14";
- try {
- connection = DriverManager.getConnection(url, "cs2043team14", "j9HOVQ9O");
- Statement st = connection.createStatement();
- ResultSet rs = st.executeQuery("SELECT * FROM RealData;");
- while(rs.next()) {
- System.out.println(sensorName[0] + rs.getString(1));
- System.out.println(sensorName[1] + rs.getString(2));
- System.out.println(sensorName[2] + rs.getString(3));
- System.out.println(sensorName[3] + rs.getString(4));
- System.out.println(sensorName[4] + rs.getString(5));
- System.out.println(sensorName[5] + rs.getString(6));
- System.out.println(sensorName[6] + rs.getString(7));
- }
- } catch (SQLException e) {
- System.err.println(e.toString());
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement