0% found this document useful (0 votes)
1 views

lab 3 ros done

This lab focuses on the fundamentals of Python programming for robotics, covering its syntax, execution methods, and installation, while comparing it to C++. Students will practice writing and executing Python scripts, learn about Python's applications in robotics, and explore various programming concepts such as variables, loops, functions, and file handling. Additionally, the lab includes tasks for installing Python on Ubuntu and handling serial ports.

Uploaded by

saqlainkharal39
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
1 views

lab 3 ros done

This lab focuses on the fundamentals of Python programming for robotics, covering its syntax, execution methods, and installation, while comparing it to C++. Students will practice writing and executing Python scripts, learn about Python's applications in robotics, and explore various programming concepts such as variables, loops, functions, and file handling. Additionally, the lab includes tasks for installing Python on Ubuntu and handling serial ports.

Uploaded by

saqlainkharal39
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 9

Lab 03: Fundamentals of Python for Robotics Programming

Student Name: Roll No:


Nimra Khursheed 21CSE39
Instructor signature: Date:

Objective

This lab introduces the fundamentals of Python programming, focusing on its role in robotics. Students
will learn Python’s syntax, execution methods, and installation process, comparing it with C++ in terms
of performance and ease of use. Additionally, they will practice writing and executing Python scripts.

Theory: Fundamentals of Python for Robotics

1. Introduction to Python

 Python is a general-purpose, high-level, object-oriented language known for its simplicity and
readability.
 It is an interpreted language, executing code line by line, making debugging easier.
 Created by Guido van Rossum, its first version was released in 1994.

2. Python vs. C++ in Robotics

 C++: Faster execution, efficient resource usage, preferred for high-performance applications.
 Python: Easier development, quick prototyping, and compatibility with libraries like OpenCV
and ROS.

3. Python in Robotics

 Commonly used for sensor data logging, serial communication, and UI development.
 Suitable for applications where computational efficiency is not the primary concern.
 Python Execution Methods
 Python Interpreter: Code is executed line by line in a shell.
 Python Scripts: Code is saved in a .py file and executed using an interpreter.

Lab Tasks:
Introduction to Python Interpreter:
 Python interpreter in the terminal
Installing Python on Ubuntu 20.04 LTS
 Python is preinstalled on Ubuntu, but the following command installs Python manually
 sudo apt-get install python python3

Verifying Python Installation:


 Location of Python interpreter

Location of Python:
Location of Python interpreter, sources, and documentation

Print a Hello World message in a Python interpreter shell


Compiling Code:
 Running Hello World in Python 3.8

Use the gedit editor


 The hello_world.py script
Running Python Code:
 Executing the hello_world.py script

 Running the nimra khursheed(21CSE39) output executable

Another way:
There is another way to execute the Python file. Use the following command.
 $ chmod a+x hello_world.py.
And after giving permission, you can simply execute the Python code using the following command.
 $ ./hello_world.py

Understanding Python Basics:


Python Variables
 Primitive variable handling in Python

 Handling lists in Python


 Handling tuples in Python

 Handling a dictionary in Python

Python Input and Conditional statement:


 Handling input and the conditional statement in Python

Output of the program:


Python: Loops:
 Usage of the while loop in Python

 Output of the while loop Python code

 Output of the program

 Python for loop code

 Output of python for loop code


Python: Functions:
Example Python code for function:
 Code snippet of public inheritance

 Output of Python function

Python: Handling Exception:


 Example Python try-except
 Output of Python function

Python: Classes:
 Python class example

 Output of Python class example

Python: Files:
 Python file I/O example
 Python file I/O output:

Python: Modules:
 Custom Python test module

 Python test module

Python: Handling Serial Ports:


 Installing PySerial in Ubuntu 20.04
$ dmesg

[ 113.016559] usb 1-5: Product: FT232R USB UART


[ 113.016561] usb 1-5: Manufacturer: FTDI
[ 113.016563] usb 1-5: SerialNumber: A900fDa9
[ 114.043095] usbcore: registered new interface driver usbserial
[ 114.043105] usbcore: registered new interface driver usbserial_generic
[ 114.043115] usbserial: USB Serial support registered for generic
[ 114.044997] usbcore: registered new interface driver ftdi_sio
[ 114.045009] usbserial: USB Serial support registered for FTDI USB Serial Device
[ 114.045050] ftdi_sio 1-5:1.0: FTDI USB Serial Device converter detected
[ 114.045077] usb 1-5: Detected FT232RL
[ 114.045219] usb 1-5: FTDI USB Serial Device converter now attached to ttyUSB0

 Python example code of writing to a serial port

You might also like