CapitalRobotics / Janxs_Brain

This repository contains the core production code for FTC #14251, including autonomous, teleop, and utility functions used in both current and past seasons.
https://sites.google.com/mpsvt.org/capital-robotics/home
BSD 3-Clause Clear License
1 stars 0 forks source link

TensorFlow Lite Implementation and TPU offloading #9

Open torinriley opened 6 days ago

torinriley commented 6 days ago

My thought is to begin integrating a Raspberry Pi equipped with Tensor Processing Units (TPUs) (e.g., Google Coral USB Accelerator) into our robot’s system. This addition would offload computationally intensive tasks, such as running custom TensorFlow Lite models for vision processing or machine learning (ML), allowing the REV Control Hub to focus on core robot control and communication with the Field Management System (FMS).

Connect the Raspberry Pi to the REV Control Hub via USB or UART.

You could integrate a method to handle request to the raspberry pi,

import java.io.InputStream;
import java.io.OutputStream;
import android.hardware.usb.UsbManager;
import android.hardware.usb.UsbDeviceConnection;
import android.hardware.usb.UsbDevice;

public class RaspberryPiHandler {
    private UsbDeviceConnection connection;
    private OutputStream outStream;
    private InputStream inStream;

    public RaspberryPiHandler(UsbManager usbManager, UsbDevice device) {
        connection = usbManager.openDevice(device);
        if (connection != null) {
            outStream = connection.getFileDescriptor();
            inStream = connection.getFileDescriptor();
        }
    }

    // Send inference request to the Pi
    public void sendInferenceRequest() throws IOException {
        if (outStream != null) {
            outStream.write("INFER\n".getBytes());
        }
    }

    // Read inference result from the Pi
    public String getInferenceResult() throws IOException {
        byte[] buffer = new byte[1024];
        int bytesRead = inStream.read(buffer);
        return new String(buffer, 0, bytesRead).trim();
    }
}

And integration with teleop (or likely auto) like this,

import com.qualcomm.robotcore.eventloop.opmode.OpMode;

@TeleOp(name = "PiIntegrationExample", group = "TeleOp")
public class PiIntegrationExample extends OpMode {
    private RaspberryPiHandler piHandler;

    @Override
    public void init() {
        UsbManager usbManager = (UsbManager) hardwareMap.appContext.getSystemService(Context.USB_SERVICE);
        UsbDevice device = findRaspberryPiDevice(usbManager); // Implement device discovery logic
        if (device != null) {
            piHandler = new RaspberryPiHandler(usbManager, device);
            telemetry.addData("Status", "Raspberry Pi connected");
        } else {
            telemetry.addData("Status", "Raspberry Pi not found");
        }
    }

    @Override
    public void loop() {
        try {
            if (piHandler != null) {
                // Request inference
                piHandler.sendInferenceRequest();

                // Get and process the inference result
                String result = piHandler.getInferenceResult();
                telemetry.addData("Inference Result", result);

                // Example: Use result for robot decision-making
                String[] parts = result.split(" ");
                double x = Double.parseDouble(parts[0]);
                double y = Double.parseDouble(parts[1]);
                if (x < 100) {
                    // Move left
                } else if (x > 200) {
                    // Move right
                }
            }
        } catch (Exception e) {
            telemetry.addData("Error", e.getMessage());
        }

        telemetry.update();
    }

    private UsbDevice findRaspberryPiDevice(UsbManager usbManager) {
        for (UsbDevice device : usbManager.getDeviceList().values()) {
            // Add logic to match the Pi's USB Vendor and Product IDs
            return device;
        }
        return null;
    }
}
cheesebroccoli commented 1 day ago

We are not allowed to use rasberry pi but we can use the camera that is allowed for tensorflow lite