Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependencies: mbed bloc_io mbed-rtos html EthernetInterface
Diff: main.cpp
- Revision:
- 2:172619ae2eb7
- Parent:
- 1:687bcd4b1c6c
- Child:
- 3:5945b9edc15a
diff -r 687bcd4b1c6c -r 172619ae2eb7 main.cpp
--- a/main.cpp Mon Oct 28 10:09:20 2019 +0000
+++ b/main.cpp Fri Nov 22 13:23:17 2019 +0000
@@ -1,5 +1,3 @@
-
-
//#include "EthernetInterface.h"
#include <stdlib.h>
#include <stdio.h>
@@ -11,33 +9,37 @@
#define RADIUS 0.2F // wheel size
#define NBPOLES 8 // magnetic pole number
#define DELTA_T 0.1F // speed measurement counting period
-
-DigitalOut PWM_VALID (p21); // valid pmw mbed pin
+#define PI 3.14F // PIIIIIII
+DigitalOut PWM_VALID (p21);
+InterruptIn TopHall (p22); // valid pmw mbed pin
AnalogIn POIGNEE (p17); // analog input connected to mbed
Bloc_IO MyPLD(p25,p26,p5,p6,p7,p8,p9,p10,p23,p24);// instantiate object needed to communicate with PLD
Serial pc(USBTX, USBRX); // tx, rx
void PWM ();
+void Mesure();
+void CalculV ();
+void saveFile(); // sauvegarde fichier
Ticker AUTO;
+Ticker CalculVitesse;
float GazMax,GazMin;
-int val,x,Overcurrent,Brake,FLTA,Direction,HALLC,HALLB,HALLA;
-char cChoix=0;
-
-
-float valpwm;
+int pwmmanuel,x,Overcurrent,Brake,FLTA,Direction,HALLC,HALLB,HALLA;
+char cChoix;
+int pwm_final=0;
+int valpwm=0;
+int mode=0;
+float vitesse;
+int tophall;
+int tophalltotal;
+int vitesssebride;
+float vitessemesure;
+float vitessebride = 25.0;
+int tophallpartiel;
// Top_Hall Pin
-
-
-
/************ persistent file parameters section *****************/
LocalFileSystem local("local");// Create the local filesystem under the name "local"
-
-
-
-
-
/********************* web server section **********************************/
var_field_t tab_balise[10]; //une balise est présente dans le squelette
@@ -52,7 +54,6 @@
bool bCan_Active=false;
-
DigitalOut led1(LED1); //initialisation des Leds présentes sur le micro-controleur Mbed*/
DigitalOut led2(LED2);
DigitalOut led3(LED3); // blink when can message is sent
@@ -60,27 +61,18 @@
-
-
//************ local function prototypes *******************
-
-
-
-
-
-
-
/**************** Read persistent data from text file located on local file system ****************/
-
-
-
/**************** write persitant data to text file located on local file system ****************/
-
-
+void saveFile()
+{
+ FILE *fp = fopen("/local/gr1.txt", "w");
+ if(fp != NULL) fprintf(fp,"valeur max : %f, valeur min: %f, tophalltotal: %d, vitesse de la bride: %f, tophallpartiel:%d",GazMin,GazMax,tophalltotal,vitessebride,tophallpartiel);
+ else printf("ERREUR FICHIER NON VALIDE");
+ fclose(fp);
-
-
+}
//************** calibation gaz function needed to record min_gaz and max_gaz value to persistent text file ******************
@@ -90,14 +82,9 @@
//********************** timer interrupt for speed measurement each 100ms *************************
-
-
-
-
//********************* Timer Interrupt for gaz ref management each 10ms ********************
-
/********* main cgi function used to patch data to the web server thread **********************************/
void CGI_Function(void) // cgi function that patch web data to empty web page
{
@@ -109,7 +96,6 @@
/*********************** CAN BUS SECTION **********************/
-
void CAN_REC_THREAD(void const *args)
{
int iCount,iError;
@@ -121,20 +107,18 @@
}
}
-
-
-
//*************************** main function *****************************************
int main()
{
MyPLD.write(0);
+ PWM_VALID.write(1);
char carac;
FILE *fp = fopen("/local/gr1.txt", "r");
if(fp != NULL) {
- fscanf(fp,"valeur max : %g, valeur min: %g",&GazMin,&GazMax);
- printf("Valeur min:%g , Valeur max:%g\n\r",GazMin,GazMax);
+ fscanf(fp,"valeur max : %f, valeur min: %f, tophalltotal: %d, vitesse de la bride: %f, tophallpartiel:%d",&GazMin,&GazMax,&tophalltotal,&vitessebride,&tophallpartiel);
+ printf("Valeur min:%f , Valeur max:%f ,tophalltotal:%d,vitesse de la bride:%f,tophallpartiel:%d\n\r",GazMin,GazMax,tophalltotal,vitessebride,tophallpartiel);
} else
- printf("ERREUR FICHIER NON VALIDE");
+ printf("ERREUR FICHIER NON VALIDE\n\r");
fclose(fp);
// int write,read;
@@ -143,9 +127,6 @@
// fclose(f_poignee);
// printf("&d",read);
-
-
-
//***************************************** web section ********************************************/
//Init_Web_Server(&CGI_Function); // create and initialize tcp server socket and pass function pointer to local CGI function
//Thread WebThread(Web_Server_Thread);// create and launch web server thread
@@ -158,7 +139,10 @@
//bCan_Active=true;// needed to lauchn CAN thread
//Thread CanThread(CAN_REC_THREAD);// create and launch can receiver thread
//********************* end can bus section *****************************************************
- AUTO.attach(&PWM,0.1);
+ TopHall.mode(PullUp);
+ AUTO.attach(&PWM,0.02);
+ TopHall.rise(&Mesure);
+ CalculVitesse.attach(&CalculV,0.1);
pc.printf("programme scooter mbed \n\r");
while(cChoix!='q' and cChoix!='Q') {
pc.printf("Veuillez saisir un choix parmi la liste proposee:\n\r");
@@ -166,8 +150,12 @@
pc.printf("b:Calibration poignee\n\r");
pc.printf("d:Mode automatique\n\r");
pc.printf("e:Lecture du registre interne\n\r");
+ pc.printf("f:Mesure de la vitesse\n\r");
+ pc.printf("g: Saisie bride\n\r");
+ pc.printf("h: Lecture vitesse\n\r");
+ pc.printf("i: Compteur kilometrique\n\r");
+ pc.printf("j: RAZ compteur partiel\n\r");
pc.printf("q:quitter\n\r");
-
//************* multithreading : main thread need to sleep in order to allow web response */
while (pc.readable()==0) { // determine if char availabler
Thread::wait(10); // wait 10 until char available on serial input
@@ -175,54 +163,30 @@
/************* end of main thread sleep ****************/
pc.scanf("%c",&cChoix);
switch (cChoix) {
- case 'a':
+ case 'a': // COMMANDE MANUEL
+ mode=1;
printf("veuillez entrer une valeur de PWM entre 0 et 255\n\r");
- scanf("%d",&val);
- /* if (val < 0 && val > 255) {
- PWM_VALID.write(0);
- MyPLD.write(0);
- printf("Erreur\n\r ");
- printf("veuillez entrer une valeur de PWM entre 0 et 255 \n\r");
- scanf("%d",&val);
- }*/
-
- /* PWM_VALID.write(1);
- MyPLD.write(val);*/
+ scanf("%d",&pwmmanuel);
break;
- case 'q':
- AUTO.detach();
- PWM_VALID.write(0);
- if (PWM_VALID.read()==0) MyPLD.write(0);
- printf("BYE");
- break;
- case 'b':
+ case 'b': // CALIBRATION DE LA POIGNEE DE GAZ (VALEUR MIN ET MAX)
AUTO.detach();
PWM_VALID.write(0);
if (PWM_VALID.read()==0) MyPLD.write(0);
printf("Veuillez entrer n'importe quel caractere si vous etes OK pour la valeur min\n\r");
- scanf("%c ",&carac);
+ scanf("%c",&carac);
GazMin=POIGNEE.read();
printf("\n\r");
printf("Veuillez entrer n'importe quel caractere si vous etes OK pour la valeur max\n\r");
- scanf("%c ",&carac);
+ scanf("%c",&carac);
GazMax=POIGNEE.read();
printf("\n\r");
printf("Valeur de la poignee min:%g\n\r",GazMin);
printf("Valeur de la poignee max:%g\n\r",GazMax);
- FILE *fp = fopen("/local/gr1.txt", "w");
- if(fp != NULL) fprintf(fp,"valeur max : %f, valeur min: %f",GazMin,GazMax);
- else printf("ERREUR FICHIER NON VALIDE");
- fclose(fp);
+ saveFile();
break;
- case 'd':
- PWM_VALID.write(1);
- AUTO.attach(&PWM,0.1);
- printf("Veuillez entrer n'importe quel caractere si vous voulez sortir du mode automatique\n\r");
- scanf("%c ",&carac);
- AUTO.detach();
- MyPLD.write(0);
+ case 'd':// MODE AUTOMATIQUE
+ mode=0;
break;
-
case 'e':
x=MyPLD.read();
Overcurrent= (x & 64)/64 ; //Technique pour avoir la valeur en binaire
@@ -233,59 +197,76 @@
HALLB=(x & 2)/2;
HALLA= x & 1;
int hall=0;
- if (HALLA == 1){
- hall++;
+ if (HALLA == 1) {
+ hall++;
}
- if (HALLB == 1){
- hall+=2;
+ if (HALLB == 1) {
+ hall+=2;
}
- if (HALLC == 1){
+ if (HALLC == 1) {
hall+=4;
}
pc.printf("hall=%d\r\n", hall);
- if (Overcurrent == 1) {
- printf("Surintensité ");
+ if (Overcurrent == 0) {
+ printf("Surintensite dans le moteur\n\r ");
} else {
- printf( " desactive");
+ printf( "Pas de surintensite dans le moteur\n\r");
}
-
+
if (Brake == 1) {
- printf(" desactive");
+ printf("Frein desactive\n\r");
} else {
- printf( " active");
+ printf( "Frein active\n\r");
}
-
- if (FLTA == 1) {
- printf("Overcurrent active");
+
+ if (FLTA == 1) { // verifier s'il n'y a pas de registre interne
+ printf("pas erreur registre interne\n\r");
} else {
- printf( " desactive");
+ printf( "erreur registre interne \n\r");
}
-
+
if (Direction == 1) {
- printf("marche avant active");
+ printf("marche avant active\n\r");
} else {
- printf( "mzrche avant desactive");
+ printf( "marche avant desactive\n\r");
}
- // printf("%d\n\r",x);
- //printf("Overcurrent:%d, Brake:%d,FLTA:%d,Direction:%d,HALLC:%d,HALLB:%d,HALLA:%d\n\r",Overcurrent,Brake,FLTA,Direction,HALLC,HALLB,HALLA);
- // break;
+ break;
+ case 'f': // VITESSE DE LA ROUE DU SCOOT
+ printf("vitesse de la roue: %f\n\r",vitesse);
+ break;
-
+ case 'g': // SAISIE DE LA BRIDE
+ printf("Veuillez saisir la valeur de la bride entre 0 et 30 km/h \n\r");
+ scanf("%f",&vitessebride);
+ saveFile();
+ printf("Valeur de la bride\n\r", vitessebride);
+ break;
+ case 'h': // VITESSE DE LA ROUE DU SCOOT
+ printf("Vitesse:%1.3f km/h \n\r",vitessemesure*3.6);
+ break;
- //f_poignee = fopen("nom-de-fichier","w");
- // while(read=fgetc(f_poignee)!= E0F) {
- // write= GazMin;
- // fputc('Valeur min:',f_poignee);
- // fputc(write,f_poignee);
- //write= GazMax;
- // fputc('Valeur max':,f_poignee);
- //fputc(write,f_poignee);
- // fclose(f_poignee);
+ case 'i': // DISTANCE DE LA ROUE TOTAL ET PARTIEL
+ printf("Distance parcourue totale: %f metres\n\r",(tophalltotal*2*PI*RADIUS)/(6*NBPOLES));
+ printf("Distance parcourue partielle: %f metres\n\r",(tophallpartiel*2*PI*RADIUS)/(6*NBPOLES));
+ break;
+ case 'j': // REMISE A ZERO DU COMPTEUR PARTIEL
+ printf("Appuyez sur n'importe quel caractere pour remettre a zero le compteur partiel\n\r");
+ scanf("%c",&carac);
+ tophallpartiel=0;
+ saveFile();
+ break;
+ case 'q': // QUITTER LE PROGRAMME
+ AUTO.detach();
+ CalculVitesse.detach();
+ PWM_VALID.write(0);
+ if (PWM_VALID.read()==0) MyPLD.write(0);
+ printf("BYE");
+ break;
}
- }
- pc.printf("fin programme scooter mbed\n\r");
-} // end while
-
+ saveFile();
+ pc.printf("fin programme scooter mbed\n\r");
+ } // end while
+}
//************** thread deinit *********************
//DeInit_Web_Server();
//bCan_Active=false;
@@ -295,29 +276,53 @@
void PWM()
{
x=MyPLD.read();
- Overcurrent= (x & 64)/64;
Brake= (x & 32)/32;
- FLTA=(x & 16)/16;
- Direction=(x & 8)/8;
- HALLC=(x & 4)/4;
- HALLB=(x & 2)/2;
- HALLA= x & 1;
- if (cChoix =='a') {
+ if (mode==1) { // MODE MANUEL
MyPLD.write(0);
- if (val > 0 && val <=255) {
+ if (pwmmanuel > 0 && pwmmanuel <=255) {
PWM_VALID.write(1);
- MyPLD.write(val);
+ MyPLD.write(pwmmanuel);
}
- } else {
- if ( Brake == 0 ) {
- PWM_VALID.write(0);
+ } else if(mode==0) {// mode automatique
+
+ if ( Brake == 0 ) { //frein actif
MyPLD.write(0);
- } else if ( Brake == 1 ) {
- PWM_VALID.write(1);
- valpwm =(int)(255/(GazMax-GazMin))*POIGNEE.read()-(255/(GazMax-GazMin))*GazMin;
- MyPLD.write(valpwm);
+ pwm_final=0;
+ } else if ( Brake == 1 ) { // frein désactif
+
+ valpwm =(255/(GazMax-GazMin))*POIGNEE.read()-(255/(GazMax-GazMin))*GazMin; // calcul de la PWM brute
}
- }
+
+ if ((vitessemesure *3.6) > vitessebride) { // VITESSE MESURE
+ pwm_final-=1;
+ } else {
+
+ if (pwm_final<valpwm) { // test pwm final (gestion progressive)
+
+ pwm_final+= 1;
+
+ } else pwm_final=valpwm;
+ if (pwm_final>255) { //limitation pwm final
+ pwm_final=255;
+ }
+ if(pwm_final<0) { //limitation
+ pwm_final=0;
+ }
+ }
+ MyPLD.write(pwm_final);
+ }
}
+void Mesure ()
+{
+ tophall++; //incrémentation nombre de pôle;
+}
+void CalculV ()
+{
+ int hall = tophall;
+ vitessemesure=(hall*2*PI*RADIUS)/(6*NBPOLES*DELTA_T);
+ tophalltotal += tophall;
+ tophallpartiel += tophall;
+ tophall=0;
+}
\ No newline at end of file