Saltware / Mbed 2 deprecated Water Play

Dependencies:   mbed DRV88255 TextLCD Ping mbed-rtos

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 #include <vector>
00002 #include <iostream>
00003 
00004 #include "SalinityController.h"
00005 #include "TemperatureController.h"
00006 #include "LCDController.h"
00007 #include "ProximityController.h"
00008 #include "SensorAlarmController.h"
00009 #include "PIDController.h"
00010 #include "settings.h"
00011 #include "testing.h"
00012 #include "MockSensorController.h"
00013 #include "mbed.h"
00014 
00015 extern int testMain();
00016 int realMain();
00017 
00018 int main() {
00019     // Either test_main() or real_main() depending on TEST_MODE define in settings.h
00020     return MAIN();    
00021 }
00022 
00023 
00024 int realMain() {
00025     
00026     
00027     // Collection of all controllers updated in the main loop, updated in the order they were added
00028     std::vector<void *> controllers;
00029 
00030     // -----------------------------------------------------------------------------
00031     /* THE FOLLOWING CONTROLLERS CAN BE THREADED BY SETTING THE FIRST ARG TO TRUE */
00032     /* THE SECOND ARG SPECIFIES THE DELAY AFTER EACH UPDATE, ONLY WHEN THREADED   */
00033     // -----------------------------------------------------------------------------
00034     
00035     TemperatureController temperature(false, 0);
00036     controllers.push_back((void*)&temperature);
00037     
00038     ProximityController proximity(false, 0);
00039     controllers.push_back((void*)&proximity);
00040     
00041     SalinityController salt(false, 0);
00042     controllers.push_back((void *)&salt);    
00043     
00044     // Add alarms to monitor sensor values
00045     SensorAlarmController temp_alarm = SensorAlarmController(false, 0, &temperature,
00046         TEMP_MIN_CRIT, TEMP_MIN_UNDESIRED, TEMP_MAX_CRIT, TEMP_MAX_UNDESIRED);
00047     temp_alarm.setUndesiredErrorMsg("Temp undes!");
00048     temp_alarm.setCriticalErrorMsg("Temp crit!");
00049     controllers.push_back((void *)&temp_alarm);
00050 
00051     SensorAlarmController salt_alarm = SensorAlarmController(false, 0, &salt,
00052         SALT_MIN_CRIT, SALT_MIN_UNDESIRED, SALT_MAX_CRIT, SALT_MAX_UNDESIRED);
00053     salt_alarm.setUndesiredErrorMsg("Salt undes!");
00054     salt_alarm.setCriticalErrorMsg("Salt crit!");
00055     controllers.push_back((void *)&salt_alarm);
00056     
00057     SensorAlarmController prox_alarm = SensorAlarmController(false, 0, &proximity,
00058         VOLUME_MIN_CRIT, VOLUME_MIN_UNDESIRED, VOLUME_MAX_CRIT, VOLUME_MAX_UNDESIRED);
00059     prox_alarm.setUndesiredErrorMsg("Vol undes!");
00060     prox_alarm.setCriticalErrorMsg("Vol crit!");
00061     controllers.push_back((void *)&prox_alarm);
00062     
00063     // PIDController last, as alarms should update first
00064     PIDController pidc(false, 0, &temperature,&salt,&proximity);
00065      
00066     // -----------------------------------------------------------------------------
00067     
00068     
00069     // Show the splash screen indicating the system is starting up
00070     LCDController::splash();    
00071     
00072     int i = 0;   
00073     
00074     // Loop forever, only breaks when an alarm triggers
00075     while(1) {
00076         
00077         // Wait for a specified amount of time after each iteration
00078         Thread::wait(MAIN_THREAD_DELAY_MS);
00079         
00080         // Iterate over all available Controllers
00081         vector<void *>::iterator v = controllers.begin();
00082         while(v != controllers.end()) {
00083             
00084             // Get the next controller
00085             Controller *c = ((Controller *)*v);
00086             
00087             if(!c->isThreaded())
00088                 cout << "Running " << c->getName() << " from main loop" << "\r\n";
00089             
00090             // The controller only updates here if it's not threaded
00091             c->run();
00092             
00093             // Advance to the next controller
00094             v++;
00095         }
00096         
00097         // If the alarm controller detected a dangerous situation then update lcd and possibly exit
00098         if(temp_alarm.isError() || salt_alarm.isError() || prox_alarm.isError()) {
00099             
00100             if(temp_alarm.isError())
00101                 LCDController::showError(temp_alarm.getErrorMessage().c_str());  
00102             else if(salt_alarm.isError())
00103                 LCDController::showError(salt_alarm.getErrorMessage().c_str());  
00104             else
00105                 LCDController::showError(prox_alarm.getErrorMessage().c_str()); 
00106             
00107             #ifdef HALT_ON_ALARM
00108             break;
00109             #endif
00110         }
00111         
00112         if(!prox_alarm.isActive()) {
00113             printf("Running pidcontroller!\r\n");
00114             pidc.run();
00115         } else {
00116             printf("Not running pidcontroller, prox alarm is active!\r\n");    
00117         }
00118 
00119 
00120         //Show temperature, salinity and volume of the water tank on the PC if connected through serial
00121         cout << "Temperature value: " <<  temperature.getValue() << "\r\n";
00122         cout << "Salinity value: " << salt.getValue() << "\r\n";
00123         cout << "Volume value: " << proximity.getValue() << "\r\n";
00124 
00125         // Show either temperature and salinity or the water level given by the proximity controller
00126         if(i++ % 2)
00127             LCDController::updateScreen(temperature.getValue(), salt.getValue(), &pidc);
00128         else
00129             LCDController::updateScreen(proximity.getValue());
00130         
00131     }
00132 
00133     return 1;
00134 }