Command Pattern៖ ជួរបញ្ជារ៉ូបូត Non-Blocking
ខ្ចប់បញ្ជា Bluetooth (F, L, R, B) ជា Object តម្រង់ជួរក្នុង Ring buffer ហើយប្រតិបត្តិម្ដងមួយដោយ millis() — ឈប់បន្ទាន់បានគ្រប់ពេល។
- បំប្លែង enum/switch FSM ទៅជា State pattern ដែលមាន enter()/update() ក្នុង Class ដាច់ៗ
- ប្រើ Observer និង Dependency Injection ភ្ជាប់សេនស័រមួយទៅ Actuator ច្រើនដោយមិនជាប់គ្នា
- តម្រង់ជួរ Command របស់រ៉ូបូតក្នុង Ring buffer ហើយប្រតិបត្តិដោយ Non-blocking និង Fail-safe
នៅពេល App ទូរស័ព្ទផ្ញើ F, L, F, B តាម Bluetooth យ៉ាងលឿន កូដសាមញ្ញដែលធ្វើ forward(); delay(1000); នឹង Block ហើយបាត់ Byte ឬមិនអាចឈប់បន្ទាន់ពេលឃើញឧបសគ្គ។ Command pattern ខ្ចប់សំណើនីមួយៗជា Object (DriveCommand មានល្បឿនឆ្វេង/ស្ដាំ និងរយៈពេល) ដែលអាចរក្សាទុក តម្រង់ជួរ កត់ត្រា ឬបោះបង់បាន ហើយបំបែក «អ្នកស្នើ» (Bluetooth parser) ពី «អ្នកប្រតិបត្តិ» (CommandRunner)។
Command ត្រូវបានរក្សាក្នុង Ring buffer ទំហំថេរ ៨ ធាតុ (FIFO)៖ push() បដិសេធពេលពេញ (ជាជាងសរសេរជាន់ Command ចាស់ដោយស្ងាត់) ហើយ pop() យកធាតុចាស់បំផុត។ DriveCommand មានទំហំ 10 bytes លើ AVR (Pointer 2 + int16_t 2 × 2 + uint32_t 4) ដូច្នេះជួរទាំងមូលប្រើតិចជាង 100 bytes នៃ SRAM 2 KB។ CommandRunner::update(nowMs) ត្រូវបានហៅរាល់ loop()៖ បើ Command បច្ចុប្បន្នមិនទាន់ដល់ពេលចប់ វាត្រឡប់ភ្លាមៗ — CPU នៅទំនេរសម្រាប់អានសេនស័រ។
សុវត្ថិភាពមុនគេ៖ emergencyStop() សម្អាតជួរទាំងមូល ហើយបញ្ឈប់ម៉ូទ័រភ្លាមៗ — បើគ្រាន់តែបញ្ឈប់ Command បច្ចុប្បន្ន រ៉ូបូតនឹងបន្តប្រតិបត្តិ Command ដែលនៅសល់ក្នុងជួរ ហើយបុកជញ្ជាំង! ដូចគ្នាដែរ Byte មិនស្គាល់ (ឧ. x ពីសំឡេងរំខាន) ត្រូវមិនអើពើ ហើយគ្រប់ Command មាន durationMs កំណត់ ដូច្នេះបើ Bluetooth ដាច់ រ៉ូបូតឈប់ដោយខ្លួនឯង (Fail-safe timeout)។
Command pattern ក៏បើកផ្លូវទៅមុខងារកម្រិតខ្ពស់ផងដែរ៖ Macro (រក្សាលំដាប់ F L F ជាកម្មវិធីដើរ), Logging (កត់ត្រា Command គ្រប់មួយសម្រាប់ Replay) និង Undo (រក្សាទុក Command ផ្ទុយ)។ ក្នុង ROS 2 គំនិតដូចគ្នានេះលេចឡើងជា Topic /cmd_vel និង Actions — Command ដែលមានរយៈពេល មាន Feedback និងអាចបោះបង់បាន — ដែលយើងនឹងស្គាល់ក្នុង Module 4។
- Command = សំណើខ្ចប់ជា Object → តម្រង់ជួរ Log ឬបោះបង់បាន
- Ring buffer ទំហំថេរ៖
push()បដិសេធពេលពេញ — កុំសរសេរជាន់ដោយស្ងាត់ - Runner ហៅរាល់
loop()និងមិន Block; E-Stop ត្រូវសម្អាតជួរទាំងមូល - Command នីមួយៗមាន
durationMs— Bluetooth ដាច់ក៏រ៉ូបូតឈប់ (Fail-safe)
// robot_commands.cpp — Command pattern: queued, non-blocking robot-car moves.
// Build & run on a PC: g++ -std=c++17 -Wall robot_commands.cpp -o commands && ./commands
#include <stdint.h>
#include <stdio.h>
// ---------------- HAL: what the runner needs from the motor driver ----------------
class IDrive {
public:
virtual void setSpeed(int16_t left, int16_t right) = 0; // -255..255 PWM
protected:
~IDrive() = default;
};
// ---------------- The command: a request packaged as an object ----------------
struct DriveCommand {
const char* label;
int16_t left;
int16_t right;
uint32_t durationMs;
};
// Translate one Bluetooth/Serial character into a command object.
bool commandFromChar(char c, DriveCommand& out) {
switch (c) {
case 'F': out = {"FORWARD", 180, 180, 1000}; return true;
case 'B': out = {"BACKWARD", -150, -150, 600}; return true;
case 'L': out = {"TURN_LEFT", -140, 140, 400}; return true;
case 'R': out = {"TURN_RIGHT", 140, -140, 400}; return true;
default: return false; // unknown byte: ignore safely
}
}
// ---------------- Fixed-size FIFO ring buffer (no heap) ----------------
template <uint8_t Capacity>
class CommandQueue {
public:
bool push(const DriveCommand& cmd) {
if (count_ == Capacity) return false; // full: reject, never overwrite
buffer_[(head_ + count_) % Capacity] = cmd;
++count_;
return true;
}
bool pop(DriveCommand& out) {
if (count_ == 0) return false;
out = buffer_[head_];
head_ = (head_ + 1) % Capacity;
--count_;
return true;
}
void clear() { head_ = count_ = 0; }
uint8_t size() const { return count_; }
private:
DriveCommand buffer_[Capacity] = {};
uint8_t head_ = 0;
uint8_t count_ = 0;
};
// ---------------- Invoker: executes one command at a time, never blocks ----------------
template <uint8_t Capacity>
class CommandRunner {
public:
CommandRunner(IDrive& drive, CommandQueue<Capacity>& queue) : drive_(drive), queue_(queue) {}
void update(uint32_t nowMs) {
if (busy_ && nowMs - startedMs_ < current_.durationMs) return; // still running
if (queue_.pop(current_)) {
busy_ = true;
startedMs_ = nowMs;
printf("[%5lu ms] start %-10s (left in queue: %u)\n", (unsigned long)nowMs, current_.label,
(unsigned)queue_.size());
drive_.setSpeed(current_.left, current_.right);
} else if (busy_) {
busy_ = false;
printf("[%5lu ms] queue empty -> stop\n", (unsigned long)nowMs);
drive_.setSpeed(0, 0);
}
}
void emergencyStop(uint32_t nowMs) {
queue_.clear();
busy_ = false;
printf("[%5lu ms] EMERGENCY STOP: queue cleared\n", (unsigned long)nowMs);
drive_.setSpeed(0, 0);
}
private:
IDrive& drive_;
CommandQueue<Capacity>& queue_;
DriveCommand current_ = {};
uint32_t startedMs_ = 0;
bool busy_ = false;
};
class PrintDrive : public IDrive {
public:
void setSpeed(int16_t left, int16_t right) override {
printf(" motors L=%4d R=%4d\n", left, right);
}
};
int main() {
PrintDrive drive;
CommandQueue<8> queue;
CommandRunner<8> runner(drive, queue);
// Bytes received from the phone app; 'x' is noise and is ignored.
const char received[] = "FLxFB";
for (const char* p = received; *p != '\0'; ++p) {
DriveCommand cmd;
if (commandFromChar(*p, cmd) && !queue.push(cmd)) printf("queue full, dropped %c\n", *p);
}
for (uint32_t t = 0; t <= 3500; t += 10) { // simulated loop() every 10 ms
runner.update(t);
// other work (sensors, telemetry) keeps running here
}
queue.push({"FORWARD", 180, 180, 1000});
runner.update(4000);
runner.emergencyStop(4300); // e.g. ultrasonic sensor saw an obstacle
runner.update(4310);
return 0;
}ការពន្យល់៖ commandFromChar() ជា Factory បំប្លែង Byte ទៅ Command; CommandQueue ជា FIFO គ្មាន Heap; CommandRunner ជា Invoker ដែលប្រតិបត្តិម្ដងមួយ។ Loop ក្លែងធ្វើរត់រៀងរាល់ 10 ms ហើយ Command ចាប់ផ្ដើមនៅ 0, 1000, 1400 និង 2400 ms តាមរយៈពេលរបស់វា — គ្មាន delay() ណាមួយទេ។
សំណួរវាស់សមត្ថភាព (Knowledge Check)
ឆ្លើយគ្រប់សំណួរ រួចចុច «ពិនិត្យចម្លើយ»។ បើត្រឹមត្រូវ ≥ ៨០% មេរៀននេះនឹងត្រូវបានកត់ត្រាថាបានបញ្ចប់ស្វ័យប្រវត្តិ។
- Command — Design Patterns refactoring.guruCommand, Invoker, Receiver និងការប្រើជាមួយ Queue/Undo។
- Game Programming Patterns — Command gameprogrammingpatterns.comការបំប្លែង Input ទៅជា Command object, Replay និង Undo។