awlsim-linuxcnc-hal 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439
  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. import sys
  24. import os
  25. import time
  26. import getopt
  27. from awlsim_loader.common import *
  28. from awlsim_loader.coreserver import *
  29. from awlsim_loader.coreclient import *
  30. class LinuxCNC_NotRunning(Exception):
  31. pass
  32. # Check presence of LinuxCNC.
  33. # Returns normally, if LinuxCNC is detected.
  34. # Raises LinuxCNC_NotRunning, if LinuxCNC is not detected.
  35. def watchdogHook(_unused = None):
  36. # Check whether LinuxCNC is running.
  37. for lockname in ("/tmp/linuxcnc.lock", "/tmp/emc.lock"):
  38. if fileExists(lockname):
  39. return True
  40. if not opt_watchdog:
  41. # The check is disabled. Return success.
  42. return True
  43. printError("LinuxCNC doesn't seem to be running. "\
  44. "(Use '--watchdog off' to disable this check.)")
  45. raise LinuxCNC_NotRunning()
  46. # Create the LinuxCNC HAL pins
  47. def createHalPins(hal,
  48. inputBase, inputSize,
  49. outputBase, outputSize):
  50. HAL_BIT, HAL_U32, HAL_S32, HAL_FLOAT = \
  51. LinuxCNC_HAL.HAL_BIT, LinuxCNC_HAL.HAL_U32, \
  52. LinuxCNC_HAL.HAL_S32, LinuxCNC_HAL.HAL_FLOAT
  53. HAL_IN, HAL_OUT, HAL_RO, HAL_RW = \
  54. LinuxCNC_HAL.HAL_IN, LinuxCNC_HAL.HAL_OUT, \
  55. LinuxCNC_HAL.HAL_RO, LinuxCNC_HAL.HAL_RW
  56. printInfo("Mapped AWL/STL input area: P#E %d.0 BYTE %d" %\
  57. (inputBase, inputSize))
  58. printInfo("Mapped AWL/STL output area: P#A %d.0 BYTE %d" %\
  59. (outputBase, outputSize))
  60. # Create the input pins
  61. for i in range(inputBase, inputBase + inputSize):
  62. offset = i - inputBase
  63. for bit in range(8):
  64. hal.newpin("input.bit.%d.%d" % (i, bit), HAL_BIT, HAL_IN)
  65. hal.newparam("input.bit.%d.%d.active" % (i, bit), HAL_BIT, HAL_RW)
  66. hal.newpin("input.u8.%d" % i, HAL_U32, HAL_IN)
  67. hal.newparam("input.u8.%d.active" % i, HAL_BIT, HAL_RW)
  68. if i % 2:
  69. continue
  70. if inputSize - offset < 2:
  71. continue
  72. hal.newpin("input.u16.%d" % i, HAL_U32, HAL_IN)
  73. hal.newparam("input.u16.%d.active" % i, HAL_BIT, HAL_RW)
  74. hal.newpin("input.s16.%d" % i, HAL_S32, HAL_IN)
  75. hal.newparam("input.s16.%d.active" % i, HAL_BIT, HAL_RW)
  76. if inputSize - offset < 4:
  77. continue
  78. hal.newpin("input.u31.%d" % i, HAL_U32, HAL_IN)
  79. hal.newparam("input.u31.%d.active" % i, HAL_BIT, HAL_RW)
  80. hal.newpin("input.s32.%d" % i, HAL_S32, HAL_IN)
  81. hal.newparam("input.s32.%d.active" % i, HAL_BIT, HAL_RW)
  82. hal.newpin("input.float.%d" % i, HAL_FLOAT, HAL_IN)
  83. hal.newparam("input.float.%d.active" % i, HAL_BIT, HAL_RW)
  84. # Create the output pins
  85. for i in range(outputBase, outputBase + outputSize):
  86. offset = i - outputBase
  87. for bit in range(8):
  88. hal.newpin("output.bit.%d.%d" % (i, bit), HAL_BIT, HAL_OUT)
  89. hal.newparam("output.bit.%d.%d.active" % (i, bit), HAL_BIT, HAL_RW)
  90. hal.newpin("output.u8.%d" % i, HAL_U32, HAL_OUT)
  91. hal.newparam("output.u8.%d.active" % i, HAL_BIT, HAL_RW)
  92. if i % 2:
  93. continue
  94. if outputSize - offset < 2:
  95. continue
  96. hal.newpin("output.u16.%d" % i, HAL_U32, HAL_OUT)
  97. hal.newparam("output.u16.%d.active" % i, HAL_BIT, HAL_RW)
  98. hal.newpin("output.s16.%d" % i, HAL_S32, HAL_OUT)
  99. hal.newparam("output.s16.%d.active" % i, HAL_BIT, HAL_RW)
  100. if outputSize - offset < 4:
  101. continue
  102. hal.newpin("output.u31.%d" % i, HAL_U32, HAL_OUT)
  103. hal.newparam("output.u31.%d.active" % i, HAL_BIT, HAL_RW)
  104. hal.newpin("output.s32.%d" % i, HAL_S32, HAL_OUT)
  105. hal.newparam("output.s32.%d.active" % i, HAL_BIT, HAL_RW)
  106. hal.newpin("output.float.%d" % i, HAL_FLOAT, HAL_OUT)
  107. hal.newparam("output.float.%d.active" % i, HAL_BIT, HAL_RW)
  108. hal.newparam("config.ready", HAL_BIT, HAL_RW)
  109. def usage():
  110. print("awlsim-linuxcnc-hal version %s" % VERSION_STRING)
  111. print("")
  112. print("Usage: awlsim-linuxcnc-hal [OPTIONS] PROJECT.awlpro")
  113. print("")
  114. print("Options:")
  115. print(" -i|--input-size SIZE The input area size, in bytes.")
  116. print(" Overrides input-size from project file.")
  117. print(" -I|--input-base BASE The AWL/STL input address base.")
  118. print(" Overrides input-base from project file.")
  119. print(" -o|--output-size SIZE The output area size, in bytes.")
  120. print(" Overrides output-size from project file.")
  121. print(" -O|--output-base BASE The AWL/STL output address base.")
  122. print(" Overrides output-base from project file.")
  123. print("")
  124. print(" -l|--listen HOST:PORT Set the address and port where the")
  125. print(" awlsim core server should listen on.")
  126. print(" Defaults to %s:%d" %\
  127. (AwlSimServer.DEFAULT_HOST, AwlSimServer.DEFAULT_PORT))
  128. print("")
  129. print(" -L|--loglevel LVL Set the log level:")
  130. print(" 0: Log nothing")
  131. print(" 1: Log errors")
  132. print(" 2: Log errors and warnings")
  133. print(" 3: Log errors, warnings and info messages (default)")
  134. print(" 4: Verbose logging")
  135. print(" 5: Extremely verbose logging")
  136. print(" -N|--nice NICE Renice the process. -20 <= NICE <= 19.")
  137. print(" Default: Do not renice")
  138. print("")
  139. print("Debugging options:")
  140. print(" -W|--watchdog 1/0 Enable/disable LinuxCNC runtime watchdog.")
  141. print(" Default: on")
  142. print(" -M|--max-runtime SEC Module will be stopped after SEC seconds.")
  143. print(" Default: No timeout")
  144. print(" -x|--extended-insns Force-enable extended instructions")
  145. print("")
  146. print("For an example LinuxCNC HAL configuration see:")
  147. print(" examples/linuxcnc-demo/linuxcnc-demo.hal")
  148. def main():
  149. global LinuxCNC_HAL
  150. global opt_inputSize
  151. global opt_inputBase
  152. global opt_outputSize
  153. global opt_outputBase
  154. global opt_listen
  155. global opt_loglevel
  156. global opt_nice
  157. global opt_watchdog
  158. global opt_maxRuntime
  159. global opt_extInsns
  160. opt_inputSize = None
  161. opt_inputBase = None
  162. opt_outputSize = None
  163. opt_outputBase = None
  164. opt_listen = (AwlSimServer.DEFAULT_HOST, AwlSimServer.DEFAULT_PORT)
  165. opt_loglevel = Logging.LOG_INFO
  166. opt_nice = None
  167. opt_watchdog = True
  168. opt_maxRuntime = None
  169. opt_extInsns = None
  170. try:
  171. (opts, args) = getopt.getopt(sys.argv[1:],
  172. "hi:I:o:O:l:L:N:W:M:x",
  173. [ "help", "input-size=", "input-base=",
  174. "output-size=", "output-base=",
  175. "listen=",
  176. "loglevel=", "nice=",
  177. "watchdog=", "max-runtime=", "extended-insns", ])
  178. except getopt.GetoptError as e:
  179. printError(str(e))
  180. usage()
  181. return 1
  182. for (o, v) in opts:
  183. if o in ("-h", "--help"):
  184. usage()
  185. return 0
  186. if o in ("-i", "--input-size"):
  187. try:
  188. opt_inputSize = int(v)
  189. except ValueError:
  190. printError("-i|--input-size: Invalid argument")
  191. return 1
  192. if o in ("-I", "--input-base"):
  193. try:
  194. opt_inputBase = int(v)
  195. except ValueError:
  196. printError("-I|--input-base: Invalid argument")
  197. return 1
  198. if o in ("-o", "--output-size"):
  199. try:
  200. opt_outputSize = int(v)
  201. except ValueError:
  202. printError("-o|--output-size: Invalid argument")
  203. return 1
  204. if o in ("-O", "--output-base"):
  205. try:
  206. opt_outputBase = int(v)
  207. except ValueError:
  208. printError("-O|--output-base: Invalid argument")
  209. return 1
  210. if o in ("-l", "--listen"):
  211. try:
  212. host, port = parseNetAddress(v)
  213. if not host.strip() or\
  214. host in {"any", "all"}:
  215. host = ""
  216. if port is None:
  217. port = AwlSimServer.DEFAULT_PORT
  218. opt_listen = (host, port)
  219. except AwlSimError as e:
  220. printError("-l|--listen: Invalid host/port")
  221. return 1
  222. if o in ("-L", "--loglevel"):
  223. try:
  224. opt_loglevel = int(v)
  225. except ValueError:
  226. printError("-L|--loglevel: Invalid log level")
  227. return 1
  228. if o in ("-N", "--nice"):
  229. try:
  230. opt_nice = int(v)
  231. if opt_nice < -20 or opt_nice > 19:
  232. raise ValueError
  233. except ValueError:
  234. printError("-N|--nice: Invalid niceness level")
  235. return 1
  236. if o in ("-W", "--watchdog"):
  237. opt_watchdog = str2bool(v)
  238. if o in ("-M", "--max-runtime"):
  239. try:
  240. opt_maxRuntime = float(v)
  241. except ValueError:
  242. printError("-M|--max-runtime: Invalid time format")
  243. return 1
  244. if o in ("-x", "--extended-insns"):
  245. opt_extInsns = True
  246. if len(args) != 1:
  247. usage()
  248. return 1
  249. projectFile = args[0]
  250. result = ExitCodes.EXIT_OK
  251. server = None
  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.cpuSetSpecs(project.getCpuSpecs())
  339. conf = copy(project.getCpuConf())
  340. if opt_maxRuntime is not None:
  341. conf.setRunTimeLimitUs(int(round(opt_maxRuntime * 1000000.0)))
  342. if opt_extInsns is not None:
  343. conf.setExtInsnsEn(opt_extInsns)
  344. server.cpuSetConf(conf)
  345. # Load the program
  346. for symSrc in project.getSymTabSources():
  347. server.loadSymTabSource(symSrc)
  348. for libSel in project.getLibSelections():
  349. server.loadLibraryBlock(libSel)
  350. for awlSrc in project.getAwlSources():
  351. server.loadAwlSource(awlSrc)
  352. for fupSrc in project.getFupSources():
  353. server.loadFupSource(fupSrc)
  354. for kopSrc in project.getKopSources():
  355. server.loadKopSource(kopSrc)
  356. # Set the Linux-CNC watchdog as cycle exit hook.
  357. server.setCycleExitHook(watchdogHook)
  358. # Run the server
  359. printInfo("Starting interpreter core...")
  360. server.startup(host = opt_listen[0],
  361. port = opt_listen[1],
  362. handleExceptionServerside = True,
  363. handleMaintenanceServerside = True)
  364. server.setRunState(server.STATE_RUN)
  365. lastExceptionTime = None
  366. while True:
  367. try:
  368. server.run()
  369. except (AwlParserError, AwlSimError) as e:
  370. now = time.time()
  371. if lastExceptionTime is not None and\
  372. now - lastExceptionTime < 1.0:
  373. # The last fault is less than one second
  374. # in the past. Raise a fatal exception.
  375. printError("Fatal fault detected")
  376. raise e
  377. else:
  378. lastExceptionTime = now
  379. # Non-fatal fault.
  380. # Ensure the CPU is stopped and enter
  381. # the run loop again.
  382. printError(e.getReport())
  383. printError("CPU stopped due to fault")
  384. server.setRunState(server.STATE_STOP)
  385. continue
  386. # Run loop exited normally. Bail out.
  387. break
  388. except LinuxCNC_NotRunning as e:
  389. result = ExitCodes.EXIT_ERR_OTHER
  390. except KeyboardInterrupt as e:
  391. result = ExitCodes.EXIT_ERR_OTHER
  392. except (AwlParserError, AwlSimError) as e:
  393. printError(e.getReport())
  394. result = ExitCodes.EXIT_ERR_SIM
  395. except MaintenanceRequest as e:
  396. if e.requestType in (MaintenanceRequest.TYPE_SHUTDOWN,
  397. MaintenanceRequest.TYPE_STOP,
  398. MaintenanceRequest.TYPE_RTTIMEOUT):
  399. result = ExitCodes.EXIT_OK
  400. else:
  401. printError("Received invalid maintenance request %d" %\
  402. e.requestType)
  403. result = ExitCodes.EXIT_ERR_SIM
  404. finally:
  405. if server:
  406. server.shutdown()
  407. printInfo("LinuxCNC HAL module shutdown.")
  408. return result
  409. if __name__ == "__main__":
  410. sys.exit(main())