Skip to content

Commit d355e8c

Browse files
committed
Implementation of a ring buffer in the thread of the analog readings
1 parent 6b24d0c commit d355e8c

File tree

5 files changed

+108
-62
lines changed

5 files changed

+108
-62
lines changed

analogsensor_thread.py

Lines changed: 0 additions & 54 deletions
This file was deleted.

analogsensor_thread_buf.py

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
#! /usr/bin/python3
2+
# -*- coding: utf-8 -*-
3+
#Ricardos.geral@gmail.com
4+
5+
import threading
6+
import time
7+
from statistics import mean
8+
from ringbuffer import RingBuffer #This version uses a circular buffer to avoid unwanted zero readings
9+
import Adafruit_ADS1x15 # Analogic digital conversor ADS 15 bit 2^15-1=32767 (needs to be installed using pip3)
10+
adc = Adafruit_ADS1x15.ADS1115(address=0x48, busnum=1) #address of ADC See in -- sudo i2cdetect -y 1
11+
12+
# Choose a gain of 1 for reading voltages from 0 to 6.14V.
13+
# Or pick a different gain to change the range of voltages that are read:
14+
# - 2/3 = +/-6.144V
15+
# - 1 = +/-4.096V
16+
# - 2 = +/-2.048V
17+
# - 4 = +/-1.024V
18+
# - 8 = +/-0.512V
19+
# - 16 = +/-0.256V
20+
21+
# See table 3 in the ADS1015/ADS1115 datasheet for more info on gain.
22+
GAIN = 1 #2/3, 1, 2, 4 , 6 , 8, 16
23+
DATA_RATE = 250 # 8, 16, 32, 64, 128, 250, 475, 860 samples/second max =860 the higher the number the higher the noise!
24+
25+
class AnalogSensor(threading.Thread):
26+
27+
28+
def __init__(self, sleep, records):
29+
threading.Thread.__init__(self)
30+
self.sleep = sleep
31+
self.records = records
32+
33+
def run(self):
34+
35+
self.A0 = RingBuffer(self.records) # creates a buffer with maximum numbers equal to the number of records /reading
36+
self.A1 = RingBuffer(self.records)
37+
self.A2 = RingBuffer(self.records)
38+
self.A3 = RingBuffer(self.records)
39+
40+
while True:
41+
# Adds measures and keep track of num of measurements
42+
self.A0.append(adc.read_adc(0, gain=GAIN,data_rate=DATA_RATE)) # pin gain and data_rate
43+
self.A1.append(adc.read_adc(1, gain=GAIN, data_rate=DATA_RATE)) # pin gain and data_rate
44+
self.A2.append(adc.read_adc(2, gain=GAIN, data_rate=DATA_RATE)) # pin gain and data_rate
45+
self.A3.append(adc.read_adc(3, gain=GAIN, data_rate=DATA_RATE)) # pin gain and data_rate
46+
47+
time.sleep(self.sleep-0.01*4) #each reading takes about 0.01s. this time is compensated during sleep
48+
49+
def read_analog(self):
50+
"""reads the channels as a rounded mean"""
51+
# Compute the mean
52+
analog=[0]*4
53+
#for channel in range(4):
54+
analog[0] = mean(self.A0.get())
55+
analog[1] = mean(self.A1.get())
56+
analog[2] = mean(self.A2.get())
57+
analog[3] = mean(self.A3.get())
58+
return analog

main.py

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,6 @@
1919

2020
######### make connection to serial UART to read/write NEXTION
2121
ser = nxlib.ser
22-
# ser = serial.Serial(port='/dev/ttyAMA0', baudrate = nxlib.BAUD,
23-
# parity=serial.PARITY_NONE,
24-
# stopbits=serial.STOPBITS_ONE,
25-
# bytesize=serial.EIGHTBITS,
26-
# timeout=0.15)
2722

2823
nxlib.nx_setsys(ser, 'bauds', nxlib.BAUD) # set default baud (default baud rate of nextion from fabric is 9600)
2924
nxlib.nx_setsys(ser, 'bkcmd',0) # sets in NEXTION 'no return error/success codes'

ringbuffer.py

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
#! /usr/bin/python3
2+
# -*- coding: utf-8 -*-
3+
#Ricardos.geral@gmail.com
4+
5+
"""
6+
Buffer ring class used in analogsensor_thread
7+
"""
8+
9+
class RingBuffer:
10+
""" class that implements a not-yet-full buffer """
11+
def __init__(self,size_max):
12+
self.max = size_max
13+
self.data = []
14+
15+
class __Full:
16+
""" class that implements a full buffer """
17+
def append(self, x):
18+
""" Append an element overwriting the oldest one. """
19+
self.data[self.cur] = x
20+
self.cur = (self.cur+1) % self.max
21+
def get(self):
22+
""" return list of elements in correct order """
23+
return self.data[self.cur:]+self.data[:self.cur]
24+
25+
def append(self,x):
26+
"""append an element at the end of the buffer"""
27+
self.data.append(x)
28+
if len(self.data) == self.max:
29+
self.cur = 0
30+
# Permanently change self's class from non-full to full
31+
self.__class__ = self.__Full
32+
33+
def get(self):
34+
""" Return a list of elements from the oldest to the newest. """
35+
return self.data
36+
37+
## sample usage
38+
# if __name__=='__main__':
39+
# x=RingBuffer(5)
40+
# x.append(1); x.append(2); x.append(3); x.append(4)
41+
# print(x.__class__, x.get( ))
42+
# x.append(5)
43+
# print(x.__class__, x.get( ))
44+
# x.append(6)
45+
# print(x.data, x.get( ))
46+
# x.append(7); x.append(8); x.append(9); x.append(10)
47+
# print(x.data, x.get( ))

sensor_server.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,15 +18,15 @@
1818
DFRobot Gravity Analog/Digital Turbidity Sensor, 5V 40mA DC
1919
3 pressure sensors : 0-5psi 5V Pressure Transducer Transmitter Sensor or Sender
2020
flow sensor : flowrate and total liters
21-
1.5" DN40 2~200L/min water Plastic Hall Turbine flow sensor industry meter
21+
1.25" DN32 1~120L/min water Plastic Hall Turbine flow sensor industry meter (Sea brand)
2222
2323
It also records the time and date of the measure.
2424
2525
"""
2626

2727
# Libraries required
2828
from time import sleep
29-
from analogsensor_thread import AnalogSensor, GAIN, DATA_RATE
29+
from analogsensor_thread_buf import AnalogSensor, GAIN, DATA_RATE
3030
from digitalSen_thread import D_Temp
3131
from datetime import datetime
3232
from math import log as log
@@ -99,7 +99,7 @@ def init(interval, no_reads):
9999

100100
global analogTread_flag
101101
if analogTread_flag == False: # avoids having multiple threats analog_sensor running
102-
analog_sensor = AnalogSensor(sleep) # average readings in the analog sensors per measure interval for signal stability)
102+
analog_sensor = AnalogSensor(sleep,no_reads) # average readings in the analog sensors per measure interval for signal stability)
103103
analog_sensor.name = "analog_sensors"
104104
analog_sensor.start()
105105
analogTread_flag = True

0 commit comments

Comments
 (0)