Friday, July 25, 2008

[electronics] PICs

Before I started programming computers, I was always trying to build some eletronic boards doing various stuff. Well, I am saying 'trying' because I never quite managed! Problem was, doing cool stuff quickly required a lot of equipement. Plus, contrary to the computer world, you have to go to a shop in person to buy the parts - and sometimes wait for weeks for them to ship in (no, there was no 'order-it-over-the-internet' at this time ... who said I am getting old?).

Anyway, I never dropped the idea of coming back to electronics one day. I mean, computers are great, but actually building a physical device really is something I find amazing.

Luckily, a new collegue of mine - David Grelaud - turns out to be an electronics guru :) He pointed me out a familly of microcontrollers - PIC's from Microchip - which are in fact fairly easy to use. These little things are programmed in assembler. Code is compiled on the PC and sent to the device through a programmer. You can order a relatively inexpensive programmer, which comes with a demo board, and get started! All tools to program are provided, as well as lessons to learn progressively the assembly language. The programmer is extremely simple to use: Just plug it to your computer USB!!

Ok, but how about the circuit board? Well, this is what is so cool about the PICs. They are extremely easy to use from a hardware point of view. I made a simple demo card with only a few wires, one resistor and a tricolor LED! I can program it from the PC, unplug the programmer and it becomes independent! Here is my - really ugly - card, plugged to the programmer:





You can also see how simple it is on this tutorial.
There are even C compilers for the PICs (C18, CCS, MikroC, PICC, SDCC). I am using SDCC which is open-source, but expect some not-so-easy install process. I had to hack a little bit because my PIC (18f4580) was not directly supported. But well, nothing too horrible if you are familiar with low level hacking (for those interested, use the 18f4585 template and setup the config bits directly in hexadecimal in the c code). The compiler produces a file containing the raw byte code (a .hex file) that is directly written into the PIC using the programmer. And voila, it just works !!

Amazing - now I am spending hours tweaking my rgb LED :-) (by quickly switching red/green/blue you can produce any color).

My head is just buzzing with cool projects to try on. I expect things will quickly complicate, but I know that the first step is always the most difficult.


Hey, did you know you can even create USB devices with a PIC ???!!?

Monday, June 9, 2008

[research] Lazy Solid Texture Synthesis

Our latest work on texture synthesis from example has been accpeted to EGSR 2008!! Check it out here: http://www-sop.inria.fr/reves/Basilic/2008/DLTD08/.

The key idea is to synthesize solid textures for surfaces, but only for the voxels around the object. Hence we never have to store or compute the entire volume. Our GPU implementation is very fast, so we can in fact synthesize a texture for the interior surfaces whenever an object is cut or broken. We obtain much more variety than a direct tiling of a 3D tile, which would create visible repetitions along some directions (and require storing a full volume tile). There is no pre-process of the object interior (ie. no thetrahedrons, no prior-knowledge of where the object will fracture) so our approach could be used for interactive breaking of large structures.

This is a collaboration with Yue Dong and Xin Tong (from MSRA). The project was very fun to work on, and I hope you'll find it interesting and - most importantly - useful.

I will add some more material on the paper webpage soon.

Saturday, May 17, 2008

[game] ragsmash

I just corrected the 'elastic band' problem in Ragdoll Smasher. This was due to a different handling of timers in XP and Vista.
This is now fixed!

Tuesday, May 13, 2008

[game] Ragsmash on YouTube

Someone posted a video of him playing Ragdoll Smasher on YouTube :-) watch it here

At least I know someone played the game ;-) It's really cool for a 'wannabe' game developer such as myself to discover this kind of things. Highly enjoyable.

A few explanations on the weird things happening that the player points out:
- guys stuck mid-air happen because the game only simulates 3 bodies. So it assumes others already landed. I could correct it ... but well, it's sort of fun.
- the 'elastic band' effect is more difficult. I have to look into it but it is likely due to instability in the stepping of the physics. Due to this, a very large force is produced. ODE is not able to propagate forces fast enough and one bone flies away while the rest of the body remains where it is.

Anyway, I am always happy to hear about ideas for a new version of Ragdoll smasher, so feel free to make suggestions. I had another stupid idea involving a chainsaw ... I played too much Doom I think ;-)

[C#] grabing pictures from a webcam

I have been looking for a simple way to grab a picture from a webcam, using C#. Well, it appears it is not so simple :-) The easiest solution I found is described here.

The code is simple, the only difficulty is to install the WIA sdk before (download from here, WIAAutSDK.zip). Don't forget to execute 'regsvr32 wiaaut.dll' in a command line, after copying the DLL in WINDOWS\system32.

WIA cannot be used for videos it seems. For this you'll need a DirectShow wrapper. (Note that the FTP is nicer in the sample code above).

And, if you just need to grab images and send them to an FTP, use Dorgem.

That was my 2 cents on webcams :-)

(I found Scott Hanselman's blog to be a great source of info on this)

Wednesday, April 9, 2008

[code] pure virtual function call

Calling a virtual method from a constructor is a very bad idea. Indeed, this can easily result in the 'pure virtual function call' runtime error. This is well explained here. Worse, if the method is not abstract it will call the method from the parent class, instead of calling the specialized one. It is worse because this will probably not directly trigger an error. Now, this 'issue' does in fact make sense: In the constructor of the parent class, the specialized class is not yet initialized. Hence, how could one of its method be called? Therefore, if you need to call a virtual method from a parent class constructor, you are probably facing a problem in your code design.

Nevertheless, I actually came accross an interesting variant of this issue, involving a thread. My parent class, A, is creating a thread in its constructor. The thread execution later involves calling a virtual method of A, which is of course meant to be implemented by inheritance.

The funny thing is that depending on 'how long it takes' before the thread calls the virtual method, we may - or may not - still be in the constructor of A. So, sometimes, this will produce the 'pure virtual call' error, sometimes not. Needless to say I was a bit puzzled the first time :-)
The solution was simply to implement a lock mechanism to make sure the thread calls the virtual method after the constructor executed.

Tuesday, April 8, 2008

[win32] Ascii, Unicode, Scan code, Virtual key

... and other keyboard nightmares :-)

I found two very useful functions in the Win32 API: ToUnicodeEx and ToAsciiEx. It converts from a virtual key code and a scan code to a unicode or ascii character. It can be used directly from a WM_KEYDOWN (or KEYUP) message.

Here is a quick code snippet:


uint scancode = (lParam >> 16) & 0xFF;
BYTE keyboardState[256];
GetKeyboardState(keyboardState);
char charvalue[2];
if (ToAsciiEx(wParam, scancode, keyboardState, (LPWORD)&charvalue[0], 0, GetKeyboardLayout(0)) == 1) {
  onKeyPressed(charvalue[0]);
}


Also check out this blog for more info.