summaryrefslogtreecommitdiffhomepage
path: root/digital/asserv/tools/asserv.py
blob: 548eb79e4da6e8731b1d1c0128df25c4879f655c (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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
import proto, time
import numpy

class Asserv:

    stats_format = {
	    'C': 'HHH',
	    'Z': 'H',
	    'S': 'bbb',
	    'P': 'hhhhhh',
	    'W': 'hhh',
	    }
    # The last occuring stats will increment stats_count, so they have to
    # be in the same order than in asserv program.
    stats_order = 'CZSPW'
    stats_items = {
	    'lc': ('C', 0),
	    'rc': ('C', 1),
	    'a0c': ('C', 2),
	    'a0z': ('Z', 0),
	    'ts': ('S', 0),
	    'as': ('S', 1),
	    'a0s': ('S', 2),
	    'te': ('P', 0),
	    'ti': ('P', 1),
	    'ae': ('P', 2),
	    'ai': ('P', 3),
	    'a0e': ('P', 4),
	    'a0i': ('P', 5),
	    'lw': ('W', 0),
	    'rw': ('W', 1),
	    'a0w': ('W', 2),
	    }

    def __init__ (self, file, **param):
	self.proto = proto.Proto (file, time.time, 0.1)
	def make_handle (s):
	    return lambda *args: self.handle_stats (s, *args)
	for (s, f) in self.stats_format.iteritems ():
	    self.proto.register (s, f, make_handle (s))
	self.stats_enabled = None
	self.param = dict (
		tkp = 0, tki = 0, tkd = 0,
		akp = 0, aki = 0, akd = 0,
		a0kp = 0, a0ki = 0, a0kd = 0,
		E = 1023, I = 1023, b = 15000,
		ta = 1, aa = 1, a0a = 1,
		tsm = 0, asm = 0, tss = 0, ass = 0, a0sm = 0, a0ss = 0
		)
	self.param.update (param)
	self.send_param ()

    def stats (self, *stats_items, **options):
	"""Activate stats."""
	interval = 1
	if 'interval' in options:
	    interval = options['interval']
	# Build list of stats letters.
	stats = [self.stats_items[i][0] for i in stats_items]
	stats = [s for s in self.stats_order if s in stats]
	stats_last_pos = 0
	stats_pos = { }
	for s in stats:
	    stats_pos[s] = stats_last_pos
	    stats_last_pos += len (self.stats_format[s])
	# Build stats item positions.
	self.stats_items_pos = [ ]
	for i in stats_items:
	    id = self.stats_items[i]
	    self.stats_items_pos.append (stats_pos[id[0]] + id[1])
	# Enable stats.
	for s in stats:
	    self.proto.send (s, 'B', interval)
	# Prepare aquisition.
	self.stats_enabled = stats
	self.stats_counter = stats[-1]
	self.stats_count = 0
	self.stats_list = [ ]
	self.stats_line = [ ]

    def get_stats (self, wait = None):
	if wait:
	    self.wait (wait)
	list = self.stats_list
	# Drop first line as it might be garbage.
	del list[0]
	for s in reversed (self.stats_enabled):
	    self.proto.send (s, 'B', 0)
	# Extract asked stats.
	array = numpy.array (list)
	array = array[:, self.stats_items_pos]
	# Cleanup.
	self.stats_enabled = None
	del self.stats_items_pos
	del self.stats_counter
	del self.stats_count
	del self.stats_list
	del self.stats_line
	return array

    def consign (self, w, c):
	"""Consign offset."""
	if w == 't':
	    self.proto.send ('c', 'hh', c, 0)
	elif w == 'a':
	    self.proto.send ('c', 'hh', 0, c)
	else:
	    assert w == 'a0'
	    self.proto.send ('c', 'h', c)

    def speed (self, w, s):
	"""Speed consign."""
	if w == 't':
	    self.proto.send ('s', 'bb', s, 0)
	elif w == 'a':
	    self.proto.send ('s', 'bb', 0, s)
	else:
	    assert w == 'a0'
	    self.proto.send ('s', 'b', s)

    def send_param (self):
	p = self.param
	self.proto.send ('p', 'BHH', ord ('p'), p['tkp'] * 256,
		p['akp'] * 256)
	self.proto.send ('p', 'BHH', ord ('i'), p['tki'] * 256,
		p['aki'] * 256)
	self.proto.send ('p', 'BHH', ord ('d'), p['tkd'] * 256,
		p['akd'] * 256)
	self.proto.send ('p', 'BH', ord ('p'), p['a0kp'] * 256)
	self.proto.send ('p', 'BH', ord ('i'), p['a0ki'] * 256)
	self.proto.send ('p', 'BH', ord ('d'), p['a0kd'] * 256)
	self.proto.send ('p', 'BH', ord ('E'), p['E'])
	self.proto.send ('p', 'BH', ord ('I'), p['I'])
	self.proto.send ('p', 'BH', ord ('b'), p['b'])
	self.proto.send ('p', 'BHH', ord ('a'), p['ta'] * 256,
		p['aa'] * 256)
	self.proto.send ('p', 'BH', ord ('a'), p['a0a'] * 256)
	self.proto.send ('p', 'BBBBB', ord ('s'), p['tsm'], p['asm'],
		p['tss'], p['ass'])

    def handle_stats (self, stat, *args):
	if self.stats_enabled is not None:
	    self.stats_line.extend (args)
	    if self.stats_counter == stat:
		self.stats_list.append (self.stats_line)
		self.stats_line = [ ]
		self.stats_count += 1

    def wait (self, cond = None):
	try:
	    cond_count = int (cond)
	    cond = lambda: self.stats_count > cond_count
	except TypeError:
	    pass
	self.proto.wait (cond)

    def reset (self):
	self.proto.send ('w')
	self.proto.send ('z')

    def close (self):
	self.reset ()
	self.wait (lambda: True)
	self.proto.file.close ()