Created
March 3, 2018 13:28
-
-
Save paulaksm/6bbf347bd9803ea3288a93ca6b479b47 to your computer and use it in GitHub Desktop.
Brick code for NXTremoteControl_TA.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* Application for brick, compiled with nxjc | |
Client-Server architecture | |
Commands sent by NXTremoteControl_TA.java*/ | |
import java.io.*; | |
import lejos.nxt.*; | |
import lejos.nxt.comm.*; | |
public class NXTtr | |
{ | |
public static DataOutputStream dataOut; | |
public static DataInputStream dataIn; | |
public static USBConnection USBLink; | |
public static int speed =50, turnSpeed = 50,speedBuffer, speedControl; | |
public static int commandValue,transmitReceived; | |
public static boolean[] control = new boolean[7]; | |
public static boolean[] command = new boolean[7]; | |
public static void main(String [] args) throws Exception | |
{ | |
connect(); | |
while(true) | |
{ | |
control = checkCommand(); | |
if (control[6] == true) | |
disconnect(); | |
speedControl = getSpeed(control); | |
move(control, speedControl); | |
} | |
}//End main | |
public static boolean[] checkCommand()//check input data | |
{ | |
try { | |
transmitReceived = dataIn.readInt(); | |
if(transmitReceived == 1) {command[0] = true;}//forward | |
if(transmitReceived == 10){command[0] = false;} | |
if(transmitReceived == 2) {command[1] = true;}//backward | |
if(transmitReceived == 20){command[1] = false;} | |
if(transmitReceived == 3) {command[2] = true;}//leftTurn | |
if(transmitReceived == 30){command[2] = false;} | |
if(transmitReceived == 4) {command[3] = true;}//rightTurn | |
if(transmitReceived == 40){command[3] = false;} | |
if(transmitReceived == 5) {command[0] = false;//stop | |
command[1] = false; | |
command[2] = false; | |
command[3] = false;} | |
if(transmitReceived == 6) {command[4] = true;}//speed up | |
if(transmitReceived == 60){command[4] = false;} | |
if(transmitReceived == 7) {command[5] = true;}//slow down | |
if(transmitReceived == 70){command[5] = false;} | |
if(transmitReceived == 100){command[6] = true;} // closes connection | |
else{}; | |
} | |
catch (IOException ioe) { | |
System.out.println("IO Exception readInt"); | |
} | |
return command; | |
}//End checkCommand | |
public static void move(boolean[]D, int S) | |
{ | |
int movingSpeed; | |
boolean[] direction = new boolean[4]; | |
direction[0] = D[0]; | |
direction[1] = D[1]; | |
direction[2] = D[2]; | |
direction[3] = D[3]; | |
movingSpeed = S; | |
Motor.A.setSpeed(movingSpeed); | |
Motor.C.setSpeed(movingSpeed); | |
if(direction[0] == true) | |
{ | |
Motor.A.forward(); | |
Motor.C.forward(); | |
} | |
if(direction[1] == true) | |
{ | |
Motor.A.backward(); | |
Motor.C.backward(); | |
} | |
if(direction[2] == true) | |
{ | |
Motor.A.setSpeed(turnSpeed); | |
Motor.C.setSpeed(turnSpeed); | |
Motor.A.forward(); | |
Motor.C.backward(); | |
} | |
if(direction[3] == true) | |
{ | |
Motor.A.setSpeed(turnSpeed); | |
Motor.C.setSpeed(turnSpeed); | |
Motor.A.backward(); | |
Motor.C.forward(); | |
} | |
if(direction[0] == true && direction[2] == true) | |
{ | |
speedBuffer = (int) (movingSpeed *1.5); | |
Motor.A.setSpeed(speedBuffer); | |
Motor.C.forward(); | |
Motor.A.forward(); | |
} | |
if(direction[0] == true && direction[3] == true) | |
{ | |
speedBuffer = (int) (movingSpeed *1.5); | |
Motor.C.setSpeed(speedBuffer); | |
Motor.C.forward(); | |
Motor.A.forward(); | |
} | |
if(direction[1] == true && direction[2] == true) | |
{ | |
speedBuffer = (int) (movingSpeed *1.5); | |
Motor.A.setSpeed(speedBuffer); | |
Motor.C.backward(); | |
Motor.A.backward(); | |
} | |
if(direction[1] == true && direction[3] == true) | |
{ | |
speedBuffer = (int) (movingSpeed *1.5); | |
Motor.C.setSpeed(speedBuffer); | |
Motor.C.backward(); | |
Motor.A.backward(); | |
} | |
if(direction[0] == false && direction[1] == false && | |
direction[2] == false && direction[3] == false) | |
{ | |
Motor.A.stop(true); | |
Motor.C.stop(); | |
} | |
}//End move | |
public static void connect() | |
{ | |
System.out.println("Listening"); | |
USBLink = USB.waitForConnection(); | |
dataOut = USBLink.openDataOutputStream(); | |
dataIn = USBLink.openDataInputStream(); | |
}//End connect | |
public static void disconnect() throws java.io.IOException | |
{ | |
System.out.println("Closing press anu key..."); | |
dataOut.close(); | |
dataIn.close(); | |
Button.waitForAnyPress(); | |
USB.usbReset(); | |
System.exit(0); | |
}//End connect | |
public static int getSpeed(boolean[] D) | |
{ | |
boolean accelerate, decelerate; | |
accelerate = D[4]; | |
decelerate = D[5]; | |
if(accelerate == true) | |
{ | |
speed += 50; | |
command[4] = false; | |
} | |
if(decelerate == true) | |
{ | |
speed -= 50; | |
command[5] = false; | |
} | |
return speed; | |
}//End getSpeed | |
}//NXTtr Class |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment