Skip to content

Commit

Permalink
slight performance improvement, updated documentation, example, ready…
Browse files Browse the repository at this point in the history
… for version 1.0.5
  • Loading branch information
LiamBindle committed Sep 17, 2017
1 parent 6cd1255 commit 9223cf5
Show file tree
Hide file tree
Showing 6 changed files with 24 additions and 26 deletions.
27 changes: 16 additions & 11 deletions docs/source/index.rst
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
PyVESC Documentation
====================
********************

.. toctree::
:titlesonly:


PyVESC is aimed at being a easy to use and robust python implementation of the
communication protocol used by the
`VESC - Open Source ESC <http://vedder.se/2015/01/vesc-open-source-esc/>`_
Expand Down Expand Up @@ -32,7 +37,7 @@ PyVESC can be used to go from a message (VESCMessage) to a packet (bytes).
.. code-block:: python
# make a SetDutyCycle message
my_msg = pyvesc.SetDutyCycle(255)
my_msg = pyvesc.SetDutyCycle(1e5)
print(my_msg.duty_cycle) # prints value of my_msg.duty_cycle
my_packet = pyvesc.encode(my_msg)
# my_packet (type: bytes) can now be sent over your UART connection
Expand Down Expand Up @@ -64,7 +69,7 @@ PyVESC serves two purposes:
#. Performs message encoding (to packet) and robust message decoding (to message object)

Messages
--------
========
Here is a list of the messages currently supported in PyVESC. Note that not all
of VESC's messages are implemented. This is because we have only implemented the
messages we use as we don't want to distribute anything that hasn't been tested.
Expand All @@ -79,7 +84,7 @@ It should be noted that all message objects can be created in 3 ways:
#. From decoding the next packet in a buffer

Setter Messages
^^^^^^^^^^^^^^^
===============
These are the setter messages which are currently implemented.

.. autoclass:: pyvesc.SetDutyCycle
Expand All @@ -90,14 +95,14 @@ These are the setter messages which are currently implemented.
.. autoclass:: pyvesc.SetRotorPositionMode

Getter Messages
^^^^^^^^^^^^^^^
===============
These are the getters that are currently implemented.

.. autoclass:: pyvesc.GetValues
.. autoclass:: pyvesc.GetRotorPosition

Implementing Additional Messages
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
================================
Here we'll take a look at how to implement your own messages. You're message
class must have the metaclass `pyvesc.VESCMessage`. In addition to this you must
define two static attributes:
Expand All @@ -116,16 +121,16 @@ the SetDutyCycle message.
class SetDutyCycle(metaclass=pyvesc.VESCMessage):
id = 5
fields = [
('duty_cycle', 'f')
('duty_cycle', 'i')
]
That's it! Taking a look at the declaration we see:

* The message's ID is 5
* The message has a single field with a name `duty_cycle` and type float32 (this
* The message has a single field with a name `duty_cycle` and type int (this
is what the
`format characters <https://docs.python.org/3.5/library/struct.html#format-characters>`_
`'f'` is)
`'i'` is)

If you are interested in the details of how this works, the `pyvesc.VESCMessage`
metaclass has a registry of all its children this registry is a dictionary
Expand All @@ -135,7 +140,7 @@ unique.


Encoding
--------
========
The following is the function call you should use to get a
packet for your message.

Expand All @@ -145,7 +150,7 @@ Encoding is done by first serializing the message object and then framing it
in a VESC packet.

Decoding
--------
========
The following is the function you should call to decode messages from the
buffer.

Expand Down
2 changes: 1 addition & 1 deletion pyvesc/examples/simple.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

def simple_example():
# lets make a SetDuty message
my_msg = pyvesc.SetDutyCycle(255)
my_msg = pyvesc.SetDutyCycle(1e5)

# now lets encode it to make get a byte string back
packet = pyvesc.encode(my_msg)
Expand Down
1 change: 1 addition & 0 deletions pyvesc/interface.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import pyvesc.messages.base
import pyvesc.packet.codec


def decode(buffer):
"""
Decodes the next valid VESC message in a buffer.
Expand Down
10 changes: 1 addition & 9 deletions pyvesc/messages/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,8 @@ def __init__(cls, name, bases, clsdict):
cls._field_scalars = []
for field, idx in zip(cls.fields, range(0, len(cls.fields))):
cls._field_names.append(field[0])
try:
if len(field) >= 3:
cls._field_scalars.append(field[2])
except IndexError:
pass
if field[1] is 's':
# string field, add % so we can vary the length
cls._fmt_fields += '%u'
Expand Down Expand Up @@ -96,12 +94,6 @@ def pack(instance, header_only = None):
return struct.pack(VESCMessage._endian_fmt + VESCMessage._id_fmt, instance.id)

field_values = []
#for field_name,field_scalar in zip(instance._field_names, instance._field_scalars):
#print(field_name, field_scalar)
#if instance._field_scalars:
# for field_name, field_scalar in zip(instance._field_names, instance._field_scalars):
# field_values.append(getattr(instance, field_name*field_scalar))
#else:
if not instance._field_scalars:
for field_name in instance._field_names:
field_values.append(getattr(instance, field_name))
Expand Down
8 changes: 4 additions & 4 deletions pyvesc/messages/setters.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,9 @@ class SetRPM(metaclass=VESCMessage):


class SetCurrent(metaclass=VESCMessage):
""" Set the current to the motor.
""" Set the current (in milliamps) to the motor.
:ivar current: Value to set the current to.
:ivar current: Value to set the current to (in milliamps).
"""
id = 6
fields = [
Expand All @@ -35,9 +35,9 @@ class SetCurrent(metaclass=VESCMessage):


class SetCurrentBrake(metaclass=VESCMessage):
""" Set the current brake.
""" Set the current brake (in milliamps).
:ivar current_brake: Value to set the current brake to.
:ivar current_brake: Value to set the current brake to (in milliamps).
"""
id = 7
fields = [
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from setuptools import setup

VERSION = '1.0.4'
VERSION = '1.0.5'

setup(
name = 'pyvesc',
Expand Down

0 comments on commit 9223cf5

Please sign in to comment.