/*----------------------------------------------*/
/* Définition
des adresses mémoires du 68HC11 */
/*----------------------------------------------*/
int BAUD = 0x102B;
int SCCR1 =
0x102C;
int SCCR2 =
0x102D;
int SCSR = 0x102E;
int SCDR = 0x102F;
int PORTA =
0x1000;
/*----------------------------------------------*/
/* Définition
des constantes */
/*----------------------------------------------*/
int gauche =
0; /* Code pour le moteur gauche */
int droit =
1; /* Code pour le moteur droit */
int car_debut
= 'Y';
int car_fin
= 'Z';
int duree_angle
= 4000;
int baud_rate
= 1200; /* Vitesse par defaut du port série */
float delta
= 1.0; /* Durée de l'éxécution de chaque commande
*/
/*----------------------------------------------*/
/* Définition
des variables */
/*----------------------------------------------*/
int temp0;
int temp1;
int temp2;
int p_gauche;
/* Puissance du moteur gauche */
int p_droit;
/* Puissance du moteur droit */
int rep_G; /*
Répartition de puissance moteur gauche */
int rep_D; /*
Répartition de puissance moteur droit */
int p;
int n;
int i;
int terminer;
int puissance;
int angle;
int duree;
int debut_recu;
int car_recu;
int pointeur;
int tampon[20];
/* Tampon pour la réception des caractères du port série
*/
/*----------------------------------------------*/
/* Définition
des fonctions de gestion. */
/*----------------------------------------------*/
/***************************************/
/* Activation/Désactivation
de pcode. */
/* Activation
-> c = 0 */
/* Désactivation
-> c = 1 */
/***************************************/
void controle_pcode(int
c){
poke(0x3c, c);
}
/*----------------------------------------------*/
/* Définition
des fonctions du port série. */
/*----------------------------------------------*/
/***************************************/
/* Transmission
d'un caractère sur le */
/* port série.
*/
/***************************************/
void Tx(int c){
while( !(peek(SCSR)
&0x80));
poke(SCDR, c);
}
/***************************************/
/* Réception
d'un caractère du port */
/* série.
*/
/***************************************/
int Rx(){
while( !(peek(SCSR)
&0x20));
return peek(SCDR);
}
/***************************************/
/* Modification
de la vitesse du port */
/* série.
*/
/***************************************/
void changer_vitesse_port(int
vit){
printf("\nVit
= %d",vit);
vit = 9600 /
vit; /* 2^x=vit */
for (i=0; i<8;
i++){
vit = vit/2;
if (vit <
1){
break;
}
vit = i + 0x30;
poke(BAUD, vit);
}
/*-----------------------------------------------*/
/* Définition
des commandes, selon le protocole. */
/*-----------------------------------------------*/
/****************************************************/
/* Commande
A = avancer. */
/* Format "YAmnnnZ"
*/
/* m = puissance
de 0 à 9 */
/* nnn = répartition
de puissance du moteur gauche */
/****************************************************/
void avancer()
{
printf("\nAvancer...\n");
puissance =
tampon[1] - '0';
puissance =
(puissance*100)/9; /* Mise en échelle de la puissance */
rep_G = (tampon[2]-'0')*100
+ (tampon[3]-'0')*10 + (tampon[4]-'0');
rep_D = 100-rep_G;
if (rep_G >
100) {
puissance =
0;
rep_G = 0;
rep_D = 0;
}
if (rep_G > rep_D)
{
p_droit = -(rep_D*puissance)/rep_G;
p_gauche = -puissance;
}
if (rep_D >
rep_G) {
p_droit = -puissance;
p_gauche = -(rep_G*puissance)/rep_D;
}
if (rep_D ==
rep_G) {
p_droit = -puissance;
p_gauche = -puissance;
}
motor(gauche,
p_gauche);
motor(droit
, p_droit);
sleep(delta);
motor(gauche,
0);
motor(droit
, 0);
}
/***************************************/
/* Commande
B = tourner */
/* Format "YBnnnZ"
*/
/* nnn = angle
de rotation en degrés, */
/* dans le sens
anti-horaire. */
/***************************************/
void tourner()
{
angle = (tampon[1]-'0')*100
+ (tampon[2]-'0')*10 + (tampon[3]-'0');
if (angle >
360 )
angle = 0;
duree = (int)
(((float) angle/360.0) * (float)duree_angle);
reset_system_time();
motor(gauche,-30);
motor(droit,
+30);
while(1) {
temp0 = (int)
mseconds();
if (temp0 >
duree)
break;
}
motor(gauche,0);
motor(droit,
0);
}
/***************************************/
/* Commande
C = contrôle de l'aimant. */
/* "YC1Z" =
Activation de l'aimant. */
/* "YC0Z" =
Désactivation de l'aimant. */
/***************************************/
void aimant()
{
if ( tampon[1]
== '0')
aimant_arret();
if ( tampon[1]
== '1')
aimant_marche();
}
/*******************************************/
/* Commande
D = vérifier l'état des piles. */
/* "YD0Z" =
Vérifier piles moteurs. */
/* "YD1Z" =
Vérifier piles contrôle. */
/* La puissance
des piles est envoyée au */
/* PC sur le
port série. */
/*******************************************/
void verifier_etat_piles() {
Tx(car_debut);
Tx('D');
if ( tampon[1]
== '0') {
p = verif_pile_mot();
Tx('0');
}
if ( tampon[1]
== '1') {
p = verif_pile_controle();
Tx('1');
}
temp0 = p%100;
Tx( p/100 +
'0' );
p = temp0;
temp0 = p%10;
Tx( p/10 + '0');
Tx( temp0 +
'0');
Tx(car_fin);
}
/***************************************/
/* Commande
F = retour à Pcode. */
/* Format "YFZ"
*/
/***************************************/
void retour_pcode()
{
controle_pcode(0);
changer_vitesse_port(9600);
terminer = 1;
}
/***************************************/
/* Erreur dans
une commande. */
/***************************************/
void erreur_commande()
{
printf("\nErreur
commande\n");
}
/*------------------------------------------------*/
/* Définition
des fonctions électroniques. */
/*------------------------------------------------*/
/***************************************/
/* Activation
de l'aimant. */
/***************************************/
void aimant_marche()
{
poke( PORTA,
peek(PORTA) & 0b11110111 );
}
/***************************************/
/* Désactivation
de l'aimant. */
/***************************************/
void aimant_arret()
{
poke( PORTA,
peek(PORTA) | 0b00001000 );
}
/**********************************************/
/* Vérification
des piles du moteur. */
/* valeur retournée
= puissance de 0 à 100%. */
/**********************************************/
int verif_pile_mot()
{
aimant_marche();
sleep(1.5);
n = analog(5);
if (n < 160)
p = 0;
if ( (n >= 160)
&& (n <= 214) ) {
n = n - 160;
p = (n*31)/54;
}
if ( (n > 214)
&& (n <=229) ) {
n = n - 214;
p = ((n*46)/10)+31;
}
if (n > 229)
p = 100;
aimant_arret();
return p;
}
/**************************************************/
/* Vérification
des piles du module de contrôle. */
/* valeur retournée
= puissance de 0 à 100%. */
/**************************************************/
int verif_pile_controle() {
n = analog(6);
if ( n < 153
)
p = 0;
if ( (n >= 153)
&& (n <=230) ) {
n = n - 153;
p = (n*100)/77;
}
if ( n > 230
)
p = 100;
return p;
}
/*----------------------------------------------*/
/* Début
du programme principal */
/*----------------------------------------------*/
void main() {
aimant_arret();
controle_pcode(1);
printf(" \n");
changer_vitesse_port(baud_rate);
temp0 = init_motors();
debut_recu =
0;
pointeur = 0;
terminer = 0;
while(terminer
== 0) {
while(1) {
car_recu = Rx();
printf("%c",car_recu);
if ( (car_recu
< '0') || ((car_recu > '9') && (car_recu < 'A')) || ((car_recu
> 'Z') && (car_recu < 'a')) || (car_recu > 'z') ){
tampon[0] =
0;
break;
}
if ( (car_recu
>= 'a') && (car_recu <= 'z') )
car_recu = car_recu
- 0x0020; /* Conversion minuscules -> majuscules. */
if (debut_recu
== 1){
tampon[pointeur]
= car_recu;
pointeur = pointeur
+ 1;
if (car_recu
== car_fin){
pointeur = 0;
debut_recu =
0;
break;}
}
if ( (debut_recu
== 0) && (car_recu == car_debut) )
debut_recu =
1;
}
if (tampon[0]
== 'A')
avancer();
if (tampon[0]
== 'B')
tourner();
if (tampon[0]
== 'C')
aimant();
if (tampon[0]
== 'D')
verifier_etat_piles();
if (tampon[0]
== 'F')
retour_pcode();
if (tampon[0]
== 0)
erreur_commande();
}
Retourner à la page principale