Skip to article frontmatterSkip to article content
Site not loading correctly?

This may be due to an incorrect BASE_URL configuration. See the MyST Documentation for reference.

Python

(3-4h)

Lists and tuples

Reading PDF

Concepts:

  • 7.1 Sequence (abstract data type)

    • list is mutable and tuple is immutable

  • 7.2 Introduction to lists

    • Syntax:

    • Repetition operator:

      [1, 'a', 42, 1, 'a', 42, 1, 'a', 42, 1, 'a', 42]
    • Iteration:

      1
      2
      3
      
    • Indexing

      1
      3
    • IndexError

      ---------------------------------------------------------------------------
      IndexError                                Traceback (most recent call last)
      Cell In[6], line 1
      ----> 1 numbers[4]
      
      IndexError: list index out of range
    • len

      3
    • Iteration by index

      👎 not Pythonic but possible:

      1
      a
      42
      

      👍 Pythonic

      0 1
      1 a
      2 42
      
    • Mutability

      [1, 'a', 42]
      [3.14, 'a', 42]
    • Concatenation

      [3.14, 'a', 42, 4, 5]
      [3.14, 'a', 42, 4, 5]
  • 7.3 list slicing

    • [3.14, 'a', 42, 4, 5]
      ['a', 42]
    • ['a', 42, 4, 5]
    • [3.14, 'a', 42]
    • [3.14, 'a', 42, 4]
    • [3.14, 42, 5]
  • 7.4 Finding items in lists using the in

    • True
  • 7.5 list methods and useful functions

    • append, count(item), index(item), insert(index, item), remove(), reverse()

      [3, 4, 7]
    • del, sum, min, max

      [3, 4]
  • 7.6 Copying lists

    • [3.14, 'a', 42, 4, 5]
      [3.14, 'a', 42, 4, 5]
      [None, 'a', 42, 4, 5]
      [None, 'a', 42, 4, 5]
    • [3.14, 'a', 42, 4, 5]
      [3.14, 'a', 42, 4, 5]
      [3.14, 'a', 42, 4, 5]
      [None, 'a', 42, 4, 5]
  • 7.7 Processing lists

    • for totaling the values, sum() better

    • averaging: statistics.mean()

    • Passing a list as an argument to a function

      • You can modify the list - it is not copied

    • Returning

    • Randomly selecting

      4
      [3.14, 3.14]
    • Working with lists and files

    • ['1. line\n', '2. line']
  • 7.8 List comprehensions

    • [3.14, 'a', 42, 4, 5]
      [3.14, 'a', 42, 4, 5]
    • [3.14, 'a', 42, 4, 5]
      [6.28, 'aa', 84, 8, 10]
    • Using if

      [3.14, 'a', 42, 4, 5]
      [84, 8, 10]
  • 7.9 two dimensional lists

    • [['App1', 'App2'], ['App3', 'App4']]
  • 7.10 Tuples

    • Why? (1) some Python features require it (2) a bit more efficient (not very important for you)

    • for defining constant sequences

    • (1, 'hi')
    • supports all list operations, but modifying ones

  • 7.11 Plotting list data with matplotlib

    • <Figure size 640x480 with 1 Axes>

More about Strings

Reading PDF

Concepts:

  • 8.1 Basic String operations

    • Many sequence operations apply

      H
      e
      l
      l
      o
      
    • len

    • +

    • immutable

  • 8.2 String slicing

    • same as sequences

  • 8.3 Testing, searching, and manipulating strings

    • in, not in

    • isalnum, isalpha, isdigit, ...

    • lower, strip ...

    • searching and replacing: find ...

      True
      'Gökçe Su'
    • ['Gökçe', 'Aydos']

URScript

(2h)

In your project, you will use UR robot arms. There are two ways to program a these:

  1. Using the PolyScope GUI on the pendant. You have tried this in first weeks.

  2. URScript

PolyScope provides a more visual programming experience and the PolyScope on the robot pendants In this unit we will introduce URScript.

URScript is the programming language for controlling robots manufactured by the company Universal Robots.

Example:

Code:

p1 = p[-.30, -.4, .1, d2r(180), 0, 0]
p2 = p[-.35, -.4, .1, d2r(180), 0, 0]
times = 0
while (times < 3):
  movel(p1)
  movel(p2)
  times = times + 1
end

Programmed robot:

Robot moves three times from the left point to the right and stops.

URScript’s syntax is similar to Python, but there are several differences, e.g.:

Nevertheless, many operations are also very similar.

URScript syntax

You can take a look at URScript’s fundamental elements or come back here later for a reference:

  1. numbers, variables, and types.

  2. control flow

  3. functions

UR Studio

In the class you will work with real robots. To warm up, we will use a simulator: The UR Studio. Let us run the example:

  1. Go to https://studio.universal-robots.com and login with your existing account that you created as part of RobotLab certification.

  2. Click Studio on the right bar:

  3. Change the Robot type to UR3e, and then click Apply.

    The simulator will be restarted and you will be back in the welcome screen.

  4. Now we will load our program. Click Program:

  5. Then click the ➕ icon:

  6. Click Script:

  7. Click Set Script:

  8. Copy Program 1 and paste it into the window, then click Save.

  9. Click Robot State, then Power On, and Unlock.

    Close the robot state window by clicking somewhere else in the window.

  10. Click 3D.

  11. Click the play symbol. You should see the robot moving. It will restart the program, due to Looping: Enabled in the program settings we had in a previous step.

Motion function movej

One of the motion commands that you will probably work often is movej. movej gets a list of six numbers as a parameter:

movej([0, d2r(-90), 0, d2r(-90), 0, 0])

[0, d2r(-90), 0, d2r(-90), 0, 0] is a list which is fed to the movej command. Each element in the list describes one joint position as shown in Figure 2.

Robot tool pose (titled tool position) and joint rotations (titled joint position) on PolyScope 5.

Figure 2:Robot tool pose (titled tool position) and joint rotations (titled joint position) on PolyScope 5.

On UR Studio, you can get joint data by clicking Move and then Joints. For example if all joint angles are zero, then you get the following robot pose:

Robot tool pose (titled tool position) and joint rotations (titled joint position) on PolyScope X and UR Studio.

Figure 3:Robot tool pose (titled tool position) and joint rotations (titled joint position) on PolyScope X and UR Studio.

In the movej command we used d2r(). This function converts degrees to radian. movej works with radians and 0° corresponds to 0 radian. Degrees are probably more intuitive for and do not involve decimal points, therefore I used the d2r() instead of writing the angles in radian.

  1. 0°: base

  2. -90°: shoulder

  3. 0°: elbow

  4. -90°: wrist 1

  5. 0°: wrist 2

  6. 0°: wrist 3

You will still see angles in PolyScope in radian (for example tool position in Figure 2), so you should know what radian is to make sense of the numbers.

radian

a unit of measuring angles (symbol: rad). Whole rotation is 2π2\pi rad and a right angle (90°) is π/2\pi/2 rad. Radian

URScript plugin for VSCodium

I recommend writing your URScript programs in VSCodium. Install ahernguo.urscript which provides syntax highlighting and formatting. The extension will be active, if you save your programs with .script extension, e.g., wave_hand.script.

Debugging URScript code in UR Studio

E: Waving robot arm

Solution to Exercise 1

Coordinate frames

The position that we provide, e.g., x = 1, y = -3, ... will be coordinates of a specific coordinate frame. So we should understand what a coordinate frame is.

coordinate frame
defines the origin and x, y, z directions.

Coordinate frames are typically drawn using 🟥, 🟩, and 🟦 colors, which correspond to the x, y, z axes, respectively. Like we write RGB (red green blue; and not BRG for example) are x, y, z axes in the same order.

Our robot has two predefined frames:

  1. Base

  2. Tool

Right hand rule for a coordinate frame showing the relations between different axes.acdx, Wikimedia Commons.

Figure 5:Right hand rule for a coordinate frame showing the relations between different axes.
acdx, Wikimedia Commons.

We will typically specify coordinates in Base coordinates. This coordinate frame is spanned by the 🟥, 🟩, and 🟦 dashed and dotted lines which intersect inside the base of the robot as shown in Figure 2 and Figure 3.

The Tool coordinate frame can be seen at the end of the head of the robot, where the tool is typically attached as shown in Figure 2. We see that the base coordinate frame is fixed (when Base is selected), but the pose of the tool coordinate frame changes with different joint positions.

The six joint positions lead to different poses of the tool coordinate frame and thus to different poses of the tool.

E: Finding a position in a coordinate frame

Solution to Exercise 2

Pose

An example pose shown in Tool position. The tool frame is rotated +90 degrees around the x-axis compared to the robot’s base frame.

Figure 6:An example pose shown in Tool position. The tool frame is rotated +90 degrees around the x-axis compared to the robot’s base frame.

Not only the position, but in which direction something is facing is also important. For example even your head has typically a fixed position relative to the chair you are sitting on, it still does matter in which direction you are looking or speaking. You can rotate your head. We introduce pose which integrates both position and rotation.

For example the tool pose in Figure 6 corresponds to the following pose. (In UR Studio, you find it right to the Joints tab.).

p1 = p[0, -.22315, .69395, 1.571, 0, 0]

Note the p in front of the bracket. This is a special list, which is reserved for a pose.

The fingers show the positive direction around an axis.
SVGguru Wikimedia Commons

Figure 7:The fingers show the positive direction around an axis.
SVGguru Wikimedia Commons

The first three elements make up the position. These are given in meters, so we get:

The remaining three the rotation in radian.

The direction is defined by the right hand rule in Figure 7.

E: Finding a feasible pose for the robot

Solution to Exercise 3

E: Pick and place imitation

Solution to Exercise 4

We meet in the classroom beside the Robotlab

See you 👋🙂