Skip to content
This repository has been archived by the owner on Aug 15, 2019. It is now read-only.

Commit

Permalink
Make handling arduino command errors more specific
Browse files Browse the repository at this point in the history
  • Loading branch information
PeterJCLaw committed Jan 27, 2018
1 parent fd728d0 commit 10c0525
Showing 1 changed file with 20 additions and 7 deletions.
27 changes: 20 additions & 7 deletions robotd/devices.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import random
import struct
import subprocess
from typing import Any, List, Tuple

import serial

Expand Down Expand Up @@ -224,6 +225,18 @@ def command(self, cmd):
self._buzz_piezo(cmd['buzz'])


class CommandError(RuntimeError):
"""The servo assembly experienced an error in processing a command."""

def __init__(self, command: Tuple[Any, ...], error: str, comments: List[str]) -> None:
self.command = command
self.error = error
self.comments = comments

def __str__(self):
return "\n".join([self.error, ''] + self.comments)


class ServoAssembly(Board):
"""
A servo assembly.
Expand Down Expand Up @@ -277,7 +290,7 @@ def start(self):
self.make_safe()
print('Finished initialising servo assembly on {}'.format(device))

def _command(self, *args):
def _command(self, *args) -> List[str]:
command_id = random.randint(1, 65535)

while True:
Expand All @@ -293,8 +306,8 @@ def _command(self, *args):

print('Sending to servo assembly:', line)

comments = []
results = []
comments = [] # type: List[str]
results = [] # type: List[str]

while True:
line = self.connection.readline()
Expand Down Expand Up @@ -323,10 +336,10 @@ def _command(self, *args):
if b'unknown command' in line:
break # try again
else:
raise RuntimeError(
line[2:].decode('utf-8') +
'\n' +
'\n'.join(comments),
raise CommandError(
args,
line[2:].decode('utf-8'),
comments,
)

elif line.startswith(b'# '):
Expand Down

0 comments on commit 10c0525

Please sign in to comment.