# Introducing Customized Blockly Blocks

## **Define Master Block**

This block enables the configuration of a USB gateway and its initialization with a user-selected baud rate. The baud rate can be conveniently chosen from a drop-down menu, ensuring flexibility and ease of use. The USB gateway serves as a critical communication bridge between the computer and the SMD Red system, facilitating seamless data exchange.

<figure><img src="https://1077748559-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-LuxEcL3mxZNc5Aa92N6%2Fuploads%2FCaJz0xrkQQEDNFfqUoDx%2Fimage.png?alt=media&#x26;token=573d409d-f2cf-4794-a940-ece09fd41e9b" alt=""><figcaption></figcaption></figure>

## **Smd Red Block**

This block establishes a connection to an SMD Red device by utilizing a specified Red ID parameter. The user is required to provide a valid Red ID to ensure accurate identification and communication with the target device.

<figure><img src="https://1077748559-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-LuxEcL3mxZNc5Aa92N6%2Fuploads%2FPMLLCn23J7ZEG58cfXBx%2Fimage.png?alt=media&#x26;token=fad89a7d-2544-478e-b6bb-91986760c5d9" alt=""><figcaption></figcaption></figure>

Above is the block structure for connecting to the smd red device with id value 0 and baudrate value 115200.

## **Set CPR Block**

This block is used to configure the **CPR (Counts Per Revolution)** value for a motor identified by a specified **Red ID**. The user provides the desired CPR value, which determines the resolution of the motor's encoder, ensuring precise control and measurement.

<figure><img src="https://1077748559-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-LuxEcL3mxZNc5Aa92N6%2Fuploads%2FNhzexDmcInuJgJdAk4KO%2Fimage.png?alt=media&#x26;token=64a270ea-1f45-461f-9435-09d5e58523cf" alt=""><figcaption></figcaption></figure>

## **Set RPM Block**

This block is used to configure the **RPM (Revolutions Per Minute)** value for a motor identified by a specified **Red ID**. The user provides the desired RPM value, which controls the motor's rotational speed to meet application-specific requirements.

<figure><img src="https://1077748559-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-LuxEcL3mxZNc5Aa92N6%2Fuploads%2FPklR6oB0HBwzNPXmUHU9%2Fimage.png?alt=media&#x26;token=bf0e1029-128c-4891-a8aa-334a8277fbfd" alt=""><figcaption></figcaption></figure>

## **Define Motor Block**

This block is used to initialize and start a motor using its default parameters. To activate the motor, the user must provide the **Red ID**, which identifies the specific motor to be started.

<figure><img src="https://1077748559-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-LuxEcL3mxZNc5Aa92N6%2Fuploads%2FLTXcA7i4haYT2NuIRMaY%2Fimage.png?alt=media&#x26;token=1aea2fb5-70de-4eef-be18-f813648cf405" alt=""><figcaption></figcaption></figure>

## Set Control Parameters Block

This block allows you to configure the PID parameters dynamically for the specific mode you choose and the Red ID you input. By specifying the appropriate `mode` (Velocity, Position, Torque) and a unique Red ID, you can assign tailored values for the proportional (Kp), integral (Ki), and derivative (Kd) gains. The functionality ensures flexibility and precision for different control scenarios.

<figure><img src="https://1077748559-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-LuxEcL3mxZNc5Aa92N6%2Fuploads%2F1k5Zs5uZ1mSNf9seRRAi%2Fimage.png?alt=media&#x26;token=7cb21fc5-651a-4554-a611-cae0833dd6eb" alt=""><figcaption></figcaption></figure>

## Enable Torque Bloc

This block is designed to initiate the torque of an motor dynamically using a specified Red ID. By entering the appropriate Red ID, the system ensures precise activation of the torque mechanism, tailored to the corresponding motor.

<figure><img src="https://1077748559-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-LuxEcL3mxZNc5Aa92N6%2Fuploads%2Fch28pqHLOOaNQSDo6MRD%2Fimage.png?alt=media&#x26;token=665a02d1-96e9-453c-9206-a7c3382bc9cf" alt=""><figcaption></figcaption></figure>

## **Set Operation Mode Block**&#x20;

This block allows the user to set the operating mode for a motor identified by a specified **Red ID**. The user can select from one of the following modes: **PWM**, **Position**, **Speed**, or **Torque**, depending on the desired control strategy for the motor.

<figure><img src="https://1077748559-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-LuxEcL3mxZNc5Aa92N6%2Fuploads%2F130daGtiRTRhHOedbqj3%2Fimage.png?alt=media&#x26;token=7cd74010-2f14-4154-b003-52cb8122f57b" alt=""><figcaption></figcaption></figure>

## Pwm Mode Blocks

These two blocks perform the same function, with the distinction that one accepts only **numerical values**, while the other accepts a **joystick variable block** as input. This flexibility allows users to choose the appropriate input method based on their specific application requirements.

<figure><img src="https://1077748559-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-LuxEcL3mxZNc5Aa92N6%2Fuploads%2F4mGiCHISIKTpQsm6tTJe%2Fimage.png?alt=media&#x26;token=ab027feb-fbfd-4f8b-9451-22dfe9dd8b6f" alt=""><figcaption></figcaption></figure>

## Position Mode Blocks

These two blocks perform the same function, but with different methods of control: one rotates the motor directly to the specified position (in degrees), while the other uses a joystick input block to control the rotation.

<figure><img src="https://1077748559-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-LuxEcL3mxZNc5Aa92N6%2Fuploads%2FrJW3YhEZWiAXKXSPTZi9%2Fimage.png?alt=media&#x26;token=1c7be226-a6de-4521-8779-6da4e42711d9" alt=""><figcaption></figcaption></figure>

## Velocity Mode Blocks

These two blocks perform the same function, but with different methods of speed control: one rotates the motor at a constant speed based on a given value, while the other adjusts the speed dynamically using a joystick input.

<figure><img src="https://1077748559-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-LuxEcL3mxZNc5Aa92N6%2Fuploads%2F5qehJdkMTnqWbxL7oF4B%2Fimage.png?alt=media&#x26;token=f72ba2bc-d023-406c-837f-99cd150fbf41" alt=""><figcaption></figcaption></figure>

## Torque Mode Blocks

These two blocks perform the same function, but with different methods for controlling the motor's current: one rotates the motor based on a fixed **milliampere** value, while the other adjusts the milliampere value dynamically using a joystick input.

<figure><img src="https://1077748559-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-LuxEcL3mxZNc5Aa92N6%2Fuploads%2FOvULMMBML6Ti87al5DMI%2Fimage.png?alt=media&#x26;token=9c60d6ae-885c-4931-a25d-646bf9305925" alt=""><figcaption></figcaption></figure>

## Get [Joystick](https://docs.acrome.net/electronics/add-on-modules/joystick-module) Block

This block receives **x**, **y**, and **button** information from the [joystick module](https://docs.acrome.net/electronics/add-on-modules/joystick-module) and synchronizes it with the corresponding variable blocks placed on the values within the block. This allows real-time control and interaction based on joystick input.

<figure><img src="https://1077748559-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-LuxEcL3mxZNc5Aa92N6%2Fuploads%2FAIX2177kUov0Eeh1SdCF%2Fimage.png?alt=media&#x26;token=c8056ef1-a8d8-4d12-895e-5ab6b19b7504" alt=""><figcaption></figcaption></figure>

```python
Red_id = None
Module_id = None
X = None
Y = None
B = None


from smd.red import *
import math
import os

from serial.tools.list_ports import comports
from platform import system
def USB_Port():
    if system() == 'Windows':
        ports = list(comports())
        if ports:
            for port, desc, hwid in sorted(ports):
                if 'USB Serial Port' in desc:
                    SMD_Port = port
                    return SMD_Port

    elif system() == 'Linux':
        ports = list(serial.tools.list_ports.comports())
        if ports:
            for port, desc, hwid in sorted(ports):
                if '/dev/ttyUSB' in port:
                    SMD_Port = port
                    return SMD_Port

    elif system() == 'Darwin':  # macOS
        ports = list(serial.tools.list_ports.comports())
        if ports:
            for port, desc, hwid in sorted(ports):
                if '/dev/tty.usbserial' in port or '/dev/tty.usbmodem' in port:
                    SMD_Port = port
                    return SMD_Port

    else:
        SMD_Port = None
        return SMD_Port

port=USB_Port()
m = Master(port,baudrate=115200)
Red_id = 0
m.attach(Red(Red_id))

Module_id = 1
while True:
  X, Y, B = m.get_joystick(Red_id, Module_id)
```

## [RGB Led](https://docs.acrome.net/electronics/add-on-modules/rgb-led-module) Block

This block controls the [**RGB LED module**](https://docs.acrome.net/electronics/add-on-modules/rgb-led-module) by setting its color according to the **RGB values** entered. Users can specify the desired red, green, and blue values to customize the LED’s color output.

<figure><img src="https://1077748559-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-LuxEcL3mxZNc5Aa92N6%2Fuploads%2FcEk3CPOuWuGzxkyQMmnb%2Fimage.png?alt=media&#x26;token=1ad27f41-5619-4042-af8c-e277642720ca" alt=""><figcaption></figcaption></figure>

```python
Red_id = None
Module_id = None


from smd.red import *
import math
import os

from serial.tools.list_ports import comports
from platform import system
def USB_Port():
    if system() == 'Windows':
        ports = list(comports())
        if ports:
            for port, desc, hwid in sorted(ports):
                if 'USB Serial Port' in desc:
                    SMD_Port = port
                    return SMD_Port

    elif system() == 'Linux':
        ports = list(serial.tools.list_ports.comports())
        if ports:
            for port, desc, hwid in sorted(ports):
                if '/dev/ttyUSB' in port:
                    SMD_Port = port
                    return SMD_Port

    elif system() == 'Darwin':  # macOS
        ports = list(serial.tools.list_ports.comports())
        if ports:
            for port, desc, hwid in sorted(ports):
                if '/dev/tty.usbserial' in port or '/dev/tty.usbmodem' in port:
                    SMD_Port = port
                    return SMD_Port

    else:
        SMD_Port = None
        return SMD_Port

port=USB_Port()
m = Master(port,baudrate=115200)
Red_id = 0
m.attach(Red(Red_id))

Module_id = 1
m.set_rgb(Red_id, Module_id, 255, 0, 0)
```

## [Button ](https://docs.acrome.net/electronics/add-on-modules/button-module)Block

This block retrieves and returns the current **value** from the [**button module**](https://docs.acrome.net/electronics/add-on-modules/button-module), allowing the system to react based on the button's state (e.g., pressed or released).

<figure><img src="https://1077748559-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-LuxEcL3mxZNc5Aa92N6%2Fuploads%2FO0OJUeQZNlPMzNR2yOWz%2Fimage.png?alt=media&#x26;token=eec4a190-fd51-493f-afcb-cf5632188f56" alt=""><figcaption></figcaption></figure>

```python
Red_id = None
Module_id = None
B = None


from smd.red import *
import math
import os

from serial.tools.list_ports import comports
from platform import system
def USB_Port():
    if system() == 'Windows':
        ports = list(comports())
        if ports:
            for port, desc, hwid in sorted(ports):
                if 'USB Serial Port' in desc:
                    SMD_Port = port
                    return SMD_Port

    elif system() == 'Linux':
        ports = list(serial.tools.list_ports.comports())
        if ports:
            for port, desc, hwid in sorted(ports):
                if '/dev/ttyUSB' in port:
                    SMD_Port = port
                    return SMD_Port

    elif system() == 'Darwin':  # macOS
        ports = list(serial.tools.list_ports.comports())
        if ports:
            for port, desc, hwid in sorted(ports):
                if '/dev/tty.usbserial' in port or '/dev/tty.usbmodem' in port:
                    SMD_Port = port
                    return SMD_Port

    else:
        SMD_Port = None
        return SMD_Port

port=USB_Port()
m = Master(port,baudrate=115200)
Red_id = 0
m.attach(Red(Red_id))

Module_id = 1
while True:
  B = m.get_button(Red_id, Module_id)
```

## [Ambient Light Sensor](https://docs.acrome.net/electronics/add-on-modules/ambient-light-sensor-module) Block

This block retrieves and returns the current **ambient light value**, allowing the system to adjust or react based on the surrounding light conditions.

<figure><img src="https://1077748559-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-LuxEcL3mxZNc5Aa92N6%2Fuploads%2F3jBVwsrVGvk4u96w6SpV%2Fimage.png?alt=media&#x26;token=fa8fbd9e-4e81-40a8-95ba-dc4dbdf65c9c" alt=""><figcaption></figcaption></figure>

```python
Red_id = None
Module_id = None
Light = None


from smd.red import *
import math
import os

from serial.tools.list_ports import comports
from platform import system
def USB_Port():
    if system() == 'Windows':
        ports = list(comports())
        if ports:
            for port, desc, hwid in sorted(ports):
                if 'USB Serial Port' in desc:
                    SMD_Port = port
                    return SMD_Port

    elif system() == 'Linux':
        ports = list(serial.tools.list_ports.comports())
        if ports:
            for port, desc, hwid in sorted(ports):
                if '/dev/ttyUSB' in port:
                    SMD_Port = port
                    return SMD_Port

    elif system() == 'Darwin':  # macOS
        ports = list(serial.tools.list_ports.comports())
        if ports:
            for port, desc, hwid in sorted(ports):
                if '/dev/tty.usbserial' in port or '/dev/tty.usbmodem' in port:
                    SMD_Port = port
                    return SMD_Port

    else:
        SMD_Port = None
        return SMD_Port

port=USB_Port()
m = Master(port,baudrate=115200)
Red_id = 0
m.attach(Red(Red_id))

Module_id = 1
while True:
  Light = m.get_light(Red_id,Module_id)
```

## [Ultrasonic Distance Sensor](https://docs.acrome.net/electronics/add-on-modules/ultrasonic-distance-sensor-module) Block

This block retrieves and returns the **distance value** measured by the [**ultrasonic distance sensor**](https://docs.acrome.net/electronics/add-on-modules/ultrasonic-distance-sensor-module), providing accurate readings of the proximity or distance to an object..

<figure><img src="https://1077748559-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-LuxEcL3mxZNc5Aa92N6%2Fuploads%2FcM17pMVwQOCiASwrjjwK%2Fimage.png?alt=media&#x26;token=53fc1c40-94cc-40d2-93b4-288561a6d399" alt=""><figcaption></figcaption></figure>

{% code lineNumbers="true" %}

```python
Red_id = None
Module_id = None
Distance = None


from smd.red import *
import math
import os

from serial.tools.list_ports import comports
from platform import system
def USB_Port():
    if system() == 'Windows':
        ports = list(comports())
        if ports:
            for port, desc, hwid in sorted(ports):
                if 'USB Serial Port' in desc:
                    SMD_Port = port
                    return SMD_Port

    elif system() == 'Linux':
        ports = list(serial.tools.list_ports.comports())
        if ports:
            for port, desc, hwid in sorted(ports):
                if '/dev/ttyUSB' in port:
                    SMD_Port = port
                    return SMD_Port

    elif system() == 'Darwin':  # macOS
        ports = list(serial.tools.list_ports.comports())
        if ports:
            for port, desc, hwid in sorted(ports):
                if '/dev/tty.usbserial' in port or '/dev/tty.usbmodem' in port:
                    SMD_Port = port
                    return SMD_Port

    else:
        SMD_Port = None
        return SMD_Port

port=USB_Port()
m = Master(port,baudrate=115200)
Red_id = 0
m.attach(Red(Red_id))

Module_id = 1
while True:
  Distance = m.get_distance(Red_id,Module_id)
```

{% endcode %}

## [Buzzer ](https://docs.acrome.net/electronics/add-on-modules/buzzer-module)Block

This block activates the [**buzzer module**](https://docs.acrome.net/electronics/add-on-modules/buzzer-module) and sets it to emit sound at the **frequency** value entered by the user.

<figure><img src="https://1077748559-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-LuxEcL3mxZNc5Aa92N6%2Fuploads%2FI1zHjdczyH0qDyyszMRt%2Fimage.png?alt=media&#x26;token=f4962aba-f94d-4635-b30c-9eca1a104e3e" alt=""><figcaption></figcaption></figure>

```python
Red_id = None
Module_id = None


from smd.red import *
import math
import os

from serial.tools.list_ports import comports
from platform import system
def USB_Port():
    if system() == 'Windows':
        ports = list(comports())
        if ports:
            for port, desc, hwid in sorted(ports):
                if 'USB Serial Port' in desc:
                    SMD_Port = port
                    return SMD_Port

    elif system() == 'Linux':
        ports = list(serial.tools.list_ports.comports())
        if ports:
            for port, desc, hwid in sorted(ports):
                if '/dev/ttyUSB' in port:
                    SMD_Port = port
                    return SMD_Port

    elif system() == 'Darwin':  # macOS
        ports = list(serial.tools.list_ports.comports())
        if ports:
            for port, desc, hwid in sorted(ports):
                if '/dev/tty.usbserial' in port or '/dev/tty.usbmodem' in port:
                    SMD_Port = port
                    return SMD_Port

    else:
        SMD_Port = None
        return SMD_Port

port=USB_Port()
m = Master(port,baudrate=115200)
Red_id = 0
m.attach(Red(Red_id))

Module_id = 1
m.set_buzzer(Red_id,Module_id,500)
```

## [IMU ](https://docs.acrome.net/electronics/add-on-modules/imu-module)Block

This block receives and processes data from the [**IMU (Inertial Measurement Unit)**](https://docs.acrome.net/electronics/add-on-modules/imu-module), which typically includes information such as orientation.

<figure><img src="https://1077748559-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-LuxEcL3mxZNc5Aa92N6%2Fuploads%2FoaHk9xpXODMkP4SnmrZu%2Fimage.png?alt=media&#x26;token=37aca06b-ce06-4103-a381-00cf6530a573" alt=""><figcaption></figcaption></figure>

```python
Red_id = None
Module_id = None
Pitch = None
roll = None


from smd.red import *
import math
import os

from serial.tools.list_ports import comports
from platform import system
def USB_Port():
    if system() == 'Windows':
        ports = list(comports())
        if ports:
            for port, desc, hwid in sorted(ports):
                if 'USB Serial Port' in desc:
                    SMD_Port = port
                    return SMD_Port

    elif system() == 'Linux':
        ports = list(serial.tools.list_ports.comports())
        if ports:
            for port, desc, hwid in sorted(ports):
                if '/dev/ttyUSB' in port:
                    SMD_Port = port
                    return SMD_Port

    elif system() == 'Darwin':  # macOS
        ports = list(serial.tools.list_ports.comports())
        if ports:
            for port, desc, hwid in sorted(ports):
                if '/dev/tty.usbserial' in port or '/dev/tty.usbmodem' in port:
                    SMD_Port = port
                    return SMD_Port

    else:
        SMD_Port = None
        return SMD_Port

port=USB_Port()
m = Master(port,baudrate=115200)
Red_id = 0
m.attach(Red(Red_id))

Module_id = 1
roll, Pitch = m.get_imu(Red_id, Module_id)  
```

## Potentiometer Block

This block retrieves the **ADC (Analog-to-Digital Conversion)** value from the [**potentiometer modul**](https://docs.acrome.net/electronics/add-on-modules/potentiometer-module)**e**, providing a digital representation of the analog voltage output based on the potentiometer's position.

<figure><img src="https://1077748559-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-LuxEcL3mxZNc5Aa92N6%2Fuploads%2FKfmZePaWjeoYijQ3NEjA%2Fimage.png?alt=media&#x26;token=50d94043-8086-4fec-a627-ad5ce36f5b12" alt=""><figcaption></figcaption></figure>

```python
Red_id = None
Module_id = None
pot = None


from smd.red import *
import math
import os

from serial.tools.list_ports import comports
from platform import system
def USB_Port():
    if system() == 'Windows':
        ports = list(comports())
        if ports:
            for port, desc, hwid in sorted(ports):
                if 'USB Serial Port' in desc:
                    SMD_Port = port
                    return SMD_Port

    elif system() == 'Linux':
        ports = list(serial.tools.list_ports.comports())
        if ports:
            for port, desc, hwid in sorted(ports):
                if '/dev/ttyUSB' in port:
                    SMD_Port = port
                    return SMD_Port

    elif system() == 'Darwin':  # macOS
        ports = list(serial.tools.list_ports.comports())
        if ports:
            for port, desc, hwid in sorted(ports):
                if '/dev/tty.usbserial' in port or '/dev/tty.usbmodem' in port:
                    SMD_Port = port
                    return SMD_Port

    else:
        SMD_Port = None
        return SMD_Port

port=USB_Port()
m = Master(port,baudrate=115200)
Red_id = 0
m.attach(Red(0))

Module_id = 1
pot = m.get_potentiometer(Red_id, Module_id)
```

## [Servo ](https://docs.acrome.net/electronics/add-on-modules/servo-module)Blocks

This block is used to set the position of a **servo motor** by specifying the desired angle. The motor adjusts its position to align with the given angle value.

<figure><img src="https://1077748559-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-LuxEcL3mxZNc5Aa92N6%2Fuploads%2FGB27b3uSjAoU5rHsWQ4K%2Fimage.png?alt=media&#x26;token=4ab82ac9-8daa-4d02-8f90-a9f46f5e5820" alt=""><figcaption></figcaption></figure>

```python
Red_id = None
Module_id = None


from smd.red import *
import math
import os

from serial.tools.list_ports import comports
from platform import system
def USB_Port():
    if system() == 'Windows':
        ports = list(comports())
        if ports:
            for port, desc, hwid in sorted(ports):
                if 'USB Serial Port' in desc:
                    SMD_Port = port
                    return SMD_Port

    elif system() == 'Linux':
        ports = list(serial.tools.list_ports.comports())
        if ports:
            for port, desc, hwid in sorted(ports):
                if '/dev/ttyUSB' in port:
                    SMD_Port = port
                    return SMD_Port

    elif system() == 'Darwin':  # macOS
        ports = list(serial.tools.list_ports.comports())
        if ports:
            for port, desc, hwid in sorted(ports):
                if '/dev/tty.usbserial' in port or '/dev/tty.usbmodem' in port:
                    SMD_Port = port
                    return SMD_Port

    else:
        SMD_Port = None
        return SMD_Port

port=USB_Port()
m = Master(port,baudrate=115200)
Red_id = 0
m.attach(Red(0))

Module_id = 1
m.set_servo(Red_id, Module_id, 90)
```

## [QTR ](https://docs.acrome.net/electronics/add-on-modules/reflectance-sensor-module)Block

This block retrieves the sensor values from the [**QTR** module](https://docs.acrome.net/electronics/add-on-modules/reflectance-sensor-module), typically used for detecting changes in proximity, position, or surface reflection.

<figure><img src="https://1077748559-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-LuxEcL3mxZNc5Aa92N6%2Fuploads%2Frv8fVflt5prKGriAATGP%2Fimage.png?alt=media&#x26;token=6e87443b-95f8-472d-bff9-ff7b67d15a8c" alt=""><figcaption></figcaption></figure>

```python
Red_id = None
Module_id = None
left = None
right = None
middle = None


from smd.red import *
import math
import os

from serial.tools.list_ports import comports
from platform import system
def USB_Port():
    if system() == 'Windows':
        ports = list(comports())
        if ports:
            for port, desc, hwid in sorted(ports):
                if 'USB Serial Port' in desc:
                    SMD_Port = port
                    return SMD_Port

    elif system() == 'Linux':
        ports = list(serial.tools.list_ports.comports())
        if ports:
            for port, desc, hwid in sorted(ports):
                if '/dev/ttyUSB' in port:
                    SMD_Port = port
                    return SMD_Port

    elif system() == 'Darwin':  # macOS
        ports = list(serial.tools.list_ports.comports())
        if ports:
            for port, desc, hwid in sorted(ports):
                if '/dev/tty.usbserial' in port or '/dev/tty.usbmodem' in port:
                    SMD_Port = port
                    return SMD_Port

    else:
        SMD_Port = None
        return SMD_Port

port=USB_Port()
m = Master(port,baudrate=115200)
Red_id = 0
m.attach(Red(0))

Module_id = 1
left, middle, right = m.get_qtr(Red_id, Module_id)
```

## Get Position Block

This block retrieves the current position of the motor identified by the specified **Red ID**. It allows the system to track and monitor the motor's position in real-time.

<figure><img src="https://1077748559-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-LuxEcL3mxZNc5Aa92N6%2Fuploads%2F0sZ4yJv30J5WNNUaP29L%2Fimage.png?alt=media&#x26;token=40c8d287-1daf-4243-809d-91cd8a553fe6" alt=""><figcaption></figcaption></figure>

```python
Red_id = None
position = None


from smd.red import *
import math
import os

from serial.tools.list_ports import comports
from platform import system
def USB_Port():
    if system() == 'Windows':
        ports = list(comports())
        if ports:
            for port, desc, hwid in sorted(ports):
                if 'USB Serial Port' in desc:
                    SMD_Port = port
                    return SMD_Port

    elif system() == 'Linux':
        ports = list(serial.tools.list_ports.comports())
        if ports:
            for port, desc, hwid in sorted(ports):
                if '/dev/ttyUSB' in port:
                    SMD_Port = port
                    return SMD_Port

    elif system() == 'Darwin':  # macOS
        ports = list(serial.tools.list_ports.comports())
        if ports:
            for port, desc, hwid in sorted(ports):
                if '/dev/tty.usbserial' in port or '/dev/tty.usbmodem' in port:
                    SMD_Port = port
                    return SMD_Port

    else:
        SMD_Port = None
        return SMD_Port

port=USB_Port()
m = Master(port,baudrate=115200)
Red_id = 0
m.attach(Red(Red_id))

position = m.get_position(Red_id)
```
