v5 Brain to Raspberry Pi Communication

Good evening,

I’ve started a project of getting communication between the brain and a Raspberry Pi. I’ve read some past threads about the topic on here and it seems using the microUSB port on the brain and a USB port on the Pi will work. I don’t really know what I’m doing but I’m prepared to figure it out. I asked ChatGPT for advice on the exact steps and they seem reasonable, I’m going to try them out and also post them below hoping that, if they’re incorrect, someone can point out the error and save me some time :slight_smile:. I’m using PROS by the way.

Step 1: Connect the VEX V5 Brain to the Raspberry Pi

  • Connect the VEX V5 Brain to one of the USB ports on the Raspberry Pi using a USB cable from the microUSB port on the V5 Brain to the USB port on the Raspberry Pi.

Step 2: Set Up the Raspberry Pi to Recognize the V5 Brain

  • Identify the Device: On your Raspberry Pi, open a terminal and use the command dmesg | grep tty to identify the device names assigned to the V5 Brain. It will usually be something like ttyACM0.
  • Modify udev Rules (if necessary): Sometimes, to ensure consistent device naming and permissions, you might need to add udev rules. Create a file in /etc/udev/rules.d/ (e.g., 99-vex.rules) and add a rule like:

makefile

SUBSYSTEM=="tty", ATTRS{idVendor}=="Your_Vendor_ID", ATTRS{idProduct}=="Your_Product_ID", SYMLINK+="vexV5", MODE="0666"

Replace Your_Vendor_ID and Your_Product_ID with the actual USB vendor and product IDs for the V5 Brain, which you can find using lsusb.

  • Reload udev rules: After modifying or adding a udev rule, reload the rules with sudo udevadm control --reload-rules && sudo udevadm trigger.

Step 3: Configure Serial Communication on Raspberry Pi

  • Open the Serial Port: Use a programming language that can handle serial communication, such as Python with the pyserial library. Install it via pip if necessary (pip install pyserial).
  • Serial Communication Script: Write a script that opens the serial port (e.g., /dev/ttyACM0) and configures the appropriate baud rate and other serial settings. Here’s a basic example in Python:
import serial
import time

# Open serial port
ser = serial.Serial('/dev/ttyACM0', 115200)  # Adjust the device name and baud rate

# Read and write loop
while True:
    if ser.in_waiting > 0:
        line = ser.readline().decode('utf-8').rstrip()
        print(f"Received: {line}")
    ser.write("Hello V5 Brain\n".encode())
    time.sleep(1)

Step 4: Setup in PROS for VEX V5

  1. Initialization: Ensure the project is set up to use stdio for sending and receiving data. This might already be configured by default in PROS.
  2. Writing to Serial: Use printf() for sending data, which writes to the standard output (connected to the Raspberry Pi via USB in your setup).
  3. Reading from Serial: Use scanf(), getchar(), or similar functions to read incoming data from the standard input.

// Task for sending data to Raspberry Pi
void sendDataToRPI(void* param) {
    int sensorValue = 0;
    while (true) {
        sensorValue = analogRead('A');  // Just an example, replace 'A' with your actual sensor port
        printf("Sensor Value: %d\n", sensorValue);
        pros::delay(1000);  // Delay for 1 second between sends
    }
}

// Task for receiving data from Raspberry Pi
void receiveDataFromRPI(void* param) {
    char buffer[256];  // Buffer to store incoming data
    while (true) {
        if (fgets(buffer, sizeof(buffer), stdin) != NULL) {
            // Process received data
            printf("Received from RPi: %s", buffer);
        }
        pros::delay(20);  // Small delay to prevent hogging CPU cycles
    }
}

// Main function to initialize tasks
void opcontrol() {
    pros::Task sendTask(sendDataToRPI, NULL, TASK_PRIORITY_DEFAULT, TASK_STACK_DEPTH_DEFAULT, "Send Data Task");
    pros::Task receiveTask(receiveDataFromRPI, NULL, TASK_PRIORITY_DEFAULT, TASK_STACK_DEPTH_DEFAULT, "Receive Data Task");

    while (true) {
        pros::delay(20);
    }
}```

Thanks,
Matthew

This topic was automatically closed 365 days after the last reply. New replies are no longer allowed.