Monday, September 2, 2013

Django, Dajax, and Getting Dajax to work (Lite-Em-Up Component Organizer)

Django

After futzing around with all sorts of database application approaches, I finally settled with Django. Here's what I messed with:
  1. Pure sqlite implementation with python application for interface
  2. MS Access Database
  3. Finally, Django
The pure approach at the top is probably what I'd end up going for if I ever wanted to mass produce this, MS Access was painful and nearly useless (it was a dark time in my life). Django has offered the easiest database creation/ application development framework of all three. So it was very attractive for my "just get it done" state of mind. Not only that but most of the interface for working on the database contents has already been made for you (in the admin site). The only thing left for me was figuring out how I was going to use the nice interface to make a box light up... a button perhaps? A button that could run a python script. The tricky part is how do you wire up the button which is in a webpage that Django serves, to run your dead simple python script? And how do you pass the script the number of the box to light up?

The answer is of course, Dajax. Not only will dajax serve me well for this project, but also for all the other projects I have going at work to create database systems for control component management and planning.

Dajax

Starting from here, I was able to gather that this dajax thing was pretty powerful, and handy. But the example leaves a lot out. I tried going through the setup documentation, but it is also operating from the assumption that you pretty much know what you're doing... Not only that, but there are almost no very basic tutorials for the current (0.9 dajax, 1.5 django) releases. So that's what I am going to write here, somewhat for you, and mostly for me.

How to get a button on a django webpage to execute a python script using dajax:

This tutorial assumes that you already have Django 1.5 setup. I do not claim to be any sort of an expert, my training was in mechatronics and physics, so getting Django to work for me has been an exercise in hacking all the way. That being said, here we go!

Dajax Setup

First install dajax and dajaxice using pip.
 sudo pip install django_dajax  

Then follow these instructions to add dajaxice to your project (required!). Some points for clarification: the urls.py to add your dajaxice url to is for the site (not the app!). You probably could have figured that out, but just to save you some questioning.

Finished? Great! Now follow these instructions to install dajax. For clarification this is what I did for the jquery step ( I was confused because I thought you simply add this line to your head:
 {% static "/static/dajax/jquery.dajax.core.js" %}  
 but you need to embed it sensibly like so:
 <script type="text/javascript" src="{% static "/static/dajax/jquery.dajax.core.js" %}"></scrip>  
t>

Also make sure you load the staticfiles lib into the template (place this at the top of the template):
 {% load staticfiles %}  

The meat

Here's my template page all together:
 {% load dajaxice_templatetags %}  
 {% load staticfiles %}  
 <head>  
 <script type="text/javascript" src="{% static "/static/dajax/jquery.dajax.core.js" %}"></script>  
 <script type="text/javascript" src="{% static "/static/eci/jquery-2.0.3.js" %}"></script>  
 {% dajaxice_js_import %}  
 </head>  
 <button onclick="Dajaxice.eci.light_up_box(Dajax.process);">Show me where</button>  

A few things to note:

  • I downloaded the newest jquery from the jquery website here, and placed it in the app folder under static/eci/ where eci is the name of my django app.
  • The onclick call goes as Dajaxice.appname.function in the ajax.py.
Here's my ajax.py code which resides in my app folder (called eci in my case, for electronics component inventory):

 from dajax.core import Dajax  
 from dajaxice.decorators import dajaxice_register  
 import serial, time  
 @dajaxice_register  
 def light_up_box(req):  
   dajax = Dajax()  
   dajax.alert("Hello World!")  
   serial_port = serial.Serial('/dev/ttyUSB0', 9600, timeout=None)  
   box_number = 4  
   number = str(box_number)  
   if box_number <10:  
     number = '0'+number+'X'  
   else:  
     number = number+'X'  
   serial_port.write(number.strip())  
   serial_port.close()  
   return dajax.json()  

I use the dajax.alert function to confirm independently that the dajax function was called. I'll probably use it later to confirm that the box was found and the part removed (ask for a quantity or something).

That's the long and short of it. I hope it helps you. I was really sad when I couldn't find a basic tutorial that explained the stupid questions I had, hopefully you don't have the same problem!

Wrap up

So as of last night the electronics component organizer has been improved a bit. I have a larger current power supply connected, which will hopefully help with the power issues I've been having. I have also finally secured my pcbs and power supply to the top of the box so it's not so janky. Also, now that I have this dajax thing working I can finally start putting together the rest of the database viewing pages that will allow me to light up boxes at will.

Thoughts for the future:

I think I might have made a mistake trying to get my computer to stream the FFT data to the arduino over serial. I may end up making a new pcb that has an audio jack pass through so that the FFT can be done directly on the arduino. Once the basic database is done I'll probably go ahead and populate the organizer with parts. That will probably take a good part of a Saturday.

Thursday, May 16, 2013

Lite-It-Up Component Organizer.

So it's been a long time. Actually over a year since I last posted about this project. There has been progress. Unfortunately for the past several months it's kind of been on the backburner. Between working on my house, working at SLAC, and nailing down my rhythm I haven't had mad this project a high priority. Hopefully the blog post means that will change soon. Enough words, let's get down to what's happened.

Where we were

So I had just finished a quick test with some foil to see if the idea of painting the drawers would serve to turn them into an effective light tube. It worked. I had also bought the LEDs and the driver chips. I made a proto-board version of the backplane, and that sucked, so I knew I needed to find a better solution. Enter Halted Electronics.

The backplane and mounting

So I found this awesome proto-board at Halted that was basically tinned copper clad on one side, with predrilled holes in it (just like regular protoboard). I mapped out on the boards where the LEDs would sit and cut out some isolated areas on the board. I forget if the rest of the board was Vcc or Gnd... Then all I had to do was solder the LEDs, and the connector, and run wire back to the connector. The other LED legs were all on a common plane.




And, yes, it took a couple of weekends to do.

For mounting the board to the drawer holder I just made sure to line them up a bit and drilled a hole in the back. Then I used a couple of regular screws and a screw driver and fixed them to the back.






Painting the drawers

Probably the longest and most arduous task in this project. To assist in the process I started out using these pieces of cardboard, but that was too slow. I ended up just using masking tape and spending a few hours taping up all the drawers. 64 drawers later I had them all painted. Then it was time to do it again!







LED Drivers and whatnot

As discussed previously the LEDs are driven using the TLC5940. My prototype uses 4 of them, one for each board, and an arduino to control them. Most of the software was already written so I designed a PCB and sent it out to the board house.



Software

Once all of the wires were soldered, and the boards populated I went about mapping the LEDs in the Arduino. With a properly mapped LED matrix I was able to start working on the screensaver. One really neat thing about this box system is that it's also great for parties.
I wanted to have the drawers light up along with music from my computer. Being that I like to do things the hard way I decided that it would be awesome to have the computer stream the results of a Fourier transform done in soft real-time. After searching around on the internet for a while I found a visualizer that I could easily hack to suit my needs. 
Impulse is a simple music visualizer for Ubuntu that looks good by itself. I liked it because it was already set up to interface with Pulseaudio. Pulseaudio is an audio software layer in Ubuntu, which has the nice feature of automatically creating a soft channel monitor for every output stream it handles. This is the software output monitor that Impulse uses to pass the audio stream to its FFT. From there it was a simple matter of importing impulse, calling the function, and massaging the output into my array -> serial protocol -> serial com link to the arduino(child's play in python).
Once I had the array in the arduino (the array is basically 8 numbers that represent the energy in each frequency band) it was straightforward to control the LEDs.

One issue that I had/have was timeout. Sometimes the serial link will have too much information passed down it. One symptom was that the device would simply quit working. To fix this I increased the baud rate, and turned on the watchdog feature for the arduino. Now if the arduino hangs because of serial issues, the watchdog will notice and restart the arduino. Here's more info on the watchdog features of the arduino: Arduino Watchdog.

I also had to do some tweaking to have the equalizer be more 'active'. Here's a video of what the system behaved like before the tweaking: pre-tweak.

After working with it a bit, this is what I have...




So what's next?

I have some time tonight. With the Maker Faire coming up on Sunday I am reminded that I have a bunch of pretty cool ideas backed up right now because I haven't finished this thing. Motivation is at an all time high, so I think I'll try and ride the wave all the way back into shore. First thing is first. I will need to finish development of my parts database. I want to have a good interface that will allow me to quickly add parts, reorganize the drawers, keep track of quantity, etc... I consider this step done when I have the database somewhat populated, I can complete searches using simple phrases like "1.uF," and the result will be returned via a lit up drawer.

After that is completed (I think about a week or so), I want to start making my android app. The android app is where I'll get voice search capabilities from. 

As a final stroke I will also include a feature that will allow a user to build a BoM and have the system automatically light up drawers so that parts can be retrieved all at once. Additionally this thing will alert you when you run low on a part so you'll know to buy more.

Saturday, May 26, 2012

Remote power button


Visual layout


I recently set up my projector to enjoy movies more comfortably from my couch. To do this I decided it would be awesome to "hide" my projector inside of the closet that comprises almost an entire side of my apartment. I wanted the video quality from my computer to be halfway decent on the projector, so I decided to move the computer into the closet as well so it could sit next to the projector, and maintain a short run of cable. At the same time, I still wanted to be able to sit at my desk and use my keyboard and monitor like a normal computer.

Room Layout
This was accomplished by getting some cables from Monoprice.com. But I still had the nagging problem of always walking into my closet, and reaching to the shelf my PC is on, to turn on the computer. So I decided to install a remote power button into my desk, using a button I had bought for this exact purpose a couple of years ago. It is a particularly awesome button.

I spend some time this past week drilling a hole into my desk (which is basically extruded steel), into which I could mount the button. My first attempts consisted of using regular drill bits followed by a Dremmel to widen the hole (to .75 in). It only took me about 10 minutes of grinding away at the desk to realize I was using the wrong tool. So I went online and ordered a set of step drill bits. They arrived last night.



I managed to finish the job in another ten minutes. Nothing feels quite like using the right tool for the job. I mounted the button and proceeded to wire it up. Crossing my fingers and praying that the computer circuit just needed a normally open dry contact, I attached the button (I had left my multimeter at work), in parallel with the PC button pins. It worked!



Another lesson learned long ago, is conscientious wiring. I used a screw terminal on the outside of the PC case, followed by jumpers inside. So if I ever need to open my case later, I can just disconnect the wires inside to not be bothered.

I finished the wiring job and cleaned off my desk (killing two birds with one stone), to take some pictures. I really like how clean my setup is now (in both senses).


Friday, May 4, 2012

Light-It-Up Component Organizer

I got the idea for doing this from this guy: http://www.instructables.com/id/StorageBot-voice-controlled-robotic-parts-finder/ . After seeing that video and learning about the TLC5940NT (a 16 bit PWM driver which can be daisychained to even greater orders), I felt inspired to finally clean up my lab supplies. I have too often felt disrupted by not having parts when I thought I did, and not knowing where those parts were when I needed them. With this system I will hopefully solve both problems, as my entire inventory will need to be recorded, and organized.

Designing the PCB's for the control circuit was simple enough. I just followed these diagrams: http://code.google.com/p/tlc5940arduino/. The main challenge so far in the project is keeping costs down while retaining a higher level of quality than I have adhered to in my projects so far. I have a bit more money to invest in this project, so I intend to make it as clean as possible.


Here's a picture of my proof-of-concept. The trouble with clear drawers is the light cross-talk between drawers when they are lit from the rear. To solve this issue I taped some aluminum foil around the drawer like a hotdog bun.  This solves the issue in the left, right and down directions, but all the drawers above it are still lit. So by adding a second hotdog bun to the drawer above, the original drawer becomes a light-pipe of sorts. I really like how the drawer is fully lit, with the light completely diffused. Of course when the idea is actually implemented I will be using a silver paint and not foil...

I also tested how the concept worked when actually filled with items and I am pleased to say that it works quite well. More on that later.

I started working on the LED backplane last night. I had designed a PCB to accomplish the task, but actually having the PCB made was going to be far too expensive. So I went ahead and tried to etch my own board. The size of the design proved to be too much for the process. I could not get the resist layer to adhere very well to the clad. So I have started making the PCB manually from proto board. It is about as much fun as counting grains of sand. To ease (or add to?) the tedium I bought "Atlas Shrugged" on audiobook. So far, 8 LED's (out of 64) are connected... this project will take some time...


Monday, April 30, 2012

Tesla Coil Overhaul

I spent some time this past weekend fixing my Tesla coil named Zeus. It is a 450W SRSG (synchronous rotary spark gap) coil that can usually produce sparks up to 800cm. Around a month or two ago I was running the coil, having just replaced the transformer (an old Franceformer), when a capacitor failed. The capacitor might have failed due to bad timing on my spark gap (some adjustment will be necessary in the future), or due to my gross oversight in the original design. The original capacitor bank was designed such that the first capacitor and last capacitor in the series were very close to each other. The potential became great enough that the first capacitor failed in a spectacular display of electrical arcing to the leads of the last capacitor.



I have since overhauled the entire capacitor bank, changing the design to keep distances maximized. While I was finishing the capacitor bank I decided to go ahead and overhaul the entire coil's wiring. Prior to the decision I had been using crimp terminals to connect wires on the coil. These shoddy connections caused the coil to be very unreliable. A demonstration item like a Tesla coil should work perfectly every time. To have it fail as a result of bad connectivity is disappointing.

After finishing the overhaul of the capacitor bank and the coil wiring I was ready to test. Usually when testing the coil I can count on the thing to refuse to start for a bit. Often it starts at around 80-90VAC as read on a variac, however, with the recent improvements the coil consistently started around 40V. I was able to achieve sparks at this voltage which was a first.

I expect that I will not have trouble with starting the system. Next I will need to develop a better spark gap but until then...

Saturday, January 21, 2012

MP3 Grabber

There is a website called mp3panda.com, of dubious legality, but that's irrelevant for this post. If someone were to want to download songs from the mp3panda, and they were unfortunately not on an updated system or were running linux, they would not have the convenience of using mp3panda's auto-download software.*

So they would have to resort to manually clicking each download link, selecting the location to download and waiting for each download to finish, so as to not overwhelm their internet connection. Talk about some serious tedium.

I decided today to create a python script that will run on linux and download all the mp3 files from the download page of your account. The only manual labor involved is navigating to the downloads page, expanding the links, saving the page to a working folder, and running the script.

The script is only about 100 lines of code, and takes care of downloading and organizing your downloads into folders. Anyone familiar with python should be able to customiz this to suit their needs.

I used a variety of packages, BeautifulSoup, eye3D, os, time, etc... BeautifulSoup is great for parsing html structures. eye3D was used to gather the mp3 metadata for organizing the files.

Mp3 Grabber

Cheers

*Perhaps the software would run on wine...

Saturday, October 9, 2010

Physics Projects


I love Advanced Lab. PHYS 427, is easily my favorite class this semester. I have to stop myself from spending too much time on it, like I am doing right now.

For the class we have three remaining projects, two small labs, and one major project. I have about five to do.
1. Finish the vacuum chamber. This is an ongoing process. It's about 96% done. Still have a few little things left to tweak. Hopefully I can find time to post about them as I move along.



2. Finish the thermal evaporation plate. This one is a really fun project. I am making a thermal evaporation system for the vacuum chamber. We'll be using it in lab to make a half silvered mirror and some Hall effect samples. I am making it essentially from scratch, most of the design is original, with a lot of inspiration from google image searches. I'll continue to post about that separately from the vacuum chamber as we go along.



3. Make a half silvered mirror. This involves finished the evap system, and will serve as a test for it. If we can successfully make a mirror I think I can say the build was a success.


4. Inverted pendulum. For our PID loop project I choose to make an inverted pendulum. Initially I plan to use LabView to implement the algorithm, but maybe later this semester I'll program an Arduino and make the unit self contained.






5. Hall effect. This one is a doozie. I am currently working to set up a hall effect measurement experiment. Right now that involves a lot of research into techniques and methods for doing it. Hopefully all that knowledge will coalesce into something tangible over the next few weeks. I hope to have something physically established for it by the end of October, giving me plenty of time to work on actually getting it to work before the end of the semester.

Some pictures from the lab.

video