Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Examples for Python client library #53

Merged
merged 5 commits into from
Mar 24, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 29 additions & 1 deletion python/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,36 @@ pip install libsbp
```python
```

## Examples

### Simple example

Receives SBP messages over a serial port, decodes `MSG_BASELINE` messages and
prints them out.

See the [`source code`](sbp/client/examples/simple.py).

Run this example with:

```shell
$ python -m sbp.client.examples.simple -p /path/to/serial/port
```

### Sending SBP messages over UDP

Receives SBP messages over a serial port and sends all incoming messages to a
UDP socket.

See the [`source code`](sbp/client/examples/udp.py).

Run this example with:

```shell
$ python -m sbp.client.examples.udp -s /path/to/serial/port
```

## LICENSE

Copyright © 2015 Swift Navigation

Distributed under LGPLv3.0.
Distributed under LGPLv3.0.
Empty file.
64 changes: 64 additions & 0 deletions python/sbp/client/examples/simple.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
# Copyright (C) 2015 Swift Navigation Inc.
# Contact: Fergus Noble <fergus@swiftnav.com>
#
# This source is subject to the license found in the file 'LICENSE' which must
# be be distributed together with this source. All other rights reserved.
#
# THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND,
# EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE IMPLIED
# WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A PARTICULAR PURPOSE.

"""
the :mod:`sbp.client.examples.simple` module contains a basic example of
reading SBP messages from a serial port, decoding BASELINE_NED messages and
printing them out.
"""

from sbp.client.drivers.pyserial_driver import PySerialDriver
from sbp.client.handler import Handler
from sbp.navigation import SBP_MSG_BASELINE_NED, MsgBaselineNED

import time

def baseline_callback(msg):
# This function is called every time we receive a BASELINE_NED message

# First decode the SBP message in "msg" into a python object, the sbp library
# has functions that do this for all the message types defined in the
# specification.
b = MsgBaselineNED(msg)

# b now contains the decoded baseline information and
# has fields with the same names as in the SBP docs

# Print out the N, E, D coordinates of the baseline
print "%.4f,%.4f,%.4f" % \
(b.n * 1e-3, b.e * 1e-3, b.d * 1e-3)

def main():

import argparse
parser = argparse.ArgumentParser(description="Swift Navigation SBP Example.")
parser.add_argument("-p", "--port",
default=['/dev/ttyUSB0'], nargs=1,
help="specify the serial port to use.")
args = parser.parse_args()

# Open a connection to Piksi using the default baud rate (1Mbaud)
with PySerialDriver(args.port[0], baud=1000000) as driver:
# Create a handler to connect our Piksi driver to our callbacks
with Handler(driver.read, driver.write, verbose=True) as handler:
# Add a callback for BASELINE_NED messages
handler.add_callback(baseline_callback, msg_type=SBP_MSG_BASELINE_NED)
handler.start()

# Sleep until the user presses Ctrl-C
try:
while True:
time.sleep(0.1)
except KeyboardInterrupt:
pass

if __name__ == "__main__":
main()

79 changes: 79 additions & 0 deletions python/sbp/client/examples/udp.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
# Copyright (C) 2015 Swift Navigation Inc.
# Contact: Fergus Noble <fergus@swiftnav.com>
#
# This source is subject to the license found in the file 'LICENSE' which must
# be be distributed together with this source. All other rights reserved.
#
# THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND,
# EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE IMPLIED
# WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A PARTICULAR PURPOSE.

"""
the :mod:`sbp.client.examples.udp` module contains an example of reading SBP
messages from a serial port and sending them to a UDP socket.
"""

from sbp.client.drivers.pyserial_driver import PySerialDriver
from sbp.client.handler import Handler

from contextlib import closing
import socket
import struct
import time

DEFAULT_SERIAL_PORT = "/dev/ttyUSB0"
DEFAULT_SERIAL_BAUD = 1000000

DEFAULT_UDP_ADDRESS = "127.0.0.1"
DEFAULT_UDP_PORT = 13320

DEFAULT_LOG_FILENAME = time.strftime("sbp-%Y%m%d-%H%M%S.log")

def get_args():
"""
Get and parse arguments.
"""
import argparse
parser = argparse.ArgumentParser(description="Swift Navigation SBP Example.")
parser.add_argument("-s", "--serial-port",
default=[DEFAULT_SERIAL_PORT], nargs=1,
help="specify the serial port to use.")
parser.add_argument("-b", "--baud",
default=[DEFAULT_SERIAL_BAUD], nargs=1,
help="specify the baud rate to use.")
parser.add_argument("-a", "--address",
default=[DEFAULT_UDP_ADDRESS], nargs=1,
help="specify the serial port to use.")
parser.add_argument("-p", "--udp-port",
default=[DEFAULT_UDP_PORT], nargs=1,
help="specify the baud rate to use.")
return parser.parse_args()

def send_udp_callback_generator(udp, args):
def send_udp_callback(msg):
s = ""
s += struct.pack("<BHHB", 0x55, msg.msg_type, msg.sender, msg.length)
s += msg.payload
s += struct.pack("<H", msg.crc)
udp.sendto(s, (args.address[0], args.udp_port[0]))

return send_udp_callback

def main():
args = get_args()

with PySerialDriver(args.serial_port[0], args.baud[0]) as driver:
with Handler(driver.read, driver.write) as handler:
with closing(socket.socket(socket.AF_INET, socket.SOCK_DGRAM)) as udp:
handler.add_callback(send_udp_callback_generator(udp, args))
handler.start()

try:
while True:
time.sleep(0.1)
except KeyboardInterrupt:
pass

if __name__ == "__main__":
main()