lección 5: tareas múltiples & relojes soluciones de programación robolab a nqc
TRANSCRIPT
Lección 5:
Tareas Múltiples & Relojes
Soluciones de Programación RoboLab a NQC
Ejercicio 1
Escriba un programa con 2 tareas. La primera monitorea un sensor de contacto y si este es presionado, emite un sonido.
La segunda monitorea otro sensor de contacto. Al accionar este otro contacto, el motor A avanza por un segundo y se detiene.
Ejercicio 1: Solución RoboLab
task main(){
SetSensor(SENSOR_1, SENSOR_TOUCH);SetSensor(SENSOR_3, SENSOR_TOUCH);
start task1;
start task2;}
task task1(){
while (true) {
until (SENSOR_1 == 1);
PlaySound(SOUND_FAST_UP);} }
task task2(){
while (true) {
until (SENSOR_3 == 1);
Fwd(OUT_A);OnFor(OUT_A, 100); }}
Ejercicio 1: Solución NQCtask main()
{
SetSensor(SENSOR_1, SENSOR_TOUCH);
SetSensor(SENSOR_3, SENSOR_TOUCH);
start task1;
start task2;
}
task task1()
{
while (true)
{
until (SENSOR_1 == 1);
PlaySound(SOUND_FAST_UP);
until (SENSOR_1 == 0);
}
}
task task2()
{
while (true)
{
until (SENSOR_3 == 1);
Fwd(OUT_A);
OnFor(OUT_A, 100);
}
}
Con esta línea, el RCX deja de sonar al liberar el contacto.
Ejercicio 2Escriba un programa con 3 tareas. La
primera emite 4 sonidos.
La segunda hace avanzar al motor por 2 segundos y luego lo detiene.
La tercera hace retroceder al motor C por 5 segundos y luego lo detiene.
Ejercicio 2: Solución RoboLab
task main(){ start task1; start task2; start task3;}
task task 1(){
repeat (4) { PlaySound(SOUND_FAST_UP);}
}
task task2(){ Fwd(OUT_A); OnFor(OUT_A, 200);}
task task3(){ Rev(OUT_C); OnFor(OUT_C, 500);}
Ejercicio 2: Solución NQCtask main()
{
start task1;
start task2;
start task3;
}
task task1()
{
repeat(4)
{
PlaySound(SOUND_FAST_UP);
}
}
task task2()
{
Fwd(OUT_A);
OnFor(OUT_A, 200);
}
task task3()
{
Rev(OUT_C);
OnFor(OUT_C, 500);
}
Ejercicio 3: Solución RoboLab Escriba un programa que emite un sonido
durante 5 segundos.
task main(){
ClearTimer(0);
while ( Timer(0) < 50 ){
PlaySound(SOUND_FAST_UP);
}
}
Ejercicio 3: Solución NQC
task main()
{
ClearTimer(0);
while ( Timer(0) < 50)
{
PlaySound(SOUND_FAST_UP);
}
}
Esto pone a cero el reloj.
El tiempo está en décimos de segundo.