Thursday 16 April 2015

Thermobot Update

Video of Bot in operation.



The thermobot also stores and plots 24 hours of data. It's finished its job now, but was logging live every minute to a webpage. You can see its results from my conservatory for 16th April 2015 here:

http://jcwyatt.ddns.net/thermobot.html

Initially, pyplot was producing a wierd colouring-in effect under the graph, but adding   

plt.clf()
 
to reset pyplot after each plot fixed the problem. I discovered this accidentally.

the updated code to include the data logging and plotting is here:


import RPi.GPIO as GPIO
import time
import matplotlib
matplotlib.use('agg')
import os
import matplotlib.pyplot as plt


#pseudo code:
#   fill a list with 60 zeros
# set the t1_temp variable to 18 deg c
# every minute:
# get the t2_temperature from the chip
# ext:write the temperature to a list or database
# calc the temperatures difference from previous value
# move the bot an amount relative to the temp difference

#initialise bot settings (stepper motor))
GPIO.setmode(GPIO.BCM)
 
enable_pin = 18
coil_A_1_pin = 7
coil_A_2_pin = 8
coil_B_1_pin = 23
coil_B_2_pin = 24
 
GPIO.setup(enable_pin, GPIO.OUT)
GPIO.setup(coil_A_1_pin, GPIO.OUT)
GPIO.setup(coil_A_2_pin, GPIO.OUT)
GPIO.setup(coil_B_1_pin, GPIO.OUT)
GPIO.setup(coil_B_2_pin, GPIO.OUT)
 
GPIO.output(enable_pin, 1)
 
delay = 2

def forward(delay, steps):
 for i in range(0, steps):
  setStep(1, 0, 1, 0)
  time.sleep(delay)
  setStep(0, 1, 1, 0)
  time.sleep(delay)
  setStep(0, 1, 0, 1)
  time.sleep(delay)
  setStep(1, 0, 0, 1)
  time.sleep(delay)
 
def backwards(delay, steps):
 for i in range(0, steps):
  setStep(1, 0, 0, 1)
  time.sleep(delay)
  setStep(0, 1, 0, 1)
  time.sleep(delay)
  setStep(0, 1, 1, 0)
  time.sleep(delay)
  setStep(1, 0, 1, 0)
  time.sleep(delay)
 
def setStep(w1, w2, w3, w4):
 GPIO.output(coil_A_1_pin, w1)
 GPIO.output(coil_A_2_pin, w2)
 GPIO.output(coil_B_1_pin, w3)
 GPIO.output(coil_B_2_pin, w4)

#from http://www.modmypi.com/blog/ds18b20-one-wire-digital-temperature-sensor-and-the-raspberry-pi:
#load drivers for temperature probe
os.system('modprobe w1-gpio')
os.system('modprobe w1-therm')

temp_sensor = '/sys/bus/w1/devices/28-0414607111ff/w1_slave'

#Get the raw data from the sensor:
def temp_raw():

 f = open(temp_sensor, 'r')
 lines = f.readlines()
 f.close()
 return lines

def read_temp():

 lines = temp_raw()
 while lines[0].strip()[-3:] != 'YES':
  time.sleep(0.2)
  lines = temp_raw()
        
 temp_output = lines[1].find('t=')

 if temp_output != -1:
  temp_string = lines[1].strip()[temp_output+2:]
  temp_c = float(temp_string) / 1000.0
  return temp_c

#initialise current temperature
currentTemp = 14.00

#make an empty list for 24 hours worth of data
tempLog = [0] * (60*24)

#run the bot
while True:
  oldTemp = currentTemp
  currentTemp = read_temp()
  tempDiff = currentTemp - oldTemp
  if tempDiff >= 0:
   steps = tempDiff * 256
   forward(int(delay) / 1000.0, int(steps)) 
  else:
   steps = tempDiff * -256
   backwards(int(delay) / 1000.0, int(steps))
  setStep(0,0,0,0)

#now do the graph stuff:
#add the new reading to the end of the list 

  tempLog.append(currentTemp)

#delete the first item in the list

  del tempLog[0]

#plot the graph as an image    
  
  plt.plot(tempLog)
  plt.ylabel ('Bedroom Temperature (deg C)')
  plt.xlabel ('Time (minutes) (Full width = 24hrs)')
  plt.savefig('/var/www/images/tempLog.png')
  plt.clf()

  hrlog=(tempLog[-60:]) 
  plt.plot(hrlog)
  plt.ylabel ('Temp (deg C)')
  plt.xlabel ('Time (minutes) last hour')
  plt.savefig('/var/www/images/tempLog60.png')
  plt.clf()

  time.sleep(60)
 

No comments:

Post a Comment