Advertisement
Guest User

in use accounting for linuxcnc

a guest
Nov 1st, 2012
151
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 7.35 KB | None | 0 0
  1. #custom_postgui.hal
  2.  
  3. net spindle-enable => pyvcp.spindle-enable-led in_use_accounting.spindle_on_in
  4. net reset_session_counters in_use_accounting.session_reset <= pyvcp.session-reset
  5. net iua00 in_use_accounting.total_runtime_out => pyvcp.total_runtime
  6. net ioa01 in_use_accounting.session_runtime_out => pyvcp.session_runtime
  7. net iua10 in_use_accounting.total_machine_on_out => pyvcp.total_machine_on
  8. net iua11 in_use_accounting.session_machine_on_out => pyvcp.session_machine_on
  9. net iua20 in_use_accounting.total_prog_run_out => pyvcp.total_prog_run
  10. net iua21 in_use_accounting.session_prog_run_out => pyvcp.session_prog_run
  11. net iua30 in_use_accounting.total_spindle_on_out => pyvcp.total_spindle_on
  12. net iua31 in_use_accounting.session_spindle_on_out => pyvcp.session_spindle_on
  13. net machine-is-on halui.machine.is-on => and2.0.in0 in_use_accounting.machine_on_in
  14. net programm-is-running halui.program.is-running => or2.0.in0 in_use_accounting.program_running_in
  15.  
  16.  
  17. #----------------------------------------------------------------------------------------------------
  18. #custompanel.xml
  19.  
  20. <?xml version='1.0' encoding='UTF-8'?>
  21. <pyvcp>
  22.          <vbox>
  23.         <labelframe text="accounting">
  24.             <table  flexible_rows="[2,3,4,5]" flexible_columns="[2,3]">
  25.             <tablesticky sticky="news"/>
  26.             <tablerow/>
  27.             <label>
  28.                 <text>""</text>
  29.             </label>
  30.             <label>
  31.                 <text>"total:"</text>
  32.             </label>
  33.             <label>
  34.                                 <text>"session:"</text>
  35.                         </label>
  36.             <tablerow/>
  37.             <label>
  38.                                 <text>"emc on:"</text>
  39.                         </label>
  40.             <number>
  41.                 <halpin>"total_runtime"</halpin>
  42.             </number>
  43.             <number>
  44.                                 <halpin>"session_runtime"</halpin>
  45.                         </number>
  46.             <tablerow/>
  47.                         <label>
  48.                                 <text>"machine_on:"</text>
  49.                         </label>
  50.                         <number>
  51.                                 <halpin>"total_machine_on"</halpin>
  52.                         </number>
  53.                         <number>
  54.                                 <halpin>"session_machine_on"</halpin>
  55.                         </number>
  56.             <tablerow/>
  57.             <label>
  58.                 <text>"program run:"</text>
  59.             </label>
  60.             <number>
  61.                                 <halpin>"total_prog_run"</halpin>
  62.                         </number>
  63.                         <number>
  64.                                 <halpin>"session_prog_run"</halpin>
  65.                         </number>
  66.             <tablerow/>
  67.                         <label>
  68.                                 <text>"spindle run:"</text>
  69.                         </label>
  70.                         <number>
  71.                                 <halpin>"total_spindle_on"</halpin>
  72.                         </number>
  73.                         <number>
  74.                                 <halpin>"session_spindle_on"</halpin>
  75.                         </number>
  76.             </table>
  77.             <button>
  78.                 <halpin>"session-reset"</halpin>
  79.                 <text>"reset session"</text>
  80.             </button>
  81.         </labelframe>
  82.         </vbox>
  83. </pyvcp>
  84.  
  85. #----------------------------------------------------------------------------------------------------
  86. #in_use_accounting.py
  87.  
  88. #!/usr/bin/python
  89. #    HAL userspace component to account for machine use
  90. #
  91. #    Copyright (C) 2009 Joachim Steiger <reprap@hyte.de>
  92. #
  93. #    This program is free software; you can redistribute it and/or modify
  94. #    it under the terms of the GNU General Public License as published by
  95. #    the Free Software Foundation; either version 2 of the License, or
  96. #    (at your option) any later version.
  97. #
  98. #    This program is distributed in the hope that it will be useful,
  99. #    but WITHOUT ANY WARRANTY; without even the implied warranty of
  100. #    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  101. #    GNU General Public License for more details.
  102. #
  103. #    You should have received a copy of the GNU General Public License
  104. #    along with this program; if not, write to the Free Software
  105. #    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
  106.  
  107. from math import *
  108. import sys
  109. import time
  110. import pickle
  111. import hal
  112.  
  113. DATAFILE = '/home/emc2/.in_use_logger/accounting.log'
  114.  
  115. try:
  116.         import psyco
  117.         psyco.full()
  118. except ImportError:
  119.         pass
  120.  
  121. try:
  122.     data_f = file(DATAFILE,'r')
  123.     data = pickle.load(data_f)
  124.     data_f.close()
  125.     foo = time.localtime()
  126.     data_f = file(DATAFILE+"_backup_%i-%i-%i_%i-%i-%i" % ( foo.tm_year, foo.tm_mon, foo.tm_mday, foo.tm_hour, foo.tm_min, foo.tm_sec),'w')
  127.     pickle.dump(data, data_f)
  128.     data_f.flush()
  129.     data_f.close()
  130.     data_f = file(DATAFILE,'w')
  131.     pickle.dump(data, data_f)
  132.     data_f.flush()
  133.  
  134. except IOError:
  135.     data = {}
  136.     data['total_runtime'] = 0.0 # program (axis) running
  137.     data['total_machine_on'] = 0.0 #stepper powered
  138.     data['total_program_running'] = 0.0 #program running
  139.     data['total_spindle_on'] = 0.0 # spindle rotation
  140.     data['session_runtime'] = 0.0 # program (axis) running
  141.     data['session_machine_on'] = 0.0 #stepper powered
  142.     data['session_program_running'] = 0.0 #program running
  143.     data['session_spindle_on'] = 0.0 # spindle rotation
  144.     data_f = file(DATAFILE,'w')
  145.     pickle.dump(data, data_f)
  146.     data_f.flush()
  147.  
  148. c = hal.component("in_use_accounting")
  149. c.newpin("machine_on_in", hal.HAL_BIT, hal.HAL_IN)
  150. c.newpin("program_running_in", hal.HAL_BIT, hal.HAL_IN)
  151. c.newpin("spindle_on_in", hal.HAL_BIT, hal.HAL_IN)
  152. c.newpin("session_reset", hal.HAL_BIT, hal.HAL_IN)
  153. c.newpin("total_runtime_out", hal.HAL_FLOAT, hal.HAL_OUT)
  154. c.newpin("total_machine_on_out", hal.HAL_FLOAT, hal.HAL_OUT)
  155. c.newpin("total_prog_run_out", hal.HAL_FLOAT, hal.HAL_OUT)
  156. c.newpin("total_spindle_on_out", hal.HAL_FLOAT, hal.HAL_OUT)
  157. c.newpin("session_runtime_out", hal.HAL_FLOAT, hal.HAL_OUT)
  158. c.newpin("session_machine_on_out", hal.HAL_FLOAT, hal.HAL_OUT)
  159. c.newpin("session_prog_run_out", hal.HAL_FLOAT, hal.HAL_OUT)
  160. c.newpin("session_spindle_on_out", hal.HAL_FLOAT, hal.HAL_OUT)
  161. c.ready()
  162.  
  163. newtime = time.time()
  164. time.sleep(1)
  165. foo = 0
  166. try:
  167.     while 1:
  168.     oldtime = newtime
  169.     newtime = time.time()
  170.     inctime = newtime - oldtime
  171.     data['total_runtime'] += inctime
  172.     data['session_runtime'] += inctime
  173.     if c['machine_on_in'] != 0:
  174.         data['total_machine_on'] += inctime
  175.         data['session_machine_on'] += inctime
  176.     if c['program_running_in'] != 0:
  177.         data['total_program_running'] += inctime
  178.         data['session_program_running'] += inctime
  179.     if c['spindle_on_in'] != 0:
  180.         data['total_spindle_on'] += inctime
  181.         data['session_spindle_on'] += inctime
  182.     if c['session_reset'] != 0:
  183.         data['session_runtime'] = 0.0 # program (axis) running
  184.         data['session_machine_on'] = 0.0 #stepper powered
  185.         data['session_program_running'] = 0.0 #program running
  186.         data['session_spindle_on'] = 0.0 # spindle rotation
  187.     c['total_runtime_out'] = data['total_runtime']
  188.     c['session_runtime_out'] = data['session_runtime']
  189.     c['total_machine_on_out'] = data['total_machine_on']
  190.     c['session_machine_on_out'] = data['session_machine_on']
  191.     c['total_prog_run_out'] = data['total_program_running']
  192.     c['session_prog_run_out'] = data['session_program_running']
  193.     c['total_spindle_on_out'] = data['total_spindle_on']
  194.     c['session_spindle_on_out'] = data['session_spindle_on']
  195.     foo += 1
  196.     if foo >10:
  197.         data_f.seek(0)
  198.         pickle.dump(data, data_f)
  199.         data_f.flush()
  200.         foo=0
  201.     time.sleep(1)
  202. except (KeyboardInterrupt,):
  203.     data_f.flush()
  204.     data_f.close()
  205.     raise SystemExit, 0
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement