Skip to content

Commit

Permalink
feat: Implement an examplary 6 axis robot
Browse files Browse the repository at this point in the history
which provides its current measure positions as array in ns=1;100111.
  • Loading branch information
AiyionPrime authored and philipp-caspers committed Jul 31, 2024
1 parent 89f3bbb commit 4ad68d7
Show file tree
Hide file tree
Showing 2 changed files with 82 additions and 0 deletions.
4 changes: 4 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -44,3 +44,7 @@ path = "examples/ros2/simple-publisher.rs"
[[example]]
name = "simple-subscriber"
path = "examples/ros2/simple-subscriber.rs"

[[example]]
name = "sine-joint-positions"
path = "examples/opc_ua/sine-joint-positions.rs"
78 changes: 78 additions & 0 deletions examples/opc_ua/sine-joint-positions.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
use opcua::server::prelude::{Config, DateTime, NodeId, Server, ServerConfig, Variable};
use std::f64::consts::PI;
use std::path::PathBuf;
use std::time::SystemTime;
use std::time::UNIX_EPOCH;

fn main() {
sine_joint_positions();
}

fn sine_joint_positions() {
opcua::console_logging::init();

let mut server =
Server::new(ServerConfig::load(&PathBuf::from("tests/resources/clock.conf")).unwrap());

add_timed_variable_array(&mut server);

server.run();
}

fn add_timed_variable_array(server: &mut Server) {
// These will be the node id of the variable AxesMeasuredPosition
let axes_measured_position = NodeId::new(1, "100111");

let address_space = server.address_space();

// The address space is guarded so obtain a lock to change it
{
let mut address_space = address_space.write();

let axes_folder_id = address_space
.add_folder("Axes", "Axes", &NodeId::objects_folder_id())
.unwrap();

let _ = address_space.add_variables(
vec![Variable::new(
&axes_measured_position,
"AxesMeasuredPosition",
"AxesMeasuredPosition",
vec![0.0f64; 6],
)],
&axes_folder_id,
);
}

{
let omegas: [f64; 6] = [
2.0 * PI * 0.25,
2.0 * PI * 0.5,
2.0 * PI,
2.0 * PI * 1.5,
2.0 * PI * 2.0,
2.0 * PI * 2.5,
];

server.add_polling_action(10, move || {
let mut address_space = address_space.write();

let timestamp_epoch = SystemTime::now().duration_since(UNIX_EPOCH).unwrap();
let seconds =
timestamp_epoch.as_secs() as f64 + timestamp_epoch.subsec_nanos() as f64 * 1e-9;

let sine_values: Vec<f64> = omegas
.iter()
.map(|&omega| (omega * seconds).sin())
.collect();

let now = DateTime::now();
let _ = address_space.set_variable_value(
axes_measured_position.clone(),
sine_values,
&now,
&now,
);
});
}
}

0 comments on commit 4ad68d7

Please sign in to comment.