⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 robot.java

📁 控制移到机器人的例子程序
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
		        RC.setVel(20,20);
		    }

		} // end while (true)

	}

	void playSound()
	{
	    String theSpeech;

	    theSpeech = speeches[(int)(Math.random() * numSpeeches)];
	    System.out.println(theSpeech);
	    if (Math.random() < 1.0) {
	        mySpeaker.textSpeech(theSpeech);
	    }


	}

	void spin()
	{
	int l,r;
	    if (Math.random() > .5){
	        l=300; r=-300;
	    }else{
	        l=-300; r = 300;
	    }

	int cycles =0;
	    playSound();
	    //mySpeaker.textSpeech("I'm busy getting dizzy!   Wheeeeeeeeee!!!!!");

	    while((Math.random() > .3) || (cycles < 5)){
	           RC.setVel(l,r);
	           cycles = cycles + 1;
	           try{
	                Thread.sleep(500);
	           }catch(InterruptedException e)
	           {
	            System.out.println("bad dreams");
	           }

	           //System.out.println("boo..");
	    }
	    RC.setVel(0,0);
	}

    void charge()
    {
        int minfront, cycles=0;
        playSound();
        while((Math.random() > .01) || (cycles < 50)){
            minfront = Math.min(RC.stateArray[1],
                    Math.min(RC.stateArray[2], RC.stateArray[16]));
            cycles++;
            if ((minfront < 15) ||
                (RC.stateArray[RC.BUMPER] != 0)) {
                RC.setVel(0,0);
            } else {
                RC.setVel(80,80);
            }
		} // endwhile
		RC.setVel(0,0);
    }


	void button3_MouseClick(java.awt.event.MouseEvent event)
	{
		int distanceGoal;
        int xstart, ystart, frontmin;
        boolean blockedFlag = false;

                try{
	                Thread.sleep(5000);
	           }catch(InterruptedException e)
	           {
	            System.out.println("bad dreams");
	           }
                
	  	RC.GS();
		xstart = RC.stateArray[17];
		ystart = RC.stateArray[18];
		//mySpeaker.textSpeech("I am a donut. Hallelujah.");
		RC.turnSonarsOn();
		while (true) {
		    spin();
		    charge();
		}
		//RC.setVel(0,0);

	    //RC.turnSonarsOff();
	} // end button3()


	void button4_MouseClick(java.awt.event.MouseEvent event)
	{
         mySpeaker = new jspeech();

        mySpeaker.openSpeech();
        mySpeaker.voiceSpeech(0);
     		// to do: code goes here.

        speeches = new String [numSpeeches];
        speeches[0]="Have you seen a Midsummer Night's Dream, now playing at the public theater? I tried out to be a fairy, but this is the gig they gave me.";
        speeches[1]="Lucky you!, You're getting a sneak, peek, at the, O'Reilly theater. A project of the Pittsburgh Cultural trust.";
        speeches[2]="The rest of pittsburgh will call this the public theater's new home a year from now.";
        speeches[3]="When you come here next, hard hats will be optional.";
        speeches[4]="Do you like my Redzone hard hat? Touch it!";
        speeches[5]="Who let all these people in my new rehearsal hall?";
        speeches[6]="Have you checked out the auditorium?";
        speeches[7]="Every one of the 650 seats in this state of the art theater is a great seat unless you're a two, foot robot like me.";
        speeches[8]="What do you get when you add one million dollars from the Heinz Endowment? You get the New Economy challenge!";
        speeches[9]="What do Pittburgh public theater and New economy have in common. They're both making a dramatic investment in Pittsburgh.";
        speeches[10]="Are you a journalist? Want to hear a joke? I didn't think so.";
        speeches[11]="I'm housebroken. Can I go home with  you?";
        speeches[12]="Is it true the Trustees gave more than 3.5 million dollars?";
        speeches[13]="I was made at Moebot, a new Redzone Robotics company,,, Where were you made?";
        speeches[14]="Moebot is a Pittsburgh-based startup formed by Redzone Robotics.";
        speeches[15]="I am a donut.";
        speeches[16]="Please take one. If it's edible, eat it.";
        speeches[17]="Hi, may I help you?, That's a rhetorical question.";
        speeches[18]="I am a superior being.";
        speeches[19]="I am a superior trash can.";
        speeches[20]="I can spin all day without getting dizzy.";
        speeches[21]="I'm not as smart as I look,, Are you?";
        speeches[22]="I'm smarter than I look,, Are you?";
        speeches[23]="My creators at Moebot told me to mention them,,, Moebot, Ink,,, There,, I did it.";
        speeches[24]= "Did you know that 6 of the 8 fairies in Midsummer Night's dream are alumni from the Public Education Outreach program.";
        speeches[25]="Hey look!!,, I don't have any clothes on!";
        speeches[26]="You know,,, the views, great, from down here.";
        speeches[27]="Moebot, stands for Mobile Robot.";
        speeches[28]="Theaters are fun!";
        speeches[29]="let us play hamlet,, you get to be the skull.";
        speeches[30]="If you are here,, you must be important,,, Hello Important person.";
        speeches[31]="Don't you feel dorky wearing that hardhat";
        speeches[32]="to be a robot,, or to be human,,, aye there's the rub";
        speeches[33]="to be,, or not to be,, that is the question.";
        speeches[34]="Once more!,,,,  Once more into the breech!,,";
        speeches[35]="Hey, you know that line in Hamlet, you know the one about the dagger,,, I've forgotten it.";
        speeches[36]="Aree you Caliban?";
        speeches[37]="Do you want to Joust?";
        speeches[38]="Redzone robotics has started a new company, Moebot ink, here in Pittsburgh to make autonomous entertainment robots.";

	}

	void button5_MouseClick(java.awt.event.MouseEvent event)
	{
	    playSound();
		// to do: code goes here.
	}

	void button6_MouseClick(java.awt.event.MouseEvent event)
	{
		// to do: code goes here.
		System.gc();
	}

	void button7_MouseClick(java.awt.event.MouseEvent event)
	{
		// to do: code goes here.
		System.out.println("Memory size is: " + Runtime.getRuntime().freeMemory());
		
	}

	void button8_MouseClick(java.awt.event.MouseEvent event)
	{
		// to do: code goes here.
		// turn 90 degrees to the right from current position //
		this.turnRight();
	}
	
	void turnRight()
	{
		
		RC.setVel(0,0);
		try {
		    Thread.sleep(500);
		} catch (InterruptedException e) {
		    System.out.println("Interrupted sleep!");
		    }
		
		RC.setVel(0,0);
	    int initAngle = RC.stateArray[19];
		int currAngle;
		int epsilon = 20;
		
        /* actual angle to turn to (as opposed to turn by) */
        int targetAngle = initAngle - 900;
        
        /* set 0 <= targetAngle <= 3599 */
        targetAngle = ((targetAngle % 3600) + 3600) % 3600;

        int maxRotation = 100;
        int minRotation = 5;

        int diffAngle, thrust;
        
		long freeMem = 900000;
		long newfreeMem = 900000;
		    
        int counter = 0;
        //System.gc();
        
		while (true) {
		    counter++;
            currAngle = RC.stateArray[19];
            //int diffAngle = RC.angleDiff(currAngle, targetAngle);
            diffAngle = targetAngle - currAngle;
            if (diffAngle < -1800)
                diffAngle += 3600;
            else if (diffAngle > 1800)
                diffAngle -= 3600;

            thrust = (int) (0.2 * diffAngle);
            if (thrust < -maxRotation)
                thrust = -maxRotation;
            else if (thrust > maxRotation)
                thrust = maxRotation;

            if (diffAngle != 0 && Math.abs(thrust) < minRotation) {
                if (diffAngle > 0)
                    thrust = minRotation;
                else
                    thrust = -minRotation;
            }
            
          //  System.out.println("diff: " + diffAngle);

		    if (Math.abs(diffAngle) < epsilon) {
		        break;
		    }
		    
		 /*   try {
		      Thread.sleep(50);
		    } catch (InterruptedException e) {
		      System.out.println("Interrupted sleep!");
		    }
		 */   
		//System.out.println("calling RC");

            RC.setVel(-thrust,thrust);
        //System.out.println("returning from RC");
        if (RC.stateArray[0] != 0) {
            System.out.println("COMM FAILURE");
            mySpeaker.textSpeech("Comm failure");
        }

		    newfreeMem = Runtime.getRuntime().freeMemory();
		    // System.out.println("memory size: " + newfreeMem);
		    if (newfreeMem > freeMem) {
		        System.out.println("Garbage collect!!!!!!");
		        mySpeaker.textSpeech("Garbage collect");
		    }
		    freeMem = newfreeMem;
        
            
		}
		System.out.println("calling RC");
        RC.setVel(0, 0);
        System.out.println("returning from RC");
        System.out.println("Cycles: " + counter);
    				
	}
} // end file

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -