]>
Commit | Line | Data |
---|---|---|
1 | ## | |
2 | ## This file is part of the libsigrokdecode project. | |
3 | ## | |
4 | ## Copyright (C) 2015 Petteri Aimonen <jpa@sigrok.mail.kapsi.fi> | |
5 | ## | |
6 | ## This program is free software; you can redistribute it and/or modify | |
7 | ## it under the terms of the GNU General Public License as published by | |
8 | ## the Free Software Foundation; either version 2 of the License, or | |
9 | ## (at your option) any later version. | |
10 | ## | |
11 | ## This program is distributed in the hope that it will be useful, | |
12 | ## but WITHOUT ANY WARRANTY; without even the implied warranty of | |
13 | ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
14 | ## GNU General Public License for more details. | |
15 | ## | |
16 | ## You should have received a copy of the GNU General Public License | |
17 | ## along with this program; if not, see <http://www.gnu.org/licenses/>. | |
18 | ## | |
19 | ||
20 | import sigrokdecode as srd | |
21 | ||
22 | class SamplerateError(Exception): | |
23 | pass | |
24 | ||
25 | class Decoder(srd.Decoder): | |
26 | api_version = 3 | |
27 | id = 'stepper_motor' | |
28 | name = 'Stepper motor' | |
29 | longname = 'Stepper motor position / speed' | |
30 | desc = 'Absolute position and movement speed from step/dir.' | |
31 | license = 'gplv2+' | |
32 | inputs = ['logic'] | |
33 | outputs = ['stepper_motor'] | |
34 | channels = ( | |
35 | {'id': 'step', 'name': 'Step', 'desc': 'Step pulse'}, | |
36 | {'id': 'dir', 'name': 'Direction', 'desc': 'Direction select'}, | |
37 | ) | |
38 | options = ( | |
39 | {'id': 'unit', 'desc': 'Unit', 'default': 'steps', | |
40 | 'values': ('steps', 'mm')}, | |
41 | {'id': 'steps_per_mm', 'desc': 'Steps per mm', 'default': 100.0}, | |
42 | ) | |
43 | annotations = ( | |
44 | ('speed', 'Speed'), | |
45 | ('position', 'Position') | |
46 | ) | |
47 | annotation_rows = ( | |
48 | ('speed', 'Speed', (0,)), | |
49 | ('position', 'Position', (1,)), | |
50 | ) | |
51 | ||
52 | def __init__(self): | |
53 | self.oldstep = None | |
54 | self.ss_prev_step = None | |
55 | self.pos = 0 | |
56 | self.prev_speed = None | |
57 | self.prev_pos = None | |
58 | ||
59 | def start(self): | |
60 | self.out_ann = self.register(srd.OUTPUT_ANN) | |
61 | ||
62 | if self.options['unit'] == 'steps': | |
63 | self.scale = 1 | |
64 | self.format = '%0.0f' | |
65 | self.unit = 'steps' | |
66 | else: | |
67 | self.scale = self.options['steps_per_mm'] | |
68 | self.format = '%0.2f' | |
69 | self.unit = 'mm' | |
70 | ||
71 | def step(self, ss, direction): | |
72 | if self.ss_prev_step is not None: | |
73 | delta = ss - self.ss_prev_step | |
74 | speed = self.samplerate / delta / self.scale | |
75 | speed_txt = self.format % speed | |
76 | pos_txt = self.format % (self.pos / self.scale) | |
77 | self.put(self.ss_prev_step, ss, self.out_ann, | |
78 | [0, [speed_txt + ' ' + self.unit + '/s', speed_txt]]) | |
79 | self.put(self.ss_prev_step, ss, self.out_ann, | |
80 | [1, [pos_txt + ' ' + self.unit, pos_txt]]) | |
81 | ||
82 | self.pos += (1 if direction else -1) | |
83 | self.ss_prev_step = ss | |
84 | ||
85 | def metadata(self, key, value): | |
86 | if key == srd.SRD_CONF_SAMPLERATE: | |
87 | self.samplerate = value | |
88 | ||
89 | def decode(self): | |
90 | if not self.samplerate: | |
91 | raise SamplerateError('Cannot decode without samplerate.') | |
92 | while True: | |
93 | step, direction = self.wait({0: 'r'}) | |
94 | self.step(self.samplenum, direction) |