summaryrefslogtreecommitdiffstats
path: root/awlsim-linuxcnc-hal
blob: 5394f158dd2b7a9863529a4f7ab080b66df63c5f (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
#!/usr/bin/env python
# -*- coding: utf-8 -*-
#
# AWL simulator - LinuxCNC HAL module
#
# Copyright 2013 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 sys
import os
import getopt

from awlsim.main import AwlSim
from awlsim.parser import AwlParser
from awlsim.util import AwlSimError, AwlParserError, printInfo, printWarning, printError, str2bool
from awlsim.version import VERSION_MAJOR, VERSION_MINOR

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
	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).")

def main():
	opt_inputSize = 32
	opt_inputBase = 0
	opt_outputSize = 32
	opt_outputBase = 0
	opt_gui = False

	try:
		(opts, args) = getopt.getopt(sys.argv[1:],
			"hi:I:o:O:g:",
			[ "help", "input-size=", "input-base=",
			  "output-size=", "output-base=", "gui=", ])
	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 len(args) != 1:
		usage()
		return 1
	awlSource = 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:
			p = AwlParser()
			p.parseFile(awlSource)
			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.
			mainwnd.runEventLoop()
		else:
			# Load the program
			s.load(p.getParseTree())

			# Run cyclic execution
			while checkLinuxCNC():
				s.runCycle()
	except KeyboardInterrupt as e:
		printInfo("Awlsim LinuxCNC HAL module shutdown")
	except (AwlParserError, AwlSimError) as e:
		printError(e.getReport())
		return 1
	return 0

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