-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsendcommand.py
More file actions
93 lines (61 loc) · 2.95 KB
/
sendcommand.py
File metadata and controls
93 lines (61 loc) · 2.95 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
import argparse
import enum
import siromlib
import canlib
def main():
parser = argparse.ArgumentParser(
description='Send commands to a SIROM device via CAN bus',
epilog='If (when) nothing works, check that the can network adapter is correctly set up',
formatter_class=argparse.ArgumentDefaultsHelpFormatter
)
parser.add_argument('-v', '--verbose', help='Enable verbose output', action='store_true')
parser.add_argument('-i', '--interface', help='CAN interface', type=str, default='can0')
parser.add_argument('-d', '--device', help='SIROM device ID', type=int, default=0x01)
parser.add_argument('-s', '--speed', help='SIROM motor speed [0-255]', type=int, default=50)
parser.add_argument('-p', '--power', dest='powerConfigOption', help='SIROM power setting', type=siromlib.SiromPowerConfig, action=EnumAction, default=siromlib.SiromPowerConfig.POWERCFG_F2R)
parser.add_argument('command', help='Command to send to sirom', type=siromlib.SiromCommandType, action=EnumAction)
args = parser.parse_args()
if args.verbose:
print(args)
print(f'Checking interface {args.interface} confuguration... ', end='')
print('OK.')
command = siromlib.SiromCommand(
commandType = args.command,
motorSpeed = siromlib.SiromMotorSpeed(args.speed),
powerConfig = args.powerConfigOption
)
print(f'Sending command {command.type.name} to SIROM {args.device} over {args.interface}... ', end='')
with canlib.LinuxCanBusTransport(args.interface) as bus:
bus.sendCommand(canbusTargetIdentifer=args.device, command=command, verbose=args.verbose)
print('Done! \n')
class EnumAction(argparse.Action):
def __init__(self, **kwargs):
# Pop off the type value
enum_type = kwargs.pop("type", None)
# Ensure an Enum subclass is provided
if enum_type is None:
raise ValueError(
"type must be assigned an Enum when using EnumAction")
if not issubclass(enum_type, enum.Enum):
raise TypeError("type must be an Enum when using EnumAction")
# Generate choices from the Enum
kwargs.setdefault("choices", tuple(e.name for e in enum_type))
super(EnumAction, self).__init__(**kwargs)
self._enum = enum_type
def __call__(self,
parser: argparse.ArgumentParser,
namespace: argparse.Namespace,
value,
option_string: str = None):
# Convert value back into an Enum
if isinstance(value, str):
value = self._enum[value]
setattr(namespace, self.dest, value)
elif value is None:
raise argparse.ArgumentTypeError(
f"You need to pass a value after {option_string}!")
else:
# A pretty invalid choice message will be generated by argparse
raise argparse.ArgumentTypeError()
if __name__ == '__main__':
main()