0% found this document useful (0 votes)
72 views12 pages

Kelompok 3 - Latihan 1 Setup Python Dan Aljabar Linier

This document summarizes an exercise on setting up Python and linear algebra. It contains 3 main sections: 1) Defining functions in Python and plotting a cosine-exponential function 2) Generating random numbers from normal and uniform distributions and plotting histograms 3) Using transformation matrices to plot laser scan data in the robot and global reference frames
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
72 views12 pages

Kelompok 3 - Latihan 1 Setup Python Dan Aljabar Linier

This document summarizes an exercise on setting up Python and linear algebra. It contains 3 main sections: 1) Defining functions in Python and plotting a cosine-exponential function 2) Generating random numbers from normal and uniform distributions and plotting histograms 3) Using transformation matrices to plot laser scan data in the robot and global reference frames
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 12

Latihan 1 : Setup Python dan Aljabar Linier

September 20, 2020

1 Sheet 0
• Kelompok 3 :
1. Muhammad Farhan Fathurrahman (23219344)
2. Adityas Regita P. (23220020)
3. Satrya Budi Pratama (23219305)
Link Question : http://ais.informatik.uni-freiburg.de/teaching/ss20/robotics/exercises/sheet00.pdf

1.1 1 : Defining Functions


[1]: import math

# function cos times exponential


def cos_exp(x):
return math.cos(x)*math.exp(x)

[2]: # invoking the function


print(cos_exp(5))

42.09920106253839

1.2 2 : Plotting Data


1.2.1 a. plot the graph of the cos_exp function

[3]: import matplotlib.pyplot as plt


import numpy as np

# make evenly spaced numbers over a specified interval


x_values = np.linspace(-2*np.pi, 2*np.pi)

# make result cos_exp function list for every x_values


y_values = [ cos_exp(x_values[i]) for i in range(len(x_values)) ]

[4]: # plot the values


plt.plot(x_values, y_values)

[4]: [<matplotlib.lines.Line2D at 0x2dd15b784c8>]

1
1.2.2 b. save plot to png

[5]: plt.plot(x_values, y_values)


plt.savefig("exercise2.png")
plt.show()

2
1.3 3 : Generating random numbers
we can find the random numpy function API on https://docs.scipy.org/doc/numpy-
1.14.0/reference/routines.random.html

1.3.1 a. normal distribution mean = 5, s_deviation = 2, size = 1000

[6]: # a. normal distribution mean = 5, s_deviation = 2, size = 1000


np.random.seed(1000)
normal_distribution = np.random.normal(loc = 5.0, scale = 2.0, size = 100000)

# show 5 first value


normal_distribution[:5]

[6]: array([3.39108339, 5.64186309, 4.94903424, 6.28864766, 4.39840665])

1.3.2 b. uniform distribution low = 0, high = 10

[7]: # b. uniform distribution low = 0, high = 10


np.random.seed(1000)
uniform_distribution = np.random.uniform(low = 0, high = 10, size=100000)

# show 5 first value


uniform_distribution[:5]

[7]: array([6.53589585, 1.15006943, 9.50282864, 4.82191401, 8.72474535])

1.3.3 c. compute meand and standar deviation of normal_distribution and unfiorm


distribution
[8]: # c. compute meand and standar deviation of normal_distribution and unfiorm␣
,→distribution

print("Normal Distribution : Mean : {} , Standart Deviation {}".format(np.


,→mean(normal_distribution),

np.
,→std(normal_distribution)))

print("Uniform Distribution : Mean : {} , Standart Deviation {}".format(np.


,→mean(uniform_distribution),

np.
,→std(uniform_distribution)))

Normal Distribution : Mean : 4.992941193695771 , Standart Deviation


2.006287767328325

3
Uniform Distribution : Mean : 5.006721764270011 , Standart Deviation
2.8892754249272854
The result of mean and standart deviation of normal distribution seems same with 5 and 2

1.3.4 d. plot histogram

[9]: # d. plot histogram


plt.hist(normal_distribution)

[9]: (array([9.4000e+01, 1.0450e+03, 6.3860e+03, 1.9843e+04, 3.1785e+04,


2.6405e+04, 1.1562e+04, 2.5530e+03, 3.1200e+02, 1.5000e+01]),
array([-2.89986803, -1.22921442, 0.4414392 , 2.11209281, 3.78274643,
5.45340005, 7.12405366, 8.79470728, 10.46536089, 12.13601451,
13.80666813]),
<a list of 10 Patch objects>)

[10]: plt.hist(uniform_distribution)

[10]: (array([ 9940., 10066., 9938., 10040., 9982., 10011., 9857., 10062.,
9990., 10114.]),
array([1.91223788e-05, 1.00001324e+00, 2.00000735e+00, 3.00000146e+00,
3.99999558e+00, 4.99998969e+00, 5.99998380e+00, 6.99997792e+00,
7.99997203e+00, 8.99996614e+00, 9.99996026e+00]),
<a list of 10 Patch objects>)

4
1.3.5 e. random value same as we run again
add seed(1000) method before calling the random method. source https://pynative.com/python-
random-seed/.

2 Sheet 1
Link Question : http://ais.informatik.uni-freiburg.de/teaching/ss20/robotics/exercises/sheet01.pdf

5
2.1 Exercise 1 : 2D Transformation as Affine Matrices

6
7
8
2.2 Exercise 2 : Sensing
2.2.1 Use Python to plot all laser end-points in the frame of reference of the laser
rangefinder.

[11]: # load data


scan_data = np.loadtxt('laserscan.dat')

# make orientation data within scan_data within spesific range


start_distance = - np.pi/2
last_distance = np.pi /2
angle_data = np.linspace(start_distance , last_distance, len(scan_data),␣
,→endpoint=True)

[12]: # get position of laser point on frame


x = scan_data * np.cos(angle_data);
y = scan_data * np.sin(angle_data);

[13]: # plot laser point


plt.plot(x, y , '.k', markersize=1)
plt.title('a) laser end-points in the frame of reference of the laser range␣
,→finder')

plt.xlabel('X (m)')
plt.ylabel('Y (m)')

# set same scale on both axes


plt.gca().set_aspect('equal')
plt.savefig('laser_scan.png')
plt.show()

9
2.2.2 The provided scan exhibits an unexpected property. Identify it an suggest an
explanation
From plot above, we can see some of the laser scan can penetrate the walls which should be
impossible. This can happen if the walls is a non-opaque walls such as glass.

2.2.3 Use homogeneous transformation matrices in Python to compute and plot the
centerof the robot, the center of the laser range finder, and all laser end-points
in worldcoordinates
[14]: # set robot location
x_robot = 1.0
y_robot = 0.5
theta_robot = np.pi / 4

# set laser position of robot


x_lidar = 0.2
y_lidar = 0.0
theta_lidar = np.pi

# set transformation matrix global robot


T_global_robot = np.array([
[np.cos(theta_robot), -np.sin(theta_robot), x_robot],
[np.sin(theta_robot), np.cos(theta_robot), y_robot],

10
[0, 0, 1]
])

# set transformation matrix global laser


T_robot_laser = np.array([
[np.cos(theta_lidar), -np.sin(theta_lidar), x_lidar],
[np.sin(theta_lidar), np.cos(theta_lidar), y_lidar],
[0, 0, 1]
])

# compute laser frame w.r.t the global frame


T_global_laser = T_global_robot.dot(T_robot_laser)

# apply transformation to scan points


w = np.ones(len(scan_data)) # make array of 1
scan_laser = np.array([x , y , w]) # join the matrix of x, y and orientation

# compute global scan data


scan_global = T_global_laser.dot(scan_laser)

# plot laser scan points


plt.figure()
plt.plot(scan_global[0, :], scan_global[1, :], '.k', markersize=1)
plt.title('c) global reference')
plt.xlabel('X (m)')
plt.ylabel('Y (m)')

#plot robot pose in blue


robot_pose, = plt.plot(T_global_robot[0, 2], T_global_robot[1, 2], '+b')

#plot laser pose in red


laser_pose, = plt.plot(T_global_laser[0, 2], T_global_laser[1, 2], '+r')

#set legend & same scale on both axes


plt.legend([robot_pose, laser_pose], ["Robot", "Laser"])
plt.gca().set_aspect('equal')
plt.savefig('global_coordinates.png')
plt.show()

11
12

You might also like