
DP3 Milestones
Final Deliverables
Python Program:
from sensor_library import *
import time
import os
from gpiozero import LED
redlight = LED(19)
greenlight = LED(13)
redState = False
muscle_sensor = Muscle_Sensor(0)
def average_value(data_list): #this function calculates the average value of any list
avg_value = sum(data_list)/len(data_list)
#print('avg_value: ',avg_value)
return avg_value
def timer(timer_count):
global redState
while (redState == True):
if (5 <= timer_count <= 10):
print ("go to hospital")
break
else:
timer_count = 0
break
def notification(avg_current_contraction,avg_longterm_contraction):# is only called after three contractions
global redState
#print('avg_current_contraction: ',avg_current_contraction)
#print("avg_longterm contraction:",avg_longterm_contraction*1.1)
if (avg_current_contraction > avg_longterm_contraction*1.1): #if 4th contraction avg is above the average of the first 3 contractions, with 10% error
redlight.on()
redState = True #keeps red LED on and green LED off so that it cannot switch back and forth
greenlight.off()
print("red light is on")
def escalation(avg_current_contraction, avg_longterm_contraction):# only called when red LED is off
global redState
if(avg_current_contraction < avg_longterm_contraction*1.1):# if 4th contraction avg is less than the average of the first 3 contractions, with 10% error
greenlight.on()
print("green light is on")
redState = False #keeps red LED off and green LED on so that it cannot switch back and forth
redlight.off()
def main():
current_contraction_data = []
longterm_contraction_data = []
avg_current_contraction = 0
contraction_count = 0
timer_count = 0
data = True
while data:
#read sensor
current_sensor_data = int(muscle_sensor.muscle_raw())
print('\n'+'current data: ',current_sensor_data)
if (current_sensor_data > 100): #if current data point is above 100
#Inside contraction
current_contraction_data.append(current_sensor_data)# append all new data to current contraction list
avg_current_contraction = average_value(current_contraction_data)#find a running average of current contraction list
timer(timer_count) #need to figure out where to place timer function
timer_count = 0
elif avg_current_contraction > 100: #If current average is above 100
if contraction_count == 0:
contraction_count += 1#contraction timer_counter
longterm_contraction_data.append(avg_current_contraction)#appends first contraction avg to longterm contraction avg list
elif ((len(longterm_contraction_data) != 0) and (avg_current_contraction != longterm_contraction_data[-1])):#appends current average to the longterm contraction avg list after every contraction
longterm_contraction_data.append(avg_current_contraction)
avg_longterm_contraction = average_value(longterm_contraction_data)
print("Long Term Data List: ", longterm_contraction_data)
elif (len(longterm_contraction_data) > 3):#If longterm contraction list has more than 3 values
notification(avg_current_contraction, avg_longterm_contraction)#runs notification function to check if contractions are increasing in strength
current_contraction_data = []#clears list so that a new avg is calculated for each contraction
timer_count += 1
print("timer: ", timer_count, '\n')
if(redState == False) :#If red LED is off
escalation(avg_current_contraction, avg_longterm_contraction)#runs escalation function to check if contractions are not increasing in strength
else:
contraction_count = 0
time.sleep(1)
redlight.off()
greenlight.off()
main()
​
​
​
CAD Support Frame Model

Final Prototypes

