Home
Search
 
What's New
Index
Books
Links
Q & A
Newsletter
Banners
 
Feedback
Tip Jar
 
C# Helper...
 
XML RSS Feed
Follow VBHelper on Twitter
 
 
 
MSDN Visual Basic Community
 
 
 
 
 
TitleSimulate a robot arm with three rotating joints in Visual Basic .NET
DescriptionThis example shows how to simulate a robot arm with three rotating joints in Visual Basic .NET.
Keywordsrobot, robotics, arm, drawing, Visual Basic .NET, VB.NET
CategoriesGraphics, VB.NET
 

The basic idea is to consider the stages of the arm one after another and use transformations to move from one to the next.

Suppose the three arm segments have lengths L1, L2, and L3 and the angles of rotation at the three joints are A1, A2, and A3.

The first joint rotates the arm by angle A1 at a fixed shoulder. The second joint is translated distance L1 away in the direction of the first arm segment.

The second joint rotates the arm by angle A2 at the "elbow." The third joint is translated distance L2 away in the direction of the second arm segment.

The third joint rotates the arm by angle A3 at the "wrist." The end of the arm is translated distance L3 away in the direction of the third arm segment.


To draw the arm, start by drawing the shoulder at the origin. This example adds a translation first to center the shoulder in the drawing area.

Next the code applies the rotation A1 at the shoulder and draws a rectangle representing the first arm segment. There are is one tricky issue here.

The rotation is prepended to the previous translation so the rotation occurs before the translation that centers the shoulder. Imagine the arm in its own world coordinate space with the shoulder at the origin. The program draws a horizontal rectangle representing the first arm segment. The transformations then rotate it to the correct angle and translate it so the shoulder is centered. This puts the first arm segment in the correct position.

Next the code must draw the elbow joint. To do this, it prepends a translation of distance L1 in the X direction and 0 in the Y direction. It then draws the elbow joint at the origin.

Again imagine the arm in world coordinate space and suppose you draw the elbow at the origin. The new translation moves the joint to (L1, 0). The rotation of angle A1 rotates the joint to match the rotation of the first arm segment. Finally the shoulder's translation moves the whole thing so the base of the first arm segment is centered in the drawing area.

The code continues like this as many times as necessary to draw each arm segment and joint. In eachv step, it prepends the new translations and rotations.


The following code shows how the program draws the arm. The code is quite simple; it's understanding the code that's tricky.

The TrackBar controls tbarJoint1, tbarJoint2, and tbarJoint3 let you adjust the joint angles at run time.

 
Private Sub picCanvas_Paint(ByVal sender As Object, ByVal e _
    As System.Windows.Forms.PaintEventArgs) Handles _
    picCanvas.Paint
    Const ARM_LENGTH_1 As Integer = 75
    Const ARM_LENGTH_2 As Integer = 50
    Const ARM_LENGTH_3 As Integer = 50

    e.Graphics.Clear(Me.BackColor)

    Dim cx As Double = picCanvas.ClientSize.Width / 2
    Dim cy As Double = picCanvas.ClientSize.Height / 2

    ' Make sure we start from scratch.
    e.Graphics.ResetTransform()

    ' For each stage in the arm, draw and then *prepend* the
    ' new transformation to represent the next arm in the
    ' sequence.

    ' Translate to center of form.
    e.Graphics.TranslateTransform(cx, cy)

    ' Draw the shoulder centered at the origin.
    e.Graphics.DrawEllipse(Pens.Red, -4, -4, 9, 9)

    ' Rotate at the shoulder.
    ' (Negative to make the angle increase
    ' counter-clockwise).
    e.Graphics.RotateTransform(-tbarJoint1.Value, _
        Drawing2D.MatrixOrder.Prepend)

    ' Draw the first arm.
    e.Graphics.DrawRectangle(Pens.Blue, 0, -1, _
        ARM_LENGTH_1, 3)

    ' Translate to the end of the first arm.
    e.Graphics.TranslateTransform(ARM_LENGTH_1, 0, _
        Drawing2D.MatrixOrder.Prepend)

    ' Draw the elbow.
    e.Graphics.DrawEllipse(Pens.Red, -4, -4, 9, 9)

    ' Rotate at the elbow.
    e.Graphics.RotateTransform(-tbarJoint2.Value, _
        Drawing2D.MatrixOrder.Prepend)

    ' Draw the second arm.
    e.Graphics.DrawRectangle(Pens.Blue, 0, -1, _
        ARM_LENGTH_2, 3)

    ' Translate to the end of the second arm.
    e.Graphics.TranslateTransform(ARM_LENGTH_2, 0, _
        Drawing2D.MatrixOrder.Prepend)

    ' Draw the wrist.
    e.Graphics.DrawEllipse(Pens.Red, -4, -4, 9, 9)

    ' Rotate at the wrist.
    e.Graphics.RotateTransform(-tbarJoint3.Value, _
        Drawing2D.MatrixOrder.Prepend)

    ' Draw the third arm.
    e.Graphics.DrawRectangle(Pens.Blue, 0, -1, _
        ARM_LENGTH_3, 3)
End Sub
 
 
Copyright © 1997-2010 Rocky Mountain Computer Consulting, Inc.   All rights reserved.
  Updated