Skip to content

Commit

Permalink
solved merge
Browse files Browse the repository at this point in the history
  • Loading branch information
samuel-cavalcanti committed Sep 19, 2023
2 parents aa87d06 + 7c6406b commit 51db122
Show file tree
Hide file tree
Showing 17 changed files with 577 additions and 582 deletions.
4 changes: 2 additions & 2 deletions c_transpiler/assets/expected.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ std::tuple<std::vector<uint8_t>, std::vector<int64_t>> getVisionSensorCharImage(
void setVisionSensorCharImage(int64_t sensorHandle, std::vector<uint8_t> image);
std::vector<int64_t> getObjectSelection();
void setObjectSelection(std::vector<double> objectHandles);
std::optional<std::vector<uint8_t>> getStringSignal(std::string signalName);
std::string getStringSignal(std::string signalName);
std::optional<int64_t> getInt32Signal(std::string signalName);
std::optional<double> getFloatSignal(std::string signalName);
int64_t addDrawingObject(int64_t objectType, double size, double duplicateTolerance, int64_t parentObjectHandle, int64_t maxItemCount, std::optional<std::vector<double>> color = {});
Expand Down Expand Up @@ -379,4 +379,4 @@ void writeCustomDataBlock(int64_t objectHandle, std::string tagName, std::vector
void writeCustomDataBlockEx(int64_t handle, std::string tagName, std::string data, std::optional<json> options = {});
void writeCustomTableData(int64_t handle, std::string tagName, json theTable, std::optional<json> options = {});
void writeTexture(int64_t textureId, int64_t options, std::vector<uint8_t> textureData, std::optional<int64_t> posX = {}, std::optional<int64_t> posY = {}, std::optional<int64_t> sizeX = {}, std::optional<int64_t> sizeY = {}, std::optional<double> interpol = {});
std::tuple<double, double, double> yawPitchRollToAlphaBetaGamma(double yawAngle, double pitchAngle, double rollAngle);
std::tuple<double, double, double> yawPitchRollToAlphaBetaGamma(double yawAngle, double pitchAngle, double rollAngle);
2 changes: 1 addition & 1 deletion c_transpiler/assets/sim.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ def getObjectSelection(self)->list[int]:
def setObjectSelection(self,objectHandles:list[float])->None:
...

def getStringSignal(self,signalName:str)->Optional[list[int]]:
def getStringSignal(self,signalName:str)->str:
...

def getInt32Signal(self,signalName:str)->Optional[int]:
Expand Down
772 changes: 386 additions & 386 deletions c_transpiler/assets/sim.rs

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion c_transpiler/assets/sim_api_header.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
// DEPRECATED END

// SPECIAL START
std::optional<std::vector<uint8_t>> getStringSignal(std::string signalName);
std::string getStringSignal(std::string signalName);
std::optional<int64_t> getInt32Signal(std::string signalName);
std::optional<double> getFloatSignal(std::string signalName);
// SPECIAL END
Expand Down
161 changes: 80 additions & 81 deletions c_transpiler/assets/sim_ik.rs

Large diffs are not rendered by default.

6 changes: 4 additions & 2 deletions c_transpiler/convert.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,13 @@

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]
trait_name = inflection.camelize(file_name)
space = ' '*4

rust_assigns = [ir_to_macro_request_rust(
assign, file_name) for assign in assigns]
rust_string = ",\n".join(rust_assigns)
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
4 changes: 2 additions & 2 deletions c_transpiler/ir_transpiler/ir_to_macro_request_rust.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@
import inflection


def ir_to_macro_request_rust(assign: FunctionAssign) -> str:
def ir_to_macro_request_rust(assign: FunctionAssign, file_name: str) -> str:
return_type = type_node_to_rust(assign.return_type)

rust_func_name = inflection.underscore(assign.function_name)
rust_func_name = f'{file_name}_{inflection.underscore(assign.function_name)}'

required_args = []
option_args = []
Expand Down
13 changes: 7 additions & 6 deletions c_transpiler/tests/test_ir.py
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ def test_ir_parser_remote_api_header(self):
ir = [ir_to_cpp(assign) for assign in assigns]
result_content = "\n".join(ir)

self.assertEqual(result_content, expected_h.read_text())
self.assertEqual(result_content + '\n', expected_h.read_text())

def test_ir_to_string(self):
vec_u8_ir = TypeNode(TokenType.VEC, [TypeNode(TokenType.U8, [])])
Expand Down Expand Up @@ -236,12 +236,13 @@ def test_ir_to_macro_request_rust(self):
wait_ir, get_vision_sensor_depth_buffer_if]

expected_strings = [
'(switch_thread,"switchThread"->())',
'(unpack_table,"unpackTable",(buffer:Vec<u8>)->serde_json::Value)',
'(wait,"wait",(dt:f64),opt(simulation_time:bool)->f64)',
'(get_vision_sensor_depth_buffer,"getVisionSensorDepthBuffer",(sensor_handle:i64),opt(pos:Vec<i64>,size:Vec<i64>)->(Vec<u8>,Vec<i64>))'
'(test_switch_thread,"switchThread"->())',
'(test_unpack_table,"unpackTable",(buffer:Vec<u8>)->serde_json::Value)',
'(test_wait,"wait",(dt:f64),opt(simulation_time:bool)->f64)',
'(test_get_vision_sensor_depth_buffer,"getVisionSensorDepthBuffer",(sensor_handle:i64),opt(pos:Vec<i64>,size:Vec<i64>)->(Vec<u8>,Vec<i64>))'
]
result = [ir_to_macro_request_rust(ir) for ir in inputs]
result = [ir_to_macro_request_rust(
ir, file_name='test') for ir in inputs]
self.assertEqualStrings(result, expected_strings)

def assertEqualStrings(self, result: list[str], expected: list[str]):
Expand Down
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
Loading

0 comments on commit 51db122

Please sign in to comment.