Home
JAQForum Ver 20.06
Log In or Join  
Active Topics
Local Time 16:39 19 Apr 2024 Privacy Policy
Jump to

Notice. New forum software under development. It's going to miss a few functions and look a bit ugly for a while, but I'm working on it full time now as the old forum was too unstable. Couple days, all good. If you notice any issues, please contact me.

Forum Index : Microcontroller and PC projects : Maximite Nunchuck and 3D wireframe libraries

Author Message
ceptimus
Senior Member

Joined: 05/07/2019
Location: United Kingdom
Posts: 130
Posted: 05:28pm 20 Sep 2019
Copy link to clipboard 
Print this post

I made a simple library (attached below) for the Nunchuck controller.  I wrote it on a Colour Maximite but I guess it would work on a Monochrome Maximite and maybe Micromites with a little modification.

I uploaded a YouTube video demonstrating it: https://youtu.be/4QQE3H2hi2c

The Nunchuck is a game controller supplied with the Nintendo Wii.  It has a two axis joystick, a 3-axis accelerometer and a couple of buttons.  The interface is I2C and it works on 3.3 volts.  You can buy them new for about twelve dollars, and much less second hand.  You can cut off the custom Nintendo connector and make your own connections or buy a simple adapter on Ebay or elsewhere.  The adapters are just a circuit board with an edge connector that pushes inside the Wii connector and a row of four header pins for you to connect to.

The library only has a few functions:

NunchuckInit$() must be called to initialize the Nunchuck.  It returns either the string "Fail" if it can't talk to a Nunchuck or the type ID and description of the Nunchuck on success.

NunchuckJoystick(x,y) returns the value 0, 1, 2, or 3 indicating that the Z button (1) or C button (2) or both (3) are pressed - or 0 if neither button pressed.  The x and y joystick positions are returned (passed by reference) in the x and y variables, if you supply them.  The range is nominally -100 to +100 with centre zero.  The library reads and uses the 'calibration data' stored inside the Nunchuck, but that calibration data isn't guaranteed to be 100% accurate, so the endpoint values and centre value could be off by a few counts.

NunchuckAccelerometer(x,y,z) This is a Sub rather than a function, so no value is returned, but the current accelerations in the x, y, and z planes are returned (passed by reference) in the three parameters you supply.  The range is nominally +/-2500 with 1000 counts corresponding to 1g.

NunchuckAngles(roll, pitch, theta)This Sub converts the accelerometer data into roll and pitch angles (plus an optional third angle, described below) all passed by reference.  It's just using accelerometer data so if the Nunchuck is accelerating (e.g. shaken) the angles will be wrong.  The angles are in radians.

The theta angle is a strange one mainly for the benefit of the 3D library.  All the Nunchuck accelerometer really 'knows' is which way it's currently accelerating, so when it's static that is which way is 'up' relative to its orientation.  Say the Nunchuck is upside-down - did it get there from upright by rolling 180 degrees or pitching 180 degrees?  It doesn't know - it has no memory - so as far as the Nunchuck is concerned upside-down is a roll angle of 180 degrees AND a pitch angle of 180 degrees.  But that's no use to a 3D-rendering library: if you roll an object 180 degrees and then pitch it 180 degrees, it's back upright again (but yawed by 180 degrees).  Theta is a modified pitch angle that's always guaranteed to be in the range +/- 90 degrees (+/- Pi/2 in radians) - so if you use that for 3D rendering then the graphic will be correctly shown as upside-down when the Nunchuck is upside-down, and will assume it got there by rolling rather than pitching.

The 3D library was just a bit of fun to demonstrate the Nunchuck library.  It loads .OFF files and displays the geometry as a wire frame with back faces culled.  There are only two Subs you need to call:

Load3D(filename$,left,top,height,distance,paper,ink)  Only the filename need be supplied so you can just do Load3D("MYFILE.OFF") if you wish - the other parameters will all be defaulted to reasonable values.  If you supply the other parameters they specify the position and height of the display window, the ink and paper colours for drawing, and the viewing distance.

Show3D(roll,pitch) This renders the model using the supplied roll and pitch angles (in radians).  It rubs out the previous display as it goes to avoid too much flickering.  This could be much nicer if the Maximite allowed double-buffering somehow.

The viewing distance is used to apply perspective to the rendered wire frame.  Set it to a large value, e.g. 1000, to render the image isometrically, and to a smaller value, e.g. 2 or 5 for an 'up close' view.  The way it works is that the model is centred on x, y, and z axes around the central rotation point when it's loaded and the distance of the furthest vertex from the rotation point is computed.  Then the viewing distance is a multiplier for that vertex distance.  Obviously the distance can't be one or less, or the object would 'poke you in the eye' when rotated to some angle.

I did have the library working with .STL files at one time - which are more common. But the format is less efficient so bigger .STL models take ages to load and set-up for further processing in memory.  There are plenty of conversion programs that allow you to convert .STL to .OFF format, so I removed the .STL part to save memory.  There are a couple of extra models included in the .zip besides the Nunchuck model.  A (very) simple boxy glider, and a simplified form of the classic teapot, which really takes too long to render.  You can easily make your own models using the free OpenSCAD program - that's how I drew the simple Nunchuck and Boxplane models.

NUNCHUCK.zip
.
Edited 2019-09-21 03:37 by ceptimus
 
Bill.b

Senior Member

Joined: 25/06/2011
Location: Australia
Posts: 225
Posted: 11:08pm 20 Sep 2019
Copy link to clipboard 
Print this post

A nunchuck routine was developed by Nick Marentes back in 2013 for the colour max and used in the game Donut Delemma. http://www.nickmarentes.com/MaximiteProjects/index.html
Bill
Edited 2019-09-21 09:14 by Bill.b
In the interests of the environment, this post has been constructed entirely from recycled electrons.
 
Poppy

Guru

Joined: 25/07/2019
Location: Germany
Posts: 486
Posted: 03:46am 21 Sep 2019
Copy link to clipboard 
Print this post

Thanks Ceptimus, I will be glad to check it out!


Andre ... such a GURU?
 
Print this page


To reply to this topic, you need to log in.

© JAQ Software 2024