Braitenberg Robot
Last updated
Last updated
The Braitenberg Robot is an autonomous, behavior-based robot that reacts to its environment using simple yet powerful control logic. Inspired by Braitenberg Vehicles, this project demonstrates sensor-driven behavior without the need for complex programming.
Light & Obstacle Sensors: The robot moves based on sensor input, simulating fear, aggression, or curiosity.
Real-time Motion Response: The robot adapts its speed and direction based on environmental stimuli.
Exploration of AI Concepts: Provides a hands-on approach to robot perception, reactive control, and behavior-based robotics.
code
from smd.red import *
import time
import sys
class Braitenberg:
""" Port and SMD ids are assigned. Modules connected to SMDs are printed.
The operation mode is set for velocity modes. Torque is enabled
"""
def __init__(self):
# SMD setup
self.port = "/dev/ttyUSB0"
self.m = Master(self.port)
self.m.attach(Red(0))
self.m.attach(Red(1))
print(self.m.scan_modules(0)) # Print the scanned modules for the first SMD ID.
print(self.m.scan_modules(1)) # Print the scanned modules for the second SMD ID.
# Motor setup
# SMD 0 is left motor
# SMD 1 is right motor
self.m.set_operation_mode(0, 2)
self.m.set_operation_mode(1, 2)
self.m.enable_torque(0, True)
self.m.enable_torque(1, True)
def map(self, value, fromLow, fromHigh, toLow, toHigh):
"""_summary_
Args:
value (_type_): value of light data from the light sensor
fromLow (_type_): minimum value of light coming from the light sensor
fromHigh (_type_): maximum value of light coming from the light sensor
toLow (_type_): minimum value of velocity
toHigh (_type_): maximum value of velocity
Returns:
_type_: Velocity value is returned in direct proportion to the incoming light value.
"""
return int((value - fromLow) * (toHigh - toLow) / (fromHigh - fromLow) + toLow)
def stop(self):
"""The motors are stopped by setting the Velocity value to 0.
"""
self.m.set_velocity(0, 0)
self.m.set_velocity(1, 0)
def get_light_values(self):
""" Incoming light data is assigned to right and left light variables
Returns:
_type_: right and left light values are returned
"""
left_light = self.m.get_light(0, 1)
right_light = self.m.get_light(0, 2)
return left_light, right_light
def fear(self):
""" The right motor is related to the light sensor on the right, the left motor is related to the light data
from the left sensor in direct proportion. Motor speeds increase in direct proportion to the light data, and
accordingly the robot tends to move away from the light.
"""
left_light, right_light = self.get_light_values()
if left_light is not None and right_light is not None:
# Adjust PWM values based on light intensity.
left_stim = self.map(left_light, 0, 2300, 40, 100)
right_stim = self.map(right_light, 0, 2300, 40, 100)
print(f"left_light {left_light}, righ_light {right_light} ")
self.m.set_velocity(0, -left_stim)
self.m.set_velocity(1, right_stim)
else:
self.stop() # Stop if any sensor is None.
def love(self):
"""
The right motor is related to the light sensor on the right, the left motor is inversely
related to the light data from the left sensor. Motor speeds increase inversely proportional to
the light data, and accordingly the robot tends to approach the light. However, as the robot
approaches the light source, its speed decreases.
"""
left_light, right_light = self.get_light_values()
if left_light is not None and right_light is not None:
# Adjust PWM values based on light intensity.
left_stim = self.map(left_light, 0, 1500, 100 ,0)
right_stim = self.map(right_light, 0, 1500, 100, 0)
if right_stim and left_stim > -1:
self.m.set_velocity(0, -left_stim)
self.m.set_velocity(1, right_stim)
else:
self.stop()
print(f"left_stim {left_stim}, right_stim {right_stim} ")
else:
self.stop() # Stop if any sensor is None.
def wander_around(self):
"""The right motor is inversely related to the light sensor on the left,
the left motor is inversely related to the light data from the right sensor.
Motor speeds increase inversely proportional to the light data, and accordingly
the robot tends to move away from the light. However, as the robot moves away from
the light source, its speed increases.
"""
left_light, right_light = self.get_light_values()
if left_light is not None and right_light is not None:
# Adjust PWM values based on light intensity.
left_stim = self.map(right_light, 0, 1500, 100, 0)
right_stim = self.map(left_light, 0, 1500, 100, 0)
self.m.set_velocity(0, right_stim)
self.m.set_velocity(1, -left_stim)
print(f"left pwm: {left_stim}, right pwm: {right_stim}")
else:
self.stop() # Stop if any sensor is None.
def agression(self):
""" The right motor is related to the light sensor on the right, the left motor is related to the light data
from the left sensor in direct proportion. Motor speeds increase in direct proportion to the light data, and
accordingly the robot tends to move away from the light.
"""
left_light, right_light = self.get_light_values()
if left_light is not None and right_light is not None:
left_stim = self.map(left_light, 0, 1500, 0, 100)
right_stim = self.map(right_light, 0, 1500, 0, 100)
self.m.set_velocity(0, -right_stim)
self.m.set_velocity(1, left_stim)
print(f"Left: {left_stim}, Right: {right_stim}")
else:
self.stop()
def run(self):
c = 0
while True:
button =self.m.get_button(0, 1)
if button == 1:
c += 1
time.sleep(0.4)
if c == 0:
print(0)
self.fear()
self.m.set_rgb(0, 1, red = 0, green = 0, blue = 255)
elif c == 1:
print(1)
self.love()
self.m.set_rgb(0, 1, red = 255, green = 0, blue = 255)
elif c == 2:
print(2)
self.agression()
self.m.set_rgb(0, 1, red = 255, green =0 , blue = 0)
elif c == 3:
print(3)
self.wander_around()
self.m.set_rgb(0, 1, red = 0, green = 255, blue = 0)
elif c > 2:
c = 0
if __name__ == "__main__":
try:
vehicle = Braitenberg()
vehicle.run()
except KeyboardInterrupt:
vehicle.stop()
sys.exit(0)