aboutsummaryrefslogtreecommitdiffstats
path: root/firmware/simulator/xytronic_simulator.py
blob: 68b7af8f158ab0c085e878bfc218d294ba48632d (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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
#!/usr/bin/env python3
"""
# Xytronic LF-1600
# Open Source firmware
# Simulator
#
# Copyright (c) 2018 Michael Buesch <m@bues.ch>
#
# 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, write to the Free Software Foundation, Inc.,
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
"""

import pyxytronic
import time
import sys


class Simulator(object):
	CURR_DIV	= 10.0
	ADC_TEMP	= 1
	ADC_CURRENT	= 2
	PWM_CURRENT	= 1
	FIXPT_SIZE	= 24
	FIXPT_SHIFT	= 6

	SETTINGS = {
		"temp_k[0].kp"		: "fixpt_t",
		"temp_k[0].ki"		: "fixpt_t",
		"temp_k[0].kd"		: "fixpt_t",
		"temp_k[0].d_decay_div"	: "fixpt_t",
		"temp_k[1].kp"		: "fixpt_t",
		"temp_k[1].ki"		: "fixpt_t",
		"temp_k[1].kd"		: "fixpt_t",
		"temp_k[1].d_decay_div"	: "fixpt_t",
		"temp_k[2].kp"		: "fixpt_t",
		"temp_k[2].ki"		: "fixpt_t",
		"temp_k[2].kd"		: "fixpt_t",
		"temp_k[2].d_decay_div"	: "fixpt_t",
		"temp_idle_setpoint"	: "fixpt_t",
		"temp_setpoint"		: "fixpt_t",
		"temp_setpoint_active"	: "uint8_t",
		"temp_adj"		: "fixpt_t",
		"serial"		: "uint8_t",
	}

	def __init__(self):
		self.xy = pyxytronic
		self.__uartbuf = bytearray()
		self.__reset_debuginterface()
		self.__runHook = []

	def addRunHook(self, hook):
		self.__runHook.append(hook)

	def run(self):
		self.xy.simulator_mainloop_once()
		self.__handle_debuginterface()
		for hook in self.__runHook:
			hook();

	def setting_get(self, name):
		value = self.xy.simulator_setting_read(name)
		if self.SETTINGS[name] == "fixpt_t":
			value = self.__fixptToFloat(value)
		return value

	def setting_set(self, name, value):
		if self.SETTINGS[name] == "fixpt_t":
			value = self.__floatToFixpt(value)
		self.xy.simulator_setting_write(name, value)

	def pwm_current_get(self):
		value, maxValue = self.xy.simulator_pwm_get(self.PWM_CURRENT)
		amps = self.__scale(raw=value,
				    raw_lo=maxValue,
				    raw_hi=0,
				    phys_lo=0.0,
				    phys_hi=5.0)
		return amps

	def adc_temp_set(self, degree):
		value = self.__unscale(phys=degree,
				       phys_lo=150.0,
				       phys_hi=480.0,
				       raw_lo=210,
				       raw_hi=411)
		self.xy.simulator_adc_set(self.ADC_TEMP, value)

	def adc_current_set(self, amps):
		value = self.__unscale(phys=amps,
				       phys_lo=0.0,
				       phys_hi=1.1,
				       raw_lo=0,
				       raw_hi=65)
		self.xy.simulator_adc_set(self.ADC_CURRENT, value)

	def __do_scale(self, raw, raw_lo, raw_hi, phys_lo, phys_hi):
		#        (phys_hi - phys_lo) * (raw - raw_lo)
		# ret = -------------------------------------- + phys_lo
		#                 raw_hi - raw_lo
		a = (phys_hi - phys_lo) * (raw - raw_lo)
		b = raw_hi - raw_lo
		ret = (a / b) + phys_lo
		return ret

	def __scale(self, raw, raw_lo, raw_hi, phys_lo, phys_hi):
		return float(self.__do_scale(int(raw), int(raw_lo), int(raw_hi),
					     float(phys_lo), float(phys_hi)))

	def __unscale(self, phys, phys_lo, phys_hi, raw_lo, raw_hi):
		return int(round(self.__do_scale(float(phys), float(phys_lo), float(phys_hi),
						 int(raw_lo), int(raw_hi))))

	def __uart_tx_get_line(self):
		data = self.xy.simulator_uart_get_tx()
		if data:
			self.__uartbuf += data
			i = self.__uartbuf.find(b'\r\n')
			if i >= 0:
				line = self.__uartbuf[:i]
				del self.__uartbuf[:i+2]
				if line:
					return line.decode("utf-8", "ignore").strip()
		return ""

	@classmethod
	def __floatToFixpt(cls, f):
		f = float(f)
		if f < 0.0:
			return int((f * float(1 << cls.FIXPT_SHIFT)) - 0.5)
		return int((f * float(1 << cls.FIXPT_SHIFT)) + 0.5)

	@classmethod
	def __fixptToFloat(cls, fixpt):
		mask = (1 << cls.FIXPT_SIZE) - 1
		upperMask = (mask >> cls.FIXPT_SHIFT) << cls.FIXPT_SHIFT
		lowerMask = mask ^ upperMask

		# sign
		if fixpt & (1 << (cls.FIXPT_SIZE - 1)):
			fixpt = -((~fixpt + 1) & mask)

		fact = -1 if fixpt < 0 else 1
		fixpt *= fact

		f = float((fixpt & upperMask) >> cls.FIXPT_SHIFT)
		f += float(fixpt & lowerMask) / float(lowerMask + 1)

		f *= fact
		return f

	def __reset_debuginterface(self):
		self.dbg_currentRealR = 0.0
		self.dbg_currentUsedR = 0.0
		self.dbg_currentRState = 0
		self.dbg_currentY = 0.0
		self.dbg_tempR = 0.0
		self.dbg_tempY1 = 0.0
		self.dbg_tempY2 = 0.0
		self.dbg_measCurr = 0
		self.dbg_filtCurr = 0
		self.dbg_measTemp = 0
		self.dbg_boostMode = 0

	@classmethod
	def __parseInt(cls, valStr, valIdent):
		try:
			val = int(valStr, 10)
		except ValueError:
			cls.error("Invalid %s" % valIdent)
			return 0
		return val

	@classmethod
	def __parseFixpt(cls, valStr, valIdent):
		val = cls.__parseInt(valStr, valIdent)
		val = float(val) / (1 << 6)
		return val

	def __handle_debuginterface(self):
		line = self.__uart_tx_get_line()
		if not line:
			return

		if line == "st":
			self.__reset_debuginterface()
			return

		i = line.find(':')
		if i < 0:
			self.error("Time stamp not found")
			return
		try:
			timeStamp = int(line[:i], 16)
		except ValueError:
			self.error("Invalid time stamp format")
			return
		line = line[i+1:].strip()

		elems = line.split()
		if len(elems) != 2:
			self.error("Unknown format: %s" % line)
			return
		if elems[0] == "cr1":
			self.dbg_currentRealR = self.__parseFixpt(elems[1], "cr1") / self.CURR_DIV
		elif elems[0] == "cr2":
			self.dbg_currentUsedR = self.__parseFixpt(elems[1], "cr2") / self.CURR_DIV
		elif elems[0] == "rs":
			self.dbg_currentRState = self.__parseInt(elems[1], "rs")
		elif elems[0] == "cy":
			self.dbg_currentY = self.__parseFixpt(elems[1], "cy") / self.CURR_DIV
		elif elems[0] == "tr":
			self.dbg_tempR = self.__parseFixpt(elems[1], "tr")
		elif elems[0] == "ty1":
			self.dbg_tempY1 = self.__parseFixpt(elems[1], "ty1")
		elif elems[0] == "ty2":
			self.dbg_tempY2 = self.__parseFixpt(elems[1], "ty2") / self.CURR_DIV
		elif elems[0] == "tb":
			self.dbg_boostMode = self.__parseInt(elems[1], "tb")
		elif elems[0] == "mc":
			self.dbg_measCurr = self.__parseInt(elems[1], "mc")
		elif elems[0] == "fc":
			self.dbg_filtCurr = self.__parseInt(elems[1], "fc")
		elif elems[0] == "mt":
			self.dbg_measTemp = self.__parseInt(elems[1], "mt")
		else:
			self.error("Unknown elem: %s" % elems[0])
			return

	@classmethod
	def error(cls, msg):
		print(msg)

	@classmethod
	def info(cls, msg):
		print(msg)

class IronTempSimulator(object):
	def __init__(self, sim):
		self.sim = sim
		self.sim.addRunHook(self.__run)

	def __run(self):
		self.sim.adc_temp_set(100)#TODO

class IronCurrentSimulator(object):
	def __init__(self, sim):
		self.sim = sim
		self.sim.addRunHook(self.__run)

	def __run(self):
		curY = self.sim.pwm_current_get()
		self.sim.adc_current_set(4)#TODO

def main():
	pyxytronic.simulator_init()
	try:
		sim = Simulator()
		tempSim = IronTempSimulator(sim)
		curSim = IronCurrentSimulator(sim)
		while 1:
			sim.run()
			time.sleep(0.0005)
	finally:
		pyxytronic.simulator_exit()
	return 0

if __name__ == "__main__":
	sys.exit(main())
bues.ch cgit interface