Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "Lib_Pedagobot_TP4.h"
- #include <stm32f4xx_hal.h>
- #include <stdio.h>
- #include <Board_Buttons.h>
- #include <Board_LED.h>
- typedef enum
- {
- ATTENTE_DEPART,
- DEMARRAGE_DROIT,
- TOURNER_DROITE,
- CHOC,
- ARRET,
- } type_etat;
- // Prototypes
- void tempo_logicielle(unsigned long long int n_iter);
- void init_TIMER6(void);
- void TIMER6(short int x,type_etat etat);
- void init_E_S(void);
- unsigned char getCollisionValues();
- void test (unsigned char *pchoc);
- int main()
- {
- unsigned char GO,fin_tempo=0,choc=0 ;
- init_PedagoBot_TP4();
- Buttons_Initialize();
- LED_Initialize();
- load_Processor(); //Surcharge du processeur
- init_TIMER6();
- init_E_S();
- printf("Initialization done !\n");
- type_etat etat = ATTENTE_DEPART;
- while(1){
- switch (etat) //transition
- {
- case ATTENTE_DEPART :
- if (Buttons_GetState())
- {
- etat = DEMARRAGE_DROIT;
- }
- break;
- case DEMARRAGE_DROIT :
- if (fin_tempo==1)
- {
- fin_tempo=0;
- etat = TOURNER_DROITE;
- }
- if (choc==1)
- {
- etat = CHOC;
- }
- break;
- case TOURNER_DROITE :
- if (fin_tempo==1)
- {
- fin_tempo=0;
- etat = DEMARRAGE_DROIT;
- }
- if (choc==1)
- {
- etat = CHOC;
- }
- break;
- case CHOC :
- etat = ARRET;
- break;
- case ARRET :
- etat = ATTENTE_DEPART;
- break;
- }
- switch (etat) //action
- {
- case ATTENTE_DEPART :
- break;
- case DEMARRAGE_DROIT :
- goStraight();
- TIMER6(6400,etat);
- fin_tempo=1;
- break;
- case TOURNER_DROITE :
- rotateRight();
- TIMER6(1640,etat);
- fin_tempo=1;
- break;
- case CHOC :
- TIMER6(1000,etat);
- break;
- case ARRET :
- stopRobot();
- break;
- }
- /*if (Buttons_GetState())
- {
- GO=1;
- }
- if(GO==1)
- {
- goStraight();
- TIMER6(6400);
- rotateRight();
- TIMER6(1600);
- goStraight();
- TIMER6(6400);
- rotateRight();
- TIMER6(1600);
- goStraight();
- TIMER6(6400);
- rotateRight();
- TIMER6(1600);
- goStraight();
- TIMER6(6400);
- rotateRight();
- TIMER6(1600);
- //}
- if(GO==1)
- {
- goStraight();
- tempo_logicielle(150100000);
- rotateRight();
- tempo_logicielle(36600000);
- goStraight();
- tempo_logicielle(150100000);
- rotateRight();
- tempo_logicielle(36600000);
- goStraight();
- tempo_logicielle(150100000);
- rotateRight();
- tempo_logicielle(36600000);
- goStraight();
- tempo_logicielle(150100000);
- rotateRight();
- tempo_logicielle(36600000);
- } */
- }
- }
- void tempo_logicielle(unsigned long long int n_iter){
- while(n_iter>0){
- n_iter--;
- }
- }
- void init_TIMER6(void)
- {
- RCC->APB1ENR |=(1<<4); //Init horloge TIMER 6
- TIM6->CR1 = (1<<3); //Mode Auto reload preload enable
- TIM6->PSC = 8999; //Prescaleur de 900
- TIM6->EGR |= (1<<0) ; //Enable clock timer 6
- }
- void TIMER6(short int x,type_etat etat)
- {
- unsigned char Cpt_col,col=0,Rappel;
- if(x==6400)
- {
- TIM6->ARR = 63999;
- }
- if(x==1640)
- {
- TIM6->ARR = 15999;
- }
- if(x==1000)
- {
- TIM6->ARR = 5000;
- }
- TIM6->CR1 |= 1; //activation compteur
- while((TIM6->CR1&1)==1)
- {
- Cpt_col=getCollisionValues();
- if ((Cpt_col==0x00)||(Cpt_col==0x01)||(Cpt_col==0x10))
- {
- col=1;
- stopRobot();
- Rappel=TIM6->CNT;
- }
- }
- TIM6->CNT=Rappel;
- if((etat==DEMARRAGE_DROIT)&&(col==1))
- {
- TIM6->CR1 |= 1; //activation compteur
- goStraight();
- while((TIM6->CR1&1)==1);
- }
- if((etat==TOURNER_DROITE)&&(col==1))
- {
- TIM6->CR1 |= 1; //activation compteur
- rotateRight();
- while((TIM6->CR1&1)==1);
- }
- }
- void init_E_S(void)
- {
- //INIT CAPTEUR COLISION 1 et 2
- RCC->AHB1ENR |= (1<<0)|(1<<7); //CLOCK POUR A et H
- GPIOA->MODER &= ~((1<<8)|(1<<9)); //CAPTEUR COLISION 1 en ENTREE sur GPIOA
- GPIOA->PUPDR |= (1<<8); // pull up
- GPIOA->PUPDR &= ~(1<<9);
- GPIOH->MODER &= ~((1<<0)|(1<<1)); //CAPTEUR COLISION 2 en ENTREE sur GPIOH
- GPIOH->PUPDR |= (1<<2);
- GPIOH->PUPDR &= ~(1<<3);
- }
- unsigned char getCollisionValues(void){
- unsigned short idr_A , idr_H ;
- unsigned short Col_D , Col_G;
- unsigned char Sortie_col=0;
- idr_A=GPIOA->IDR;
- idr_H=GPIOH->IDR;
- Col_D =idr_A & 0x0010; // pas appuyé : IDRx=1 appuyé IDRx=0
- Col_G =idr_H & 0x0002;
- if (Col_D!=0)
- {
- Sortie_col |= (1<<0); // pas appuyé
- }
- else
- {
- Sortie_col &= ~(1<<0); // appuyé
- }
- if (Col_G!=0)
- {
- Sortie_col |= (1<<4);
- }
- else
- {
- Sortie_col &= ~(1<<4);
- }
- return(Sortie_col);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement