← Back
engineering /

Firebot, a depth-aware firefighting robot on a cloud wire

A robotics course project that grew from a fire detector into a small robot that drives toward flames, steers around obstacles and blows them out with a fan. Two YOLOv8 models, a depth transformer, Firebase as the wire and an ESP32 that gets the last word.

On a black background, a small white arrow on the left marks the robot. A wedge of faint grey points fans out from it like a top-down depth scan. Bright white arcs show the near faces of obstacles, each casting an empty shadow behind it. A dotted orange path weaves between them to a glowing orange fire on the right, framed by orange corner brackets labelled FIRE 0.91.

Firebot started as a robotics course project at UPN Veteran Jawa Timur. I started from an existing two-wheeled robot, the BNU v2, and set out to give it a new job. Firefighting. Find a flame, drive to it, stop at a safe distance and put it out with a 12 V fan.

This post goes through how it’s put together, from the fire detector to the firmware rule on the robot that ended up mattering more than I expected. The code is on GitHub.

The system

The robot is not smart. It’s an ESP32, two DC motors, an infrared flame sensor, a relay and a fan. All the thinking happens on a laptop with a GPU, which watches a camera and decides one of four things every frame: stop, forward, left or right.

The two talk through a Firebase Realtime Database. The laptop writes a single integer to /firebot/command, and the ESP32 reads it twice a second. That’s the whole protocol.

The system. On the laptop, each webcam frame goes to three models in parallel. DPT with a SwinV2-tiny backbone predicts a dense depth map, stock YOLOv8n finds obstacles with COCO class IDs 0 to 79, and the fine-tuned YOLOv8n finds fire and smoke as IDs 80 and 81. A fusion step takes the median depth inside each box, turns it into a distance score and sorts everything closest first. Four priority rules run in order, first match wins. Avoid a near obstacle, target the closest fire, explore if the centre is clear, otherwise halt. The command, 0 to 3, is written to Firebase Realtime Database at /firebot/command when it changes or every second. A manual remote writes to the /REMOTE paths. The ESP32 polls Firebase every 500 ms, reads its IR flame sensor smoothed with an EMA, overrides the laptop near the fire, and drives two DC motors and a relay for a 12 V fan.
The whole system · everything left of Firebase runs on the laptop, every frame. Orange is the fire path. The grey manual remote is only listened to when /REMOTE/AUTO is off.

Firebase sounds like an odd choice for a robot, and it is. I used it because nothing had to be configured on a network. The laptop and the robot only need internet, not each other’s IP addresses, and a manual remote control could read and write the same paths. The cost is latency, which I’ll come back to.

Step one, a fire detector

I started in April with the part everything else depends on. A YOLOv8 detector fine-tuned on the FireSmokeDetection dataset from Roboflow, trained on a free Kaggle GPU.

The dataset had three classes, fire, Fire and smoke, because two annotators had disagreed about capital letters. I merged the two fires and remapped them to class IDs 80 and 81, right after COCO’s 80 classes. My thinking was that the model could keep detecting people and chairs and add fire and smoke on top.

That’s not what happens. The dataset has no labels for the 80 COCO classes, so the detection head gets resized to 82 outputs and learns that classes 0 to 79 never appear. You get a fire and smoke detector with 80 dead slots. I only understood this later, and it’s why the final system runs two YOLO models instead of one.

I trained two candidates on the same split, 492 validation images with 657 boxes.

Model Epochs Training Inference Precision Recall mAP50 mAP50-95
YOLOv8s 20 0.6 h 4.9 ms 0.913 0.859 0.927 0.557
YOLOv8n 200 3.9 h 2.4 ms 0.940 0.919 0.952 0.606

The nano model with ten times more epochs beat the small one, and it runs in half the time, 2.4 ms per image against 4.9 ms on the same Tesla T4. It took longer to train, 3.9 hours against 0.6, but training happens once and inference happens every frame. Per class, fire landed at 0.953 mAP50 and smoke at 0.951. Fire is the easier class by mAP50-95 (0.651 against 0.562), which makes sense. Smoke has no edges.

On the small model, F1 peaked at 0.88 around a confidence of 0.38, so I set the runtime threshold to 0.4 and kept it for the nano model. The trained weights are on Kaggle if you want to try them.

Step two, steering by thirds

The first navigation script in May was simple. Split the frame into thirds. If the most confident fire is in the left third, turn left. Right third, turn right. Middle, go forward. Nothing, stop.

It worked on a video of a campfire and was useless anywhere else. It had no idea how far anything was, so it would happily drive into a chair leg on its way to a candle, and it never knew when to stop.

Step three, adding depth

The obvious fix is a depth sensor. I didn’t have one, and the chassis had no room for one anyway. So depth had to come from the same webcam.

I first tried Mask2Former to segment people as obstacles. A mask tells you where a person is, but still nothing about how far away. Then I switched to monocular depth with DPT on a SwinV2 tiny backbone, Intel/dpt-swinv2-tiny-256 from Hugging Face. It predicts a dense depth map from a single RGB frame and is small enough to run alongside two YOLO models on a laptop GPU.

The navigation view · detections with their distance score on the left, the DPT depth map in the middle and a top-down map on the right. The yellow line at the bottom is the command being sent.

Each frame now goes through three models.

  1. Depth. DPT predicts a depth map, which gets upsampled back to the camera resolution.
  2. Obstacles. Stock yolov8n.pt finds people, chairs, bottles and the other COCO classes.
  3. Fire and smoke. The fine-tuned model from April finds classes 80 and 81.

This is where the 80 and 81 IDs paid off after all. Both models’ boxes go into one list with no class collisions, so the rest of the code doesn’t care which model saw what.

For every box, I take the median depth inside it and turn that into a distance score.

d=kmedian⁡(i,j)∈BDij,k=25000d = \frac{k}{\operatorname{median}_{(i,j) \in B} D_{ij}}, \qquad k = 25000

Here DD is the depth map DPT predicts, one value per pixel, BB is the set of pixels inside the bounding box, and kk is a constant. The result dd is the number the rules work with.

The median rather than the mean because boxes are rectangles and objects aren’t. A box around a bottle is half background, and if the background is a far wall, its values would drag a mean far off. The median only cares about which value sits in the middle, so as long as the object fills a good part of the box, it ignores the wall.

In code it’s three lines.

depth_roi = depth_map[y1:y2, x1:x2]
raw_depth = np.median(depth_roi)
distance_metric = 25000 / raw_depth

A word of honesty about kk. DPT predicts relative inverse depth, not meters. Bigger values mean closer, roughly like 1/z1/z for a true distance zz, but with a scale and offset that shift from scene to scene. Dividing a constant by it flips it back, so a bigger dd means farther. If DPT’s output were exactly c/zc/z for some unknown cc, then dd would be (k/c) z(k/c)\,z, the real distance times an unknown factor. It isn’t exact, so dd is only roughly proportional to distance, and I picked kk by hand so that the numbers landed in a readable range. The overlay prints dd with an “m” on the end, but it isn’t meters. It’s a consistent-enough ordering, which turned out to be all the rules needed.

The rules

All detections are sorted by that distance score, closest first, and then four rules run in order. The first one that fires wins.

Priority When Command
1. Avoid The closest obstacle is nearer than the safe threshold (20) Turn away
2. Target A fire is visible Steer toward it
3. Explore No fire, and the middle third has no obstacle Forward
4. Halt Anything else Stop

Targeting steers by thirds toward the closest fire, and stops once that fire is inside the threshold too. Avoidance beats targeting on purpose. A robot that knocks over a person on its way to a fire has made things worse. Smoke is detected and drawn but never steered toward, because smoke drifts, and following it in a room mostly leads to the ceiling.

The top-down map places every detection on a bird’s-eye view. Each object needs two coordinates there, how far ahead it is and how far to the side. How far ahead is just dd. How far to the side comes from the pinhole camera model.

f=W/2tan⁡(θ/2),x=(u−W/2) dff = \frac{W/2}{\tan(\theta/2)}, \qquad x = \frac{(u - W/2)\, d}{f}

WW is the frame width in pixels, θ=65°\theta = 65° is the camera’s horizontal field of view, uu is the horizontal centre of the box in pixels and xx is the sideways offset, negative to the left. The first equation finds the focal length ff in pixels. Half the frame spans half the field of view, so tan⁡(θ/2)=(W/2)/f\tan(\theta/2) = (W/2)/f. The second is similar triangles. A point u−W/2u - W/2 pixels off centre on an image plane ff pixels away lies xx off centre at distance dd, so x/d=(u−W/2)/fx/d = (u - W/2)/f.

As an example, a 640-pixel-wide frame gives f=320/tan⁡32.5°≈502f = 320 / \tan 32.5° \approx 502 pixels. A box centred at u=480u = 480, with d=30d = 30, lands at x=160×30/502≈9.6x = 160 \times 30 / 502 \approx 9.6 to the right. Since dd isn’t in meters, neither is xx, but both share the same scale, so the map’s shape is right even if its units aren’t.

The map isn’t used for decisions. It’s there so a human can see at a glance what the robot thinks is around it, which made tuning the threshold much faster.

Sending commands without blocking

The Firebase Admin SDK’s set is a blocking HTTP call, and waiting on it every frame stalls the whole pipeline. The main loop now runs under asyncio and hands each write to a thread pool.

async def send_command_to_firebase_async(command_code, command_path):
    loop = asyncio.get_event_loop()
    ref = db.reference(command_path)
    await loop.run_in_executor(None, ref.set, command_code)

The loop fires the task and moves on. It also only writes when the command changes, or once a second as a heartbeat, so Firebase isn’t flooded with thirty identical “forward” writes a second. On exit, a finally block always sends one last stop. A robot whose controller crashes while it’s driving forward should not keep driving forward.

Every number in this post, from the confidence threshold and the frame thirds to the safe distance, the field of view and the heartbeat interval, lives in one config.yaml. Tuning meant editing a text file and restarting, never touching code.

The robot gets the last word

The firmware on the ESP32 has two modes, switched by /REMOTE/AUTO. In manual mode it follows remote commands from /REMOTE/COMMAND, plus a manual fan switch. In auto mode it follows the laptop, but not blindly. In both modes it also writes its own flame reading back to /MIKROKONTROLER/FIREVALUE and /MIKROKONTROLER/FIRESTATUS every 500 ms, so the sensor can be watched live from the same database.

The robot has an infrared flame sensor on the front. Its analog output, read by the ESP32’s 12-bit ADC as a number from 0 to 4095, goes down as the infrared it sees goes up, so a lower reading means a closer flame. Raw readings jump around, so the firmware smooths them with an exponential moving average.

vt=α rt+(1−α) vt−1,α=0.5v_t = \alpha\, r_t + (1 - \alpha)\, v_{t-1}, \qquad \alpha = 0.5

rtr_t is the new raw reading, vt−1v_{t-1} is the previous smoothed value and vtv_t is the new one. With α=0.5\alpha = 0.5, the newest reading counts for half, the one before for a quarter, the one before that for an eighth, and so on. A single spike gets halved straight away and has mostly faded after a few readings, while a real change still shows up within a second or two at one reading every 500 ms. A smaller α\alpha would be smoother but slower to react.

In auto mode that smoothed reading overrides the laptop.

Flame sensor reading What the robot does
No command, or stop Stop, fan off
Below 1, fire almost touching Back up, fan on
1 to 150, close enough Stop and run the fan
150 to 350, approaching Follow the laptop, fan on
Above 350, far or none Follow the laptop, fan off

The fan stays on while the robot backs away, so it keeps blowing at the flame as it makes room. The relay is active-low, so the ESP32 pulls its pin low to turn the fan on.

This split ended up being the most important design decision in the project. The camera is good at finding a fire across a room and bad at judging the last 20 cm, where monocular depth is noisiest and the flame washes out the image. The IR sensor is useless at range and very reliable up close. So the laptop handles the approach and the robot handles the arrival.

It also covers for the network. The ESP32 polls Firebase every 500 ms, on top of two network round trips, so a motor can react up to a second or more after the frame that caused it. At that delay you can’t stop on time by vision alone. Whatever the laptop says, the robot won’t drive into the fire.

What I’d do differently

A few things, looking back.

  • Train the fire model on two classes. Remapping IDs at inference gives the same result without 80 dead outputs.
  • Calibrate the depth. A handful of measured distances would turn the score into something close to meters.
  • Take Firebase out of the loop. It was the easiest way to connect everything, but a local WebSocket or MQTT link that pushes commands would cut the delay to tens of milliseconds.
  • Give it a short memory. Every frame is judged from scratch, so the robot twitches whenever a detection drops out for a frame.

It’s a rough robot, but every part does its job, from a fire detector trained on a free GPU to a fan on a relay. For a course project, I’m happy with that.

Credits

Keep reading

All posts ↗
ESC
↑↓ navigate ↵ open