diff --git a/MTEST04-MTN-ARM_parameters.yaml b/MTEST04-MTN-ARM_parameters.yaml index a6e25fb..7898c2c 100644 --- a/MTEST04-MTN-ARM_parameters.yaml +++ b/MTEST04-MTN-ARM_parameters.yaml @@ -1,6 +1,6 @@ ioc_name: MTEST04-MTN-ARM ioc_host: c6025a-08 -ioc_port: 50001 +ioc_port: 50002 ioc_user: root epics_version: 7.0.10 os: deb12 diff --git a/Makefile b/Makefile index 110a049..24f1ead 100644 --- a/Makefile +++ b/Makefile @@ -1,36 +1,66 @@ -CXX ?= c++ -CXXFLAGS ?= -O2 -std=c++17 -Wall -Wextra -pedantic -PYTHON ?= python3 -ECMC_ROOT ?= ../ecmc -ifeq ($(shell uname -s),Darwin) -EXT := dylib -SHARED := -dynamiclib -else -EXT := so -SHARED := -shared +include /ioc/tools/sls_ioc.makefile + +# The offline build also creates a build/ directory. Always run the IOC +# build recipe so the source Makefile can check its own dependencies. +.PHONY: default build clean + +IOC_STARTUP_FILE := $(notdir $(firstword $(wildcard *_startup.script))) +IOC_PARAMS_FILE := $(notdir $(firstword $(wildcard *_parameters.yaml))) +IOC_NAME ?= $(patsubst %_startup.script,%,$(IOC_STARTUP_FILE)) +ifeq ($(IOC_NAME),) +IOC_NAME := $(patsubst %_parameters.yaml,%,$(IOC_PARAMS_FILE)) endif -.DEFAULT_GOAL := all -.PHONY: all viewer-lib test test-logic logic clean -all: viewer-lib build/test_kinematics -viewer-lib: build/libarm.$(EXT) -build/libarm.$(EXT): src/c_api.cpp src/kinematics.hpp + +CPP_LOGIC_ROOT ?= $(abspath ../..) +PYTHON ?= python3 +GENERATE_QT ?= 1 +IOC_QT_PANEL := qt/$(IOC_NAME)_cpp_logic.ui +IOC_QTGEN := $(CPP_LOGIC_ROOT)/utils/cpp_logic_ioc_qtgen.py +IOC_SUBS_FILE := cfg/libmain.so_cpp_logic.subs + +# Python's ctypes library is built locally, independently of the IOC module. +CXX ?= c++ +VIEWER_CXXFLAGS ?= -O2 -std=c++17 -Wall -Wextra -pedantic +ifeq ($(shell uname -s),Darwin) +VIEWER_LIBRARY := build/libarm.dylib +VIEWER_SHARED := -dynamiclib +else +VIEWER_LIBRARY := build/libarm.so +VIEWER_SHARED := -shared +endif + +.PHONY: viewer-lib +viewer-lib: $(VIEWER_LIBRARY) + +$(VIEWER_LIBRARY): src/c_api.cpp src/kinematics.hpp mkdir -p $(@D) - $(CXX) $(CXXFLAGS) -fPIC $(SHARED) -o $@ src/c_api.cpp + $(CXX) $(VIEWER_CXXFLAGS) -fPIC $(VIEWER_SHARED) -o $@ src/c_api.cpp + +.PHONY: pvs ui + +pvs: + $(MAKE) -C src stage + +ui: pvs + if [ "$(GENERATE_QT)" = "1" ]; then $(PYTHON) $(IOC_QTGEN) --input $(IOC_SUBS_FILE) --output $(IOC_QT_PANEL) --pv-prefix "$(IOC_NAME):" --title "$(IOC_NAME) cpp_logic PVs"; fi + +# Local numerical checks are independent of the PSI IOC build. +ECMC_ROOT ?= ../ecmc +.PHONY: test test-logic logic +logic: build + build/test_kinematics: tests/test_kinematics.cpp src/kinematics.hpp mkdir -p $(@D) - $(CXX) $(CXXFLAGS) -Isrc -o $@ $< -test: all + $(CXX) $(VIEWER_CXXFLAGS) -Isrc -o $@ $< + +test: viewer-lib build/test_kinematics ./build/test_kinematics $(PYTHON) python/viewer.py --self-test $(PYTHON) tests/test_epics_source.py -logic: bin/libmain.$(EXT) -bin/libmain.$(EXT): src/logic.cpp src/kinematics.hpp - mkdir -p $(@D) - $(CXX) $(CXXFLAGS) -fPIC $(SHARED) -I$(ECMC_ROOT)/devEcmcSup/logic -o $@ $< + build/test_logic: tests/test_logic.cpp src/logic.cpp src/kinematics.hpp mkdir -p $(@D) - $(CXX) $(CXXFLAGS) -I$(ECMC_ROOT)/devEcmcSup/logic -o $@ $< + $(CXX) $(VIEWER_CXXFLAGS) -I$(ECMC_ROOT)/devEcmcSup/logic -o $@ $< + test-logic: build/test_logic ./build/test_logic -clean: - rm -rf build bin diff --git a/README.md b/README.md index b1de966..0166449 100644 --- a/README.md +++ b/README.md @@ -37,7 +37,8 @@ load, drive dynamics, collision checks or physical safety system. Both Python and the IOC share `src/kinematics.hpp`. The viewer calls a small C ABI in `src/c_api.cpp`; `make viewer-lib` rebuilds `build/libarm.so` (`.dylib` -on macOS). No EPICS or Python GUI dependencies are needed by `make test`. +on macOS). No EPICS libraries or Python GUI dependencies are needed by `make test`; +the Makefiles use the PSI `/ioc/tools` build infrastructure. ## Arm geometry and conventions @@ -107,16 +108,24 @@ Build on the Linux IOC development host with the source headers matching the installed ecmc version: ```sh -make logic ECMC_ROOT=/path/to/ecmc +make +make pvs CPP_LOGIC_ROOT=/path/to/ecmccfg/examples/PSI/plugins/cpp_logic make test-logic ECMC_ROOT=/path/to/ecmc ``` -`make logic` produces `bin/libmain.so`; deploy it with `cfg/` and the startup -script. Custom PV substitutions are checked in as -`cfg/libmain.so_cpp_logic.subs`, matching `loadCppLogic.cmd` defaults. -`src/Makefile` also provides the PSI `driver.makefile` build option for Debian -IOC architectures; it builds only `logic.cpp`. The top-level Makefile is a -portable local build and does not install or launch an IOC. +The Makefiles follow the hexapod project's PSI pattern: the top-level +Makefile includes `/ioc/tools/sls_ioc.makefile` and delegates `make build` to +`src/Makefile`, which includes `/ioc/tools/driver.makefile`. The module is `main`, +uses C++17, targets `deb10%` / `deb12%`, and requires `ecmc v11.2.0_RC1`. +Use the normal PSI build/install workflow to stage `bin/libmain.so`, together +with `cfg/` and the startup script. `make logic` is an alias for the IOC build. +`build` is phony so the local `build/` directory cannot suppress IOC compilation. + +Custom PV substitutions are checked in as `cfg/libmain.so_cpp_logic.subs`. +`make pvs` regenerates them from `src/logic.cpp`; set `CPP_LOGIC_ROOT` to the +source tree's `examples/PSI/plugins/cpp_logic` directory. `make ui` uses the same +path to generate `qt/MTEST04-MTN-ARM_cpp_logic.ui`. The viewer's local C API +library remains a separate `make viewer-lib` target. `MTEST04-MTN-ARM_startup.script` follows the ecmccfg `cpp_logic` examples and requires `ecmccfg v11.2.0_RC1` / `ecmc v11.2.0_RC1`, no EtherCAT master, @@ -149,8 +158,9 @@ IK seed/branch; automatic bumpless mode transfer is not implemented. Fault codes are latched: 0 OK; 1 invalid input or joint bounds; 2 IK failure; 3 host output write failure; 4 axis error. Fix the cause and restart/reload. -A fault retains outputs; it is not a coherent group stop, and a failed write can -leave partial updates. C++ faults are not wired into a safety-rated stop system. +A fault inhibits new IK trajectory commands, but valid simulated joint encoder +and FK feedback continue following the selected joint trajectories. This is not +a coherent group stop, and a failed write can leave partial updates. C++ faults are not wired into a safety-rated stop system. The built-in master/slave state machine does not replace that integration. Independent Cartesian motor moves are not a coordinated six-axis Cartesian path. @@ -162,6 +172,8 @@ Measure the complete IOC cycle and overruns before assuming 1 kHz operation. ## Read-only EPICS viewer and caQtDM +![Running ecmc arm IOC with caQtDM axis panels and the read-only 3D viewer](ecmc_arm_ioc_running.png) + ```sh .venv/bin/pip install -r python/requirements-ioc.txt make viewer-lib @@ -178,6 +190,8 @@ Live Python mode disables motion controls and subscribes to: | `arm-pose-set` / `arm-pose-actual` | Six home-relative tool coordinates, mm/degrees | | `arm-joint-set` / `arm-joint-actual` | Six home-relative joint offsets, degrees | | `arm-fault` | Latched adapter fault | +| `arm-fault-axis` / `arm-fault-axis-error` | First faulting axis ID and its error code, captured for fault 4 | +| `arm-joint-trajectory` | Selected trajectory of each joint axis, home-relative degrees | | `arm-ik-residual` | Weighted IK residual norm | | `arm-ik-iterations` | IK correction attempts | | `arm-simulation` | Always 1 | @@ -197,3 +211,20 @@ and mocked CA cache connection/value checks. `make test-logic` checks bindings, IK/plant/FK timing, direct joint feedback, joint limits and latched faults. A headless viewer snapshot checks rendering separately. A live robot-arm IOC and physical robot have not been tested; the full workspace is not validated. + +### Tracing simulated feedback + +During a small move, compare `arm-joint-set` (IK command), +`arm-joint-trajectory` (each joint's selected trajectory), and `arm-joint-actual` +(encoder feedback). In this ideal simulation the latter two should agree after +cyclic pipeline latency. A changing IK command with stationary selected +trajectories points to the axis/source/enable/state-machine path. A changing +selected trajectory with stationary encoder feedback points to the simulation +plant, encoder-source selection, or a stopped C++ logic instance. The joint +`Jn-PosSet` and `Jn-PosAct` records expose those same axis values. + +`arm-fault-axis` and `arm-fault-axis-error` capture the first axis error seen by +the adapter, even if that axis error later clears. Feedback continues through +latched IK/axis faults when joint samples are finite and within model limits; +new IK commands remain inhibited until reload/restart. This allows actual +feedback to follow an axis stop trajectory instead of freezing on the fault. diff --git a/cfg/libmain.so_cpp_logic.subs b/cfg/libmain.so_cpp_logic.subs index 67839e0..49242ee 100644 --- a/cfg/libmain.so_cpp_logic.subs +++ b/cfg/libmain.so_cpp_logic.subs @@ -6,11 +6,14 @@ file ecmcCppLogicWaveformIn.template { { "arm-pose-set", "arm.pose_set", "arm.pose_set", "6", "DOUBLE", "asynFloat64ArrayIn" } { "arm-pose-actual", "arm.pose_actual", "arm.pose_actual", "6", "DOUBLE", "asynFloat64ArrayIn" } { "arm-joint-set", "arm.joint_set", "arm.joint_set", "6", "DOUBLE", "asynFloat64ArrayIn" } + { "arm-joint-trajectory", "arm.joint_trajectory", "Selected joint trajectory", "6", "DOUBLE", "asynFloat64ArrayIn" } { "arm-joint-actual", "arm.joint_actual", "arm.joint_actual", "6", "DOUBLE", "asynFloat64ArrayIn" } } file ecmcCppLogicLongIn.template { pattern { REC, PARAM, DESC } + { "arm-fault-axis", "arm.fault_axis", "Axis causing latched fault" } + { "arm-fault-axis-error", "arm.fault_axis_error", "Axis error at fault latch" } { "arm-fault", "arm.fault", "arm.fault" } { "arm-ik-iterations", "arm.ik_iterations", "arm.ik_iterations" } { "arm-simulation", "arm.simulation", "arm.simulation" } diff --git a/ecmc_arm_ioc_running.png b/ecmc_arm_ioc_running.png new file mode 100644 index 0000000..0fc1dc1 Binary files /dev/null and b/ecmc_arm_ioc_running.png differ diff --git a/src/Makefile b/src/Makefile index 77a3c5a..3238c8a 100644 --- a/src/Makefile +++ b/src/Makefile @@ -1,11 +1,24 @@ -# Optional PSI driver build: make -C src build (on the IOC development host). include /ioc/tools/driver.makefile MODULE = main BUILDCLASSES = Linux ARCH_FILTER = deb10% deb12% -EXCLUDE_VERSIONS += 3 7.0.5 7.0.6 7.0.7 +EXCLUDE_VERSIONS+=3 7.0.5 7.0.6 7.0.7 USR_CXXFLAGS += -std=c++17 ecmc_VERSION = v11.2.0_RC1 -SOURCES = logic.cpp -HEADERS = kinematics.hpp -# The portable C API for the viewer is built separately by make viewer-lib. +DIRS+=. +SOURCES += $(foreach d,${DIRS}, $(wildcard $d/*.c) $(wildcard $d/*.cpp)) +HEADERS += $(foreach d,${DIRS}, $(wildcard $d/*.h)) +HEADERS += $(foreach d,${DIRS}, $(wildcard $d/*.hpp)) + +CPP_LOGIC_ROOT ?= $(abspath ../../..) +PYTHON ?= python3.9 +SUBSGEN := $(CPP_LOGIC_ROOT)/utils/ecmcCppLogicSourceSubstGen.py +STAGE_SUBS ?= ../cfg/lib$(MODULE).so_cpp_logic.subs +STAGE_DIR := $(dir $(STAGE_SUBS)) +LOCAL_MODULE := $(notdir $(abspath ..)) + +.PHONY: stage + +stage: + mkdir -p $(STAGE_DIR); \ + $(PYTHON) $(SUBSGEN) --source logic.cpp --logic-name "$(LOCAL_MODULE)" --output $(STAGE_SUBS) diff --git a/src/logic.cpp b/src/logic.cpp index 3ae366d..b42a3de 100644 --- a/src/logic.cpp +++ b/src/logic.cpp @@ -7,6 +7,7 @@ struct ArmLogic : ecmcCpp::LogicBase { arm::Six pose_set{},pose_actual{},joint_set{},joint_actual{},joint_trajectory{}; arm::Six seed=model.home; int32_t fault=0,ik_iterations=0,simulation=1; + int32_t fault_axis=0,fault_axis_error=0; double ik_residual=0; int coordFirst=1,jointFirst=7; ArmLogic() { @@ -31,29 +32,47 @@ struct ArmLogic : ecmcCpp::LogicBase { .readOnlyArray("arm.pose_actual",pose_actual) .readOnlyArray("arm.joint_set",joint_set) .readOnlyArray("arm.joint_actual",joint_actual) + .readOnlyArray("arm.joint_trajectory",joint_trajectory) .readOnly("arm.fault",fault) + .readOnly("arm.fault_axis",fault_axis) + .readOnly("arm.fault_axis_error",fault_axis_error) .readOnly("arm.ik_iterations",ik_iterations) .readOnly("arm.ik_residual",ik_residual) .readOnly("arm.simulation",simulation); } + void latch(int32_t code) { if(!fault) fault=code; } void run() override { - if(fault) return; - for(int i=0;i<6;++i) if(ecmcCpp::axisHasError(coordFirst+i)||ecmcCpp::axisHasError(jointFirst+i)) { fault=4; return; } - if(!arm::finite(pose_set)||!arm::finite(joint_actual)||!arm::finite(joint_trajectory)) { fault=1; return; } + // Simulation plant and FK feedback must keep running independently of IK + // and axis faults. Do not manufacture feedback from the Cartesian demand. + if(!arm::finite(joint_actual)||!arm::finite(joint_trajectory)) { latch(1); return; } arm::Six actual{},trajectory{}; for(int i=0;i<6;++i) { actual[i]=joint_actual[i]+model.home[i]; trajectory[i]=joint_trajectory[i]+model.home[i]; } - if(!model.withinLimits(actual)||!model.withinLimits(trajectory)) { fault=1; return; } - // Previous accepted command selects a continuous local IK branch. + if(!model.withinLimits(actual)||!model.withinLimits(trajectory)) { latch(1); return; } + pose_actual=model.pose(actual); + for(int i=0;i<6;++i) { + int rc=ecmcCpp::axisSetExternalEncoderPos(jointFirst+i,joint_trajectory[i]); + rc|=ecmcCpp::axisSetExternalEncoderPos(coordFirst+i,pose_actual[i]); + if(rc) { latch(3); return; } + } + + // Faults still latch and inhibit new IK trajectory commands. + if(fault) return; + for(int i=0;i<6;++i) { + for(int axis:{coordFirst+i,jointFirst+i}) { + if(ecmcCpp::axisHasError(axis)) { + fault_axis=axis; fault_axis_error=ecmcCpp::axisGetErrorId(axis); + latch(4); return; + } + } + } + if(!arm::finite(pose_set)) { latch(1); return; } const auto result=model.inverse(pose_set,seed); ik_iterations=result.iterations; ik_residual=result.residual; - if(!result.converged) { fault=2; return; } - seed=result.joints; pose_actual=model.pose(actual); + if(!result.converged) { latch(2); return; } + seed=result.joints; for(int i=0;i<6;++i) joint_set[i]=result.joints[i]-model.home[i]; for(int i=0;i<6;++i) { - int rc=ecmcCpp::axisSetExternalSetpointPos(jointFirst+i,joint_set[i]); - rc|=ecmcCpp::axisSetExternalEncoderPos(jointFirst+i,joint_trajectory[i]); - rc|=ecmcCpp::axisSetExternalEncoderPos(coordFirst+i,pose_actual[i]); - if(rc) { fault=3; return; } + if(ecmcCpp::axisSetExternalSetpointPos(jointFirst+i,joint_set[i])) { latch(3); return; } } } }; diff --git a/tests/test_logic.cpp b/tests/test_logic.cpp index b5ce3dc..e087726 100644 --- a/tests/test_logic.cpp +++ b/tests/test_logic.cpp @@ -4,12 +4,13 @@ std::array setpoint{},encoder{}; int badAxis=0,writeFailure=0; int32_t error(int32_t axis) { return axis==badAxis; } +int32_t errorId(int32_t axis) { return axis==badAxis?85017:0; } int32_t putSet(int32_t axis,double value) { setpoint.at(axis)=value; return writeFailure; } int32_t putEnc(int32_t axis,double value) { encoder.at(axis)=value; return writeFailure; } void check(bool ok,const char* msg) { if(!ok) throw std::runtime_error(msg); } int main() { ecmcCppLogicHostServices host{}; - host.get_axis_error=error; host.set_axis_ext_set_pos=putSet; host.set_axis_ext_act_pos=putEnc; + host.get_axis_error=error; host.get_axis_error_id=errorId; host.set_axis_ext_set_pos=putSet; host.set_axis_ext_act_pos=putEnc; ecmcCpp::setHostServices(&host); ArmLogic logic; logic.run(); check(!logic.fault,"zero axes represent home"); // Verify real binding descriptors rather than relying on the mock to resolve names. @@ -27,8 +28,19 @@ int main() { direct.run(); check(!direct.fault&&std::abs(direct.pose_actual[0])>.01,"direct joint FK"); const auto held=setpoint; logic.pose_set[0]=10000; logic.run(); check(logic.fault==2&&setpoint==held,"IK failure holds outputs"); logic.pose_set={}; logic.run(); check(logic.fault==2,"latched fault"); + // An IK fault must stop new commands without freezing the simulation plant. + logic.joint_trajectory[0]+=0.5; + logic.run(); + check(logic.fault==2&&setpoint==held,"fault inhibits new IK commands"); + check(encoder[7]==logic.joint_trajectory[0],"plant runs after IK fault"); ArmLogic limits; limits.joint_trajectory[0]=200; limits.run(); check(limits.fault==1,"joint limit"); - ArmLogic axis; badAxis=7; axis.run(); check(axis.fault==4,"axis error"); badAxis=0; + ArmLogic axis; badAxis=1; axis.joint_trajectory[0]=2; axis.run(); + check(axis.fault==4&&axis.fault_axis==1&&axis.fault_axis_error==85017,"axis stall diagnostics"); + check(encoder[7]==2,"plant runs on axis fault cycle"); + const auto stopped=setpoint; + axis.joint_trajectory[0]=3; axis.joint_actual[0]=2; axis.run(); + check(encoder[7]==3&&std::abs(axis.pose_actual[1])>.01,"plant and FK run while fault latched"); + check(setpoint==stopped,"axis fault inhibits new IK commands"); badAxis=0; ArmLogic write; writeFailure=1; write.run(); check(write.fault==3,"host failure"); std::cout<<"PASS: input bindings, IK/FK pipeline, direct joint plant, latched faults\n"; }