Acrome-SMD Docs
All Acrome ProductsReferencesBlogCase StudiesContact Us
  • ACROME SMD
  • Electronics
    • 🔴SMD Red
      • Troubleshooting Guide
    • 🔵SMD Blue
    • 🟢SMD Green
    • Gateway Modules
      • Arduino Gateway Module
      • USB Gateway Module
    • Electrical Motors
      • Brushed DC Motors (BDC)
      • Stepper DC Motors (SDC)
      • Brushless DC Motor (BLDC)
      • Linear Actuator with Feedback – 75 lbs
    • Add-on Modules
      • Ambient Light Sensor Module
      • Button Module
      • Buzzer Module
      • IMU Module
      • Joystick Module
      • Potentiometer Module
      • Reflectance Sensor Module
      • RGB LED Module
      • Servo Module
      • Ultrasonic Distance Sensor Module
  • SMD Kits
    • Starter Kit
      • Basic Brushed DC motor Applications
    • Education Kit
    • Motion Kit
      • Differential Robot Projects
  • Software
    • Libraries
      • Python Library
      • Arduino Library
      • Java Library
      • Matlab Library
    • SMD UI
    • SMD Blockly
      • Introducing Customized Blockly Blocks
  • SMD Applications
    • Basics
      • Blink
      • Action - Reaction
      • Autonomous Lighting
      • Smart Doorbell
      • Security System
      • Distance Buzzer Warning
      • Distance Auto Stop
      • Smart Light Control
    • Interactive
      • Automatic Trash Bin
      • Radar
      • Chrome Dino Game Player
      • Play Chrome Dino Game With Joystick
      • Snake Game With Joystick
      • Pan-Tilt with Joystick Module
      • Joystick Mouse Control
      • Rev Up the Engine
      • Motor Rotation Based on Turn Input Value
      • Basic Motor Speed Control Application
      • Basic Motor Control Application Using PWM Input
      • Basic Motor Position Control Application
      • Basic Motor Torque Control Application
      • Motor Rotation Based on Joystick Counting
    • Robotics
      • Mouse Cursor Tracker Motion Robot
      • Waypoint tracker robot
      • Braitenberg Robot
      • Line-Follower Robot
      • Object Tracking Robot
      • Teleoperation Robot
      • Obstacle Avoidance Robot
      • ESP32 Wireless Controlled Mobile Robot
  • AI
    • Groq Chatbot-Controlled Robot
  • Mechanics
    • Building Set
      • Plates
        • 2x2 Plate Package
        • 2x3 120° Plate Package
        • 3x3 Plate Package
        • 3x5 Plate Package
        • 3x9 Plate Package
        • 11x19 Plate
        • 9x19 Plate
        • 5x19 Plate
        • 3x19 Plate
        • 9x11 Plate
        • 5x13 Plate
      • Joints
        • 60° Joint Package
        • 90° Joint Package
        • 120° Joint Package
        • Slot Joint M2 Package
        • Slot Joint M3 Package
        • U Joint Package
      • Mounts
        • Add-on Mount Package
        • Motor L Mount Package
        • Pan-Tilt Package
      • Wheels
        • Ball Wheel Package
        • Caster Wheel Package
        • Wheel Package
      • Cables
        • Power Cable 10 cm Package
        • Power Cable 20 cm Package
        • Power Cable 35 cm Package
        • RJ-11 Cable 7.5 cm Package
        • RJ-11 Cable 20 cm Package
        • RJ-11 Cable 35 cm Package
        • RJ-45 Cable 7.5 cm Package
        • RJ-45 Cable 20 cm Package
        • RJ-45 Cable 35 cm Package
      • Fasteners
        • M2x5 Allen Hex Screw Package
        • M3x6 Allen Hex Screw Package
        • M3x8 Allen Hex Screw Package
        • M3 Hex Nut Package
  • Help
    • Manual
    • Shops
    • Reach Us
Powered by GitBook
On this page
  1. SMD Kits
  2. Motion Kit
  3. Differential Robot Projects

Braitenberg Robot

Last updated 1 month ago

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.

Key Features:

  • 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)
Discover More About the Braitenberg Robot