Skip to content

Commit

Permalink
new API
Browse files Browse the repository at this point in the history
  • Loading branch information
samuel-cavalcanti committed Sep 15, 2023
2 parents e3121d0 + 610a64e commit ed24699
Show file tree
Hide file tree
Showing 17 changed files with 227 additions and 298 deletions.
5 changes: 4 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -77,8 +77,11 @@ add this crate at your cargo.toml :

```toml
# the branch is the coppelia version
zmq_remote_api = { git = "https://github.com/samuel-cavalcanti/rust_zmqRemoteApi", branch = "CoppeliaSim_4.4.0"}
zmq_remote_api = { git = "https://github.com/samuel-cavalcanti/rust_zmqRemoteApi", branch = "CoppeliaSim_4.5.1"}
```

See this simple [example](examples/get_simulation_time.rs) to understand how to use this create.
<<<<<<< HEAD

=======
>>>>>>> CoppeliaSim_4.5.1
4 changes: 2 additions & 2 deletions c_transpiler/assets/sim.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
impl Sim {
pub trait Sim: RemoteApiClientInterface {
requests! {
"sim",
(get_joint_max_force,"getJointMaxForce",(joint_handle:i64)->f64),
Expand All @@ -10,7 +10,7 @@ impl Sim {
(set_vision_sensor_char_image,"setVisionSensorCharImage",(sensor_handle:i64,image:Vec<u8>)->()),
(get_object_selection,"getObjectSelection"->Vec<i64>),
(set_object_selection,"setObjectSelection",(object_handles:Vec<f64>)->()),
(get_string_signal,"getStringSignal",(signal_name:String)->String),// changed),
(get_string_signal,"getStringSignal",(signal_name:String)->String),
(get_int32_signal,"getInt32Signal",(signal_name:String)->Option<i64>),
(get_float_signal,"getFloatSignal",(signal_name:String)->Option<f64>),
(add_drawing_object,"addDrawingObject",(object_type:i64,size:f64,duplicate_tolerance:f64,parent_object_handle:i64,max_item_count:i64),opt(color:Vec<f64>)->i64),
Expand Down
154 changes: 77 additions & 77 deletions c_transpiler/assets/sim_ik.rs

Large diffs are not rendered by default.

5 changes: 3 additions & 2 deletions c_transpiler/convert.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,9 @@ def cpp_to_rust(assigns: list[FunctionAssign], rust_file: Path) -> None:
rust_assigns = [ir_to_macro_request_rust(assign) for assign in assigns]
rust_string = ",\n".join(rust_assigns)
file_name = rust_file.name.split(".")[0]
struct_name = inflection.camelize(file_name)
content = f' impl {struct_name} {{\n requests!{{\n"{file_name}",\n{rust_string}\n}}\n}}'
trait_name = inflection.camelize(file_name)
space = ' '*4
content = f'pub trait {trait_name} : RemoteApiClientInterface {{\n{space}requests!{{\n"{file_name}",\n{rust_string}\n}}\n}}'

rust_file.write_text(content)

Expand Down
10 changes: 3 additions & 7 deletions examples/get_simulation_time.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
use std::rc::Rc;
use zmq_remote_api::{sim::Sim, RemoteAPIError, RemoteApiClientParams};

/*
Expand All @@ -9,24 +8,21 @@ fn main() -> Result<(), RemoteAPIError> {
// use the env variable RUST_LOG="trace" or RUST_LOG="debug" to observe the zmq communication
env_logger::init();

let client = zmq_remote_api::RemoteApiClient::new(RemoteApiClientParams {
let sim = zmq_remote_api::RemoteApiClient::new(RemoteApiClientParams {
host: "localhost".to_string(),
..RemoteApiClientParams::default()
})?;

// Rc means Reference counter, is a smart pointer that counter the number of references
let client = Rc::new(client);
let sim = Sim::new(client.clone());

client.set_stepping(true)?;
sim.set_stepping(true)?;

sim.start_simulation()?;

let mut time = sim.get_simulation_time()?;

while time < 3.0 {
println!("Simulation time: {:.3} [s]", time);
client.step(true)?;
sim.step(true)?;
time = sim.get_simulation_time()?;
}

Expand Down
15 changes: 6 additions & 9 deletions examples/opencv_example/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,30 +19,27 @@ fn main() -> Result<(), RemoteAPIError> {
..RemoteApiClientParams::default()
})?;

// Rc means Reference counter, is a smart pointer that counter the number of references
let client = Rc::new(client);
let sim = Sim::new(client.clone());

let vision_sensor_handle = sim.get_object("/VisionSensor".to_string(), None)?;
let vision_sensor_handle = client.get_object("/VisionSensor".to_string(), None)?;

client.set_stepping(true)?;

sim.start_simulation()?;
client.start_simulation()?;

let start_time = sim.get_simulation_time()?;
let start_time = client.get_simulation_time()?;
let mut time = start_time;
while time - start_time < 5.0 {
let (img, res) = sim.get_vision_sensor_img(vision_sensor_handle, None, None, None, None)?;
let (img, res) = client.get_vision_sensor_img(vision_sensor_handle, None, None, None, None)?;

opencv_show_image(img, res);

println!("time: {:.2}", time);

client.step(true)?;
time = sim.get_simulation_time()?;
time = client.get_simulation_time()?;
}

sim.stop_simulation()?;
client.stop_simulation()?;

println!("Program ended");

Expand Down
27 changes: 11 additions & 16 deletions examples/p_controller.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
use std::f64::consts::PI;
use std::rc::Rc;

use zmq_remote_api::{sim::Sim, RemoteAPIError, RemoteApiClient, RemoteApiClientParams};

Expand All @@ -26,13 +25,9 @@ fn main() -> Result<(), RemoteAPIError> {
..RemoteApiClientParams::default()
})?;

// Rc means Reference counter, is a smart pointer that counter the number of references
let client = Rc::new(client);
let sim = Sim::new(client.clone());

let joint_handle = sim.get_object("/Cuboid[0]/joint".to_string(), None)?;
let mut join_angle = sim.get_joint_position(joint_handle)?;
sim.set_joint_target_velocity(joint_handle, 360.0 * PI, None)?;
let joint_handle = client.get_object("/Cuboid[0]/joint".to_string(), None)?;
let mut join_angle = client.get_joint_position(joint_handle)?;
client.set_joint_target_velocity(joint_handle, 360.0 * PI, None)?;
/*
enable the stepping mode on the client, that means
the simulation waits the trigger: client.step()
Expand All @@ -41,52 +36,52 @@ fn main() -> Result<(), RemoteAPIError> {
*/
client.set_stepping(true)?;

sim.start_simulation()?;
client.start_simulation()?;

move_to_angle(
45.0 * PI / 180.0,
&mut join_angle,
&sim,
&client,
&client,
&joint_handle,
)?;

move_to_angle(
90.0 * PI / 180.0,
&mut join_angle,
&sim,
&client,
&client,
&joint_handle,
)?;

move_to_angle(
-89.0 * PI / 180.0,
&mut join_angle,
&sim,
&client,
&client,
&joint_handle,
)?;

move_to_angle(
0.0 * PI / 180.0,
&mut join_angle,
&sim,
&client,
&client,
&joint_handle,
)?;

sim.stop_simulation()?;
client.stop_simulation()?;

println!("Program ended");
Ok(())
}

const MAX_FORCE: f64 = 100.0;

fn move_to_angle(
fn move_to_angle<S: Sim>(
target_angle: f64,
join_angle: &mut f64,
sim: &Sim,
sim: &S,
client: &RemoteApiClient,
joint_handle: &i64,
) -> Result<(), RemoteAPIError> {
Expand Down
37 changes: 16 additions & 21 deletions examples/send_ik_movement_sequence_mov.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
use std::rc::Rc;
use zmq_remote_api::serde_json::json;
use zmq_remote_api::{sim, sim::Sim, RemoteAPIError, RemoteApiClient, RemoteApiClientParams};
/*
Expand All @@ -11,9 +10,9 @@ use zmq_remote_api::{sim, sim::Sim, RemoteAPIError, RemoteApiClient, RemoteApiCl
Do not launch simulation, then run this script
*/

fn wait_for_movement_executed(
fn wait_for_movement_executed<S: Sim>(
id: String,
sim: &Sim,
sim: &S,
signal_name: String,
) -> Result<(), RemoteAPIError> {
let mut string = sim.get_string_signal(signal_name.clone())?;
Expand All @@ -33,26 +32,22 @@ fn main() -> Result<(), RemoteAPIError> {
..RemoteApiClientParams::default()
})?;

/// Rc means Reference counter, is a smart pointer that counter the number of references
let client = Rc::new(client);
let sim = Sim::new(client.clone());

println!("Program started");

let target_arm = "/LBR4p".to_string();

let signal_name = format!("{}_executedMovId", target_arm);

let arm_handle = sim.get_object(target_arm, None)?;
let script_handle = sim.get_script(sim::SCRIPTTYPE_CHILDSCRIPT, Some(arm_handle), None)?;
let arm_handle = client.get_object(target_arm, None)?;
let script_handle = client.get_script(sim::SCRIPTTYPE_CHILDSCRIPT, Some(arm_handle), None)?;

sim.start_simulation()?;
client.start_simulation()?;

println!("Wait until ready");
wait_for_movement_executed("ready".to_string(), &sim, signal_name.clone())?;
wait_for_movement_executed("ready".to_string(), &client, signal_name.clone())?;

println!("Get initial pose");
let json = sim.call_script_function(
let json = client.call_script_function(
String::from("remoteApi_getPoseAndConfig"),
script_handle,
None,
Expand All @@ -67,21 +62,21 @@ fn main() -> Result<(), RemoteAPIError> {

let movement_data = json!({"id": "movSeq1", "type": "mov", "targetPose": [0, 0, 0.85, 0, 0, 0, 1], "maxVel": MAX_VEL, "maxAccel":MAX_ACCEL});

let _json = sim.call_script_function(
let _json = client.call_script_function(
String::from("remoteApi_movementDataFunction"),
script_handle,
Some(movement_data),
)?;

println!("Execute first movement sequence");
let _json = sim.call_script_function(
let _json = client.call_script_function(
String::from("remoteApi_executeMovement"),
script_handle,
Some(json!("movSeq1")),
)?;

println!("Wait until above movement sequence finished executing");
wait_for_movement_executed("movSeq1".to_string(), &sim, signal_name.clone())?;
wait_for_movement_executed("movSeq1".to_string(), &client, signal_name.clone())?;

println!("Send second and third movement sequence, where third one should execute immediately after the second one");
let target_pose = vec![
Expand All @@ -96,38 +91,38 @@ fn main() -> Result<(), RemoteAPIError> {

let movement_data = json!({"id": "movSeq2", "type": "mov", "targetPose": target_pose, "maxVel": MAX_VEL, "maxAccel": MAX_ACCEL});

let _json = sim.call_script_function(
let _json = client.call_script_function(
String::from("remoteApi_movementDataFunction"),
script_handle,
Some(movement_data),
)?;

let movement_data = json!({"id": "movSeq3", "type": "mov", "targetPose": initial_pose, "maxVel": MAX_VEL, "maxAccel": MAX_ACCEL});

let _json = sim.call_script_function(
let _json = client.call_script_function(
String::from("remoteApi_movementDataFunction"),
script_handle,
Some(movement_data),
)?;

println!("Execute second and third movement sequence");

sim.call_script_function(
client.call_script_function(
String::from("remoteApi_executeMovement"),
script_handle,
Some(json!("movSeq2")),
)?;
sim.call_script_function(
client.call_script_function(
String::from("remoteApi_executeMovement"),
script_handle,
Some(json!("movSeq3")),
)?;

println!("Wait until above 2 movement sequences finished executing");

wait_for_movement_executed("movSeq3".to_string(), &sim, signal_name.clone())?;
wait_for_movement_executed("movSeq3".to_string(), &client, signal_name.clone())?;

sim.stop_simulation()?;
client.stop_simulation()?;

println!("Program ended");
Ok(())
Expand Down
25 changes: 10 additions & 15 deletions examples/send_ik_movement_sequence_pts.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
use std::rc::Rc;
use zmq_remote_api::serde_json::json;
use zmq_remote_api::{sim, sim::Sim, RemoteAPIError, RemoteApiClient, RemoteApiClientParams};

Expand All @@ -21,18 +20,14 @@ fn main() -> Result<(), RemoteAPIError> {
..RemoteApiClientParams::default()
})?;

// Rc means Reference counter, is a smart pointer that counter the number of references
let client = Rc::new(client);
let sim = Sim::new(client.clone());

println!("Program started");

let target_arm = "/LBR4p".to_string();

let signal_name = format!("{}_executedMovId", target_arm);

let arm_handle = sim.get_object(target_arm, None)?;
let script_handle = sim.get_script(sim::SCRIPTTYPE_CHILDSCRIPT, Some(arm_handle), None)?;
let arm_handle = client.get_object(target_arm, None)?;
let script_handle = client.get_script(sim::SCRIPTTYPE_CHILDSCRIPT, Some(arm_handle), None)?;

// Set-up some movement variables:
let times = vec![
Expand Down Expand Up @@ -426,10 +421,10 @@ fn main() -> Result<(), RemoteAPIError> {
1.000, 1.000, 1.000, 1.000, 1.000, 1.000, 1.000, 1.000, 1.000, 1.000, 1.000,
];

sim.start_simulation()?;
client.start_simulation()?;

println!("Wait until ready");
wait_for_movement_executed("ready".to_string(), &sim, signal_name.clone())?;
wait_for_movement_executed("ready".to_string(), &client, signal_name.clone())?;

let movement_data = json!(
{"id": "movSeq1",
Expand All @@ -440,30 +435,30 @@ fn main() -> Result<(), RemoteAPIError> {
});

println!("Execute first movement sequence");
sim.call_script_function(
client.call_script_function(
String::from("remoteApi_movementDataFunction"),
script_handle,
Some(movement_data),
)?;

sim.call_script_function(
client.call_script_function(
String::from("remoteApi_executeMovement"),
script_handle,
Some(json! {"movSeq1"}),
)?;

println!("Wait until above movement sequence finished executing");
wait_for_movement_executed("movSeq1".to_string(), &sim, signal_name.clone())?;
wait_for_movement_executed("movSeq1".to_string(), &client, signal_name.clone())?;

sim.stop_simulation()?;
client.stop_simulation()?;

println!("Program ended");
Ok(())
}

fn wait_for_movement_executed(
fn wait_for_movement_executed<S: Sim>(
id: String,
sim: &Sim,
sim: &S,
signal_name: String,
) -> Result<(), RemoteAPIError> {
let mut string = sim.get_string_signal(signal_name.clone())?;
Expand Down
Loading

0 comments on commit ed24699

Please sign in to comment.