Code required to drive the microcontroller of the NMF Atlantis

Dependencies:   mbed WakeUp DHT

Files at this revision

API Documentation at this revision

Comitter:
nmf_atlantis
Date:
Mon Jan 20 15:47:07 2020 +0000
Commit message:
NMF Atlantis final version

Changed in this revision

DHT.lib Show annotated file Show diff for this revision Revisions of this file
Pulse.cpp Show annotated file Show diff for this revision Revisions of this file
Pulse.h Show annotated file Show diff for this revision Revisions of this file
RangeFinder.cpp Show annotated file Show diff for this revision Revisions of this file
RangeFinder.h Show annotated file Show diff for this revision Revisions of this file
WakeUp.lib 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
mbed.bld Show annotated file Show diff for this revision Revisions of this file
diff -r 000000000000 -r dbaef09f6d82 DHT.lib
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/DHT.lib	Mon Jan 20 15:47:07 2020 +0000
@@ -0,0 +1,1 @@
+https://os.mbed.com/users/Wimpie/code/DHT/#9b5b3200688f
diff -r 000000000000 -r dbaef09f6d82 Pulse.cpp
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/Pulse.cpp	Mon Jan 20 15:47:07 2020 +0000
@@ -0,0 +1,139 @@
+/* Copyright (c) 2012 Nick Ryder, University of Oxford
+ * nick.ryder@physics.ox.ac.uk
+ *
+ *  MIT License
+ *
+ * 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.
+ */
+ 
+ 
+#include "Pulse.h"
+
+PulseInOut::PulseInOut(PinName pin):
+        startval(0), pulsetime(), runtime(), io(pin)    {
+}
+
+
+PulseInOut::~PulseInOut() {
+}
+
+void PulseInOut::write(int val) {
+    io.output();
+    io = val;
+}
+
+void PulseInOut::write_us(int val, int time)   {
+    io.output();
+    io = val;
+    wait_us(time);
+    io = !val;
+}
+
+int PulseInOut::read_high_us()  {
+    pulsetime.reset();
+    io.input();
+    while (io == 1) {
+    }
+    while (io == 0) {
+    }
+    pulsetime.start();
+    while (io == 1) {
+    }
+    pulsetime.stop();
+    return pulsetime.read_us();
+}
+
+int PulseInOut::read_high_us(int timeout)  {
+    runtime.reset();
+    runtime.start();
+    pulsetime.reset();
+    io.input();
+    while (io == 1) {
+        if (runtime.read_us() > timeout)   return -1;
+    }
+    while (io == 0) {
+        if (runtime.read_us() > timeout)   return -1;
+    }
+    pulsetime.start();
+    while (io == 1) {
+        if (runtime.read_us() > timeout)   return -1;
+    }
+    pulsetime.stop();
+    return pulsetime.read_us();
+}
+
+int PulseInOut::read_low_us()   {
+    pulsetime.reset();
+    io.input();
+    while (io == 0) {
+    }
+    while (io == 1) {
+    }
+    pulsetime.start();
+    while (io == 0) {
+    }
+    pulsetime.stop();
+    return pulsetime.read_us();
+}
+
+int PulseInOut::read_low_us(int timeout)   {
+    runtime.reset();
+    runtime.start();
+    pulsetime.reset();
+    io.input();
+    while (io == 0) {
+        if (runtime.read_us() > timeout)   return -1;
+    }
+    while (io == 1) {
+        if (runtime.read_us() > timeout)   return -1;
+    }
+    pulsetime.start();
+    while (io == 0) {
+        if (runtime.read_us() > timeout)   return -1;
+    }
+    pulsetime.stop();
+    return pulsetime.read_us();
+}
+
+int PulseInOut::read_us()  {
+    pulsetime.reset();
+    io.input();
+    startval = io;
+    while (io == startval)   {
+    }
+    pulsetime.start();
+    while (io != startval)  {
+    }
+    pulsetime.stop();
+    return pulsetime.read_us();
+}
+
+int PulseInOut::read_us(int timeout)   {
+    runtime.reset();
+    runtime.start();
+    pulsetime.reset();
+    io.input();
+    startval = io;
+    while (io == startval)  {
+        if (runtime.read_us() > timeout)   return -1;
+    }
+    pulsetime.start();
+    while (io != startval)   {
+        if (runtime.read_us() > timeout)   return -1;
+    }
+    pulsetime.stop();
+    return pulsetime.read_us();
+}
diff -r 000000000000 -r dbaef09f6d82 Pulse.h
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/Pulse.h	Mon Jan 20 15:47:07 2020 +0000
@@ -0,0 +1,73 @@
+/* Copyright (c) 2012 Nick Ryder, University of Oxford
+ * nick.ryder@physics.ox.ac.uk
+ *
+ *  MIT License
+ *
+ * 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_PULSE_H
+#define MBED_PULSE_H
+
+#include "mbed.h"
+
+/** Pulse Input/Output Class(es)
+ */
+ 
+class PulseInOut   {
+    public:
+        /** Create a PulseInOut object connected to the specified pin
+        * @param pin i/o pin to connect to
+        */
+        PulseInOut(PinName);
+        ~PulseInOut();
+        /** Set the value of the pin
+        * @param val Value to set, 0 for LOW, otherwise HIGH
+        */
+        void write(int val);
+        /** Send a pulse of a given value for a specified time
+        * @param val Value to set, 0 for LOW, otherwise HIGH
+        * @param time Length of pulse in microseconds
+        */
+        void write_us(int val, int time);
+        /** Return the length of the next HIGH pulse in microsconds
+        */
+        int read_high_us();
+        /** Return the length of the next HIGH pulse in microseconds or -1 if longer than timeout
+        * @param timeout Time before pulse reading aborts and returns -1, in microseconds
+        */
+        int read_high_us(int timeout);
+        /** Return the length of the next LOW pulse in microsconds
+        */
+        int read_low_us();
+        /** Return the length of the next LOW pulse in microseconds or -1 if longer than timeout
+        * @param timeout Time before pulse reading aborts and returns -1, in microseconds
+        */
+        int read_low_us(int timeout);
+        /** Return the length of the next pulse in microsconds
+        */
+        int read_us();
+        /** Return the length of the next pulse in microseconds or -1 if longer than timeout
+        * @param timeout Time before pulse reading aborts and returns -1, in microseconds
+        */
+        int read_us(int timeout);
+    private:
+        int startval;
+        Timer pulsetime, runtime;
+        DigitalInOut io;
+};
+
+#endif
diff -r 000000000000 -r dbaef09f6d82 RangeFinder.cpp
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/RangeFinder.cpp	Mon Jan 20 15:47:07 2020 +0000
@@ -0,0 +1,40 @@
+/* Copyright (c) 2012 Nick Ryder, University of Oxford
+ * nick.ryder@physics.ox.ac.uk
+ *
+ *  MIT License
+ *
+ * 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.
+ */
+ 
+ 
+ #include "RangeFinder.h"
+
+RangeFinder::RangeFinder(PinName pin, int pulsetime, float scale, int time):
+    pio(pin), scale(scale), pulsetime(pulsetime), timeout(time)  {
+}
+
+RangeFinder::~RangeFinder() {
+}
+
+float RangeFinder::read_m()  {
+    pio.write_us(1, pulsetime);
+    float t = (float) pio.read_high_us(timeout);
+    if (t == -1.0)   {
+        return -1.0;
+    }
+    return t / scale;
+}
+
diff -r 000000000000 -r dbaef09f6d82 RangeFinder.h
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/RangeFinder.h	Mon Jan 20 15:47:07 2020 +0000
@@ -0,0 +1,49 @@
+/* Copyright (c) 2012 Nick Ryder, University of Oxford
+ * nick.ryder@physics.ox.ac.uk
+ *
+ *  MIT License
+ *
+ * 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_RANGEFINDER_H
+#define MBED_RANGEFINDER_H
+
+#include "Pulse.h"
+
+/** Range Finder class
+*/
+
+class RangeFinder   {
+    public:
+        /** Create a RangeFinder object
+        * @param pin Digital I/O pin the range finder is connected to.
+        * @param pulsetime Time of pulse to send to the rangefinder to trigger a measurement, in microseconds.
+        * @param scale Scaling of the range finder's output pulse from microseconds to metres.
+        * @param timeout Time to wait for a pulse from the range finder before giving up.
+        */
+        RangeFinder(PinName pin, int pulsetime, float scale, int timeout);
+        ~RangeFinder();
+        /** Return the distance to the nearest object, or -1.0 if reading the pulse timed out.
+        */
+        float read_m();
+    private:
+        PulseInOut pio;
+        float scale;
+        int pulsetime, timeout;
+};
+
+#endif
diff -r 000000000000 -r dbaef09f6d82 WakeUp.lib
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/WakeUp.lib	Mon Jan 20 15:47:07 2020 +0000
@@ -0,0 +1,1 @@
+http://mbed.org/users/Sissors/code/WakeUp/#49ca85e8822f
diff -r 000000000000 -r dbaef09f6d82 main.cpp
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/main.cpp	Mon Jan 20 15:47:07 2020 +0000
@@ -0,0 +1,107 @@
+#include "mbed.h"
+#include "DHT.h"
+#include "Pulse.h"
+#include "RangeFinder.h"
+#include "WakeUp.h"
+
+
+RangeFinder rf(D12, 10, 5800, 100000); //(Pin, duree du pulse en us, distance [m] = pulse width [us] / 5800 [us / m] , temps de fonctionnement)
+
+DigitalOut led(LED1);
+Serial serial(D1, D0);
+Serial pc(USBTX, USBRX);
+DHT HTSensor(D10,22);
+AnalogIn battery(A1);
+
+
+int main()
+{
+
+    pc.printf("Starting...\n\r\n\r");
+
+    float d;
+    int dInt;
+    
+    float temperature;
+    int tInt;
+    float humidity;
+    int hInt;
+    float b;
+    int bInt;
+    int cpt = 0;
+  
+      
+    // Depending on the waterway
+    float sensor_limit = 4.0*100.0;
+    float danger_limit = 50.0;
+    int danger_time = 15;
+    int default_time = 30;
+    int sending_interval = 3600; //every hour (1h = 3600 s)
+    
+    while (1)   
+    {   
+        //reduced battery level measurement (between 0 V et 1.85 V) 
+        b = battery.read()*3.3f;
+        bInt = (int)(b*100/3.3f);
+        
+        /*pc.printf("Voltage = %f volts\r\n\r\n", b);
+        pc.printf("Voltage code = %d percentage\r\n\r\n", bInt);*/
+                 
+        //Humidity and temperature measurement
+        
+        HTSensor.readData();
+        wait_us(500);
+        
+        temperature = HTSensor.ReadTemperature(KELVIN) - 273,15;
+        tInt = (int) (temperature + 40);
+        humidity = HTSensor.ReadHumidity();
+        hInt = (int) humidity;
+        
+        //level measurement
+        d = rf.read_m();
+        d = d*100;
+        if (d < 0)  
+        {
+            d = 0;
+            //Timeout Error
+            WakeUp::set(default_time); //time in second
+            deepsleep();
+            cpt += default_time; 
+
+        } 
+        else if (d >= sensor_limit) 
+        {
+            // Seeed's sensor has a maximum range of 4m, it returns
+            // something like 7m if the ultrasound pulse isn't reflected.
+            d = sensor_limit;
+            WakeUp::set(default_time); //time in second
+            deepsleep();
+            cpt += Temps_default; 
+        }
+        else if (d <= danger_limit) //Inondation
+        {
+            dInt = (int)d;
+            //Data sending
+            serial.printf("AT$SF=%04x%02x%02x%02x\r\n",dInt,tInt,hInt, bInt);
+            
+            WakeUp::set(danger_time); //time in second
+            deepsleep();
+            cpt += danger_time;
+        }
+        else
+        {            
+            //Data sending            
+            WakeUp::set(default_time); //time in second
+            deepsleep();
+            cpt += default_time;
+        }
+        
+        if (cpt >= sending_interval)
+        {
+            dInt = (int)d;
+            serial.printf("AT$SF=%04x%02x%02x%02x\r\n",dInt,tInt,hInt, bInt);
+            cpt = 0;
+        }
+
+    }
+}
\ No newline at end of file
diff -r 000000000000 -r dbaef09f6d82 mbed.bld
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/mbed.bld	Mon Jan 20 15:47:07 2020 +0000
@@ -0,0 +1,1 @@
+https://os.mbed.com/users/mbed_official/code/mbed/builds/f9eeca106725
\ No newline at end of file