orangenarwhals topnav

Wednesday, November 30, 2011

[Edit: solved, the error was in the fab hello world makefile line
$(OBJCOPY) -j .text -O ihex $(PROJECT).out $(PROJECT).hex
To fix, add -j data or drop the -j option altogether)
See http://www.micahcarrick.com/avr-tutorial-digital-output.html. also, man avr-objcopy.
What was actually happening was in one example the compiler replaces the code with the array data and then loads it into the microcontroller, while for the other example it loads the compiled code into the micro, and in both cases the constant array never makes it into the microcontroller.]

Yes, I tried int instead if char in case C arrays wanted int indices. And volatile char in case the compiler was like "You, variable! You are a useless waste of memory. OPTIMIZED." (sfx notes: that should sound like RECTIFIED in Tron).

Okay, so why doesn't AVR Studio 5 Debugger work?

Turned out AVR Studio debugger is straightforward, but it doesn't work if you have a

Also, AVR Studio 5 is amazing. I can peer at all the DDRB and PORTB and all the pins -- even with an oscilloscope I would need like 8 arms to do this.

Oh right, back from worshiping AVR for making AVR Studio freely
available (the Gobsmacking Glory of which is only slightly diminished by
my sadness that it's not open-source / doesn't run on linux (haven't
tried WINE yet)) and on to compiler issues.

If you're using AVR eclipse (and I strongly recommend you do, as AVR
development has way too many steps to comfortably manage manually), make
sure you change the build target from "Debug" to "Release" or else it
won't create the appropriate binary. Also if you're not using AVR
eclipse it's awesome! http://avr-eclipse.sourceforge.net/wiki/index.php/The_AVR_Eclipse_Plugin

I grabbed the avr-gcc commands from the terminal output at the bottom of AVR Studio 5, edited the filenames, and used it to compile C code on ubuntu. (Note that choosing "Release" uses -Os, full optimization, while choosing "Debug" uses -O0, no optimization. For my code this results in 3 kilobytes flashed versus 150 bytes! -- 3kb = almost 90% of available mem on attiny45)

I'm writing some C code for the attiny45 microcontroller for how to make anything, and I'm debugging a problem where for the exact same C code, AVR Studio creates a happy .hex file but the class-derived makefile creates a "I'm-going-to-half-work-and-drive-you-crazy-debugging" .hex file (see C code above for the symptoms).

The -f options change the behavior of the compiler. -funsigned-char tells it that the 'char' type is by default unsigned (that is, cannot represent negative numbers, but can store numbers twice as large.) Normally in C 'char' is signed, and if you want an unsigned one you have to say 'unsigned char'. This flag swaps it around, so that 'char' is unsigned by default and you have to say 'signed char' if you want a signed one. Generally, when writing microcontroller code, I very much prefer to be explicit, never assuming what the compiler is going to do. <stdint.h> is part of the C standard library and defines types like 'uint8_t' (an unsigned 8-bit integer, same as 'unsigned char') and 'int8_t' (a signed 8-bit integer, same as 'signed char.') I prefer these over the char, short, int, long types because I know exactly what I'm getting.

-funsigned-bitfields says that bitfields are also unsigned. If you don't know what bitfields are, you're probably not using them. (Most code for avr-gcc doesn't use them.)

-fpack-struct says that all structs should be packed. (Look up C structs if you're not sure what these are.) On some processor architectures, memory is addressed a 'word' at a time—on a 32-bit x86 processor, for example, a word is 32 bits (or four bytes) long. The instructions for accessing a word of memory tend to be faster when the word being accessed is 'aligned' to a word boundary, i.e. its address in memory is a multiple of four bytes. In a struct, if you were to have, for example, three fields that were each one byte long (uint8_t), and then one field that was four bytes long (uint32_t), and the beginning of the struct is aligned to a word boundary, then the 32-bit variable won't be, and accessing it will be slower than if it were. So sometimes the compiler will insert 'padding' to make sure that all multi-byte fields in a struct are aligned to word boundaries; in this case, it might insert one byte of padding in between our three single-byte variables and the four-byte variable to push it over onto the word boundary. Telling the compiler that a struct should be 'packed' tells it *not* to insert this padding. On a microcontroller with limited memory, you might prefer the slower access to unaligned fields to the 'wasted' memory that padding creates, or if you're implementing a network protocol that needs the fields to be at specific locations.

-fshort-enums tells the compiler that enums (again, look this up if you don't know what they are) should be stored as a 'short' rather than an 'int'.

My guess is that it's likely one of the above flags that makes your code work, but without seeing your code I couldn't say for sure.

This tells the compiler to include debugging information in the code it generates; if you use a debugger later, it uses this information to map between which instruction in the compiled program corresponds to which line of the C files.

> -Wall

This tells the compiler to enable all warnings. There are some warnings that aren't enabled by default (such as you defined a variable but didn't use it, there's nothing wrong with that, but it might be a mistake.) This can sometimes help point out programming bugs that compile fine but might not be what you meant to write.

> -c

This tells the compiler that it's going to build an object file instead of a complete program. Object files contain compiled functions, but they're not yet 'linked' together into a complete program. When you use a C compiler, one way to do it is to just give it all of your C files at once and have it produce a complete program all in one go (this is how Neil's example Makefiles do it). In more complex programs, it's often useful to produce the intermediate object files first, and then link them together into the complete program at the end. If you have lots of .c files, and only change one, then you only need to rebuild the .o object file corresponding to the .c file that changed. For a big project with lots of code, that can be faster.

You can read more about what 'compiling' and 'linking' mean (lots of info online) if you want to get a better understanding of what's happening.

> -std=gnu99

This tells the compiler to use the GNU99 standard. There are a couple of different variants of C. There's 'ANSI C' or C89 (the '89' corresponds to 1989, the year the standard was ratified.) C99 is a newer revision to the standard that adds some extra features to the language (like being able to declare varaibles at other places than the top of a block.) GNU99 adds in a handful of GCC-specific extensions.

> -mmcu=attiny45

This, of course, tells the compiler what processor to produce code for.

> -MD -MP -MF"v0.1.45.d" -MT"v0.1.45.d"

These are all dependency-tracking options, that tell the compiler to save information about which header files are used by each .c file, which can be used later to determine, if a header changes, which source files need to be rebuilt. This is useful for large projects, but not terribly useful for small microcontroller stuff.

> -o"v0.1.45.o"

This specifies the output filename. Usually the convention for object files is whatever the name of the .c file was, but with .o instead.

> "v0.1.45.c"

And finally, this is the .c file to compile. And that's it for the compilation step. If you had multiple .c files in the program, this would be repeated for each one.

This also uses the avr-gcc command, but since there's no -c flag, it's going to build a complete program instead. The linker will take all of the object files (in this case, only one) and assemble them together into a complete program, figuring out where everything fits in memory. The object files have 'notes' in them that say things like 'here I want to call function foo()'; it's the linker's job to decide where function 'foo()' is actually going to be in memory, and replace that 'note' with the actual address for the program to jump to. The -Wl,-Map= flag tells the linker to also produce a file that says where it's putting things, which can be helpful to reference when debugging sometimes.

The -o flag, like before, specifies the output file; for a complete program, this will be in 'elf' format. (This is a standard format for executable programs, you can read more about it if you want.) Finally, a list of all of the .o files to include in the program follows (in this case, again, we only have the one.)

Mostly for historical reasons, a lot of programming tools won't read an elf file directly. Customarily, the file format that's used is Intel HEX (ihex, or simply .hex.) (Some embedded systems also use Motorola S-record or srec files). The objcopy command here is basically copying the compiled code from the elf file into a hex file. It's told (via the -R flags) to ignore the .eeprom, .fuse, .lock, and .signature sections (so this hex file will basically only contain the program code, or the .text (in executable parlance, 'text' often means compiled code) section.)

> avr-objdump -h -S "v0.1.45.elf" > "v0.1.45.lss"

objdump here is generating a file with the disassembly listing of the compiled program. This is completely unnecessary to generating working code, but sometimes looking at the disassembled program to see what instructions the compiler generated can be a useful debugging tool.

if at any point, you find the experience of writing a nontrivial
makefile akin to swimming upstream in a river of tepid molasses, don't
despair. that is the expected behavior. make sucks, that's life :(.

SCons is good for projects where everyone knows Python. If nobody knows
Python, it's a really terrible idea. :) Just look at Mixxx's
SConstructs. I've attempted to bring some sanity to them, but they're
mostly crap.

And that's only like 70% of the SCons files we have. After dealing with this long enough, I coined this addage:
Give a person a turing-complete build system and they will find a way to club a baby seal.

In my experience, you have two options if you are stuck with make:

1) Spend years learning by example and eventually understand most of what is going on but still be utterly confused 10% of the time (e.g. dealing with autoconf-generated makefiles).

Build systems are like the ultimate doomed project area. There are so many systems that reinvent the wheel in slightly different ways and don't provide a bulletproof solution for everyone.

Every user has completely different requirements and is always migrating from some other system where xyz was easy, and so they just grow and grow features that make them incomprehensible at the end of the day.

Projects in this area that haven't solved all your problems yet:

make

autoconf/automake

cmake (we use this for DynamoRIO and DrMemory)

Boost's jam

jom

eclipse

visual studio

ninja

scons

rake

BSD make

Ant

Maven and all the *other* Java build systems

gyp (we use this for Chrome, go NIH)

the new LLVM build system ddunbar proposed

It's just... I ** hate build systems. They all suck. They're too slow, don't run on platform x, can't crosscompile, mess up the deps so you have to do a clean build...

that second one reminds me of duff's quote about his device (with respect to whether fall-through was ok), "This code forms some sort of argument in that debate, but I'm not sure whether it's for or against.”

then perry huang, some hall alum I have never met I think:

from the creator of make:

"Why the tab in column 1? Yacc was new, Lex was brand new. I hadn't tried either, so I figured this would be a good excuse to learn. After getting myself snarled up with my first stab at Lex, I just did something simple with the pattern newline-tab. It worked, it stayed. And then a few weeks later I had a user population of about a dozen, most of them friends, and I didn't want to screw up my embedded base. The rest, sadly, is history."-- Stuart Feldman

So yea. I'm waiting for when microcontrollers get so powerful and cheap that I can run python on them. >__> <__<

How to fit design pcbs to fit CAD models, specifically how to interchange between eagle and solidworks in 2d (copy pasting from my email b/c I'm too lazy to reformat):

Me: if I want to make a board in eagle which fits inside a shape that I have a solidworks model of (I only care about 2d for now). It's pretty straightforward, so I can do the oldskool from x1 to x2 thing, but what if it were more complicated? Is the only way to do this export an image in solidworks and make a library part of it, or use some import_bmp script and trace over it in eagle, or something crazy? I guess the opposite is easier, show only the board, export image, import into solidworks, and trace it... What if I cared about 3d? Is eagle just have zero support for this?

Charles Guan:
Also, if you want to make a board of a certain shape in Eagle, it's a terrible pain. I was in fact just messing with this tool yesterday:http://www.micromagicsystems.com/#/dxf-converter/4523812840
What I did was make a solid model of the plan of the board (sketch > extrude), then laid it out in a DXF drawing as if I were waterjetting it. If you just export a sketch as a DXF you have to exclude things like dimensions. The tool just imports every DXF entity into the .brd file on the Dimension layer.
Example board outline: http://etotheipiplusone.net/pics/moter/nicesensor3.png

If your board has internal slotting and stuff it will import that too.

You can poke the eagle file on github (https://github.com/nouyang/pov-yoyo > eagle-board > .brd) and see that I simply drew a rectangle starting at the origin and then set the x2 and y2 coordinates according to the 2d solidworks measurements. For my prototype, I didn't even end up using that board outline -- I sheared off the excess copperclad and sanded* the pcb where my shearing was file until the pcb fit inside, and then used rolled up masking tape to keep it in 'cos I sanding failed past pressfit tolerances.

Two buttons, high enough that can be accessed by pressing down on the clear thermoformed plastic cover over the circuitboard (similar to plastic water bottle in feel)

on/off power button(decided against the centrifugal switch designed by paulina mustafa, another classsmate, for their iron man arc reactor yoyo, because gives user option to operate pov-display without spinning the yoyo for a more stable display)

if we order pcbs, can have holes for people to stick FTDI or ISP headers in, so people don't need to solder little wires straight to the microcontroller if they want to edit or update the firmware on the ucontroller (or make a clamp breadboard) or buy one of the clamp programmers

can have another mode (hold down button longer?) to change the flicker rate, since we'll be using internal microcontroller clock which is RC and due to manufacturing variability only accurate to +- 10%

discarded option: right-angle button which sticks out side of yoyo. more complicated injection molds and I was worried if it'd be structural enough, since solder joints would be taking the strain. Seemed most likely to fail after repeated use.

PCB should snap fit in, so people can mill their own pcb's and swap out the pcb if wanted

Rectangular PCB should fit, so it's easier to panelize and easier for people to make their own board (can etch and shear off one side instead of having to have a mill and mill a circular board, although circular boards look cool .___.)

Ideally PCB is single-sided

run off a 3v button cell and contain a spare so people can go longer without ordering their own.

pcb serves as cell-holder, batteries should snap fit in to further simplify mechanical design

TODO not sure how batteries will be electrically connected to the board

for changing the battery / updating the firmware, top half of each side will be removable via a notch on the side

assembly: teach a microcontroller series and have people assemble two and keep one?

--maybe have a fourth state changing the flicker rate, in case a specific microcontroller's clock is off (on/off/text programming/flickr rate programming)

-- I'll just pre-program the flickr rate (and make it adjustable via the light-serial-programming protocol?)

total: $15 per yoyo + some digikey shipping costs, so close to $17 each if we count the aluminum molds and injection molding / thermoforming raw mats as free (due to 2.008). and of course free labor XD

So, at 50 yoyo's, we're looking at $6 each which is over our budget. However, I'm confident if we look a bit harder we can get the prices down to $5 each.

=====

Timeline:

Thursday: [everyone] finish the CAD for the molds
Friday: [nancy] check the dims / tolerances with Pat and Dave
Then CAM over the weekend Saturday, say meet for dinner on putz @ 5pm and then CAM (putzgiving is Sunday). Order electrical parts.
Hopefully mill / turn the molds over Mon - Wed before thanksgiving.
Finish electrical design -> gerber and programming over thanksgiving break and order pcbs if ordering them. by 26th
And injection mold / thermoform the week after (done by Friday December 2nd).
Assembly done by Dec 9th.

=====
PoV no accelerometer notes:
I got nanopov example working this weekend -- no accelerometer indeed means there's an optimum speed the display wants to be waved at. wave it too fast and the image gets stretched out, too slow and the whole image isn't displayed or it's super-squished.

Oh, Tom went over this in his blog post:

One downside of this design is that there is no synchronisation between movement of the display and the display output. So if e.g. you wave it left-right-left all output is mirror-inverted half the time. Also, there is no stable, repeating image as you would get from a constantly rotating POV.

Tools / raw materials search: Maybe we should do our startup around this, for integrating into one of the hardware version control platforms. Octopart is doing it for EE; now lets get the meche side done too. Then we can really have full integrated open source products sharing+version control online, and then life will be awesome :)
If open hardware hub got popular enough (they allow "links" when you submit parts for your project), that would be cool.
[Edit 6 Dec 2011: I found a few search engines. See http://orangenarwhals.blogspot.com/2011/12/hardware-search-engines.html]

We have weekly meeting at Tuesday night, and workshops at weekend. The space is opened every day, and you may want send us a mail before you come so we can make sure there is some members in the space

bjmakerspace, g.com

yff -- not sure if it's actual makerspace looks corporate or spam.

Guangzhou:

hackerspaces@gz (1 member?)

HTG (not active since 2010)

So looks like I should try to make Shanghai and Shenzhen, possibly Beijing although that's covering a lot of ground in a week (mmm transportation costs). Guangzhou ones seem dead, which is a pity since that's actually close to Shenzhen.

Unlike most other hackerspaces, our members have very little free time, and as such, the space is not in constant use. Our members have a key, and can come any time they like, but its hard to predict when that will be.

I would highly suggest that you join our google group and introduce yourself and your schedule there: http://groups.google.com/group/tokyohackerspace You'll have a better chance at finding what everyone is up to, and when they can meet up.

===
update 2 dec 2011
according to David of shanghai's xinchejian, There are current 4 in China. OnionCapsule in Hangzhou is the first student lead hackerspace in the China Academy of Fine Art by a group of new media art students. They have a public performance scheduled on Dec 24. If visiting hackerspaces is one of the goal of your trip, I'd suggest you visit them at this event. It's crazy party and a lot of fun.

Makes me regret not thinking about watermarking my images (free publicity!). Thankfully, these are but brief lapses in my unfailing devotion to laziness.

Meanwhile, I've been exploring the joys of the lasercutter. I found some scrap wood sitting around the lasercutter and etched some of my best friend's art (shout-out to Alice Chung! http://the-crowned.deviantart.com/)

I'm sitting in the media lab shop right now helping a friend, Cathy Wu, make business cards for her imminent plane trip to some conference somewhere (_sigh_ smart people...). I was asleep in my room at 11pm when I heard a loud knocking, which turned out to be a hyper Cathy excited about cutting out business cards with the laser cutter :) I had some anodized aluminum let over from the waterjet clock class / Kevin Rustagi, so there we go.
Giraffe design courtesy of Laura Shumaker, another awesome friend.

The 100 watt lasercutter is definitely not awesome enough to cut this out (we tried full power really slow speed high ppi and it looked to have cut to the same depth as the etching). Maybe the BEAM lasercutter? We settled on using the power shears to cut it out.

People online seem to be getting lighter engravings, though, and I can't figure that out! I tried all sorts of different settings for the construction paper and all I get is the burnt look. The cardstock I know for sure is white through and through and it also gives me this brown color. ??? I need to figure out this mystery:

In other news, my surface mount soldering skills have vastly improved with a touch of patience. These all used the: tin one copper pad | tweezer solder the component onto that pad so that it's straight | solder the other pad on | reflow the first pad. This seemed tedious to me in the past, but it actually goes pretty quickly and helps me place my components correctly (darn lack of silkscreening) since I'll go through and tin one pad for all the components.

I also printed tiny-to-be-painted-and-turned-into-earring nyancat:

This is the 3d printer:

Meanwhile in 2.008 we thermoformed for the first time. Here's the thermoform machine:

turned out pretty well, almost none of the webbing we were afraid of.

The machine is super-straightforward to use. I'll write it down sometime.

then you use the punch/die to cut out the part you want

some of the injection molded parts. The metal shim actually really affects the shrinking of the part, so our ring and body parts didn't press-fit together (both used a 3% shrinkage estimate). To be fixed!

Oh, and my food-grade silicone arrived. $17 for a lb off of amazon.

And I shopbot'd a new foam positive. But the silicone negative mold turned out fuzzy :/ with bits of construction foam attached:

Make lots of sacrificial cake until all the foam is melted away? I'm not sure. This silicone will stand up to 400F while the foam melts pretty easily (eg at the hint of a heat gun).

My vending machine coils arrived off of ebay. They definitely look like something I could make by hand.

Thursday, November 3, 2011

There's something about watching the media lab lasercutter etch nyancat onto a strawberry poptart that is soul-deep satisfying. Surely now the quality of my education is irrefutably worth my tuition...

meta-nyan! not yet recursive...

The most difficult part of this turns out to be not eating the poptarts long enough to make it from the grocery store to the lasercutter. I've wanted to do this for weeks, ever since I saw the Louisville hackerspace posting: http://www.lvl1.org/2011/07/15/new-laser-cutter/, but never made it to the lasercutter until last night. (and I'd never even eaten poptarts before the first time I bought poptarts with the intention of lasercutting them, hah).

that's the original inspiration on the right

So, first things first, go to google images and find an image to trace (I used: http://thelemurblog.com/gallery/Nyan%20Cat.png). I tried to do my grid + draw lines method that I used with solidworks, now in GIMP, but ended up simply using select-by-color + bucket-fill-entire-selection.

this is the exact picture I used, if you want to laser-etch one of your own.

I wonder how it made its way there o.o?? I only posted it on facebook (this blog post was after the fact), so it's a little creepy.

FAQ
People wondering how they can make one of their own--http://fslaser.com/40w-deluxe-hobby-laser-engraver-and-cutter
$2.5 to 3k 40W CO2 laser may be your best bet for cheap. It comes with caveats, though, see: http://hackaday.com/2011/04/14/buying-a-laser-cutter-from-china/ and other reviews online.
Probably a better idea to instead look at the list of hacker/makerspaces and contact your nearest one to see if they have a lasercutter you can use. http://hackerspaces.org/wiki/List_of_Hacker_Spaces
Alternatively, cut a stencil out of aluminum foil and blowtorch-singe or stovetop-pan-singe the appropriate parts. Although this tends to make a melty mess and I'm not sure how you would cut a stencil for a shape like this -- I guess connect some areas with thin bridges like they do for stenciled letters.
Although probably not recommended depending on what else was cut in the lasercutter, the resultant poptart is safe to eat -- it's the equivalent of a poptart which was super-toasted in some areas (all the laser is doing is dumping heat into some parts of the poptart, it's not dumping radiation or anything).
In fact, I stuck it in my hall's free-food table and both nyantarts were gone in an hour. :)