Carte Romeo
De Wiki Arobose
m (A protégé « Carte Romeo » ([edit=sysop] (infini) [move=sysop] (infini))) |
m (→Commande à distance) |
||
| Ligne 21 : | Ligne 21 : | ||
==== Langage C ==== | ==== Langage C ==== | ||
| + | ===== Fonctions PWM ===== | ||
Pour générer un signal PWM, le microcontroleur a besoin d'un timer interne. Il utilise le timer 3A pour E1 et le timer 4D pour E2. | Pour générer un signal PWM, le microcontroleur a besoin d'un timer interne. Il utilise le timer 3A pour E1 et le timer 4D pour E2. | ||
| Ligne 95 : | Ligne 96 : | ||
</syntaxhighlight> | </syntaxhighlight> | ||
| + | ===== Fonctions moteur ===== | ||
Nous avons ensuite créer des fonctions qui permettent au robot d'avancer, reculer et tourner en précisant la vitesse (de 0 à 255). | Nous avons ensuite créer des fonctions qui permettent au robot d'avancer, reculer et tourner en précisant la vitesse (de 0 à 255). | ||
<syntaxhighlight lang="c"> | <syntaxhighlight lang="c"> | ||
| + | |||
/* stop */ | /* stop */ | ||
void | void | ||
stop(void) | stop(void) | ||
{ | { | ||
| + | PORTD &= ~(1 << PORT4); /* E1 (PD4) = 0 */ | ||
| + | PORTE &= ~(1 << PORT6); /* E2 (PE6) = 0 */ | ||
pwm_4D_write(0); | pwm_4D_write(0); | ||
pwm_3A_write(0); | pwm_3A_write(0); | ||
| Ligne 108 : | Ligne 113 : | ||
* speed from 0 to 255 */ | * speed from 0 to 255 */ | ||
void | void | ||
| − | advance( | + | advance(uint8_t m1_speed, uint8_t m2_speed) |
{ | { | ||
PORTD |= (1 << PORT4); /* E1 (PD4) = 1 */ | PORTD |= (1 << PORT4); /* E1 (PD4) = 1 */ | ||
PORTE |= (1 << PORT6); /* E2 (PE6) = 1 */ | PORTE |= (1 << PORT6); /* E2 (PE6) = 1 */ | ||
| − | + | pwm_3A_write(m1_speed); | |
| − | + | pwm_4D_write(m2_speed); | |
} | } | ||
| Ligne 119 : | Ligne 124 : | ||
* speed from 0 to 255 */ | * speed from 0 to 255 */ | ||
void | void | ||
| − | back_off( | + | back_off(uint8_t m1_speed, uint8_t m2_speed) |
{ | { | ||
PORTD &= ~(1 << PORT4); /* E1 (PD4) = 0 */ | PORTD &= ~(1 << PORT4); /* E1 (PD4) = 0 */ | ||
PORTE &= ~(1 << PORT6); /* E2 (PE6) = 0 */ | PORTE &= ~(1 << PORT6); /* E2 (PE6) = 0 */ | ||
| − | + | pwm_3A_write(m1_speed); | |
| − | + | pwm_4D_write(m2_speed); | |
} | } | ||
| Ligne 130 : | Ligne 135 : | ||
* speed from 0 to 255 */ | * speed from 0 to 255 */ | ||
void | void | ||
| − | turn_left( | + | turn_left(uint8_t m1_speed, uint8_t m2_speed) |
{ | { | ||
PORTD &= ~(1 << PORT4); /* E1 (PD4) = 0 */ | PORTD &= ~(1 << PORT4); /* E1 (PD4) = 0 */ | ||
PORTE |= (1 << PORT6); /* E2 (PE6) = 1 */ | PORTE |= (1 << PORT6); /* E2 (PE6) = 1 */ | ||
| − | + | pwm_3A_write(m1_speed); | |
| − | + | pwm_4D_write(m2_speed); | |
| + | |||
} | } | ||
| Ligne 141 : | Ligne 147 : | ||
* speed from 0 to 255 */ | * speed from 0 to 255 */ | ||
void | void | ||
| − | turn_right( | + | turn_right(uint8_t m1_speed, uint8_t m2_speed) |
{ | { | ||
PORTD |= (1 << PORT4); /* E1 (PD4) = 1 */ | PORTD |= (1 << PORT4); /* E1 (PD4) = 1 */ | ||
PORTE &= ~(1 << PORT6); /* E2 (PE6) = 0 */ | PORTE &= ~(1 << PORT6); /* E2 (PE6) = 0 */ | ||
| − | + | pwm_3A_write(m1_speed); | |
| − | + | pwm_4D_write(m2_speed); | |
} | } | ||
</syntaxhighlight> | </syntaxhighlight> | ||
| − | Pour finir, | + | Pour finir, la fonction principale main() et les fonctions setup() qui définissent l'état des pins (entrée ou sortie) et active les modules PWM. |
<syntaxhighlight lang="c"> | <syntaxhighlight lang="c"> | ||
| − | |||
void | void | ||
| − | + | motor_setup(void) | |
{ | { | ||
/* Motor controller */ | /* Motor controller */ | ||
| − | DDRD = (1 << PORTD4); /* PD4 as output pin */ | + | DDRD = (1 << PORTD4); /* E1 (PD4) as output pin */ |
| − | DDRE = (1 << PORTE6); /* PE6 as output pin */ | + | DDRE = (1 << PORTE6); /* E2 (PE6) as output pin */ |
| − | + | ||
| − | |||
| − | |||
| − | |||
| − | |||
pwm_3_setup(); | pwm_3_setup(); | ||
pwm_4_setup(); | pwm_4_setup(); | ||
| + | |||
pwm_3A_enable(); | pwm_3A_enable(); | ||
pwm_4D_enable(); | pwm_4D_enable(); | ||
| + | } | ||
| + | |||
| + | void | ||
| + | setup(void) | ||
| + | { | ||
| + | motor_setup(); | ||
} | } | ||
| Ligne 201 : | Ligne 208 : | ||
USART_1_setup(void) | USART_1_setup(void) | ||
{ | { | ||
| − | + | DDRD &= ~(1 << PD2); /* initialize pin PD2 (RX) as input pin */ | |
| − | + | DDRD |= (1 << PD3); /* initialize pin PD3 (TX) as output pin */ | |
UBRR1 = 103; /* 9600 Baud at 16MHz */ | UBRR1 = 103; /* 9600 Baud at 16MHz */ | ||