第104章 开发一个机器人 SAI
第103章建立了 Web SAI,解决的是:
浏览器如何通过 Web Controller 与 Individual 进行信息交互。
第104章进一步把 Individual 放入机器人环境。
机器人 SAI 不只是“控制机器人移动”,而是形成:
Robot
↓
Sensor
↓
Information
↓
Scene
↓
Perception
↓
Cognition
↓
Memory
↓
Reasoning
↓
Decision
↓
Behavior
↓
RobotAdapter
↓
Robot
↓
Feedback
↓
Learning
本章的核心目标是:
让 Individual 能够感知机器人周围的环境,理解环境,记忆过去状态,根据规则和经验进行推理和决策,组织机器人行为,通过 RobotAdapter 控制机器人,再根据反馈形成经验并学习。
整个结构仍然采用对象、属性、状态、关系、规则、方法、离散计算和 PHP OOP 结构,不依赖大模型机制。
1. Robot Object
1.1 Robot Object 定义
Robot Object 是 SAI 内部对真实机器人的结构化表示。
它不是物理机器人本身。
Physical Robot
↓
Sensor / Adapter
↓
Robot Object
Robot Object 可以描述:
Identity
State
Properties
Position
Motion
Sensors
Methods
Abilities
Relations
1.2 Robot Object 示例
$robot = array(
'id' => 'Robot_A',
'name' => 'Mobile Robot A',
'type' => 'mobile_robot',
'state' => 'MOVING',
'properties' => array(
'speed' => 1.2,
'battery' => 76
),
'position' => array(
'x' => 10,
'y' => 5
),
'abilities' => array(
'MOVE',
'STOP',
'TURN_LEFT',
'TURN_RIGHT'
)
);
1.3 Robot Object 的层次
Robot_A
│
├── Identity
├── State
├── Properties
├── Position
├── Motion
├── Sensors
├── Methods
├── Abilities
└── Relations
例如:
State:
MOVING
Property:
battery = 76%
Position:
x = 10
y = 5
Motion:
speed = 1.2 m/s
direction = NORTH
2. Sensor
2.1 Sensor 定义
Sensor 是机器人获取外部环境信息的来源。
例如:
DistanceSensor
CameraSensor
TemperatureSensor
PositionSensor
SpeedSensor
BatterySensor
Sensor 的基本职责:
获取数据。
Sensor 本身不负责:
理解
推理
决策
行为
2.2 Distance Sensor
例如:
$distanceSensor = array(
'id' => 'DistanceSensor_A',
'type' => 'distance',
'state' => 'ACTIVE',
'unit' => 'meter',
'value' => 0.8
);
得到:
distance = 0.8m
这只是原始信息。
还不能直接得出:
STOP
2.3 Sensor 与 Perception
两者关系:
Sensor
↓
获取数据
↓
Information
↓
Perception
↓
识别结构
因此:
Sensor 是信息来源,Perception 是信息结构化过程。
3. Scene
3.1 Scene 定义
Scene 是 Robot SAI 对机器人当前环境进行结构化组织后的场景。
例如机器人当前看到:
Robot_A
Obstacle_A
Road_A
Wall_A
可以形成:
Scene_A
包含:
Objects
Properties
States
Relations
Position
Environment
Time
3.2 Robot Scene
例如:
$scene = array(
'id' => 'Scene_A',
'objects' => array(
'Robot_A',
'Obstacle_A',
'Wall_A'
),
'robot' => array(
'speed' => 1.2,
'direction' => 'NORTH'
),
'obstacles' => array(
array(
'id' => 'Obstacle_A',
'distance' => 0.8,
'direction' => 'FRONT'
)
),
'time' => time()
);
3.3 Scene 的作用
没有 Scene:
distance = 0.8
speed = 1.2
只是两个独立的数据。
形成 Scene 后:
Robot_A
↓
front
↓
Obstacle_A
distance = 0.8m
speed = 1.2m/s
Individual 才能理解:
机器人正在以一定速度接近前方障碍物。
4. Information
Sensor 获取的数据进入 Individual 后形成 Information。
例如:
$information = array(
'source' => 'DistanceSensor_A',
'type' => 'DISTANCE',
'target' => 'Obstacle_A',
'data' => array(
'distance' => 0.8,
'direction' => 'FRONT'
),
'timestamp' => time()
);
同时 SpeedSensor:
$information2 = array(
'source' => 'SpeedSensor_A',
'type' => 'SPEED',
'target' => 'Robot_A',
'data' => array(
'speed' => 1.2
),
'timestamp' => time()
);
Individual 收到:
Information_1
Information_2
4.1 Information 的来源
机器人 SAI 的 Information 可以来自:
Sensor
Camera
RobotAdapter
External System
Operator
Internal State
Feedback
所以:
Information 是进入 Individual 的信息结构,而不是 Sensor 本身。
5. Perception
5.1 Perception
Perception 将 Sensor Information 转换成:
Element
Object
Property
State
Relation
例如:
Sensor:
distance = 0.8m
direction = FRONT
经过 Perception:
Object:
Obstacle_A
Property:
distance = 0.8m
Relation:
Obstacle_A
↓
FRONT_OF
↓
Robot_A
5.2 PerceptionResult
$perception = array(
'objects' => array(
'Robot_A',
'Obstacle_A'
),
'properties' => array(
'Robot_A.speed' => 1.2,
'Obstacle_A.distance' => 0.8
),
'relations' => array(
array(
'source' => 'Robot_A',
'type' => 'FRONT_OF',
'target' => 'Obstacle_A'
)
),
'state' => 'PERCEIVED'
);
5.3 Perception 不做 Decision
Perception 得到:
Obstacle_A distance = 0.8m
但不能直接:
STOP
正确路径:
Perception
↓
Cognition
↓
Reasoning
↓
Decision
6. Cognition
6.1 Cognition 定义
Cognition 对 Perception 结果进行理解。
当前:
Robot_A.speed = 1.2m/s
Obstacle_A.distance = 0.8m
Obstacle_A = FRONT
Cognition 可以形成:
Robot_A is moving
Obstacle_A is in front
Obstacle_A is very close
Robot_A is approaching obstacle
6.2 Relative Motion
如果连续 Scene:
t1:
distance = 1.2m
t2:
distance = 1.0m
t3:
distance = 0.8m
Individual 可以计算:
distance:
1.2 → 1.0 → 0.8
因此:
distance_decreasing = TRUE
进一步形成:
approaching = TRUE
这是离散状态计算。
6.3 Cognition Result
$cognition = array(
'robot' => 'Robot_A',
'understanding' => array(
'moving' => true,
'obstacle_front' => true,
'obstacle_distance' => 0.8,
'approaching' => true
),
'state' => 'UNDERSTOOD'
);
7. Memory
机器人不能只依赖当前 Scene。
它还需要记住:
过去的位置
过去的障碍物
过去的行为
过去的结果
过去的反馈
7.1 ShortMemory
ShortMemory 保存当前任务和当前环境连续信息。
例如:
t1 distance = 1.2
t2 distance = 1.0
t3 distance = 0.8
ShortMemory:
$shortMemory = array(
'robot' => 'Robot_A',
'scene_history' => array(
array('distance' => 1.2),
array('distance' => 1.0),
array('distance' => 0.8)
)
);
7.2 LongMemory
LongMemory 保存长期事实和经验。
例如:
Fact:
Obstacle distance < 1.0 requires caution
以及:
Experience:
When obstacle was close,
STOP was safe.
7.3 Memory 的作用
Robot SAI:
Current Scene
+
ShortMemory
+
LongMemory
共同提供:
当前理解
历史事实
历史经验
给 Reasoning 使用。
8. Reasoning
8.1 Reasoning
Reasoning 使用:
Scene
Cognition
Memory
Fact
Rule
Experience
进行离散逻辑计算。
例如:
Robot speed = 1.2m/s
Obstacle distance = 0.8m
Obstacle is FRONT
Approaching = TRUE
规则:
IF
obstacle_distance < 1.0
AND
obstacle_direction = FRONT
AND
approaching = TRUE
THEN
risk = HIGH
8.2 条件计算
0.8 < 1.0
TRUE
FRONT = FRONT
TRUE
approaching = TRUE
TRUE
得到:
risk = HIGH
8.3 Reasoning Result
$reasoning = array(
'conclusion' => array(
'target' => 'Robot_A',
'property' => 'risk',
'value' => 'HIGH'
),
'state' => 'CONFIRMED',
'reasoning_chain' => array(
'distance < 1.0',
'obstacle in front',
'approaching = true',
'risk = HIGH'
)
);
9. Decision
9.1 Decision 候选
当风险为 HIGH:
CONTINUE
SLOW_DOWN
STOP
TURN_LEFT
TURN_RIGHT
Individual 不能简单地:
HIGH → STOP
而应该检查:
Condition
Risk
Priority
Ability
Device State
Environment
Experience
9.2 Decision 示例
当前:
Obstacle distance = 0.8m
Risk = HIGH
Robot moving
候选:
CONTINUE
SLOW_DOWN
STOP
TURN_LEFT
TURN_RIGHT
如果没有足够空间转向:
TURN_LEFT = unavailable
TURN_RIGHT = unavailable
如果减速仍不足以降低风险:
SLOW_DOWN = insufficient
最终:
Decision = STOP
9.3 Decision Result
$decision = array(
'target' => 'Robot_A',
'candidates' => array(
'CONTINUE',
'SLOW_DOWN',
'STOP',
'TURN_LEFT',
'TURN_RIGHT'
),
'selected' => 'STOP',
'state' => 'DECIDED',
'reason' => 'FRONT_OBSTACLE_HIGH_RISK'
);
10. Behavior
Decision:
STOP
并不代表机器人已经停止。
需要 Behavior 组织执行过程。
10.1 Stop Behavior
Behavior:
SAFE_STOP
可以包含:
1. CHECK_ROBOT_STATE
2. REDUCE_SPEED
3. STOP_MOTOR
4. READ_ROBOT_STATE
5. VERIFY
10.2 Behavior 结构
$behavior = array(
'id' => 'Behavior_001',
'type' => 'SAFE_STOP',
'target' => 'Robot_A',
'actions' => array(
'CHECK_STATE',
'REDUCE_SPEED',
'STOP',
'READ_STATE',
'VERIFY'
),
'state' => 'RUNNING'
);
10.3 Behavior 与 Action
Decision:
STOP
Behavior:
SAFE_STOP
Action:
SET_SPEED = 0
所以:
Decision = 选择停止
Behavior = 组织安全停止过程
Action = SET_SPEED 0
三者不能混淆。
11. RobotAdapter
11.1 RobotAdapter 定义
RobotAdapter 是 Individual 与真实机器人控制系统之间的连接层。
Individual
↓
Action
↓
Command
↓
RobotAdapter
↓
Robot Control System
↓
Physical Robot
11.2 RobotAdapter 的职责
RobotAdapter 负责:
连接
转换
发送
接收
断开
例如:
class RobotAdapter
{
protected $state = 'DISCONNECTED';
public function connect($robot)
{
$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';
}
}
11.3 Action 转 Command
例如:
Action:
SET_SPEED
speed = 0
形成:
$command = array(
'id' => 'Command_001',
'robot_id' => 'Robot_A',
'type' => 'SET_SPEED',
'parameters' => array(
'speed' => 0
),
'timestamp' => time()
);
RobotAdapter 将 Command 转换成机器人控制系统实际使用的形式。
11.4 RobotAdapter 不做 Decision
错误:
RobotAdapter
↓
发现障碍
↓
决定 STOP
正确:
Perception
↓
Cognition
↓
Reasoning
↓
Decision
↓
Behavior
↓
Action
↓
Command
↓
RobotAdapter
12. Feedback
12.1 Robot Response
机器人执行 Command 后返回:
Robot_A
speed = 0
state = STOPPED
形成:
$response = array(
'robot_id' => 'Robot_A',
'command_id' => 'Command_001',
'state' => 'SUCCESS',
'output' => array(
'speed' => 0,
'robot_state' => 'STOPPED'
),
'timestamp' => time()
);
12.2 Feedback
Response 进入:
Feedback
例如:
$feedback = array(
'source' => 'Robot_A',
'type' => 'ACTION_RESULT',
'state' => 'SUCCESS',
'changes' => array(
'speed' => '1.2 → 0',
'state' => 'MOVING → STOPPED'
)
);
12.3 Feedback 重新进入感知
关键闭环:
Robot
↓
Response
↓
Feedback
↓
Information
↓
Perception
↓
Scene
↓
Cognition
机器人不能因为收到:
Command SUCCESS
就认为:
Robot stopped
必须重新确认真实状态。
例如 Sensor 返回:
speed = 0
state = STOPPED
才可以确认:
STOP verified
13. Learning
机器人完成一次行为以后,不能只结束当前任务。
需要保存:
Condition
Decision
Behavior
Action
Response
Feedback
Result
形成 Experience。
13.1 Experience
例如:
$experience = array(
'condition' => array(
'obstacle_distance' => 0.8,
'obstacle_direction' => 'FRONT',
'robot_speed' => 1.2
),
'decision' => 'STOP',
'behavior' => 'SAFE_STOP',
'action' => 'SET_SPEED_0',
'result' => 'SUCCESS',
'feedback' => array(
'robot_state' => 'STOPPED'
),
'outcome' => 'SAFE'
);
13.2 Learning
Learning 从 Experience 中获取知识。
例如多次经历:
Experience 1:
distance = 0.8
STOP
SUCCESS
Experience 2:
distance = 0.7
STOP
SUCCESS
Experience 3:
distance = 0.9
STOP
SUCCESS
可以形成结构化知识:
Knowledge:
When:
front obstacle
distance < 1.0
approaching = TRUE
Action:
STOP
Historical result:
SAFE
13.3 Learning 不直接修改 Decision
Learning 得到知识以后:
Experience
↓
Learning
↓
Knowledge
↓
Memory Update
下一次运行:
New Information
↓
Perception
↓
Cognition
↓
Memory
↓
Reasoning
↓
Decision
新的知识作为 Decision 的资源,而不是绕过 Decision 直接控制机器人。
14. 完整机器人 SAI
现在将本章所有结构连接起来。
Physical Robot
│
┌──────┴──────┐
↓ ↓
Sensor Robot State
│ │
└──────┬──────┘
↓
Information
↓
Scene
↓
Perception
↓
Objects
↓
Relations
↓
Cognition
↓
Memory
↓
Reasoning
↓
Decision
↓
Behavior
↓
Action
↓
Command
↓
RobotAdapter
↓
Physical Robot
↓
Robot Response
↓
Feedback
↓
Information
│
└────────→ Scene
同时:
Feedback
↓
Experience
↓
Learning
↓
Knowledge
↓
Memory Update
↓
下一轮 Reasoning
15. 完整运行实例
假设:
Robot_A
当前:
state = MOVING
speed = 1.2 m/s
前方:
Obstacle_A
distance = 0.8m
第一步:Sensor
DistanceSensor_A
→ 0.8m
SpeedSensor_A
→ 1.2m/s
第二步:Information
Distance Information
Speed Information
进入 Individual。
第三步:Scene
形成:
Scene_A
Robot_A
speed = 1.2
state = MOVING
Obstacle_A
distance = 0.8
direction = FRONT
第四步:Perception
识别:
Robot_A
Obstacle_A
建立:
Robot_A
↓
FRONT_OF
↓
Obstacle_A
第五步:Cognition
形成:
Robot moving
Obstacle front
Obstacle close
Distance decreasing
Approaching = TRUE
第六步:Memory
读取历史:
similar obstacle conditions
过去经验:
STOP → SAFE
第七步:Reasoning
规则:
distance < 1.0
AND
front obstacle
AND
approaching
结果:
risk = HIGH
第八步:Decision
候选:
CONTINUE
SLOW_DOWN
STOP
TURN_LEFT
TURN_RIGHT
最终:
STOP
第九步:Behavior
SAFE_STOP
包含:
CHECK_STATE
REDUCE_SPEED
STOP
READ_STATE
VERIFY
第十步:Action
SET_SPEED = 0
第十一步:Command
Robot_A
SET_SPEED
0
第十二步:RobotAdapter
Action
↓
Command
↓
RobotAdapter
↓
Robot Control
第十三步:Robot
机器人:
MOVING
↓
STOPPING
↓
STOPPED
第十四步:Response
返回:
state = STOPPED
speed = 0
result = SUCCESS
第十五步:Feedback
形成:
Robot_A.speed
1.2 → 0
Robot_A.state
MOVING → STOPPED
第十六步:重新感知
Sensor 再次确认:
speed = 0
state = STOPPED
形成新的 Scene:
Scene_B
第十七步:Learning
保存:
Condition
→ Decision
→ Behavior
→ Action
→ Result
→ Feedback
形成 Experience。
Learning 从 Experience 中获得:
Knowledge
更新:
Memory
于是下一次遇到类似环境时,历史知识可以参与 Reasoning。
17. 机器人 SAI 完整生命周期
本章最终形成:
Robot Object
↓
Sensor
↓
Information
↓
Scene
↓
Perception
↓
Object + Property + State + Relation
↓
Cognition
↓
Memory
↓
Reasoning
↓
Decision
↓
Behavior
↓
Action
↓
Command
↓
RobotAdapter
↓
Physical Robot
↓
Robot Response
↓
Feedback
↓
Information
↓
Scene
学习路径:
Behavior
↓
Action
↓
Result
↓
Feedback
↓
Experience
↓
Learning
↓
Knowledge
↓
Memory Update
最终闭环:
┌──────────────────────┐
│ Physical World │
└──────────┬───────────┘
│
Sensor
│
↓
Information
↓
Scene
↓
Perception
↓
Cognition
↕
Memory
↓
Reasoning
↓
Decision
↓
Behavior
↓
Action
↓
Command
↓
RobotAdapter
↓
Robot Control
↓
Physical Robot
│
↓
Feedback
│
┌──────────┴──────────┐
↓ ↓
Information Experience
↓
Learning
↓
Knowledge
↓
Memory Update
本章核心定义
Robot Object
Individual 对真实机器人进行结构化表示后的内部对象。
Sensor
获取机器人自身状态和外部环境信息的信息来源。
Scene
Individual 对机器人当前环境、对象、属性、状态、位置和关系进行组织后的场景结构。
Information
Sensor、RobotAdapter、外部系统和 Feedback 等来源进入 Individual 的结构化信息。
Perception
将 Information 转换为 Element、Object、Property、State、Relation 的过程。
Cognition
对机器人自身状态和外部环境结构进行理解的过程。
Memory
保存当前场景连续信息、事实、关系、状态和历史经验的机制。
Reasoning
根据当前认知、Memory、Fact、Rule 和 Experience 推导结论。
Decision
根据推理结果、条件、风险、优先级、能力和经验选择机器人下一步行为。
Behavior
对机器人目标行为进行组织和执行的过程。
RobotAdapter
连接 Individual 与真实机器人控制系统,并负责 Command 的转换、发送和接收。
Feedback
机器人实际执行后的状态、结果、变化和错误重新返回 Individual 的信息。
Learning
从机器人历史 Experience 中获取可复用 Knowledge,并更新 Memory 和相关能力资源。
本章最终模型
机器人世界
↓
Sensor
↓
Information
↓
Scene
↓
Perception
↓
Object / Relation / State
↓
Cognition
↓
Memory
↓
Reasoning
↓
Decision
↓
Behavior
↓
Action
↓
Command
↓
RobotAdapter
↓
Robot
↓
Response
↓
Feedback
↓
Experience
↓
Learning
↓
Knowledge
↓
Memory Update
↓
重新感知
因此,第104章完成了从**“设备控制 SAI”向“机器人闭环 SAI”**的进一步扩展:
机器人不再只是接受一个控制命令,而是成为 Individual 感知、认知、记忆、推理、决策、行为、反馈和学习的完整运行对象。