Blame view

Robot/main.c 2.25 KB
d2f14e3c   pfrison   Debut programme a...
1
2
3
4
5
6
  #include <avr/io.h>
  #include <avr/interrupt.h>
  #include <util/delay.h>
  
  #include "serial.h"
  #include "main.h"
9a371461   pfrison   Programme robot
7
8
  #include "deplacement.h"
  #include "tlc5947.h"
d2f14e3c   pfrison   Debut programme a...
9
10
11
12
  
  #define NB_TICK 104 //1563
  
  /** ----- Taches ----- */
9a371461   pfrison   Programme robot
13
14
  void task_deplacement_gauche(void){
  	deplacement(GAUCHE);
d2f14e3c   pfrison   Debut programme a...
15
  }
9a371461   pfrison   Programme robot
16
17
18
19
20
21
22
23
  void task_deplacement_droit(void){
  	deplacement(DROIT);
  }
  void task_com_bluetooth(void){
  	// TODO
  }
  void task_leds(void){
  	// TODO
d2f14e3c   pfrison   Debut programme a...
24
25
26
27
28
29
30
31
  }
  
  /** ----- Ordonnancement ----- */
  struct task{
  	uint16_t sp_vise;
  };
  uint8_t cpt = 0;
  uint8_t premier_lancement = 0;
9a371461   pfrison   Programme robot
32
33
34
35
  struct task deplacement_gauche = {0x0300};
  struct task deplacement_droit = {0x0500};
  struct task com_bluetooth = {0x0700};
  struct task leds = {0x0900};
d2f14e3c   pfrison   Debut programme a...
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
  
  // Démarre le timer pour l'ordonnancement
  void init_timer(){
  	TCCR1B |= _BV(WGM12); // CTC mode with value in OCR1A 
  	TCCR1B |= _BV(CS12); // CS12 = 1; CS11 = 1; CS10 =1 => CLK/1024 prescaler
  	TCCR1B |= _BV(CS10);
  	OCR1A = NB_TICK;
  	TIMSK1 |= _BV(OCIE1A);
  }
  
  // Changement de contexte
  ISR(TIMER1_COMPA_vect){
  	if(premier_lancement == 0){
  		premier_lancement++;
  		sei();
9a371461   pfrison   Programme robot
51
52
  		SP = deplacement_gauche.sp_vise;
  		task_deplacement_gauche();
d2f14e3c   pfrison   Debut programme a...
53
54
  	}else if(premier_lancement == 1){
  		SAVE_CONTEXT();
9a371461   pfrison   Programme robot
55
56
57
58
59
60
61
62
  		deplacement_gauche.sp_vise = SP;
  		premier_lancement++;
  		sei();
  		SP = deplacement_droit.sp_vise;
  		task_deplacement_droit();
  	}else if(premier_lancement == 2){
  		SAVE_CONTEXT();
  		deplacement_droit.sp_vise = SP;
d2f14e3c   pfrison   Debut programme a...
63
64
  		premier_lancement++;
  		sei();
9a371461   pfrison   Programme robot
65
66
67
68
69
70
71
72
73
  		SP = com_bluetooth.sp_vise;
  		task_com_bluetooth();
  	}else if(premier_lancement == 3){
  		SAVE_CONTEXT();
  		com_bluetooth.sp_vise = SP;
  		premier_lancement++;
  		sei();
  		SP = leds.sp_vise;
  		task_leds();
d2f14e3c   pfrison   Debut programme a...
74
75
76
  	}else{
  		if(cpt==0){
  			SAVE_CONTEXT();
9a371461   pfrison   Programme robot
77
78
  			com_bluetooth.sp_vise = SP;
  			SP = deplacement_gauche.sp_vise;
d2f14e3c   pfrison   Debut programme a...
79
80
81
82
  			RESTORE_CONTEXT();
  			cpt++;
  		}else if(cpt==1){
  			SAVE_CONTEXT();
9a371461   pfrison   Programme robot
83
84
85
86
87
88
89
90
91
92
93
94
95
96
  			deplacement_gauche.sp_vise = SP;
  			SP = deplacement_droit.sp_vise;
  			RESTORE_CONTEXT();
  			cpt++;
  		}else if(cpt==2){
  			SAVE_CONTEXT();
  			deplacement_droit.sp_vise = SP;
  			SP = com_bluetooth.sp_vise;
  			RESTORE_CONTEXT();
  			cpt++;
  		}else if(cpt==3){
  			SAVE_CONTEXT();
  			com_bluetooth.sp_vise = SP;
  			SP = leds.sp_vise;
d2f14e3c   pfrison   Debut programme a...
97
98
99
100
101
102
103
104
105
106
107
  			RESTORE_CONTEXT();
  			cpt = 0;
  		}
  	}
  	sei();
  }
  
  int main(void){
  	// Initialisation
  	init_timer();
  	init_serial(9600);
9a371461   pfrison   Programme robot
108
  	init_deplacement();
d2f14e3c   pfrison   Debut programme a...
109
110
111
112
113
  	sei();
  
  	while(1){}
  	return 0;
  }