Skip to content

Commit

Permalink
Python: added support for gimbal
Browse files Browse the repository at this point in the history
  • Loading branch information
julianoes committed Aug 20, 2018
1 parent f5a9095 commit 7f496cb
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 17 deletions.
3 changes: 3 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ if (PYTHON)
${CMAKE_CURRENT_SOURCE_DIR}/plugins/mission/include/plugins/mission/mission.h
${CMAKE_CURRENT_SOURCE_DIR}/plugins/mission/include/plugins/mission/mission_item.h
${CMAKE_CURRENT_SOURCE_DIR}/plugins/telemetry/include/plugins/telemetry/telemetry.h
${CMAKE_CURRENT_SOURCE_DIR}/plugins/gimbal/include/plugins/gimbal/gimbal.h
--template ${CMAKE_CURRENT_SOURCE_DIR}/core/pydronecode_sdk.cpp.in
--output ${CMAKE_CURRENT_BINARY_DIR}/core/pydronecode_sdk.cpp
--verbose
Expand All @@ -86,6 +87,7 @@ if (PYTHON)
${CMAKE_CURRENT_SOURCE_DIR}/plugins/mission/include/plugins/mission/mission.h
${CMAKE_CURRENT_SOURCE_DIR}/plugins/mission/include/plugins/mission/mission_item.h
${CMAKE_CURRENT_SOURCE_DIR}/plugins/telemetry/include/plugins/telemetry/telemetry.h
${CMAKE_CURRENT_SOURCE_DIR}/plugins/gimbal/include/plugins/gimbal/gimbal.h
${CMAKE_CURRENT_SOURCE_DIR}/core/pydronecode_sdk.cpp.in
OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/core/pydronecode_sdk.cpp
COMMENT "Generating code for pybind11."
Expand All @@ -101,6 +103,7 @@ if (PYTHON)
dronecode_sdk_action
dronecode_sdk_mission
dronecode_sdk_telemetry
dronecode_sdk_gimbal
${CMAKE_THREAD_LIBS_INIT}
${CURL_LIBRARY}
${TINYXML2_LIBRARY}
Expand Down
42 changes: 26 additions & 16 deletions generate_pybind_definition.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@ def debug(string):
if verbose:
print(string)

class PluginParseException(Exception):
pass

class Method(object):
def __init__(self):
self.return_type = ""
Expand Down Expand Up @@ -69,8 +72,7 @@ def parse_plugin_header(self, lines):
"async" in line or \
"class " + self.class_name in line or \
self.class_name + "()" in line or \
"operator" in line or \
"&rhs" in line: # TODO: remove this hack
"operator" in line:
continue

if "struct {" in line:
Expand Down Expand Up @@ -116,7 +118,7 @@ def determine_class_name(self, lines):
format(self.class_name))
return

raise Exception("Could not find class name")
raise PluginParseException("Could not find class name")

def find_method(self, part):
# We ignore static methods for now.
Expand All @@ -127,7 +129,7 @@ def find_method(self, part):
m = p.match(part)
if m:
if len(m.groups()) != 3:
raise Exception("Unknown number of groups {} for {}"
raise ("Unknown number of groups {} for {}"
.format(m.groups(), part))

new_method = Method()
Expand Down Expand Up @@ -155,7 +157,7 @@ def find_enum(self, part):
new_enum.name = m.groups()[0]
debug("Found enum name: '{}'".format(new_enum.name))
else:
raise Exception("Could not parse enum name")
raise PluginParseException("Could not parse enum name")

p = re.compile('([\w\d]*),')
m = p.findall(part)
Expand All @@ -166,7 +168,7 @@ def find_enum(self, part):
self.enums.append(new_enum)
return True
else:
raise Exception("Could not find enum entries")
raise PluginParseException("Could not find enum entries")

def find_struct(self, part):
if not 'struct' in part:
Expand All @@ -180,7 +182,7 @@ def find_struct(self, part):
new_struct.name = m.groups()[0]
debug("Found struct name: '{}'".format(new_struct.name))
else:
raise Exception("Could not parse struct name")
raise PluginParseException("Could not parse struct name")

p = re.compile('[\w\d]*\s*([\w\d]+);')
m = p.findall(part)
Expand All @@ -191,16 +193,18 @@ def find_struct(self, part):
self.structs.append(new_struct)
return True
else:
raise Exception("Could not parse struct fields")
raise PluginParseException("Could not parse struct fields")



def parse_part(self, part):
debug("Parsing: '{}'".format(part))
if not self.find_method(part) and \
not self.find_enum(part) and \
not self.find_struct(part):
raise Exception("Could not parse '{}'".format(part))
try:
if self.find_method(part) or \
self.find_enum(part) or \
self.find_struct(part):
return
except PluginParseException as e:
print("Ignoring: '{}', error: {}".format(part, e))


def get_pybind_entry(self):
Expand All @@ -209,7 +213,7 @@ def get_pybind_entry(self):
format(self.class_name)
self.output += ' .def(py::init<System &>())\n'
elif self.parent_name is not None:
raise Exception("Can't deal with parent other than PluginBase")
raise PluginParseException("Can't deal with parent other than PluginBase")
elif self.class_name == 'MissionItem':
# TODO: remove this gack for std::shared_ptr used
# to upload/download mission items.
Expand Down Expand Up @@ -253,7 +257,11 @@ def get_pybind_entry(self):

return self.output


def remove_newlines_inside_brackets(content):
return re.sub(r'\(.*?\)',
lambda m: m.group().replace("\n", " "),
content,
flags=re.DOTALL)

def main():

Expand Down Expand Up @@ -283,7 +291,9 @@ def main():
for header in args.headers:
includes += '#include "{}"\n'.format(header)
with open(header, "r") as f:
lines = f.readlines()
content = f.read()
content = remove_newlines_inside_brackets(content)
lines = content.splitlines()

header_parser = HeaderParser()
header_parser.parse_plugin_header(lines)
Expand Down
2 changes: 1 addition & 1 deletion plugins/gimbal/include/plugins/gimbal/gimbal.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ class Gimbal : public PluginBase {
* @brief Possible results returned for gimbal commands.
*/
enum class Result {
SUCCESS = 0, /**< @brief Success. The gimbal command was accepted. */
SUCCESS, /**< @brief Success. The gimbal command was accepted. */
ERROR, /**< @brief Error. An error occured sending the command. */
TIMEOUT, /**< @brief Timeout. A timeout occured sending the command. */
UNKNOWN /**< @brief Unspecified error. */
Expand Down

0 comments on commit 7f496cb

Please sign in to comment.