jrl-umi3218 / mc_rtc

mc_rtc is an interface for simulated and real robotic systems suitable for real-time control
BSD 2-Clause "Simplified" License
118 stars 35 forks source link

Accessing Config values using Python #426

Closed jlee1218 closed 7 months ago

jlee1218 commented 8 months ago

Hi,

I am trying to access values in my FSM controller's config file using Python, but I am having some issues.

I currently have a mc_control.MCGlobalController object instance called 'mc_gc'. I am trying to access the value associated with the key "TaskCompleted", and I am currently doing this by mc_gc.controller().config()("TaskCompleted"). However, this returns a 'mc_rtc.mc_rtc.Configuration object' and not the value.

I have confirmed that I can access this value from the configuration file using c++ by using ctl.config()("TaskCompleted"), where ctl is a mc_control::fsm::Controller object.

Is there a seperate API documentation that I should follow for Python implementations? I am currently just referencing https://jrl-umi3218.github.io/mc_rtc/doxygen.html.

Thank you in advance for your help.

gergondet commented 8 months ago

Hi @jlee1218

The difference between the C++ API and the Python API is explained here

In C++, we rely on implicit conversion to go from mc_rtc::Configuration objects to other types but this is not possible with the Python API so you have to either explicitly provide the type or provide a default value (the desired type is then deduced from the type of the default) so the following would work in your case:

# For readability 
cfg = mc_gc.controller().config()
# Throws a RuntimeError if "TaskCompleted" is not in the configuration or if it cannot convert to a bool
value = cfg("TaskCompleted", bool)
assert type(value) == bool
# Only throws if "TaskCompleted" is not in the configuration, if the conversion fails, value will be False
value = cfg("TaskCompleted", False)
assert type(value) == bool