Acrome-SMD Docs
All Acrome ProductsReferencesBlogCase StudiesContact Us
  • ACROME SMD
  • Electronics
    • 🔴SMD Red
      • Coding Guide
      • Robot with Raspberry Pi Setup Guide
      • Robot with Arduino Setup Guide
      • 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
    • 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
      • What You Can Build
    • Education Kit
      • What You Can Build
    • Motion Kit
      • What You Can Build
  • 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
      • Differential Robot Projects
      • Mouse Cursor Tracker Motion Robot
      • Waypoint tracker robot
      • Braitenberg Robot
      • Line-Follower Robot
      • Teleoperation Robot
      • Obstacle Avoidance Robot
      • ESP32 Wireless Controlled Mobile Robot
  • AI
    • Object Tracking Robot
    • Groq Chatbot-Controlled Robot
  • ROS
    • Teleoperation Robot with ROS
  • 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
  • Step 1: Hardware & Software Overview
  • Step 2: Assemble
  • Step 3: Run & Test
  • Codes
  1. SMD Applications
  2. Robotics

Mouse Cursor Tracker Motion Robot

PreviousDifferential Robot ProjectsNextWaypoint tracker robot

Last updated 1 month ago

The Mouse Cursor Tracker Motion Robot is a project designed to control a robot’s movement by tracking the position of a mouse cursor in real-time. It utilizes Python with PyQt5 for the graphical interface and serial communication to send movement commands to the robot. The system allows the robot to move forward, backward, left, and right based on cursor position while also visualizing its movement path. A reverse mode enables inverted motion control for more flexibility. The project integrates an SMD Red Module for motor control, ensuring smooth and precise movement. By combining real-time tracking with serial communication, this project can be applied to robotics simulations, interactive motion control, and remote navigation systems.

About Tools and Materials:

2x ()

()

()

2x ()

Step 1: Hardware & Software Overview

Project Key Components

  1. The SMD acts as a bridge between the script and the modules. It is responsible for interpreting the commands sent by the script and translating them into actions that read input from the and meanwhile, actuate the motor for the continuous reading of the script.

  2. The 100 RPM BDC Motor with Encoder is used to rotate the radar mechanism in a full circle. The user can precisely control the motor and get the position through the built-in encoder.

Key Features

  • Robot Control: The robot's movement is controlled through a graphical interface using mouse clicks. The robot's position and orientation are updated based on user input.

  • Motor Control: Two motors (left and right) drive the robot's forward, backward, and rotational movements.

  • Path Tracking: The path followed by the robot during movement is displayed on the screen. Users can clear the path if needed.

  • Target Movement: The robot moves towards a target point specified by the user with the mouse.

Step 2: Assemble

Getting Started

  1. Hardware Setup

  • USB Connection: The USB_Port() function detects a suitable USB device connected to the platform and establishes a connection.

  • Qt Interface: A graphical user interface (GUI) built with PyQt5 enables users to visually control the robot.

  • Dynamic Motor Speed Adjustment: Motor speeds are dynamically adjusted based on the distance to the target and angular differences.

  • Reverse Mode: Users can enable reverse mode to make the robot move in the opposite direction.

Project Wiring Diagram

Step 3: Run & Test

  1. When the application runs, it searches for a device connected to the USB port.

  2. In the graphical interface, the robot is represented as a red rectangle (or green in reverse mode).

  3. Users can set the robot's target by clicking on the desired position in the interface, and the robot will move towards it.

  4. Control buttons like "Start," "Stop," "Clear," and "Reverse" allow users to manage the robot's movement.

Control Buttons

  1. Start

    • Function: Initializes the robot's motors and begins the movement logic.

    • Use Case: Click this button to start the robot. The robot will move towards the target point set by clicking on the interface.

  2. Stop

    • Function: Stops the robot's motors and halts movement.

    • Use Case: Use this button to pause the robot's movement at any time. The robot will remain stationary until "Start" is pressed again.

  3. Clear

    • Function: Resets the robot's path and position.

    • Use Case: Click this button to clear the path drawn by the robot on the interface and reset its position to the center of the canvas. Useful for starting a new simulation.

  4. Reverse

    • Function: Toggles the robot's movement direction between forward and reverse.

    • Use Case: Press this button to enable or disable reverse mode. When enabled, the robot will move backward relative to its target. The button text changes dynamically to indicate the current mode:

      • "Reverse": Forward movement mode is active.

      • "Forward": Reverse movement mode is active.

Codes

from smd.red import *
import time
import math
import sys
from PyQt5.QtWidgets import QApplication, QMainWindow, QWidget, QPushButton, QVBoxLayout, QHBoxLayout
from PyQt5.QtGui import QPainter, QColor, QPen
from PyQt5.QtCore import Qt, QTimer
from serial.tools.list_ports import comports
from platform import system

# Function to detect and return the available USB port
def USB_Port():
    ports = list(comports())
    usb_names = {
        "Windows": ["USB Serial Port"],
        "Linux": ["/dev/ttyUSB"],
        "Darwin": [
            "/dev/tty.usbserial",
            "/dev/tty.usbmodem",
            "/dev/tty.SLAB_USBtoUART",
            "/dev/tty.wchusbserial",
            "/dev/cu.usbserial",
            "/dev/cu.usbmodem",
            "/dev/cu.SLAB_USBtoUART",
            "/dev/cu.wchusbserial",
        ]
    }
    
    os_name = system()
    if ports:
        for port, desc, hwid in sorted(ports):
            if any(name in port or name in desc for name in usb_names.get(os_name, [])):
                print("Connected")
                return port
        print("Available ports:")
        for port, desc, hwid in ports:
            print(f"Port: {port}, Description: {desc}, HWID: {hwid}")
    else:
        print("No ports detected!")
    return None

# Widget class to visualize the robot movement
class RobotCanvas(QWidget):
    def __init__(self):
        super().__init__()
        self.setMinimumSize(1200, 800)  # Set the canvas size
        
        # Initialize robot position and parameters
        self.robot_x = 600
        self.robot_y = 400
        self.robot_angle = 0
        self.robot_size = 15
        self.path_points = []  # Store movement path
        
        # Mouse tracking variables
        self.mouse_x = 600
        self.mouse_y = 400
        self.is_mouse_pressed = False
        self.is_reverse_mode = False
        self.mouse_target_distance = float('inf')  # Infinite distance by default

    # Event triggered when mouse button is pressed
    def mousePressEvent(self, event):
        self.is_mouse_pressed = True
        self.mouse_x = event.x()
        self.mouse_y = event.y()

    # Event triggered when mouse button is released
    def mouseReleaseEvent(self, event):
        self.is_mouse_pressed = False
        self.mouse_target_distance = float('inf')

    # Event triggered when mouse is moved
    def mouseMoveEvent(self, event):
        if self.is_mouse_pressed:
            self.mouse_x = event.x()
            self.mouse_y = event.y()

    # Paint event to draw the robot and its path
    def paintEvent(self, event):
        painter = QPainter(self)
        painter.setRenderHint(QPainter.Antialiasing)
        
        # Draw movement path
        if len(self.path_points) > 1:
            pen = QPen(QColor('blue'), 2)
            if self.is_reverse_mode:
                pen = QPen(QColor('purple'), 2)
            painter.setPen(pen)
            for i in range(len(self.path_points) - 1):
                x1, y1 = self.path_points[i]
                x2, y2 = self.path_points[i + 1]
                painter.drawLine(int(x1), int(y1), int(x2), int(y2))
        
        # Draw the robot
        painter.setPen(QPen(QColor('black'), 2))
        painter.setBrush(QColor('red' if not self.is_reverse_mode else 'green'))
        painter.drawRect(
            int(self.robot_x - self.robot_size),
            int(self.robot_y - self.robot_size),
            self.robot_size * 2,
            self.robot_size * 2
        )
        
        # Draw direction indicator
        painter.setPen(QPen(QColor('black'), 2))
        painter.drawLine(
            int(self.robot_x),
            int(self.robot_y),
            int(self.robot_x + self.robot_size * math.cos(self.robot_angle)),
            int(self.robot_y - self.robot_size * math.sin(self.robot_angle))
        )

        # Draw mouse click target position
        if self.is_mouse_pressed:
            painter.setPen(QPen(QColor('green'), 2))
            painter.drawEllipse(self.mouse_x - 5, self.mouse_y - 5, 10, 10)

# Main class for robot control
class LineFollower(QMainWindow):
    def __init__(self, port):
        super().__init__()
        
        # Initialize SMD motor controller
        self.m = Master(port)
        self.redLeft = self.m.attach(Red(1))  # Left motor
        self.redRight = self.m.attach(Red(0))  # Right motor
        
        self.main_speed = 50
        self.running = False
        
        self.setWindowTitle("Robot Mouse Control")
        
        # Create main layout
        main_widget = QWidget()
        self.setCentralWidget(main_widget)
        layout = QVBoxLayout(main_widget)
        
        # Add canvas to layout
        self.canvas = RobotCanvas()
        layout.addWidget(self.canvas)
        
        # Add control buttons
        button_layout = QHBoxLayout()
        self.start_button = QPushButton("Start")
        self.stop_button = QPushButton("Stop")
        self.clear_button = QPushButton("Clear")
        self.reverse_button = QPushButton("Reverse")

        # Connect buttons to their respective functions
        self.start_button.clicked.connect(self.start)
        self.stop_button.clicked.connect(self.stop)
        self.clear_button.clicked.connect(self.clear_path)
        self.reverse_button.clicked.connect(self.toggle_reverse)

        # Add buttons to layout
        button_layout.addWidget(self.start_button)
        button_layout.addWidget(self.stop_button)
        button_layout.addWidget(self.clear_button)
        button_layout.addWidget(self.reverse_button)
        
        layout.addLayout(button_layout)
        
        # Timer for updating robot movement
        self.timer = QTimer()
        self.timer.timeout.connect(self.update_robot)
        self.timer.setInterval(1)

    # Toggle reverse movement mode
    def toggle_reverse(self):
        self.canvas.is_reverse_mode = not self.canvas.is_reverse_mode
        self.reverse_button.setText("Forward" if self.canvas.is_reverse_mode else "Reverse")
        self.canvas.mouse_target_distance = float('inf')

    # Setup motor configurations
    def setup(self):
        try:
            self.m.set_operation_mode(1, OperationMode.PWM)
            self.m.set_operation_mode(0, OperationMode.PWM)
            
            self.m.enable_torque(1, True)
            self.m.enable_torque(0, True)
            return True
        except Exception as e:
            print(f"Error: {e}")
            return False

    # Update robot position and movement
    def update_robot_position(self, left_speed, right_speed):
        left_speed = left_speed / 100.0
        right_speed = right_speed / 100.0
        
        angular_velocity = (right_speed - left_speed) * 0.1
        self.canvas.robot_angle += angular_velocity
        
        forward_speed = (right_speed + left_speed) * 0.5
        multiplier = -1 if self.canvas.is_reverse_mode else 1
        
        self.canvas.robot_x += multiplier * forward_speed * math.cos(self.canvas.robot_angle) * 5
        self.canvas.robot_y -= multiplier * forward_speed * math.sin(self.canvas.robot_angle) * 5
        
        self.canvas.path_points.append((self.canvas.robot_x, self.canvas.robot_y))
        
        self.canvas.update()

    # Control loop to adjust robot movement based on mouse input
    def update_robot(self):
        if self.running:
            dx = self.canvas.mouse_x - self.canvas.robot_x
            dy = self.canvas.mouse_y - self.canvas.robot_y
            
            current_distance = math.sqrt(dx**2 + dy**2)
            target_angle = math.atan2(-dy, dx)

            if self.canvas.is_reverse_mode:
                target_angle = target_angle + math.pi if target_angle < 0 else target_angle - math.pi

            angle_diff = target_angle - self.canvas.robot_angle

            left_speed = self.main_speed - (angle_diff * 30)
            right_speed = self.main_speed + (angle_diff * 30)

            self.m.set_duty_cycle(0, -left_speed)
            self.m.set_duty_cycle(1, right_speed)

            self.update_robot_position(left_speed, right_speed)

    def start(self):
        if not self.setup():
            print("Setup Failed!")
            return
        self.running = True
        self.timer.start()

    def stop(self):
        self.running = False
        self.timer.stop()
        self.m.set_duty_cycle(0, 0)
        self.m.set_duty_cycle(1, 0)

def main():
    app = QApplication(sys.argv)
    port = USB_Port()
    window = LineFollower(port)
    window.show()
    sys.exit(app.exec_())

if __name__ == "__main__":
    main()
// Some code
SMD Red
Purchase Here
SMD USB Gateway
Purchase Here
Arduino Gateway Module
Purchase Here
BDC Motor
Purchase Here
SMD
Ultrasonic Distance Sensor Module
BDC Motor