2013年7月10日 星期三

輸入數字組 Inputting set of numbers

我們將輸入一組數字並顯示在螢幕上。應用左鍵作「減」、右鍵作「加」及中鍵作「確定」並利用陣列的方式來讓我們可以使用迴圈四次來輸入四個數字,最後使用螢幕顯示指令來將輸入結果輸出。

We will input a set of numbers and display the set.  We can apply the left button to be "minus", right button to be "plus" and center button to be "confirm".  We can use the array to let us to apply loop for 4 times to input 4 numbers and output the result with display command finally.

Example:  

task main()
{
int a[4]={0,0,0,0},i=0;
while(i<4)
{
while(!ButtonPressed(BTNCENTER,true))
{
if(ButtonPressed(BTNRIGHT,true))a[i]++;
if(ButtonPressed(BTNLEFT,true))a[i]--;
NumOut(i*10,0,a[i]);
}
while(ButtonPressed(BTNCENTER,true))
i++;
}
while(i>0)
{
NumOut(i*10,10,a[i]);
i--;
}
Wait(5000);
}

比例控制 Proportional control (P)

我們可以使用比例控制去讓機器人滑順地停止。概念是將電力和差距呈比例關係。差距是目標位置減現在位置(測量值)。電力大小為差距的大小的比例值而機器人移動方向為差距的正負。當差距等於零時,機器人靜止。當差距為正時,機器人向前。當差距為負時,機器人向後。我們使用角度感應器來測量差距。

電力式:電力 = 常數×差距 = 常數×(目標 - 測量) = K * (target - measurement)

We can use the Proportional control (P) to let the robot smooth stop.  The concept is that we can let the power be controlled proportionally by distance(error).  The distance is defined as the target position minus now position(measurement).  The magnitude of power is proportion of the magnitude of distance and The direction of the robot is the direction of distance.  If the distance is zero, the robot is static.  If the distance is positive, the robot moves forward.  If the distance is negative, the robot moves backward. We use degrees sensor to measure the distance.

The power formula is:  Power = constant × distance = K * (target - measurement)

Example:

task main()
{
int power,K,target,measurement;
K = 1;
target = 1000;
measurement = MotorRotationCount(OUT_A);
while(1)
{
measurement = MotorRotationCount(OUT_A);
power = K * (target - measurement);
OnFwd(OUT_AC,power);
}
}

2013年7月9日 星期二

Avoiding wall with ultrasonic

We can avoid the wall with touch sensor or ultrasonic sensor.  We can install a ultrasonic sensor on the front of a car.  When the sensor senses a wall, the car turns right to avoid the wall.


Example:

task main()
{
SetSensorUltrasonic(S1);
while(1)
{
if(SensorUS(S1) < 20)
{
OnFwd(OUT_A,100);
OnFwd(OUT_C,-100);
}
else
{
OnFwd(OUT_AC,100);
}
}

}

The tracking line car

We can create a car which can follow a line with light sensor.  The car moves right and the sensor installed on the left front of the car.  When the sensor senses the line, the car turns left to correct the direction.

Example:

task main()
{

SetSensorLight(S1);
while(1)
{
if(Sensor(S1)< 40)
{
OnFwd(OUT_A,-100);
OnFwd(OUT_C,100);
}
else
{
OnFwd(OUT_A,100);
OnFwd(OUT_C,80);
}
}

}

The avoiding wall car

We can use a touch sensor to avoid a wall.  A touch sensor can be installed on the front of a car. When the sensor touches a wall, the car turn right to avoid the wall.

Example:

task main()
{
SetSensorTouch(S1);
while(1)
{
if(Sensor(S1))
{
OnFwd(OUT_A,100);
OnRev(OUT_C,100);
}
else
{
OnFwd(OUT_AC,100);
}

}

}