Tuesday, June 30, 2009

On Productivity

One of my biggest problems is distraction and forgetfulness. There are so many projects and related things I want to get to that finding time to do them, heck even remembering to do them, is becoming a real challenge. Thats why when I come across good ideas for addressing this, I try to remember them.

Here is a nice collection from lifehacker for productivity

Try some of those out and let me know what you think.

Oh, and Firefox 3.5 was released =) We finally have multi-threaded Firefox =P

Now my system shouldn't hang so bad when I have 100 tabs open. Woot

More learning on the Arduino...

So I was able to control a servomotor with a potentiometer this past weekend. That was pretty cool, it was pretty much 1:1, half turn on the pot was about a half turn on the servo. I was using Analog out to control the motor, most of the tutorials I've found use PWM on a digital pin. Seems my way is easier (but I'm sure there's a reason for the PWM on digital that I have yet to discover).
Then I got the wild hair to hack an old mouse. Didn't work too well, but nothing detrimental. I cut off the PS/2 plug and stripped the wires: red, green, blue and white - notice anything missing? That's right, the black. But that's not the worst of it, the red wasn't even the power in; the blue was. green happened to be ground, red and white were send and receive. It took 5V @ 20 mA - just about the upper safe threshold for the Arduino. I wrote a program that read the red and white and sent back to the computer what it was reading to print on screen. I know something was going on because I initialized the variables to 0 but all I got on screen was:
mouseVar1 = 1
mouseVar2 = 1
mouseVar1 = 1
mouseVar2 = 1...
so it was reading something, but nothing discernible. I read online that the receive line was supposed to be a clock signal, so I'm guessing the mouse was having a hissy fit because it wasn't getting a clock in. I probably should have read a little more on the PS/2 mouse - somehow you're supposed to get an X, a Y, and 3 mouse clicks from just one line.
Next on the list, I'm going to have to buy a few MOSFETs and start learning about controlling high loads. Though here is a gripe I'm having with all of these tutorials, it's a trap I'm sure I'd fall into if I were writing them as well. It seems to me that the people writing these tutorials know the subject so well they're taking for granted that their audience knows certain things. For example, yesterday I searched between 10 and 15 tutorials for controlling DC motors with the Arduino, no two were alike when it came to the additional circuitry required to control the higher loads. That's fine, it shows the flexibility and different options. But what's not fine is not a single one explained why they chose what they did. Some used MOSFETs, others used H-bridges, some used 2 H-bridges, others used commercial motor control chips, and there were more that used off the shelf motor shields. Another example is the resistors, all the tutorials say "use xx Ohm resistor" and I'm left thinking, "Ok, no problem but... why that value?"
I'm mechanical. If I can't see it, I have a hard time figuring out how it works. A little explanation would go a long way to improving some of these tutorials.

[EDIT] Ok, so I'm learning now :P

It seems that the MOSFET is used for simple one direction high load on/off switching.
The H-bridge is for bi-direction, varying loading, and multiple high loads, and seems to be basically an array of MOSFETs.

here's a good academic link:
http://itp.nyu.edu/physcomp/Labs/DCMotorControl

Monday, June 29, 2009

Determining Multiple CPUs with Python

Just a quick entry for something i learned about that's pretty nifty. So if you wanted to take advantage of multiple cpus in python, it used to be you'd have to do a bit of OS detection first. This post has the source i repeat here:


def detectCPUs():
"""
Detects the number of CPUs on a system. Cribbed from pp.
"""
# Linux, Unix and MacOS:
if hasattr(os, "sysconf"):
if os.sysconf_names.has_key("SC_NPROCESSORS_ONLN"):
# Linux & Unix:
ncpus = os.sysconf("SC_NPROCESSORS_ONLN")
if isinstance(ncpus, int) and ncpus > 0:
return ncpus
else: # OSX:
return int(os.popen2("sysctl -n hw.ncpu")[1].read())
# Windows:
if os.environ.has_key("NUMBER_OF_PROCESSORS"):
ncpus = int(os.environ["NUMBER_OF_PROCESSORS"]);
if ncpus > 0:
return ncpus
return 1 # Default


That is a bit of a pain to remember all the time. Thankfully since Python 2.6, you can use the multiprocessing library to handle this for you. All the details of how it determines the number of cpus are now abstracted away from me. I don't really care, just do it =)


import multiprocessing
numCPUs = multiprocessing.cpu_count()


Then you can go from there. For instance, if i have 1 cpu, the program could decide to use the threading library. If i have 2+ cpus then it may benefit from using the full on multiprocessor library. Depends on your application.

Wednesday, June 24, 2009

"My" first Arduino program

Ray invited me to post on his blog to share in learning experiences and to collaborate on different projects. We both seem to be interested in learning the Arduino at the moment, so here's a bit on that, "my" first Arduino program. Not too interesting to many, but to anyone learning microcontrollers (specifically the Arduino) there might be a lesson or two to be gleaned.

I'll admit that it's not 100% mine, infact it's nearly all not mine (thus the quotes around "my"), BUT it didn't come verbatim from a tutorial (I hacked a couple tutorials to come up with it, mainly to force me not to just follow the bouncing tutorial-dot). It's a script that has chasing LEDs (Knight Rider style), and the speed which the LEDs chase is controlled directly with a potatometer (less resistance = faster chase. From 0 millisec up to 1023 millisec, just over 1 full sec in between flashes for max resistance).

The code is posted below. I would have posted pictures but my camera seems to have a crappy USB port, and won't upload pics. A moving picture would be better, but to my knowledge man has yet to invent moving pictures (or "movies" as I would like to call them).


The Schematic shown here is, if nothing else, pretty ugly. The jagged scribbles between the Arduino and the LEDs are resistors (I used 220 Ohm, for no other reason than that's about what all the tutorials use for LEDs - no idea why that specific value though). There's also an error in it, the digital pins 1-5 should be 2-6.

The first thing I realized was that pin 14 is also analog pin 0. They're one in the same, the only difference is when using it as an analog pin with "analogWrite" (or read) it's pin 0, when using it as a digital pin with "digitalWrite" (or read) it's pin 14. The second interesting thing I learned was kind of the opposite of PWM control, though it was inadvertent. I accidentially hooked up the potatometer's ground to the LED ground. When I ran the program, the potatometer controlled the brightness of the LED's rather than their speed. I was using an analog signal to control the digital outputs (in PWM, you use pulsed digital to control analog). Another interesting thing was that the Arduino was still reading the analog 0 pin (which was hooked up to nothing) so it read some random value, which just happened to give me about a half-second delay. Luckily I saw that I was hooked up incorrectly and rewired. The program ran exactly as intended. Turning the pot slowed or sped the chasing lights accordingly.

This excercise also drove home the operation of the "for" function. When the lights are chasing quickly, you don't notice, but when they're chasing slowly and you turn the pot, the way this program is structured, the lights will complete their chase before reading the new pot value to speed things up.

I was also able to notice how the "delay(x)" function delays everything - could be bad to use in certian situations when you need an immediate change or read, etc... if the sketch is in the middle of a "delay" it doesn't seem to respond to anything until it's good and ready.

Thanks for reading, hope I didn't bore you too much and maybe you learned from some of my errors.

Here's the code:


/* Variable Chasing LEDs
This adjusts the rate at which a chasing LED array operates
Circut: Potatometer is attached to analog input 0 (digital pin 14), 5 LEDs are attached to digital outputs 2 through 6 (to ground through 220 Ohm resistors).
*/

int sensorPin = 0; //initalize which pin is potentometer input
int ledPin[] = {2,3,4,5,6}; // initalize LED pin outputs
int numleds = 5; // number of LED outputs in circut
int potVal = 0; // potentometer's value (initalized @ zero)

void setup(){
//loop to initalize LED pins as outputs:
for(i=0; i
pinMode(ledPin[i], OUTPUT);}
}

void loop(){
potVal = analogRead(sensorPin); //read potentometer value

// forward chasing, delayed according to value of potentometer:
for(int i=0; i
digitalWrite(ledPin[i], HIGH);
delay(potVal);
digitalWrite(ledPin[i], LOW);
}
// now back chasing:
for(int i=numleds-1; i>=0; i--){
digitalWrite(ledPin[i], HIGH);
delay(potVal);
digitalWrite(ledPin[i], LOW);
}
}

Monday, June 22, 2009

Arduino and Contributing Author

The main intention of the blog is for it to be as much a way to share research progress as it is a way to keep in touch with friends and family. As such I want to share this space with my good friend Ben who is a fellow Casual Engineer (and who it happens my son is named after). I look forward to future posts from him =)

Ok, now an update on our Arduino progress. Ben got his hands on a Duemilanove this past week and got to monkeying with the tutorials. I have to still order mine but am reading as much as I can beforehand, not to mention daydreaming about all the projects =) MMmmmmm kegerator outfitted with sensors and digital display. It could send a tweet when the keg is running low, or maybe send a preformatted email to the local distributor to order a new one. Excellent!! (insert maniacle laugh)

Here are some of my favorite links so far:
Arduino's main site
Hacknmod
Ladyada
Sheepdog

So far I'm really liking how simple the basic software structure is.
void setup(){
setup stuff;
}

void loop(){
do stuff;
}

pretty cool. more to come later.

oh and a bit of coolness. uav with arduino =P

Solstice and Father's Day

This past weekend was pretty busy. Here in Alaska we celebrate the solstice, the longest day of the year. Fairbanks throws a huge block party and there is a midnight baseball game our local team hosts. Pretty cool.

This year we were absent from the town festivities since our friends Abby and Kyle got married on Saturday. That was a fun time with plenty of beer =P It was an outdoor wedding and wouldn't you know it we had our first big rain of the summer. But the party went on.

Sunday was also father's day =) Becky let me game all day with a few breaks to eat and open presents lol. It was nice to escape to the dungeon for a while. She rocked me with a sweet Dremel tool set!! I can't wait to start building things with it. And her parents surprised me with some desperately needed garden tools like a wheel barrow and yard rakes. It was a sweet father's day. I couldn't have been happier =)

Tuesday, June 16, 2009

Python, Arduino, and CUDA

It seems uncommon anymore to get really pumped about something. And by pumped, i mean real ultimate power pumped. It just so happens that a few things have recently got me that pumped.

1. The birth of my son. No question there and pretty self-explanatory so we'll move onto the others.
2. Python programming language. Holy heck is this a rocking language.
3. Arduino prototyping platform. Once again, rocking.
4. nVidia CUDA library. Using your video card as a math coprocessor? Awesome.

I've been programming for a little while now, mostly Matlab and Perl. Some C/C++ from classes but nothing production. It's only been recent that I've discovered Python, and quite frankly I wish I'd discovered it earlier. I'm enjoying how straightforward the syntax is and how much you can do with little coding. It's very programmer friendly. I intend to write much more about Python, especially since there is now a Python interface to nVidia's CUDA library.

The Arduino is essentially the face of physical computing. Generally speaking, it isn't a trivial thing to get your computer to interface with the real world and act upon it. There's all sorts of kits and what have you for the crummy basic stamp and related trash, but they were always extremely limiting and rather proprietary. In fact I always found those to be rather discouraging.

The Arduino, however, is open source, powerful, and very flexible. There are tons of projects on instructables involving the Arduino. There's tons of info out there where all sorts of people have fiddled with it and made really cool things. Heck here's one that has your plants twitter you when they need watering. Freaking cool. The biggest thing about what I find on instructables is how inspiring the projects are. Everyone swears by how easy the Arduino is to program. It's time to start fiddling.

And then there's nVidia's CUDA library. It essentially allows you to use your video card for matrix math operations. The one I have here at work was able to run a n-body simulation with 27,000 objects at 360 GFlops. Trust me, that's freaking insane. It also did an eigenvector decomposition of a 2048 x 2048 randomly generated matrix in 4.8ms. So yeah, if it's matrix math you need done, especially on a large matrix or system of equations, the CUDA library lets you have a supercomputer on your desktop.

Anyways I'll be writing more about these later. This is just the starting point.

Monday, June 15, 2009

Little Visitor


We had a little visitor in the house this weekend. Looks like he flew in from an open window. This is the second time we've had a bird in the house so it looks like I will need to put a screen up around the back deck. In fact, the whole deck needs some reworking, including the screen, a door, and astro turf.

There's also all this extra wood construction from when the previous owners housed their dog. They even built a ramp for him from the deck to the ground. I guess it had trouble with the stairs. So very sweet, but it has to go.

Yesterday I took the afternoon off work. Becky was thrilled and we planned on going to the movies and hanging out. After a nap of course, since we were exhausted. We wake up to hit the theater and realize it's 8pm lol. Of course the sun is up all day long now, so it doesn't feel like 8pm. Guess we needed to catch up on our sleep =P I ran out the blockbuster and rented some movies and got a Papa Murphy's pizza. We're planning to have a big date on the 30th though. I can't wait =)

Friday, June 12, 2009

Friday with some hockey

So today is Friday and as it turns out, it's also the NHL Stanley Cup finals. To celebrate game 7, the director of our facility is letting us out early. Out here the game will be starting at 4pm, so we're all heading out around 3-3:30. Pretty cool. It helps that she is a die hard hockey fan =P It seems many people in Fairbanks are.

I'm hoping to get quite a bit done this weekend. I really need to clean up the downstairs area at the house. It seems permanently under construction. Since my folks are coming up in a few weeks it seems better to go ahead and get some of this knocked out sooner rather than later.

There's a couple outdoor projects that need to get some traction as well. During the breakup, where all the snow and ice melt, our driveway was completely flooded. Since the drive is all gravel I've been thinking about having some more gravel dumped and spread around in an attempt to raise it. Maybe next year my little civic won't be swimming as bad.

Last weekend I tore through the backyard with the lawnmower, clearing out all the overgrowth. It seems the previous owners didn't do much in the way of yard care. It was looking like nature had reclaimed the land. The mower blasted most of the knee-high growth out so this weekend will be all about raking and preparing it for proper tilling. I just want to turn the soil over and start fresh. Heh, will need to put some before and after photos up to show the difference.

Wednesday, June 10, 2009

Begin

Hello to everyone. With all the requests for updates on Ben and life in alaska with Becky I figured it's time to try blogging. Not ready to do facebook or whatever, and this may allow for more creativity. That and some place to put the random things I get thinking about =P

Thanks, Justin, for giving some inspiration to do this. And best of luck with your move to Japan soon.