awlsim-linuxcnc-hal 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381
  1. #!/usr/bin/env python
  2. # -*- coding: utf-8 -*-
  3. #
  4. # AWL simulator - LinuxCNC HAL module
  5. #
  6. # Copyright 2013-2020 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 getopt
  26. from awlsim_loader.common import *
  27. from awlsim_loader.core import *
  28. from awlsim_loader.coreserver import *
  29. from awlsim_loader.coreclient import *
  30. from awlsim.common.monotonic import monotonic_time
  31. class LinuxCNC_NotRunning(Exception):
  32. pass
  33. def linuxCNCTriggerEStop():
  34. # Try to trigger LinuxCNC emergency stop.
  35. global linuxcnc_mod
  36. if linuxcnc_mod is not None:
  37. try:
  38. cmd = linuxcnc_mod.command()
  39. cmd.state(linuxcnc_mod.STATE_ESTOP)
  40. cmd.wait_complete()
  41. except Exception as e:
  42. print("ERROR: Failed to trigger LinuxCNC E-Stop: %s" % str(e),
  43. file=sys.stderr)
  44. else:
  45. print("Triggered LinuxCNC E-Stop")
  46. # Check presence of LinuxCNC.
  47. # Returns normally, if LinuxCNC is detected.
  48. # Raises LinuxCNC_NotRunning, if LinuxCNC is not detected.
  49. def watchdogHook(_unused = None):
  50. # Check whether LinuxCNC is running.
  51. for lockname in ("/tmp/linuxcnc.lock", "/tmp/emc.lock"):
  52. if os.path.exists(lockname):
  53. return True
  54. if not opt_watchdog:
  55. # The check is disabled. Return success.
  56. return True
  57. printError("LinuxCNC doesn't seem to be running. "\
  58. "(Use '--watchdog off' to disable this check.)")
  59. raise LinuxCNC_NotRunning()
  60. def usage():
  61. print("awlsim-linuxcnc-hal version %s" % VERSION_STRING)
  62. print("")
  63. print("Usage: awlsim-linuxcnc-hal [OPTIONS] PROJECT.awlpro")
  64. print("")
  65. print("Options:")
  66. print(" -w|--rw-project Enable project file writing after download of new program.")
  67. print(" Default: Do not write to the project file.")
  68. print("")
  69. print(" -i|--input-size SIZE The input area size, in bytes.")
  70. print(" Overrides input-size from project file.")
  71. print(" -I|--input-base BASE The AWL/STL input address base.")
  72. print(" Overrides input-base from project file.")
  73. print(" -o|--output-size SIZE The output area size, in bytes.")
  74. print(" Overrides output-size from project file.")
  75. print(" -O|--output-base BASE The AWL/STL output address base.")
  76. print(" Overrides output-base from project file.")
  77. print("")
  78. print(" -l|--listen HOST:PORT Set the address and port where the")
  79. print(" awlsim core server should listen on.")
  80. print(" Defaults to %s:%d" %\
  81. (AwlSimServer.DEFAULT_HOST, AwlSimServer.DEFAULT_PORT))
  82. print("")
  83. print(" -L|--loglevel LVL Set the log level:")
  84. print(" 0: Log nothing")
  85. print(" 1: Log errors")
  86. print(" 2: Log errors and warnings")
  87. print(" 3: Log errors, warnings and info messages (default)")
  88. print(" 4: Verbose logging")
  89. print(" 5: Extremely verbose logging")
  90. print(" -N|--nice NICE Renice the process. -20 <= NICE <= 19.")
  91. print(" Default: Do not renice")
  92. print("")
  93. print("Debugging options:")
  94. print(" -W|--watchdog 1/0 Enable/disable LinuxCNC runtime watchdog.")
  95. print(" Default: on")
  96. print(" -M|--max-runtime SEC Module will be stopped after SEC seconds.")
  97. print(" Default: No timeout")
  98. print(" -x|--extended-insns Force-enable extended instructions")
  99. print("")
  100. print("For an example LinuxCNC HAL configuration see:")
  101. print(" examples/linuxcnc-demo/linuxcnc-demo.hal")
  102. def main():
  103. global linuxcnc_mod
  104. linuxcnc_mod = None
  105. global opt_inputSize
  106. global opt_inputBase
  107. global opt_outputSize
  108. global opt_outputBase
  109. global opt_listen
  110. global opt_loglevel
  111. global opt_nice
  112. global opt_watchdog
  113. global opt_maxRuntime
  114. global opt_extInsns
  115. global opt_rwProject
  116. opt_inputSize = None
  117. opt_inputBase = None
  118. opt_outputSize = None
  119. opt_outputBase = None
  120. opt_listen = (AwlSimServer.DEFAULT_HOST, AwlSimServer.DEFAULT_PORT)
  121. opt_loglevel = Logging.LOG_INFO
  122. opt_nice = None
  123. opt_watchdog = True
  124. opt_maxRuntime = None
  125. opt_extInsns = None
  126. opt_rwProject = False
  127. try:
  128. (opts, args) = getopt.getopt(sys.argv[1:],
  129. "hi:I:o:O:l:L:N:W:M:xw",
  130. [ "help", "input-size=", "input-base=",
  131. "output-size=", "output-base=",
  132. "listen=",
  133. "loglevel=", "nice=",
  134. "watchdog=", "max-runtime=", "extended-insns",
  135. "rw-project", ])
  136. except getopt.GetoptError as e:
  137. printError(str(e))
  138. usage()
  139. return 1
  140. for (o, v) in opts:
  141. if o in ("-h", "--help"):
  142. usage()
  143. return 0
  144. if o in ("-i", "--input-size"):
  145. try:
  146. opt_inputSize = int(v)
  147. except ValueError:
  148. printError("-i|--input-size: Invalid argument")
  149. return 1
  150. if o in ("-I", "--input-base"):
  151. try:
  152. opt_inputBase = int(v)
  153. except ValueError:
  154. printError("-I|--input-base: Invalid argument")
  155. return 1
  156. if o in ("-o", "--output-size"):
  157. try:
  158. opt_outputSize = int(v)
  159. except ValueError:
  160. printError("-o|--output-size: Invalid argument")
  161. return 1
  162. if o in ("-O", "--output-base"):
  163. try:
  164. opt_outputBase = int(v)
  165. except ValueError:
  166. printError("-O|--output-base: Invalid argument")
  167. return 1
  168. if o in ("-l", "--listen"):
  169. try:
  170. host, port = parseNetAddress(v)
  171. if not host.strip() or\
  172. host in {"any", "all"}:
  173. host = ""
  174. if port is None:
  175. port = AwlSimServer.DEFAULT_PORT
  176. opt_listen = (host, port)
  177. except AwlSimError as e:
  178. printError("-l|--listen: Invalid host/port")
  179. return 1
  180. if o in ("-L", "--loglevel"):
  181. try:
  182. opt_loglevel = int(v)
  183. except ValueError:
  184. printError("-L|--loglevel: Invalid log level")
  185. return 1
  186. if o in ("-N", "--nice"):
  187. try:
  188. opt_nice = int(v)
  189. if opt_nice < -20 or opt_nice > 19:
  190. raise ValueError
  191. except ValueError:
  192. printError("-N|--nice: Invalid niceness level")
  193. return 1
  194. if o in ("-W", "--watchdog"):
  195. opt_watchdog = str2bool(v)
  196. if o in ("-M", "--max-runtime"):
  197. try:
  198. opt_maxRuntime = float(v)
  199. except ValueError:
  200. printError("-M|--max-runtime: Invalid time format")
  201. return 1
  202. if o in ("-x", "--extended-insns"):
  203. opt_extInsns = True
  204. if o in ("-w", "--rw-project"):
  205. opt_rwProject = True
  206. if len(args) != 1:
  207. usage()
  208. return 1
  209. projectFile = args[0]
  210. result = ExitCodes.EXIT_OK
  211. server = None
  212. try:
  213. Logging.setPrefix("awlsim-linuxcnc: ")
  214. Logging.setLoglevel(opt_loglevel)
  215. # Adjust process priority
  216. if opt_nice is not None:
  217. try:
  218. os.nice(opt_nice)
  219. except OSError as e:
  220. printError("Failed to renice process to "
  221. "%d: %s" % (opt_nice, str(e)))
  222. return 1
  223. # Try to import the LinuxCNC HAL module
  224. try:
  225. import hal as LinuxCNC_HAL
  226. except ImportError as e:
  227. printError("Failed to import LinuxCNC HAL "
  228. "module: %s" % str(e))
  229. return 1
  230. try:
  231. import linuxcnc as linuxcnc_mod
  232. except ImportError as e:
  233. printError("Failed to import LinuxCNC "
  234. "module: %s" % str(e))
  235. return 1
  236. # Create the LinuxCNC HAL component.
  237. halComponent = LinuxCNC_HAL.component("awlsim")
  238. # Read the project.
  239. project = Project.fromProjectOrRawAwlFile(projectFile)
  240. hwmodSettings = project.getHwmodSettings()
  241. # Get the 'linuxcnc' hardware module descriptor.
  242. linuxcncHwmodDesc = None
  243. for modDesc in hwmodSettings.getLoadedModules():
  244. if modDesc.getModuleName() == "linuxcnc":
  245. if linuxcncHwmodDesc:
  246. printError("ERROR: More than one 'linuxcnc' hardware "
  247. "module found in the project file. Only "
  248. "one 'linuxcnc' module is supported.")
  249. linuxcncHwmodDesc = modDesc
  250. if not linuxcncHwmodDesc:
  251. if opt_inputBase is None and\
  252. opt_outputBase is None and\
  253. opt_inputSize is None and\
  254. opt_outputSize is None:
  255. printWarning("Warning: Hardware module 'linuxcnc' not "
  256. "included in project file. "
  257. "Loading module nevertheless.")
  258. modDesc = HwmodDescriptor(moduleName = "linuxcnc",
  259. parameters = {
  260. "inputAddressBase" : "0",
  261. "outputAddressBase" : "0",
  262. "inputSize" : "32",
  263. "outputSize" : "32",
  264. })
  265. hwmodSettings.addLoadedModule(modDesc)
  266. linuxcncHwmodDesc = modDesc
  267. # Override hardware module parameters, if required.
  268. if opt_inputBase is not None:
  269. linuxcncHwmodDesc.setParameterValue("inputAddressBase",
  270. str(opt_inputBase))
  271. if opt_inputSize is not None:
  272. linuxcncHwmodDesc.setParameterValue("inputSize",
  273. str(opt_inputSize))
  274. if opt_outputBase is not None:
  275. linuxcncHwmodDesc.setParameterValue("outputAddressBase",
  276. str(opt_outputBase))
  277. if opt_outputSize is not None:
  278. linuxcncHwmodDesc.setParameterValue("outputSize",
  279. str(opt_outputSize))
  280. # Override CPUConf, if required.
  281. conf = project.getCpuConf()
  282. if opt_maxRuntime is not None:
  283. conf.setRunTimeLimitUs(int(round(opt_maxRuntime * 1000000.0)))
  284. if opt_extInsns is not None:
  285. conf.setExtInsnsEn(opt_extInsns)
  286. # Pass the hal component singleton to the Awlsim hw module.
  287. loader = HwModLoader.loadModule("linuxcnc")
  288. mod = loader.getModule()
  289. mod.setLinuxCNCHalComponentSingleton(LinuxCNC_HAL, halComponent)
  290. server = AwlSimServer()
  291. server.setCycleExitHook(watchdogHook)
  292. printInfo("Starting core server...")
  293. server.startup(host=opt_listen[0],
  294. port=opt_listen[1],
  295. project=project,
  296. projectWriteBack=opt_rwProject,
  297. raiseExceptionsFromRun=True,
  298. handleMaintenanceServerside=True)
  299. try:
  300. server.setRunState(server.STATE_RUN)
  301. except AwlSimError as e:
  302. printError("Failed to go to RUN state.")
  303. # Continue to main loop.
  304. lastExceptionTime = monotonic_time() - 1.0
  305. exceptionCount = 0
  306. while True:
  307. try:
  308. server.run()
  309. except (AwlParserError, AwlSimError) as e:
  310. linuxCNCTriggerEStop()
  311. now = monotonic_time()
  312. if now - lastExceptionTime < 1.0:
  313. # The last fault is less than one second ago.
  314. exceptionCount += 1
  315. else:
  316. exceptionCount = 0
  317. lastExceptionTime = now
  318. if exceptionCount >= 10:
  319. # Many exceptions happened in a row.
  320. # Raise a fatal exception.
  321. printError("Fatal fault detected")
  322. raise e
  323. else:
  324. # Non-fatal fault.
  325. # Ensure the CPU is stopped and enter
  326. # the run loop again.
  327. printError(e.getReport())
  328. printError("CPU stopped due to fault")
  329. server.setRunState(server.STATE_STOP)
  330. continue
  331. # Run loop exited normally. Bail out.
  332. break
  333. except LinuxCNC_NotRunning as e:
  334. result = ExitCodes.EXIT_ERR_OTHER
  335. except KeyboardInterrupt as e:
  336. result = ExitCodes.EXIT_ERR_OTHER
  337. except (AwlParserError, AwlSimError) as e:
  338. printError(e.getReport())
  339. result = ExitCodes.EXIT_ERR_SIM
  340. except MaintenanceRequest as e:
  341. if e.requestType in (MaintenanceRequest.TYPE_SHUTDOWN,
  342. MaintenanceRequest.TYPE_STOP,
  343. MaintenanceRequest.TYPE_RTTIMEOUT):
  344. result = ExitCodes.EXIT_OK
  345. else:
  346. printError("Received invalid maintenance request %d" %\
  347. e.requestType)
  348. result = ExitCodes.EXIT_ERR_SIM
  349. finally:
  350. linuxCNCTriggerEStop()
  351. if server:
  352. server.shutdown()
  353. printInfo("LinuxCNC HAL module shutdown.")
  354. return result
  355. if __name__ == "__main__":
  356. sys.exit(main())