Permalink
Browse files

Changed the client

  • Loading branch information...
1 parent a0aed85 commit a0d1e0aa1cad0f42ea5f1033e75b4b3bbece1129 @paul-thomson paul-thomson committed Mar 30, 2012
View
184 CommandReciever/src/group2/sdp/robot/commandreciever/Ear.java
@@ -1,184 +0,0 @@
-package group2.sdp.robot.commandreciever;
-
-import group2.sdp.common.candypacket.CandyPacket;
-
-import java.io.DataInputStream;
-import java.io.DataOutputStream;
-import java.io.IOException;
-
-import lejos.nxt.LCD;
-import lejos.nxt.comm.BTConnection;
-import lejos.nxt.comm.Bluetooth;
-
-public class Ear {
-
- /**
- * The bluetooth connection that Alfie uses for communication.
- */
- private static BTConnection btc;
-
- /**
- * The input stream of the BT communication.
- */
- private static DataInputStream dis;
- /**
- * The output stream of the BT communication.
- */
- private static DataOutputStream dos;
-
- public static void main(String [] args)
- {
- String connected = "Connected";
- String waiting = "Waiting...";
- String closing = "Closing...";
-
- // Initiate BRAIN!
- Brain.init();
-
- boolean exit = false;
- while (!exit) {
- LCD.drawString(waiting,0,0);
- LCD.refresh();
-
- btc = Bluetooth.waitForConnection();
-
- LCD.clear();
- LCD.drawString(connected,0,0);
- LCD.refresh();
-
- dis = btc.openDataInputStream();
- dos = btc.openDataOutputStream();
-
- boolean reset = false;
- while (!exit && !reset) {
- CandyPacket candy = receiveCandy();
- if (candy != null) {
- consumeCandy(candy);
- if (candy.getBrand() == CandyPacket.RESET_CANDY)
- reset = true;
- if (candy.getBrand() == CandyPacket.SLEEP_CANDY)
- exit = true;
- giveCandyBack(candy);
- } else {
- reset = true;
- }
- }
-
- try {
- dis.close();
- dos.close();
- Thread.sleep(100); // wait for data to drain
- LCD.clear();
- LCD.drawString(closing,0,0);
- LCD.refresh();
- btc.close();
- LCD.clear();
- } catch (Exception exc) {
- // Ignore, the connection is already closed.
- }
- }
-
- }
-
- /**
- * Receives a packet full of candy over the blue-tooth connection.
- * @return The CandyPacket that was received.
- */
- private static CandyPacket receiveCandy() {
- CandyPacket candy = null;
- try {
- byte [] b = new byte [CandyPacket.PACKET_SIZE];
- if (dis.read(b, 0, CandyPacket.PACKET_SIZE) != CandyPacket.PACKET_SIZE) {
- // Connection was closed by the server, without telling Alfie!
- return null;
- }
- candy = new CandyPacket(b);
- } catch (IOException e) {
- // Connection was closed by the server, without telling Alfie!
- return null;
- }
- return candy;
- }
-
- /**
- * Alfie kindly returns the CandyPacket he was given. Unfortunately, he drooled
- * on the feedback sweets, so they are modified now.
- * @param candy The CandyPacket, modified after being drooled on.
- */
- private static void giveCandyBack(CandyPacket candy) {
- try {
- dos.write(candy.getSweets(), 0, CandyPacket.PACKET_SIZE);
- dos.flush();
- } catch (IOException e) {
- LCD.clear();
- LCD.drawString("Could not give candy", 0, 0);
- }
- }
-
- /**
- * Alfie first reacts on the CandyPacket and then drools on it in order
- * to modify its feedback sweets.
- * @param candy The candy that was given to Alfie.
- */
- private static void consumeCandy(CandyPacket candy) {
- reactOnCandy(candy);
- droolOnCandy(candy);
- }
-
- /**
- * Makes Alfie react depending on the type of CandyPacket he received.
- * @param candy The CandyPacket that was given to Alfie over blue-tooth.
- */
- private static void reactOnCandy(CandyPacket candy) {
- switch (candy.getBrand()) {
- case CandyPacket.STOP_CANDY:
- Brain.stop();
- break;
- case CandyPacket.GO_FORWARD_CANDY:
- Brain.goForward(candy.getPretzel(0), candy.getPretzel(1));
- break;
- case CandyPacket.GO_BACKWARDS_CANDY:
- Brain.goBackwards(candy.getPretzel(0), candy.getPretzel(1));
- break;
- case CandyPacket.SPIN_LEFT_CANDY:
- Brain.spin(candy.getPretzel(0), candy.getPretzel(1));
- break;
- case CandyPacket.SPIN_RIGHT_CANDY:
- // The pretzels are always positive, so we need to negate the angle
- Brain.spin(-candy.getPretzel(0), -candy.getPretzel(1));
- break;
- case CandyPacket.FORWARD_RIGHT_ARC_CANDY:
- // According to leJOS API the angle sent should be positive
- // but in tests this proved incorrect. Same problem occured for
- // BACKWARDS_RIGHT
- Brain.moveArc((float)candy.getPretzel(0) / 10000, candy.getPretzel(1));
- break;
- case CandyPacket.FORWARD_LEFT_ARC_CANDY:
- Brain.moveArc((float)-candy.getPretzel(0) / 10000, -candy.getPretzel(1));
- break;
- case CandyPacket.BACKWARDS_RIGHT_ARC_CANDY:
- Brain.moveArc((float)candy.getPretzel(0) / 10000, -candy.getPretzel(1));
- break;
- case CandyPacket.BACKWARDS_LEFT_ARC_CANDY:
- Brain.moveArc((float)-candy.getPretzel(0) / 10000, candy.getPretzel(1));
- break;
- case CandyPacket.KICK_CANDY:
- Brain.kick();
- break;
- case CandyPacket.RESET_CANDY:
- break;
- case CandyPacket.SLEEP_CANDY:
- break;
- }
- }
-
- /**
- * Modifies the CandyPacket as a result of drooling over it. The feedback
- * sweets and pretzels of the CandyPacket are updated.
- * @param candy The CandyPacket that was given to Alfie.
- */
- private static void droolOnCandy(CandyPacket candy) {
- candy.setFeedbackSweet(0, Brain.getLeftTouchFlagAndReset() ? (byte)1 : 0);
- candy.setFeedbackSweet(1, Brain.getRightTouchFlagAndReset() ? (byte) 1 : 0);
- }
-}
View
129 CommandReciever/src/group2/sdp/robot/commandreciever/Milestone1.java
@@ -0,0 +1,129 @@
+package group2.sdp.robot.commandreciever;
+
+import lejos.nxt.LCD;
+import lejos.util.TextMenu;
+
+/**
+ * This class presents a menu for selecting which of the Milestone1 tasks should
+ * be executed (Cross Pitch or Kick).
+ */
+public class Milestone1 {
+
+ // The time to wait before executing a command.
+ private static final int TIMEOUT_BEFORE_EXECUTION = 1800;
+ // Defines the speed by which the robot will cross the pitch.
+ private static final int CRUISE_SPEED = 35;
+ private static final String [] menuItems = {"Cross Pitch", "Travel 2m", "Kick", "Ram", "Exit"};
+
+ public static void main (String [] args) {
+ // Initiate BRAIN!
+ Brain.init();
+ Brain.setVerbose(false);
+
+ TextMenu menu = new TextMenu(menuItems);
+
+ for (;;) {
+ LCD.clear();
+ int selection = menu.select();
+ switch (selection) {
+ case 0:
+ crossPitch();
+ break;
+ case 1:
+ travel2m();
+ break;
+ case 2:
+ kick();
+ break;
+ case 3:
+ ram();
+ break;
+ default:
+ LCD.clear();
+ LCD.drawString("Finished",3,4);
+ LCD.refresh();
+ return;
+ }
+ }
+ }
+
+
+ /**
+ * Waits for {@link #TIMEOUT_BEFORE_EXECUTION} ms and performs a kick
+ * as required for milestone 1.
+ */
+ private static void kick() {
+ try {
+ LCD.clear();
+ LCD.drawString("Waiting for " + TIMEOUT_BEFORE_EXECUTION + "ms", 0, 0);
+ LCD.refresh();
+ Thread.sleep(TIMEOUT_BEFORE_EXECUTION);
+ } catch (InterruptedException e) {
+ e.printStackTrace();
+ }
+ LCD.clear();
+ LCD.drawString("Kick!", 0, 0);
+ LCD.refresh();
+
+ Brain.kick();
+ }
+
+ /**
+ * Waits for {@link #TIMEOUT_BEFORE_EXECUTION} ms and performs a kick
+ * as required for milestone 1.
+ */
+ private static void ram() {
+ try {
+ LCD.clear();
+ LCD.drawString("Waiting for " + TIMEOUT_BEFORE_EXECUTION + "ms", 0, 0);
+ LCD.refresh();
+ Thread.sleep(TIMEOUT_BEFORE_EXECUTION);
+ } catch (InterruptedException e) {
+ e.printStackTrace();
+ }
+ LCD.clear();
+ LCD.drawString("Ram!", 0, 0);
+ LCD.refresh();
+
+ Brain.ram();
+ }
+
+ /**
+ * Waits for {@link #TIMEOUT_BEFORE_EXECUTION} ms and travels 2 meters.
+ */
+ private static void travel2m() {
+ try {
+ LCD.clear();
+ LCD.drawString("Waiting for " + TIMEOUT_BEFORE_EXECUTION + "ms", 0, 0);
+ LCD.refresh();
+ Thread.sleep(TIMEOUT_BEFORE_EXECUTION);
+ } catch (InterruptedException e) {
+ e.printStackTrace();
+ }
+ LCD.clear();
+ LCD.drawString("Travelling 2m", 0, 0);
+ LCD.refresh();
+
+ Brain.goForward(CRUISE_SPEED, 200);
+ }
+
+ /**
+ * Waits for {@link #TIMEOUT_BEFORE_EXECUTION} ms and crosses the pitch
+ * as required for milestone 1.
+ */
+ private static void crossPitch() {
+ try {
+ LCD.clear();
+ LCD.drawString("Waiting for " + TIMEOUT_BEFORE_EXECUTION + "ms", 0, 0);
+ LCD.refresh();
+ Thread.sleep(TIMEOUT_BEFORE_EXECUTION);
+ } catch (InterruptedException e) {
+ e.printStackTrace();
+ }
+ LCD.clear();
+ LCD.drawString("Crossing the pitch...", 0, 0);
+ LCD.refresh();
+
+ Brain.goForward(CRUISE_SPEED, 0);
+ }
+}

0 comments on commit a0d1e0a

Please sign in to comment.