Quellcode der Software zur Steuerung


#define light_color 750
int gp_flag; // Flag zur Kommunikation zw. task trommel und task schiene
task main()
{
start trommel; //beide Tasks starten
start schiene;
}
task schiene ()
{
int loop_count;
int light_sens3;
SetSensorType (SENSOR_1, SENSOR_TYPE_LIGHT); //Sensoren und Motoren initialisieren
SetSensorMode (SENSOR_1, SENSOR_MODE_RAW);
SetSensorType (SENSOR_3, SENSOR_TYPE_LIGHT);
SetSensorMode (SENSOR_3, SENSOR_MODE_RAW);
SelectDisplay(DISPLAY_SENSOR_3);
SetPower(OUT_A,8);
SetDirection(OUT_A,OUT_FWD);
SetOutput(OUT_A,OUT_ON);
loop_count = 0;
light_sens3 = 0;
gp_flag = 0;
do
{
do
{
if (SENSOR_1 > light_color) //wenn schwarze Markierung erreicht ?
{
SetOutput(OUT_A,OUT_OFF); // Motor aus -> stop
break;
}
}while(1);
Wait(50); //halbe sekunde warten zum beruhigen des Lichtsensors - vorsichtshalber
light_sens3 = 0;
light_sens3 = SENSOR_3; //aktuellen Sensorwert speichern
//solange aktueller Sensor innerhalb von +/- 10 um gespeicherten Wert warten
//Abweichung grösser bedeutet Ball ist in Behälter gefallen
do{}while ((SENSOR_3 <= light_sens3+10) && (SENSOR_3 >= light_sens3-10));
gp_flag = 1; //Flag an Trommel Task setzen
if (loop_count < 3)
{
SetOutput(OUT_A,OUT_ON); //Motor wieder starten
do{} while(SENSOR_1 > light_color);
}
loop_count = loop_count + 1;
}while(loop_count < 4); //wenn 4 mal durchgefuehrt -> alle Kugeln gezogen

SetDirection(OUT_A,OUT_REV); //Motoren umstellen und in Ausgangspos. fahren
SetOutput(OUT_A,OUT_ON);
Wait(500);
SetOutput(OUT_A,OUT_OFF);
}
task trommel ()
{
int loopcnt;
SetSensorType (SENSOR_2, SENSOR_TYPE_TOUCH);
SetSensorMode (SENSOR_2, SENSOR_MODE_BOOL);
loopcnt = 0;
do
{
SetPower(OUT_B,1);
SetDirection(OUT_B,OUT_REV); //mischen
SetOutput(OUT_B,OUT_ON);
Wait(500); //5 sec warten
do {
if (SENSOR_2)
{
SetOutput(OUT_B,OUT_OFF);
break;
}
}while(1);
Wait(200);
SetPower(OUT_B,1);
SetDirection(OUT_B,OUT_FWD);
SetOutput(OUT_B,OUT_ON);
do { //Ball fangen
if (SENSOR_2) break;
}while(1);
do {
if (!(SENSOR_2)) break;
}while(1);
do{
do {
if (SENSOR_2)
{
Wait(40);
SetOutput(OUT_B,OUT_FLOAT);
Wait(100);
break;
}
}while(1);

Wait(200);
if (gp_flag)
{
loopcnt = loopcnt +1;
gp_flag = 0;
break;
}
else SetOutput(OUT_B,OUT_ON);
}while(1);
}while(loopcnt < 4);
}