Skip to content

Instantly share code, notes, and snippets.

@paulaksm
Created March 20, 2018 14:02
Show Gist options
  • Save paulaksm/ed11dcfedf9df4c3b7571223a20fa93a to your computer and use it in GitHub Desktop.
Save paulaksm/ed11dcfedf9df4c3b7571223a20fa93a to your computer and use it in GitHub Desktop.
Python <-> Java
/* 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 NXTpy
{
public static DataOutputStream dataOut;
public static DataInputStream dataIn;
public static USBConnection USBLink;
public static int speed =70, 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){
if (dataIn.available() > 0){
byte x = dataIn.readByte();
control = checkCommand((int)x);
if (control[6] == true)
disconnect();
speedControl = getSpeed(control);
move(control, speedControl);
}
}
}//End main
public static boolean[] checkCommand(int transmitReceived)//check input data
{
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{};
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(0, NXTConnection.RAW);
dataOut = USBLink.openDataOutputStream();
dataIn = USBLink.openDataInputStream();
}//End connect
public static void disconnect() throws java.io.IOException
{
// System.out.println("Closing press anu key...");
System.out.println("Closing...");
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
}//NXTpy Class
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment