Mario Simaremare / Mbed 2 deprecated g3_waterplay

Dependencies:   mbed

Files at this revision

API Documentation at this revision

Comitter:
mariosimaremare
Date:
Sun Jun 19 23:48:07 2016 +0000
Parent:
6:931a7fe8fa52
Child:
8:6be230b22314
Commit message:
Update the pouring water.

Changed in this revision

Ping.cpp Show annotated file Show diff for this revision Revisions of this file
Ping.h Show annotated file Show diff for this revision Revisions of this file
Procedure.cpp Show annotated file Show diff for this revision Revisions of this file
Procedure.h Show annotated file Show diff for this revision Revisions of this file
Tank.cpp Show annotated file Show diff for this revision Revisions of this file
Tank.h Show annotated file Show diff for this revision Revisions of this file
TemperatureSensor.cpp Show annotated file Show diff for this revision Revisions of this file
main.cpp Show annotated file Show diff for this revision Revisions of this file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/Ping.cpp	Sun Jun 19 23:48:07 2016 +0000
@@ -0,0 +1,54 @@
+#include "Ping.h"
+ 
+#include "mbed.h"
+ 
+Ping::Ping(PinName PING_PIN)
+        : _event(PING_PIN)
+        , _cmd(PING_PIN)
+        , _timer()
+        {
+            _event.rise(this,&Ping::_Starts);
+            _event.fall(this,&Ping::_Stops);
+            _SPEED_OF_SOUND_CM = 33;
+        } 
+        
+void Ping::_Starts(void)
+{
+      _Valid = false;  // start the timere, and invalidate the current time.
+      _Busy = true;
+      _timer.start();
+      _Time = _timer.read_us();      
+}
+ 
+void Ping::_Stops(void)
+{
+      _Valid = true;  // When it stops, update the time
+      _Busy = false;
+      _Time = _timer.read_us()-_Time;
+}
+ 
+void Ping::Send()
+{
+     
+     _cmd.output();
+     _cmd.write(0);  // see the ping documentation http://www.parallax.com/Portals/0/Downloads/docs/prod/acc/28015-PING-v1.6.pdf
+     wait_us(3);
+     _cmd.write(1);
+     wait_us(3);
+     _cmd.write(0);
+     _cmd.input();
+     
+}
+void Ping::Set_Speed_of_Sound(int SoS_ms )
+{
+     _SPEED_OF_SOUND_CM = SoS_ms;
+}
+ 
+int Ping::Read_cm()
+// -1 means not valid.
+{
+    if(_Valid && ~_Busy) 
+        return ((_Time*_SPEED_OF_SOUND_CM)/1000);
+    else 
+        return -1;
+}
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/Ping.h	Sun Jun 19 23:48:07 2016 +0000
@@ -0,0 +1,92 @@
+/* mbed Ping Library
+ * Copyright (c) 2007-2010 rosienej
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to deal
+ * in the Software without restriction, including without limitation the rights
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+ * THE SOFTWARE.
+ */
+    
+ #ifndef MBED_PING_H
+ #define MBED_PING_H
+ 
+#include "mbed.h"
+/** Ping  class, based on an InterruptIn pin, and a timer
+ *  works with the parallax Ping))) sensor (www.parallax.com)
+ *
+ * Example:
+ * @code
+ * // Continuously send pings and read the sensor
+ * #include "mbed.h"
+ * #include "Ping.h"
+ * 
+ * Ping Pinger(p21);
+ * 
+ * int main() {
+ *     int range;
+ 
+ *     while(1) {
+ *  
+ *        Pinger.Send();    
+ *        wait_ms(30);
+ *        range = Pinger.Read_cm();
+ *     }
+ * }
+ * @endcode
+ */
+class Ping {
+  public:
+   /** Create a Ping object connected to the specified InterruptIn pin
+    *
+    * @param PING_PIN InterruptIn pin to connect to 
+    */
+    Ping(PinName PING_PIN);
+    
+     /** Sends a Ping 
+      *
+      * @param none
+      */
+    void Send(void);
+    
+     /** Set the speed of sound, default 33 cm/ms 
+      *
+      * @param Speed of sound in centimeters per milliseconds
+      */
+    void Set_Speed_of_Sound(int SoS_ms);
+    
+    /** Read the result in centimeters
+      *
+      * @param none
+      */
+    int Read_cm(void);
+    
+  protected:
+    
+    InterruptIn     _event;
+    DigitalInOut    _cmd;
+    Timer           _timer;
+ 
+    bool _Valid;
+    bool _Busy;
+    int  _Time;
+    int  _SPEED_OF_SOUND_CM; /* in milliseconds */
+    
+    void _Starts(void);
+    void _Stops(void);
+    
+  };
+  
+  #endif
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/Procedure.cpp	Sun Jun 19 23:48:07 2016 +0000
@@ -0,0 +1,29 @@
+/*
+* G3: WATERPLAY
+*/
+
+#include "Procedure.h"
+
+Procedure::Procedure(
+    Printer &printer
+):
+    _printer(printer)
+{
+}
+int Procedure::proceed()
+{
+    int number_of_procedure = 5;
+    char* procedures[] = {
+        "Q1?",
+        "Q2?",
+        "Q3?",
+        "Q4?",
+        "Q5?",
+    };
+    
+    for(int counter = 0; counter < number_of_procedure; ++counter){
+        _printer.toBoth(procedures[counter]);
+    }
+    
+    return(1);
+}
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/Procedure.h	Sun Jun 19 23:48:07 2016 +0000
@@ -0,0 +1,23 @@
+/*
+* G3: WATERPLAY
+*/
+
+#ifndef PROCEDURE_H
+#define PROCEDURE_H
+
+#include "Printer.h"
+#include "mbed.h"
+
+class Procedure
+{
+public:
+    Procedure(
+        Printer &printer
+    );
+    int proceed();
+
+private:
+    Printer &_printer;
+};
+
+#endif
\ No newline at end of file
--- a/Tank.cpp	Fri Jun 17 12:19:16 2016 +0000
+++ b/Tank.cpp	Sun Jun 19 23:48:07 2016 +0000
@@ -9,90 +9,93 @@
 Tank::Tank(
     Printer &printer,
     DRV8825 &salinitySyringe,
-    DRV8825 &waterSyringe
+    DRV8825 &waterSyringe,
+    Ping &pinger
 ):
     _printer(printer),
     _salinitySyringe(salinitySyringe),
     _waterSyringe(waterSyringe),
+    _pinger(pinger),
     _previous_direction_salt(-1),
     _previous_direction_pure(-1),
     _position_salt(14),
     _position_pure(27)
 {
 }
+
+bool Tank::is_proximity_ok()
+{
+    int range = 0;
+    char buffer[32];
+
+    _pinger.Send();
+    wait(0.1);
+    range = _pinger.Read_cm();
+
+    range = range/2;
+    sprintf(
+        buffer,
+        "Range in cm: %d",
+        range
+    );
+    _printer.toBoth(buffer);
+
+    if (range >= 12) {
+        _printer.toBoth("Proximity ok");
+        return true;
+    }
+    return false;
+}
+
 void Tank::add(int type, int mililiters, int direction)
 {
-    if (mililiters > 0) {
-        if (type == TYPE_SALT) {
-            if (direction == DIRECTION_PULL) {
-                if (_position_salt + mililiters >= POSITION_MAX)
-                    mililiters = 0;
-                else
-                    _position_salt = _position_salt + mililiters;
-            } else {
-                if (_position_salt - mililiters <= POSITION_MIN)
-                    mililiters = 0;
-                else
-                    _position_salt = _position_salt - mililiters;
-            }
-        } else {
-            if (direction == DIRECTION_PULL)
-                if (_position_pure + mililiters >= POSITION_MAX)
-                    mililiters = 0;
-                else
-                    _position_pure = _position_pure + mililiters;
-            else if (_position_pure - mililiters <= POSITION_MIN)
-                mililiters = 0;
-            else
-                _position_pure = _position_pure - mililiters;
-        }
+    for (int i = 25; i < MAX_SPEED; i+=20) {
+        if (type == TYPE_SALT)
+            _salinitySyringe.settings(1/MICROSTEPS_PER_STEP, direction, i);
+        else
+            _waterSyringe.settings(1/MICROSTEPS_PER_STEP, direction, i);
+    }
 
-        //accelerate
-        for (int i = 25; i < MAX_SPEED; i+=20) {
-            if (type == TYPE_SALT)
-                _salinitySyringe.settings(1/MICROSTEPS_PER_STEP, direction, i);
-            else
-                _waterSyringe.settings(1/MICROSTEPS_PER_STEP, direction, i);
-        }
+    //fix the changing of directions for pure
+    if (type == TYPE_PURE) {
+        //mililiters = mililiters * 2.0;
+        if (_previous_direction_pure != direction)
+            mililiters = mililiters + 1.0;
+    }
 
-        //fix the changing of directions for pure
-        if (type == TYPE_PURE) {
+    //fix the changing of directions for salt
+    if (type == TYPE_SALT) {
+        //mililiters = mililiters * 2.0;
+        if (_previous_direction_salt != direction)
+            mililiters = mililiters + 1.0;
+        //if (_previous_direction_salt == DIRECTION_PULL)
+        //mililiters = mililiters + 0.5;
+    }
 
-            mililiters = mililiters * 2.2;
-            if (_previous_direction_pure != direction)
-                mililiters = mililiters + 1;
-        }
+    //move with constant speed
+    for (int i = 1; i < (MOVEMENT + (int)floor(((mililiters - 1.0)*STEP_EXTRA))); i+=1) {
+        if (type == TYPE_SALT)
+            _salinitySyringe.settings(1/MICROSTEPS_PER_STEP, direction, MAX_SPEED);
+        else
+            _waterSyringe.settings(1/MICROSTEPS_PER_STEP, direction, MAX_SPEED);
 
-        //fix the changing of directions for salt
-        if (type == TYPE_SALT) {
-            mililiters = mililiters * 0.9;
-            if (_previous_direction_pure != direction)
-                mililiters = mililiters + 1;
-        }
+        //if ((i - MOVEMENT) % STEP_EXTRA == 0 && !proximity_ok())
+        //  break;
+    }
 
-        //move with constant speed
-        int max_loop = (MOVEMENT + (mililiters - 1) * STEP_EXTRA);
-        for (int i = 1; i < max_loop; i++) {
-            if (type == TYPE_SALT)
-                _salinitySyringe.settings(1/MICROSTEPS_PER_STEP, direction, MAX_SPEED);
-            else
-                _waterSyringe.settings(1/MICROSTEPS_PER_STEP, direction, MAX_SPEED);
-        }
+    //de-accelerate
+    for (int i = MAX_SPEED; i > 0; i-=20) {
+        if (type == TYPE_SALT)
+            _salinitySyringe.settings(1/MICROSTEPS_PER_STEP, direction, i);
+        else
+            _waterSyringe.settings(1/MICROSTEPS_PER_STEP, direction, i);
+    }
 
-        //de-accelerate
-        for (int i = MAX_SPEED; i > 0; i-=20) {
-            if (type == TYPE_SALT)
-                _salinitySyringe.settings(1/MICROSTEPS_PER_STEP, direction, i);
-            else
-                _waterSyringe.settings(1/MICROSTEPS_PER_STEP, direction, i);
-        }
-
-        //remember previous direction
-        if (type == TYPE_SALT)
-            _previous_direction_salt = direction;
-        else
-            _previous_direction_pure = direction;
-        wait(2);
+    //remember previous direction
+    if (type == TYPE_SALT) {
+        _previous_direction_salt = direction;
+    } else {
+        _previous_direction_pure = direction;
     }
 }
 void Tank::react(double salinity)
--- a/Tank.h	Fri Jun 17 12:19:16 2016 +0000
+++ b/Tank.h	Sun Jun 19 23:48:07 2016 +0000
@@ -6,6 +6,7 @@
 #define TANK_H
 
 #include "DRV8825.h"
+#include "Ping.h"
 #include "Printer.h"
 #include "mbed.h"
 
@@ -15,7 +16,8 @@
     Tank(
         Printer &printer,
         DRV8825 &salinitySyringe,
-        DRV8825 &waterSyringe
+        DRV8825 &waterSyringe,
+        Ping &pinger
     );
     static const int DIRECTION_PULL = 1;
     static const int DIRECTION_PUSH = 0;
@@ -25,15 +27,17 @@
     static const int POSITION_MIN = 0;
     static const int MAX_SPEED = 8000;
     static const int MICROSTEPS_PER_STEP = 32;
-    static const int MOVEMENT = 1800;
-    static const int STEP_EXTRA = 2500;
+    static const int MOVEMENT = 4600;
+    static const int STEP_EXTRA = 3000;
     void add(int type, int mililiters, int direction);
     void react(double salinity);
+    bool is_proximity_ok();
 
 private:
     Printer &_printer;
     DRV8825 &_salinitySyringe;
     DRV8825 &_waterSyringe;
+    Ping &_pinger;
     int _status;
     int _strStatus;
     int _previous_direction_salt;
--- a/TemperatureSensor.cpp	Fri Jun 17 12:19:16 2016 +0000
+++ b/TemperatureSensor.cpp	Sun Jun 19 23:48:07 2016 +0000
@@ -38,7 +38,11 @@
     double k1 = K1 * logRT;
     double k2 = K2 * pow(logRT, 3.0);
     double kelvin = 1.0 / (k0 + k1 + k2);
-    this->_temperature = (kelvin + KELVIN_TO_CELCIUS) + VARIANCE;
+    this->_temperature = (kelvin + KELVIN_TO_CELCIUS);
+    
+    if(this->_temperature > KELVIN_TO_CELCIUS){
+        this->_temperature += VARIANCE;
+    }
     
     this->_status = 0.0;
     this->_strStatus = "OK";
--- a/main.cpp	Fri Jun 17 12:19:16 2016 +0000
+++ b/main.cpp	Sun Jun 19 23:48:07 2016 +0000
@@ -44,10 +44,13 @@
     LED4
 );
 
+Ping pinger(p30);
+
 Tank tank(
     printer,
     salinitySyringe,
-    waterSyringe
+    waterSyringe,
+    pinger
 );
 
 Waterplay waterplay(