summaryrefslogtreecommitdiff
path: root/decoders/stepper_motor/pd.py
blob: 05463958a28797b196405843cda420f4fd7d543d (plain)
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
94
##
## This file is part of the libsigrokdecode project.
##
## Copyright (C) 2015 Petteri Aimonen <jpa@sigrok.mail.kapsi.fi>
##
## This program is free software; you can redistribute it and/or modify
## it under the terms of the GNU General Public License as published by
## the Free Software Foundation; either version 2 of the License, or
## (at your option) any later version.
##
## This program is distributed in the hope that it will be useful,
## but WITHOUT ANY WARRANTY; without even the implied warranty of
## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
## GNU General Public License for more details.
##
## You should have received a copy of the GNU General Public License
## along with this program; if not, see <http://www.gnu.org/licenses/>.
##

import sigrokdecode as srd

class Decoder(srd.Decoder):
    api_version = 3
    id = 'stepper_motor'
    name = 'Stepper motor'
    longname = 'Stepper motor position / speed'
    desc = 'Absolute position and movement speed from step/dir.'
    license = 'gplv2+'
    inputs = ['logic']
    outputs = ['stepper_motor']
    channels = (
        {'id': 'step', 'name': 'Step', 'desc': 'Step pulse'},
        {'id': 'dir', 'name': 'Direction', 'desc': 'Direction select'},
    )
    options = (
        {'id': 'unit', 'desc': 'Unit', 'default': 'steps',
         'values': ('steps', 'mm')},
        {'id': 'steps_per_mm', 'desc': 'Steps per mm', 'default': 100.0},
    )
    annotations = (
        ('speed', 'Speed'),
        ('position', 'Position')
    )
    annotation_rows = (
        ('speed', 'Speed', (0,)),
        ('position', 'Position', (1,)),
    )

    def __init__(self):
        self.reset()

    def reset(self):
        self.samplerate = None
        self.oldstep = None
        self.ss_prev_step = None
        self.pos = 0
        self.prev_speed = None
        self.prev_pos = None

    def start(self):
        self.out_ann = self.register(srd.OUTPUT_ANN)

        if self.options['unit'] == 'steps':
            self.scale = 1
            self.format = '%0.0f'
            self.unit = 'steps'
        else:
            self.scale = self.options['steps_per_mm']
            self.format = '%0.2f'
            self.unit = 'mm'

    def step(self, ss, direction):
        if self.ss_prev_step is not None:
            if self.samplerate:
                delta = ss - self.ss_prev_step
                speed = self.samplerate / delta / self.scale
                speed_txt = self.format % speed
                self.put(self.ss_prev_step, ss, self.out_ann,
                    [0, [speed_txt + ' ' + self.unit + '/s', speed_txt]])
            pos_txt = self.format % (self.pos / self.scale)
            self.put(self.ss_prev_step, ss, self.out_ann,
                [1, [pos_txt + ' ' + self.unit, pos_txt]])

        self.pos += (1 if direction else -1)
        self.ss_prev_step = ss

    def metadata(self, key, value):
        if key == srd.SRD_CONF_SAMPLERATE:
            self.samplerate = value

    def decode(self):
        while True:
            step, direction = self.wait({0: 'r'})
            self.step(self.samplenum, direction)