编写多线程的最佳方法

时间:2018-08-21 16:19:27

标签: java robotics nxt

我必须使用多线程进行分配。我有2节关于起重机运动的课程。 在这种情况下,分类有2个选项,即爪式超声波传感器是否看到一个盒子(见下)。 在另一堂课中,我必须从初始开始位置拿起一个盒子,并将其放到1-3(这是基于按键的距离)上的另一个位置。

我该如何写我缺少的课程?

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 个答案:

没有答案