Blame view

PercTeacher/Sources/Robot/robot.c 2.52 KB
7d329109   pifou   PercTeacher progr...
1
2
3
4
  #include <avr/io.h>
  #include <util/delay.h>
  
  #define SERIAL_BAUDE_RATE 9600
6a806062   pfrison   PercTeacher test ...
5
  #define DELAI_STEP 2
b08b30f4   pfrison   Correction PercTe...
6
7
8
9
10
11
12
13
14
15
  
  #define DDR_GAUCHE DDRB
  #define PORT_GAUCHE PORTB
  #define STEP_MOTEUR_GAUCHE 0x40 // pin 12
  #define DIR_MOTEUR_GAUCHE 0x20 // pin 11
  
  #define DDR_DROIT DDRH
  #define PORT_DROIT PORTH
  #define STEP_MOTEUR_DROIT 0x10 // pin 7
  #define DIR_MOTEUR_DROIT 0x08 // pin 6
7d329109   pifou   PercTeacher progr...
16
  
6601d4de   pfrison   Test moteurs sur ...
17
18
  #define CPU_FREQ 16000000L
  
7d329109   pifou   PercTeacher progr...
19
20
  //gestion des moteurs
  void init_pins(void){
b08b30f4   pfrison   Correction PercTe...
21
22
      DDR_GAUCHE = STEP_MOTEUR_GAUCHE | DIR_MOTEUR_GAUCHE;
  	DDR_DROIT = STEP_MOTEUR_DROIT | DIR_MOTEUR_DROIT;
7d329109   pifou   PercTeacher progr...
23
24
  }
  
6a806062   pfrison   PercTeacher test ...
25
  void pas_moteur(int g, int d){
3fcb1d7b   pfrison   update robot code...
26
      if (g != 0)
b08b30f4   pfrison   Correction PercTe...
27
          PORT_GAUCHE |= STEP_MOTEUR_GAUCHE;
3fcb1d7b   pfrison   update robot code...
28
      if (d != 0)
b08b30f4   pfrison   Correction PercTe...
29
          PORT_DROIT |= STEP_MOTEUR_DROIT;
6a806062   pfrison   PercTeacher test ...
30
      _delay_ms(DELAI_STEP);
3fcb1d7b   pfrison   update robot code...
31
      if (g != 0)
b08b30f4   pfrison   Correction PercTe...
32
          PORT_GAUCHE &= ~STEP_MOTEUR_GAUCHE;
3fcb1d7b   pfrison   update robot code...
33
      if (d != 0)
b08b30f4   pfrison   Correction PercTe...
34
          PORT_DROIT &= ~STEP_MOTEUR_DROIT;
6a806062   pfrison   PercTeacher test ...
35
      _delay_ms(DELAI_STEP);
7d329109   pifou   PercTeacher progr...
36
37
  }
  
3fcb1d7b   pfrison   update robot code...
38
39
  void moteur_set_dir(int g, int d){
  	if(g != 0)
b08b30f4   pfrison   Correction PercTe...
40
          PORT_GAUCHE |= DIR_MOTEUR_GAUCHE;
3fcb1d7b   pfrison   update robot code...
41
  	else
b08b30f4   pfrison   Correction PercTe...
42
          PORT_DROIT &= ~DIR_MOTEUR_GAUCHE;
3fcb1d7b   pfrison   update robot code...
43
  	if(d != 0)
b08b30f4   pfrison   Correction PercTe...
44
          PORT_GAUCHE |= DIR_MOTEUR_DROIT;
3fcb1d7b   pfrison   update robot code...
45
  	else
b08b30f4   pfrison   Correction PercTe...
46
          PORT_DROIT &= ~DIR_MOTEUR_DROIT;
3fcb1d7b   pfrison   update robot code...
47
48
  }
  
7d329109   pifou   PercTeacher progr...
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
  //gestion de la liaison serie
  void init_serial(int speed){
      UBRR0 = CPU_FREQ / (((unsigned long int) speed) << 4) - 1; //Set baud rate
      UCSR0B = (1 << TXEN0 | 1 << RXEN0); //Enable transmitter & receiver
      UCSR0C = (1 << UCSZ01 | 1 << UCSZ00); //Set 8 bits character and 1 stop bit
      UCSR0A &= ~(1 << U2X0); //Set off UART baud doubler
  }
  
  void send_serial(unsigned char c){
      loop_until_bit_is_set(UCSR0A, UDRE0);
      UDR0 = c;
  }
  
  unsigned char get_serial(void){
      loop_until_bit_is_set(UCSR0A, RXC0);
      return UDR0;
  }
  
  int main(void){
      init_pins();
      init_serial(SERIAL_BAUDE_RATE);
  
      while(1){
          unsigned char deltaL1 = get_serial();
          unsigned char deltaL2 = get_serial();
          unsigned char deltaR1 = get_serial();
          unsigned char deltaR2 = get_serial();
3fcb1d7b   pfrison   update robot code...
76
  		
7d329109   pifou   PercTeacher progr...
77
          // conversion 2x8 bits => 16 bits
3fcb1d7b   pfrison   update robot code...
78
79
80
81
          uint16_t deltaL = (deltaL1 << 8) + deltaL2;
          uint16_t deltaR = (deltaR1 << 8) + deltaR2;
  		
  		// sens rotation
b08b30f4   pfrison   Correction PercTe...
82
  		moteur_set_dir((deltaL & 0x80) == 0x80 ? 1 : 0, (deltaR & 0x80) == 0x80 ? 1 : 0);
3fcb1d7b   pfrison   update robot code...
83
84
  		deltaL &= 0x7F;
  		deltaR &= 0x7F;
7d329109   pifou   PercTeacher progr...
85
          
3fcb1d7b   pfrison   update robot code...
86
          uint16_t deltaMax = deltaL > deltaR ? deltaL : deltaR;
b08b30f4   pfrison   Correction PercTe...
87
88
  		uint16_t i;
          for(i=0; i<deltaMax; i++){
6a806062   pfrison   PercTeacher test ...
89
90
91
92
93
94
              if(deltaL > i && deltaR > i)
                  pas_moteur(1, 1);
              else if(deltaL > i)
                  pas_moteur(1, 0);
              else if(deltaR > i)
                  pas_moteur(0, 1);
7d329109   pifou   PercTeacher progr...
95
          }
3fcb1d7b   pfrison   update robot code...
96
97
  		
  		send_serial(0x01);
7d329109   pifou   PercTeacher progr...
98
99
100
      }
      return 0;
  }