mirror of
https://github.com/odriverobotics/ODrive.git
synced 2026-08-21 06:23:07 +08:00
Merge pull request #108 from bkinman/feat_python27_tools
Added compatibility for python 2.7
This commit is contained in:
@@ -3,6 +3,8 @@
|
||||
Example usage of the ODrive python library to monitor and control ODrive devices
|
||||
"""
|
||||
|
||||
from __future__ import print_function
|
||||
|
||||
import odrive.core
|
||||
import time
|
||||
import math
|
||||
|
||||
@@ -174,7 +174,7 @@ def create_object(name, json_data, namespace, channel, printer=noprint):
|
||||
attributes[member_name] = attribute
|
||||
|
||||
# Create a type from the property list and instantiate it
|
||||
jit_type = type(namespace, (object,), attributes)
|
||||
jit_type = type(str(namespace), (object,), attributes)
|
||||
new_object = jit_type()
|
||||
return new_object
|
||||
|
||||
|
||||
@@ -2,7 +2,18 @@
|
||||
|
||||
import time
|
||||
import struct
|
||||
from abc import ABC, abstractmethod
|
||||
import sys
|
||||
|
||||
import abc
|
||||
|
||||
if sys.version_info >= (3, 4):
|
||||
ABC = abc.ABC
|
||||
else:
|
||||
ABC = abc.ABCMeta('ABC', (), {})
|
||||
|
||||
if sys.version_info <= (3, 3):
|
||||
from monotonic import monotonic
|
||||
time.monotonic = monotonic
|
||||
|
||||
SYNC_BYTE = 0xAA
|
||||
CRC8_INIT = 0x42
|
||||
@@ -30,6 +41,8 @@ def calc_crc(remainder, value, polynomial, bitwidth):
|
||||
def calc_crc8(remainder, value):
|
||||
if isinstance(value, bytearray) or isinstance(value, bytes) or isinstance(value, list):
|
||||
for byte in value:
|
||||
if not isinstance(byte,int):
|
||||
byte = ord(byte)
|
||||
remainder = calc_crc(remainder, byte, CRC8_DEFAULT, 8)
|
||||
else:
|
||||
remainder = calc_crc(remainder, byte, CRC8_DEFAULT, 8)
|
||||
@@ -38,6 +51,8 @@ def calc_crc8(remainder, value):
|
||||
def calc_crc16(remainder, value):
|
||||
if isinstance(value, bytearray) or isinstance(value, bytes) or isinstance(value, list):
|
||||
for byte in value:
|
||||
if not isinstance(byte, int):
|
||||
byte = ord(byte)
|
||||
remainder = calc_crc(remainder, byte, CRC16_DEFAULT, 16)
|
||||
else:
|
||||
remainder = calc_crc(remainder, value, CRC16_DEFAULT, 16)
|
||||
@@ -59,22 +74,22 @@ class DeviceInitException(Exception):
|
||||
|
||||
|
||||
class StreamSource(ABC):
|
||||
@abstractmethod
|
||||
@abc.abstractmethod
|
||||
def get_bytes(self, deadline):
|
||||
pass
|
||||
|
||||
class StreamSink(ABC):
|
||||
@abstractmethod
|
||||
@abc.abstractmethod
|
||||
def process_bytes(self, bytes):
|
||||
pass
|
||||
|
||||
class PacketSource(ABC):
|
||||
@abstractmethod
|
||||
@abc.abstractmethod
|
||||
def get_packet(self, deadline):
|
||||
pass
|
||||
|
||||
class PacketSink(ABC):
|
||||
@abstractmethod
|
||||
@abc.abstractmethod
|
||||
def process_packet(self, packet):
|
||||
pass
|
||||
|
||||
@@ -272,7 +287,7 @@ class Channel(PacketSink):
|
||||
self._expected_acks[seq_no] = packet[2:]
|
||||
|
||||
else:
|
||||
#if (calc_crc16(crc16, struct.pack('<HBB', PROTOCOL_VERSION, packet[-2], packet[-1]))):
|
||||
# raise Exception("CRC16 mismatch")
|
||||
#if (calc_crc16(CRC16_INIT, struct.pack('<HBB', PROTOCOL_VERSION, packet[-2], packet[-1]))):
|
||||
# raise Exception("CRC16 mismatch")
|
||||
print("endpoint requested")
|
||||
# TODO: handle local endpoint operation
|
||||
|
||||
@@ -30,6 +30,8 @@ class USBBulkTransport(odrive.protocol.PacketSource, odrive.protocol.PacketSink)
|
||||
return string
|
||||
|
||||
def init(self, printer=noprint):
|
||||
# Resetting device to start init from a known state
|
||||
self.dev.reset()
|
||||
# detach kernel driver
|
||||
try:
|
||||
if self.dev.is_kernel_driver_active(1):
|
||||
@@ -82,7 +84,7 @@ class USBBulkTransport(odrive.protocol.PacketSource, odrive.protocol.PacketSink)
|
||||
bufferLen = self.epr.wMaxPacketSize
|
||||
timeout = max(int((deadline - time.monotonic()) * 1000), 0)
|
||||
ret = self.epr.read(bufferLen, timeout)
|
||||
return ret
|
||||
return bytearray(ret)
|
||||
except usb.core.USBError as ex:
|
||||
if ex.errno == 19: # "no such device"
|
||||
raise odrive.protocol.ChannelBrokenException()
|
||||
|
||||
Reference in New Issue
Block a user