Saturday, 6 July 2013

Robotic Arm

Below is the simple LeJOS program for making the Robotic Arm work. The LeJOS firmware for NXT can be downloaded and installed on the NXT brick from LeJOS NXJ 0.9.1. After installation of LeJOS, please load my below program on NXT brick, press the LEFT, RIGHT, UP and DOWN buttons and the Touch Sensor. See the performance. The LEFT and RIGHT buttons should move the arm to the left and right. The ENTER and ESCAPE button should move the arm up and down. When the touch sensor is pressed and if the Claw is in open condition in the past (as defined by the toggle flag), the Claw gets closed. And if the Claw is in closed condition in the past, the Claw gets opened. As you guessed it correctly, the toggle flag stores the past condition in which the Claw was and reverses it. 


import lejos.nxt.Button;
import lejos.nxt.LCD;
import lejos.nxt.Motor;
import lejos.nxt.MotorPort;
import lejos.nxt.NXTMotor;
import lejos.nxt.SensorPort;
import lejos.nxt.TouchSensor;
public class ArmMovement {
static NXTMotor mA = new NXTMotor(MotorPort.A);
static NXTMotor mB = new NXTMotor(MotorPort.B);
static NXTMotor mC = new NXTMotor(MotorPort.C);
static TouchSensor touchSensor = new TouchSensor(SensorPort.S1);
static boolean toggle = true;
/**
* @param args
*/
public static void main(String[] args) {
LCD.drawString("RoboticArm", 0, 0, false);
while (true) {
//Move the arm left and right
while(Button.LEFT.isDown()) {
mA.setPower(30);
mA.forward();
if(Button.LEFT.isUp()) {
mA.stop();
break;
}
}
while(Button.RIGHT.isDown()) {
mA.setPower(30);
mA.backward();
if(Button.RIGHT.isUp()) {
mA.stop();
break;
}
}
//Move the arm up and down
while(Button.ENTER.isDown()) {
mB.setPower(30);
mB.forward();
if(Button.ENTER.isUp()) {
mB.stop();
break;
}
}
while(Button.ESCAPE.isDown()) {
mB.setPower(30);
mB.backward();
if(Button.ESCAPE.isUp()) {
mB.stop();
break;
}
}
while(touchSensor.isPressed() && toggle) {
mC.setPower(30);
mC.forward();
if(!touchSensor.isPressed()) {
mC.stop();
toggle = false;
break;
}
}
while(touchSensor.isPressed() && !toggle) {
mC.setPower(30);
mC.backward();
if(!touchSensor.isPressed()) {
mC.stop();
toggle = true;
break;
}
}
}
}
}