Page 1 of 1

Code optimization

Posted: Sun Apr 15, 2007 5:34 pm
by cerbellum
Hi guys,

A couple of months ago I started working on the WiiM8 (http://www.ramdac.be - forum thread). I finally found some time to improve it and add more options like controlling color, scale, ... with the Wii-remote, another big improvement is the addition of value smoothing as the original version was very "twitchy".

At the moment I'm looking into the back-end of the module in Xcode and optimization of my python code in the module as I believe this could be achieved with much less code.

Here's a part of my PeriodicalEvent code

Code: Select all

x = txt[0] x = float(x) #x signal smoothing listx.append(x) if len(listx) == 4: del listx[0] x = (listx[0]+2*listx[1] + listx[2])/4 #visualize value module.setValue ('accX',0,x) #mode selection if mode == 'rotate': x -= x0 x = x/xdiv x = asin(x) x = degrees(x) module.setValue ('degX',0,x) modul8.setValue ('direct_layer_rotation_z',x,0) elif mode == 'colorize': x -= x0 x = x/xdiv x = asin(x) modul8.setValue ('direct_layer_color_additiveR',x,0) elif mode == 'scale': x -= x0 x = x/xdiv x = asin(x) modul8.setValue ('direct_layer_scale_x',x,0)
I think this part is actually quite OK, the matter is that I've copied this part 3 times, for the X, Y and Z values of the 3-axis accelerometers. I believe this could be achieved much easier using 'def' so I can define 1 function to use for the X, Y, Z values but this is where it usually goes wrong.

Any help on defining this function would be greatly appreciated.

Here's the full PeriodicalEvent code:

Code: Select all

count += elapsed if calc == 'done' : if count >= 0.04 : #25fps count = 0.0 resetvalues() x = txt[0] x = float(x) #x signal smoothing listx.append(x) if len(listx) == 4: del listx[0] x = (listx[0]+2*listx[1] + listx[2])/4 #visualize value module.setValue ('accX',0,x) #mode selection if mode == 'rotate': x -= x0 x = x/xdiv x = asin(x) x = degrees(x) module.setValue ('degX',0,x) modul8.setValue ('direct_layer_rotation_z',x,0) elif mode == 'colorize': x -= x0 x = x/xdiv x = asin(x) modul8.setValue ('direct_layer_color_additiveR',x,0) elif mode == 'scale': x -= x0 x = x/xdiv x = asin(x) modul8.setValue ('direct_layer_scale_x',x,0) y = txt[1] y = float(y) #y signal smoothing listy.append(y) if len(listy) == 4: del listy[0] y = (listy[0]+2*listy[1] + listy[2])/4 #visualize value module.setValue ('accY',0,y) #mode selection if mode == 'rotate': y -= y0 y = y/ydiv y = asin(y) y = degrees(y) module.setValue ('degY',0,y) modul8.setValue ('direct_layer_rotation_x',y,0) elif mode == 'colorize': y -= y0 y = y/ydiv y = asin(y) modul8.setValue ('direct_layer_color_additiveG',y,0) elif mode == 'scale': y -= y0 y = y/ydiv y = asin(y) modul8.setValue ('direct_layer_scale_y',y,0) z = txt[2] z = float(z) #z signal smoothing listz.append(z) if len(listz) == 4: del listz[0] z = (listz[0]+2*listz[1] + listz[2])/4 #visualize value module.setValue ('accZ',0,z) #mode selection if mode == 'rotate': z -= z0 z = z/zdiv z = asin(z) z = degrees(z) module.setValue ('degZ',0,z) modul8.setValue ('direct_layer_rotation_z',z,0) elif mode == 'colorize': z -= z0 z = z/zdiv z = asin(z) modul8.setValue ('direct_layer_color_additiveB',z,0) elif mode == 'scale': z -= z0 z = z/zdiv z = asin(z) modul8.setValue ('direct_layer_scale_z',z,0)
Thanks in advance,

Kevin

Posted: Mon Apr 16, 2007 7:34 pm
by Lupin
tu peux utiliser un script qui va evaluer x y et z au sein d'une fonction: voici le principe de base

Code: Select all

# considérons 3 modules de barre , 'barx', 'bary', 'barz' avec comme valeurs assignables un float allant de 0.0 à 5.0 x0 = 5 y0 = 3 z0 = 1.5 paramlist=['x','y','z'] # listes des paramètres à optimiser #definition de la fonction qui évalue les objets selon la liste de paramètres def test (): for i in paramlist: tempval= eval(i+'0') # évaluation de respectivement x0,y0 et z0 moduletemp = 'bar'+i # création d'un pointeur vers un module module.setValue(moduletemp,0,tempval) #application des valeurs dynamiques test() #exécution de la fonction

Posted: Mon Apr 16, 2007 10:22 pm
by The Midi Thief
Did that solve it for you? If not, I'd take a stab at it. Are you going to post that module soon? I'd love to try it out soon.

Posted: Tue Apr 17, 2007 4:04 pm
by cerbellum
The above sollution didn't solve it but after f*cking around with it for some hours I got finally got it :).

Code: Select all

def wiictrl(b, c, a0, adiv, accvis = '', degvis = '', rot = '', col = '', sca = ''): a = txt[b] a = float(a) #x signal smoothing c.append(a) if len(c) == 4: del c[0] a = (c[0]+2*c[1] + c[2])/4 #visualize value module.setValue (accvis,0,a) #mode selection if mode == 'rotate': a -= a0 a = a/adiv a = asin(a) a = degrees(a) print a module.setValue (degvis,0,a) modul8.setValue (rot,a,0) elif mode == 'colorize': a -= a0 a = a/adiv modul8.setValue (col,a,0) elif mode == 'scale': a -= a0 a = a/adiv a = asin(a) modul8.setValue (sca,a,0)
and the triggering lines for x, y and z:

Code: Select all

wiictrl(0,listx,x0,xdiv,accvis = 'accX', degvis = 'degX', rot = 'direct_layer_rotation_z', col = 'direct_layer_color_additiveR', sca = 'direct_layer_scale_x') wiictrl(1,listy,y0,ydiv,accvis = 'accY', degvis = 'degY', rot = 'direct_layer_rotation_y', col = 'direct_layer_color_additiveG', sca = 'direct_layer_scale_y') wiictrl(2,listz,z0,zdiv,accvis = 'accZ', degvis = 'degZ', rot = 'direct_layer_rotation_x', col = 'direct_layer_color_additiveB', sca = 'direct_layer_scale_z')
Way cleaner I'd say :)

Thanks for the input guys!

I'll be uploading the new module to the project blog (http://www.ramdac.be) when ready, I'll keep you guys updated!

Posted: Tue Apr 17, 2007 4:50 pm
by Lupin
oh yeah taht's smart i totally forgot that we cold add expresisons inside arguements :)

i eager to test your module.
hehe i already have a wiimote and tried with wii to midi but the signal is so jerky. i saw you used a mean over 4 values to smooth it, did you think about adding a tweaking pot to change the smoothing amount?

Posted: Tue Apr 17, 2007 9:31 pm
by cerbellum
Posted the updated version on my blog, all necessary files included, be sure to check te readme file ;)

right after the jump!

Feel free to comment on it on my project blog and have fun with it!

Posted: Thu Apr 19, 2007 11:00 am
by The Midi Thief
Nice Work! I fiddled around with it for a short while and after a while I seemed to get most things working propperly. I never got the color option to work however. Now off to work, no time for play.

WII MODULE ELUSIVE! :(

Posted: Mon Jun 02, 2008 9:07 pm
by G4jima
Hey guys,

do you think this module is gonna be shared for us non coding lunatics.
Only im driving myself up the wall checking ramdac and attempting to email a pop3 account that gets returned nonetheless.

I need this module should I be able to compile it using just this script for I have no idea unfortunately, I am at your mercy.

Ramdac wwhats happenin dude?