Skip to content

Commit 445b2c7

Browse files
committed
example simplified
1 parent a8fb16a commit 445b2c7

File tree

3 files changed

+118
-87
lines changed

3 files changed

+118
-87
lines changed
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
use std::{
2+
error::Error,
3+
f32::consts::PI,
4+
thread,
5+
time::{Duration, SystemTime},
6+
};
7+
8+
use poulpe_ethercat_grpc::PoulpeRemoteClient;
9+
10+
use poulpe_ethercat_controller::state_machine::{
11+
parse_homing_error_flags, parse_motor_error_flags, parse_state_from_status_word,
12+
parse_status_word, CiA402State,
13+
};
14+
15+
// takes the salve id as argument
16+
// and moves the motor in a sinusoidal motion
17+
fn main() -> Result<(), Box<dyn Error>> {
18+
env_logger::init();
19+
20+
// args
21+
let args: Vec<String> = std::env::args().collect();
22+
if args.len() != 2 {
23+
log::error!("Usage:\n{} <id> \nor \n{} slave_name", args[0], args[0]);
24+
return Err("Invalid number of arguments".into());
25+
}
26+
27+
// first element is the nema or the id
28+
let client = if let Ok(id) = args[1].parse::<u16>() {
29+
log::info!("Connecting to the slave with id: {}", id);
30+
PoulpeRemoteClient::connect(
31+
"http://127.0.0.1:50098".parse()?,
32+
vec![id],
33+
Duration::from_secs_f32(0.001),
34+
)
35+
} else {
36+
let name = args[1].clone();
37+
log::info!("Connecting to the slave with name: {}", name);
38+
PoulpeRemoteClient::connect_with_name(
39+
"http://127.0.0.1:50098".parse()?,
40+
vec![name],
41+
Duration::from_secs_f32(0.001),
42+
)
43+
}.map_err(|e| {
44+
log::error!("Failed to connect to the server: {}", e);
45+
e
46+
})?;
47+
48+
let id = id.unwrap();
49+
let name = name.unwrap();
50+
log::info!("Slave id: {}", id);
51+
log::info!("Slave name: {}", name);
52+
53+
thread::sleep(Duration::from_secs(1));
54+
55+
let state = client.get_cia402_state(id).unwrap();
56+
let cia_state: CiA402State = parse_state_from_status_word(state as u16);
57+
let status_bits = parse_status_word(state as u16);
58+
log::info!(
59+
"{}: Board State: {:?}\nStatus bits [{:?}]",
60+
name,
61+
cia_state,
62+
status_bits
63+
);
64+
let error_codes = client.get_error_codes(id).unwrap();
65+
let homing_error_flags = parse_homing_error_flags((error_codes[0] as u16).to_le_bytes());
66+
log::info!("{}: homing Error flags: {:?}", name, homing_error_flags);
67+
for (i, e) in error_codes.iter().enumerate().skip(1) {
68+
let motor_error = parse_motor_error_flags((*e as u16).to_le_bytes());
69+
log::info!("{}: motor {} | Error flags: {:?}", name, i, motor_error);
70+
}
71+
Ok(())
72+
}

poulpe_ethercat_grpc/examples/client_listener.rs

Lines changed: 27 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -18,52 +18,35 @@ fn main() -> Result<(), Box<dyn Error>> {
1818
log::error!("Usage:\n{} <id> \nor \n{} slave_name", args[0], args[0]);
1919
return Err("Invalid number of arguments".into());
2020
}
21-
22-
// if the first argument is a number, it is the slave id
23-
// otherwise it is the slave name
24-
let mut id: Option<u16> = None;
25-
let mut name = None;
26-
match args[1].parse::<u16>() {
27-
Ok(i) => id = Some(i),
28-
Err(_) => name = Some(args[1].clone()),
21+
22+
// args
23+
let args: Vec<String> = std::env::args().collect();
24+
if args.len() != 2 {
25+
log::error!("Usage:\n{} <id> \nor \n{} slave_name", args[0], args[0]);
26+
return Err("Invalid number of arguments".into());
2927
}
28+
29+
// first element is the nema or the id
30+
let client = if let Ok(id) = args[1].parse::<u16>() {
31+
log::info!("Connecting to the slave with id: {}", id);
32+
PoulpeRemoteClient::connect(
33+
"http://127.0.0.1:50098".parse()?,
34+
vec![id],
35+
Duration::from_secs_f32(0.001),
36+
)
37+
} else {
38+
let name = args[1].clone();
39+
log::info!("Connecting to the slave with name: {}", name);
40+
PoulpeRemoteClient::connect_with_name(
41+
"http://127.0.0.1:50098".parse()?,
42+
vec![name],
43+
Duration::from_secs_f32(0.001),
44+
)
45+
}.map_err(|e| {
46+
log::error!("Failed to connect to the server: {}", e);
47+
e
48+
})?;
3049

31-
let mut client = match (id, name) {
32-
(Some(id), None) => {
33-
log::info!("Connecting to the slave with id: {}", id);
34-
let client = match PoulpeRemoteClient::connect(
35-
"http://127.0.0.1:50098".parse()?,
36-
vec![id],
37-
Duration::from_secs_f32(0.001),
38-
) {
39-
Ok(client) => client,
40-
Err(e) => {
41-
log::error!("Failed to connect to the server: {}", e);
42-
return Err(e.into());
43-
}
44-
};
45-
client
46-
}
47-
(None, Some(name)) => {
48-
log::info!("Connecting to the slave with name: {}", name);
49-
let client = match PoulpeRemoteClient::connect_with_name(
50-
"http://127.0.0.1:50098".parse()?,
51-
vec![name],
52-
Duration::from_secs_f32(0.001),
53-
) {
54-
Ok(client) => client,
55-
Err(e) => {
56-
log::error!("Failed to connect to the server: {}", e);
57-
return Err(e.into());
58-
}
59-
};
60-
client
61-
}
62-
_ => {
63-
log::error!("Usage:\n{} <id> \nor \n{} slave_name", args[0], args[0]);
64-
return Err("Invalid number of arguments".into());
65-
}
66-
};
6750
let id = client.ids[0];
6851
let name = client.names[0].clone();
6952
log::info!("Slave id: {}", id);

poulpe_ethercat_grpc/examples/client_sinus.rs

Lines changed: 19 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -22,49 +22,25 @@ fn main() -> Result<(), Box<dyn Error>> {
2222

2323
// if the first argument is a number, it is the slave id
2424
// otherwise it is the slave name
25-
let mut id: Option<u16> = None;
26-
let mut name = None;
27-
match args[1].parse::<u16>() {
28-
Ok(i) => id = Some(i),
29-
Err(_) => name = Some(args[1].clone()),
30-
}
31-
32-
let mut client = match (id, name) {
33-
(Some(id), None) => {
34-
log::info!("Connecting to the slave with id: {}", id);
35-
let client = match PoulpeRemoteClient::connect(
36-
"http://127.0.0.1:50098".parse()?,
37-
vec![id],
38-
Duration::from_secs_f32(0.001),
39-
) {
40-
Ok(client) => client,
41-
Err(e) => {
42-
log::error!("Failed to connect to the server: {}", e);
43-
return Err(e.into());
44-
}
45-
};
46-
client
47-
}
48-
(None, Some(name)) => {
49-
log::info!("Connecting to the slave with name: {}", name);
50-
let client = match PoulpeRemoteClient::connect_with_name(
51-
"http://127.0.0.1:50098".parse()?,
52-
vec![name],
53-
Duration::from_secs_f32(0.001),
54-
) {
55-
Ok(client) => client,
56-
Err(e) => {
57-
log::error!("Failed to connect to the server: {}", e);
58-
return Err(e.into());
59-
}
60-
};
61-
client
62-
}
63-
_ => {
64-
log::error!("Usage:\n{} <id> \nor \n{} slave_name", args[0], args[0]);
65-
return Err("Invalid number of arguments".into());
66-
}
67-
};
25+
let mut client = if let Ok(id) = args[1].parse::<u16>() {
26+
log::info!("Connecting to the slave with id: {}", id);
27+
PoulpeRemoteClient::connect(
28+
"http://127.0.0.1:50098".parse()?,
29+
vec![id],
30+
Duration::from_secs_f32(0.001),
31+
)
32+
} else {
33+
let name = args[1].clone();
34+
log::info!("Connecting to the slave with name: {}", name);
35+
PoulpeRemoteClient::connect_with_name(
36+
"http://127.0.0.1:50098".parse()?,
37+
vec![name],
38+
Duration::from_secs_f32(0.001),
39+
)
40+
}.map_err(|e| {
41+
log::error!("Failed to connect to the server: {}", e);
42+
e
43+
})?;
6844

6945
let id = client.ids[0];
7046
let name = client.names[0].clone();

0 commit comments

Comments
 (0)