Mario Simaremare / Mbed 2 deprecated g3_waterplay

Dependencies:   mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers Tank.cpp Source File

Tank.cpp

00001 /*
00002 * G3: WATERPLAY
00003 */
00004 
00005 #include "Printer.h"
00006 #include "Tank.h"
00007 #include "mbed.h"
00008 
00009 Tank::Tank(
00010     Printer &printer,
00011     DRV8825 &salinitySyringe,
00012     DRV8825 &waterSyringe,
00013     Ping &pinger,
00014     Flasher &alarm
00015 ):
00016     _printer(printer),
00017     _salinitySyringe(salinitySyringe),
00018     _waterSyringe(waterSyringe),
00019     _pinger(pinger),
00020     _alarm(alarm),
00021     _strStatus("Add nothing"),
00022     _previous_direction_salt(-1),
00023     _previous_direction_pure(-1),
00024     _position_salt(14),
00025     _position_pure(27),
00026     _added_liquid(0.0)
00027 {
00028 }
00029 
00030 char* Tank::getStrStatus()
00031 {
00032     return (this->_strStatus);
00033 }
00034 
00035 bool Tank::is_proximity_ok()
00036 {
00037     int range = 0;
00038     char buffer[32];
00039 
00040     _pinger.Send();
00041     wait(0.1);
00042     range = _pinger.Read_cm();
00043 
00044     range = range/2;
00045 
00046     sprintf(
00047         buffer,
00048         "proximity is (%d cm)",
00049         range
00050     );
00051     _printer.toBothln(buffer);
00052 
00053     if (range >= 7) {
00054         return true;
00055     }
00056 
00057     return false;
00058 }
00059 
00060 void Tank::add(int type, int mililiters, int direction)
00061 {
00062     for (int i = 25; i < MAX_SPEED; i+=20) {
00063         if (type == TYPE_SALT)
00064             _salinitySyringe.settings(1/MICROSTEPS_PER_STEP, direction, i);
00065         else
00066             _waterSyringe.settings(1/MICROSTEPS_PER_STEP, direction, i);
00067     }
00068 
00069     //fix the changing of directions for pure
00070     if (type == TYPE_PURE) {
00071         //mililiters = mililiters * 2.0;
00072         if (_previous_direction_pure != direction)
00073             mililiters = mililiters + 1.0;
00074     }
00075 
00076     //fix the changing of directions for salt
00077     if (type == TYPE_SALT) {
00078         //mililiters = mililiters * 2.0;
00079         if (_previous_direction_salt != direction)
00080             mililiters = mililiters + 1.0;
00081         //if (_previous_direction_salt == DIRECTION_PULL)
00082         //mililiters = mililiters + 0.5;
00083     }
00084 
00085     //move with constant speed
00086     for (int i = 1; i < (MOVEMENT + (int)floor(((mililiters - 1.0)*STEP_EXTRA))); i+=1) {
00087         if (type == TYPE_SALT)
00088             _salinitySyringe.settings(1/MICROSTEPS_PER_STEP, direction, MAX_SPEED);
00089         else
00090             _waterSyringe.settings(1/MICROSTEPS_PER_STEP, direction, MAX_SPEED);
00091 
00092         //if ((i - MOVEMENT) % STEP_EXTRA == 0 && !proximity_ok())
00093         //  break;
00094     }
00095 
00096     //de-accelerate
00097     for (int i = MAX_SPEED; i > 0; i-=20) {
00098         if (type == TYPE_SALT)
00099             _salinitySyringe.settings(1/MICROSTEPS_PER_STEP, direction, i);
00100         else
00101             _waterSyringe.settings(1/MICROSTEPS_PER_STEP, direction, i);
00102     }
00103 
00104     //remember previous direction
00105     if (type == TYPE_SALT) {
00106         _previous_direction_salt = direction;
00107     } else {
00108         _previous_direction_pure = direction;
00109     }
00110 }
00111 void Tank::react(double salinity)
00112 {
00113     _strStatus = "Add nothing";
00114     float added_liquid = 0.0;
00115     
00116     if(is_proximity_ok()) {
00117         if(salinity < 0.0) {
00118             _strStatus = "Add salt";
00119             _printer.toBothln("set salt valve to the tank");
00120             _alarm.salinity_flash();
00121             this->add(TYPE_SALT, ADDED_SALT, DIRECTION_PUSH);
00122             _printer.toBothln("set salt valve to the reservoir");
00123             _alarm.salinity_flash();
00124             this->add(TYPE_SALT, ADDED_SALT, DIRECTION_PULL);
00125             added_liquid = ADDED_SALT;
00126         } else if(salinity > 0.0) {
00127             _strStatus = "Add water";
00128             _printer.toBothln("set water valve to the tank");
00129             _alarm.water_flash();
00130             this->add(TYPE_PURE, ADDED_WATER, DIRECTION_PUSH);
00131             _printer.toBothln("set water valve to reservoir");
00132             _alarm.water_flash();
00133             this->add(TYPE_PURE, ADDED_WATER, DIRECTION_PULL);
00134             added_liquid = ADDED_WATER;
00135         }
00136         this->_added_liquid += added_liquid;
00137     } else {
00138         _strStatus = "Tank is overflowing";
00139         _alarm.danger_flash();
00140     }
00141 }
00142 
00143 void Tank::initialize()
00144 {
00145     _printer.toBothln("initializing...");
00146     //initialize
00147     _alarm.water_flash(); //turn the thing to pulling from reservoir
00148     this->add(TYPE_PURE, 20.0, DIRECTION_PULL);//20
00149     _alarm.water_flash(); //turn the thing to pushing to glass
00150     this->add(TYPE_PURE, 20.0, DIRECTION_PUSH);//19.5
00151     _alarm.water_flash(); //turn the thing to pulling from reservoir
00152     this->add(TYPE_PURE, 20.0, DIRECTION_PULL); //20
00153     _alarm.water_flash(); //turn the thing to pushing to glass
00154     this->add(TYPE_PURE, 20.0, DIRECTION_PUSH);
00155     _alarm.water_flash(); //turn the thing to pulling from reservoir
00156     this->add(TYPE_PURE, 25.0, DIRECTION_PULL); //20
00157     //done
00158 
00159     //initialize
00160     _alarm.salinity_flash(); //turn the thing to pulling from reservoir
00161     this->add(TYPE_SALT, 20.0, DIRECTION_PULL);//20
00162     _alarm.salinity_flash(); //turn the thing to pushing to glass
00163     this->add(TYPE_SALT, 20.0, DIRECTION_PUSH);//19.5
00164     _alarm.salinity_flash(); //turn the thing to pulling from reservoir
00165     this->add(TYPE_SALT, 20.0, DIRECTION_PULL); //20
00166     _alarm.salinity_flash(); //turn the thing to pushing to glass
00167     this->add(TYPE_SALT, 20.0, DIRECTION_PUSH);
00168     _alarm.salinity_flash(); //turn the thing to pulling from reservoir
00169     this->add(TYPE_SALT, 25.0, DIRECTION_PULL); //20
00170 }