#---DECLARATION DES VARIABLES--- #VARIABLES DE DESSIN var counter_cross #var counter_circle var counter_square #var circle_size=38 #variable de taille du cercle, valeure abstraite var counter_90_left var counter_90_right var counter_move_forward var counter_move_backward var draw_cross_ongoing=0 #var draw_circle_ongoing=0 var draw_square_ongoing=0 var turn_90_left_ongoing=0 var turn_90_right_ongoing=0 var move_forward_ongoing=0 var move_backward_ongoing=0 #VARIABLES DE DEPLACEMENT var speed=300 var multiplier_angle=1 #multiplicateur pour les angles (tourner sur place) 1: 90° 2: 180°, valeure maximale 2 var multiplier_move=1 #multiplicateur pour les déplacements 1: 1case 2: 2 case, valeure maximale 2 #JEUX var counter_player=0 #on utilisera un modulo 2 pour déterminer quel type de dessin le thymio doit effectuer var orientation=1 #orientation du thymio => 0: haut, 1: droite, 2: bas, 3: gauche var pos_x=1 #position du robot (1;1): coin haut gauche var pos_y=1 #(3;3): coin bas #VARIABLES D'ECHANGES var receiving_value_on=1 var received_value=0 #valeur reçue par la communication entre les 2 robots var received_x=0 var received_y=0 var to_draw=0 # si le rebot doit dessiner une croix ou un cercle var start_new_movement=0 var temp #variable temporaire pour du stockage call prox.comm.enable(1) prox.comm.tx=0 timer.period[1]=200 #-----FONCTIONS MOTEURS------ sub zero #met à 0 la vitesse des moteurs motor.left.target=0 motor.right.target=0 sub forward #le robot avance tout droit motor.left.target=speed motor.right.target=speed sub backward #le robot recule motor.left.target=-speed motor.right.target=-speed sub turn_anticlockwise #le robot tourne sur lui-même dans le sens inverse des aiguilles d'une montre motor.left.target=-speed motor.right.target=speed-10 sub turn_clockwise #le robot tourne sur lui-même dans le sens des aiguilles d'une montre motor.left.target=speed-10 motor.right.target=-speed sub circle_clockwise #cercle dans le sens des aiguilles d'une montre motor.left.target=-speed+150 motor.right.target=speed #-SUPRRESION DES ACTIONS EN COURS- sub reset_ongoing #draw_circle_ongoing=0 draw_cross_ongoing=0 draw_square_ongoing=0 turn_90_left_ongoing=0 turn_90_right_ongoing=0 move_forward_ongoing=0 move_backward_ongoing=0 #----------CROIX------------ sub draw_cross #initialise le lancement du dessin de la croix callsub reset_ongoing draw_cross_ongoing=1 call leds.top(32,0,0) counter_cross=0 timer.period[0]=250 #delta entre chaque pas de boucle #----------CERCLE----------- #*sub draw_circle callsub reset_ongoing draw_circle_ongoing=1 call leds.top(0,0,32) counter_circle=0 timer.period[0]=170 #delta entre chaque pas de boucle*# #----------SQUARE------------ sub draw_square callsub reset_ongoing draw_square_ongoing=1 call leds.top(0,0,32) counter_square=0 timer.period[0]=160 #-ROTATION STATIONNAIRE ANTICLOCKWISE- sub turn_90_left callsub reset_ongoing turn_90_left_ongoing=1 counter_90_left=0 timer.period[0]=165 #delta entre chaque pas de boucle #-ROTATION STATIONNAIRE CLOCKWISE- sub turn_90_right callsub reset_ongoing turn_90_right_ongoing=1 counter_90_right=0 timer.period[0]=165 #delta entre chaque pas de boucle #-DEPLACEMENT EN AVANT DE X CASES- sub move_forward callsub reset_ongoing move_forward_ongoing=1 counter_move_forward=0 timer.period[0]=200 #-DEPLACEMENT EN ARRIERE DE X CASES- sub move_backward callsub reset_ongoing move_backward_ongoing=1 counter_move_backward=0 timer.period[0]=200 #----GESTION DES BOUTONS---- #DEBUG #*onevent buttons if button.forward==1 then call leds.top(32,32,0) end if button.backward==1 then callsub draw_square end if button.right==1 then callsub move_forward end if button.left==1 then call leds.top(0,0,0) callsub turn_90_left end*# #-GESTION DU TIMER ET DES PAS- onevent timer0 if draw_cross_ongoing==1 then #Si la croix est en cours de dessin if counter_cross==0 or counter_cross==10 then callsub turn_anticlockwise elseif counter_cross==2 or counter_cross==4 or counter_cross==7 or counter_cross==9 or counter_cross==13 or counter_cross==15 or counter_cross==18 or counter_cross==20 then callsub zero elseif counter_cross==3 or counter_cross==8 or counter_cross==16 then callsub forward elseif counter_cross==5 or counter_cross==14 or counter_cross==19 then callsub backward elseif counter_cross==21 then callsub turn_clockwise elseif counter_cross==26 then call leds.top(0,32,0) callsub zero draw_cross_ongoing=0 #fin de la croix timer.period[0]=0 receiving_value_on=1 prox.comm.tx++ end counter_cross++ return end #*if draw_circle_ongoing==1 then #Si le cercle est en cours de dessin if counter_circle==0 or counter_circle==circle_size+9 then callsub turn_clockwise elseif counter_circle==5 or counter_circle==7 or counter_circle==13 or counter_circle==circle_size or counter_circle==circle_size+6 or counter_circle==circle_size+8 then callsub zero elseif counter_circle==6 or counter_circle==circle_size+7 then callsub forward elseif counter_circle==8 or counter_circle==circle_size+1 then callsub turn_anticlockwise elseif counter_circle==14 then callsub circle_clockwise elseif counter_circle==circle_size+14 then call leds.top(0,32,0) callsub zero draw_circle_ongoing=0 #fin du cercle timer.period[0]=0 receiving_value_on=1 prox.comm.tx++ end counter_circle++ return end*# if draw_square_ongoing==1 then if counter_square==0 or counter_square==8 or counter_square==16 or counter_square==25 or counter_square==34 or counter_square==43 then callsub forward elseif counter_square==1 or counter_square==7 or counter_square==9 or counter_square==15 or counter_square==18 or counter_square==24 or counter_square==27 or counter_square==33 or counter_square==36 or counter_square==42 or counter_square==44 or counter_square==50 then callsub zero elseif counter_square==2 or counter_square==10 or counter_square==19 or counter_square==28 or counter_square==37 then callsub turn_clockwise elseif counter_square==45 then callsub turn_anticlockwise elseif counter_square==51 then callsub backward elseif counter_square==52 then call leds.top(0,32,0) callsub zero draw_square_ongoing=0 #fin du carré timer.period[0]=0 receiving_value_on=1 prox.comm.tx++ end counter_square++ return end if turn_90_left_ongoing==1 then if counter_90_left==0 then start_new_movement=0 callsub turn_anticlockwise elseif counter_90_left==5*multiplier_angle then #temps pour tourner 90°==850ms callsub zero turn_90_left_ongoing=0 timer.period[0]=0 temp=orientation-multiplier_angle if temp<0 then orientation=4+temp else orientation=temp end start_new_movement=1 #suite des mouvements end counter_90_left++ return end if turn_90_right_ongoing==1 then if counter_90_right==0 then start_new_movement=0 callsub turn_clockwise elseif counter_90_right==5*multiplier_angle then #temps pour tourner 90°==850ms callsub zero turn_90_right_ongoing=0 timer.period[0]=0 orientation=(orientation+multiplier_angle)%4 start_new_movement=1 end counter_90_right++ return end if move_forward_ongoing==1 then if counter_move_forward==0 then start_new_movement=0 callsub forward elseif counter_move_forward==3*multiplier_move then callsub zero move_forward_ongoing=0 timer.period[0]=0 start_new_movement=1 if orientation==0 then pos_y-=multiplier_move #le robot est monté d'une case elseif orientation==2 then pos_y+=multiplier_move #le robot est descendu d'une case elseif orientation==1 then pos_x+=multiplier_move #le robot a avancé d'une case elseif orientation==3 then pos_x-=multiplier_move #le robot a reculé d'une case end end counter_move_forward++ return end if move_backward_ongoing==1 then if counter_move_backward==0 then callsub backward start_new_movement=0 elseif counter_move_backward==3*multiplier_move then callsub zero move_backward_ongoing=0 timer.period[0]=0 start_new_movement=1 if orientation==2 then pos_y-=multiplier_move #le robot est monté de X case elseif orientation==0 then pos_y+=multiplier_move #le robot est descendu de X case elseif orientation==3 then pos_x+=multiplier_move #le robot a avancé de X case elseif orientation==1 then pos_x-=multiplier_move #le robot a reculé de X case end end counter_move_backward++ return end onevent prox.comm #on écoute les communications du robot if received_value!=prox.comm.rx and receiving_value_on==1 and prox.comm.rx!=0 then call leds.top(10,10,10) received_value=prox.comm.rx start_new_movement=1 received_y=received_value%10 received_x=(received_value-received_y)/10 receiving_value_on=0 call leds.top(0,0,0) end onevent timer1 if start_new_movement==1 then if pos_y<received_y then #le robot est plus haut que sa position de desination multiplier_move=abs(pos_y-received_y) multiplier_angle=1 if orientation==0 then callsub move_backward elseif orientation==1 then callsub turn_90_right elseif orientation==2 then callsub move_forward elseif orientation==3 then callsub turn_90_left end elseif pos_y>received_y then #le robot est plus bas que sa position de desination multiplier_move=abs(pos_y-received_y) multiplier_angle=1 if orientation==0 then callsub move_forward elseif orientation==1 then callsub turn_90_left elseif orientation==2 then callsub move_backward elseif orientation==3 then callsub turn_90_right end else if pos_x<received_x then #le robot est plus à gauche que sa position de desination multiplier_move=abs(pos_x-received_x) multiplier_angle=1 if orientation==0 then callsub turn_90_right elseif orientation==1 then callsub move_forward elseif orientation==2 then callsub turn_90_left elseif orientation==3 then callsub move_backward end elseif pos_x>received_x then #le robot est plus à droite que sa position de desination multiplier_move=abs(pos_x-received_x) multiplier_angle=1 if orientation==0 then callsub turn_90_left elseif orientation==1 then callsub move_backward elseif orientation==2 then callsub turn_90_right elseif orientation==3 then callsub move_forward end else #le robot est à destination to_draw=1 start_new_movement=0 call leds.top(32,0,0) end end end if pos_x==received_x and pos_y==received_y and to_draw==1 then temp=counter_player%2 if temp==0 then callsub draw_cross else callsub draw_square end counter_player++ to_draw=0 end if orientation==0 then call leds.circle(0,0,0,0,0,0,0,0) elseif orientation==1 then call leds.circle(32,0,0,0,0,0,0,0) elseif orientation==2 then call leds.circle(32,32,0,0,0,0,0,0) elseif orientation==3 then call leds.circle(32,32,32,0,0,0,0,0) end