Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Tank Level control & Drain valve closure
#11
Hi Ben,

It has been a while.
I wanted to let you know about some progress I have made with the tank level plugin.
I have re-written the code for the ultrasonic distance sensor using the pigpio library.
http://abyz.me.uk/rpi/pigpio/

The library is included with the latest full versions of Raspbian OS. If you are using the light version you can install it with:

Code:
sudo apt-get update
sudo apt-get install pigpio python-pigpio python3-pigpio


and enable it to start at boot up with:

Code:
sudo systemctl enable pigpiod
sudo reboot

The new version of the ultrasonic code is much more accurate than the original and has fewer lines of code. The pigpio
library uses a C program running in the background and does timing in micro seconds.
Here is some output from the new code without averaging multiple measurements:

155.2
155.3
155.2
155.2
155.2
155.2
155.2


If you get a chance, try it out and let me know what you think:

Code:
#!/usr/bin/python

from __future__ import print_function, division
import pigpio
from time import sleep

pi = pigpio.pi()

# Define GPIO pins to use on Pi (BMC numbering)
pin_trigger = 23
pin_echo = 24

start = None
distance = None

# Speed of sound in cm/ms at temperature (changed from cm/s)
temperature = 24
sound_speed = 33.1 + (0.0006*temperature)

#  Set pin modes and output level
pi.set_mode(pin_trigger, pigpio.OUTPUT)
pi.write(pin_trigger, 0)
pi.set_mode(pin_echo, pigpio.INPUT)

def calc(gpio, level, tick):
   """
   Callback function to handle distance calculation.
   """
   global start, distance
   if level == 1: #  Start of echo pulse
       start = tick
   if start and level == 0: #  If start has a value and echo pulse ended
      echo = pigpio.tickDiff(start, tick) # Length of echo pulse in microseconds
      distance = round(((echo / 1000) * sound_speed)/2, 1)
      print(distance)
      start = None

def measure():
   """
   Send a pulse and time the echo.
   """
   pi.gpio_trigger(pin_trigger, 10, 1) #  Send a 10 microsecond pulse on the trigger pin
   cb = pi.callback(pin_echo, pigpio.EITHER_EDGE, calc) #  catch the echo and pass timing to calc callback function

while True:
   measure()
   sleep(1)

I will be working on the plugin over the next few days. The rainy season has started here and I have time for coding.

Dan
Reply
#12
Hi Dan,

It has been a while but we all get busy that's for sure. I've tested the new code which works perfect until it is left running for after 5 minutes then you get a really low number compared to the rest here and there. The plugin will have to operate for a short period or increase the sampling rate. But once again Dan great work!!
Reply
#13
Hmm..

I ran the code for about a half hour and didn't see any great variation in output. Raspbian is not a real-time operating system. It switches between tasks in the background and this may cause occasional problems with timing. I am running the lite version which has fewer background processes than the full version.

I added a check in the code that will skip any reading that is much different from the previous one. This should provide a more consistent output than averaging multiple readings if there is an outlier.

You can find the updated code on GitHub at:
https://gist.github.com/Dan-in-CA/681a1a...48662492c6

Dan
Reply
#14
Ben,

I have a question.
First, the plugin page will have some fields for user input:
  • maximum tank capacity in centimeters
  • Sensor offset (distance of sensor above maximum)
  • minimum water level which will trigger stopping outflow from the tank.
   
click to enlarge.

The water level will be calculated by subtracting (measured distance + sensor offset) from the maximum capacity in cm.
When the water level reaches the minimum, do you want SIP to suspend / stop all programs?
or
Do you want SIP to switch to an alternate water supply?
That would involve using 2 master valves, one for the tank and one for the other water supply. SIP already has a master valve option for a single station to be master. We could probably have the plugin select a second station for the alternate supply. The plugin could re-assign which station is the master without making any changes to the core program.

If the tank is filled by rain, you would probably want to have the plugin switch back to using water from the tank. That would require an additional input field for a water level that would trigger the switch back to using the tank for irrigation.

Let me know.

Dan
Reply
#15
(2018 Dec 12, 12:04 AM)dan Wrote: Ben,

I have a question.
First, the plugin page will have some fields for user input:
  • maximum tank capacity in centimeters
  • Sensor offset (distance of sensor above maximum)
  • minimum water level which will trigger stopping outflow from the tank.
click to enlarge.

The water level will be calculated by subtracting (measured distance + sensor offset) from the maximum capacity in cm.
When the water level reaches the minimum, do you want SIP to suspend / stop all programs?
or
Do you want SIP to switch to an alternate water supply?
That would involve using 2 master valves, one for the tank and one for the other water supply. SIP already has a master valve option for a single station to be master. We could probably have the plugin select a second station for the alternate supply. The plugin could re-assign which station is the master without making any changes to the core program.

If the tank is filled by rain, you would probably want to have the plugin switch back to using water from the tank. That would require an additional input field for a water level that would trigger the switch back to using the tank for irrigation.

Let me know.

Dan
Hi Dan,

Sorry for the long delay in replying, this year has just taken off! The Maximum capacity is 180cm, the measured distance from the sensor to the minimum is 160cm minimum level is 10cm. In this instance there is no backup water supply, just the rain. Could we make this all into a plugin where the maximum capacity, offset to the sensor and backup master valve allocation? 


Cheers

Ben
Reply
#16
OK, I'll see what I can come up with.

will post here when I have something for you to test. It may be a couple of weeks as it is planting time here and there are a couple of other projects I am working on.

Dan
Reply
#17
Hello, I have been using SIP for a few years and now I have started to try to complete and integrate this magnificent system, this plugin would be perfect for my needs and the complement on the website but I see that it has been stopped for a long time and I wanted to ask if it was finished or it was abandoned. thx
Reply
#18
It is still on the list of things to do.

I may have time to work on it soon.

Dan
<p><br></p>
Reply


Forum Jump:


Users browsing this thread: 3 Guest(s)