stoppingbak
mutex moveMutex;
task move_square()
{
while (true)
{
Acquire(moveMutex); mutex moveMutex;
task move_square()
{
while (true)
{
Acquire(moveMutex);
OnFwd(OUT_B, 75); Wait(1000);
Release(moveMutex);
}
}
task check_sensors()
{
while (true)
{
if (SENSOR_1 == 1)
{
Acquire(moveMutex);
OnFwd(OUT_B, 0); Wait(5000);
Release(moveMutex);
}
}
}
task main()
{
SetSensor(IN_1,SENSOR_TOUCH);
Precedes(check_sensors, move_square);
}
OnFwd(OUT_B, 75); Wait(1000);
Release(moveMutex);
}
}
task check_sensors()
{
while (true)
{
if (SENSOR_1 == 1)
{
Acquire(moveMutex);
OnFwd(OUT_B, 0); Wait(5000);
Release(moveMutex);
}
}
}
task main()
{
SetSensor(IN_1,SENSOR_TOUCH);
Precedes(check_sensors, move_square);
}
rung-kutta
・Runge-Kutta法
微分方程式が
dx/dt = -x + tsin(t)
のとき


/* Differential Equation : F(t,x) */
double f(double t,double x){
double y;
y = -x + t*sin(t);
return y;
}
————————————————————————————————
/* Runge-Kutta Function */
double runge_kutta(double x0){ // x0:Initial Value
double h = 0.01; // Step Size
double k1,k2,k3,k4,x,nx,T;
// Initial Value
x = x0;
// Runge-Kutta Method
for(T = 0.0;T < 50;T += h){
k1 = f(T,x);
k2 = f(T+h/2,x+(h/2)*k1);
k3 = f(T+h/2,x+(h/2)*k2);
k4 = f(T+h,x+h*k3);
nx = x + h*(k1+2*k2+2*k3+k4)/6;
x = nx;
}
return x;
}


Touch2
task main()
{
SetSensor(IN_2,SENSOR_TOUCH);
OnFwd(OUT_AC, 75);
while (true)
{
if (SENSOR_2 == 1){
Off(OUT_AC);
OnRev(OUT_B, 75);
Wait(500);
PlayToneEx(262,400,3,FALSE); Wait(500);
Off(OUT_B);
}
}
}
lego-touch
task main()
{
SetSensor(IN_2,SENSOR_TOUCH);
OnFwd(OUT_AC, 75);
while (true)
{
if (SENSOR_2 == 1){
Off(OUT_AC);
}
}
}
ReadFile
import java.io.*;
import java.io.File;
import java.io.FileReader;
import java.io.FileNotFoundException;
import java.io.IOException;
class ReadFile1{
public static void main(String[] args) throws IOException{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
System.out.println(“Please input file’s name”);
String fileName = br.readLine();
if(args.length > 0){
fileName = args[0];
}
try {
BufferedReader reader = new BufferedReader(new FileReader(fileName));
while(true){
String line = reader.readLine();
if(line == null) break;
System.out.println(line);
}
reader.close();
}
catch(FileNotFoundException e){
System.out.println(“File Not Found !”);
}
catch(IOException e){
System.out.println(“Input ERROR !”);
}
}
}