Moderators: roger, 99jonathan, imaqine

import lejos.nxt.Motor;
import org.jcsp.nxt.io.ActiveButtons;
import org.jcsp.nxt.io.ActiveLightSensor;
import org.jcsp.nxt.io.ActiveMotor;
import org.jcsp.nxt.lang.Alternative;
import org.jcsp.nxt.lang.AltingChannelInputInt;
import org.jcsp.nxt.lang.CSProcess;
import org.jcsp.nxt.lang.ChannelInputInt;
import org.jcsp.nxt.lang.ChannelOutputInt;
import org.jcsp.nxt.lang.Guard;
import org.jcsp.nxt.lang.One2OneChannel;
import org.jcsp.nxt.lang.One2OneChannelInt;
import org.jcsp.nxt.lang.One2OneChannelIntImpl;
import org.jcsp.nxt.lang.Parallel;
/**
* @author NewIrelandComputing
*
*/
public class logic implements CSProcess {
int id1 = 1;
int id2 = 2;
int id3 = 3;
private ChannelOutputInt buttonPress = new One2OneChannelIntImpl();
private Object lightLeft;
private Object lightCenter;
private Object lightRight;
private Object lightLevelLeft;
private Object lightLevelCenter;
private Object lightLevelRight;
private AltingChannelInputInt filterOutputWhiteLeft;
private AltingChannelInputInt filterOutputWhiteCenter;
private AltingChannelInputInt filterOutputWhiteRight;
private AltingChannelInputInt filterOutputBlackLeft;
private AltingChannelInputInt filterOutputBlackCenter;
private AltingChannelInputInt filterOutputBlackRight;
private ChannelOutputInt lightConfigLeft;
private ChannelOutputInt lightConfigCenter;
private ChannelOutputInt lightConfigRight;
private ChannelOutputInt filterConfigLeft;
private ChannelOutputInt filterConfigCenter;
private ChannelOutputInt filterConfigRight;
private Parallel par = new Parallel();
private Motor m1 = Motor.A;
private Motor m2 = Motor.B;
private ChannelOutputInt motorSpeed;
private ChannelOutputInt buttonPressRtn;
public logic(ChannelInputInt levelLeft,
ChannelInputInt levelCenter,
ChannelInputInt levelRight,
AltingChannelInputInt filterOutWhiteLeft,
AltingChannelInputInt filterOutWhiteCenter,
AltingChannelInputInt filterOutWhiteRight,
AltingChannelInputInt filterOutBlackLeft,
AltingChannelInputInt filterOutBlackCenter,
AltingChannelInputInt filterOutBlackRight,
ChannelOutputInt configLeft,
ChannelOutputInt configCenter,
ChannelOutputInt configRight,
ChannelOutputInt filterConfigLeft,
ChannelOutputInt filterConfigCenter,
ChannelOutputInt filterConfigRight){
ActiveButtons press = new ActiveButtons(buttonPress.in());
par.addProcess(press);
this.buttonPressRtn = buttonPress;
this.lightLevelLeft = lightLeft;
this.lightLevelCenter = lightCenter;
this.lightLevelRight = lightRight;
this.filterOutputWhiteLeft = filterOutWhiteLeft;
this.filterOutputWhiteCenter = filterOutWhiteCenter;
this.filterOutputWhiteRight = filterOutWhiteRight;
this.filterOutputBlackLeft = filterOutBlackLeft;
this.filterOutputBlackCenter = filterOutBlackCenter;
this.filterOutputBlackRight = filterOutBlackRight;
this.lightConfigLeft = configLeft;
this.lightConfigCenter = configCenter;
this.lightConfigRight = configRight;
this.filterConfigLeft = filterConfigLeft;
this.filterConfigCenter = filterConfigCenter;
this.filterConfigRight = filterConfigRight;
this.id1 = 1;
this.id2 = 2;
this.id3 = 3;
par.addProcess(new ActiveMotor( m1, motorSpeed.in()));
par.addProcess(new ActiveMotor( m2, motorSpeed.in()));
this.motorSpeed = motorSpeed.out();
}
public void run() {
par.run();
int rotate = 0;
int noRotate = -2;
int altIndex = -1;
buttonPressRtn.read(); // configure black level
//Display.print("B");
//Display.print(id);
lightConfigLeft.write(ActiveLightSensor.LEVEL);
lightConfigCenter.write(ActiveLightSensor.LEVEL);
lightConfigRight.write(ActiveLightSensor.LEVEL);
//Display.print("C");
//Display.print(id);
int lowleft = ((ChannelInputInt) lightLevelLeft).read();
int lowcenter = ((ChannelInputInt) lightLevelCenter).read();
int lowright = ((ChannelInputInt) lightLevelRight).read();
//Display.print("L");
//Display.print(low);
buttonPress.read(); // configure white level
//Display.print("W");
//Display.print(id);
lightConfigLeft.write(ActiveLightSensor.LEVEL);
lightConfigCenter.write(ActiveLightSensor.LEVEL);
lightConfigRight.write(ActiveLightSensor.LEVEL);
//Display.print("C");
//Display.print(id);
int highLeft = ((ChannelInputInt) lightLevelLeft).read();
int highCenter = ((ChannelInputInt) lightLevelCenter).read();
int highRight = ((ChannelInputInt) lightLevelRight).read();
//Display.print("H");
//Display.print(high);
int midLeft = lowleft + ((highLeft - lowleft)/2);
int midCenter = lowcenter + ((highCenter - lowcenter)/2);
int midRight = lowright + ((highRight - lowright)/2);
//Display.print("M");
//Display.print(mid);
filterConfigLeft.write( midLeft ); // write mid point value to the filter
filterConfigCenter.write( midCenter ); // write mid point value to the filter
filterConfigRight.write( midRight ); // write mid point value to the filter
//Display.print("F");
//Display.print(id);
lightConfigLeft.write(ActiveLightSensor.ACTIVATE);
lightConfigCenter.write(ActiveLightSensor.ACTIVATE);
lightConfigRight.write(ActiveLightSensor.ACTIVATE);
//Display.print("A");
Alternative a = new Alternative( new Guard[] { filterOutputWhiteLeft, filterOutputWhiteCenter, filterOutputWhiteRight, filterOutputBlackLeft, filterOutputBlackCenter, filterOutputBlackRight, (Guard) buttonPress } );
while (true) {
//rotate = buttonPress.read(); // read speed of wheel rotation
//Display.println( rotate );
buttonPress.read(); // the go signal
boolean going = true;
while (going) {
altIndex = a.fairSelect();
if (altIndex == 0) {
filterOutputWhiteLeft.read();
if(altIndex == 2){
filterOutputWhiteRight.read();
if(altIndex == 4){
filterOutputBlackCenter.read();
m1.forward();
m2.forward();
}
}else if ( altIndex == 5){
filterOutputBlackRight.read();
if(altIndex == 1){
filterOutputWhiteCenter.read();
m1.forward();
m2.backward();
}else if(altIndex == 4){
filterOutputBlackCenter.read();
m1.forward();
m2.backward();
}
}
}
//=========
if (altIndex == 2) {
filterOutputWhiteRight.read();
if(altIndex == 0){
filterOutputWhiteLeft.read();
if(altIndex == 4){
filterOutputBlackCenter.read();
m1.forward();
m2.forward();
}
}else if ( altIndex == 3){
filterOutputBlackLeft.read();
if(altIndex == 1){
filterOutputWhiteCenter.read();
m2.forward();
m1.backward();
}else if(altIndex == 4){
filterOutputBlackCenter.read();
m2.forward();
m1.backward();
}
}
}
//==========
if (altIndex == 4) {
filterOutputBlackCenter.read();
if(altIndex == 2){
filterOutputWhiteRight.read();
if(altIndex == 0){
filterOutputWhiteLeft.read();
m1.forward();
m2.forward();
}
}else if ( altIndex == 5){
filterOutputBlackRight.read();
if(altIndex == 0){
filterOutputWhiteLeft.read();
m1.forward();
m2.backward();
}else if(altIndex == 3){
filterOutputBlackLeft.read();
m1.forward();
}
}
}
if (altIndex == 6) {
buttonPress.read(); // the stop signal
going = false;
}
}
}
}

I'm really desperate for a lejos nxt emulator. It would be really helpful to test out code without the bother of putting on the lejos firmware and then reloading the normal one. I'm on the highschool lego league team but there is only one nxt between us and the junior high team...
/**
* Signum function. Only the sign of the argument is returned.
*/
public static int sgn(int a) {
if (a < 0)
return -1;
else
return 1;
}
/**
* Signum function. Only the sign of the argument is returned.
*/
public static double sgn(double a) {
if (a < 0)
return -1.0;
else
return 1.0;
}Users browsing this forum: No registered users and 3 guests