Monday 30 March 2015

Thermo-Bot Project




In this post, I describe how I took a stepper motor, some Lego(R) and a temperature probe to make a temperature sensitive robot. Basically it's a Pi-on-wheels that moves to the correct position on a temperature scale to indicate the current room temperature. Don't ask why I thought this was a good idea, but I'm quite proud that it works!



Video of it working here (time lapse of 50 minutes) https://youtu.be/bbkwAPPF_vY


I bought a stepper motor with a view to making some kind of robot.

This tutorial from Simon Monk on the Adafruit site is all you need for Pi-controlled stepper motors:

https://learn.adafruit.com/adafruits-raspberry-pi-lesson-10-stepper-motors

I bought my stepper motor from ebay with the controller chip included on a board with some LEDs, which were really helpful in debugging, since the wires in the Adafruit diagram don't 'line up' obviously with the Pi GPIO pins. My motor would only go forwards until I followed the diagram MUCH more carefully. (The fault was with me not Simon's diagram.)

I also did some work with the single wire temperature sensor, again using the Adafruit tutorial from Simon Monk:

https://learn.adafruit.com/adafruits-raspberry-pi-lesson-11-ds18b20-temperature-sensing

Although some of the Python code came from here:

http://www.modmypi.com/blog/ds18b20-one-wire-digital-temperature-sensor-and-the-raspberry-pi:


However, this time it wasn't my fault when it didn't work. With model B+ Pis you have to add this to the /boot/config.txt file:

dtoverlay=w1-gpio,gpiopin=4

Once this was in place the sensor worked fine.

The robot is cobbled together really quickly (badly) using lego. Initial belt-drive version had problems with the elastic-band drive belt jumping off the drive wheel, so direct drive was used instead.

Then it was time to combine the two tutorials and with a few extra lines of code I have a robot that moves up and down a temperature scale according to the current room temperature.


Here's the code I used. You'll notice most of it is pretty much left as per the Adafruit example and the modmypi link above, so credit to Simon Monk and the ModMyPi guru.



import RPi.GPIO as GPIO
import time
import os

#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 start value (and place bot on 14 deg C mark before running)
currentTemp = 14.00


#run the bot
while True:
  oldTemp = currentTemp
  currentTemp = read_temp()
  tempDiff = currentTemp - oldTemp
  print (oldTemp, currentTemp, tempDiff,)
  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)
  time.sleep(60)
  
 
It works!

No comments:

Post a Comment