maserCycle.py – Cycle through lasers on servos and draw lines

This is the third of five articles on my 2017 laser science project “Rock, Paper, Lasers.”

WEAR GOGGLES.

To use this code you should have read part one, for servo control, and part two for laser control.

DO NOT DELETE THE PART OF THE CODE THAT REMINDS YOU TO WEAR GOGGLES.

After the project was completed, I used this to make lines and squares. I want to go back and make stars and circles too.

DID I MENTION THE RIGHT COLOR GOGGLES ARE REALLY IMPORTANT?

I started playing with different ways to make the lasers visible too, I got a lot of advice from people, and some of it worked out well. In the end, only a real fog machine was able to do the work.

DON’T FORGET THE GOGGLES.

The little fog machines for science kits and projects didn’t make enough fog, my dad broke my “fog gun,” and other methods were less than successful, although some of them made my new makerspace smell really funny. Big fog machines are fun to play with. 

#!/usr/bin/python

#import libraries
import os
import time
import picamera
import RPi.GPIO as GPIO

#start camera
camera = picamera.PiCamera()

#get timestamp
timeStamp = time.strftime("%Y%m%d_%H%M-%S",time.gmtime())


#set scalar variables for top tilt, bottom tilt, angle increment, start of fire in seconds, counts seconds, seconds per cycle, and Pan (side movement)
topHat = 20
bottomHat = 11
angleHat = 1
startHat = 2
endHat = 30
stepHat = 2
sideHat = 31


#naming array of colors and their values - port on GPIO, name, and what color goggles
colorHat = {
"r" : {
		"port" : 29,
		"name" : "red",
		"goggles" : "blue"},
"b" : {
		"port" : 35,
		"name" : "blue",
		"goggles" : "red"},
"g" : {
		"port" : 33,
		"name" : "green",
		"goggles" : "red or blue"}
}


# Start the servo motors 
#startServod = "--step-size=40us --cycle-time=100000us " # not in use
startServod = "  "
os.system('sudo /home/pi/PiBits/ServoBlaster/user/servod ' + startServod)

# send camera output to video file in "discobunnies" directory
camera.start_recording('discoBunnies/video_' + timeStamp + '.h264')
time.sleep(2)

#variable to start servo at bottom of tilt
ai = bottomHat

#move servos to starting position
os.system("echo 1=" + str(sideHat) + "% > /dev/servoblaster")
os.system("echo 0=" + str(bottomHat) + "% > /dev/servoblaster")
time.sleep(0.5)


#open LASER GPIO ports
GPIO.setmode(GPIO.BOARD)
for myPort in colorHat.keys() :  # same as ["r","b","g"]
	GPIO.setup(colorHat[myPort]["port"], GPIO.OUT)
	GPIO.output(colorHat[myPort]["port"], False)



#what colors can we use? (its an array)
colorsAvail = ('r','g','b')

# add start delay to total of seconds
seconds=startHat

# fire lasers for seconds counted as set above
while (seconds <= endHat):

	#fire each color laser 
	for color in colorHat.keys() :

		#display laser fire and seconds elapsed	
		print "Okay I will fire the " + colorHat[color]["name"] + " laser for " + str(seconds) + " seconds"
		
		#move servo to next increment again (safety)
		os.system("echo 0=" + str(ai) + "% > /dev/servoblaster")
		
		#wait half a second for servo
		time.sleep(0.5)
	
		#prompt user to start firing, display goggles warning	
		raw_input("Press Return To Fire, please wear your " + colorHat[color]["goggles"] + " now...")
		
		#fire laser code try and except if program is interrupted - SAFETY
		try :
			GPIO.output(colorHat[color]["port"], True)
			time.sleep(seconds)
			GPIO.output(colorHat[color]["port"], False)
			
		except : 
			GPIO.output(colorHat[color]["port"], False)
			os.system('sudo killall servod')
			camera.stop_recording()
		
	#increase  angle for next firing
	ai = (ai + angleHat)
	
	#add used seconds to total seconds
	seconds = (seconds + stepHat)

#display all clear prompt
print "Firing complete, remove goggles."	
	
	

#stop firing just in case -- safety only
GPIO.output(29, False)
GPIO.output(35, False)
GPIO.output(33, False)

# move servos back to bottom - safety only
os.system("echo 0=" + str(bottomHat) + "% > /dev/servoblaster")
time.sleep(0.5)

#stop camera 
camera.stop_recording()

#stop servo control system
os.system('sudo killall servod')

WATCH THE FUN VIDEOS.  IF YOU DO THIS IN PERSON WEAR GOGGLES. USE VIDEO TO VIEW IT IN FULL LATER.

This one is a view of the lasers firing with the fog machine running for a few seconds at the beginning and then again at the end of the video.

 

This one is a view from the pi-camera inside the fire box :

Facebooktwitterredditpinterestlinkedinmail

0 thoughts on “maserCycle.py – Cycle through lasers on servos and draw lines”

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.