Skip to content

Panique

massteff edited this page Sep 10, 2018 · 3 revisions

Précédent | Home | Suivant

Panique

But de l'exercice

Exercice 1

La lumière du haut clignote en rouge. Indice : Vous pouvez utiliser le bloc événement capteur avec tous les carrés des capteurs en mode gris comme décrit dans Annexe B.

Exercice 2

Implémentez le clignotement à l’aide du bloc événement boutons au lieu du bloc événement capteurs. Y a-t-il une différence de comportement du robot ? Si oui, quelle en est la cause ?

Comment procéder?

Nous allons utiliser les capteurs afin de créer un robot voulant éviter tous les obstacles qui se présentent autour de lui. Il faudra également implémenter un minuteur (1s) qui allumera et éteindra la lumière supérieure grâce aux capteurs et par la suite à l'aide du bloc événement boutons.

Screenshots

Exercice 1

Panique v1

Exercice 2

Panique avec minuteur bouton

Code

Exercice 1

# variables for state
var state[4] = [0,0,0,0]
var new_state[4] = [0,0,0,0]

# stop timer 0
timer.period[0] = 0
# reset outputs
call sound.system(-1)
call leds.top(0,0,0)
call leds.bottom.left(0,0,0)
call leds.bottom.right(0,0,0)
call leds.circle(0,0,0,0,0,0,0,0)

# subroutine to display the current state
sub display_state
	call leds.circle(0,state[1]*32,0,state[3]*32,0,state[2]*32,0,state[0]*32)

onevent prox
	when prox.horizontal[2] >= 2000 do
		motor.left.target = -500
		motor.right.target = -500
		timer.period[0] = 1000
		call leds.top(0,0,0)
		emit pair_run 0
	end

	when prox.horizontal[0] >= 2000 do
		motor.left.target = -350
		motor.right.target = 0
		timer.period[0] = 1000
		call leds.top(0,0,0)
		emit pair_run 1
	end

	when prox.horizontal[4] >= 2000 do
		motor.left.target = 0
		motor.right.target = -350
		timer.period[0] = 1000
		call leds.top(0,0,0)
		emit pair_run 2
	end

	when prox.horizontal[5] >= 2000 do
		motor.left.target = 500
		motor.right.target = 0
		timer.period[0] = 1000
		call leds.top(0,0,0)
		emit pair_run 3
	end

	when prox.horizontal[5] >= 2000 and prox.horizontal[6] >= 2000 do
		motor.left.target = 500
		motor.right.target = 500
		timer.period[0] = 1000
		call leds.top(0,0,0)
		emit pair_run 4
	end

	when prox.horizontal[6] >= 2000 do
		motor.left.target = 0
		motor.right.target = 350
		timer.period[0] = 1000
		call leds.top(0,0,0)
		emit pair_run 5
	end

	when prox.ground.delta[0] >= 400 and prox.ground.delta[0] <= 450 and prox.ground.delta[1] >= 400 and prox.ground.delta[1] <= 450 do
		motor.left.target = -500
		motor.right.target = -500
		timer.period[0] = 1000
		call leds.top(0,0,0)
		emit pair_run 6
	end


onevent timer0
	timer.period[0] = 0
	timer.period[0] = 1000
	call leds.top(32,0,0)
	emit pair_run 7

Exercice 2

# variables for state
var state[4] = [0,0,0,0]
var new_state[4] = [0,0,0,0]

# stop timer 0
timer.period[0] = 0
# reset outputs
call sound.system(-1)
call leds.top(0,0,0)
call leds.bottom.left(0,0,0)
call leds.bottom.right(0,0,0)
call leds.circle(0,0,0,0,0,0,0,0)

# subroutine to display the current state
sub display_state
	call leds.circle(0,state[1]*32,0,state[3]*32,0,state[2]*32,0,state[0]*32)

onevent buttons
	when button.forward == 1 do
		if state[0] == 0 and state[1] == 0 and state[2] == 0 and state[3] == 0 then
			timer.period[0] = 1000
			call leds.top(32,0,0)
			emit pair_run 7
		end
	end


onevent prox
	when prox.horizontal[2] >= 2000 do
		motor.left.target = -500
		motor.right.target = -500
		emit pair_run 0
	end

	when prox.horizontal[0] >= 2000 do
		motor.left.target = -350
		motor.right.target = 0
		emit pair_run 1
	end

	when prox.horizontal[4] >= 2000 do
		motor.left.target = 0
		motor.right.target = -350
		emit pair_run 2
	end

	when prox.horizontal[5] >= 2000 do
		motor.left.target = 500
		motor.right.target = 0
		emit pair_run 3
	end

	when prox.horizontal[5] >= 2000 and prox.horizontal[6] >= 2000 do
		motor.left.target = 500
		motor.right.target = 500
		emit pair_run 4
	end

	when prox.horizontal[6] >= 2000 do
		motor.left.target = 0
		motor.right.target = 350
		emit pair_run 5
	end

	when prox.ground.delta[0] >= 400 and prox.ground.delta[0] <= 450 and prox.ground.delta[1] >= 400 and prox.ground.delta[1] <= 450 do
		motor.left.target = -500
		motor.right.target = -500
		emit pair_run 6
	end


onevent timer0
	timer.period[0] = 0
	if state[0] == 0 and state[1] == 0 and state[2] == 0 and state[3] == 0 then
		new_state[0] = 1
		new_state[1] = 1
		new_state[2] = 1
		new_state[3] = 1
		timer.period[0] = 1000
		call leds.top(0,0,0)
		emit pair_run 8
	end

	if state[0] == 1 and state[1] == 1 and state[2] == 1 and state[3] == 1 then
		new_state[0] = 0
		new_state[1] = 0
		new_state[2] = 0
		new_state[3] = 0
		timer.period[0] = 1000
		call leds.top(32,0,0)
		emit pair_run 9
	end

	call math.copy(state, new_state)
	callsub display_state

Conclusion

La première option nous pose un problème. Un minuteur s'enclenche à chaque fois que le robot rencontre un objet. On obtient donc 1 seconde seulement lorsqu'il aperçoit un seul objet. La deuxième option est donc la plus avantageuse car il nous permet d’enclencher le minuteur une seule fois. Le robot fera la boucle automatiquement et on aura un résultat parfait de 1 seconde.

Clone this wiki locally