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
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
use super::NanonisClient;
use crate::error::NanonisError;
use crate::types::NanonisValue;
/// Slope direction for Generic PI controller.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
pub enum GenPISlope {
/// No change to current setting
#[default]
NoChange = 0,
/// Positive slope
Positive = 1,
/// Negative slope
Negative = 2,
}
impl From<GenPISlope> for u16 {
fn from(slope: GenPISlope) -> Self {
slope as u16
}
}
impl TryFrom<u16> for GenPISlope {
type Error = NanonisError;
fn try_from(value: u16) -> Result<Self, Self::Error> {
match value {
0 => Ok(GenPISlope::Negative),
1 => Ok(GenPISlope::Positive),
_ => Err(NanonisError::Protocol(format!(
"Invalid GenPISlope value: {}",
value
))),
}
}
}
/// AC mode toggle for demodulator channel.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
pub enum ACMode {
/// No change to current setting
#[default]
NoChange = 0,
/// AC mode on
On = 1,
/// AC mode off
Off = 2,
}
impl From<ACMode> for u16 {
fn from(mode: ACMode) -> Self {
mode as u16
}
}
/// Generic PI Controller properties.
#[derive(Debug, Clone, Copy, Default)]
pub struct GenPICtrlProps {
/// Setpoint value
pub setpoint: f32,
/// Proportional gain
pub p_gain: f32,
/// Time constant
pub time_constant: f32,
/// Slope direction
pub slope: GenPISlope,
}
/// Analog output properties for Generic PI controller.
#[derive(Debug, Clone)]
pub struct AOProps {
/// Signal name
pub signal_name: String,
/// Physical units
pub units: String,
/// Upper physical limit
pub upper_limit: f32,
/// Lower physical limit
pub lower_limit: f32,
/// Calibration per volt
pub calibration_per_volt: f32,
/// Offset in physical units
pub offset: f32,
}
impl Default for AOProps {
fn default() -> Self {
Self {
signal_name: String::new(),
units: String::new(),
upper_limit: 10.0,
lower_limit: -10.0,
calibration_per_volt: 1.0,
offset: 0.0,
}
}
}
impl NanonisClient {
/// Enable or disable the Generic PI Controller.
///
/// # Arguments
/// * `enabled` - True to enable, false to disable
///
/// # Errors
/// Returns `NanonisError` if communication fails.
///
/// # Examples
/// ```no_run
/// use nanonis_rs::NanonisClient;
///
/// let mut client = NanonisClient::new("127.0.0.1", 6501)?;
/// client.gen_pi_ctrl_on_off_set(true)?;
/// # Ok::<(), Box<dyn std::error::Error>>(())
/// ```
pub fn gen_pi_ctrl_on_off_set(&mut self, enabled: bool) -> Result<(), NanonisError> {
let status = if enabled { 1u32 } else { 0u32 };
self.quick_send(
"GenPICtrl.OnOffSet",
vec![NanonisValue::U32(status)],
vec!["I"],
vec![],
)?;
Ok(())
}
/// Get the on/off status of the Generic PI Controller.
///
/// # Returns
/// True if controller is enabled.
///
/// # Errors
/// Returns `NanonisError` if communication fails.
pub fn gen_pi_ctrl_on_off_get(&mut self) -> Result<bool, NanonisError> {
let result = self.quick_send("GenPICtrl.OnOffGet", vec![], vec![], vec!["I"])?;
if !result.is_empty() {
Ok(result[0].as_u32()? != 0)
} else {
Err(NanonisError::Protocol("Invalid response".to_string()))
}
}
/// Set the output signal value of the User Output controlled by the Generic PI controller.
///
/// # Arguments
/// * `output_value` - Output value
///
/// # Errors
/// Returns `NanonisError` if communication fails.
pub fn gen_pi_ctrl_ao_val_set(&mut self, output_value: f32) -> Result<(), NanonisError> {
self.quick_send(
"GenPICtrl.AOValSet",
vec![NanonisValue::F32(output_value)],
vec!["f"],
vec![],
)?;
Ok(())
}
/// Get the output signal value of the User Output controlled by the Generic PI controller.
///
/// # Returns
/// The current output value.
///
/// # Errors
/// Returns `NanonisError` if communication fails.
pub fn gen_pi_ctrl_ao_val_get(&mut self) -> Result<f32, NanonisError> {
let result = self.quick_send("GenPICtrl.AOValGet", vec![], vec![], vec!["f"])?;
if !result.is_empty() {
Ok(result[0].as_f32()?)
} else {
Err(NanonisError::Protocol("Invalid response".to_string()))
}
}
/// Set the properties of the User Output controlled by the Generic PI controller.
///
/// # Arguments
/// * `props` - An [`AOProps`] struct with analog output properties
///
/// # Errors
/// Returns `NanonisError` if communication fails.
pub fn gen_pi_ctrl_ao_props_set(&mut self, props: &AOProps) -> Result<(), NanonisError> {
self.quick_send(
"GenPICtrl.AOPropsSet",
vec![
NanonisValue::String(props.signal_name.clone()),
NanonisValue::String(props.units.clone()),
NanonisValue::F32(props.upper_limit),
NanonisValue::F32(props.lower_limit),
NanonisValue::F32(props.calibration_per_volt),
NanonisValue::F32(props.offset),
],
vec!["+*c", "+*c", "f", "f", "f", "f"],
vec![],
)?;
Ok(())
}
/// Get the properties of the User Output controlled by the Generic PI controller.
///
/// # Returns
/// An [`AOProps`] struct with current analog output properties.
///
/// # Errors
/// Returns `NanonisError` if communication fails.
pub fn gen_pi_ctrl_ao_props_get(&mut self) -> Result<AOProps, NanonisError> {
let result = self.quick_send(
"GenPICtrl.AOPropsGet",
vec![],
vec![],
vec!["i", "*-c", "i", "*-c", "f", "f", "f", "f"],
)?;
if result.len() >= 8 {
Ok(AOProps {
signal_name: result[1].as_string()?.to_string(),
units: result[3].as_string()?.to_string(),
upper_limit: result[4].as_f32()?,
lower_limit: result[5].as_f32()?,
calibration_per_volt: result[6].as_f32()?,
offset: result[7].as_f32()?,
})
} else {
Err(NanonisError::Protocol("Invalid response".to_string()))
}
}
/// Set the index of the User Output controlled by the Generic PI controller.
///
/// # Arguments
/// * `output_index` - Output index (1 to number of available outputs)
///
/// # Errors
/// Returns `NanonisError` if communication fails.
pub fn gen_pi_ctrl_mod_ch_set(&mut self, output_index: i32) -> Result<(), NanonisError> {
self.quick_send(
"GenPICtrl.ModChSet",
vec![NanonisValue::I32(output_index)],
vec!["i"],
vec![],
)?;
Ok(())
}
/// Get the index of the User Output controlled by the Generic PI controller.
///
/// # Returns
/// The output index (0 means no output selected).
///
/// # Errors
/// Returns `NanonisError` if communication fails.
pub fn gen_pi_ctrl_mod_ch_get(&mut self) -> Result<i32, NanonisError> {
let result = self.quick_send("GenPICtrl.ModChGet", vec![], vec![], vec!["i"])?;
if !result.is_empty() {
Ok(result[0].as_i32()?)
} else {
Err(NanonisError::Protocol("Invalid response".to_string()))
}
}
/// Set the index of the signal demodulated by the Generic PI controller.
///
/// # Arguments
/// * `input_index` - Input index (0-127 for signals, -1 for no change)
/// * `ac_mode` - AC mode setting
///
/// # Errors
/// Returns `NanonisError` if communication fails.
pub fn gen_pi_ctrl_demod_ch_set(
&mut self,
input_index: i32,
ac_mode: ACMode,
) -> Result<(), NanonisError> {
self.quick_send(
"GenPICtrl.DemodChSet",
vec![
NanonisValue::I32(input_index),
NanonisValue::U16(ac_mode.into()),
],
vec!["i", "H"],
vec![],
)?;
Ok(())
}
/// Get the index of the signal demodulated by the Generic PI controller.
///
/// # Returns
/// The input index.
///
/// # Errors
/// Returns `NanonisError` if communication fails.
pub fn gen_pi_ctrl_demod_ch_get(&mut self) -> Result<i32, NanonisError> {
let result = self.quick_send("GenPICtrl.DemodChGet", vec![], vec![], vec!["i"])?;
if !result.is_empty() {
Ok(result[0].as_i32()?)
} else {
Err(NanonisError::Protocol("Invalid response".to_string()))
}
}
/// Set the properties of the Generic PI controller.
///
/// # Arguments
/// * `props` - A [`GenPICtrlProps`] struct with controller properties
///
/// # Errors
/// Returns `NanonisError` if communication fails.
///
/// # Examples
/// ```no_run
/// use nanonis_rs::NanonisClient;
/// use nanonis_rs::gen_pi_ctrl::{GenPICtrlProps, GenPISlope};
///
/// let mut client = NanonisClient::new("127.0.0.1", 6501)?;
/// let props = GenPICtrlProps {
/// setpoint: 0.0,
/// p_gain: 1.0,
/// time_constant: 0.001,
/// slope: GenPISlope::Positive,
/// };
/// client.gen_pi_ctrl_props_set(&props)?;
/// # Ok::<(), Box<dyn std::error::Error>>(())
/// ```
pub fn gen_pi_ctrl_props_set(&mut self, props: &GenPICtrlProps) -> Result<(), NanonisError> {
self.quick_send(
"GenPICtrl.PropsSet",
vec![
NanonisValue::F32(props.setpoint),
NanonisValue::F32(props.p_gain),
NanonisValue::F32(props.time_constant),
NanonisValue::U16(props.slope.into()),
],
vec!["f", "f", "f", "H"],
vec![],
)?;
Ok(())
}
/// Get the properties of the Generic PI controller.
///
/// # Returns
/// A [`GenPICtrlProps`] struct with current controller properties.
///
/// # Errors
/// Returns `NanonisError` if communication fails.
pub fn gen_pi_ctrl_props_get(&mut self) -> Result<GenPICtrlProps, NanonisError> {
let result = self.quick_send(
"GenPICtrl.PropsGet",
vec![],
vec![],
vec!["f", "f", "f", "H"],
)?;
if result.len() >= 4 {
Ok(GenPICtrlProps {
setpoint: result[0].as_f32()?,
p_gain: result[1].as_f32()?,
time_constant: result[2].as_f32()?,
slope: result[3].as_u16()?.try_into()?,
})
} else {
Err(NanonisError::Protocol("Invalid response".to_string()))
}
}
}