In order to use, upload to VEX.V5. Upload port on the physical robot, then connect to a potentiometer connected to a mouse. C++ and Python. Remember to alter the code for the VEX Cortex A9 V5 Brain to the specific port. Code in notes.
#include "vex.h" #include <opencv2/opencv.hpp> #include <iostream> using namespace vex; using namespace cv; // VEX Brain instance named AP9 brain AP9; // Motors motor motor1(PORT1, ratio18_1, false); // Motor on port 1 motor motor2(PORT2, ratio18_1, false); // Motor on port 2 // Sensors potentiometer potentiometer1(ANALOG_PORT1); // Potentiometer on analog port 1 lineTracker lineTracker1(DIGITAL_PORT2); // Line Tracker on digital port 2 led led(AP9.ThreeWirePort.A); // LED on the three-wire port A // Function to activate motors based on potentiometer value void activateMotors() { int potentiometerValue = potentiometer1.value(); // Get potentiometer value (0 to 4095) int speed = potentiometerValue / 4095.0 * 100; // Normalize to range 0-100 // Set motor velocities based on potentiometer value motor1.setVelocity(speed, percent); motor2.setVelocity(speed, percent); motor1.spin(forward); motor2.spin(forward); } // Function to detect red color in the frame void detectRed(cv::Mat& frame) { // Convert image to HSV (Hue, Saturation, Value) cv::Mat hsv; cvtColor(frame, hsv, COLOR_BGR2HSV); // Define the range for detecting red color (in HSV) cv::Scalar lowerRed(0, 120, 70); cv::Scalar upperRed(10, 255, 255); // Create a mask for the red color cv::Mat mask; inRange(hsv, lowerRed, upperRed, mask); // Find contours of the red areas std::vector<std::vector<cv::Point>> contours; findContours(mask, contours, RETR_TREE, CHAIN_APPROX_SIMPLE); if (contours.size() > 0) { led.set(led.green); // If red objects are detected, set LED to Green activateMotors(); // Activate motors if red is detected } else { led.set(led.off); // Turn off LED if no red is detected } } int main() { // Initialize the webcam cv::VideoCapture cap(0); // Open the default camera if (!cap.isOpened()) { std::cerr << "Error: Could not open the camera." << std::endl; return -1; } while (true) { // Forever loop cv::Mat frame; cap >> frame; // Capture frame from the camera if (frame.empty()) { std::cerr << "Error: Empty frame captured." << std::endl; break; } // Detect red objects in the frame detectRed(frame); // Display the processed frame imshow("MACE Autonomous Soldier - Red Detection"(40,29) , frame); // Break the loop if the 'q' key is pressed if (cv::waitKey(1) == 'q') { break; } } // Release the webcam and close all OpenCV windows cap.release(); cv::destroyAllWindows(); return 0; }