@Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.main, menu); return true; } public int[] getScrWH(Activity activity){
int scrWH[]={0,0};
DisplayMetrics dm = new DisplayMetrics();
activity.getWindowManager().getDefaultDisplay().getMetrics(dm);
scrWH[0] = dm.widthPixels;
scrWH[1] = dm.heightPixels;
return scrWH;
} //按鈕事件 class ButtonClickListener implements OnClickListener {
// onClick 方法(觸碰按鈕時的事件處理器)
public void onClick(View v){
//取得 tag
String tag = (String)v.getTag();
btn_Brick.setText(strBtn[ibtn][0]);
btn_Brick.setTextSize(ifisze);
btn_Brick.setTag(strBtn[ibtn][1]);
//-------------
int spacing=0;
int width=btn_Brick.getMeasuredWidth();
int height=btn_Brick.getMeasuredHeight();
int txt_width=(int)(btn_Brick.getTextSize()*btn_Brick.getText().length());
int txt_height=(int)(btn_Brick.getLineCount()*btn_Brick.getLineHeight());
int img_width=0;
int img_height=0;
int content_height=0;
int content_width=0;
int padding_w=0;
int padding_h=0;
int isel=0;
Returns a boolean value to indicate if this behavior should become active. For example, if a touch sensor indicates the robot has bumped into an object, this method should return true. This method should return quickly, not perform a long calculation.
The code in this method begins performing its task when the behavior becomes active. For example, if takeControl() detects the robot has collided with an object, the action() code could make the robot back up and turn away from the object.
A behavior is active as long as its action() method is running, so the action() method should exit when its task is complete. Also, the action() method should exit promptly when suppress() is called. When it exits, it should leave the robot in a safe state for the next behavior.
The code in the suppress() method should immediately terminate the code running in the action() method. It also should exit quickly. 仲裁者(Arbitrator)依行動者(behaviors)陣列索引由大至小來進行,因此那個行為最高優先的要放最後面。 仲裁者(Arbitrator)先檢查boolean takeControl()是否回傳Ture,接著執行void action(),這期間如果有其他更高優先權的行為發生時仲裁者(Arbitrator)會執行現在行為的void suppress() ,因此程式在執行時必須要注意void suppress()的變化來決定是否要停止或繼續執行。
/**
* Demonstration of use of the Behavior and Pilot classes to
* implement a simple line following robot.
*
* Requires a wheeled vehicle with two independently controlled
* wheels with motors connected to motor ports A and C, and a light
* sensor mounted forwards and pointing down, connected to sensor port 1.
*
* Press ENTER to start the robot.
*
* You can run the PilotParams sample to create a property file which
* sets the parameters of the Pilot to the dimensions
* and motor connections for your robot.
*
* @author Lawrie Griffiths
*
*/
public class LineFollower { public static void main (String[] aArg) throws Exception { PilotProps pp = new PilotProps(); pp.loadPersistentValues(); float wheelDiameter = Float.parseFloat(pp.getProperty(PilotProps.KEY_WHEELDIAMETER, "4.96")); float trackWidth = Float.parseFloat(pp.getProperty(PilotProps.KEY_TRACKWIDTH, "13.0")); RegulatedMotor leftMotor = PilotProps.getMotor(pp.getProperty(PilotProps.KEY_LEFTMOTOR, "B")); RegulatedMotor rightMotor = PilotProps.getMotor(pp.getProperty(PilotProps.KEY_RIGHTMOTOR, "C")); boolean reverse = Boolean.parseBoolean(pp.getProperty(PilotProps.KEY_REVERSE,"false")); // Change last parameter of Pilot to specify on which // direction you want to be "forward" for your vehicle. // The wheel and axle dimension parameters should be // set for your robot, but are not critical. final RotateMoveController pilot = new DifferentialPilot(wheelDiameter, trackWidth, leftMotor, rightMotor, reverse); final LightSensor light = new LightSensor(SensorPort.S1);
pilot.setRotateSpeed(180);
/**
* this behavior wants to take control when the light sensor sees the line
*/ Behavior DriveForward = new Behavior() { public boolean takeControl() { return light.readValue() <= 40; } public void suppress() { pilot.stop(); } public void action() { pilot.forward();
while(light.readValue() <= 40) Thread.yield(); //action complete when not on line } }; Behavior OffLine = new Behavior() { private boolean suppress = false; public boolean takeControl() { return light.readValue() > 40; }
public void suppress() { suppress = true; } public void action() { int sweep = 10; while (!suppress) { pilot.rotate(sweep,true); while (!suppress && pilot.isMoving()) Thread.yield(); sweep *= -2; } pilot.stop(); suppress = false; } };