From: Joseph Coffland Date: Wed, 28 Nov 2018 00:06:23 +0000 (-0800) Subject: Handle id wrapping by restricting ids to 16-bit X-Git-Url: https://git.buildbotics.com/?a=commitdiff_plain;h=f24925e1ae402e2fc2e7e652641b39316f3ad955;p=bbctrl-firmware Handle id wrapping by restricting ids to 16-bit --- diff --git a/src/avr/src/command.c b/src/avr/src/command.c index 9f01b30..498a297 100644 --- a/src/avr/src/command.c +++ b/src/avr/src/command.c @@ -68,7 +68,7 @@ static struct { bool active; - uint32_t id; + uint16_t id; uint32_t last_empty; volatile uint16_t count; float position[AXES]; @@ -284,5 +284,5 @@ bool command_exec() { // Var callbacks -uint32_t get_id() {return cmd.id;} -void set_id(uint32_t id) {cmd.id = id;} +uint16_t get_id() {return cmd.id;} +void set_id(uint16_t id) {cmd.id = id;} diff --git a/src/avr/src/vars.def b/src/avr/src/vars.def index e5a206d..51a7546 100644 --- a/src/avr/src/vars.def +++ b/src/avr/src/vars.def @@ -120,7 +120,7 @@ VAR(hy_rated_rpm, hq, u16, 0, 0, 1) // Huanyang rated RPM VAR(hy_status, hs, u8, 0, 0, 1) // Huanyang status flags // Machine state -VAR(id, id, u32, 0, 1, 1) // Last executed command ID +VAR(id, id, u16, 0, 1, 1) // Last executed command ID VAR(feed_override, fo, u16, 0, 1, 1) // Feed rate override VAR(speed_override, so, u16, 0, 1, 1) // Spindle speed override VAR(optional_pause, op, b8, 0, 1, 1) // Optional pause state diff --git a/src/py/bbctrl/CommandQueue.py b/src/py/bbctrl/CommandQueue.py index b04ad63..75b68ce 100644 --- a/src/py/bbctrl/CommandQueue.py +++ b/src/py/bbctrl/CommandQueue.py @@ -32,6 +32,10 @@ log = logging.getLogger('CmdQ') log.setLevel(logging.WARNING) +# 16-bit less with wrap around +def id_less(a, b): return (1 << 15) < (a - b) % ((1 << 16) - 1) + + class CommandQueue(): def __init__(self): self.lastEnqueueID = 0 @@ -60,7 +64,7 @@ class CommandQueue(): id, cb, args, kwargs = self.q[0] # Execute commands <= releaseID - if self.releaseID < id: return + if id_less(self.releaseID, id): return log.info('releasing id=%d' % id) self.q.popleft() @@ -73,7 +77,7 @@ class CommandQueue(): def release(self, id): - if id and id <= self.releaseID: + if id and not id_less(self.releaseID, id): log.warning('id out of order %d <= %d' % (id, self.releaseID)) self.releaseID = id diff --git a/src/py/bbctrl/Planner.py b/src/py/bbctrl/Planner.py index e1f892e..7e20b37 100644 --- a/src/py/bbctrl/Planner.py +++ b/src/py/bbctrl/Planner.py @@ -310,6 +310,10 @@ class Planner(): def _encode(self, block): + # Handle id wrapping by restricting ids to 16-bit + # See CommandQueue id_less() + block['id'] %= (1 << 16) - 1 + cmd = self.__encode(block) if cmd is not None: