-
Notifications
You must be signed in to change notification settings - Fork 45
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'master' into xc_pydronecan
- Loading branch information
Showing
11 changed files
with
283 additions
and
44 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -9,5 +9,5 @@ | |
# David Buzz | ||
# | ||
|
||
__version__ = '1.0.25' | ||
__version__ = '1.0.26' | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,104 @@ | ||
#!/usr/bin/env python3 | ||
import dronecan, time, threading, socket | ||
|
||
# get command line arguments | ||
from argparse import ArgumentParser | ||
parser = ArgumentParser(description='mavlink DroneCAN tunnel') | ||
parser.add_argument("--node-id", default=100, type=int, help="CAN node ID") | ||
parser.add_argument("--debug", action='store_true', help="enable debug") | ||
parser.add_argument("--tcp-host", type=str, default="localhost", help="tcp server host") | ||
parser.add_argument("--tcp-port", type=int, default=5790, help="tcp server port") | ||
parser.add_argument("--rate", type=float, default=10, help="broadcast rate Hz") | ||
parser.add_argument("uri", default=None, type=str, help="CAN URI") | ||
args = parser.parse_args() | ||
|
||
# Create a TCP/IP socket | ||
tcp_sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) | ||
|
||
# Bind the socket to the address | ||
server_address = (args.tcp_host, args.tcp_port) | ||
tcp_sock.bind(server_address) | ||
|
||
# Listen for incoming connections | ||
tcp_sock.listen(1) | ||
connection = None | ||
|
||
def handle_Targetted(msg): | ||
'''handle Targetted message''' | ||
global last_mode, is_armed, connection | ||
if args.debug: | ||
# display the message on the console in human readable format | ||
print(dronecan.to_yaml(msg)) | ||
|
||
if msg.message.target_node == args.node_id: | ||
if msg.message.buffer: | ||
if connection: | ||
connection.sendall(bytes(msg.message.buffer)) | ||
|
||
def publish_NodeInfo(): | ||
msg = dronecan.uavcan.protocol.NodeStatus() | ||
msg.uptime_sec = int(time.time()) | ||
msg.health = msg.HEALTH_OK | ||
msg.mode = msg.MODE_OPERATIONAL | ||
msg.submode = 0 | ||
msg.vendor_specific_status_code = 0 | ||
node.broadcast(msg) | ||
if args.debug: | ||
# display the message on the console in human readable format | ||
print(dronecan.to_yaml(msg)) | ||
|
||
def publish_MavlinkMsg(mav_pkt): | ||
'''send Tunnel message''' | ||
buffer = list(mav_pkt) | ||
chunks = [buffer[i:i + 120] for i in range(0, len(buffer), 120)] | ||
for chunk in chunks: | ||
msg = dronecan.uavcan.tunnel.Targetted() | ||
msg.serial_id = 1 | ||
msg.target_node = 10 | ||
msg.protocol.protocol = 1 # MAVLink2 | ||
msg.baudrate = 115200 | ||
msg.options = msg.OPTION_LOCK_PORT | ||
msg.buffer = chunk | ||
node.broadcast(msg) | ||
if args.debug: | ||
# display the message on the console in human readable format | ||
print(dronecan.to_yaml(msg)) | ||
|
||
def tcp_server_loop(): | ||
global connection | ||
while True: | ||
print(f'Waiting for GCS Connection on tcp:{args.tcp_host}:{args.tcp_port}...') | ||
connection, client_address = tcp_sock.accept() | ||
print("GCS connected") | ||
|
||
try: | ||
# Receive the data in small chunks and retransmit it | ||
while True: | ||
data = connection.recv(1024) | ||
if data: | ||
publish_MavlinkMsg(data) | ||
|
||
finally: | ||
# Clean up the connection | ||
connection.close() | ||
|
||
# Initializing a DroneCAN node instance. | ||
node = dronecan.make_node(args.uri, node_id=args.node_id, bitrate=1000000) | ||
|
||
# Initializing a node monitor, so we can see what nodes are online | ||
node_monitor = dronecan.app.node_monitor.NodeMonitor(node) | ||
|
||
node.periodic(1.0/args.rate, publish_NodeInfo) | ||
|
||
node.add_handler(dronecan.uavcan.tunnel.Targetted, handle_Targetted) | ||
|
||
tcp_server_loop_thread = threading.Thread(target=tcp_server_loop) | ||
tcp_server_loop_thread.start() | ||
|
||
# Running the node until the application is terminated or until first error. | ||
try: | ||
node.spin() | ||
except KeyboardInterrupt: | ||
tcp_server_loop.join() | ||
if connection: | ||
connection.close() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
#!/usr/bin/env python3 | ||
''' | ||
demonstrate servo monitoring and servo control | ||
the selected servo will be given sinisoidal control while printing position | ||
''' | ||
|
||
import dronecan | ||
import time | ||
import math | ||
from dronecan import uavcan | ||
|
||
# get command line arguments | ||
from argparse import ArgumentParser | ||
parser = ArgumentParser(description='test servo actuators') | ||
parser.add_argument("port", default=None, help="CAN URI") | ||
parser.add_argument("--bitrate", default=1000000, type=int, help="CAN bit rate") | ||
parser.add_argument("--node-id", default=125, type=int, help="CAN node ID") | ||
parser.add_argument("--target-node-id", default=100, type=int, help="target CAN node ID") | ||
parser.add_argument("--rate", default=20, type=float, help="servo update rate") | ||
parser.add_argument("--freq", default=1, type=float, help="servo sinisoid frequency") | ||
parser.add_argument("--actuator-min", default=-1, type=float, help="actuator min") | ||
parser.add_argument("--actuator-max", default=1, type=float, help="actuator max") | ||
parser.add_argument("--actuator-id", default=1, type=float, help="actuator ID") | ||
|
||
args = parser.parse_args() | ||
|
||
port = args.port | ||
|
||
# Initializing a DroneCAN node instance. | ||
node = dronecan.make_node(port, node_id=args.node_id, bitrate=args.bitrate) | ||
|
||
def handle_actuator_status(msg): | ||
'''handle actuator status message''' | ||
print(dronecan.to_yaml(msg)) | ||
|
||
def control_actuator(): | ||
'''control for actuator, called at rate Hz''' | ||
t = time.time() | ||
sint = math.sin(t * args.freq * math.pi * 2) | ||
amplitude = 0.5 * (args.actuator_max-args.actuator_min) | ||
mid = args.actuator_min + amplitude | ||
v = mid + sint * amplitude | ||
|
||
req = uavcan.equipment.actuator.ArrayCommand() | ||
cmd = uavcan.equipment.actuator.Command() | ||
cmd.actuator_id = args.actuator_id | ||
cmd.command_type = cmd.COMMAND_TYPE_UNITLESS | ||
cmd.command_value = v | ||
req.commands = [ cmd ] | ||
|
||
node.broadcast(req) | ||
|
||
|
||
# Subscribe only to actuator status messages | ||
node.add_handler(uavcan.equipment.actuator.Status, handle_actuator_status) | ||
|
||
node.periodic(1.0/args.rate, control_actuator) | ||
|
||
while True: | ||
node.spin() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
#!/usr/bin/env python3 | ||
''' | ||
simulate CAN actuators | ||
''' | ||
|
||
import dronecan, time, math | ||
|
||
# get command line arguments | ||
from argparse import ArgumentParser | ||
parser = ArgumentParser(description='simulate DroneCAN servo') | ||
parser.add_argument("--node-id", default=100, type=int, help="CAN node ID") | ||
parser.add_argument("--debug", action='store_true', help="enable debug") | ||
parser.add_argument("--rate", type=float, default=70, help="broadcast rate Hz") | ||
parser.add_argument("uri", default=None, type=str, help="CAN URI") | ||
args = parser.parse_args() | ||
|
||
def publish_ActuatorStatus(): | ||
'''send ActuatorStatus message''' | ||
msg = dronecan.uavcan.equipment.actuator.Status() | ||
msg.position = math.sin(time.time()*math.pi*2) | ||
msg.force = 1 | ||
msg.speed = 2 | ||
msg.power_rating_pct = 17 | ||
|
||
# two actuators | ||
msg.actuator_id = 3 | ||
node.broadcast(msg) | ||
|
||
msg.actuator_id = 5 | ||
node.broadcast(msg) | ||
|
||
if args.debug: | ||
# display the message on the console in human readable format | ||
print(dronecan.to_yaml(msg)) | ||
|
||
# Initializing a DroneCAN node instance. | ||
node = dronecan.make_node(args.uri, node_id=args.node_id, bitrate=1000000) | ||
|
||
# setup to publish servo status | ||
node.periodic(1.0/args.rate, publish_ActuatorStatus) | ||
|
||
# Running the node until the application is terminated | ||
while True: | ||
try: | ||
node.spin() | ||
except KeyboardInterrupt: | ||
break | ||
except dronecan.transport.TransferError as ex: | ||
print(ex) | ||
pass |
Oops, something went wrong.