Ask Your Question
0

How to work with PID controller / OpenCV for A.R Drone 2.0?

asked 2017-11-04 11:04:33 -0600

JOE gravatar image

updated 2017-11-04 14:51:08 -0600

Tetragramm gravatar image

I have been looking at codes for autonomous drones and encountered this one on this repository: https://github.com/puku0x/cvdrone . Im trying to understand the code, I am new to controller algorithms and OpenCV. I tried going on the OpenCV website and understand functions but it didn't help very much. Any help would be appreciated.

    if (contour_index >= 0) {
        // Moments
        cv::Moments moments = cv::moments(contours[contour_index], true);
        double marker_y = (int)(moments.m01 / moments.m00);
        double marker_x = (int)(moments.m10 / moments.m00);

        // Show result
        cv::Rect rect = cv::boundingRect(contours[contour_index]);
        cv::rectangle(image, rect, cv::Scalar(0, 255, 0));


       // Tracking
        if (track) {
            const double kp = 0.005;
            vx = kp * (binalized.rows / 2 - marker_y);;
            vy = 0.0;
            vz = kp; 
            vr = kp * (binalized.cols / 2 - marker_x);
            std::cout << "(vx, vy, vz, vr)" << "(" << vx << "," << vy << "," << vz << "," << vr << ")" << std::endl;
            std::cout << "Altitude = " << ardrone.getAltitude() << "%" << std::endl;
        }
              // Marker tracking
    if (track) {
        // PID gains
        const double kp = 0.001;
        const double ki = 0.000;
        const double kd = 0.000;

        // Errors
        double error_x = (binalized.rows / 2 - marker.y);   // Error front/back
        double error_y = (binalized.cols / 2 - marker.x);   // Error left/right

        // Time [s]
        static int64 last_t = 0.0;
        double dt = (cv::getTickCount() - last_t) / cv::getTickFrequency();
        last_t = cv::getTickCount();

        // Integral terms
        static double integral_x = 0.0, integral_y = 0.0;
        if (dt > 0.1) {
            // Reset
            integral_x = 0.0;
            integral_y = 0.0;
        }
        integral_x += error_x * dt;
        integral_y += error_y * dt;

        // Derivative terms
        static double previous_error_x = 0.0, previous_error_y = 0.0;
        if (dt > 0.1) {
            // Reset
            previous_error_x = 0.0;
            previous_error_y = 0.0;
        }
        double derivative_x = (error_x - previous_error_x) / dt;
        double derivative_y = (error_y - previous_error_y) / dt;
        previous_error_x = error_x;
        previous_error_y = error_y;

        // Command velocities
        vx = kp * error_x + ki * integral_x + kd * derivative_x;
        vy = 0.0;//kp * error_y + ki * integral_y + kd * derivative_y;
        vz = 0.0;
        vr = 0.0;

    }
    }

    // Display the image
    cv::putText(image, (track) ? "track on" : "track off", cv::Point(10, 20), cv::FONT_HERSHEY_SIMPLEX, 0.5, (track) ? cv::Scalar(0, 0, 255) : cv::Scalar(0, 255, 0), 1, cv::LINE_AA);
    cv::imshow("camera", image);
    ardrone.move3D(vx, vy, vz, vr);
}
edit retag flag offensive close merge delete

Comments

Link broken.

supra56 gravatar imagesupra56 ( 2017-11-04 12:11:27 -0600 )edit

Link Fixed.

Tetragramm gravatar imageTetragramm ( 2017-11-04 14:51:18 -0600 )edit

1 answer

Sort by ยป oldest newest most voted
0

answered 2017-11-04 15:04:58 -0600

Tetragramm gravatar image

The part of this that is OpenCV is pretty simple.

Somewhere before the start of this code, contours are found. One of them is selected (represented by countour_index).

The cv::moments is being used to find the center of the contour. Then it draws a bounding rectangle on the image around the contour.

Then a bunch of stuff with nothing to do with OpenCV, and then it writes either "track on" or "track off" on the image and displays it.

All that stuff in the middle? I dunno what the first if statement is, it all get overwritten anyway. The second if statement uses two PID loops with no I or D components to create velocity components. There is no altitude or rotation change, so just x and y. No, I'm not going to explain a PID loop when wikipedia has a pretty good explanation already.

So I think this just hovers over some marker? Move the marker and the drone follows it, I guess.

edit flag offensive delete link more

Question Tools

1 follower

Stats

Asked: 2017-11-04 10:55:47 -0600

Seen: 991 times

Last updated: Nov 04 '17