summaryrefslogtreecommitdiffstats
path: root/awlsim-linuxcnc-hal
blob: 12e53f5a401b20004138fe3e5ab20e74a596d076 (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
#!/usr/bin/env python
# -*- coding: utf-8 -*-
#
# AWL simulator - LinuxCNC HAL module
#
# Copyright 2013-2014 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.
#

from __future__ import division, absolute_import, print_function, unicode_literals
from awlsim.common.compat import *

import sys
import os
import getopt

from awlsim.common.project import *
from awlsim.common.version import *

from awlsim.core.main import *
from awlsim.core.parser import *
from awlsim.core.util import *

try:
	import hal as LinuxCNC_HAL
except ImportError as e:
	printError("Failed to import LinuxCNC HAL module: %s" % str(e))
	sys.exit(1)


# Check presence of LinuxCNC. Returns True, if LinuxCNC is detected.
# Raises KeyboardInterrupt, is LinuxCNC is not detected.
def checkLinuxCNC():
	for lockname in ("/tmp/linuxcnc.lock", "/tmp/emc.lock"):
		try:
			os.stat(lockname)
			return True
		except OSError:
			pass
	if not opt_checkcnc:
		# The check is disabled. Return success.
		return True
	printError("LinuxCNC: doesn't seem to be running")
	raise KeyboardInterrupt

# Create the LinuxCNC HAL pins
def createHalPins(hal,
		  inputBase, inputSize,
		  outputBase, outputSize):
	HAL_BIT, HAL_U32, HAL_S32, HAL_FLOAT = \
		LinuxCNC_HAL.HAL_BIT, LinuxCNC_HAL.HAL_U32, \
		LinuxCNC_HAL.HAL_S32, LinuxCNC_HAL.HAL_FLOAT
	HAL_IN, HAL_OUT, HAL_RO, HAL_RW = \
		LinuxCNC_HAL.HAL_IN, LinuxCNC_HAL.HAL_OUT, \
		LinuxCNC_HAL.HAL_RO, LinuxCNC_HAL.HAL_RW

	# Create the input pins
	for i in range(inputBase, inputBase + inputSize):
		for bit in range(8):
			hal.newpin("input.bit.%d.%d" % (i, bit), HAL_BIT, HAL_IN)
			hal.newparam("input.bit.%d.%d.active" % (i, bit), HAL_BIT, HAL_RW)
		if inputBase + inputSize - i < 4:
			continue
		hal.newpin("input.u32.%d" % i, HAL_U32, HAL_IN)
		hal.newparam("input.u32.%d.active" % i, HAL_BIT, HAL_RW)
		hal.newpin("input.s32.%d" % i, HAL_S32, HAL_IN)
		hal.newparam("input.s32.%d.active" % i, HAL_BIT, HAL_RW)
		hal.newpin("input.float.%d" % i, HAL_FLOAT, HAL_IN)
		hal.newparam("input.float.%d.active" % i, HAL_BIT, HAL_RW)

	# Create the output pins
	for i in range(outputBase, outputBase + outputSize):
		for bit in range(8):
			hal.newpin("output.bit.%d.%d" % (i, bit), HAL_BIT, HAL_OUT)
		if outputBase + outputSize - i < 4:
			continue
		hal.newpin("output.u32.%d" % i, HAL_U32, HAL_OUT)
		hal.newpin("output.s32.%d" % i, HAL_S32, HAL_OUT)
		hal.newpin("output.float.%d" % i, HAL_FLOAT, HAL_OUT)

def loadHardwareModule(awlSim, hal,
		       inputBase, outputBase,
		       inputSize, outputSize):
	# Load the hardware interface layer
	parameters = {
		"hal"			: hal,
		"inputAddressBase"	: str(inputBase),
		"outputAddressBase"	: str(outputBase),
		"inputSize"		: str(inputSize),
		"outputSize"		: str(outputSize),
	}
	hwClass = awlSim.loadHardwareModule("linuxcnc")
	awlSim.registerHardwareClass(hwClass = hwClass,
				     parameters = parameters)

def usage():
	printInfo("awlsim-linuxcnc-hal version %d.%d" % (VERSION_MAJOR, VERSION_MINOR))
	printInfo("")
	printInfo("%s [OPTIONS] AWL-source" % sys.argv[0])
	printInfo("")
	printInfo("Options:")
	printInfo(" -i|--input-size 32      The input area size, in bytes.")
	printInfo(" -I|--input-base 0       The AWL input address base.")
	printInfo(" -o|--output-size 32     The output area size, in bytes.")
	printInfo(" -O|--output-base 0      The AWL output address base.")
	printInfo(" -g|--gui 1/0            Enable/disable the GUI (default off).")
	printInfo("")
	printInfo(" -1|--onecycle           Only run one cycle")
	printInfo(" -C|--check-cnc 1/0      Enable/disable LinuxCNC runtime check (default on).")

def main():
	global opt_checkcnc

	opt_inputSize = 32
	opt_inputBase = 0
	opt_outputSize = 32
	opt_outputBase = 0
	opt_gui = False
	opt_onecycle = False
	opt_checkcnc = True

	try:
		(opts, args) = getopt.getopt(sys.argv[1:],
			"hi:I:o:O:g:C:1",
			[ "help", "input-size=", "input-base=",
			  "output-size=", "output-base=", "gui=",
			  "check-cnc=", "onecycle", ])
	except getopt.GetoptError as e:
		printError(str(e))
		usage()
		return 1
	for (o, v) in opts:
		if o in ("-h", "--help"):
			usage()
			return 0
		if o in ("-i", "--input-size"):
			try:
				opt_inputSize = int(v)
			except ValueError:
				printError("-i|--input-size: Invalid argument")
				return 1
		if o in ("-I", "--input-base"):
			try:
				opt_inputBase = int(v)
			except ValueError:
				printError("-I|--input-base: Invalid argument")
				return 1
		if o in ("-o", "--output-size"):
			try:
				opt_outputSize = int(v)
			except ValueError:
				printError("-o|--output-size: Invalid argument")
				return 1
		if o in ("-O", "--output-base"):
			try:
				opt_outputBase = int(v)
			except ValueError:
				printError("-O|--output-base: Invalid argument")
				return 1
		if o in ("-g", "--gui"):
			opt_gui = str2bool(v)
		if o in ("-1", "--onecycle"):
			opt_onecycle = True
		if o in ("-C", "--check-cnc"):
			opt_checkcnc = str2bool(v)
	if len(args) != 1:
		usage()
		return 1
	awlFile = args[0]

#	try:
#		os.nice(-5)
#	except OSError as e:
#		printWarning("WARNING: Failed to renice LinuxCNC "
#			"awlsim HAL module: %s" % str(e))

	try:
		hal = LinuxCNC_HAL.component("awlsim")
		createHalPins(hal,
			      opt_inputBase, opt_inputSize,
			      opt_outputBase, opt_outputSize)

		if opt_gui:
			from awlsim.gui import MainWindow

			mainwnd = MainWindow.start(initialAwlSource = awlSource)
			s = mainwnd.getSim()
		else:
			source = AwlSource(name = awlFile,
					   filepath = awlFile,
					   sourceBytes = awlFileRead(awlFile, encoding="binary"))
			p = AwlParser()
			p.parseSource(source)
			s = AwlSim()

		loadHardwareModule(awlSim = s, hal = hal,
				   inputBase = opt_inputBase,
				   outputBase = opt_outputBase,
				   inputSize = opt_inputSize,
				   outputSize = opt_outputSize)

		if opt_gui:
			mainwnd.cpuRun()
			#TODO checkLinuxCNC() is not called.
			#TODO --onecycle is not honored
			mainwnd.runEventLoop()
		else:
			# Load the program
			s.load(p.getParseTree())
			s.startup()

			# Run cyclic execution
			while checkLinuxCNC():
				s.runCycle()
				if opt_onecycle:
					break
	except KeyboardInterrupt as e:
		printInfo("Awlsim LinuxCNC HAL module shutdown")
	except (AwlParserError, AwlSimError) as e:
		printError(e.getReport())
		return 1
	except MaintenanceRequest as e:
		if e.requestType in (MaintenanceRequest.TYPE_SHUTDOWN,
				     MaintenanceRequest.TYPE_STOP):
			return 0
		else:
			printError("Received invalid maintenance request %d" %\
				   e.requestType)
	return 0

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