awlsim-linuxcnc-hal 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431
  1. #!/usr/bin/env python
  2. # -*- coding: utf-8 -*-
  3. #
  4. # AWL simulator - LinuxCNC HAL module
  5. #
  6. # Copyright 2013-2016 Michael Buesch <m@bues.ch>
  7. #
  8. # This program is free software; you can redistribute it and/or modify
  9. # it under the terms of the GNU General Public License as published by
  10. # the Free Software Foundation; either version 2 of the License, or
  11. # (at your option) any later version.
  12. #
  13. # This program is distributed in the hope that it will be useful,
  14. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. # GNU General Public License for more details.
  17. #
  18. # You should have received a copy of the GNU General Public License along
  19. # with this program; if not, write to the Free Software Foundation, Inc.,
  20. # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  21. #
  22. from __future__ import division, absolute_import, print_function, unicode_literals
  23. from awlsim.common.compat import *
  24. import sys
  25. import os
  26. import time
  27. import getopt
  28. from awlsim.common import *
  29. from awlsim.coreserver import *
  30. from awlsim.coreclient import *
  31. class LinuxCNC_NotRunning(Exception):
  32. pass
  33. # Check presence of LinuxCNC.
  34. # Returns normally, if LinuxCNC is detected.
  35. # Raises LinuxCNC_NotRunning, if LinuxCNC is not detected.
  36. def watchdogHook(_unused = None):
  37. # Check whether LinuxCNC is running.
  38. for lockname in ("/tmp/linuxcnc.lock", "/tmp/emc.lock"):
  39. if fileExists(lockname):
  40. return True
  41. if not opt_watchdog:
  42. # The check is disabled. Return success.
  43. return True
  44. printError("LinuxCNC doesn't seem to be running. "\
  45. "(Use '--watchdog off' to disable this check.)")
  46. raise LinuxCNC_NotRunning()
  47. # Create the LinuxCNC HAL pins
  48. def createHalPins(hal,
  49. inputBase, inputSize,
  50. outputBase, outputSize):
  51. HAL_BIT, HAL_U32, HAL_S32, HAL_FLOAT = \
  52. LinuxCNC_HAL.HAL_BIT, LinuxCNC_HAL.HAL_U32, \
  53. LinuxCNC_HAL.HAL_S32, LinuxCNC_HAL.HAL_FLOAT
  54. HAL_IN, HAL_OUT, HAL_RO, HAL_RW = \
  55. LinuxCNC_HAL.HAL_IN, LinuxCNC_HAL.HAL_OUT, \
  56. LinuxCNC_HAL.HAL_RO, LinuxCNC_HAL.HAL_RW
  57. printInfo("Mapped AWL/STL input area: P#E %d.0 BYTE %d" %\
  58. (inputBase, inputSize))
  59. printInfo("Mapped AWL/STL output area: P#A %d.0 BYTE %d" %\
  60. (outputBase, outputSize))
  61. # Create the input pins
  62. for i in range(inputBase, inputBase + inputSize):
  63. offset = i - inputBase
  64. for bit in range(8):
  65. hal.newpin("input.bit.%d.%d" % (i, bit), HAL_BIT, HAL_IN)
  66. hal.newparam("input.bit.%d.%d.active" % (i, bit), HAL_BIT, HAL_RW)
  67. hal.newpin("input.u8.%d" % i, HAL_U32, HAL_IN)
  68. hal.newparam("input.u8.%d.active" % i, HAL_BIT, HAL_RW)
  69. if i % 2:
  70. continue
  71. if inputSize - offset < 2:
  72. continue
  73. hal.newpin("input.u16.%d" % i, HAL_U32, HAL_IN)
  74. hal.newparam("input.u16.%d.active" % i, HAL_BIT, HAL_RW)
  75. hal.newpin("input.s16.%d" % i, HAL_S32, HAL_IN)
  76. hal.newparam("input.s16.%d.active" % i, HAL_BIT, HAL_RW)
  77. if inputSize - offset < 4:
  78. continue
  79. hal.newpin("input.u31.%d" % i, HAL_U32, HAL_IN)
  80. hal.newparam("input.u31.%d.active" % i, HAL_BIT, HAL_RW)
  81. hal.newpin("input.s32.%d" % i, HAL_S32, HAL_IN)
  82. hal.newparam("input.s32.%d.active" % i, HAL_BIT, HAL_RW)
  83. hal.newpin("input.float.%d" % i, HAL_FLOAT, HAL_IN)
  84. hal.newparam("input.float.%d.active" % i, HAL_BIT, HAL_RW)
  85. # Create the output pins
  86. for i in range(outputBase, outputBase + outputSize):
  87. offset = i - outputBase
  88. for bit in range(8):
  89. hal.newpin("output.bit.%d.%d" % (i, bit), HAL_BIT, HAL_OUT)
  90. hal.newparam("output.bit.%d.%d.active" % (i, bit), HAL_BIT, HAL_RW)
  91. hal.newpin("output.u8.%d" % i, HAL_U32, HAL_OUT)
  92. hal.newparam("output.u8.%d.active" % i, HAL_BIT, HAL_RW)
  93. if i % 2:
  94. continue
  95. if outputSize - offset < 2:
  96. continue
  97. hal.newpin("output.u16.%d" % i, HAL_U32, HAL_OUT)
  98. hal.newparam("output.u16.%d.active" % i, HAL_BIT, HAL_RW)
  99. hal.newpin("output.s16.%d" % i, HAL_S32, HAL_OUT)
  100. hal.newparam("output.s16.%d.active" % i, HAL_BIT, HAL_RW)
  101. if outputSize - offset < 4:
  102. continue
  103. hal.newpin("output.u31.%d" % i, HAL_U32, HAL_OUT)
  104. hal.newparam("output.u31.%d.active" % i, HAL_BIT, HAL_RW)
  105. hal.newpin("output.s32.%d" % i, HAL_S32, HAL_OUT)
  106. hal.newparam("output.s32.%d.active" % i, HAL_BIT, HAL_RW)
  107. hal.newpin("output.float.%d" % i, HAL_FLOAT, HAL_OUT)
  108. hal.newparam("output.float.%d.active" % i, HAL_BIT, HAL_RW)
  109. hal.newparam("config.ready", HAL_BIT, HAL_RW)
  110. def usage():
  111. print("awlsim-linuxcnc-hal version %s" % VERSION_STRING)
  112. print("")
  113. print("Usage: awlsim-linuxcnc-hal [OPTIONS] PROJECT.awlpro")
  114. print("")
  115. print("Options:")
  116. print(" -i|--input-size SIZE The input area size, in bytes.")
  117. print(" Overrides input-size from project file.")
  118. print(" -I|--input-base BASE The AWL/STL input address base.")
  119. print(" Overrides input-base from project file.")
  120. print(" -o|--output-size SIZE The output area size, in bytes.")
  121. print(" Overrides output-size from project file.")
  122. print(" -O|--output-base BASE The AWL/STL output address base.")
  123. print(" Overrides output-base from project file.")
  124. print("")
  125. print(" -l|--listen HOST:PORT Set the address and port where the")
  126. print(" awlsim core server should listen on.")
  127. print(" Defaults to %s:%d" %\
  128. (AwlSimServer.DEFAULT_HOST, AwlSimServer.DEFAULT_PORT))
  129. print("")
  130. print(" -L|--loglevel LVL Set the log level:")
  131. print(" 0: Log nothing")
  132. print(" 1: Log errors")
  133. print(" 2: Log errors and warnings")
  134. print(" 3: Log errors, warnings and info messages (default)")
  135. print(" 4: Verbose logging")
  136. print(" 5: Extremely verbose logging")
  137. print(" -N|--nice NICE Renice the process. -20 <= NICE <= 19.")
  138. print(" Default: Do not renice")
  139. print("")
  140. print("Debugging options:")
  141. print(" -W|--watchdog 1/0 Enable/disable LinuxCNC runtime watchdog.")
  142. print(" Default: on")
  143. print(" -M|--max-runtime SEC Module will be stopped after SEC seconds.")
  144. print(" Default: No timeout")
  145. print(" -x|--extended-insns Force-enable extended instructions")
  146. print("")
  147. print("For an example LinuxCNC HAL configuration see:")
  148. print(" examples/linuxcnc-demo/linuxcnc-demo.hal")
  149. def main():
  150. global LinuxCNC_HAL
  151. global opt_inputSize
  152. global opt_inputBase
  153. global opt_outputSize
  154. global opt_outputBase
  155. global opt_listen
  156. global opt_loglevel
  157. global opt_nice
  158. global opt_watchdog
  159. global opt_maxRuntime
  160. global opt_extInsns
  161. opt_inputSize = None
  162. opt_inputBase = None
  163. opt_outputSize = None
  164. opt_outputBase = None
  165. opt_listen = (AwlSimServer.DEFAULT_HOST, AwlSimServer.DEFAULT_PORT)
  166. opt_loglevel = Logging.LOG_INFO
  167. opt_nice = None
  168. opt_watchdog = True
  169. opt_maxRuntime = -1.0
  170. opt_extInsns = None
  171. try:
  172. (opts, args) = getopt.getopt(sys.argv[1:],
  173. "hi:I:o:O:l:L:N:W:M:x",
  174. [ "help", "input-size=", "input-base=",
  175. "output-size=", "output-base=",
  176. "listen=",
  177. "loglevel=", "nice=",
  178. "watchdog=", "max-runtime=", "extended-insns", ])
  179. except getopt.GetoptError as e:
  180. printError(str(e))
  181. usage()
  182. return 1
  183. for (o, v) in opts:
  184. if o in ("-h", "--help"):
  185. usage()
  186. return 0
  187. if o in ("-i", "--input-size"):
  188. try:
  189. opt_inputSize = int(v)
  190. except ValueError:
  191. printError("-i|--input-size: Invalid argument")
  192. return 1
  193. if o in ("-I", "--input-base"):
  194. try:
  195. opt_inputBase = int(v)
  196. except ValueError:
  197. printError("-I|--input-base: Invalid argument")
  198. return 1
  199. if o in ("-o", "--output-size"):
  200. try:
  201. opt_outputSize = int(v)
  202. except ValueError:
  203. printError("-o|--output-size: Invalid argument")
  204. return 1
  205. if o in ("-O", "--output-base"):
  206. try:
  207. opt_outputBase = int(v)
  208. except ValueError:
  209. printError("-O|--output-base: Invalid argument")
  210. return 1
  211. if o in ("-l", "--listen"):
  212. try:
  213. host, port = parseNetAddress(v)
  214. if not host.strip() or\
  215. host in {"any", "all"}:
  216. host = ""
  217. if port is None:
  218. port = AwlSimServer.DEFAULT_PORT
  219. opt_listen = (host, port)
  220. except AwlSimError as e:
  221. printError("-l|--listen: Invalid host/port")
  222. return 1
  223. if o in ("-L", "--loglevel"):
  224. try:
  225. opt_loglevel = int(v)
  226. except ValueError:
  227. printError("-L|--loglevel: Invalid log level")
  228. return 1
  229. if o in ("-N", "--nice"):
  230. try:
  231. opt_nice = int(v)
  232. if opt_nice < -20 or opt_nice > 19:
  233. raise ValueError
  234. except ValueError:
  235. printError("-N|--nice: Invalid niceness level")
  236. return 1
  237. if o in ("-W", "--watchdog"):
  238. opt_watchdog = str2bool(v)
  239. if o in ("-M", "--max-runtime"):
  240. try:
  241. opt_maxRuntime = float(v)
  242. except ValueError:
  243. printError("-M|--max-runtime: Invalid time format")
  244. return 1
  245. if o in ("-x", "--extended-insns"):
  246. opt_extInsns = True
  247. if len(args) != 1:
  248. usage()
  249. return 1
  250. projectFile = args[0]
  251. result = 0
  252. try:
  253. Logging.setPrefix("awlsim-linuxcnc: ")
  254. Logging.setLoglevel(opt_loglevel)
  255. # Adjust process priority
  256. if opt_nice is not None:
  257. try:
  258. os.nice(opt_nice)
  259. except OSError as e:
  260. printError("Failed to renice process to "
  261. "%d: %s" % (opt_nice, str(e)))
  262. return 1
  263. # Try to import the LinuxCNC HAL module
  264. try:
  265. import hal as LinuxCNC_HAL
  266. except ImportError as e:
  267. printError("Failed to import LinuxCNC HAL "
  268. "module: %s" % str(e))
  269. return 1
  270. # Create the LinuxCNC HAL component.
  271. hal = LinuxCNC_HAL.component("awlsim")
  272. # Read the project.
  273. project = Project.fromProjectOrRawAwlFile(projectFile)
  274. hwmodSettings = project.getHwmodSettings()
  275. # Get the 'linuxcnc' hardware module descriptor.
  276. linuxcncHwmodDesc = None
  277. for modDesc in hwmodSettings.getLoadedModules():
  278. if modDesc.getModuleName() == "linuxcnc":
  279. if linuxcncHwmodDesc:
  280. printError("ERROR: More than one 'linuxcnc' hardware "
  281. "module found in the project file. Only "
  282. "one 'linuxcnc' module is supported.")
  283. linuxcncHwmodDesc = modDesc
  284. if not linuxcncHwmodDesc:
  285. if opt_inputBase is None and\
  286. opt_outputBase is None and\
  287. opt_inputSize is None and\
  288. opt_outputSize is None:
  289. printWarning("Warning: Hardware module 'linuxcnc' not "
  290. "included in project file. "
  291. "Loading module nevertheless.")
  292. modDesc = HwmodDescriptor(moduleName = "linuxcnc",
  293. parameters = {
  294. "hal" : None,
  295. "removeOnReset" : "0",
  296. "inputAddressBase" : "0",
  297. "outputAddressBase" : "0",
  298. "inputSize" : "32",
  299. "outputSize" : "32",
  300. })
  301. hwmodSettings.addLoadedModule(modDesc)
  302. linuxcncHwmodDesc = modDesc
  303. # Override hardware module parameters, if required.
  304. linuxcncHwmodDesc.setParameterValue("hal", hal)
  305. linuxcncHwmodDesc.setParameterValue("removeOnReset", "0")
  306. if opt_inputBase is not None:
  307. linuxcncHwmodDesc.setParameterValue("inputAddressBase",
  308. str(opt_inputBase))
  309. if opt_inputSize is not None:
  310. linuxcncHwmodDesc.setParameterValue("inputSize",
  311. str(opt_inputSize))
  312. if opt_outputBase is not None:
  313. linuxcncHwmodDesc.setParameterValue("outputAddressBase",
  314. str(opt_outputBase))
  315. if opt_outputSize is not None:
  316. linuxcncHwmodDesc.setParameterValue("outputSize",
  317. str(opt_outputSize))
  318. # Create the HAL pins, as requested.
  319. try:
  320. modDesc = linuxcncHwmodDesc
  321. inputBase = int(modDesc.getParameter("inputAddressBase"))
  322. inputSize = int(modDesc.getParameter("inputSize"))
  323. outputBase = int(modDesc.getParameter("outputAddressBase"))
  324. outputSize = int(modDesc.getParameter("outputSize"))
  325. except ValueError:
  326. printError("ERROR: 'linuxcnc' module hardware parameter "
  327. "value error.")
  328. return 1
  329. createHalPins(hal = hal,
  330. inputBase = inputBase,
  331. inputSize = inputSize,
  332. outputBase = outputBase,
  333. outputSize = outputSize)
  334. # Configure the core and the core server.
  335. server = AwlSimServer()
  336. for modDesc in hwmodSettings.getLoadedModules():
  337. server.loadHardwareModule(modDesc)
  338. server.cpuEnableObTempPresets(project.getObTempPresetsEn())
  339. server.cpuEnableExtendedInsns(project.getExtInsnsEn() or\
  340. opt_extInsns)
  341. #TODO set cycle time limit
  342. server.cpuSetRunTimeLimit(opt_maxRuntime)
  343. server.cpuSetSpecs(project.getCpuSpecs())
  344. # Load the program
  345. for symSrc in project.getSymTabSources():
  346. server.loadSymTabSource(symSrc)
  347. for libSel in project.getLibSelections():
  348. server.loadLibraryBlock(libSel)
  349. for awlSrc in project.getAwlSources():
  350. server.loadAwlSource(awlSrc)
  351. # Set the Linux-CNC watchdog as cycle exit hook.
  352. server.setCycleExitHook(watchdogHook)
  353. # Run the server
  354. printInfo("Starting interpreter core...")
  355. server.startup(host = opt_listen[0],
  356. port = opt_listen[1],
  357. handleExceptionServerside = True,
  358. handleMaintenanceServerside = True)
  359. server.setRunState(server.STATE_RUN)
  360. lastExceptionTime = None
  361. while True:
  362. try:
  363. server.run()
  364. except (AwlParserError, AwlSimError) as e:
  365. now = time.time()
  366. if lastExceptionTime is not None and\
  367. now - lastExceptionTime < 1.0:
  368. # The last fault is less than one second
  369. # in the past. Raise a fatal exception.
  370. printError("Fatal fault detected")
  371. raise e
  372. else:
  373. lastExceptionTime = now
  374. # Non-fatal fault.
  375. # Ensure the CPU is stopped and enter
  376. # the run loop again.
  377. printError(e.getReport())
  378. printError("CPU stopped due to fault")
  379. server.setRunState(server.STATE_STOP)
  380. continue
  381. # Run loop exited normally. Bail out.
  382. break
  383. except LinuxCNC_NotRunning as e:
  384. result = 1
  385. except KeyboardInterrupt as e:
  386. result = 1
  387. except (AwlParserError, AwlSimError) as e:
  388. printError(e.getReport())
  389. result = 1
  390. except MaintenanceRequest as e:
  391. if e.requestType in (MaintenanceRequest.TYPE_SHUTDOWN,
  392. MaintenanceRequest.TYPE_STOP,
  393. MaintenanceRequest.TYPE_RTTIMEOUT):
  394. result = 0
  395. else:
  396. printError("Received invalid maintenance request %d" %\
  397. e.requestType)
  398. result = 1
  399. printInfo("LinuxCNC HAL module shutdown.")
  400. return result
  401. if __name__ == "__main__":
  402. sys.exit(main())