第102章 开发一个设备控制 SAI
第101章解决的是:
Individual 如何感知环境。
第102章进一步解决:
Individual 如何控制一个真实设备,并根据设备返回的实际状态继续运行。
本章重点不是“让 SAI 直接控制设备”,而是建立清晰的内部结构:
Device
→ Device State
→ Action
→ Behavior
→ Command
→ Adapter
→ Physical Device
→ Device Response
→ Feedback
→ SAI
仍然采用 PHP OOP、对象、属性、状态、方法、规则和离散逻辑,不引入任何大模型机制。
1. Device
1.1 Device 定义
Device 是 SAI 可以识别、描述和控制的设备对象。
例如:
Motor_A
Fan_A
Light_A
Pump_A
Robot_A
Machine_A
在 SAI 内部,Device 不是物理设备本身,而是对物理设备的结构化表示。
Physical Device
↓
Device
↓
Identity
Property
State
Method
Ability
Command
Response
1.2 Device 基本结构
$device = array(
'id' => 'Motor_A',
'name' => 'Main Motor',
'type' => 'motor',
'state' => 'READY',
'properties' => array(
'speed' => 0,
'temperature' => 65
),
'abilities' => array(
'START',
'STOP',
'SET_SPEED'
)
);
这里:
Device
├── Identity
├── State
├── Properties
├── Methods
├── Abilities
└── Relations
1.3 Device Ability
Ability 描述:
设备能够做什么。
例如:
Motor_A
├── START
├── STOP
├── SET_SPEED
└── GET_STATUS
但 Ability 并不是当前正在执行的 Action。
例如:
Ability:
SET_SPEED
只是说明:
Motor_A 有设置速度的能力。
而:
Action:
SET_SPEED = 800
才表示当前真正要执行的操作。
2. Device State
2.1 Device State 定义
Device State 表示设备当前实际状态。
常见状态:
OFF
INITIALIZING
READY
RUNNING
PAUSED
STOPPING
STOPPED
ERROR
MAINTENANCE
DISCONNECTED
UNKNOWN
例如:
Motor_A.state = RUNNING
表示设备当前正在运行。
2.2 Device State 与 Property
必须区分:
State = 设备处于什么状态
Property = 设备有什么属性以及属性值
例如:
Motor_A.state = RUNNING
Motor_A.speed = 1200
Motor_A.temperature = 72
Motor_A.load = 65
其中:
RUNNING → State
1200 → speed Property
72 → temperature Property
65 → load Property
2.3 Device State Transition
设备状态不是任意改变的。
例如:
OFF
↓
INITIALIZING
↓
READY
↓
RUNNING
↓
STOPPING
↓
STOPPED
如果:
RUNNING → OFF
通常不是一个合法的直接转换。
需要:
RUNNING
↓
STOPPING
↓
STOPPED
↓
OFF
因此设备控制必须考虑 State Machine。
2.4 状态检查
在执行 Action 前:
Action = START
必须检查:
Device exists TRUE
Device state READY
Ability START TRUE
Risk acceptable
Condition TRUE
只有条件满足:
Action → Command
3. Adapter
3.1 Adapter 定义
Adapter 是 SAI 与真实设备之间的连接组件。
核心关系:
SAI
↓
Adapter
↓
External Device
Adapter 不负责:
- Cognition
- Reasoning
- Decision
Adapter 的职责是:
把 SAI 的 Action/Command 转换成设备能够接收的实际通信形式,并把设备响应带回 SAI。
3.2 Adapter 的作用
例如:
Action
SET_SPEED = 800
Adapter 可以转换成:
Device Command
device_id = Motor_A
command = SET_SPEED
speed = 800
然后通过实际协议发送:
Adapter
↓
HTTP
TCP
Serial
Modbus
CAN
GPIO
PLC
具体采用哪一种取决于设备。
3.3 Adapter 结构
class DeviceAdapter
{
protected $state = 'DISCONNECTED';
public function connect($device)
{
$this->state = 'CONNECTED';
return true;
}
public function send($command)
{
if ($this->state !== 'CONNECTED') {
return false;
}
return true;
}
public function receive()
{
return null;
}
public function disconnect()
{
$this->state = 'DISCONNECTED';
}
public function getState()
{
return $this->state;
}
}
3.4 Adapter 不做 Decision
错误结构:
Decision
↓
Adapter决定是否启动
正确结构:
Cognition
↓
Reasoning
↓
Decision
↓
Behavior
↓
Action
↓
Command
↓
Adapter
↓
Device
Adapter 只负责连接和转换。
4. Action
4.1 Action 定义
Action 是已经确定要执行的具体操作。
例如:
START
STOP
SET_SPEED
SET_TEMPERATURE
OPEN
CLOSE
RESET
Action 结构:
$action = array(
'id' => 'Action_001',
'type' => 'SET_SPEED',
'target' => 'Motor_A',
'parameters' => array(
'speed' => 800
),
'state' => 'READY'
);
4.2 Action 与 Decision
两者不能混淆。
Decision:
选择降低 Motor_A 速度
Action:
SET_SPEED = 800
所以:
Decision 是选择,Action 是具体操作。
4.3 Action 执行前检查
Action
↓
Device Exists?
↓
Ability Exists?
↓
Device State Allowed?
↓
Parameter Valid?
↓
Risk Acceptable?
↓
READY
例如:
Motor_A.state = ERROR
此时:
SET_SPEED
不能直接执行。
应该返回:
Action = REJECTED
Reason = DEVICE_ERROR
5. Behavior
5.1 Behavior 定义
Behavior 是多个 Action 组成的完整执行过程。
例如:
Behavior:
Start Motor
可以包含:
Action 1:
CHECK_STATE
Action 2:
START
Action 3:
READ_STATUS
Action 4:
VERIFY
因此:
Behavior
├── Action
├── Action
├── Action
└── Action
5.2 Behavior 与 Action
Behavior = 怎么完成一个行为过程
Action = 具体执行什么操作
例如:
Behavior:
降低机器速度
Actions:
1. CHECK_STATE
2. SET_SPEED 800
3. READ_SPEED
4. VERIFY
5.3 Behavior State
NEW
READY
RUNNING
WAITING
PAUSED
SUCCESS
FAILED
CANCELLED
TIMEOUT
CONFLICT
UNKNOWN
例如:
Behavior:
SET MOTOR SPEED
NEW
↓
READY
↓
RUNNING
↓
WAITING
↓
SUCCESS
6. Command
6.1 Command 定义
Command 是发送给设备或设备控制系统的具体设备指令。
关系:
Decision
↓
Behavior
↓
Action
↓
Command
例如:
Action:
SET_SPEED
speed = 800
形成:
$command = array(
'id' => 'Command_001',
'device_id' => 'Motor_A',
'type' => 'SET_SPEED',
'parameters' => array(
'speed' => 800
),
'timestamp' => time(),
'state' => 'READY'
);
6.2 Action 与 Command
两者非常接近,但职责不同。
Action
SAI 内部的操作对象:
SET_SPEED
target = Motor_A
speed = 800
Command
准备发送到设备环境的指令:
device_id = Motor_A
command = SET_SPEED
speed = 800
因此:
Action 属于 SAI 执行结构,Command 属于设备通信结构。
6.3 Command 不等于 Device Response
Command:
SET_SPEED 800
表示:
要求设备设置到 800。
而:
Device Response:
speed = 800
state = RUNNING
表示:
设备实际返回了什么。
所以:
Command ≠ Response
7. Device Response
7.1 Device Response 定义
Device Response 是设备收到 Command 后返回的实际结果。
例如:
Command:
Motor_A
SET_SPEED
800 RPM
设备返回:
Motor_A
state = RUNNING
speed = 800
temperature = 84
形成:
$response = array(
'id' => 'Response_001',
'device_id' => 'Motor_A',
'command_id' => 'Command_001',
'state' => 'SUCCESS',
'output' => array(
'speed' => 800,
'device_state' => 'RUNNING',
'temperature' => 84
),
'timestamp' => time()
);
7.2 Response 状态
可以使用:
SUCCESS
FAILED
REJECTED
TIMEOUT
ERROR
UNKNOWN
例如:
Command = SET_SPEED 800
Response:
SUCCESS
actual_speed = 800
或者:
Response:
FAILED
reason = MOTOR_ERROR
7.3 请求成功不等于目标成功
这是设备控制中非常重要的一点。
例如:
Command sent = SUCCESS
只能说明:
指令成功发送。
但是:
Motor speed = 800
可能仍然没有实现。
因此需要:
Command
↓
Response
↓
Feedback
↓
Verification
确认实际状态。
8. Feedback
8.1 Feedback 定义
Feedback 是设备执行之后返回给 Individual 的实际环境信息。
例如:
Command:
SET_SPEED 800
设备返回:
speed = 800
temperature = 84
state = RUNNING
形成:
Feedback
然后:
Feedback
↓
Information
↓
Perception
↓
Cognition
8.2 Feedback 更新 Device Object
原来的 Device:
Motor_A
state = RUNNING
speed = 1200
temperature = 92
执行:
SET_SPEED = 800
反馈:
actual_speed = 800
temperature = 84
于是内部 Device Object 更新:
Motor_A
state = RUNNING
speed = 800
temperature = 84
8.3 Feedback 与 Experience
Feedback 还可以进入:
ExperienceMemory
例如:
Condition:
temperature = 92
load = 82
Decision:
REDUCE_SPEED
Action:
SET_SPEED = 800
Result:
SUCCESS
Feedback:
temperature = 84
speed = 800
形成一次完整 Experience。
9. 完整控制流程
现在把本章全部连接起来。
External Environment
│
↓
Sensor
│
↓
Information
│
↓
Scene
│
↓
Perception
│
↓
Cognition
│
↓
Reasoning
│
↓
Decision
│
↓
Behavior
│
↓
Action
│
↓
Command
│
↓
Adapter
│
↓
Physical Device
│
↓
Device Response
│
↓
Feedback
│
┌────────────┼────────────┐
↓ ↓ ↓
Information Memory Experience
│
↓
Perception
│
↓
Scene
10. 完整控制实例
建立一个:
Motor_A
初始状态:
state = RUNNING
speed = 1200
temperature = 92
load = 82
10.1 Sensor
传感器获取:
temperature = 92
load = 82
speed = 1200
10.2 Perception
形成:
Motor_A
属性:
temperature = 92
load = 82
speed = 1200
状态:
RUNNING
10.3 Cognition
Individual 理解:
Motor_A is running
temperature is high
load is high
speed is high
10.4 Reasoning
规则:
IF
temperature > 85
AND
load > 80
THEN
REDUCE_SPEED
计算:
92 > 85 → TRUE
82 > 80 → TRUE
得到:
Conclusion:
Motor_A requires speed reduction
10.5 Decision
候选:
CONTINUE
REDUCE_SPEED
STOP
选择:
REDUCE_SPEED
10.6 Behavior
创建:
Behavior:
REDUCE_MOTOR_SPEED
Actions:
CHECK_STATE
SET_SPEED
READ_STATUS
VERIFY
10.7 Action
具体操作:
Action:
SET_SPEED
target:
Motor_A
speed:
800
10.8 Command
形成:
Command:
device_id = Motor_A
type = SET_SPEED
speed = 800
10.9 Adapter
Adapter:
connect(Motor_A)
send(Command)
receive()
把 Command 转换为设备实际能够接收的通信形式。
10.10 Device
真实设备执行:
Motor_A
1200 RPM
↓
1000 RPM
↓
800 RPM
同时:
temperature
92℃
↓
88℃
↓
84℃
10.11 Device Response
设备返回:
state = RUNNING
speed = 800
temperature = 84
result = SUCCESS
10.12 Feedback
形成:
Feedback:
Motor_A.speed
1200 → 800
Motor_A.temperature
92 → 84
Motor_A.state
RUNNING → RUNNING
然后重新进入:
Information
↓
Perception
↓
Scene
↓
Cognition
10.13 新一轮认知
新的 Scene:
Motor_A
speed = 800
temperature = 84
load = 82
state = RUNNING
原规则:
temperature > 85
现在:
84 > 85 = FALSE
因此不再执行:
REDUCE_SPEED
Individual 得到新的环境认知:
Motor_A temperature is within current rule threshold
系统进入等待下一次信息。
11. 设备控制 SAI 的完整闭环
最终形成:
┌─────────────────────────────┐
│ External Device │
└──────────────┬──────────────┘
│
Device Response
│
↓
Feedback
│
↓
Information
│
↓
Scene
│
↓
Perception
│
↓
Cognition
│
↓
Reasoning
│
↓
Decision
│
↓
Behavior
│
↓
Action
│
↓
Command
│
↓
Adapter
│
↓
External Device
这个闭环最关键的地方是:
SAI 不是发出 Command 后就结束,而是必须等待 Device Response,并把实际结果重新作为 Feedback 进入下一轮感知。
本章核心区别
| 结构 | 核心作用 |
|---|---|
| Device | 表示设备 |
| Device State | 表示设备当前状态 |
| Adapter | 连接 SAI 与设备环境 |
| Action | SAI 要执行的具体操作 |
| Behavior | 组织完整执行过程 |
| Command | 发给设备的具体指令 |
| Device Response | 设备返回的实际结果 |
| Feedback | 将实际结果重新带回 SAI |
最终可以浓缩为:
Device = 控制谁
State = 当前怎样
Decision = 决定做什么
Behavior = 如何组织执行
Action = 执行什么
Command = 告诉设备做什么
Adapter = 如何连接设备
Response = 设备实际返回什么
Feedback = 把实际结果带回 Individual
因此,第102章建立了一个完整的设备控制 SAI:
感知设备
↓
理解设备
↓
推理设备状态
↓
决定控制目标
↓
组织行为
↓
执行 Action
↓
生成 Command
↓
Adapter 连接设备
↓
设备执行
↓
Device Response
↓
Feedback
↓
重新感知设备
这使第101章的环境感知 SAI进一步进入第102章的环境控制 SAI。