Skip to content

Commit 905a161

Browse files
grantphllpsGrant PhillipsGrant Phillipsevan-palmer
authored
Add debug vector send capability (#118)
* Added support for debug vect * added debug_vect to send_debug_message() * final testing complete * added a better example for debug * remove unwanted file changes * fixed stale comments and warnings for PR * remooved unwanted mission report * Fixed spelling errors and error handling issues * final cleanup for send_debug.py * final testing complete * Gramatical fixes * Updated changelog and bumped project version * Fix ci pipeline Co-authored-by: Grant Phillips <gphillip@cse-jbradley-11.local> Co-authored-by: Grant Phillips <gphillip@cse-jbradley-11.unl.edu> Co-authored-by: Evan Palmer <evanp922@gmail.com>
1 parent 090281d commit 905a161

File tree

6 files changed

+141
-11
lines changed

6 files changed

+141
-11
lines changed

.github/workflows/ci.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ jobs:
3333
python -m pip install --upgrade pip
3434
python -m pip install tox tox-gh-actions
3535
- name: Test implementation
36-
run: tox
36+
run: python -m tox
3737

3838
package:
3939
name: Build & publish package

CHANGELOG.rst

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,15 @@ utilizes `Semantic Versioning`_ for project versioning.
77

88
.. _Semantic Versioning: https://semver.org/
99

10+
[v1.1.0] - 2022-09-04
11+
---------------------
12+
13+
Added
14+
^^^^^
15+
16+
- Support for sending debug vectors: `PR #118`_
17+
18+
.. _PR #118: https://github.com/unl-nimbus-lab/pymavswarm/pull/118
1019

1120
[v1.0.0] - 2022-08-13
1221
---------------------

examples/send_debug.py

Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
# pymavswarm is an interface for swarm control and interaction
2+
# Copyright (C) 2022 Grant Phillips
3+
4+
# This program is free software: you can redistribute it and/or modify
5+
# it under the terms of the GNU General Public License as published by
6+
# the Free Software Foundation, either version 3 of the License, or
7+
# (at your option) any later version.
8+
9+
# This program is distributed in the hope that it will be useful,
10+
# but WITHOUT ANY WARRANTY; without even the implied warranty of
11+
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12+
# GNU General Public License for more details.
13+
14+
# You should have received a copy of the GNU General Public License
15+
# along with this program. If not, see <https://www.gnu.org/licenses/>.
16+
17+
from __future__ import annotations
18+
19+
import time
20+
from argparse import ArgumentParser
21+
from concurrent.futures import Future
22+
from typing import Any
23+
24+
from pymavswarm import MavSwarm
25+
26+
27+
def parse_args() -> Any:
28+
"""
29+
Parse the script arguments.
30+
31+
:return: argument namespace
32+
:rtype: Any
33+
"""
34+
parser = ArgumentParser()
35+
parser.add_argument(
36+
"port", type=str, help="port to establish a MAVLink connection over"
37+
)
38+
parser.add_argument("baud", type=int, help="baudrate to establish a connection at")
39+
parser.add_argument("name", type=str, help="debug value name")
40+
parser.add_argument("data1", type=float, help="debug vector payload1")
41+
parser.add_argument("data2", type=float, help="debug vector payload2")
42+
parser.add_argument("data3", type=float, help="debug vector payload3")
43+
return parser.parse_args()
44+
45+
46+
# Define a callback to attach to a future
47+
def print_message_response_cb(future: Future) -> None:
48+
"""
49+
Print the result of the future.
50+
51+
:param future: message execution future
52+
:type future: Future
53+
"""
54+
responses = future.result()
55+
56+
if isinstance(responses, list):
57+
for response in responses:
58+
print(
59+
f"Result of {response.message_type} message sent to "
60+
f"({response.target_agent_id}): {response.code}"
61+
)
62+
else:
63+
print(
64+
f"Result of {responses.message_type} message sent to "
65+
f"({responses.target_agent_id}): {responses.code}"
66+
)
67+
68+
return
69+
70+
71+
def main() -> None:
72+
"""Demonstrate how to send a debug vector."""
73+
# Parse the script arguments
74+
args = parse_args()
75+
76+
# Create a new MavSwarm instance
77+
mavswarm = MavSwarm()
78+
79+
# Attempt to create a new MAVLink connection
80+
if not mavswarm.connect(args.port, args.baud):
81+
return
82+
83+
# Wait for the swarm to auto-register new agents
84+
while not list(filter(lambda agent_id: agent_id[1] == 1, mavswarm.agent_ids)):
85+
print("Waiting for the system to recognize agents in the network...")
86+
time.sleep(0.5)
87+
88+
# Send a debug message to all agents in the swarm
89+
future = mavswarm.send_debug_message(
90+
args.name, [args.data1, args.data2, args.data3]
91+
)
92+
future.add_done_callback(print_message_response_cb)
93+
94+
# Wait for the arm command to complete
95+
while not future.done():
96+
pass
97+
98+
# Disconnect from the swarm
99+
mavswarm.disconnect()
100+
101+
return
102+
103+
104+
if __name__ == "__main__":
105+
main()

pymavswarm/mavswarm.py

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1118,7 +1118,7 @@ def executor(agent_id: AgentID) -> None:
11181118
def send_debug_message(
11191119
self,
11201120
name: str,
1121-
value: int | float,
1121+
value: int | float | list,
11221122
agent_ids: AgentID | list[AgentID] | None = None,
11231123
retry: bool = False,
11241124
message_timeout: float = 2.5,
@@ -1133,7 +1133,7 @@ def send_debug_message(
11331133
:param name: debug message name
11341134
:type name: str
11351135
:param value: debug message value
1136-
:type value: int | float
1136+
:type value: int | float | list
11371137
:param agent_ids: optional list of target agent IDs, defaults to None
11381138
:type agent_ids: AgentID | list[AgentID] | None,
11391139
optional
@@ -1152,9 +1152,14 @@ def send_debug_message(
11521152
if not isinstance(name, str):
11531153
raise TypeError(f"Invalid name provided. Expected string, got {type(name)}")
11541154

1155-
if not isinstance(value, int) and not isinstance(value, float):
1155+
if (
1156+
not isinstance(value, int)
1157+
and not isinstance(value, float)
1158+
and not isinstance(value, list)
1159+
):
11561160
raise TypeError(
1157-
f"Invalid value provided. Expected an int or a float, got {type(value)}"
1161+
"Invalid value provided. Expected an int, float or list, got "
1162+
f"{type(value)}"
11581163
)
11591164

11601165
def executor(agent_id: AgentID) -> None:
@@ -1163,15 +1168,24 @@ def executor(agent_id: AgentID) -> None:
11631168
self._connection.mavlink_connection.target_system = agent_id[0]
11641169
self._connection.mavlink_connection.target_component = agent_id[1]
11651170

1166-
# Send flight mode
1171+
# Send debug message
11671172
if isinstance(value, int):
11681173
self._connection.mavlink_connection.mav.named_value_int_send(
11691174
int(time.time()), str.encode(name), value
11701175
)
1171-
else:
1176+
elif isinstance(value, float):
11721177
self._connection.mavlink_connection.mav.named_value_float_send(
11731178
int(time.time()), str.encode(name), value
11741179
)
1180+
elif isinstance(value, list):
1181+
if len(value) != 3:
1182+
raise ValueError(
1183+
"Invalid number of debug vector elements provided. "
1184+
f"Expected 3, got {len(value)}"
1185+
)
1186+
self._connection.mavlink_connection.mav.debug_vect_send(
1187+
str.encode(name), int(time.time()), *value
1188+
)
11751189

11761190
return
11771191

setup.cfg

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
[metadata]
22
name = pymavswarm
3-
version = 1.0.0
3+
version = 1.1.0
44
description = Python library used to communicate with multiple UAS simultaneously using MAVLink
5-
long_description = file: README.md
6-
long_description_content_type = text/markdown
5+
long_description = file: README.rst
6+
long_description_content_type = text/x-rst
77
url = https://github.com/unl-nimbus-lab/pymavswarm
88
author = Evan Palmer
99
author_email = evanp922@gmail.com

tox.ini

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,9 @@ commands = coverage run -m unittest
1515

1616
[testenv:coverage-report]
1717
deps = coverage
18-
commands = coverage report --skip-covered --skip-empty
18+
commands =
19+
coverage combine
20+
coverage report --skip-covered --skip-empty
1921

2022
[testenv:lint]
2123
skip_install = true

0 commit comments

Comments
 (0)