From 143820f5142dfa52920c5dac26fb972fb8619e8a Mon Sep 17 00:00:00 2001
From: Michael Haberler <git@mah.priv.at>
Date: Thu, 5 Jan 2012 08:37:18 +0100
Subject: [PATCH] sim: try sim_spindle component

speed rampup works
G33.1, G84 cycles still broken - speed reverses at end of cycle
---
 configs/sim/axis/axis_simspindle.ini |  212 ++++++++++++++++++++++++++++++++++
 configs/sim/axis/simspindle.halshow  |    1 +
 configs/sim/sim_spindle_encoder.hal  |   35 ++++++
 src/hal/components/sim_spindle.comp  |   27 +++++
 4 files changed, 275 insertions(+), 0 deletions(-)
 create mode 100644 configs/sim/axis/axis_simspindle.ini
 create mode 100644 configs/sim/axis/simspindle.halshow
 create mode 100644 configs/sim/sim_spindle_encoder.hal
 create mode 100644 src/hal/components/sim_spindle.comp

diff --git a/configs/sim/axis/axis_simspindle.ini b/configs/sim/axis/axis_simspindle.ini
new file mode 100644
index 0000000..73f1a26
--- /dev/null
+++ b/configs/sim/axis/axis_simspindle.ini
@@ -0,0 +1,212 @@
+# EMC controller parameters for a simulated machine.
+
+# General note: Comments can either be preceded with a # or ; - either is
+# acceptable, although # is in keeping with most linux config files.
+
+# General section -------------------------------------------------------------
+[EMC]
+
+# Version of this INI file
+VERSION =               $Revision$
+
+# Name of machine, for use with display, etc.
+MACHINE =               EMC-HAL-SIM-AXIS
+
+# Debug level, 0 means no messages. See src/emc/nml_int/emcglb.h for others
+DEBUG =               0x7FFFFFFF
+#DEBUG = 0
+
+# Sections for display options ------------------------------------------------
+[DISPLAY]
+
+# Name of display program, e.g., xemc
+DISPLAY = axis
+
+# Cycle time, in seconds, that display will sleep between polls
+CYCLE_TIME =            0.100
+
+# Path to help file
+HELP_FILE =             doc/help.txt
+
+# Initial display setting for position, RELATIVE or MACHINE
+POSITION_OFFSET =       RELATIVE
+
+# Initial display setting for position, COMMANDED or ACTUAL
+POSITION_FEEDBACK =     ACTUAL
+
+# Highest value that will be allowed for feed override, 1.0 = 100%
+MAX_FEED_OVERRIDE =     1.2
+MAX_SPINDLE_OVERRIDE =  1.0
+
+MAX_LINEAR_VELOCITY =   1.2
+DEFAULT_LINEAR_VELOCITY =   .25
+# Prefix to be used
+PROGRAM_PREFIX = ../../../nc_files/
+
+# Introductory graphic
+INTRO_GRAPHIC = emc2.gif
+INTRO_TIME = 5
+
+EDITOR = gedit
+TOOL_EDITOR = tooledit
+
+INCREMENTS = 1 in, 0.1 in, 10 mil, 1 mil, 1mm, .1mm, 1/8000 in
+
+[FILTER]
+PROGRAM_EXTENSION = .png,.gif,.jpg Grayscale Depth Image
+PROGRAM_EXTENSION = .py Python Script
+
+png = image-to-gcode
+gif = image-to-gcode
+jpg = image-to-gcode
+py = python
+
+# Task controller section -----------------------------------------------------
+[TASK]
+
+# Name of task controller program, e.g., milltask
+TASK =                  milltask
+
+# Cycle time, in seconds, that task controller will sleep between polls
+CYCLE_TIME =            0.001
+
+# Part program interpreter section --------------------------------------------
+[RS274NGC]
+
+# File containing interpreter variables
+PARAMETER_FILE =        ../sim.var
+
+# Motion control section ------------------------------------------------------
+[EMCMOT]
+
+EMCMOT =              motmod
+
+# Timeout for comm to emcmot, in seconds
+COMM_TIMEOUT =          1.0
+
+# Interval between tries to emcmot, in seconds
+COMM_WAIT =             0.010
+
+# BASE_PERIOD is unused in this configuration but specified in core_sim.hal
+BASE_PERIOD  =               0
+# Servo task period, in nano-seconds
+SERVO_PERIOD =               1000000
+
+# Hardware Abstraction Layer section --------------------------------------------------
+[HAL]
+
+# The run script first uses halcmd to execute any HALFILE
+# files, and then to execute any individual HALCMD commands.
+#
+
+# list of hal config files to run through halcmd
+# files are executed in the order in which they appear
+HALFILE =                    ../core_sim.hal
+HALFILE =                    ../sim_spindle_encoder.hal
+HALFILE =		     ../axis_manualtoolchange.hal
+HALFILE = ../simulated_home.hal
+#HALFILE = ../gamepad.hal
+
+# list of halcmd commands to execute
+# commands are executed in the order in which they appear
+#HALCMD =                    save neta
+
+# Single file that is executed after the GUI has started.  Only supported by
+# AXIS at this time (only AXIS creates a HAL component of its own)
+#POSTGUI_HALFILE = ../test_postgui.hal
+
+HALUI = halui
+
+# Trajectory planner section --------------------------------------------------
+[TRAJ]
+
+AXES =                  3
+COORDINATES =           X Y Z
+HOME =                  0 0 0
+LINEAR_UNITS =          inch
+ANGULAR_UNITS =         degree
+CYCLE_TIME =            0.010
+DEFAULT_VELOCITY =      1.2
+POSITION_FILE = position.txt
+MAX_LINEAR_VELOCITY =   1.2
+
+# Axes sections ---------------------------------------------------------------
+
+# First axis
+[AXIS_0]
+
+TYPE =                          LINEAR
+HOME =                          0.000
+MAX_VELOCITY =                  4
+MAX_ACCELERATION =              100.0
+BACKLASH = 0.000
+INPUT_SCALE =                   4000
+OUTPUT_SCALE = 1.000
+MIN_LIMIT =                     -40.0
+MAX_LIMIT =                     40.0
+FERROR = 0.050
+MIN_FERROR = 0.010
+HOME_OFFSET =                    0.0
+HOME_SEARCH_VEL =                20.0
+HOME_LATCH_VEL =                 20.0
+HOME_USE_INDEX =                 NO
+HOME_IGNORE_LIMITS =             NO
+HOME_SEQUENCE = 1
+HOME_IS_SHARED = 1
+
+# Second axis
+[AXIS_1]
+
+TYPE =                          LINEAR
+HOME =                          0.000
+MAX_VELOCITY =                  4
+MAX_ACCELERATION =              100.0
+BACKLASH = 0.000
+INPUT_SCALE =                   4000
+OUTPUT_SCALE = 1.000
+MIN_LIMIT =                     -40.0
+MAX_LIMIT =                     40.0
+FERROR = 0.050
+MIN_FERROR = 0.010
+HOME_OFFSET =                    0.0
+HOME_SEARCH_VEL =                20.0
+HOME_LATCH_VEL =                 20.0
+HOME_USE_INDEX =                 NO
+HOME_IGNORE_LIMITS =             NO
+HOME_SEQUENCE = 1
+
+# Third axis
+[AXIS_2]
+
+TYPE =                          LINEAR
+HOME =                          0.0
+MAX_VELOCITY =                  4
+MAX_ACCELERATION =              100.0
+BACKLASH = 0.000
+INPUT_SCALE =                   4000
+OUTPUT_SCALE = 1.000
+MIN_LIMIT =                     -8.0
+MAX_LIMIT =                     8.0
+FERROR = 0.050
+MIN_FERROR = 0.010
+HOME_OFFSET =                    1.0
+HOME_SEARCH_VEL =                20.0
+HOME_LATCH_VEL =                 20.0
+HOME_USE_INDEX =                 NO
+HOME_IGNORE_LIMITS =             NO
+HOME_SEQUENCE = 0
+HOME_IS_SHARED = 1
+
+# section for main IO controller parameters -----------------------------------
+[EMCIO]
+
+# Name of IO controller program, e.g., io
+EMCIO = 		io
+
+# cycle time, in seconds
+CYCLE_TIME =    0.100
+
+# tool table file
+TOOL_TABLE =    italy.tbl
+TOOL_CHANGE_POSITION = 0 0 0
+TOOL_CHANGE_QUILL_UP = 1
diff --git a/configs/sim/axis/simspindle.halshow b/configs/sim/axis/simspindle.halshow
new file mode 100644
index 0000000..dfa01cd
--- /dev/null
+++ b/configs/sim/axis/simspindle.halshow
@@ -0,0 +1 @@
+pin+motion.spindle-speed-in pin+motion.spindle-speed-cmd-rps pin+motion.spindle-at-speed
diff --git a/configs/sim/sim_spindle_encoder.hal b/configs/sim/sim_spindle_encoder.hal
new file mode 100644
index 0000000..dd05d5a
--- /dev/null
+++ b/configs/sim/sim_spindle_encoder.hal
@@ -0,0 +1,35 @@
+loadrt sim_spindle
+loadrt limit2 count=1
+loadrt lowpass count=1
+loadrt near
+
+# simulate spindle mass
+setp limit2.0.maxv 500.0 # rpm/second
+
+# encoder reset control
+# hook up motion controller's sync output
+net spindle-index-enable motion.spindle-index-enable <=> sim-spindle.0.index-enable
+
+# report our revolution count to the motion controller
+net spindle-pos sim-spindle.0.position-fb => motion.spindle-revs
+
+# for spindle velocity estimate
+
+# spindle speed control
+net spindle-speed-cmd  motion.spindle-speed-out => limit2.0.in
+net spindle-speed-limited limit2.0.out => sim-spindle.0.velocity-cmd lowpass.0.in
+
+net spindle-rpm-filtered lowpass.0.out motion.spindle-speed-in near.0.in2
+setp lowpass.0.gain .07
+addf lowpass.0 servo-thread
+
+# for at-speed detection
+addf near.0 servo-thread
+setp near.0.scale 1.1
+setp near.0.difference 10
+net spindle-speed-cmd => near.0.in1
+net spindle-at-speed near.0.out motion.spindle-at-speed
+net  spindle-fwd <= motion.spindle-forward
+
+addf limit2.0      servo-thread
+addf sim-spindle.0 servo-thread
diff --git a/src/hal/components/sim_spindle.comp b/src/hal/components/sim_spindle.comp
new file mode 100644
index 0000000..f69130a
--- /dev/null
+++ b/src/hal/components/sim_spindle.comp
@@ -0,0 +1,27 @@
+component sim_spindle "Simulated spindle with index pulse";
+
+pin in float velocity-cmd "Commanded speed, in revolutions per second";
+pin out float position-fb "Feedback position, in revolutions";
+pin io bit index-enable "Reset position-fb to 0 at the next full rotation";
+
+license "GPL";
+
+function _;
+;;
+#include <rtapi_math.h>
+
+
+FUNCTION(_) {
+   double old_position = position_fb;
+   double new_position = position_fb + velocity_cmd * fperiod;
+
+   if(index_enable && (floor(old_position) != floor(new_position))) {
+       index_enable = false;
+       if(velocity_cmd < 0)
+           new_position = new_position - ceil(new_position);
+       else
+           new_position = new_position - floor(new_position);
+   }
+
+   position_fb = new_position;
+}
\ No newline at end of file
-- 
1.7.8.2.302.g17b4

