Wednesday, October 1, 2014

Designing a Brew Chamber Controller (Update 5)

I've also updated the trend function.  I wanted to incorporate Object Oriented Programming OOP into the codebase and go full OOP in the final GUI version so I started on the trend function which is of no importance to the operation of the program in case I screwed it up :).

The main program loop calls the Trend Class  method move_averages(). Move_averages stores the last 4 temperature readings.  It then calls the set_average and set_trend methods. These methods store the average temperature over the last minute, and the trend.  These values are then accessible from other functions, for example, if we had an object called trending that was of the class Trend, we could access the average temperature and trend by calling trending.moving_avg_temp and trending.trend respectively.

The complete code can be found at: https://github.com/cyberlord8/bcc

#Trend Class##############################################
#calculates whether temp went up or down or stayed the same since last checked
#may make this an averaging function so we check the current temp compared to last # averages

class Trend:

  def __init__(self):#######################

    #initialize static variables
    self.trend ="-"
    self.moving_avg_temp = 0
    self.temp1 = 0
    self.temp2 = 0
    self.temp3 = 0
    self.temp4 = 0

    return


  def move_average(self):#Called every 15 seconds from main program loop#

    #move the temperatures through the 4 variables
    #since this is updated every 15 seconds there is one minute of data stored here
    self.temp4 = self.temp3
    self.temp3 = self.temp2
    self.temp2 = self.temp1
    self.temp1 = current_temperature

    self.set_average() #average the 4 values
    self.set_trend() #set the trend indicator

    return


  def set_trend(self):######################

    if current_temperature > self.moving_avg_temp+.01: self.trend = "^" #upward trend
    elif current_temperature < self.moving_avg_temp-.01: self.trend = "v" #downward trend
    else: self.trend = "-"
   
    return


  def set_average(self):###################

    self.moving_avg_temp = (self.temp1 + self.temp2 + self.temp3 + self.temp4) / 4

    return

No comments:

Post a Comment