Raspberry Pi Toy Car

We bring a HS enhanced Raspberry Pi Toy car by using object detector.

Hardware list:

  • HS device
  • Raspberry Pi 3b
  • Assembled Raspberry Pi toy car with SCM controlled motors

Workflow

1-1

The built-in model Mobilenet-SSD object detector is used in this DIY demo. The logic is illustrated as below:

1-2

Implementation

Firstly we assemble the toy car, the car is not necessary to be the same as ours, it should work as long as it is controlled through GPIO pin. Then we install SungemSDK to the Raspberry Pi and remotely test and debug through a Python IDE. We recommend pycharm.

Please note that due to the low stability of Raspberry Pi USB power transfer (max 600mA), it might be sometimes insufficient to power the HS device or other external devices. However, it’s possible to use an extra cable for power.

2-2

python code for controlling the car:

#!/usr/bin/env python3
# coding=utf-8

import sys
sys.path.append("../../SungemSDK-Python")
import hsapi as hs
import drive
from video import VideoProcessor


def process(ret):
    bicycles = [x for x in ret[1] if x[0] in {1}]  # 1 bicycle
    if len(bicycles) > 0:  # when bicycle detected
        bike = bicycles[0]
        x_mid = (bike[2] + bike[4]) / 2 / ret[0].shape[1]
        if x_mid < 0.4:  # target LHS
            return drive.Direction.left
        elif x_mid > 0.6:  # target RHS
            return drive.Direction.right
        else:  # front
            return drive.Direction.up
    else:
        return drive.Direction.stop


if __name__ == '__main__':
    t_driver = drive.DriveThread()  # a thread for driving car
    t_video = VideoProcessor(("192.168.50.201", 10700))  # socket thread for transferring video frames

    try:
        net = hs.ObjectDetector(zoom=True, verbose=0, threshSSD=0.2)
        t_driver.start()
        t_video.start()

        while True:
            result = net.run()  # return result from HS device
            t_driver.direction = process(result)
            image = net.plot(result)
            if t_video.has_client():
                t_video.input_queue.put(image)
    finally:
        drive.cleanup()

Outcome

3-1

When target object (bicycle) is detected, the car will run toward to the object. 3-2