Skip to content

Commit

Permalink
updated Rust ZMQ api
Browse files Browse the repository at this point in the history
  • Loading branch information
samuel-cavalcanti committed Sep 19, 2023
1 parent db350dd commit 7c6406b
Show file tree
Hide file tree
Showing 9 changed files with 94 additions and 101 deletions.
14 changes: 7 additions & 7 deletions examples/get_simulation_time.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,24 +8,24 @@ fn main() -> Result<(), RemoteAPIError> {
// use the env variable RUST_LOG="trace" or RUST_LOG="debug" to observe the zmq communication
env_logger::init();

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

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

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

let mut time = sim.get_simulation_time()?;
let mut time = client.sim_get_simulation_time()?;

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

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

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

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

client.set_stepping(true)?;

client.start_simulation()?;
client.sim_start_simulation()?;

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

opencv_show_image(img, res);

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

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

client.stop_simulation()?;
client.sim_stop_simulation()?;

println!("Program ended");

Expand Down
17 changes: 9 additions & 8 deletions examples/p_controller.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,10 @@ fn main() -> Result<(), RemoteAPIError> {
..RemoteApiClientParams::default()
})?;

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)?;
let joint_handle = client.sim_get_object("/Cuboid[0]/joint".to_string(), None)?;
let mut join_angle = client.sim_get_joint_position(joint_handle)?;
client.sim_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 @@ -36,7 +37,7 @@ fn main() -> Result<(), RemoteAPIError> {
*/
client.set_stepping(true)?;

client.start_simulation()?;
client.sim_start_simulation()?;

move_to_angle(
45.0 * PI / 180.0,
Expand Down Expand Up @@ -70,7 +71,7 @@ fn main() -> Result<(), RemoteAPIError> {
&joint_handle,
)?;

client.stop_simulation()?;
client.sim_stop_simulation()?;

println!("Program ended");
Ok(())
Expand All @@ -87,10 +88,10 @@ fn move_to_angle<S: Sim>(
) -> Result<(), RemoteAPIError> {
while (target_angle - *join_angle).abs() > 0.1 * PI / 180.0 {
let velocity = compute_target_velocity(target_angle, *join_angle);
sim.set_joint_target_velocity(*joint_handle, velocity, None)?;
sim.set_joint_max_force(*joint_handle, MAX_FORCE)?;
sim.sim_set_joint_target_velocity(*joint_handle, velocity, None)?;
sim.sim_set_joint_max_force(*joint_handle, MAX_FORCE)?;
client.step(true)?;
*join_angle = sim.get_joint_position(*joint_handle)?;
*join_angle = sim.sim_get_joint_position(*joint_handle)?;
}

Ok(())
Expand Down
27 changes: 14 additions & 13 deletions examples/send_ik_movement_sequence_mov.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@ fn wait_for_movement_executed<S: Sim>(
sim: &S,
signal_name: String,
) -> Result<(), RemoteAPIError> {
let mut string = sim.get_string_signal(signal_name.clone())?;
let mut string = sim.sim_get_string_signal(signal_name.clone())?;
while string != id {
string = sim.get_string_signal(signal_name.clone())?;
string = sim.sim_get_string_signal(signal_name.clone())?;
}

Ok(())
Expand All @@ -38,16 +38,17 @@ fn main() -> Result<(), RemoteAPIError> {

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

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

client.start_simulation()?;
client.sim_start_simulation()?;

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

println!("Get initial pose");
let json = client.call_script_function(
let json = client.sim_call_script_function(
String::from("remoteApi_getPoseAndConfig"),
script_handle,
None,
Expand All @@ -62,14 +63,14 @@ 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 = client.call_script_function(
let _json = client.sim_call_script_function(
String::from("remoteApi_movementDataFunction"),
script_handle,
Some(movement_data),
)?;

println!("Execute first movement sequence");
let _json = client.call_script_function(
let _json = client.sim_call_script_function(
String::from("remoteApi_executeMovement"),
script_handle,
Some(json!("movSeq1")),
Expand All @@ -91,28 +92,28 @@ fn main() -> Result<(), RemoteAPIError> {

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

let _json = client.call_script_function(
let _json = client.sim_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 = client.call_script_function(
let _json = client.sim_call_script_function(
String::from("remoteApi_movementDataFunction"),
script_handle,
Some(movement_data),
)?;

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

client.call_script_function(
client.sim_call_script_function(
String::from("remoteApi_executeMovement"),
script_handle,
Some(json!("movSeq2")),
)?;
client.call_script_function(
client.sim_call_script_function(
String::from("remoteApi_executeMovement"),
script_handle,
Some(json!("movSeq3")),
Expand All @@ -122,7 +123,7 @@ fn main() -> Result<(), RemoteAPIError> {

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

client.stop_simulation()?;
client.sim_stop_simulation()?;

println!("Program ended");
Ok(())
Expand Down
18 changes: 9 additions & 9 deletions examples/send_ik_movement_sequence_pts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,9 @@ fn main() -> Result<(), RemoteAPIError> {

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

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

// Set-up some movement variables:
let times = vec![
Expand Down Expand Up @@ -420,8 +421,7 @@ 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, 1.000, 1.000,
1.000, 1.000, 1.000, 1.000, 1.000, 1.000, 1.000, 1.000, 1.000, 1.000, 1.000,
];

client.start_simulation()?;
client.sim_start_simulation()?;

println!("Wait until ready");
wait_for_movement_executed("ready".to_string(), &client, signal_name.clone())?;
Expand All @@ -435,13 +435,13 @@ fn main() -> Result<(), RemoteAPIError> {
});

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

client.call_script_function(
client.sim_call_script_function(
String::from("remoteApi_executeMovement"),
script_handle,
Some(json! {"movSeq1"}),
Expand All @@ -450,7 +450,7 @@ fn main() -> Result<(), RemoteAPIError> {
println!("Wait until above movement sequence finished executing");
wait_for_movement_executed("movSeq1".to_string(), &client, signal_name.clone())?;

client.stop_simulation()?;
client.sim_stop_simulation()?;

println!("Program ended");
Ok(())
Expand All @@ -461,9 +461,9 @@ fn wait_for_movement_executed<S: Sim>(
sim: &S,
signal_name: String,
) -> Result<(), RemoteAPIError> {
let mut string = sim.get_string_signal(signal_name.clone())?;
let mut string = sim.sim_get_string_signal(signal_name.clone())?;
while string != id {
string = sim.get_string_signal(signal_name.clone())?;
string = sim.sim_get_string_signal(signal_name.clone())?;
}

Ok(())
Expand Down
36 changes: 20 additions & 16 deletions examples/simple_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,56 +26,60 @@ fn main() -> Result<(), RemoteAPIError> {
// slow, since the idle loop runs at 8 Hz by default. So let's make
// sure that the idle loop runs at full speed for this program:

let default_idle_fps = client.get_int32_param(sim::INTPARAM_IDLE_FPS)?;
client.set_int32_param(sim::INTPARAM_IDLE_FPS, 0)?;
let default_idle_fps = client.sim_get_int32_param(sim::INTPARAM_IDLE_FPS)?;
client.sim_set_int32_param(sim::INTPARAM_IDLE_FPS, 0)?;

// Create a few dummies and set their positions:
let handles: Vec<i64> = (0..50)
.map(|_| client.create_dummy(0.01).unwrap())
.map(|_| client.sim_create_dummy(0.01).unwrap())
.collect();

for (i, h) in handles.iter().enumerate() {
let i = i as f64;
client.set_object_position(*h, sim::HANDLE_WORLD, vec![0.01 * i, 0.01 * i, 0.01 * i])?;
client.sim_set_object_position(
*h,
sim::HANDLE_WORLD,
vec![0.01 * i, 0.01 * i, 0.01 * i],
)?;
}

client.start_simulation()?;
client.sim_start_simulation()?;
// Run a simulation in asynchronous mode:
let mut time = client.get_simulation_time()?;
let mut time = client.sim_get_simulation_time()?;
while time < 3.0 {
time = client.get_simulation_time()?;
time = client.sim_get_simulation_time()?;

println!("Simulation time: {time:.2} [s] (simulation running asynchronously to client, i.e. non-stepped)", time = time);
}

client.stop_simulation()?;
client.sim_stop_simulation()?;
// if you need to make sure we really stopped:
while client.get_simulation_state()? != sim::SIMULATION_STOPPED {
while client.sim_get_simulation_state()? != sim::SIMULATION_STOPPED {
std::thread::sleep(std::time::Duration::from_secs_f64(0.1))
}

client.set_stepping(true)?;
client.start_simulation()?;
client.sim_start_simulation()?;

// Run a simulation in stepping mode:
let mut time = client.get_simulation_time()?;
let mut time = client.sim_get_simulation_time()?;
while time < 3.0 {
time = client.get_simulation_time()?;
time = client.sim_get_simulation_time()?;

let message = format!("Simulation time: {time:.2} [s] (simulation running asynchronously to client, i.e. stepped)", time = time);
println!("{}", message);
client.add_log(sim::VERBOSITY_SCRIPTINFOS, message)?;
client.sim_add_log(sim::VERBOSITY_SCRIPTINFOS, message)?;
client.step(true)?; //triggers next simulation step
}
client.stop_simulation()?;
client.sim_stop_simulation()?;

//Remove the dummies created earlier:
for h in handles {
client.remove_object(h)?;
client.sim_remove_object(h)?;
}

//Restore the original idle loop frequency:
client.set_int32_param(sim::INTPARAM_IDLE_FPS, default_idle_fps)?;
client.sim_set_int32_param(sim::INTPARAM_IDLE_FPS, default_idle_fps)?;
println!("Program ended");

Ok(())
Expand Down
16 changes: 8 additions & 8 deletions examples/synchronous_image_transmission.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,26 +19,26 @@ fn main() -> Result<(), RemoteAPIError> {
..RemoteApiClientParams::default()
})?;

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

let passive_vision_sensor_handle =
client.get_object("/PassiveVisionSensor".to_string(), None)?;
client.sim_get_object("/PassiveVisionSensor".to_string(), None)?;

client.set_stepping(true)?;

client.start_simulation()?;
client.sim_start_simulation()?;

let start_time = client.get_simulation_time()?;
let start_time = client.sim_get_simulation_time()?;

while client.get_simulation_time()? - start_time < 5.0 {
while client.sim_get_simulation_time()? - start_time < 5.0 {
let (img, _res) =
client.get_vision_sensor_img(vision_sensor_handle, None, None, None, None)?;
client.sim_get_vision_sensor_img(vision_sensor_handle, None, None, None, None)?;

client.set_vision_sensor_img(passive_vision_sensor_handle, img, None, None, None)?;
client.sim_set_vision_sensor_img(passive_vision_sensor_handle, img, None, None, None)?;
client.step(true)?;
}

client.stop_simulation()?;
client.sim_stop_simulation()?;

println!("Program ended");

Expand Down
15 changes: 1 addition & 14 deletions src/remote_api_client/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -79,20 +79,7 @@ impl RemoteApiClient {
Ok(message)
}

// pub fn send_request<R: RawRequest>(&self, request: R) -> Result<JsonValue, Error> {
// let payload = request.to_raw_request();
//
// let result = self.call(payload)?;
//
// let decoded: CborValue = ciborium::de::from_reader(result.as_slice()).unwrap();
//
// let json = serde_json::json!(decoded);
// log::trace!("Json response: {}", json);
//
// Ok(json)
// }

pub fn get_object_client(&self, name: String) -> Result<JsonValue, RemoteAPIError> {
pub fn get_object(&self, name: String) -> Result<JsonValue, RemoteAPIError> {
let request = ZmqRequest::remote_api_info(name);

self.send_raw_request(request.to_raw_request())
Expand Down
Loading

0 comments on commit 7c6406b

Please sign in to comment.