Celebrating MINDSTORMS with a Remix - Part 3
1 year ago
This blog is for all those who program the NXT Mindstorms robot in Lejos or those who wish to know more about such programming. Source Code available for download
package abstraction;
public interface Formula {
public float getResultOfFormula(float x);
}
package abstraction;
public class ImplementedFormula implements Formula {
public float getResultOfFormula(float x) {
return (float)Math.exp(x);
}
}
package abstraction;
public class Computational {
private abstraction.Formula userDefinedFormula = null;
public void defineFormula(abstraction.Formula f){
userDefinedFormula = f;
}
public float getValue(float y){
if(userDefinedFormula == null){
return 2*y;
}else{
return userDefinedFormula.getResultOfFormula(y);
}
}
}
package abstraction;
public class WorkingClass {
public void runThisClass(){
Computational comp = new Computational();
comp.defineFormula(new ImplementedFormula());
System.out.println(comp.getValue(30.0f));
}
public static void main(String[] args) {
WorkingClass wc = new WorkingClass();
wc.runThisClass();
}
}
public void removeFormula(){
userDefinedFormula = null;
}
Description: The user requests the battery voltage
Command 0, 0, 0
Reply millivoltage, 0, 0, 0, 0, 0, 0, 0
Description: The user asks for the ultrasonic sensor to ping and report back
Command 1,0,0
Reply A series of 8 integers being the echo values
Description: The user requests that the robot move forward/backward
Command 2,distance,speed
Reply A series of 8 integers being the echo values
Description The user requests that the robot turn left/right
Command 3,angle,speed
Reply A series of 8 integers being the echo values
Description The user requests that the Bluetooth connection be terminated
Command 4,0,0
Reply 255,255,255,255,255,255,255,255
public void clickConnectButton(){
if(connectButton.getText().equals("Disconnect")){
connectButton.doClick();
}
}
addressField.setText("00:16:53:00:57:37");
public void setButtonsEnabled(boolean setting){
initialize.setEnabled(setting);
move.setEnabled(setting);
turn.setEnabled(setting);
speedSlider.setEnabled(setting);
}
package local; import java.awt.event.*; public class EventHandler implements ActionListener { public void actionPerformed(ActionEvent e){ } }
EventHandler eventHndl = new EventHandler(); Statistics statsPanel = new Statistics(eventHndl);
statsPanel.setButtonsEnabled(true);
import lejos.nxt.Button;
import lejos.nxt.LCD;
import lejos.nxt.SensorPort;
import lejos.nxt.comm.*;
import lejos.nxt.addon.*;
import lejos.nxt.*;
public class EOPDSensor2 implements ButtonListener{
private boolean keepItRunning = true;
public void buttonPressed(Button b){
keepItRunning = false;
}
public void buttonReleased(Button b){}
public void runProgram(){
Button.ESCAPE.addButtonListener(this);
RConsole.openBluetooth(40000);
LCD.drawString("Connected", 0, 2);
EOPD electroSensor = new EOPD(SensorPort.S1);
while(keepItRunning){
electroSensor.setModeLong();
RConsole.println(Integer.toString(
electroSensor.readRawValue()));
RConsole.println(Integer.toString(
electroSensor.processedValue()));
try{
Thread.sleep(5000);
}catch(InterruptedException e){
System.exit(0);
}
}
RConsole.println("Closing...");
RConsole.close();
}
public static void main(String[] args){
EOPDSensor2 programInstance = new EOPDSensor2();
programInstance.runProgram();
}
}
The methods of the RConsole class are all static; the methods belong to the class and not to any instance of the class. So, the RConsole class should not be instantiated with the new keyword. The methods should be called in the form RConsole.method().
import lejos.nxt.Button;
import lejos.nxt.LCD;
import lejos.nxt.SensorPort;
import lejos.nxt.comm.*;
import lejos.nxt.addon.*;
public class EOPDSensor {
/**
* @param args
*/
public static void main(String[] args) {
RConsole.openBluetooth(40000);
LCD.drawString("Connected", 0, 2);
EOPD electroSensor = new EOPD(SensorPort.S1);
while(!Button.ESCAPE.isPressed()){
electroSensor.setModeLong();
RConsole.println(Integer.toString(
electroSensor.readRawValue()));
RConsole.println(Integer.toString(
electroSensor.processedValue()));
try{
Thread.sleep(5000);
}catch(InterruptedException e){
System.exit(0);
}
}
RConsole.println("Closing...");
RConsole.close();
}
}