1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
#[cfg(any(feature="usb-ftdi", feature="raspberrypi", feature="raspberrypi_cm"))]
use cands_presentation::cyphal::digitalservo::{
dictionary::{Dict, DigitalServoPrimitiveData, IntoDigitalServoDataType},
string::Str,
};
use cands_transport::cyphal::CyphalRxData;
use futures_lite::FutureExt;
use async_io::{block_on, Timer};
const CHECK_FIFO_POLLING_MS: u64 = 2;
#[cfg(any(feature="usb-ftdi", feature="raspberrypi", feature="raspberrypi_cm"))]
impl crate::CANInterface {
/// [DEPRECATED IN RELIABLE PROCESS]
///
/// It does not requires a child node replying.
///
/// In the worst case, the request would lost when a child node failed to receive a packet due to insufficient processing capacity.
///
pub fn send_digitalservo_message<T>(
&mut self,
key: &str,
value: &[T]
) -> Result<(), Box<dyn std::error::Error>>
where
T: Clone + IntoDigitalServoDataType + Into<DigitalServoPrimitiveData>
{
const SUBJECT_ID: u16 = 0x488;
let payload:Vec<u8> = Dict::serialize(key, value);
self.send_message(SUBJECT_ID, &payload)
}
#[deprecated]
/// [DEPRECATED]
///
/// It does not requires a child node replying.
///
/// In the worst case, the request would lost when a child node failed to receive a packet due to insufficient processing capacity.
///
pub fn send_digitalservo_response<T>(
&mut self,
channel: u8,
key: &str,
value: &[T]
) -> Result<(), Box<dyn std::error::Error>>
where
T: Clone + IntoDigitalServoDataType + Into<DigitalServoPrimitiveData>
{
const SERVICE_ID: u16 = 0x80;
let payload:Vec<u8> = Dict::serialize(key, &value);
self.send_response(SERVICE_ID, channel, &payload)
}
#[deprecated]
/// [DEPRECATED]
///
/// It does not requires a child node replying in a certain time
///
/// in the worst case, the request would lost when a child node failed to receive a packet due to insufficient processing capacity.
///
pub fn send_digitalservo_request(&mut self, channel: u8, key: &str) -> Result<(), Box<dyn std::error::Error>> {
const SERVICE_ID: u16 = 0x80;
let payload:Vec<u8> = Dict::serialize(key, &[0.0]);
self.send_request(SERVICE_ID, channel, &payload)
}
/// It requires a child node replying a result when it successfully receive a message.
///
/// In case of communication failure (e.g., a child node failed to receive), this function would retry to send a message.
/// If no acknowledge signal returns within the specified number of times, this function returns error.
///
/// A timeout for each trial and the limit number of retries are in Self::timeout and Self::retry_count.
/// These can be set by
/// ```
/// self.set_retry_count(retry_count);
/// self.set_timeout(timeout);
/// ```
///
pub fn send_digitalservo_set_value <T>(
&mut self,
channel: u8,
key: &str,
value: &[T],
) -> Result<(), Box<dyn std::error::Error>>
where
T: Clone + IntoDigitalServoDataType + Into<DigitalServoPrimitiveData>
{
const SERVICE_ID: u16 = 0x81;
let payload:Vec<u8> = Dict::serialize(key, &value);
let timeout = self.timeout;
for _ in 0..self.retry_count {
self.send_request(SERVICE_ID, channel, &payload)?;
let ret: Result<(), ()> = {
let task = async {
loop {
let _ = self.load_frames();
let results = match self.get_result(Some(channel)) {
Ok(ret) => ret,
Err(_) => continue,
};
if let Some(results) = results {
if results.iter().all(|y| y.data == 0) {
return Ok(())
}
}
Timer::after(std::time::Duration::from_millis(CHECK_FIFO_POLLING_MS)).await;
}
};
let timeout_handler = async {
Timer::after(timeout).await;
Err(())
};
block_on(task.or(timeout_handler))
};
if let Ok(success) = ret {
return Ok(success)
}
}
let err: std::io::Error = std::io::ErrorKind::TimedOut.into();
Err(err.into())
}
/// [DEPRECATED IN RELIABLE PROCESS]
///
/// It does not requires a child node replying in a certain time
/// (in the worst case, the request would lost when a child node failed to receive a packet due to insufficient processing capacity).
///
/// It can be used as non-blocking process.
/// Use this only in cyclic operation and get data in after the cycle.
///
pub fn send_digitalservo_get_value_request(&mut self, channel: u8, key: &str) -> Result<(), Box<dyn std::error::Error>> {
const SERVICE_ID: u16 = 0x82;
let payload:Vec<u8> = Str::serialize(key);
self.send_request(SERVICE_ID, channel, &payload)
}
/// It requires a child node replying data when it successfully receive a message.
///
/// In case of communication failure (e.g., a child node failed to receive), this function would retry to send a message.
/// If no acknowledge signal returns within the specified number of times, this function returns error.
///
/// A timeout for each trial and the limit number of retries are in Self::timeout and Self::retry_count.
/// These can be set by
/// ```
/// self.set_retry_count(retry_count);
/// self.set_timeout(timeout);
/// ```
///
pub fn send_digitalservo_get_value(
&mut self,
channel: u8,
key: &str,
) -> Result<Vec<CyphalRxData<Dict>>, Box<dyn std::error::Error>> {
const SERVICE_ID: u16 = 0x82;
let payload:Vec<u8> = Str::serialize(key);
let timeout = self.timeout;
for _ in 0..self.retry_count {
self.send_request(SERVICE_ID, channel, &payload)?;
let ret: Result<Vec<CyphalRxData<Dict>>, ()> = {
let task = async {
loop {
let results = match self.get_key_value(Some(key), Some(channel)) {
Ok(ret) => ret,
Err(_) => continue,
};
if let Some(results) = results {
return Ok(results)
}
Timer::after(std::time::Duration::from_millis(CHECK_FIFO_POLLING_MS)).await;
}
};
let timeout_handler = async {
Timer::after(timeout).await;
Err(())
};
block_on(task.or(timeout_handler))
};
if let Ok(success) = ret {
return Ok(success)
}
}
let err: std::io::Error = std::io::ErrorKind::TimedOut.into();
Err(err.into())
}
}