0% found this document useful (0 votes)
3 views10 pages

Python For Robotics

The document provides an overview of using Python for robotics within the VS Code environment, emphasizing the importance of project organization through folders and the functionality of the Explorer, Editor, and Terminal panels. It details the process of sensor data logging, explaining the significance of logging for debugging and performance analysis, and includes example code for logging sensor data. Additionally, it introduces serial communication concepts and the use of the pyserial library for communication between Python and microcontrollers.

Uploaded by

sreee1746
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)
3 views10 pages

Python For Robotics

The document provides an overview of using Python for robotics within the VS Code environment, emphasizing the importance of project organization through folders and the functionality of the Explorer, Editor, and Terminal panels. It details the process of sensor data logging, explaining the significance of logging for debugging and performance analysis, and includes example code for logging sensor data. Additionally, it introduces serial communication concepts and the use of the pyserial library for communication between Python and microcontrollers.

Uploaded by

sreee1746
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

Python for robotics

VS Code
It is a workspace where you write, run, and manage code.
VS Code = Workshop
Python = Tool
Folder = Project table
When we clicked File → Open Folder → robotics_python, you told VS Code:
“This folder is my project.”
VS Code now:
Watches that folder
Shows its files on the left
Runs code inside that folder
Saves outputs (like .txt files) there
If you don’t use folders, everything becomes chaos.
—> Folder = project boundary

Explorer Panel (Left side) — WHAT it is


It shows
robotics_python
├── robot_basics.py
├── serial_read.py
├── serial_write.py
├── sensor_log.txt
└── [Link]
This is your project structure.

3️⃣ robot_basics.py — WHAT this file is

Python for robotics 1


.py means Python file
This is NOT running yet
It’s just text until you press Run
VS Code lets you:
Write code ; Save it ; Run it using Python ; See output

4️⃣ Run Button / Run Python File — WHAT happens


When you click Run Python File:
VS Code does this behind the scenes:
1. Finds Python ( python3 ); [Link] your file to Python
3. Runs it; [Link] output in the Terminal

VS Code → asks Python → Python executes → result shown


VS Code itself never runs your code.

5️⃣ Terminal (Bottom panel) — WHY it appears


That black area at the bottom is the Terminal.
It is:
Your computer’s command line
Integrated into VS Code for convenience

SUMMARY
Explorer → shows files
Editor → write code
Terminal → see output
Extensions → add powers

Python for robotics 2


STEP 3 — SENSOR DATA LOGGING
(PYTHON FOR ROBOTICS)
What STEP 3 is about (A → Z)
A. Purpose
Robots must store sensor data, not just print it.
This is called data logging.

B. Why logging is important in robotics


Debugging sensor errors
Performance analysis
Proof that the robot worked
Post-processing (graphs, reports)
Printing is temporary.
Logging is permanent.

C. Files used
Python file: robot_basics.py

Log file (auto-created): sensor_log.txt

Both exist inside:

Documents/robotics_python/

D. Complete Code (STEP 3)

Python for robotics 3


import time
import random

LOG_FILE ="sensor_log.txt"

print("Robot program started")

whileTrue:
sensor_value = [Link](20,50)
print("Sensor value:", sensor_value)

withopen(LOG_FILE,"a")as file:
[Link](f"Sensor value: {sensor_value}\n")

if sensor_value >35:
print("ACTION: Cooling system ON")
else:
print("ACTION: System normal")

[Link](2)

E. Code Explanation (SHORT & CLEAR)


import time
Used for delay control ( sleep ).

import random
Used to simulate a sensor.

LOG_FILE = "sensor_log.txt"
Stores the file name in one place (clean practice).

Python for robotics 4


while True:
Infinite loop → robot keeps running.

[Link](20, 50)
Generates fake sensor data
(later replaced by Arduino data).

open(LOG_FILE, "a")
"a" = append mode
Adds data without deleting old data

with open(...) as file:


Safely opens & closes the file automatically.

[Link](...)
Writes sensor value into the log file.
\n → moves to next line.

if sensor_value > 35
Decision logic (robot behavior).

[Link](2)
Controls sampling rate
(1 reading every 2 seconds).

F. What happens when you run it


1. Program starts
2. Sensor value generated
3. Value printed to terminal

Python for robotics 5


4. Value written to file
5. Action decided
6. Program waits
7. Loop repeats

G. Output locations (IMPORTANT)


Output Type Where it appears
print() Terminal
Logged data sensor_log.txt

H. Engineer’s mindset (VERY IMPORTANT)


Terminal output → temporary
Log file → permanent evidence
Real robots rely on logs, not screens.

I. Key robotics pattern learned


READ → DECIDE → LOG → WAIT → REPEAT

This is universal in robotics.

Functions are IMPORTANT in robotics.


To convert a messy robot program into a clean, readable, reusable one.
import time
import random
LOG_FILE = "sensor_log.txt"

Python for robotics 6


def read_sensor():
"""Simulates reading a sensor"""
return [Link](20, 50)
def log_data(value):
"""Logs sensor value to a file"""
with open(LOG_FILE, "a") as file:
[Link](f"Sensor value: {value}\n")
def decide_action(value):
"""Decides what action to take"""
if value > 35:
print("ACTION: Cooling system ON")
else:
print("ACTION: System normal")
print("Robot program started")
while True:
sensor_value = read_sensor()
print("Sensor value:", sensor_value)

log_data(sensor_value)
decide_action(sensor_value)

[Link](2)

SERIAL COMMUNICATION (CONCEPT ONLY)


First: what “serial communication” actually means
Serial communication =
data sent one value at a time, in sequence, over a channel.

NOTE ( ”general” ): (base) sreeparvathyanand@sreeparvathys-MacBook-Air ~


%

Python for robotics 7


Breakdown:
(base) → Conda environment (ignore for now, not a problem)
sreeparvathyanand → your username
MacBook-Air → your computer name
~ → home directory
% → READY FOR COMMANDS
👉 The % is the key symbol
This means: “Type terminal commands here.”
If you see % → type system commands

If you see >>> → type Python code

STEP 5 — SERIAL COMMUNICATION USING pyserial

Purpose
To enable Python ↔ microcontroller communication using serial ports.

What is Serial Communication


Data sent byte by byte
Line-based ( \n )
Used by Arduino, sensors, embedded systems

Why pyserial
Python cannot access serial ports by default.
pyserial provides:
Port access
Read / write functions

Python for robotics 8


Baud-rate control

Serial Port
Communication channel
Examples:
macOS/Linux: /dev/[Link]…

Windows: COM3

In simulation: loop://

Loopback ( loop:// )
Virtual serial port
Sent data is received back
Used for testing without hardware

Core Functions Used


Function Purpose
[Link]() Open real port
serial_for_url() Open virtual port
.write() Send data
.readline() Receive data
.encode() String → bytes
.decode() Bytes → string

Data Flow
SEND → SERIAL PORT → RECEIVE

Python for robotics 9


Same logic for real Arduino.

Key Rule
Serial communication works with bytes, not strings.

Mapping to Real Arduino


Simulation Real Arduino
loop:// /dev/[Link]

.write() [Link]()

.readline() Python reading Arduino

Python for robotics 10

You might also like