Best way to write the multithread

48 Views Asked by At

I have to make an assignment with multi-threading. I have 2 classes for movement of a crane. In this cases class there are 2 options, whether the claws ultrasonic sensor sees a box or not (seen down below). In the other class i have to pick up a box from the initial starting position and put it to another position from 1-3(which is a distance based on keypress).

How do i write the class I am missing?

public class Cases extends Thread

UltrasonicSensor usc= new UltrasonicSensor(SensorPort.S1);
int box=0;
boolean isBoxThere=true;
public Cases cases;


public void error() throws InterruptedException
{
    LCD.drawString("There are no boxes", 0, 0);
    LCD.drawString(" At the location", 0, 1);
    Button.waitForAnyPress();
    LCD.clear();
    Thread.sleep(1000);
}
//Claw movement
public void down() throws InterruptedException{

    MotorPort motorY=MotorPort.B;

    motorY.resetTachoCount();
    motorY.controlMotor(85, 1);


    stopY();
}
public void up() throws InterruptedException{
    MotorPort motorY = MotorPort.B;
    motorY.controlMotor(85, 2);
    while(motorY.getTachoCount()>0);                //After the crane grabs the box it lifts it back up
    stopY();
}
public void clawclose() throws InterruptedException{
    MotorPort motorC=MotorPort.C;
    motorC.controlMotor(65, 2);
    {
        Thread.sleep(505);                          //Thread sleep is used for the cranes precise stopping
    }
    motorC.controlMotor(100, 3);
    {
        Thread.sleep(1000);
    }
    stopC();

}
public void clawopen() throws InterruptedException{
    MotorPort motorC=MotorPort.C;

    {
        Thread.sleep(520);                          
    }
    motorC.controlMotor(100, 3);
    {
        Thread.sleep(1000);
    }
    stopC();

}
//Codes for stopping of Motors
public static void stopY () throws InterruptedException{
    MotorPort motorY = MotorPort.B;
    motorY.controlMotor(100, 3);
    Thread.sleep(1000);
}
public static void stopC () throws InterruptedException{
    MotorPort motorC = MotorPort.C;
    motorC.controlMotor(100, 3);
    Thread.sleep(1000);
}

public void run() 
{

        try
        {

            while(true)
            {
                switch(box)
                {

                case 1:
                {
                    while(box==1)
                    {
                        isBoxThere=true;
                        try {
                            clawopen();
                        } catch (InterruptedException e2) {
                            // TODO Auto-generated catch block
                            e2.printStackTrace();
                        }
                        try {
                            down();
                        } catch (InterruptedException e1) {
                            // TODO Auto-generated catch block
                            e1.printStackTrace();
                        }
                        while(usc.getDistance()<10)
                        {
                            try 
                            {
                                Thread.sleep(100);
                            }
                            catch(InterruptedException e)
                            {
                                e.printStackTrace();
                            }
                        }
                        try 
                        {
                            clawclose();
                        } 
                        catch (InterruptedException e) {
                            // TODO Auto-generated catch block
                            e.printStackTrace();
                        }



                    }
                }


                case 2:
                {
                    while(box==0)
                    {
                        isBoxThere=false;
                        try
                        {
                            error();
                        }
                        catch (InterruptedException e)
                        {
                            e.printStackTrace();
                        }
                    }
                }

                }
            }
    }
        finally{}
}

}

0

There are 0 best solutions below