Exoskeleton for a cockroach

cockroach Dead head and CraftDuino

For some time now, cockroaches have been living in our laboratory. More specifically, cockroaches “Dead Head” (Blaberus craniifer) (see the reference about this type of cockroach at the end of the article).

And I decided to make an exoskeleton for them.
The idea of ​​the exoskeleton is to use the camera to track the movements of the cockroach and, in accordance with its movement, issue control commands to the motors of the mobile platform.

For the basis of the exoskeleton, I took the chassis from the Makeblock constructor from Alvin the robot.

The CraftDuino controller with a motor shield, which was made from a proto-shield with a screwed motor module based on a microcircuit, went into action. L298.
A DC-DC converter with a power button that is used to convert 12V from a Li-ion battery to 5V to power a Raspberry Pi single board computer.
And of course, I attached a Raspberry Pi to the robot with a camera module and a USB hub and a Wi-Fi whistle (for remote access to a computer).

In addition, there are 2 IR sensors and 1 ultrasonic rangefinder on the robot, but they are not currently used.

The result is such a mobile platform and it is also an exoskeleton for a cockroach:
exoskeleton for cockroach

On the front beam of the chassis (right under the camera), with the help of double-sided tape, I fixed an empty bucket from the PKL-a.
empty front beam
bucket on the front beam

This bucket will be the working space for the driver cockroach.
To make the cockroach stand out more contrast against the background, I cut out a circle of white paper at the bottom of the bucket, and so that the cockroach does not run away, I smeared the edges of the sides of the bucket vaseline (I got this method from American researchers [1]).

We take several test shots of a cockroach in order to select thresholds for its detection.
bank driver cockroach
bank driver cockroach
bank driver cockroach
bank driver cockroach

Let’s write a simple Python script that uses an open source computer vision library OpenCV and adaptive thresholding (cv2.adaptiveThreshold()) for cockroach detection.
Let’s select the thresholds using the sliders, and to improve performance, select a rectangle (green frame in the screenshot), where you generally need to look for a cockroach (ROI).


def find_roach(img):
    gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)

    blockSize = cv2.getTrackbarPos('blockSize', 'roacha')
    C = cv2.getTrackbarPos('C', 'roacha')

    if blockSize % 2 == 0:
        blockSize = blockSize+1

    print("blockSize: {0} C: {1}".format(blockSize, C))
    bina = cv2.adaptiveThreshold(gray, 255, cv2.ADAPTIVE_THRESH_MEAN_C, cv2.THRESH_BINARY_INV, blockSize, C)
    cv2.imshow('roacha', bina)

selected parameters for cockroach detection

test image for cockroach search:
test image for cockroach search

No cockroach:
test picture where there is no cockroach

The cockroach is:
test image for cockroach search

Cockroach found:
cockroach discovered

After the threshold transformation, we search for contours (cv2.findContours()) and for the found contour we calculate a rectangle with which it can be circled (cv2.boundingRect()).

def find_roach(img):
    gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)

    blockSize = 109
    C = 53

    x1 = 55
    y1 = 16
    x2 = 258
    y2 = 219

    print("blockSize: {0} C: {1}".format(blockSize, C))
    bina = cv2.adaptiveThreshold(gray, 255, cv2.ADAPTIVE_THRESH_MEAN_C, cv2.THRESH_BINARY_INV, blockSize, C)
    cv2.imshow('roacha', bina)

    roi = bina[y1:y2, x1:x2]
    cv2.imshow('roach', roi)

    roi2 = img[y1:y2, x1:x2]

    contours, hierarchy = cv2.findContours(roi, cv2.RETR_LIST, cv2.CHAIN_APPROX_SIMPLE)
    # Choose largest contour
    best = 0
    maxsize = 0
    count = 0
    for cnt in contours:
        if cv2.contourArea(cnt) > maxsize :
            maxsize = cv2.contourArea(cnt)
            best = count
        count = count + 1
    x,y,w,h = cv2.boundingRect(contours[best])
    cv2.rectangle(roi2, (x,y), (x+w,y+h), (0,255,0), 2)
    cv2.circle(roi2, (x+w/2, y+h/2), 2, (0, 255, 0), -1)

    cv2.imshow('roi2', roi2)

Result – our cockroach is found and circled:
found a cockroach

Now let’s write a sketch for the CraftDuino controller, which receives control commands via the serial port and issues the appropriate commands to control the motors (forward-backward, right-left, stop).
The commands correspond to the standard gamer motion keys: ‘w’, ‘s’, ‘a’, ‘d’ and ‘ ‘ – space to stop.


void loop()
    // read message from serial
    int c = 0;
    if(Serial.available()) {
        c = Serial.read();
        if(c == 'w') {
            motor_drive(0, SPEED);
            motor_drive(1, SPEED);
        else if(c == 's') {
            motor_drive(0, -SPEED);
            motor_drive(1, -SPEED);
        else if(c == 'a') {
            motor_drive(0, SPEED);
            motor_drive(1, -SPEED);
        else if(c == 'd') {
            motor_drive(0, -SPEED);
            motor_drive(1, SPEED);
        else if(c == ' ') {
            motor_drive(0, 0);
            motor_drive(1, 0);

Thus, if the sketch reads the character ‘w’ from the serial port, then the “forward” command is issued to the motors, and so on.
It remains to adapt the Python script to work not with a picture, but with a video camera and add interaction with CraftDuino via a serial port using the module serial.

The final script – tracks the cockroach and issues commands to the serial port:

 prev_x = -1
    prev_y = -1

    while True:
        ret, frame = cap.read()
        #cv2.imwrite("frame.png", frame)
        x,y = find_roach(frame)
        print("x: {0} y: {1}".format(x, y))
        if x != -1 and y != -1:
            if prev_x == -1 and prev_y == -1:
                prev_x = x
                prev_y = y
            if abs(prev_x - x) > 2 or abs(prev_y - y) > 2 :
                if x > prev_x :
                elif prev_x > x :
                if y > prev_y :
                elif prev_y > y:
                ser.write(" ")
            prev_x = x
            prev_y = y

        ch = cv2.waitKey(150)
        if ch == 27:

To detect in which direction our driver cockroach is moving, the script remembers where it was detected on the last frame and, thus, determines in which direction it has moved.

As a result, we got such a wonderful exoskeleton for a cockroach:
exoskeleton for cockroachexoskeleton for cockroachexoskeleton for cockroach

Making and programming this sort of bio-mechanical cyber-thing was a lot of fun.
Not a single cockroach was harmed during the manufacture and testing of the exoskeleton.

After some time, the cockroach, or rather, as it turned out – the cockroach – even gave birth to offspring.
cockroach giving birth

Here is the happy ending.


project on github roach_driver

Information about the cockroach:

Blaberus craniifer
Blaberus craniifer – a species of South American cockroach of the family. Blaberidae of the genus Blaberus.
Name “Dead Head” received because of the pattern located on the pronotum, which resembles scull.
The habitat is the tropics of Central and South America. Lives in the forest floor.
In the daytime, the insect hides in fallen leaves or near the roots of trees.
Actively avoid light. They feed mainly on leaf litter.
It has wings that allow the cockroach to glide.
Conditions of detention: temperature + 28 … 39 degrees Celsius, air humidity from 60 to 70%.
Lives at home from 1 to 2.5 years.
It is mainly acquired as an ornamental and fodder insect (for food for spiders, lizards, chameleons, iguanas, snakes, etc.).
Used on cockroach races.
One of the largest cockroaches: the length of an adult is 60-75 mm (large females sometimes up to 80 mm).
Females differ from males in the fused last segments of the abdomen on the underside (a characteristic feature of the family).
Viviparous (oviparous), one female brings up to 30 nymphs. The ootheca is incubated for 60 to 90 days (depending on temperature).
The larva is wide, flat, 6-7 mm long. After birth, they burrow into the substrate and spend most of their time there.
Larvae go through 9 to 13 developmental stages. They shed while sitting on a vertical surface. The time of transformation of a nymph into an adult insect takes 4-5 months.

Blaberus craniifer

one. Brian R Tietz (2012) Models of Cockroach Shelter Seeking Implemented on a Robotic Test Platform (PDF)

Exoskeleton for a cockroach

Leave a Reply

Your email address will not be published.

Scroll to top