Skip to content
项目
群组
代码片段
帮助
当前项目
正在载入...
登录 / 注册
切换导航面板
R
RoboPLC
项目
项目
详情
活动
周期分析
仓库
仓库
文件
提交
分支
标签
贡献者
图表
比较
统计图
议题
0
议题
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
CI / CD
CI / CD
流水线
作业
日程
统计图
Wiki
Wiki
代码片段
代码片段
成员
成员
折叠边栏
关闭边栏
活动
图像
聊天
创建新问题
作业
提交
问题看板
Open sidebar
黄新宇
RoboPLC
Commits
766bc301
提交
766bc301
authored
7月 03, 2024
作者:
Serhij S
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
controller shared variables no longer under RwLock by default
上级
75e96170
显示空白字符变更
内嵌
并排
正在显示
4 个修改的文件
包含
24 行增加
和
15 行删除
+24
-15
eapi.rs
examples/eapi.rs
+10
-5
modbus-master.rs
examples/modbus-master.rs
+5
-2
modbus-slave.rs
examples/modbus-slave.rs
+4
-2
controller.rs
src/controller.rs
+5
-6
没有找到文件。
examples/eapi.rs
浏览文件 @
766bc301
...
...
@@ -5,7 +5,10 @@ use roboplc::{
time
::
interval
,
};
use
serde
::
Deserialize
;
use
std
::
sync
::
Arc
;
use
std
::
sync
::{
atomic
::{
AtomicBool
,
Ordering
},
Arc
,
};
use
tracing
::
info
;
#[derive(Clone,
Debug)]
...
...
@@ -18,7 +21,7 @@ struct Env {
#[derive(Default)]
struct
Variables
{
fan
:
b
ool
,
fan
:
AtomicB
ool
,
}
#[derive(DataPolicy,
Clone)]
...
...
@@ -51,8 +54,10 @@ impl Worker<Message, Variables> for Worker1 {
pressure
:
temp
as
f64
/
3.0
,
},
)
?
;
self
.eapi
.state_push
(
oid
.clone
(),
u8
::
from
(
context
.variables
()
.read
()
.fan
))
?
;
self
.eapi
.state_push
(
oid
.clone
(),
u8
::
from
(
context
.variables
()
.fan
.load
(
Ordering
::
Acquire
)),
)
?
;
//self.eapi.dobj_error(dobj_name.clone())?;
if
!
context
.is_online
()
{
break
;
...
...
@@ -83,7 +88,7 @@ fn main() -> std::result::Result<(), Box<dyn std::error::Error>> {
.action_handler
(
"unit:tests/fan"
.parse
()
.unwrap
(),
|
action
,
context
|
{
let
params
=
action
.take_unit_params
()
?
;
let
val
=
u8
::
deserialize
(
params
.value
)
?
;
context
.variables
()
.
write
()
.fan
=
val
!=
0
;
context
.variables
()
.
fan
.store
(
val
!=
0
,
Ordering
::
Release
)
;
Ok
(())
});
// this creates a connector instance with the name `fieldbus.HOSTNAME.plc`. To use a custom
...
...
examples/modbus-master.rs
浏览文件 @
766bc301
use
roboplc
::
locking
::
Mutex
;
use
roboplc
::{
comm
::{
tcp
,
Client
},
time
::
interval
,
...
...
@@ -11,9 +12,11 @@ const MODBUS_TIMEOUT: Duration = Duration::from_secs(1);
// Do not make any decision if the sensor is older than this
const
ENV_DATA_TTL
:
Duration
=
Duration
::
from_millis
(
1
);
type
Variables
=
Mutex
<
VariablesData
>
;
// A shared traditional PLC context, does not used for logic here but provided as an example
#[derive(Default)]
struct
Variables
{
struct
Variables
Data
{
temperature
:
f32
,
}
...
...
@@ -66,7 +69,7 @@ impl Worker<Message, Variables> for ModbusPuller1 {
for
_
in
interval
(
Duration
::
from_millis
(
500
))
{
match
self
.sensor_mapping.read
::
<
EnvironmentSensors
>
()
{
Ok
(
v
)
=>
{
context
.variables
()
.
write
()
.temperature
=
v
.temperature
;
context
.variables
()
.
lock
()
.temperature
=
v
.temperature
;
hub
.send
(
Message
::
EnvSensorData
(
TtlCell
::
new_with_value
(
ENV_DATA_TTL
,
v
,
...
...
examples/modbus-slave.rs
浏览文件 @
766bc301
use
roboplc
::
comm
::
Protocol
;
use
roboplc
::
io
::
modbus
::
prelude
::
*
;
use
roboplc
::
locking
::
Mutex
;
use
roboplc
::{
prelude
::
*
,
time
::
interval
};
use
tracing
::
info
;
...
...
@@ -35,10 +36,11 @@ struct Input {
// This example does not use controller's data hub
type
Message
=
();
type
Variables
=
Mutex
<
VariableData
>
;
// Controller's shared variables
#[derive(Default)]
struct
Variable
s
{
struct
Variable
Data
{
data
:
Data
,
relays
:
Relays
,
input
:
Input
,
...
...
@@ -58,7 +60,7 @@ struct Worker1 {
impl
Worker
<
Message
,
Variables
>
for
Worker1
{
fn
run
(
&
mut
self
,
context
:
&
Context
<
Message
,
Variables
>
)
->
WResult
{
for
_
in
interval
(
Duration
::
from_secs
(
2
))
{
let
mut
vars
=
context
.variables
()
.
write
();
let
mut
vars
=
context
.variables
()
.
lock
();
vars
.data.counter
+=
1
;
vars
.relays.relay1
=
u8
::
from
(
vars
.data.counter
%
2
==
0
);
vars
.relays.relay2
=
u8
::
from
(
vars
.data.counter
%
2
!=
0
);
...
...
src/controller.rs
浏览文件 @
766bc301
...
...
@@ -15,7 +15,6 @@ use crate::{
thread_rt
::{
Builder
,
RTParams
,
Scheduling
},
Error
,
Result
,
};
use
parking_lot_rt
::
RwLock
;
pub
use
roboplc_derive
::
WorkerOpts
;
use
rtsc
::
data_policy
::
DataDeliveryPolicy
;
use
signal_hook
::{
...
...
@@ -106,7 +105,7 @@ where
supervisor
:
Supervisor
<
()
>
,
hub
:
Hub
<
D
>
,
state
:
State
,
variables
:
Arc
<
RwLock
<
V
>
>
,
variables
:
Arc
<
V
>
,
}
impl
<
D
,
V
>
Controller
<
D
,
V
>
...
...
@@ -132,7 +131,7 @@ where
supervisor
:
<
_
>
::
default
(),
hub
:
<
_
>
::
default
(),
state
:
State
::
new
(),
variables
:
Arc
::
new
(
RwLock
::
new
(
variables
)
),
variables
:
Arc
::
new
(
variables
),
}
}
/// Spawns a worker
...
...
@@ -280,7 +279,7 @@ where
&
self
.supervisor
}
/// Controller shared variables
pub
fn
variables
(
&
self
)
->
&
Arc
<
RwLock
<
V
>
>
{
pub
fn
variables
(
&
self
)
->
&
Arc
<
V
>
{
&
self
.variables
}
}
...
...
@@ -304,7 +303,7 @@ where
{
hub
:
Hub
<
D
>
,
state
:
State
,
variables
:
Arc
<
RwLock
<
V
>
>
,
variables
:
Arc
<
V
>
,
}
impl
<
D
,
V
>
Clone
for
Context
<
D
,
V
>
...
...
@@ -331,7 +330,7 @@ where
&
self
.hub
}
/// Controller's shared variables (locked)
pub
fn
variables
(
&
self
)
->
&
Arc
<
RwLock
<
V
>
>
{
pub
fn
variables
(
&
self
)
->
&
Arc
<
V
>
{
&
self
.variables
}
/// Controller's state
...
...
编写
预览
Markdown
格式
0%
重试
或
添加新文件
添加附件
取消
您添加了
0
人
到此讨论。请谨慎行事。
请先完成此评论的编辑!
取消
请
注册
或者
登录
后发表评论