Alen Širbegović/ DEVSPACE
←Alle Projekte

ESP32 / BLE / C++

B0B

Vom Semesterprojekt zur nachvollziehbaren Robotersteuerung: BLE-Fernbedienung, Liniensperre und eine am Rechner getestete Zustandslogik.

Weiterentwicklung

Kleine Steuerung, klare Zustände.

01

Freigabe und Verbindung

Keine Fahrt bei ausgeschalteter Freigabe oder gemeldetem Verbindungsabbruch. Erneutes Starten verlangt OFF und neutrale Regler.

02

Liniensperre

Hysterese gegen flackernde Sensorwerte und eine verriegelte Stoppreaktion. Automatische Rückfahrt ist standardmäßig aus.

03

Prüfbarer Kern

20 native C++-Testszenarien prüfen Eingabegrenzen, Wiederanlauf und Zeitüberlauf. Das ersetzt keinen Test am echten Roboter.

Live-Dateivorschau

Geprüfter Quellcode und englische Dokumentation zum direkten Lesen.

firmware/b0b/controller.h
C++
#ifndef B0B_CONTROLLER_H
#define B0B_CONTROLLER_H
#include <cstdint>

namespace b0b {
enum class Mode { Disconnected, Disabled, AwaitRelease, AwaitNeutral, Ready, Driving, LineStop, Recovering, SensorFault, ConfigFault };
struct Config {
  std::uint16_t lineOn = 3500;
  std::uint16_t lineOff = 3300;
  std::uint8_t deadband = 5;
  std::uint8_t maxPwm = 180;
  std::uint8_t recoveryPwm = 0; // No autonomous movement unless explicitly enabled.
  std::uint16_t stopMs = 100;
  std::uint16_t recoveryMs = 350;
};
struct Input {
  bool connected = false;
  bool enabled = false;
  std::int16_t left = 0;
  std::int16_t right = 0;
  std::uint16_t sensorLeft = 0;
  std::uint16_t sensorRight = 0;
  bool sensorsValid = true;
};
struct Output { std::int16_t left = 0; std::int16_t right = 0; Mode mode = Mode::Disconnected; };

class Controller {
 public:
  explicit Controller(Config config = {}) : config_(config) {}
  Output update(const Input& in, std::uint32_t now) {
    if (!validConfig()) return stopped(Mode::ConfigFault);
    if (!in.connected) {
      seenOff_ = armed_ = recovering_ = latched_ = line_ = false;
      return stopped(Mode::Disconnected);
    }
    if (!in.sensorsValid || in.sensorLeft > 4095 || in.sensorRight > 4095) {
      seenOff_ = armed_ = recovering_ = false;
      latched_ = true;
      latchMode_ = Mode::SensorFault;
      return stopped(Mode::SensorFault);
    }
    if (in.sensorLeft >= config_.lineOn || in.sensorRight >= config_.lineOn) line_ = true;
    else if (in.sensorLeft <= config_.lineOff && in.sensorRight <= config_.lineOff) line_ = false;
    if (!in.enabled) {
      seenOff_ = true;
      armed_ = recovering_ = latched_ = false;
      return stopped(Mode::Disabled);
    }
    if (recovering_) {
      const std::uint32_t elapsed = now - recoveryStarted_; // Handles millis() wraparound.
      if (!line_ || elapsed >= std::uint32_t(config_.stopMs) + config_.recoveryMs) {
        recovering_ = false;
        return stopped(Mode::LineStop);
      }
      if (elapsed < config_.stopMs) return stopped(Mode::LineStop);
      return {static_cast<std::int16_t>(-config_.recoveryPwm), static_cast<std::int16_t>(-config_.recoveryPwm), Mode::Recovering};
    }
    if (!seenOff_) return stopped(latched_ ? latchMode_ : Mode::AwaitRelease);
    if (!armed_) {
      if (line_) { seenOff_ = false; latched_ = true; latchMode_ = Mode::LineStop; return stopped(Mode::LineStop); }
      if (pwm(in.left) || pwm(in.right)) return stopped(Mode::AwaitNeutral);
      armed_ = true;
      return stopped(Mode::Ready);
    }
    if (line_) {
      armed_ = seenOff_ = false;
      latched_ = true;
      latchMode_ = Mode::LineStop;
      if (config_.recoveryPwm && (pwm(in.left) > 0 || pwm(in.right) > 0)) {
        recovering_ = true;
        recoveryStarted_ = now;
      }
      return stopped(Mode::LineStop);
    }
    const auto left = pwm(in.left), right = pwm(in.right);
    return {left, right, left || right ? Mode::Driving : Mode::Ready};
  }
 private:
  Config config_;
  bool seenOff_ = false, armed_ = false, recovering_ = false, latched_ = false, line_ = false;
  std::uint32_t recoveryStarted_ = 0;
  Mode latchMode_ = Mode::LineStop;
  static Output stopped(Mode mode) { return {0,0,mode}; }
  bool validConfig() const {
    return config_.lineOff < config_.lineOn && config_.lineOn <= 4095 && config_.deadband < 100 &&
           config_.maxPwm > 0 && config_.recoveryPwm <= 128 && config_.recoveryPwm <= config_.maxPwm &&
           config_.stopMs >= 20 && config_.stopMs <= 1000 && config_.recoveryMs > 0 && config_.recoveryMs <= 750;
  }
  std::int16_t pwm(std::int16_t raw) const {
    const int value = raw < -100 ? -100 : raw > 100 ? 100 : raw;
    if (value >= -config_.deadband && value <= config_.deadband) return 0;
    return static_cast<std::int16_t>(value * config_.maxPwm / 100);
  }
};
} // namespace b0b
#endif
Die Vorschau lädt dieselben Dateien, die im ZIP enthalten sind.UTF-8 · LF