]>
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 Decoder(srd.Decoder): | |
23 | api_version = 3 | |
24 | id = 'stepper_motor' | |
25 | name = 'Stepper motor' | |
26 | longname = 'Stepper motor position / speed' | |
27 | desc = 'Absolute position and movement speed from step/dir.' | |
28 | license = 'gplv2+' | |
29 | inputs = ['logic'] | |
30 | outputs = ['stepper_motor'] | |
31 | tags = ['Embedded/industrial'] | |
32 | channels = ( | |
33 | {'id': 'step', 'name': 'Step', 'desc': 'Step pulse'}, | |
34 | {'id': 'dir', 'name': 'Direction', 'desc': 'Direction select'}, | |
35 | ) | |
36 | options = ( | |
37 | {'id': 'unit', 'desc': 'Unit', 'default': 'steps', | |
38 | 'values': ('steps', 'mm')}, | |
39 | {'id': 'steps_per_mm', 'desc': 'Steps per mm', 'default': 100.0}, | |
40 | ) | |
41 | annotations = ( | |
42 | ('speed', 'Speed'), | |
43 | ('position', 'Position') | |
44 | ) | |
45 | annotation_rows = ( | |
46 | ('speed', 'Speed', (0,)), | |
47 | ('position', 'Position', (1,)), | |
48 | ) | |
49 | ||
50 | def __init__(self): | |
51 | self.reset() | |
52 | ||
53 | def reset(self): | |
54 | self.samplerate = None | |
55 | self.oldstep = None | |
56 | self.ss_prev_step = None | |
57 | self.pos = 0 | |
58 | self.prev_speed = None | |
59 | self.prev_pos = None | |
60 | ||
61 | def start(self): | |
62 | self.out_ann = self.register(srd.OUTPUT_ANN) | |
63 | ||
64 | if self.options['unit'] == 'steps': | |
65 | self.scale = 1 | |
66 | self.format = '%0.0f' | |
67 | self.unit = 'steps' | |
68 | else: | |
69 | self.scale = self.options['steps_per_mm'] | |
70 | self.format = '%0.2f' | |
71 | self.unit = 'mm' | |
72 | ||
73 | def step(self, ss, direction): | |
74 | if self.ss_prev_step is not None: | |
75 | if self.samplerate: | |
76 | delta = ss - self.ss_prev_step | |
77 | speed = self.samplerate / delta / self.scale | |
78 | speed_txt = self.format % speed | |
79 | self.put(self.ss_prev_step, ss, self.out_ann, | |
80 | [0, [speed_txt + ' ' + self.unit + '/s', speed_txt]]) | |
81 | pos_txt = self.format % (self.pos / self.scale) | |
82 | self.put(self.ss_prev_step, ss, self.out_ann, | |
83 | [1, [pos_txt + ' ' + self.unit, pos_txt]]) | |
84 | ||
85 | self.pos += (1 if direction else -1) | |
86 | self.ss_prev_step = ss | |
87 | ||
88 | def metadata(self, key, value): | |
89 | if key == srd.SRD_CONF_SAMPLERATE: | |
90 | self.samplerate = value | |
91 | ||
92 | def decode(self): | |
93 | while True: | |
94 | step, direction = self.wait({0: 'r'}) | |
95 | self.step(self.samplenum, direction) |