yahboom
HomeLearn
Solution
Blog
Online Store
About Us
EN/中文
Raspbot📖
1.First Trial and Installation
  • 1.1 Installation video
  • 1.2 First Trial
2.Remote control course
  • 2.1 APP control car
  • 2.2 IR control car
3.Preparation
  • 3.1 Preparation
  • 3.2 Run a code on JupyterLab
  • 3.5 TF card expand capacity
4.OpenCV Basic course
A.Getting started with OpenCV
  • 1.Introduction of OpenCV
  • 2.OpenCV image read and display
  • 3.OpenCV image write
  • 4.OpenCV image quality
  • 5.OpenCV pixel operation
B.OpenCV Geometric transformat
  • 1.Picture Zoom
  • 2.Picture Cut
  • 3.Picture Shift
  • 4.Picture Mirroring
  • 5.Affine Map
  • 6.Picture Rotation
  • 7.Perspective Transformation
C.OpenCV picture processing
  • 1.Grayscale processing
  • 2.Image Binarization
  • 3.Edge detection
  • 4.Drawing line segment
  • 5.Drawing rectangle circle
  • 6.Drawing text picture
D.OpenCV image beautification
  • 1.Histogram of color pictures
  • 2.Histogram equalization
  • 3.Image restoration
  • 4.Brightness enhancement
  • 5.Skin whitening
  • 6.Gaussian mean filtering
  • 7.Median filtering
E.OpenCV machine learning
  • 1.KNN recognition of handwritten numbers
  • 2.CNN recognition of handwritten numbers
  • 3.SVM recognition of handwritten numbers
5.Hardware Control course
  • 5.1 Drive buzzer
  • 5.2 Drive motor
  • 5.3 Drive servo
  • 5.4 Ultrasonic ranging
  • 5.5 Ultrasonic avoid
  • 5.6 Ultrasonic+IR avoid
  • 5.7 Tracking
  • 5.8 Restricting
  • 5.9 Drive LED
  • 5.10 IR control test
  • 5.11 IR control car
6.AI vision course
  • 6.1 Driver camera
  • 6.2 Color recognition
  • 6.3 HSV value test
  • 6.4 Camera color tracking
  • 6.5 Car color tracking
  • 6.6 Tensorflow object recognition
  • 6.7 QR code recognition
  • 6.8 QR code control
  • 6.9 Face detection
  • 6.10 Face tracking
  • 6.11 Autopilot
7.Battery and charging
  • Battery and charging
Download
  • APP(iOS/Android)
  • Code
  • Download image
  • Instruction manual
  • More Annex
Download ZIP
FAQ
  • 1、Raspbot-Raspberry Pi 4b version desktop connection WiFi method
  • 2、Can't connect your home WiFi
  • 3、How to check IIC function on Raspbot
  • 4、Gesture recognition cannot work normally.
  • 5、How to write image file into SD card

Welcome to Raspbot repository

6.11 Autopilot


Path: /home/pi/Yahboom_Project/Raspbot/3.AI Vision course/12.Autopilot/Autopilot.ipynb

1. OpenCV coordinate system

row == heigh == Point.y

col == width == Point.x

Mat::at(Point(x, y)) == Mat::at(y,x)

image.png 

Obtain image: ret, frame = image.read()

Image resize: frame = cv2.resize(frame,(320,240))

2.Perspective transformation

Because the picture seen by the camera is distorted, we need to transform the area in front of the body into a top view through perspective transformation. Then zoom in to the entire picture area.

As shown in the figure below, the left is the original camera picture, and the right is the picture after perspective transformation.

image.png  image.png

        【original camera picture】  【picture after perspective transformation】

Function:

matSrc = np.float32([[0, 149],  [310, 149], [271, 72], [43, 72]])

       matDst = np.float32([[0,240], [310,240], [310,0], [0,0]])

        matAffine = cv2.getPerspectiveTransform(matSrc,matDst)# mat 1 src 2 dst

        dst = cv2.warpPerspective(frame,matAffine,(320,240))

Then, we can draw the rectangle based on the perspective coordinates.

 # Draw the rectangle box

        pts = np.array([[0, 149],  [310, 149], [271, 72], [43, 72]], np.int32)

        pts = pts.reshape((-1, 1, 2))

        cv2.polylines(frame, [pts],True, (255, 0, 0), 3)

3.Binary image

The core idea of binarization is to set a threshold, and the value greater than the threshold is 0 (black) or 255 (white), making the image called black and white. The threshold can be fixed or adaptive.

Threshold function is provided in Python-OpenCV:

cv2.threshold(src, threshold, maxValue, method)

eg:

    dst_gray = cv2.cvtColor(dst, cv2.COLOR_RGB2GRAY)

    dst_retval, dst_binaryzation = cv2.threshold(dst_gray, 110, 255, cv2.THRESH_BINARY)

After the perspective transformation of the picture, the converted image is shown below.

image.png  

4. Get the edge of the black line and draw the line

 

image.png  

The purpose of drawing the line is to facilitate the user to check whether there is a problem in the above process.

Draw the position of the two edges of the black line, and the position of the center line of the image and the center of the black line.

l The image size after perspective is still 320*240, so the centerline of the image is: midpoint: 320/2-1=159 Or: midpoint = np.int(histogram.shape[0]/2)

l Search on the left side of the black line: take the minimum index of the left image, and implement the following function: leftx_base = np.argmax(histogram[:midpoint], axis = 0)

l Search to find the right line of the black line: Invert the image and take the index of the minimum value on the left of the image, and then subtract the index from the image width:

l rightx_base = np.argmin(histogram[::-1][:rightpoint], axis = 0) #Reverse the histogram and take the rightmost value

rightx_base = 319-rightx_base

l Calculation of actual midline position: (left line + right line)/2

l lane_center = int((leftx_base + rightx_base)/2) #The middle position of the left and right lines

l Image centerline:  cv2.line(dst_binaryzation, (159,0), (159,240),(255,0,255),2)  #purple

l Left line: cv2.line(dst_binaryzation,(leftx_base,0),(leftx_base,240),(0,255,0),2)   #green

l Righht line: cv2.line(dst_binaryzation,(rightx_base,0),(rightx_base,240),(0,255,0),2)  #green

l Actual midline: cv2.line(dst_binaryzation,(lane_center,0),(lane_center,240),(255,0,0),2)  #blue

5. Calculate the offset

After finding the white line of Autopilot in step 5, we can know the size of the body deviation according to the difference between the actual center line and the center line of the image, so that the car can be controlled to correct the deviation to achieve the purpose of Autopilot.

Calculation method:

Bias = midpoint-lane_center

According to the sign and magnitude of the offset value, we limit it to -20 ~ 20, which means that the maximum speed of the steering is 20, to prevent the steering from losing the white line of the track too quickly.

Steering angle PID adjustment:

     Z_axis_pid.SystemOutput = Bias

        Z_axis_pid.SetStepSignal(0)

        Z_axis_pid.SetInertiaTime(0.5, 0.2)

After the PID calculation, the PID output is also limited to a maximum of ± 20.

        if Z_axis_pid.SystemOutput > 20:

            Z_axis_pid.SystemOutput = 20

        elif Z_axis_pid.SystemOutput < -20:

            Z_axis_pid.SystemOutput = -20

6. Control the robot to complete autopilot     

image.pngimage.png 

Pic-1                       Pic-2

image.png image.png

               Pic-3                                 Pic-4

Pic-1: Slight deviation, go straight.

Pic-2: A large right deviation is corrected by turning right.

Figure 3: The deviation caused by the left right angle is calibrated by turning in place.

Figure 4: The picture is lost when passing a right angle, and the direction of rotation is judged according to the previous state before detection.

About code:

        if leftx_base == 0 and rightx_base == 319:
            if prev_left > prev_right:
                car.Control_Car(-70, 60)
            elif prev_left < prev_right:
                car.Control_Car(70, -70)
            prev_left = 0
            prev_right = 0
        else:
            if Bias > 3:   
                #prev_left = 1
                #prev_right = 0
                if Bias > 140:
                    car.Control_Car(-70, 60)
                    prev_left = 0
                    prev_right = 0
                else:
                    car.Control_Car(45+int(Z_axis_pid.SystemOutput), 45-int(Z_axis_pid.SystemOutput))
                time.sleep(0.001)
            elif Bias < -3:   
                #prev_right = 1
                #prev_left = 0
                if Bias < -140:   
                    car.Control_Car(60, -70)
                    prev_left = 0
                    prev_right = 0
                else:
                    car.Control_Car(45+int(Z_axis_pid.SystemOutput), 45-int(Z_axis_pid.SystemOutput))
                time.sleep(0.001)
            else:
                car.Car_Run(45, 45)
        if left_sum != right_sum:
            if left_sum < right_sum:
                prev_left = prev_left + 1
            elif right_sum < left_sum:
                prev_right = prev_right + 1

 

 


Face tracking
Battery and charging

COMPANY

  • About us
  • Team Style
  • Blog

LEARNING & EXPLORATION

  • Tutorials
  • Edge AI hardware
  • Educational robots
  • Embedded intelligent hardware

WHERE TO BUY

  • Yahboom Online Store
  • Amazon
  • Alibaba
  • AliExpress
  • Mercado

TECHNOLOGY SUPPORT

  • Email: support@yahboom.com

BE A DISTRIBUTOR

  • Email: sales@yahboom.com
QR Code
QR Code

Copyright © 2017-2026 Yahboom. All Rights Reserved.

EN/中文