DEV Community

Cover image for Vacuum Cleaner
Jorge Martin
Jorge Martin

Posted on • Updated on

Vacuum Cleaner

This is the blog of the P1-RM, where I programmed the behavior of a vacuum cleaner (Roomba).

Objective

The objective of this project was to program the behavior of a vacuum cleaner robot so that it could autonomously, reactively, and safely navigate the environment. The robot utilized sensors such as bumpers and a laser to detect obstacles and adjust its movements accordingly.

FSM

FSM Vacuum Cleaner
The functionality of the code resides in a finite state machine with 3 states:

  • Spiral: the robot starts by moving in a spiral until it reaches maximum speed or crashes, at which point it switches to turn.
  • Turn: the robot depending on the bumper hit, it turns left or right and a random time each time, when the time's up, it switches to forward.
  • Forward: the robot starts going forward, if the time's up it switches to spiral, while moving, if the robot hit something or the laser detects an obstacle in front of the robot, it switches to turn.

Coding

Libraries

  • This library provides functions to generate random numbers, select random elements from sequences, shuffle lists, and other functionalities related to randomness.
import random
Enter fullscreen mode Exit fullscreen mode
  • This library provides functions related to time management, such as getting the current time or measuring elapsed time.
import time
Enter fullscreen mode Exit fullscreen mode

Implementation

  • The most important function is the execute() function, as it will be running constantly. This function is also responsible for deciding which state to transition to, depending on the sensor readings.
vacuum = VacuumCleaner()

while True:
    vacuum.execute()
Enter fullscreen mode Exit fullscreen mode
  • I implement a random turn and forward movement, I achieved this by adjusting their durations. To do so, I incorporated a timer that measures the time elapsed since the movement started.
self.turn_start_time = time.time()
self.turn_duration = random.uniform(0.5, 1.25)

self.elapsed_turn_time = time.time() - self.turn_start_time
if self.elapsed_turn_time >= self.turn_duration:
   self.end_turn = True
Enter fullscreen mode Exit fullscreen mode

Difficulties

  • Random Movement: one of the main challenges was balancing the random time and distance factors. The robot needed to move in a seemingly unpredictable way while ensuring it covered most of the environment efficiently.

Logbook

  • Day 1 (16/09/24): I focused on implementing the skeleton of the finite state machine with the three states (Forward, Turn, Spiral).

  • Day 2 (17/09/24): I have implemented the turning function with random times and I also finished the spiral movement function.

  • Day 3 (18/09/24): Implementation of a timer in the forward function to cover a random distance each time. If it completes the time, it starts to perform the spiral; if it collides, it turns.

  • Day 4 (21/09/24): Program completed, implementation of the laser, and depending on which bumper it hits, it will turn one way or the other.

  • Day 5 (23/09/24): Final tests and minor adjustments to the speeds and times.

Functionality

Test of the robot starting in the samallest room [5min]


(In case the video doesn´t play try this link: https://youtu.be/vAxX1OhKjNM)

Cleaning Test [20min]


(In case the video doesn´t play try this link: https://youtu.be/dWYY3eG4c-U)

Cleaning Test [1h]


(In case the video doesn´t play try this link: https://youtu.be/VXfXEwkdKzQ)

Top comments (0)