Change the bar lengths and press the space bar to see how it affects Klann's foot-path. Press left or right arrows to rotate the crank. If a bar's length makes it impossible to link 2 joints, the joint will be plotted in the lower left corner. To load Klann's linkage from his patent press "k" then press the space bar. Press "2" to see the bar map, "1" to see the joint map. Note: the 4 frame variables are the X,Y distances from the center of the crank in LEGO scale. For a description of how the algorithms are used in the code see https://www.diywalkers.com/linkage-simulation.html We used this code to optimize Klann's linkage for LEGO. Here's a video of a LEGO Klann walking around: https://www.youtube.com/watch?v=kswm_D9xvho
Mechanical linkages connect "bars" (Klann's blue lines) at "joints" that allow the bars to rotate. When the arrow keys are pressed, Klann's crank turns, which moves all the joints to make it walk. This code uses algebra and analytical geometry to calculate each joint's X and Y position, allowing Klann to be drawn with the pen. The joint positions depend on the bar's lengths, which are loaded into the "bar" variables when the green flag is pressed. To see a map of Klann's bar lengths press "2", for the joint map press "1". To make the code run faster, the X,Y values of each joint are first calculated for one full rotation of the crank, which are saved to each joint's list. Then, when Klann is moved the "rotation" variable is incremented and the joint values are retrieved from these lists. The main algorithm used to find the joint's X,Y values is called "Circle Intersection", which finds the two points where 2 circles intersect. There are 4 choices for choosing between the two points: "low Y", "high Y", "left X", and "right X". If you look at the joint you are trying to calculate and imagine the path it should take, that should help you to see which of the 4 choices will always give you the correct intersection point. An overview of mechanical walkers like Klann, TrotBot, and Strider can be found here: www.diywalkers.com/walker-abcs "Circle Intersection" is based on Paul Bourke's "Intersection of two circles" solution, where he used the Pythagorean theorem and some clever analytical geometry to find the 2 intersection points. His derivation is the 5th one down on this webpage: http://paulbourke.net/geometry/circlesphere/