essay on programming languages, computer science, information techonlogies and all.

Thursday, June 26, 2014

SketchUp motion script uploaded on github

Here sketchup-motion.git is the git that contains the ruby script that can make animation in the SketchUp.

And here is a PCB inspection equipment animation.



Have you noticed that the cable duct is also moving around as the stage moves ? I had to spent some times to make this works with Ruby API. It is crude mimic but it should tells how the cable duct works.

Tuesday, April 15, 2014

Heartbeat of SketchUp animation pumping Ruby script

The heartbeat is a timer that ticks regularly. This tick runs Ruby script block, which increments position or angle of SketchUp's instance toward target. Refer below code snippet for a timer usage and block execution by the timer tick.


In the code, I said the heart beats at precise interval of 'period' and you can say that I should somehow replace the timer with atom clock to get that behaviour. Well, we may meet in the middle by measuring duration; We can't make consistent period but we can get elapsed time between consequent timer calls. Refer below code.



Below is code for object translation. 'entity.transform!' updates the position. This change will be shown by model.active_view.update - search up above code.

Updating position in the middle is easy as it is moving in constant speed. Difficulty lies at the boundaries - reminding me of studying differential equations, asking for great care on boundary condition at all times. In here I have to figure out where and when to stop. I am just checking the remaining distance to the target position in here.




This simple approach - constant speed - makes unrealistic movements at the boundaries. It starts and stops in infinite acceleration. Better simulation should involve smooth change at ends.

Reference : Animate Yo' Cheese

Tuesday, April 8, 2014

One step further with Sketchup and RubyAPI

Trying to impress audience, I have gone further on this simulation and created this glass handling robot interacting with AOI and cassette.



Apparent complex movement are all come down to either rotation or translation in a certain axis. And then these simple motions are chained to each other or set to run concurrently. In the end, it is all about designing class to easily create, link and launch motions.

Wednesday, March 26, 2014

Animation in SketchUp using RubyAPI

Having tried to animate objects using dynamic component in SketchUp, I feel that it isn't adequate to make a complicate movement. But before giving up, found that there is Ruby API which can allow to move around object at a timer call. As timer call will be periodically ticking which can increment position of object to desired position, it will definitely the right way to animate objects.

It takes time to be fluent on Ruby as it is a script language which has lots of unfamiliar concepts and syntactic sugar. But it is worth investment as I can make below animation



The outliner at the above video shows the hierarchy of the objects. For example, 'tractor_base' has 'tractor's and 'grip_base' which are moved along as the 'tractor_base' moves. Also note that 'glass' is rebased to 'tractor_base' when it is loaded on the stage and then rebased to 'bed' after it is released from the grip.

And the Ruby console shows internal debugging prints that must means nothing to outsider but invaluable information for the programmer.

I will describe what Ruby code is running behind from next blogs. But for those who can't wait, below shows what is driving in high level.



Saturday, February 1, 2014

Embedding Lua in C++

Lua is a script language. It is light weight and easily embedded in C. Below is a code snippet that embeds Lua and retrieves result back from the script. And the script used from above C++ code can be like below.

To further assist the embedding and interacting with C/C++, there is Luabind. You can make method and class binded to Lua script. And it also provides utility class that hide the details of stack.

Refer below code snippet to include Luabind in the C++ code. Lua syntax is quite simple and intuitive. It can be applied to a configuration file and can be a good alternative to the INI. Refer below. Each name value pair can be string, real number, boolean or table. A name represents a table if it contains another sets of name-value pair like variable 'b' at below.

It is not necessary to enclose configuration items with 'config={ }'. Any variable can be defined at the highest level - global level. But there are other global variables used in Lua - e.g. string, preload, package, os, setmetable and so on - which cause problem when we want to iterate through config items. If we choose to use a fixed name, then it will be straight forward on iteration. Just find the 'config' variable and iterate through all variables inside of it. It will be shown in code later.

With above code, you can simply access each variable presumed that you know the variable names and hierachy.

But what if you want to access configuration like SAX does ? There should be a way to iterate through configuration items. Lua can iterate through items in the table, and we can make the script calls you with predefined interface. Here is code snippet that can do it.

Monday, January 20, 2014

Automatic optical inspection equipment running in SketchUp by dynamic component

It will be great if you can see that the equipment you designed running even in a computer. You can find out lots of thing that can go wrong before hand. Actually, I guess that is the motto of the SketchUp. Design and see whether it works. To that extent, using component and ensuring edges of different component meet exactly is a big step forward. Next step can be seeing those components running around in times and see how each components interact with each other.

But adding another dimension - in here, it is time - is always a great challenge. Sketchup answers it with dynamic component. Here is an example that use dynamic component to run automatic optical inspection system. You can see that a glass coming and sitting on the stage and gripped by tractor and being scanned by cameras that are also moving to cover whole glass area.



To make this kind of simulation, you need to slice the whole time and put which component should move in what direction in each time slice. Refer below screen shot for this.

When user click the bed component, the "ANIMATECUSTOM( "t", 10, 0, 0, 0, 10 )" is executed which increments variable 't' from 0 to 10 in 10 seconds. Refer SketchUp OnClick Functions for detail. Of course, 't' is the time in unit of seconds.

Now we can move parts in various ways in this 10 seconds. In above screen, you can see that the red marks that 't' updates variable 'servox1' to 'servox5' and when time is 2 seconds to 4 seconds, the servox2 is incremented from 0 to 120 cm with 60cm/seconds. Then it is added up to servox and servox is fed into X position of 'tractor' which makes SketchUp draws the tractor component moves along the x-axis at that time slice.

Well, it works but you can see that it is awkward. How ugly it looks to see those variables. It will makes you crazy if you are asked to change any movement in this code. Just think about how much changes should be there if you decided to put 1 second slice after second 2. Maybe there is a better way to organize code. But still you have to write everything in a line. And also look at all those methods - even there is 'AND' method. Of course, there is OR method. Certainly this will make dynamic component in less favorable option.

Reference : Google SektchUp Workshop. Edited by Laurent Brixius, Appendix : Dynamic Components by Matt Chambers

Monday, January 13, 2014

Employ matrix operation in C++ using MTL4

Developing algorithm using matrix asks for number of tools. First a straight forward and proven tool is needed like Matlab. It is better to minimize unknowns when a new algorithm is tried. You have to be sure that the result is solely come from one source - mostly from maturing algorithm. And that is the reason why you sticks to a tool that you are familiar with.

After making up an algorithm with matrix, it is time to make it a working code. It is not desirable to stick to the algorithm development tool as it comes together with a big library and license. It is MTL4 's turn to play.

MTL stands for Matrix Template Library. It is C++ template based and make no dependencies on a library file nor dll. Hence it goes in a small foot print and can be deployed in any platform.

But as template library goes, the most of the work comes down to a successful compilation of the source code. There are number of things to note. First, download appropriate zip files and copies files to an appropriate folders. In Windows, you can download from here and copies whole usr folder and name it MTL4. Second there is conflict in variable names between MTL4 and WinDefs. Refer below code snippet to fix the compilation error in Windows.
There are number of warning that can be ignored - I know I am lazy on it and will pay the debt at someday but until then. The code to include the MTL4 will be like below.
Now time to port the algorithm code to C++ code using MTL4. Refer below code snippet. As above code shows, the C++ code can be very similar to a code in the Matlab thanks to operator overloading in MTL4. In here, very limited features of MTL4 is shown and you can find more in the MTL4.