Skip to content

Commit

Permalink
Merge pull request #267 from mavlink/update-proto
Browse files Browse the repository at this point in the history
Update submodule, adding distance sensor to telemetry
  • Loading branch information
JonasVautherin authored Oct 13, 2020
2 parents 5e3ce79 + 445e39e commit ec60634
Show file tree
Hide file tree
Showing 10 changed files with 1,327 additions and 234 deletions.
22 changes: 22 additions & 0 deletions mavsdk/action.py
Original file line number Diff line number Diff line change
Expand Up @@ -368,6 +368,28 @@ async def shutdown(self):
raise ActionError(result, "shutdown()")


async def terminate(self):
"""
Send command to terminate the drone.
This will run the terminate routine as configured on the drone (e.g. disarm and open the parachute).
Raises
------
ActionError
If the request fails. The error contains the reason for the failure.
"""

request = action_pb2.TerminateRequest()
response = await self._stub.Terminate(request)


result = self._extract_result(response)

if result.result is not ActionResult.Result.SUCCESS:
raise ActionError(result, "terminate()")


async def kill(self):
"""
Send command to kill the drone.
Expand Down
208 changes: 146 additions & 62 deletions mavsdk/action_pb2.py

Large diffs are not rendered by default.

36 changes: 36 additions & 0 deletions mavsdk/action_pb2_grpc.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,11 @@ def __init__(self, channel):
request_serializer=action_dot_action__pb2.ShutdownRequest.SerializeToString,
response_deserializer=action_dot_action__pb2.ShutdownResponse.FromString,
)
self.Terminate = channel.unary_unary(
'/mavsdk.rpc.action.ActionService/Terminate',
request_serializer=action_dot_action__pb2.TerminateRequest.SerializeToString,
response_deserializer=action_dot_action__pb2.TerminateResponse.FromString,
)
self.Kill = channel.unary_unary(
'/mavsdk.rpc.action.ActionService/Kill',
request_serializer=action_dot_action__pb2.KillRequest.SerializeToString,
Expand Down Expand Up @@ -173,6 +178,16 @@ def Shutdown(self, request, context):
context.set_details('Method not implemented!')
raise NotImplementedError('Method not implemented!')

def Terminate(self, request, context):
"""
Send command to terminate the drone.
This will run the terminate routine as configured on the drone (e.g. disarm and open the parachute).
"""
context.set_code(grpc.StatusCode.UNIMPLEMENTED)
context.set_details('Method not implemented!')
raise NotImplementedError('Method not implemented!')

def Kill(self, request, context):
"""
Send command to kill the drone.
Expand Down Expand Up @@ -314,6 +329,11 @@ def add_ActionServiceServicer_to_server(servicer, server):
request_deserializer=action_dot_action__pb2.ShutdownRequest.FromString,
response_serializer=action_dot_action__pb2.ShutdownResponse.SerializeToString,
),
'Terminate': grpc.unary_unary_rpc_method_handler(
servicer.Terminate,
request_deserializer=action_dot_action__pb2.TerminateRequest.FromString,
response_serializer=action_dot_action__pb2.TerminateResponse.SerializeToString,
),
'Kill': grpc.unary_unary_rpc_method_handler(
servicer.Kill,
request_deserializer=action_dot_action__pb2.KillRequest.FromString,
Expand Down Expand Up @@ -476,6 +496,22 @@ def Shutdown(request,
options, channel_credentials,
call_credentials, compression, wait_for_ready, timeout, metadata)

@staticmethod
def Terminate(request,
target,
options=(),
channel_credentials=None,
call_credentials=None,
compression=None,
wait_for_ready=None,
timeout=None,
metadata=None):
return grpc.experimental.unary_unary(request, target, '/mavsdk.rpc.action.ActionService/Terminate',
action_dot_action__pb2.TerminateRequest.SerializeToString,
action_dot_action__pb2.TerminateResponse.FromString,
options, channel_credentials,
call_credentials, compression, wait_for_ready, timeout, metadata)

@staticmethod
def Kill(request,
target,
Expand Down
254 changes: 253 additions & 1 deletion mavsdk/param.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,238 @@
from enum import Enum


class IntParam:
"""
Type for integer parameters.
Parameters
----------
name : std::string
Name of the parameter
value : int32_t
Value of the parameter
"""



def __init__(
self,
name,
value):
""" Initializes the IntParam object """
self.name = name
self.value = value

def __equals__(self, to_compare):
""" Checks if two IntParam are the same """
try:
# Try to compare - this likely fails when it is compared to a non
# IntParam object
return \
(self.name == to_compare.name) and \
(self.value == to_compare.value)

except AttributeError:
return False

def __str__(self):
""" IntParam in string representation """
struct_repr = ", ".join([
"name: " + str(self.name),
"value: " + str(self.value)
])

return f"IntParam: [{struct_repr}]"

@staticmethod
def translate_from_rpc(rpcIntParam):
""" Translates a gRPC struct to the SDK equivalent """
return IntParam(

rpcIntParam.name,


rpcIntParam.value
)

def translate_to_rpc(self, rpcIntParam):
""" Translates this SDK object into its gRPC equivalent """




rpcIntParam.name = self.name





rpcIntParam.value = self.value





class FloatParam:
"""
Type for float paramters.
Parameters
----------
name : std::string
Name of the parameter
value : float
Value of the parameter
"""



def __init__(
self,
name,
value):
""" Initializes the FloatParam object """
self.name = name
self.value = value

def __equals__(self, to_compare):
""" Checks if two FloatParam are the same """
try:
# Try to compare - this likely fails when it is compared to a non
# FloatParam object
return \
(self.name == to_compare.name) and \
(self.value == to_compare.value)

except AttributeError:
return False

def __str__(self):
""" FloatParam in string representation """
struct_repr = ", ".join([
"name: " + str(self.name),
"value: " + str(self.value)
])

return f"FloatParam: [{struct_repr}]"

@staticmethod
def translate_from_rpc(rpcFloatParam):
""" Translates a gRPC struct to the SDK equivalent """
return FloatParam(

rpcFloatParam.name,


rpcFloatParam.value
)

def translate_to_rpc(self, rpcFloatParam):
""" Translates this SDK object into its gRPC equivalent """




rpcFloatParam.name = self.name





rpcFloatParam.value = self.value





class AllParams:
"""
Type collecting all integer and float parameters.
Parameters
----------
int_params : [IntParam]
Collection of all parameter names and values of type int
float_params : [FloatParam]
Collection of all parameter names and values of type float
"""



def __init__(
self,
int_params,
float_params):
""" Initializes the AllParams object """
self.int_params = int_params
self.float_params = float_params

def __equals__(self, to_compare):
""" Checks if two AllParams are the same """
try:
# Try to compare - this likely fails when it is compared to a non
# AllParams object
return \
(self.int_params == to_compare.int_params) and \
(self.float_params == to_compare.float_params)

except AttributeError:
return False

def __str__(self):
""" AllParams in string representation """
struct_repr = ", ".join([
"int_params: " + str(self.int_params),
"float_params: " + str(self.float_params)
])

return f"AllParams: [{struct_repr}]"

@staticmethod
def translate_from_rpc(rpcAllParams):
""" Translates a gRPC struct to the SDK equivalent """
return AllParams(

map(lambda elem: IntParam.translate_from_rpc(elem), rpcAllParams.int_params),


map(lambda elem: FloatParam.translate_from_rpc(elem), rpcAllParams.float_params)
)

def translate_to_rpc(self, rpcAllParams):
""" Translates this SDK object into its gRPC equivalent """




rpc_elems_list = []
for elem in self.int_params:
rpc_elem = param_pb2.IntParam()
elem.translate_to_rpc(rpc_elem)
rpc_elems_list.append(rpc_elem)
rpcAllParams.int_params.extend(rpc_elems_list)





rpc_elems_list = []
for elem in self.float_params:
rpc_elem = param_pb2.FloatParam()
elem.translate_to_rpc(rpc_elem)
rpc_elems_list.append(rpc_elem)
rpcAllParams.float_params.extend(rpc_elems_list)





class ParamResult:
"""
Result type.
Expand Down Expand Up @@ -321,4 +553,24 @@ async def set_param_float(self, name, value):

if result.result is not ParamResult.Result.SUCCESS:
raise ParamError(result, "set_param_float()", name, value)



async def get_all_params(self):
"""
Get all parameters.
Returns
-------
params : AllParams
Collection of all parameters
"""

request = param_pb2.GetAllParamsRequest()
response = await self._stub.GetAllParams(request)



return AllParams.translate_from_rpc(response.params)

Loading

0 comments on commit ec60634

Please sign in to comment.