Simple yet powerful Python code for useful robot applications
This code provides a foundation for a robot that can perform basic tasks like movement, sensing, and simple decision making.
import time
import RPi.GPIO as GPIO # For Raspberry Pi
class SimpleRobot:
def __init__(self):
# Initialize motor pins (example for 2-wheel robot)
self.left_motor_pin = 17
self.right_motor_pin = 18
# Setup GPIO
GPIO.setmode(GPIO.BCM)
GPIO.setup(self.left_motor_pin, GPIO.OUT)
GPIO.setup(self.right_motor_pin, GPIO.OUT)
# Create PWM instances for speed control
self.left_pwm = GPIO.PWM(self.left_motor_pin, 100) # 100Hz frequency
self.right_pwm = GPIO.PWM(self.right_motor_pin, 100)
self.left_pwm.start(0)
self.right_pwm.start(0)
def move_forward(self, speed=50, duration=1):
"""Move robot forward at specified speed for duration seconds"""
self.left_pwm.ChangeDutyCycle(speed)
self.right_pwm.ChangeDutyCycle(speed)
time.sleep(duration)
self.stop()
def turn_left(self, speed=50, duration=0.5):
"""Turn robot left"""
self.left_pwm.ChangeDutyCycle(0)
self.right_pwm.ChangeDutyCycle(speed)
time.sleep(duration)
self.stop()
def turn_right(self, speed=50, duration=0.5):
"""Turn robot right"""
self.left_pwm.ChangeDutyCycle(speed)
self.right_pwm.ChangeDutyCycle(0)
time.sleep(duration)
self.stop()
def stop(self):
"""Stop all motors"""
self.left_pwm.ChangeDutyCycle(0)
self.right_pwm.ChangeDutyCycle(0)
def cleanup(self):
"""Clean up GPIO resources"""
self.stop()
self.left_pwm.stop()
self.right_pwm.stop()
GPIO.cleanup()
# Example usage
if __name__ == "__main__":
robot = SimpleRobot()
try:
print("Moving forward for 2 seconds...")
robot.move_forward(duration=2)
print("Turning left...")
robot.turn_left()
print("Moving forward again...")
robot.move_forward(duration=1.5)
finally:
robot.cleanup()
This enhanced version adds cleaning functionality with sensors for obstacle avoidance.
import time
import RPi.GPIO as GPIO
from random import randint
class CleaningRobot:
def __init__(self):
# Motor setup (same as basic robot)
self.left_motor_pin = 17
self.right_motor_pin = 18
self.cleaning_motor_pin = 22
# Sensor pins
self.ultrasonic_trigger = 23
self.ultrasonic_echo = 24
self.left_bumper = 5
self.right_bumper = 6
# Setup GPIO
GPIO.setmode(GPIO.BCM)
GPIO.setup([self.left_motor_pin, self.right_motor_pin,
self.cleaning_motor_pin, self.ultrasonic_trigger], GPIO.OUT)
GPIO.setup([self.ultrasonic_echo, self.left_bumper,
self.right_bumper], GPIO.IN)
# Motor PWMs
self.left_pwm = GPIO.PWM(self.left_motor_pin, 100)
self.right_pwm = GPIO.PWM(self.right_motor_pin, 100)
self.clean_pwm = GPIO.PWM(self.cleaning_motor_pin, 100)
self.left_pwm.start(0)
self.right_pwm.start(0)
self.clean_pwm.start(0)
def get_distance(self):
"""Measure distance using ultrasonic sensor"""
GPIO.output(self.ultrasonic_trigger, True)
time.sleep(0.00001)
GPIO.output(self.ultrasonic_trigger, False)
start_time = time.time()
stop_time = time.time()
while GPIO.input(self.ultrasonic_echo) == 0:
start_time = time.time()
while GPIO.input(self.ultrasonic_echo) == 1:
stop_time = time.time()
time_elapsed = stop_time - start_time
distance = (time_elapsed * 34300) / 2 # Speed of sound in cm/s
return distance
def start_cleaning(self, speed=30):
"""Start cleaning brushes"""
self.clean_pwm.ChangeDutyCycle(speed)
def stop_cleaning(self):
"""Stop cleaning brushes"""
self.clean_pwm.ChangeDutyCycle(0)
def auto_clean(self, duration=60):
"""Autonomous cleaning mode"""
start_time = time.time()
self.start_cleaning()
try:
while time.time() - start_time < duration:
dist = self.get_distance()
if dist < 20 or GPIO.input(self.left_bumper) or GPIO.input(self.right_bumper):
# Obstacle detected - turn randomly
self.stop()
turn_direction = randint(0, 1) # 0 for left, 1 for right
turn_time = randint(1, 3) / 2 # 0.5 to 1.5 seconds
if turn_direction == 0:
self.turn_left(duration=turn_time)
else:
self.turn_right(duration=turn_time)
else:
# Move forward
self.move_forward(speed=40)
time.sleep(0.1) # Small delay between checks
finally:
self.stop()
self.stop_cleaning()
# Include all movement methods from SimpleRobot class here
# (move_forward, turn_left, turn_right, stop, cleanup)
# Example usage
if __name__ == "__main__":
robot = CleaningRobot()
try:
print("Starting 1-minute cleaning cycle...")
robot.auto_clean(duration=60)
finally:
robot.cleanup()
Add GPS or SLAM algorithms for precise location tracking and path planning.
Integrate OpenCV for object recognition, face detection, or QR code reading.
Add WiFi/Bluetooth for remote control via smartphone or web interface.