Blame view

PercTeacher/Sources/Robot/servoAndStepperControl.c 1.89 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
7d329109   pifou   PercTeacher progr...
6
7
8
  #define PIN_MOTEUR_GAUCHE 0x40 // pin 12
  #define PIN_MOTEUR_DROIT 0x20 // pin 11
  
6601d4de   pfrison   Test moteurs sur ...
9
10
  #define CPU_FREQ 16000000L
  
7d329109   pifou   PercTeacher progr...
11
12
  //gestion des moteurs
  void init_pins(void){
7d329109   pifou   PercTeacher progr...
13
14
15
      DDRB = PIN_MOTEUR_GAUCHE | PIN_MOTEUR_DROIT;
  }
  
6a806062   pfrison   PercTeacher test ...
16
17
18
19
20
21
22
23
24
25
26
  void pas_moteur(int g, int d){
      if (g)
          PORTB |= PIN_MOTEUR_GAUCHE;
      if (d)
          PORTB |= PIN_MOTEUR_DROIT;
      _delay_ms(DELAI_STEP);
      if (g)
          PORTB &= ~PIN_MOTEUR_GAUCHE;
      if (d)
          PORTB &= ~PIN_MOTEUR_DROIT;
      _delay_ms(DELAI_STEP);
7d329109   pifou   PercTeacher progr...
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
  }
  
  //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();
6a806062   pfrison   PercTeacher test ...
56
57
58
  
  
  	send_serial('a');
7d329109   pifou   PercTeacher progr...
59
60
          
          // conversion 2x8 bits => 16 bits
6601d4de   pfrison   Test moteurs sur ...
61
62
          int deltaL = (deltaL1 << 8) + deltaL2;
          int deltaR = (deltaR1 << 8) + deltaR2;
7d329109   pifou   PercTeacher progr...
63
64
65
          
          int deltaMax = deltaL > deltaR ? deltaL : deltaR;
          for(int i=0; i<deltaMax; i++){
6a806062   pfrison   PercTeacher test ...
66
67
68
69
70
71
              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...
72
          }
6601d4de   pfrison   Test moteurs sur ...
73
  	
6a806062   pfrison   PercTeacher test ...
74
  	send_serial('b');
6601d4de   pfrison   Test moteurs sur ...
75
  	send_serial(0x01);
7d329109   pifou   PercTeacher progr...
76
77
78
      }
      return 0;
  }