ffpaladin

Kevin's MSP 2013 Robot Arm Control

Jul 3rd, 2013
18
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. package com.sherolchen.msp;
  2.  
  3. //A number of imports needed for manual control
  4. import lejos.nxt.Button;
  5. import lejos.nxt.ButtonListener;
  6. import lejos.nxt.SensorPort;
  7. import lejos.nxt.SensorPortListener;
  8. import lejos.nxt.Motor;
  9. import lejos.nxt.LCD;
  10. import lejos.nxt.TouchSensor;
  11. import com.sherolchen.msp.NXTArmAndGripper;
  12.  
  13. /**
  14. * KTong's Robotic Arm
  15. * -------------------
  16. * This is from the laboratory of Kevin, the "mad scientist"
  17. * LOG
  18. * Day 1: The arm got built really fast. This LEGO company makes things really
  19. *  easy. Fortunate for me since the storm is starting to roll in.
  20. * Day 4: Got Eclipse and leJOS set up today. Took me a bit as snow is starting
  21. *  to build up outside and my fingers are getting rather cold. The weather
  22. *  forecast looks good for tomorrow though. Looking forward to a sunny day.
  23. * Day 8: IT'S ALIVE!...sort of. It moves...sort of. I'll figure it out later.
  24. *  The weather forecast took a turn for the worst so I got to start getting
  25. *  ready for the snow storm of the year apparently.
  26. * Day 10: Car broke down. No one in sight. Luckily I have some food rations
  27. *  saved up from the last storm. They are past the expiration date but they
  28. *  should be fine I think.
  29. *  
  30. *  I haven't had much time to work on my arm but at least I added a touch
  31. *  sensor.
  32. * Day 17: Rations are dwindling. I can bearly tipe. It'sss getting rewally cold
  33. *  iin the ccabin. And the arm issn't really werking write now.
  34. * Day 3?: mwahahahaha HAHAHAAA HAHAHAHAHA ... ** cough **
  35. */
  36. public class ArmManualControl {
  37.  
  38.     /**
  39.      * A number of variables used for my robotic arm...mwahahahaha
  40.      */
  41.     // Determines which options to display first
  42.     private static boolean inTurningMode = true;
  43.     // The instance of the arm used to control the arm
  44.     private static NXTArmAndGripper myArm = new NXTArmAndGripper(Motor.B, Motor.C, Motor.A);
  45.     // Constant variables used for displaying the menu options on the NXT
  46.     private final static int NUM_CHAR_LCD_X = 16;
  47.     private final static int NUM_CHAR_LCD_Y = 8;
  48.     private final static String TURN_MODE = "TURN";
  49.     private final static String LEFT_OPTION = "LEFT";
  50.     private final static String RIGHT_OPTION = "RIGHT";
  51.     private final static String LIFT_MODE = "LEFT";
  52.     private final static String UP_OPTION = "UP";
  53.     private final static String DOWN_OPTION = "DOWN";
  54.  
  55.     /**
  56.      * Function: main(String[] args)
  57.      * Description:
  58.      * The main code the sets up the listeners for the buttons on the NXT
  59.      * brick and the touch sensor and commands the arm to react accordingly
  60.      */
  61.     public static void main(String[] args) throws Exception {
  62.        
  63.         // The initial display screen to wait for the user to push a button
  64.         System.out.println("Kevin the Mad Scientist...");
  65.         System.out.println("Press any key to start manual control of arm.");
  66.         Button.waitForAnyPress();
  67.        
  68.         // Displays the first menu option
  69.         LCD.setAutoRefresh(false);
  70.         updateLCDDisplay();
  71.        
  72.         // Sets up the listener for the Enter button on the NXT brick
  73.         Button.ENTER.addButtonListener( new ButtonListener() {
  74.             // Called every time the enter button is pressed
  75.             // Toggles between Turning and Lifting and updates the display
  76.             public void buttonPressed( Button button) {
  77.                 if (inTurningMode) {
  78.                     inTurningMode = false;
  79.                 } else {
  80.                     inTurningMode = true;
  81.                 }
  82.                 updateLCDDisplay();
  83.             }
  84.             // Called every time the enter button is released
  85.             // Shouldn't do anything
  86.             public void buttonReleased( Button button) {
  87.                 updateLCDDisplay();
  88.             }
  89.         }
  90.         );
  91.  
  92.         // Sets up the listener for the Left button on the NXT brick
  93.         Button.LEFT.addButtonListener( new ButtonListener() {
  94.             // Called every time the left button is pressed
  95.             // Depending on the mode, either turns left or lifts up        
  96.             public void buttonPressed( Button button) {
  97.                 if (inTurningMode) {
  98.                     myArm.liftDown();
  99.                 } else {
  100.                     myArm.liftUp();
  101.                 }
  102.             }
  103.             // Called every time the enter button is released
  104.             // Should stop the turning motor or lifting motor
  105.             public void buttonReleased( Button button) {
  106.                 if (inTurningMode) {
  107.                     myArm.stopTurning();
  108.                 } else {
  109.                     myArm.stopLifting();
  110.                 }
  111.             }
  112.           }
  113.         );
  114.        
  115.         // Sets up the listener for the Right button on the NXT brick
  116.         Button.RIGHT.addButtonListener( new ButtonListener() {
  117.             // Called every time the right button is pressed
  118.             // Depending on the mode, either turns right or lifts down 
  119.             public void buttonPressed( Button button) {
  120.                 if (inTurningMode) {
  121.                     myArm.turnLeft();
  122.                 } else {
  123.                     myArm.liftUp();
  124.                 }
  125.             }
  126.             // Called every time the enter button is released
  127.             // Should stop the turning motor or lifting motor
  128.             public void buttonReleased( Button button) {
  129.                 if (inTurningMode) {
  130.                     myArm.stopTurning();
  131.                 } else {
  132.                     myArm.stopLifting();
  133.                 }
  134.             }
  135.           }
  136.         );
  137.  
  138.         // Sets up the listener for the touch sensor
  139.         // Assumes that the touch sensor is connected to port S4
  140.         SensorPort.S4.addSensorPortListener(new SensorPortListener() {
  141.             // This function gets called everytime touch sensor changes state
  142.             public void stateChanged(SensorPort aSource, int aOldValue, int aNewValue) {       
  143.                 if (aOldValue > aNewValue) {
  144.                     myArm.openGripper();
  145.                 } else {
  146.                     myArm.closeGripper();
  147.                 }
  148.             }
  149.         }
  150.         );
  151.  
  152.         // Essentially waits until finished playing
  153.         Button.ESCAPE.waitForPressAndRelease();
  154.        
  155.         // Closing screen. Say's goodbye, waits for a bit, and ends
  156.         LCD.clear();
  157.         System.out.println("Goodbye...");
  158.         LCD.refresh();
  159.         Thread.sleep(1000);
  160.     }
  161.    
  162.     /**
  163.      * Function: updateLCDDisplay()
  164.      * Description:
  165.      * Updates the LCD display to show the mode in which the controls are in.
  166.      * If inTurningMode is true, the LCD displays the turning options.
  167.      * Otherwise, the LCD displays the lifting options.
  168.      */
  169.     public static void updateLCDDisplay() {
  170.         LCD.clear();    // Clears the LCD and gets it ready to be written to
  171.         if (inTurningMode) { // In turning mode so should display turn options
  172.             LCD.drawString(TURN_MODE, NUM_CHAR_LCD_X/2 - TURN_MODE.length()/2,
  173.                                       NUM_CHAR_LCD_Y/2);
  174.             LCD.drawString(LEFT_OPTION, 0, NUM_CHAR_LCD_Y-1);
  175.             LCD.drawString(RIGHT_OPTION, NUM_CHAR_LCD_X-RIGHT_OPTION.length(),
  176.                                          NUM_CHAR_LCD_Y-1);
  177.         } else { // Not in turning mode so should display lift options
  178.             LCD.drawString(LIFT_MODE, NUM_CHAR_LCD_X/2 - LIFT_MODE.length()/2,
  179.                                       NUM_CHAR_LCD_Y/2);
  180.             LCD.drawString(UP_OPTION, 0, NUM_CHAR_LCD_Y-1);
  181.             LCD.drawString(DOWN_OPTION, NUM_CHAR_LCD_X-DOWN_OPTION.length(),
  182.                                          NUM_CHAR_LCD_Y-1);
  183.         }
  184.         LCD.refresh();  // Actually updates the screen to show what was drawn
  185.     }
  186.    
  187. }
RAW Paste Data