Guillaume GARBAY / Mbed 2 deprecated Major_dHome

Dependencies:   HTU21D MD25 TextLCD mbed VCNL4000

Fork of HTU21D_HELLOWORLD by Guillaume GARBAY

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 // Librairies
00002 #include "mbed.h"               // Standard
00003 #include "MD25.h"               // Moteurs
00004 #include "TextLCD.h"            // Ecran LCD
00005 #include "HTU21D.h"             // Capteur Temperature / Humidite
00006 #include "VCNL4000.h"           // Capteur Luminosite / Distance
00007 
00008 
00009 // Declarations E/S //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
00010 
00011 DigitalIn bouton (USER_BUTTON);                                     // Bouton d'arret total du robot
00012 DigitalIn bouton_poussoir(D9);                                      // Pin PC7 Bouton poussoir demarrage
00013 DigitalIn cap_arret(PA_9);                                          // Pin PA9 capteur d'arret (Devant)
00014  
00015 Serial pc(SERIAL_TX, SERIAL_RX);                                    // Port Serie avec le PC
00016 
00017 TextLCD lcd(D2, D3, D4, D5, D6, D7,TextLCD::LCD20x4);               // Ecran LDC (rs, e, d4-d7)
00018 MD25 i2c(I2C_SDA, I2C_SCL);                                         // Moteurs  :   100kHz clock, addr B0, reg 0=speedl, 1=speedr (0 (Full Reverse) 128 (Stop) 255 (Full Forward). 15=mode (set to 0 default)
00019 HTU21D temphumid(D14, D15);//HTU21D temp(I2C_SDA, I2C_SCL);                                    // Capteur Temperature / Humidite 
00020 VCNL4000 vcnl(D14, D15 );//VCNL40x0 VCNL40x0_Device (I2C_SDA, I2C_SCL, VCNL40x0_ADDRESS);      // Capteur Luminosite / Distance Define SDA, SCL pin and I2C address 
00021 
00022 
00023 // Fonctions pour les Moteurs ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
00024 // Prototypes ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
00025     void init_motors (void);                            // Initialisation des Moteurs
00026     void setSpeed (int speedMotor1, int speedMotor2);   // Definir Vitesse moteurs
00027 
00028 // Definition ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
00029     void init_motors(void) {
00030         i2c.setMode(0);         // MODE 0, 0=marche arriere 128=stop 255=marche arriere vmax
00031         i2c.setCommand(32);     // 0x20 reset encoders
00032         i2c.setCommand(50);     // 0X32 Disable time out
00033         //i2c.setCommand(32);   // 0x20 reset encoders
00034         //i2c.setCommand(48);   // 0X32 Disable speed regulation
00035     }
00036      
00037     void setSpeed (int vitMotor1, int vitMotor2) {
00038         i2c.setSpeedRegisters(vitMotor1,vitMotor2); 
00039     }
00040 //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
00041 
00042 ////Fonction d'Initialisation //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
00043 // Luminosite /////////////////
00044 void init_vcnl () {
00045 vcnl.setALAveragingFunction( 0x0f );
00046     vcnl.setIRLedCurrent( 2000 );
00047     vcnl.setProximityMeasurementSigFreq( 2 );
00048     vcnl.setProxiModulatorDelayTime( 0x04 );
00049     vcnl.setProxiModulatorDeadTime( 0x01 );
00050 }
00051 
00052 // Initialisation /////////////////
00053 void initialisations () { 
00054     lcd.printf("Initialisation\n");   
00055     pc.printf("Initialisation\n\r");
00056     bouton.mode (PullUp);                       // Bouton Utilisateur 
00057     bouton_poussoir.mode (PullUp);              // Bouton Poussoir
00058     init_motors();                              // Initialise Moteur
00059     int status = vcnl.getStatus();
00060     if( status != 0 ) {
00061         lcd.printf( "Erreur VCNL !!\n\r"); wait (4);
00062         exit( 1 );
00063     }
00064     init_vcnl();
00065     
00066     pc.printf("Fin Initialisation\n\r");
00067     lcd.printf("Fin Initialisation\n");
00068     wait(0.5);
00069 }
00070 
00071 // Fonction Principale //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
00072 int main() { //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
00073     pc.printf("\n\n\n\n\n\n\n\n Majord'home: Hello! Programme\n\r"); //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
00074     lcd.printf("Majord'home: Hello!\n"); //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
00075     wait(1); //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
00076     initialisations (); //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
00077     
00078 // Declaration Variables Locales
00079     float Ftemp, Ctemp, Ktemp, Humid;       // Temperature et humidite
00080     int prox, ambl;                         // Proximite et Luminosite
00081  
00082     
00083 // Init des Temperatures et Humidite
00084     Ftemp = temphumid.sample_ftemp();       Ctemp = temphumid.sample_ctemp();       Ktemp = temphumid.sample_ktemp();       Humid = temphumid.sample_humid();
00085 // Init des Proximite et Luminosite   
00086     prox = vcnl.getProximity();     ambl = vcnl.getAmbientLight();
00087     
00088 // Boucle Infinie //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
00089     lcd.cls();  lcd.locate(0,0);
00090     lcd.printf("%.2f C  %.2f %% Hum", Ctemp, Humid); lcd.locate(0,1);
00091     lcd.printf("%.2f F  %.2f K", Ftemp, Ktemp); lcd.locate(0,2);
00092     lcd.printf("Lum %d  Pr %d", ambl, prox); lcd.locate(0,3);
00093     lcd.printf("Debut While(1)");
00094     pc.printf("Debut While(1)\n\r");
00095     wait(2); lcd.cls();  lcd.locate(0,0);
00096     
00097     while(1) { //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
00098 
00099         // Affichage temperature humidite
00100         lcd.locate(0,0);
00101         Ctemp = temphumid.sample_ctemp();   Humid = temphumid.sample_humid();
00102         lcd.printf("%.2f C  %.2f %% Hum", Ctemp, Humid);
00103         // Affichage Luminosite proximite
00104         lcd.locate(0,1);
00105         prox = vcnl.getProximity();     ambl = vcnl.getAmbientLight(); 
00106         lcd.printf("Lum %d  Pr %d", ambl, prox);       
00107         
00108         
00109         // Arret moteur si Obstacle detecte
00110         lcd.locate(0,2);
00111         cap_arret.read();
00112         if (cap_arret==0) {
00113             lcd.printf("A=0");
00114             setSpeed(128,128);
00115             }
00116         else
00117             lcd.printf("A=1");
00118         
00119         // Lancement moteur
00120         if (bouton_poussoir == 0) 
00121             setSpeed(60,60);   
00122         
00123     }
00124 }