diff --git a/docs/source/index.rst b/docs/source/index.rst index 12eb838..56846fb 100644 --- a/docs/source/index.rst +++ b/docs/source/index.rst @@ -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 `_ @@ -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 @@ -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. @@ -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 @@ -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: @@ -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 `_ - `'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 @@ -135,7 +140,7 @@ unique. Encoding --------- +======== The following is the function call you should use to get a packet for your message. @@ -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. diff --git a/pyvesc/examples/simple.py b/pyvesc/examples/simple.py index 831cc3f..9740ee0 100644 --- a/pyvesc/examples/simple.py +++ b/pyvesc/examples/simple.py @@ -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) diff --git a/pyvesc/interface.py b/pyvesc/interface.py index e077ced..a974854 100644 --- a/pyvesc/interface.py +++ b/pyvesc/interface.py @@ -1,6 +1,7 @@ import pyvesc.messages.base import pyvesc.packet.codec + def decode(buffer): """ Decodes the next valid VESC message in a buffer. diff --git a/pyvesc/messages/base.py b/pyvesc/messages/base.py index 434f19e..2bd0d9b 100644 --- a/pyvesc/messages/base.py +++ b/pyvesc/messages/base.py @@ -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' @@ -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)) diff --git a/pyvesc/messages/setters.py b/pyvesc/messages/setters.py index 808ff66..5c9ab25 100644 --- a/pyvesc/messages/setters.py +++ b/pyvesc/messages/setters.py @@ -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 = [ @@ -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 = [ diff --git a/setup.py b/setup.py index 9dce5d8..eaa2ead 100644 --- a/setup.py +++ b/setup.py @@ -1,6 +1,6 @@ from setuptools import setup -VERSION = '1.0.4' +VERSION = '1.0.5' setup( name = 'pyvesc',