Linux is an open-source operating system, created by Linus Torvalds in 1991, and is used on everything from personal computers to servers. Unlike DOS, which is a single-tasking, text-based operating system, Linux supports multitasking and multiuser operations. It offers both a graphical interface and a powerful command-line interface.
For this robotics project, we will use Docker to set up our Linux environment. Docker is a platform that allows you to automate the deployment of applications inside lightweight, portable containers. These containers package everything you need to run the software, including the operating system, libraries, and dependencies, ensuring consistent performance across different environments.
- Consistency: Docker ensures that the software runs the same way, regardless of where it’s deployed.
- Isolation: Each Docker container is isolated, preventing conflicts between different software environments.
- Portability: Docker containers can be easily shared and run on any system with Docker installed, making it ideal for collaboration and deployment.
Important
Ensure that Virtualtization is enabled in BIOS for Your system.
Important
Ensure that WSL is Enabled in your Windows.
- Download Docker Desktop: Go to the Docker Desktop for Windows download page.
- Install Docker Desktop: Run the installer and follow the on-screen instructions.
- Start Docker Desktop: After installation, Docker will start automatically. You can access it from the Start menu.
- Verify Installation: Open PowerShell and run the command:
docker run hello-world
-
Update Your System: Open your terminal and run the following commands:
sudo apt update sudo apt upgrade
-
Install Docker: Go to the Docker Desktop for Linux and follow the isntructions
-
Verify Installation: Open PowerShell and run the command:
docker run hello-world
- Download Docker Desktop: Visit the Docker Desktop for Mac download page.
- Install Docker Desktop: Open the downloaded .dmg file and drag the Docker icon to your Applications folder.
- Start Docker: Launch Docker from your Applications folder. Docker will start running in the background.
- Verify Installation: Open PowerShell and run the command:
docker run hello-world
Clone the repository on your local machine using the command:
git clone https://github.com/Robotics-Society-PEC/Getting-Started-With-Robotics.git
Important
Ensure that you run this command from within the repository.
This repo provides a Docker image with ubuntu installed, To build the image run the command:
docker build -t <name_of_image> .
After the image is done building, you can enter the Linux enviornment using the command:
docker run -it <name_of_image>
Understanding how to navigate the filesystem is fundamental when working with Linux.
The pwd
command shows the full path of the current directory you are in.
pwd
Example: If you are in the /home/user/documents directory
, running pwd will output:
/home/user/documents
The ls
command lists all files and directories in the current directory.
ls
Example: To list files with details (like file size and permissions):
ls -l
The cd
command changes the current directory to a different one.
cd /path/to/directory
Example: To move to the home directory:
cd ~
These commands help you create, move, and manage files and directories.
The touch
command creates an empty file.
touch filename
Example: To create a file named example.txt:
touch example.txt
The mkdir
command creates a new directory
mkdir directory_name
Example: To create a directory named projects:
mkdir project
The cp
command copies files or directories from one location to another.
cp source_file destination
Example: To copy file.txt to another directory:
cp file.txt /home/user/documents
The mv command moves or renames files or directories.
mv old_name new_name
Example: To move file.txt to another directory:
mv file.txt /home/user/documents
Or to rename file.txt
to newfile.txt
:
The rm
command removes files or directories.
rm filename
Example: To remove a file named file.txt:
rm file.txt
To remove a directory and its contents:
rm -r directory_name
When working inside a Docker container, you might create or modify files within the container's filesystem. However, these changes are isolated from your local machine, which means:
- Files created or modified inside the container will not automatically appear in your local filesystem.
- Git won't be able to track changes made inside the container since they aren't visible in your local directory.
To solve this, we can link a directory inside the Docker container to a directory on your local machine. This way, any changes made in the container will be reflected on your local machine, and vice versa.
Docker Compose is a tool that allows you to define and manage multi-container Docker applications. With Docker Compose, you can define all your services, networks, and volumes in a single YAML file. This simplifies the process of managing complex applications by allowing you to spin up an entire environment with a single command.
Let's set up Docker Compose to link your local volume with the Docker container.
Create a new file named docker-compose.yml
in your project directory. This file will define the Docker services and link the volumes.
services:
robotics-env:
build: .
volumes:
- ./workspace:/home/myuser/workspace
stdin_open: true # Equivalent to `-i` in `docker run`
tty: true # Equivalent to `-t` in `docker run`
command: /bin/bash
stdin_open: true
: This is equivalent to using the-i
flag withdocker run
. It keeps the standard input (stdin) open even if not attached.tty: true
: This is equivalent to using the-t
flag withdocker run
. It allocates a pseudo-TTY, which makes the terminal session interactive.command: /bin/bash
: Ensures that when the container starts, it opens a bash shell, allowing you to interact with the container.
To start the container in interactive mode, simply use:
docker-compose up
This command will start your container as specified in the docker-compose.yml
file, attaching the terminal to it, so you can interact with the shell inside the container.
Once the container is running, you should automatically be placed inside the container's bash shell, where you can run commands interactively.
If you later want to run the container in detached mode (not interactively), you can add the -d
flag to the docker-compose up
command:
docker-compose up -d
This runs the container in the background, freeing up your terminal for other tasks.
Let's test this by creating a file inside the container and ensuring it's visible on your local machine.
- Inside the Docker Contianer:
touch /home/myuser/workspace/newfile.txt
- On your local machine: Check your
./workspace
directory, and you should seenewfile.txt
there.
This synchronization ensures that all your work inside the Docker environment is consistent with your local development setup, making it easier to manage code and collaborate.
To work with C programs in a Linux environment, you'll need to know how to compile and run your code using the command line. Here's how you can do it:
To compile the C program, you can use the gcc compiler, which is widely used in Linux environments. The general syntax for compiling a C program is:
gcc -o output_name source_file.c
For our example, compile the main.c file like this:
gcc -o hello hello.c
gcc
: The GNU C Compiler.-o hello
: This specifies the output file name. In this case, the compiled program will be namedhello
.hello.c
: The source file you want to compile.
Once your program is compiled successfully, you can run it using the following command:
./hello
This will execute the hello
program and output:
Hello, World!
- Hello, World!
- Variables and Data Type
- If Else Statements
- While Loop
- For Loop
- Functions
- Arrays
- Structures
- Robot Simulation
Create a simulation for a robot that can move in a 2-dimensional space. The robot should have the ability to move both horizontally (x-axis) and vertically (y-axis). After simulating the movement, calculate and print the total displacement of the robot from its starting position.
- Define a
struct
for the Robot:
- The
Robot
struct should include:name
: The name of the robot.x_position
: The position of the robot on the x-axis.y_position
: The position of the robot on the y-axis.x_speed
: The speed of the robot along the x-axis.y_speed
: The speed of the robot along the y-axis.
-
Create a function to update the robot's position based on its speed and the time interval.
-
Create a function to print the robot's current position.
-
Simulate the robot's movement over a series of time intervals (e.g., 10 intervals).
-
Calculate the total displacement of the robot from its starting position. The displacement is the magnitude of the vector from the starting position
(0, 0)
to the final position(x_position, y_position)
and can be calculated using the formula:
- Print the robot's position at each time interval and the total displacement at the end.
Example Output:
- Time Interval 1: Position (x, y) = (1, 2)
- Time Interval 2: Position (x, y) = (2, 4)
- ...
- Total Displacement: 10.77 units
Allow the robot to change its speed randomly at each time interval and observe how this affects its final displacement.
This project is GPL-3 licensed.