54 Commits

Author SHA1 Message Date
354e9d90fb Fixed missing initializer for variables in Phytron-Axis 2025-06-20 13:46:10 +02:00
deea821e3f Merge pull request 'el734' (#2) from el734 into master
Reviewed-on: #2
2025-06-11 15:07:19 +02:00
7a46788fd5 Install a el734 db file
also remove explicit libversion in the makefile
2025-06-11 15:05:14 +02:00
9e77eb585c Merge branch 'lift_axis_no_autoenable' 2025-04-17 17:00:19 +02:00
d0c009ea38 Removed PMAC drivers
Commented out the PMAC drivers to avoid namespace clashes with the new
turboPmac driver library.
2025-04-17 16:52:52 +02:00
7e1fc78f76 Moved curses from top-level import to function-level import and added a
comment why that is necessary
2024-10-24 10:49:18 +02:00
9e0d8a4322 Added a new script utils/decodeMasterMACStatusR10.py which allows to
decode the R10 status message of the MasterMACs controller.

Also fixed a bug in utils/deltatau.py (error when printing too much text
at once)
2024-10-24 10:34:19 +02:00
3cccfe930c Removed typo from C804Axis.cpp 2024-10-18 09:53:47 +02:00
8860d0c59f Updated the first-time-poll of C804 Axis 2024-10-18 09:48:17 +02:00
b6c38be113 Initial driver version for the C804 controller 2024-10-18 09:48:17 +02:00
b14b50c25a Merge branch 'can-we-have-pipelines' into 'master'
Adds CI-Pipeline with Formatting, Linter Checks and Build Steps

See merge request sinqdev/sinqepicsapp!4
2024-10-14 10:07:16 +02:00
477ffdbc0b Adds CI-Pipeline with Formatting, Linter Checks and Build Steps 2024-10-14 10:07:16 +02:00
0a23ec8f22 clang is too old 2024-10-10 13:22:09 +02:00
eb1bb58c36 Fixed an uninitialized memory bug: In AmorDetectorAxis, the variables
det_starting and det_startTime were not initialized before reading them
in the poll function, leading to erratic behaviour.
2024-10-04 17:04:56 +02:00
80205727c7 File pmacAxis.h:
The default constructor of LiftAxis just forwards to the pmacAxis
constructor, which has an optional argument "autoenable" with the default
value "true". However, we want that argument to be false, hence we provide
an explicit constructor.

File C804Axis.cpp:
Removed a typing error

File Makefile.RHEL8:
Switched compilation target name to 2024-amor-no-autoenable-lift-axis to
not disturb other instruments. The newly created library is meant just
for Amor. if no problems occur, we can upstream the changes to master
at the end of October and create a new library "2023-v3".
2024-10-04 14:57:21 +02:00
39098fd0d1 Adds .clang-format style for formatting files 2024-09-25 16:21:01 +02:00
d44fdbf736 Updated the first-time-poll of C804 Axis 2024-09-23 16:21:02 +02:00
20e5c35d44 Initial driver version for the C804 controller 2024-09-23 16:21:02 +02:00
1539bfc66a bugfix: don't need to run on initialisation 2024-09-23 09:04:08 +02:00
d88e5877a7 Bugfix: A disabled PMac is no longer consider moving 2024-09-18 10:57:57 +02:00
118e177e04 Bugfix: A disabled PMac is no longer consider moving 2024-09-18 10:48:59 +02:00
f61daf0b49 just adds some warnings for potentially incorrect code 2024-09-11 15:07:25 +02:00
6dbd0fc0d7 Merge branch 'SINQSW-86-read-after-motor-reconnection' into 'master'
SINQSW-86 fix build error

See merge request sinqdev/sinqepicsapp!2
2024-09-10 09:21:15 +02:00
e657ea675c SINQSW-86 fix build error 2024-09-10 09:21:15 +02:00
5c0c917be9 Merge branch 'initializer_for_EL734Axis' into 'master'
Girder axis and initializer EL734Axis

See merge request sinqdev/sinqepicsapp!3
2024-09-10 09:19:24 +02:00
35c12274b0 Merge branch 'master' into 'initializer_for_EL734Axis'
# Conflicts:
#   sinqEPICSApp/src/EL734Driver.cpp
2024-09-10 09:19:06 +02:00
a6a8f14b26 The member variable oredMSR of EL734Axis contains the axis status from the previous poll.
Therefore, it needs to initialized with a sensible value (i.e. 1 which
means that the axis is standing still and has no errors.
2024-09-10 08:58:59 +02:00
33f118ce1e Fixed type in pmacController.cpp 2024-08-20 11:14:18 +02:00
adf8b30692 Minor changes to SINQ drivers:
- Fixed enable/disable at MasterMACS
2024-08-20 11:12:18 +02:00
c972cce072 Added girder translation axis drivers 2024-07-18 13:43:12 +02:00
64c8b08ce4 Fixed an initialisation bug in EL734Driver which prevented motors from
being polled. The value in question was next_poll which is now set to -1
in the constructor.
2024-06-07 13:36:34 +02:00
30228adf50 - Fixed a enable PV initialisation bug in MasterMACS
- Made the HRPT axis base itself on V3 of the pmacAxis
- Improved and added utils programs
2024-06-07 08:58:53 +02:00
d9d6dae19f Updated the sync utilities 2024-05-23 16:00:32 +02:00
32a8c27dbf Merge branch 'speed' 2024-02-23 11:34:26 +01:00
235e403fb3 - Added Makefile.RHEL8 for RHEL8
- Added a interMessagePeriod to SINQController, pmacController and MasterMACSController. Also a iocsh command to modify this at runtime
2024-02-23 11:30:46 +01:00
b4e201ae86 Final version of the SINQ EPICS module for RHEL7 in 2023
- Fixes to MasterMACS, mostly.
- Minor changes
2024-01-11 14:05:49 +01:00
9422353107 Alex: Committing Mark's fixes to el737 devsup 2023-09-12 16:43:24 +02:00
e3ac2962f5 - Many improvements to the MasterMACS driver
- Slowing down the pmac driver
- Fixing a bug in the Nanotec driver which caused an IOC crash when the
  motor sends bad data
2023-08-28 15:01:48 +02:00
7a81e2c5a0 Reduced output from MasterMacsDriver 2023-07-13 10:57:07 +02:00
f1a17bc295 Added AMOR detector tower special pmac motors: working now 2023-07-07 13:54:21 +02:00
8a6441927a Implemented that enable commands are only sent when a status chnage is required
Stop commands are only sent by EL734 and pmac when actually moving. The
motor record logic calls stop() in an excessive way.
2023-05-31 09:37:22 +02:00
72afd02b4e Many improvements to the MasterMACS motor driver. It is working but the hardware is shaky.
Added support for dose rate controlled Phytron motors. Not tested!
Small bug fixes
2023-05-31 09:13:41 +02:00
f333a27482 Added missing file after merging MasterMACS 2023-03-21 15:05:25 +01:00
4a2731b054 Added yet another missing file 2023-03-21 14:58:21 +01:00
9055a86b57 Added two missing files 2023-03-21 14:56:38 +01:00
ccd73babd5 First working version of the MasterMACS EPICS driver
Also added some test code
2023-03-21 14:55:07 +01:00
b8896b7a85 Implemented speed for the PMAC controller 2023-02-22 11:54:54 +01:00
acf1751081 Merge branch 'master' into speed 2023-02-17 10:46:06 +01:00
61290b5e2c Added utility programs which update motor subsitutions files from
the values read rom the actual controller
2023-02-17 10:45:15 +01:00
d706915a46 Implemented changing speed on EL734
Added a test IOCs for EL734
2023-02-17 10:43:51 +01:00
3c9932dc18 - Reduced poll rate of EL737
- Removed unneeded hardware limit reading code from EL734
2023-02-16 14:29:05 +01:00
9b9072b83b Improved error handling in the EL737 scalerRecord driver 2023-01-31 11:42:04 +01:00
3b2a21094c Fixed pmacV3 axis codes 2023-01-12 15:16:49 +01:00
a899a28182 sMore debugging output in devScalerEL737 2023-01-11 15:09:43 +01:00
72 changed files with 25712 additions and 945 deletions

236
.clang-format Normal file
View File

@ -0,0 +1,236 @@
---
Language: Cpp
# BasedOnStyle: LLVM
AccessModifierOffset: -2
AlignAfterOpenBracket: Align
AlignArrayOfStructures: None
AlignConsecutiveAssignments:
Enabled: false
AcrossEmptyLines: false
AcrossComments: false
AlignCompound: false
PadOperators: true
AlignConsecutiveBitFields:
Enabled: false
AcrossEmptyLines: false
AcrossComments: false
AlignCompound: false
PadOperators: false
AlignConsecutiveDeclarations:
Enabled: false
AcrossEmptyLines: false
AcrossComments: false
AlignCompound: false
PadOperators: false
AlignConsecutiveMacros:
Enabled: false
AcrossEmptyLines: false
AcrossComments: false
AlignCompound: false
PadOperators: false
AlignConsecutiveShortCaseStatements:
Enabled: false
AcrossEmptyLines: false
AcrossComments: false
AlignCaseColons: false
AlignEscapedNewlines: Right
AlignOperands: Align
AlignTrailingComments:
Kind: Always
OverEmptyLines: 0
AllowAllArgumentsOnNextLine: true
AllowAllParametersOfDeclarationOnNextLine: true
AllowShortBlocksOnASingleLine: Never
AllowShortCaseLabelsOnASingleLine: false
AllowShortEnumsOnASingleLine: true
AllowShortFunctionsOnASingleLine: All
AllowShortIfStatementsOnASingleLine: Never
AllowShortLambdasOnASingleLine: All
AllowShortLoopsOnASingleLine: false
AlwaysBreakAfterDefinitionReturnType: None
AlwaysBreakAfterReturnType: None
AlwaysBreakBeforeMultilineStrings: false
AlwaysBreakTemplateDeclarations: MultiLine
AttributeMacros:
- __capability
BinPackArguments: true
BinPackParameters: true
BitFieldColonSpacing: Both
BraceWrapping:
AfterCaseLabel: false
AfterClass: false
AfterControlStatement: Never
AfterEnum: false
AfterExternBlock: false
AfterFunction: false
AfterNamespace: false
AfterObjCDeclaration: false
AfterStruct: false
AfterUnion: false
BeforeCatch: false
BeforeElse: false
BeforeLambdaBody: false
BeforeWhile: false
IndentBraces: false
SplitEmptyFunction: true
SplitEmptyRecord: true
SplitEmptyNamespace: true
BreakAfterAttributes: Never
BreakAfterJavaFieldAnnotations: false
BreakArrays: true
BreakBeforeBinaryOperators: None
BreakBeforeConceptDeclarations: Always
BreakBeforeBraces: Attach
BreakBeforeInlineASMColon: OnlyMultiline
BreakBeforeTernaryOperators: true
BreakConstructorInitializers: BeforeColon
BreakInheritanceList: BeforeColon
BreakStringLiterals: true
ColumnLimit: 80
CommentPragmas: '^ IWYU pragma:'
CompactNamespaces: false
ConstructorInitializerIndentWidth: 4
ContinuationIndentWidth: 4
Cpp11BracedListStyle: true
DerivePointerAlignment: false
DisableFormat: false
EmptyLineAfterAccessModifier: Never
EmptyLineBeforeAccessModifier: LogicalBlock
ExperimentalAutoDetectBinPacking: false
FixNamespaceComments: true
ForEachMacros:
- foreach
- Q_FOREACH
- BOOST_FOREACH
IfMacros:
- KJ_IF_MAYBE
IncludeBlocks: Preserve
IncludeCategories:
- Regex: '^"(llvm|llvm-c|clang|clang-c)/'
Priority: 2
SortPriority: 0
CaseSensitive: false
- Regex: '^(<|"(gtest|gmock|isl|json)/)'
Priority: 3
SortPriority: 0
CaseSensitive: false
- Regex: '.*'
Priority: 1
SortPriority: 0
CaseSensitive: false
IncludeIsMainRegex: '(Test)?$'
IncludeIsMainSourceRegex: ''
IndentAccessModifiers: false
IndentCaseBlocks: false
IndentCaseLabels: false
IndentExternBlock: AfterExternBlock
IndentGotoLabels: true
IndentPPDirectives: None
IndentRequiresClause: true
IndentWidth: 4
IndentWrappedFunctionNames: false
InsertBraces: false
InsertNewlineAtEOF: false
InsertTrailingCommas: None
IntegerLiteralSeparator:
Binary: 0
BinaryMinDigits: 0
Decimal: 0
DecimalMinDigits: 0
Hex: 0
HexMinDigits: 0
JavaScriptQuotes: Leave
JavaScriptWrapImports: true
KeepEmptyLinesAtTheStartOfBlocks: true
KeepEmptyLinesAtEOF: false
LambdaBodyIndentation: Signature
LineEnding: DeriveLF
MacroBlockBegin: ''
MacroBlockEnd: ''
MaxEmptyLinesToKeep: 1
NamespaceIndentation: None
ObjCBinPackProtocolList: Auto
ObjCBlockIndentWidth: 2
ObjCBreakBeforeNestedBlockParam: true
ObjCSpaceAfterProperty: false
ObjCSpaceBeforeProtocolList: true
PackConstructorInitializers: BinPack
PenaltyBreakAssignment: 2
PenaltyBreakBeforeFirstCallParameter: 19
PenaltyBreakComment: 300
PenaltyBreakFirstLessLess: 120
PenaltyBreakOpenParenthesis: 0
PenaltyBreakString: 1000
PenaltyBreakTemplateDeclaration: 10
PenaltyExcessCharacter: 1000000
PenaltyIndentedWhitespace: 0
PenaltyReturnTypeOnItsOwnLine: 60
PointerAlignment: Right
PPIndentWidth: -1
QualifierAlignment: Leave
ReferenceAlignment: Pointer
ReflowComments: true
RemoveBracesLLVM: false
RemoveParentheses: Leave
RemoveSemicolon: false
RequiresClausePosition: OwnLine
RequiresExpressionIndentation: OuterScope
SeparateDefinitionBlocks: Leave
ShortNamespaceLines: 1
SortIncludes: CaseSensitive
SortJavaStaticImport: Before
SortUsingDeclarations: LexicographicNumeric
SpaceAfterCStyleCast: false
SpaceAfterLogicalNot: false
SpaceAfterTemplateKeyword: true
SpaceAroundPointerQualifiers: Default
SpaceBeforeAssignmentOperators: true
SpaceBeforeCaseColon: false
SpaceBeforeCpp11BracedList: false
SpaceBeforeCtorInitializerColon: true
SpaceBeforeInheritanceColon: true
SpaceBeforeJsonColon: false
SpaceBeforeParens: ControlStatements
SpaceBeforeParensOptions:
AfterControlStatements: true
AfterForeachMacros: true
AfterFunctionDefinitionName: false
AfterFunctionDeclarationName: false
AfterIfMacros: true
AfterOverloadedOperator: false
AfterRequiresInClause: false
AfterRequiresInExpression: false
BeforeNonEmptyParentheses: false
SpaceBeforeRangeBasedForLoopColon: true
SpaceBeforeSquareBrackets: false
SpaceInEmptyBlock: false
SpacesBeforeTrailingComments: 1
SpacesInAngles: Never
SpacesInContainerLiterals: true
SpacesInLineCommentPrefix:
Minimum: 1
Maximum: -1
SpacesInParens: Never
SpacesInParensOptions:
InCStyleCasts: false
InConditionalStatements: false
InEmptyParentheses: false
Other: false
SpacesInSquareBrackets: false
Standard: Latest
StatementAttributeLikeMacros:
- Q_EMIT
StatementMacros:
- Q_UNUSED
- QT_REQUIRE_VERSION
TabWidth: 4
UseTab: Never
VerilogBreakBetweenInstancePorts: true
WhitespaceSensitiveMacros:
- BOOST_PP_STRINGIZE
- CF_SWIFT_NAME
- NS_SWIFT_NAME
- PP_STRINGIZE
- STRINGIZE
...

4
.gitignore vendored
View File

@ -5,6 +5,9 @@
# Ignore text editor (e.g. emacs) autosave files
*~
# Build Artifacts
O.*_Common/
O.*_RHEL8-x86_64/
# Compiled Object files
*.slo
@ -66,6 +69,7 @@ local.properties
*.user
*.sln.docstates
*.sdf
.cvsignore
#Test results
*.log

51
.gitlab-ci.yml Normal file
View File

@ -0,0 +1,51 @@
default:
image: docker.psi.ch:5000/wall_e/sinqepics:latest
stages:
- test
- build
cppcheck:
stage: test
script:
- cppcheck --std=c++17 --addon=cert --addon=misc --error-exitcode=1 sinqEPICSApp/
allow_failure: true # Long term this needs to be removed
artifacts:
expire_in: 1 week
tags:
- docker
formatting:
stage: test
script:
- clang-format --style=file --Werror --dry-run sinqEPICSApp/src/*.cpp sinqEPICSApp/src/*.c sinqEPICSApp/src/*.h
allow_failure: true # Long term this needs to be removed
artifacts:
expire_in: 1 week
tags:
- docker
# clangtidy:
# stage: test
# script:
# - curl https://docker.psi.ch:5000/v2/_catalog
# # - dnf update -y
# # - dnf install -y clang-tools-extra
# # - clang-tidy sinqEPICSApp/src/*.cpp sinqEPICSApp/src/*.c sinqEPICSApp/src/*.h -checks=cppcoreguidelines-*,cert-*
# # tags:
# # - docker
build_module:
stage: build
script:
- sed -i 's/ARCH_FILTER=.*/ARCH_FILTER=linux%/' Makefile.RHEL8
- make -f Makefile.RHEL8 install
- cp -rT "/ioc/modules/sinq/$(ls -U /ioc/modules/sinq/ | head -1)" "./sinq-${CI_COMMIT_SHORT_SHA}"
artifacts:
name: "sinq-${CI_COMMIT_SHORT_SHA}"
paths:
- "sinq-${CI_COMMIT_SHORT_SHA}/*"
expire_in: 1 week
when: always
tags:
- docker

View File

@ -2,7 +2,6 @@
include /ioc/tools/driver.makefile
MODULE=sinq
LIBVERSION=koennecke
BUILDCLASSES=Linux
EPICS_VERSIONS=3.14.12 7.0.4.1
@ -32,5 +31,6 @@ SOURCES += sinqEPICSApp/src/EuroMoveDriver.cpp
SOURCES += sinqEPICSApp/src/pmacAsynIPPort.c
SOURCES += sinqEPICSApp/src/pmacAxis.cpp
SOURCES += sinqEPICSApp/src/pmacController.cpp
SOURCES += sinqEPICSApp/src/drvAsynMasterMACSPort.c
SOURCES += sinqEPICSApp/src/MasterMACSDriver.cpp
# MISCS would be the place to keep the stream device template files

View File

@ -2,7 +2,7 @@
include /ioc/tools/driver.makefile
MODULE=sinq
LIBVERSION=brambilla_m
LIBVERSION=brambilla_m_test
BUILDCLASSES=Linux
EPICS_VERSIONS=3.14.12 7.0.4.1
ARCH_FILTER=RHEL7-x86_64

42
Makefile.RHEL8 Normal file
View File

@ -0,0 +1,42 @@
# This build the sinq extensions for the PSI EPICS setup
include /ioc/tools/driver.makefile
MODULE=sinq
BUILDCLASSES=Linux
EPICS_VERSIONS=7.0.7
ARCH_FILTER=RHEL8%
# additional module dependencies
REQUIRED+=SynApps
REQUIRED+=stream
REQUIRED+=scaler
REQUIRED+=motorBase
# DB files to include in the release
TEMPLATES += sinqEPICSApp/Db/dimetix.db
TEMPLATES += sinqEPICSApp/Db/slsvme.db
TEMPLATES += sinqEPICSApp/Db/spsamor.db
TEMPLATES += sinqEPICSApp/Db/el734.db
# DBD files to include in the release
DBDS += sinqEPICSApp/src/sinq.dbd
# Source files to build
SOURCES += sinqEPICSApp/src/devScalerEL737.c
SOURCES += sinqEPICSApp/src/SINQController.cpp
SOURCES += sinqEPICSApp/src/SINQAxis.cpp
SOURCES += sinqEPICSApp/src/EL734Driver.cpp
SOURCES += sinqEPICSApp/src/NanotecDriver.cpp
SOURCES += sinqEPICSApp/src/stptok.cpp
SOURCES += sinqEPICSApp/src/PhytronDriver.cpp
SOURCES += sinqEPICSApp/src/EuroMoveDriver.cpp
# SOURCES += sinqEPICSApp/src/pmacAsynIPPort.c
# SOURCES += sinqEPICSApp/src/pmacAxis.cpp
# SOURCES += sinqEPICSApp/src/pmacController.cpp
SOURCES += sinqEPICSApp/src/MasterMACSDriver.cpp
SOURCES += sinqEPICSApp/src/C804Axis.cpp
SOURCES += sinqEPICSApp/src/C804Controller.cpp
USR_CFLAGS += -Wall -Wextra # -Werror
# MISCS would be the place to keep the stream device template files

View File

@ -38,3 +38,12 @@ Those political problems require a special development model:
Take care of the sinqEPICsApp/src/sinq.dbd file. This is the one which differs mostly between
amorsim and master branches.
# Formatting
Formatting is done via the [`.clang-format`](./.clang-format) file checked into
the repository. One option to apply the formatting to a given file is via the
command below.
```
clang-format -i -style=file <file>
```

View File

@ -4,6 +4,7 @@ record(motor,"$(P)$(M)")
field(DTYP,"$(DTYP)")
field(DIR,"$(DIR)")
field(VELO,"$(VELO)")
field(VMAX,"$(VMAX=0)")
field(VBAS,"$(VBAS)")
field(ACCL,"$(ACCL)")
field(BDST,"$(BDST)")

34
sinqEPICSApp/Db/el734.db Normal file
View File

@ -0,0 +1,34 @@
record(motor,"$(P)$(M)")
{
field(DESC,"$(DESC)")
field(DTYP,"$(DTYP)")
field(DIR,"$(DIR)")
field(VELO,"$(VELO)")
field(HVEL,"$(VELO)")
field(VBAS,"$(VELO)")
field(VMAX, "${VMAX}")
field(ACCL,"$(ACCL)")
field(BDST,"$(BDST)")
field(BVEL,"$(BVEL)")
field(BACC,"$(BACC)")
field(OUT,"@asyn($(PORT),$(ADDR))")
field(MRES,"$(MRES)")
field(PREC,"$(PREC)")
field(EGU,"$(EGU)")
field(DHLM,"$(DHLM)")
field(DLLM,"$(DLLM)")
field(INIT,"$(INIT)")
field(PINI, "NO")
field(TWV,"1")
field(RTRY,"0")
}
# The message text
record(waveform, "$(P)$(M)-MsgTxt") {
field(DTYP, "asynOctetRead")
field(INP, "@asyn($(PORT),$(N),1) MOTOR_MESSAGE_TEXT")
field(FTVL, "CHAR")
field(NELM, "80")
field(SCAN, "I/O Intr")
}

View File

@ -0,0 +1,54 @@
record(motor,"$(P)$(M)")
{
field(DESC,"$(DESC)")
field(DTYP,"$(DTYP)")
field(DIR,"$(DIR)")
field(VELO,"$(VELO)")
field(VMAX,"$(VMAX=$(VELO))")
field(VBAS,"$(VBAS)")
field(ACCL,"$(ACCL)")
field(BDST,"$(BDST)")
field(BVEL,"$(BVEL)")
field(BACC,"$(BACC)")
field(OUT,"@asyn($(PORT),$(ADDR))")
field(MRES,"$(MRES)")
field(PREC,"$(PREC)")
field(EGU,"$(EGU)")
field(DHLM,"$(DHLM)")
field(DLLM,"$(DLLM)")
field(INIT,"$(INIT)")
field(TWV,"1")
field(RDBD,"$(RDBD)")
}
# The message text
record(waveform, "$(P)$(M)-MsgTxt") {
field(DTYP, "asynOctetRead")
field(INP, "@asyn($(PORT),$(N),1) MOTOR_MESSAGE_TEXT")
field(FTVL, "CHAR")
field(NELM, "80")
field(SCAN, "I/O Intr")
}
# enable axis
record(longout, "$(P)$(M):Enable") {
field(DTYP, "asynInt32")
field(OUT, "@asyn($(PORT),$(N),1) ENABLE_AXIS")
field(PINI, "YES")
}
# enable axis
record(longin, "$(P)$(M):Enable_RBV") {
field(DTYP, "asynInt32")
field(INP, "@asyn($(PORT),$(N),1) AXIS_ENABLED")
field(PINI, "YES")
field(SCAN, "1 second")
}
# reread encoder
record(longout, "$(P)$(M):Reread_Encoder") {
field(DTYP, "asynInt32")
field(OUT, "@asyn($(PORT),$(N),1) REREAD_ENCODER_POSITION")
field(PINI, "NO")
}

View File

@ -0,0 +1,477 @@
#include "C804Axis.h"
#include "C804Controller.h"
#include <cmath>
#include <errlog.h>
#include <limits>
#include <math.h>
#include <string.h>
#include <unistd.h>
C804Axis::C804Axis(C804Controller *pC, int axisNo)
: SINQAxis(pC, axisNo), pC_(pC) {
/*
The superclass constructor SINQAxis calls in turn its superclass constructor
asynMotorAxis. In the latter, a pointer to the constructed object this is
stored inside the array pAxes_:
pC->pAxes_[axisNo] = this;
Therefore, the axes are managed by the controller pC. See C804Controller.cpp
for further explanation. If axisNo is out of bounds, asynMotorAxis prints an
error (see
https://github.com/epics-modules/motor/blob/master/motorApp/MotorSrc/asynMotorAxis.cpp,
line 40). However, we want the IOC creation to stop completely, since this
is a configuration error.
*/
if (axisNo >= pC->numAxes_) {
exit(-1);
}
last_position_steps_ = 0;
last_poll_ = 0.0;
}
C804Axis::~C804Axis(void) {
// Since the controller memory is managed somewhere else, we don't need to
// clean up the pointer pC here.
}
/*
The polling function informs us about the state of the axis, in particular if it
is currently moving. It is called periodically, with the period defined by
the controller constructor arguments idlePollPeriod and movingPollPeriod
depending on the current axis state.
*/
asynStatus C804Axis::poll(bool *moving) {
// Local variable declaration
static const char *functionName = "C804Axis::poll";
// The poll function is just a wrapper around poll_no_param_lib_update and
// handles mainly the callParamCallbacks() function
asynStatus status_poll = C804Axis::poll_no_param_lib_update(moving);
// According to the function documentation of asynMotorAxis::poll, this
// function should be called at the end of a poll implementation.
asynStatus status_callback = callParamCallbacks();
if (status_callback != asynSuccess) {
asynPrint(pC_->pasynUserSelf, ASYN_TRACE_ERROR,
"%s: Updating the parameter library failed for axis %d\n",
functionName, axisNo_);
return status_callback;
} else {
return status_poll;
}
}
// Perform the actual poll
asynStatus C804Axis::poll_no_param_lib_update(bool *moving) {
// Local variable declaration
static const char *functionName = "C804Axis::poll";
asynStatus status;
int axis_status = 0;
// The controller returns the position and velocity in encoder steps.
// This value needs to be converted in user units (engineering units EGU)
// via the record field MRES of the motor record. This field has already
// been read by the constructor into the member variable
// motorRecResolution_. To go from steps to user units, multiply with
// motorRecResolution_ Example: If 10 steps correspond to 1 mm, MRES should
// be 0.1.
int position_error_steps = 0;
int motor_position_steps = 0;
int motor_velocity_steps = 0;
int programmed_motor_velocity_steps = 0;
double position_error = .0;
double motor_position = .0;
double motor_velocity = .0;
double programmed_motor_velocity = .0;
// The buffer sizes for command and response are defined in the controller
// (see the corresponding source code files)
char command[pC_->C804_MAXBUF_], response[pC_->C804_MAXBUF_];
/*
Cancel the poll if the last poll has "just" happened.
*/
if (time(NULL) < last_poll_ + 0.5 * pC_->movingPollPeriod_) {
asynPrint(pC_->pasynUserSelf, ASYN_TRACE_WARNING,
"%s: Aborted poll since the last poll for axis %d happened a "
"short time ago\n",
functionName, axisNo_);
return asynSuccess;
} else {
last_poll_ = time(NULL);
}
/*
The parameter motorRecResolution_ is coupled to the field MRES of the motor
record in the following manner:
- In sinq_asyn_motor.db, the PV (motor_record_pv_name)MOTOR_REC_RESOLUTION
is defined as a copy of the field (motor_record_pv_name).MRES:
record(ao,"$(P)$(M):Resolution") {
field(DESC, "$(M) resolution")
field(DOL, "$(P)$(M).MRES CP MS")
field(OMSL, "closed_loop")
field(DTYP, "asynFloat64")
field(OUT, "@asyn($(PORT),$(ADDR))MOTOR_REC_RESOLUTION")
field(PREC, "$(PREC)")
}
- The PV name MOTOR_REC_RESOLUTION is coupled in asynMotorController.h to
the constant motorRecResolutionString
- ... which in turn is assigned to motorRecResolution_ in
asynMotorController.cpp This way of making the field visible to the driver
is described here: https://epics.anl.gov/tech-talk/2020/msg00378.php This is
a one-way coupling, changes to the parameter library via setDoubleParam are
NOT transferred to (motor_record_pv_name).MRES or to
(motor_record_pv_name):Resolution.
NOTE: This function must not be called in the constructor (e.g. in order to
save the read result to the member variable earlier), since the parameter
library is updated at a later stage!
*/
pC_->getDoubleParam(axisNo_, pC_->motorRecResolution_,
&motorRecResolution_);
asynPrint(pC_->pasynUserSelf, ASYN_TRACE_FLOW, "Poll axis %d\n", axisNo_);
/*
We know that the motor resolution must not be zero. During the startup of
the IOC, polls can happen before the record is fully initialized. In that
case, all values are zero.
*/
if (motorRecResolution_ == 0) {
return asynError;
}
/*
Assume that the axis does not have a status problem. If it does have a
problem, this value will be overwritten further below. Setting this value
in itself does not trigger a callback immediately, any callbacks
(such as e.g. updating camonitor) are done in callParamCallbacks() at the
end of this function.
*/
setIntegerParam(pC_->motorStatusProblem_, false);
// Read out the position error of the axis (delta of target position to
// actual position)
snprintf(command, pC_->C804_MAXBUF_ - 1, "%dTE", axisNo_);
status = pC_->lowLevelWriteRead(axisNo_, command, response, true);
if (status == asynSuccess) {
int parsed_axis;
sscanf(response, "%2dE%10d", &parsed_axis, &position_error_steps);
// Scale from the encoder resultion to user units
position_error = double(position_error_steps) * motorRecResolution_;
asynPrint(pC_->pasynUserSelf, ASYN_TRACE_FLOW,
"%s: Axis %d, response %s, position error %f\n", functionName,
axisNo_, response, position_error);
} else {
asynPrint(pC_->pasynUserSelf, ASYN_TRACE_ERROR,
"%s: Reading the position error failed for axis %d\n",
functionName, axisNo_);
setIntegerParam(pC_->motorStatusProblem_, true);
// Stop the evaluation prematurely
return status;
}
// Read the current position.
snprintf(command, this->pC_->C804_MAXBUF_ - 1, "%dTP", this->axisNo_);
status =
this->pC_->lowLevelWriteRead(this->axisNo_, command, response, true);
if (status == asynSuccess) {
int parsed_axis;
sscanf(response, "%2dP%10d", &parsed_axis, &motor_position_steps);
// Scale from the encoder resultion to user units
motor_position = double(motor_position_steps) * motorRecResolution_;
asynPrint(pC_->pasynUserSelf, ASYN_TRACE_FLOW,
"%s: Axis %d, response %s, position %f\n", functionName,
axisNo_, response, motor_position);
setDoubleParam(pC_->motorPosition_, motor_position);
setDoubleParam(pC_->motorEncoderPosition_, motor_position);
} else {
asynPrint(pC_->pasynUserSelf, ASYN_TRACE_ERROR,
"%s: Reading the position failed for axis %d\n", functionName,
axisNo_);
setIntegerParam(pC_->motorStatusProblem_, true);
return status;
}
// Read the current velocity
snprintf(command, this->pC_->C804_MAXBUF_ - 1, "%dTV", this->axisNo_);
status =
this->pC_->lowLevelWriteRead(this->axisNo_, command, response, true);
if (status == asynSuccess) {
int parsed_axis;
sscanf(response, "%2dV%10d", &parsed_axis, &motor_velocity_steps);
// Scale from the encoder resultion to user units
motor_velocity = double(motor_velocity_steps) * motorRecResolution_;
asynPrint(pC_->pasynUserSelf, ASYN_TRACE_FLOW,
"%s: Axis %d, response %s, velocity %f\n", functionName,
axisNo_, response, motor_velocity);
} else {
asynPrint(pC_->pasynUserSelf, ASYN_TRACE_ERROR,
"%s: Reading the velocity failed for axis %d\n", functionName,
axisNo_);
setIntegerParam(pC_->motorStatusProblem_, true);
return status;
}
// Read the programmed velocity
snprintf(command, this->pC_->C804_MAXBUF_ - 1, "%dTY", this->axisNo_);
status =
this->pC_->lowLevelWriteRead(this->axisNo_, command, response, true);
if (status == asynSuccess) {
int parsed_axis;
sscanf(response, "%2dY%10d", &parsed_axis,
&programmed_motor_velocity_steps);
// Scale from the encoder resultion to user units
programmed_motor_velocity =
double(programmed_motor_velocity_steps) * motorRecResolution_;
asynPrint(pC_->pasynUserSelf, ASYN_TRACE_FLOW,
"%s: Axis %d, response %s, programmed velocity %f\n",
functionName, axisNo_, response, programmed_motor_velocity);
} else {
asynPrint(pC_->pasynUserSelf, ASYN_TRACE_ERROR,
"%s: Reading the programmed velocity failed for axis %d\n",
functionName, axisNo_);
setIntegerParam(pC_->motorStatusProblem_, true);
return status;
}
// Read the motor status
snprintf(command, pC_->C804_MAXBUF_ - 1, "%dTS", axisNo_);
status = pC_->lowLevelWriteRead(this->axisNo_, command, response, true);
if (status == asynSuccess) {
int parsed_axis;
sscanf(response, "%2dS%10d", &parsed_axis, &axis_status);
asynPrint(pC_->pasynUserSelf, ASYN_TRACE_FLOW,
"%s: Axis %d, response %s, status %d\n", functionName,
axisNo_, response, axis_status);
} else {
asynPrint(pC_->pasynUserSelf, ASYN_TRACE_ERROR,
"%s: Reading the motor status %d\n", functionName, axisNo_);
setIntegerParam(pC_->motorStatusProblem_, true);
// Stop prematurely
return status;
}
// Check if the axis is enabled by reading out bit 2 (see
// https://stackoverflow.com/questions/2249731/how-do-i-get-bit-by-bit-data-from-an-integer-value-in-c)
int mask = 1 << 2;
int masked_n = axis_status & mask;
// Is 1 if the axis is disabled
int disabled = masked_n >> 2;
if (disabled) {
enabled_ = false;
} else {
enabled_ = true;
}
/*
Determine if the motor is moving. This is determined by the following
criteria: 1) The motor position changes from poll to poll 2) The motor is
enabled
*/
*moving = enabled_ && motor_position_steps != this->last_position_steps_;
// Update the cached_position
this->last_position_steps_ = motor_position_steps;
/*
Calculate the time the motor should need to reach its target, based on the
programmed velocity and compare this to the actual time the motor has spent
moving. If it has spent too much time in a moving state without reaching
the target, stop the motor and return an error.
*/
if (*moving) {
int motorStatusMoving = 0;
pC_->getIntegerParam(axisNo_, pC_->motorStatusMoving_,
&motorStatusMoving);
// motor is moving, but didn't move in the last poll
if (motorStatusMoving == 0) {
time_t current_time = time(NULL);
// Factor 2 of the calculated moving time
estimatedArrivalTime_ =
current_time + std::ceil(2 * std::fabs(position_error) /
programmed_motor_velocity);
} else {
// /*
// Motor is moving for a longer time than it should: Stop it
// */
// if (time(NULL) > estimatedArrivalTime_)
// {
// snprintf(command, pC_->C804_MAXBUF_ - 1, "%dST", axisNo_);
// status = pC_->lowLevelWriteRead(axisNo_, command, response);
// asynPrint(pC_->pasynUserSelf, ASYN_TRACE_ERROR, "%s: Stopped
// axis %d since it moved for double the time it should to reach
// its target\n", functionName, axisNo_);
// }
}
}
/*
One of these parameters (or both) are used to set (PV-name).DMOV.
This PV tells EPICS whether the axis / motor is currently moving or not.
*/
setIntegerParam(pC_->motorStatusMoving_, *moving);
setIntegerParam(pC_->motorStatusDone_, !(*moving));
callParamCallbacks();
return status;
}
asynStatus C804Axis::move(double position, int relative, double minVelocity,
double maxVelocity, double acceleration) {
asynStatus status;
static const char *functionName = "C804Axis::move";
char command[pC_->C804_MAXBUF_], response[pC_->C804_MAXBUF_];
double position_c_units = 0.0; // Controller units
int position_steps = 0;
// Convert from user coordinates (EGU) to controller coordinates (steps).
// Check for overflow
if (motorRecResolution_ == 0.0) {
asynPrint(pC_->pasynUserSelf, ASYN_TRACE_ERROR,
"%s: MRES must not be zero. Movement is aborted",
functionName);
return asynError;
}
position_c_units = position / motorRecResolution_;
// Check for overflow during the division
if (position_c_units * motorRecResolution_ != position) {
asynPrint(
pC_->pasynUserSelf, ASYN_TRACE_ERROR,
"%s: could not convert from user units (%f) to controller units "
"(user units divided by resolution MRES %f) due to overflow.",
functionName, position, motorRecResolution_);
return asynError;
}
// Steps can only be integer values => cast to integer while checking for
// overflow
if (std::numeric_limits<int>::max() < position_c_units ||
std::numeric_limits<int>::min() > position_c_units) {
asynPrint(pC_->pasynUserSelf, ASYN_TRACE_ERROR,
"%s: target position %f cannot be converted to int "
"(overflow). Check target value %f and MRES %f",
functionName, position_c_units, position_c_units,
motorRecResolution_);
return asynError;
}
position_steps = static_cast<int>(position_c_units);
// Convert from relative to absolute values
if (relative) {
position_steps += last_position_steps_;
}
// If the axis is currently disabled, enable it
if (!enabled_) {
snprintf(command, pC_->C804_MAXBUF_ - 1, "%dGO", axisNo_);
status =
pC_->lowLevelWriteRead(this->axisNo_, command, response, false);
if (status != asynSuccess) {
asynPrint(pC_->pasynUserSelf, ASYN_TRACE_ERROR,
"%s: Enabling axis %d\n failed", functionName, axisNo_);
return status;
}
}
// Start movement
snprintf(command, pC_->C804_MAXBUF_ - 1, "%dMA%d", axisNo_, position_steps);
status = pC_->lowLevelWriteRead(this->axisNo_, command, response, false);
if (status != asynSuccess) {
asynPrint(pC_->pasynUserSelf, ASYN_TRACE_ERROR,
"%s: Setting the target position %d failed for axis %d\n",
functionName, position_steps, axisNo_);
setIntegerParam(pC_->motorStatusProblem_, true);
return status;
}
setIntegerParam(pC_->motorStatusProblem_, false);
// Reset the error flag
errorReported_ = 0;
return status;
}
asynStatus C804Axis::moveVelocity(double min_velocity, double max_velocity,
double acceleration) {
static const char *functionName = "C804Axis::moveVelocity";
return asynError;
}
asynStatus C804Axis::stop(double acceleration) {
asynStatus status = asynSuccess;
static const char *functionName = "C804Axis::stop";
char command[pC_->C804_MAXBUF_], response[pC_->C804_MAXBUF_];
bool moving = false;
poll(&moving);
if (moving) {
// ST = Stop
snprintf(command, pC_->C804_MAXBUF_ - 1, "%dST", axisNo_);
status = pC_->lowLevelWriteRead(axisNo_, command, response, false);
asynPrint(pC_->pasynUserSelf, ASYN_TRACEIO_DEVICE, "%s: Stop axis %d\n",
functionName, axisNo_);
}
return status;
}
asynStatus C804Axis::home(double minVelocity, double maxVelocity,
double acceleration, int forwards) {
asynStatus status = asynSuccess;
static const char *functionName = "C804Axis::home";
char command[pC_->C804_MAXBUF_], response[pC_->C804_MAXBUF_];
snprintf(command, pC_->C804_MAXBUF_ - 1, "%dFE0",
axisNo_); // Home to the upper limit of the axis (25 mm)
status = pC_->lowLevelWriteRead(axisNo_, command, response, false);
asynPrint(pC_->pasynUserSelf, ASYN_TRACEIO_DEVICE, "%s: Homing axis %d\n",
functionName, axisNo_);
return status;
}
/**
If on is 0, disable the motor, otherwise enable it.
*/
asynStatus C804Axis::enable(int on) {
asynStatus status = asynSuccess;
static const char *functionName = "C804Axis::enable";
char command[pC_->C804_MAXBUF_], response[pC_->C804_MAXBUF_];
if (on == 0) {
snprintf(command, pC_->C804_MAXBUF_ - 1, "%dMF", axisNo_);
status = pC_->lowLevelWriteRead(axisNo_, command, response, false);
asynPrint(pC_->pasynUserSelf, ASYN_TRACEIO_DEVICE,
"%s: Disable axis %d\n", functionName, axisNo_);
} else {
snprintf(command, pC_->C804_MAXBUF_ - 1, "%dMN", axisNo_);
status = pC_->lowLevelWriteRead(axisNo_, command, response, false);
asynPrint(pC_->pasynUserSelf, ASYN_TRACEIO_DEVICE,
"%s: Enable axis %d\n", functionName, axisNo_);
}
return status;
}

View File

@ -0,0 +1,40 @@
#ifndef C804Axis_H
#define C804Axis_H
#include "SINQController.h"
#include "SINQAxis.h"
// Forward declaration of the controller class to resolve the cyclic dependency
// between C804Controller.h and C804Axis.h. See https://en.cppreference.com/w/cpp/language/class.
class C804Controller;
class C804Axis : public SINQAxis
{
public:
/* These are the methods we override from the base class */
C804Axis(C804Controller *pController, int axisNo);
virtual ~C804Axis();
asynStatus move(double position, int relative, double min_velocity, double max_velocity, double acceleration);
asynStatus moveVelocity(double min_velocity, double max_velocity, double acceleration);
asynStatus stop(double acceleration);
asynStatus home(double minVelocity, double maxVelocity, double acceleration, int forwards);
asynStatus poll(bool *moving);
asynStatus poll_no_param_lib_update(bool *moving);
asynStatus enable(int on);
protected:
C804Controller *pC_;
void checkBounds(C804Controller *pController, int axisNo);
int last_position_steps_;
double motorRecResolution_;
time_t estimatedArrivalTime_;
time_t last_poll_;
int errorReported_;
bool enabled_;
private:
friend class C804Controller;
};
#endif

View File

@ -0,0 +1,458 @@
/**
Overview EPICS documentation
- https://docs.epics-controls.org/en/latest/index.html
- https://epics.anl.gov/modules/soft/asyn/R4-29/asynDriver.html
- https://www.physicstom.com/epics/
- https://epics.anl.gov/modules/soft/asyn/R4-20/asynDriver.pdf
*/
#include "C804Controller.h"
#include "asynOctetSyncIO.h"
#include <errlog.h>
#include <string.h>
#include <unistd.h>
#include <iocsh.h>
#include <epicsExport.h>
#include <registryFunction.h>
const double C804Controller::C804_TIMEOUT_ = 5.0;
static const char *driverName = "C804Controller";
/*
In SINQ, this constructor is usually called by its C interface function via the IOC shell.
A typical call looks like that (taken from SANS instrument st.cmd):
pmacAsynIPConfigure("pmcu1","sans1-mcu1:1025")
pmacV3CreateController("mcu1","pmcu1",0,9,50,10000);
pmacV3CreateAxis("mcu1",1,0);
pmacV3CreateAxis("mcu1",2,0);
pmacV3CreateAxis("mcu1",3,0);
pmacV3CreateAxis("mcu1",4,0);
pmacV3CreateAxis("mcu1",5,0);
pmacV3CreateAxis("mcu1",6,0);
pmacV3CreateAxis("mcu1",7,0);
pmacV3CreateAxis("mcu1",8,0);
The first call creates a port object in EPICS (port 1025 in this specific case) with the name "pmcu1".
The second call creates the controller with the following arguments:
- portName = "mcu1": The controller is registered by this name in EPICS. The axes
constructors below use the name to get the controller pointer from EPICS.
- lowLevelPortName = "pmcu1": The EPICS controller object connects to the physical
device via the port object "pmcu1".
- lowLevelPortAddress = 0: Connects to port 0 of asynOctetSyncIO. asynOctetSyncIO
is an interface for ASCII-string-based message communication between driver and device.
This value seems to be always zero (at least for all pmacAsynIPConfigure in the SINQ IOCs)
- numAxes = 9: Constructs the array "pAxes_" with space for 9 axis pointers (see below).
In the axes constructors below, we use the array indices 1 to 8, hence leaving the
first element of the array unitialized (it has index 0). This is mainly a convenience
to avoid talking about an "axis 0" when meaning the first axis.
- movingPollPeriod: The method C804Controller::poll is called in a loop every
movingPollPeriod seconds when the axis is moving
- idlePollPeriod: The method C804Controller::poll is called in a loop every
idlePollPeriod seconds when the axis is not moving
*/
C804Controller::C804Controller(const char *portName, const char *lowLevelPortName, int lowLevelPortAddress,
int numAxes, double movingPollPeriod, double idlePollPeriod, const int &extraParams)
: SINQController(portName, lowLevelPortName, numAxes, extraParams)
{
// Definition of local variables.
asynStatus status = asynSuccess;
/*
functionName is overwritten by the name of the current function in each method
of C804Controller. This is used for feedback messages to the user.
*/
static const char *functionName = "C804Controller::C804Controller";
/*
Update: We don't need this array, since all axes accesses are done
with the getAxis-function, which accesses the base class array
============================================================================
The array pAxes_ is a member of the superclass asynMotorController and is
allocated when calling the constructor of asynMotorController, which
is done by the constructor of SINQController:
pAxes_ = (asynMotorAxis**) calloc(numAxes, sizeof(asynMotorAxis*));
Therefore, on this line we create a pointer to that array while interpreting
every pointer in it as one to a C804Axis. This is valid because we populate
the array in the constructor of C804Axis and can therefore be sure that
all asynMotorAxis pointers in asynMotorController::pAxes_ are in fact pointers
to C804Axis axes.
Interestingly, this array usually is leaked after destruction of asynMotorController
(n this class, we clean it up in the constructor).
The reason for that is that the EPICS devices are usually created only once at
the start of the program and the memory is cleaned up by the OS.
*/
// pAxes_ = (C804Axis **)(asynMotorController::pAxes_);
// Initialize non static data members
lowLevelPortUser_ = NULL;
movingPollPeriod_ = movingPollPeriod;
idlePollPeriod_ = idlePollPeriod;
/*
We try to connect to the port via the port name provided by the constructor.
If this fails, we return an error message.
*/
status = pasynOctetSyncIO->connect(lowLevelPortName, lowLevelPortAddress, &lowLevelPortUser_, NULL);
if (status != asynSuccess)
{
/*
ASYN_TRACE_ERROR is a mask for the trace of asynUser (member variable this->lowLevelPortUser_)
The different mask options are listed on this page: https://epics.anl.gov/modules/soft/asyn/R4-29/asynDriver.html (section AsynTrace):
0x1 ASYN_TRACE_ERROR Run time errors are reported, e.g. timeouts.
0x2 ASYN_TRACEIO_DEVICE Device support reports I/O activity.
0x4 ASYN_TRACEIO_FILTER Any layer between device support and the low level driver reports any filtering it does on I/O.
0x8 ASYN_TRACEIO_DRIVER Low level driver reports I/O activity.
0x10 ASYN_TRACE_FLOW Report logic flow. Device support should report all queue requests, callbacks entered, and all calls to drivers. Layers between device support and low level drivers should report all calls they make to lower level drivers. Low level drivers report calls they make to other support.
0x20 ASYN_TRACE_WARNING Report warnings, i.e. conditions that are between ASYN_TRACE_ERROR and ASYN_TRACE_FLOW.
To see the output of these functions e.g. on the shell, a trace mask needs
to be set:
asynSetTraceIOMask("L0", -1, 0x2)
asynSetTraceMask("L0", -1, 0x9) <- this enables 0x8 + 0x1 => ASYN_TRACE_ERROR and ASYN_TRACEIO_DRIVER
https://github-wiki-see.page/m/ISISComputingGroup/ibex_developers_manual/wiki/ASYN-Trace-Masks-(Debugging-IOC,-ASYN)
Here, we are unable to connect to the controller, which is a runtime error -> ASYN_TRACE_ERROR.
However, since lowLevelPortUser_ might still be NULL (because
pasynOctetSyncIO->connect failed for some reason), we use the alternative
EPICS function errlogPrintf. This function does not provide a timestamp
and masking facilities.
*/
// asynPrint(this->lowLevelPortUser_, ASYN_TRACE_ERROR,
// "%s: cannot connect to C804 controller\n",
// functionName); // Asyn-framework function, needs to be configured by setTraceMasks
errlogPrintf("Fatal error in %s: cannot connect to C804 controller\n", functionName);
exit(-1);
}
/*
Here we define the terminators for messages sent to / received from the
physical device (Eos = End of string).
In the C804 manual, the terminator for an outgoing message is specified as
(rtn) == Carriage Return. From https://en.wikipedia.org/wiki/Escape_sequences_in_C:
(rtn) => \r
An incoming report is terminated by a CRLF ETX (again referring to https://en.wikipedia.org/wiki/Escape_sequences_in_C)
CR => \r
LF (line feed) = \n
ETX (end-of-text, see https://www.asciitable.com/) => \x03
*/
const char *message_to_device = "\r";
const char *message_from_device = "\x03";
pasynOctetSyncIO->setOutputEos(lowLevelPortUser_, message_to_device, strlen(message_to_device)); // Output: from EPICS to device
pasynOctetSyncIO->setInputEos(lowLevelPortUser_, message_from_device, strlen(message_from_device)); // Input: from device to EPICS
/*
See documentation of function in asynMotorController.cpp:
"Starts the motor poller thread.
* Derived classes will typically call this at near the end of their constructor.
[...]
"
The function arguments are:
* movingPollPeriod The time between polls when any axis is moving (in seconds).
* idlePollPeriod The time between polls when no axis is moving (in seconds).
* forcedFastPolls The number of times to force the movingPollPeriod after waking up the poller.
*/
startPoller(movingPollPeriod, idlePollPeriod, 1);
/*
After changing values in the parameter library (e.g. by calls to setIntegerParam),
the PV's need to be updated. This is done explictly by callParamCallbacks()
callParamCallbacks due to the separation asyn - EPICS (param lib vs. driver support)
*/
callParamCallbacks();
}
C804Controller::~C804Controller(void)
{
/*
Cleanup of the memory allocated in this->pAxes_. As discussed in the constructor,
this is not strictly necessary due to the way EPICS works, but it is good
practice anyway to properly clean up resources.
*/
free(this->pAxes_);
}
/*
Access one of the axes of the controller via the axis adress stored in asynUser.
If the axis does not exist or is not a C804Axis, a nullptr is returned and an error is emitted.
*/
C804Axis *C804Controller::getAxis(asynUser *pasynUser)
{
asynMotorAxis *asynAxis = asynMotorController::getAxis(pasynUser);
return C804Controller::castToC804Axis(asynAxis);
}
/*
Access one of the axes of the controller via the axis index.
If the axis does not exist or is not a C804Axis, the function must return Null
*/
C804Axis *C804Controller::getAxis(int axisNo)
{
asynMotorAxis *asynAxis = asynMotorController::getAxis(axisNo);
return C804Controller::castToC804Axis(asynAxis);
}
C804Axis *C804Controller::castToC804Axis(asynMotorAxis *asynAxis)
{
static const char *functionName = "C804Controller::getAxis";
// If the axis slot of the pAxes_ array is empty, a nullptr must be returned
if (asynAxis == nullptr)
{
return nullptr;
}
// Here, an error is emitted since asyn_axis is not a nullptr but also not an instance of C804Axis
C804Axis *axis = dynamic_cast<C804Axis *>(asynAxis);
if (axis == nullptr)
{
asynPrint(lowLevelPortUser_, ASYN_TRACE_ERROR, "%s: Axis %d is not a C804 Axis", functionName, axis->axisNo_);
}
return axis;
}
/*
Sends the given command to the axis specified by axisNo and returns the response
of the axis.
*/
asynStatus C804Controller::lowLevelWriteRead(int axisNo, const char *command, char *response, bool expect_response)
{
// Definition of local variables.
static const char *functionName = "C804Controller::lowLevelWriteRead";
asynStatus status = asynSuccess;
C804Axis *axis = getAxis(axisNo);
if (axis == nullptr)
{
// We already did the error logging directly in getAxis
return asynError;
}
// TBD: Is this interpretation correct?
int eomReason = 0; // Flag indicating why the message has ended
size_t nbytesOut = 0; // Number of bytes of the outgoing message (which is command + the end-of-string terminator defined in the constructor)
size_t nbytesIn = 0; // Number of bytes of the incoming message (which is response + the end-of-string terminator defined in the constructor)
// If the class instance could not be connected to the device, set an error flag.
if (lowLevelPortUser_ == nullptr)
{
asynPrint(this->lowLevelPortUser_, ASYN_TRACE_ERROR,
"%s: not connected to C804 controller\n",
functionName);
// Adjust the parameter library
setIntegerParam(this->motorStatusCommsError_, 1);
return asynError;
}
// Mask ASYN_TRACEIO_DRIVER is defined as "Device support reports I/O activity"
asynPrint(lowLevelPortUser_, ASYN_TRACEIO_DRIVER, "%s: command: %s\n", functionName, command);
// Writes the command to the port and blocks until a response has been received or until the timeout has been reached.
// For some inputs (such as TP = Tell position), we expect a response which is terminated by a character array "x03"
// Other messages such as MA100 (move) don't return a response
if (expect_response)
{
status = pasynOctetSyncIO->writeRead(lowLevelPortUser_,
command, strlen(command),
response, this->C804_MAXBUF_,
C804_TIMEOUT_,
&nbytesOut, &nbytesIn, &eomReason);
}
else
{
status = pasynOctetSyncIO->write(lowLevelPortUser_,
command, strlen(command),
C804_TIMEOUT_,
&nbytesOut);
}
// Writing and/or reading succeded
if (status == asynSuccess)
{
asynPrint(lowLevelPortUser_, ASYN_TRACEIO_DRIVER, "%s: device response: %s\n", functionName, response);
// Reset any error which might have been set
setIntegerParam(this->motorStatusCommsError_, 0);
}
else
{
asynPrint(this->lowLevelPortUser_, ASYN_TRACE_ERROR,
"%s: asynOctetSyncIO->writeRead failed for command %s on axis %d\n",
functionName, command, axisNo);
setIntegerParam(this->motorStatusCommsError_, 1);
}
// Block the thread to avoid sending too many messages in a short timeframe
usleep(interMessageSleep);
return status;
}
/*************************************************************************************/
/** The following functions are C-wrappers, and can be called directly from iocsh */
extern "C"
{
/*
C wrapper for the C804Controller constructor.
*/
asynStatus C804CreateController(const char *portName, const char *lowLevelPortName, int lowLevelPortAddress,
int numAxes, double movingPollPeriod, double idlePollPeriod)
{
/*
We create a new instance of C804CreateController, using the "new" keyword to allocate it
on the heap while avoiding RAII.
TBD: Where is the pointer to the controller stored?
https://github.com/epics-modules/motor/blob/master/motorApp/MotorSrc/asynMotorController.cpp
https://github.com/epics-modules/asyn/blob/master/asyn/asynPortDriver/asynPortDriver.cpp
Setting the pointer to nullptr / NULL immediately after construction is simply
done to avoid compiler warnings, see page 7 of this document:
https://subversion.xray.aps.anl.gov/synApps/measComp/trunk/documentation/measCompTutorial.pdf
*/
C804Controller *pController = new C804Controller(portName, lowLevelPortName, lowLevelPortAddress, numAxes, movingPollPeriod, idlePollPeriod);
pController = nullptr;
return asynSuccess;
}
/*
C wrapper for the C804Axis constructor.
See C804Axis::C804Axis.
*/
asynStatus C804CreateAxis(const char *C804Name, int axis)
{
C804Axis *pAxis;
static const char *functionName = "C804CreateAxis";
/*
findAsynPortDriver is a asyn library FFI function which uses the C ABI.
Therefore it returns a void pointer instead of e.g. a pointer to a superclass
of the controller such as asynPortDriver. Type-safe upcasting via dynamic_cast
is therefore not possible directly. However, we do know that the void
pointer is either a pointer to asynPortDriver (if a driver with the specified name exists)
or a nullptr. Therefore, we first do a nullptr check, then a cast to asynPortDriver
and lastly a (typesafe) dynamic_upcast to C804Controller
https://stackoverflow.com/questions/70906749/is-there-a-safe-way-to-cast-void-to-class-pointer-in-c
*/
void *ptr = findAsynPortDriver(C804Name);
if (ptr == nullptr)
{
/*
We can't use asynPrint here since this macro would require us
to get a lowLevelPortUser_ from a pointer to an asynPortDriver.
However, the given pointer is a nullptr and therefore doesn't
have a lowLevelPortUser_! printf is an EPICS alternative which
works w/o that, but doesn't offer the comfort provided
by the asynTrace-facility
*/
printf("%s:%s: Error port %s not found\n", driverName, functionName, C804Name);
return asynError;
}
// Unsafe cast of the pointer to an asynPortDriver
asynPortDriver *apd = (asynPortDriver *)(ptr);
// Safe downcast
C804Controller *pC = dynamic_cast<C804Controller *>(apd);
if (pC == nullptr)
{
printf("%s: controller on port %s is not a C804Controller\n", functionName, C804Name);
return asynError;
}
// Prevent manipulation of the controller from other threads while we create the new axis.
pC->lock();
/*
We create a new instance of C804Axis, using the "new" keyword to allocate it
on the heap while avoiding RAII. In the constructor, a pointer to the new object is stored in
the controller object "pC". Therefore, the axis instance can still be
reached later by quering "pC".
Setting the pointer to nullptr / NULL immediately after construction is simply
done to avoid compiler warnings, see page 7 of this document:
https://subversion.xray.aps.anl.gov/synApps/measComp/trunk/documentation/measCompTutorial.pdf
*/
pAxis = new C804Axis(pC, axis);
pAxis = nullptr;
// Allow manipulation of the controller again
pC->unlock();
return asynSuccess;
}
/*
This is boilerplate code which is used to make the FFI functions
C804CreateController and C804CreateAxis "known" to the IOC shell (iocsh).
TBD: If the code is compiled for running on vxWorks, this registration is
apparently not necessary?
*/
#ifdef vxWorks
#else
/*
Define name and type of the arguments for the C804CreateController function
in the iocsh. This is done by creating structs with the argument names and types
and then providing "factory" functions (configC804CreateControllerCallFunc).
These factory functions are used to register the constructors during compilation.
*/
static const iocshArg C804CreateControllerArg0 = {"Controller port name", iocshArgString};
static const iocshArg C804CreateControllerArg1 = {"Low level port name", iocshArgString};
static const iocshArg C804CreateControllerArg2 = {"Low level port address", iocshArgInt};
static const iocshArg C804CreateControllerArg3 = {"Number of axes", iocshArgInt};
static const iocshArg C804CreateControllerArg4 = {"Moving poll rate (s)", iocshArgDouble};
static const iocshArg C804CreateControllerArg5 = {"Idle poll rate (s)", iocshArgDouble};
static const iocshArg *const C804CreateControllerArgs[] = {&C804CreateControllerArg0,
&C804CreateControllerArg1,
&C804CreateControllerArg2,
&C804CreateControllerArg3,
&C804CreateControllerArg4,
&C804CreateControllerArg5};
static const iocshFuncDef configC804CreateController = {"C804CreateController", 6, C804CreateControllerArgs};
static void configC804CreateControllerCallFunc(const iocshArgBuf *args)
{
C804CreateController(args[0].sval, args[1].sval, args[2].ival, args[3].ival, args[4].dval, args[5].dval);
}
/*
Same procedure as for the C804CreateController function, but for the axis itself.
*/
static const iocshArg C804CreateAxisArg0 = {"Controller port name", iocshArgString};
static const iocshArg C804CreateAxisArg1 = {"Axis number", iocshArgInt};
static const iocshArg *const C804CreateAxisArgs[] = {&C804CreateAxisArg0,
&C804CreateAxisArg1};
static const iocshFuncDef configC804CreateAxis = {"C804CreateAxis", 2, C804CreateAxisArgs};
static void configC804CreateAxisCallFunc(const iocshArgBuf *args)
{
C804CreateAxis(args[0].sval, args[1].ival);
}
// This function is made known to EPICS in sinq.dbd and is called by EPICS
// in order to register both functions in the IOC shell
// TBD: Does this happen during compilation?
static void C804ControllerRegister(void)
{
iocshRegister(&configC804CreateController, configC804CreateControllerCallFunc);
iocshRegister(&configC804CreateAxis, configC804CreateAxisCallFunc);
}
epicsExportRegistrar(C804ControllerRegister);
#endif
} // extern "C"

View File

@ -0,0 +1,50 @@
#ifndef C804Controller_H
#define C804Controller_H
#include "SINQController.h"
#include "C804Axis.h"
#include "asynMotorAxis.h"
class C804Controller : public SINQController
{
public:
C804Controller(const char *portName, const char *lowLevelPortName, int lowLevelPortAddress, int numAxes, double movingPollPeriod,
double idlePollPeriod, const int &extraParams = 2);
virtual ~C804Controller();
/* These are the methods that we override */
C804Axis *getAxis(asynUser *pasynUser);
C804Axis *getAxis(int axisNo);
C804Axis *castToC804Axis(asynMotorAxis *asynAxis);
protected:
asynUser *lowLevelPortUser_;
// User-defined polling periods in ms
time_t movingPollPeriod_;
time_t idlePollPeriod_;
void log(const char *message);
asynStatus lowLevelWriteRead(int axisNo, const char *command, char *response, bool expect_response);
private:
// Set the maximum buffer size. This is an empirical value which must be large
// enough to avoid overflows for all commands to the device / responses from it.
static const uint32_t C804_MAXBUF_ = 200;
/*
When trying to communicate with the device, the underlying asynOctetSyncIO
interface waits for a response until this time (in seconds) has passed,
then it declares a timeout. This variable has to be specified in the .cpp-file.
Tying to specify in the .h-file results in the following compiler error:
In file included from ../sinqEPICSApp/src/C804Axis.cpp:2:
../sinqEPICSApp/src/C804Controller.h:38:23: error: constexpr needed for in-class initialization
of static data member const double C804Controller::C804_TIMEOUT_ of non-integral type
*/
static const double C804_TIMEOUT_;
friend class C804Axis;
};
#endif

View File

@ -11,7 +11,6 @@ Mark Koennecke, May, August 2017
*/
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
@ -30,84 +29,82 @@ Mark Koennecke, May, August 2017
#define IDLEPOLL 60
/** Creates a new EL734Controller object.
* \param[in] portName The name of the asyn port that will be created for this driver
* \param[in] EL734PortName The name of the drvAsynSerialPort that was created previously to connect to the EL734 controller
* \param[in] numAxes The number of axes that this controller supports
*/
* \param[in] portName The name of the asyn port that will be created for this driver
* \param[in] EL734PortName The name of the drvAsynSerialPort that was created previously to connect to the EL734 controller
* \param[in] numAxes The number of axes that this controller supports
*/
EL734Controller::EL734Controller(const char *portName, const char *EL734PortName, int numAxes)
: SINQController(portName, EL734PortName, numAxes)
: SINQController(portName, EL734PortName, numAxes)
{
int axis;
asynStatus status;
static const char *functionName = "EL734Controller::EL734Controller";
/* Connect to EL734 controller */
status = pasynOctetSyncIO->connect(EL734PortName, 0, &pasynUserController_, NULL);
if (status) {
asynPrint(this->pasynUserSelf, ASYN_TRACE_ERROR,
"%s: cannot connect to EL734 controller\n",
functionName);
if (status)
{
asynPrint(this->pasynUserSelf, ASYN_TRACE_ERROR,
"%s: cannot connect to EL734 controller\n",
functionName);
}
pasynOctetSyncIO->setOutputEos(pasynUserController_,"\r",strlen("\r"));
pasynOctetSyncIO->setInputEos(pasynUserController_,"\r",strlen("\r"));
pasynOctetSyncIO->setOutputEos(pasynUserController_, "\r", strlen("\r"));
pasynOctetSyncIO->setInputEos(pasynUserController_, "\r", strlen("\r"));
switchRemote();
for (axis=0; axis<numAxes; axis++) {
new EL734Axis(this, axis+1);
for (axis = 0; axis < numAxes; axis++)
{
new EL734Axis(this, axis + 1);
}
startPoller(1000./1000., IDLEPOLL, 2);
startPoller(1000. / 1000., IDLEPOLL, 2);
}
/** Creates a new EL734Controller object.
* Configuration command, called directly or from iocsh
* \param[in] portName The name of the asyn port that will be created for this driver
* \param[in] EL734PortName The name of the drvAsynIPPPort that was created previously to connect to the EL734 controller
* \param[in] numAxes The number of axes that this controller supports
*/
* Configuration command, called directly or from iocsh
* \param[in] portName The name of the asyn port that will be created for this driver
* \param[in] EL734PortName The name of the drvAsynIPPPort that was created previously to connect to the EL734 controller
* \param[in] numAxes The number of axes that this controller supports
*/
extern "C" int EL734CreateController(const char *portName, const char *EL734PortName, int numAxes)
{
EL734Controller *pEL734Controller
= new EL734Controller(portName, EL734PortName, numAxes);
EL734Controller *pEL734Controller = new EL734Controller(portName, EL734PortName, numAxes);
pEL734Controller = NULL;
return(asynSuccess);
return (asynSuccess);
}
/** Reports on status of the driver
* \param[in] fp The file pointer on which report information will be written
* \param[in] level The level of report detail desired
*
* If details > 0 then information is printed about each axis.
* After printing controller-specific information it calls asynMotorController::report()
*/
* \param[in] fp The file pointer on which report information will be written
* \param[in] level The level of report detail desired
*
* If details > 0 then information is printed about each axis.
* After printing controller-specific information it calls asynMotorController::report()
*/
void EL734Controller::report(FILE *fp, int level)
{
fprintf(fp, "EL734 motor driver %s, numAxes=%d\n",
this->portName, numAxes_);
fprintf(fp, "EL734 motor driver %s, numAxes=%d\n",
this->portName, numAxes_);
// Call the base class method
asynMotorController::report(fp, level);
}
/** Returns a pointer to an EL734Axis object.
* Returns NULL if the axis number encoded in pasynUser is invalid.
* \param[in] pasynUser asynUser structure that encodes the axis index number. */
EL734Axis* EL734Controller::getAxis(asynUser *pasynUser)
* Returns NULL if the axis number encoded in pasynUser is invalid.
* \param[in] pasynUser asynUser structure that encodes the axis index number. */
EL734Axis *EL734Controller::getAxis(asynUser *pasynUser)
{
return static_cast<EL734Axis*>(asynMotorController::getAxis(pasynUser));
return static_cast<EL734Axis *>(asynMotorController::getAxis(pasynUser));
}
/** Returns a pointer to an EL734Axis object.
* Returns NULL if the axis number encoded in pasynUser is invalid.
* \param[in] axisNo Axis index number. */
EL734Axis* EL734Controller::getAxis(int axisNo)
* Returns NULL if the axis number encoded in pasynUser is invalid.
* \param[in] axisNo Axis index number. */
EL734Axis *EL734Controller::getAxis(int axisNo)
{
return static_cast<EL734Axis*>(asynMotorController::getAxis(axisNo));
return static_cast<EL734Axis *>(asynMotorController::getAxis(axisNo));
}
void EL734Controller::switchRemote()
@ -116,27 +113,27 @@ void EL734Controller::switchRemote()
size_t in, out;
int reason;
strcpy(command,"RMT 1");
strcpy(command, "RMT 1");
pasynOctetSyncIO->writeRead(pasynUserController_, command, strlen(command),
reply,COMLEN, 1.,&out,&in,&reason);
strcpy(command,"ECHO 0");
reply, COMLEN, 1., &out, &in, &reason);
strcpy(command, "ECHO 0");
pasynOctetSyncIO->writeRead(pasynUserController_, command, strlen(command),
reply,COMLEN, 1.,&out,&in,&reason);
strcpy(command,"RMT 1");
reply, COMLEN, 1., &out, &in, &reason);
strcpy(command, "RMT 1");
pasynOctetSyncIO->writeRead(pasynUserController_, command, strlen(command),
reply,COMLEN, 1.,&out,&in,&reason);
strcpy(command,"ECHO 0");
reply, COMLEN, 1., &out, &in, &reason);
strcpy(command, "ECHO 0");
pasynOctetSyncIO->writeRead(pasynUserController_, command, strlen(command),
reply,COMLEN, 1.,&out,&in,&reason);
reply, COMLEN, 1., &out, &in, &reason);
}
/**
* send a command to the EL734 and read the reply. Do some error and controller
* send a command to the EL734 and read the reply. Do some error and controller
* issue fixing on the way
* \param[in] command The command to send
* \param[out] reply The controllers reply
*/
asynStatus EL734Controller::transactController(int axisNo,char command[COMLEN], char reply[COMLEN])
asynStatus EL734Controller::transactController(int axisNo, char command[COMLEN], char reply[COMLEN])
{
asynStatus status;
size_t in, out, i;
@ -145,58 +142,76 @@ asynStatus EL734Controller::transactController(int axisNo,char command[COMLEN],
SINQAxis *axis = getAxis(axisNo);
pasynOctetSyncIO->flush(pasynUserController_);
if (axis != NULL)
{
axis->updateMsgTxtFromDriver("");
}
status = pasynOctetSyncIO->writeRead(pasynUserController_, command, strlen(command),
reply,COMLEN, 2.,&out,&in,&reason);
if(status != asynSuccess){
if(axis!= NULL){
reply, COMLEN, 2., &out, &in, &reason);
if (status != asynSuccess)
{
if (axis != NULL)
{
axis->updateMsgTxtFromDriver("Lost connection to motor controller");
}
return status;
}
/*
/*
check for the offline reply
*/
if(strstr(reply,"?LOC") != NULL){
if (strstr(reply, "?LOC") != NULL)
{
switchRemote();
return pasynOctetSyncIO->writeRead(pasynUserController_, command, strlen(command),
reply,COMLEN, 1.,&out,&in,&reason);
reply, COMLEN, 1., &out, &in, &reason);
}
/*
check for echos. This means that the thing is offline.
*/
if(strstr(reply,"p ") != NULL || strstr(reply,"u ") != NULL || strstr(reply,"msr ") != NULL){
if (strstr(reply, "p ") != NULL || strstr(reply, "u ") != NULL || strstr(reply, "msr ") != NULL)
{
switchRemote();
return pasynOctetSyncIO->writeRead(pasynUserController_, command, strlen(command),
reply,COMLEN, 1.,&out,&in,&reason);
reply, COMLEN, 1., &out, &in, &reason);
}
/*
check for EL734 errors
*/
strcpy(myReply, reply);
for(i = 0; i < strlen(reply); i++){
for (i = 0; i < strlen(reply); i++)
{
myReply[i] = (char)tolower((int)reply[i]);
}
if(strstr(myReply,"?cmd") != NULL){
snprintf(errTxt,sizeof(errTxt), "Bad command %s at axis %d", command, axisNo);
errlogSevPrintf(errlogMajor, errTxt);
if(axis!= NULL){
if (strstr(myReply, "?cmd") != NULL)
{
snprintf(errTxt, sizeof(errTxt), "Bad command %s at axis %d", command, axisNo);
asynPrint(this->pasynUserSelf, ASYN_TRACE_ERROR, errTxt);
if (axis != NULL)
{
axis->updateMsgTxtFromDriver(errTxt);
}
return asynError;
} else if(strstr(myReply,"?par") != NULL){
snprintf(errTxt,sizeof(errTxt), "Bad parameter in command %s", command);
errlogSevPrintf(errlogMajor, errTxt);
if(axis!= NULL){
}
else if (strstr(myReply, "?par") != NULL)
{
snprintf(errTxt, sizeof(errTxt), "Bad parameter in command %s", command);
asynPrint(this->pasynUserSelf, ASYN_TRACE_ERROR, errTxt);
if (axis != NULL)
{
axis->updateMsgTxtFromDriver(errTxt);
}
return asynError;
} else if(strstr(myReply,"?rng") != NULL){
snprintf(errTxt,sizeof(errTxt), "Parameter out of range in command %s", command);
errlogSevPrintf(errlogMajor, errTxt);
if(axis!= NULL){
}
else if (strstr(myReply, "?rng") != NULL)
{
snprintf(errTxt, sizeof(errTxt), "Parameter out of range in command %s", command);
asynPrint(this->pasynUserSelf, ASYN_TRACE_ERROR, errTxt);
if (axis != NULL)
{
axis->updateMsgTxtFromDriver(errTxt);
}
return asynError;
@ -208,14 +223,14 @@ asynStatus EL734Controller::transactController(int axisNo,char command[COMLEN],
// These are the EL734Axis methods
/** Creates a new EL734Axis object.
* \param[in] pC Pointer to the EL734Controller to which this axis belongs.
* \param[in] axisNo Index number of this axis, range 0 to pC->numAxes_-1.
*
* Initializes register numbers, etc.
*/
* \param[in] pC Pointer to the EL734Controller to which this axis belongs.
* \param[in] axisNo Index number of this axis, range 0 to pC->numAxes_-1.
*
* Initializes register numbers, etc.
*/
EL734Axis::EL734Axis(EL734Controller *pC, int axisNo)
: SINQAxis(pC, axisNo), pC_(pC)
{
: SINQAxis(pC, axisNo), pC_(pC)
{
char command[COMLEN], reply[COMLEN];
asynStatus status;
int count = 0;
@ -224,58 +239,81 @@ EL734Axis::EL734Axis(EL734Controller *pC, int axisNo)
/*
get the hardware limits from the controller
*/
sprintf(command,"H %d",axisNo_);
status = pC_->transactController(axisNo_,command,reply);
if(status == asynSuccess){
count = sscanf(reply,"%f %f",&low, &high);
if(count >= 2){
pC_->setDoubleParam(axisNo_,pC_->motorLowLimit_,low);
pC_->setDoubleParam(axisNo_,pC_->motorHighLimit_,high);
} else {
errlogPrintf("Bad response - %s - requesting limits at axis %d", reply, axisNo_);
sprintf(command, "H %d", axisNo_);
status = pC_->transactController(axisNo_, command, reply);
if (status == asynSuccess)
{
count = sscanf(reply, "%f %f", &low, &high);
if (count >= 2)
{
pC_->setDoubleParam(axisNo_, pC_->motorLowLimit_, low);
pC_->setDoubleParam(axisNo_, pC_->motorHighLimit_, high);
callParamCallbacks();
}
else
{
asynPrint(pC_->pasynUserSelf, ASYN_TRACE_ERROR,
"Bad response - %s - requesting limits at axis %d", reply, axisNo_);
}
} else {
errlogPrintf("Failed to read limits at axis %d", axisNo_);
}
else
{
asynPrint(pC_->pasynUserSelf, ASYN_TRACE_ERROR,
"Failed to read limits at axis %d", axisNo_);
}
/*
oredMSR contains the axis status from the previous poll. The initial value
1 means that the axis is not moving and has no errors.
*/
oredMSR = 1;
next_poll = -1;
}
/** Reports on status of the axis
* \param[in] fp The file pointer on which report information will be written
* \param[in] level The level of report detail desired
*
* After printing device-specific information calls asynMotorAxis::report()
*/
* \param[in] fp The file pointer on which report information will be written
* \param[in] level The level of report detail desired
*
* After printing device-specific information calls asynMotorAxis::report()
*/
void EL734Axis::report(FILE *fp, int level)
{
if (level > 0) {
if (level > 0)
{
fprintf(fp, " axis %d\n",
axisNo_);
}
// Call the base class method
//asynMotorAxis::report(fp, level);
// asynMotorAxis::report(fp, level);
}
asynStatus EL734Axis::move(double position, int relative, double minVelocity, double maxVelocity, double acceleration)
{
asynStatus status;
//static const char *functionName = "EL734Axis::move";
// static const char *functionName = "EL734Axis::move";
char command[COMLEN], reply[COMLEN];
// status = sendAccelAndVelocity(acceleration, maxVelocity);
if (relative) {
errlogPrintf("minVelocity = %f, maxVelocity = %f\n", minVelocity, maxVelocity);
/*
* set speed
*/
sprintf(command, "J %d %d", axisNo_, (int)maxVelocity);
status = pC_->transactController(axisNo_, command, reply);
if (relative)
{
position += this->position;
}
oredMSR = 0;
homing = 0;
errorReported = 0;
errlogPrintf("Starting axis %d with destination %f", axisNo_,position/1000);
sprintf(command, "p %d %.3f", axisNo_, position/1000.);
status = pC_->transactController(axisNo_,command,reply);
asynPrint(pC_->pasynUserSelf, ASYN_TRACE_ERROR,
"Starting axis %d with destination %f", axisNo_, position / 1000);
sprintf(command, "p %d %.3f", axisNo_, position / 1000.);
status = pC_->transactController(axisNo_, command, reply);
setIntegerParam(pC_->motorStatusProblem_, false);
updateMsgTxtFromDriver("");
next_poll = -1;
@ -285,7 +323,7 @@ asynStatus EL734Axis::move(double position, int relative, double minVelocity, do
asynStatus EL734Axis::home(double minVelocity, double maxVelocity, double acceleration, int forwards)
{
asynStatus status;
//static const char *functionName = "EL734Axis::home";
// static const char *functionName = "EL734Axis::home";
char command[COMLEN], reply[COMLEN];
// status = sendAccelAndVelocity(acceleration, maxVelocity);
@ -296,49 +334,55 @@ asynStatus EL734Axis::home(double minVelocity, double maxVelocity, double accele
sprintf(command, "R %d", axisNo_);
homing = 1;
next_poll= -1;
status = pC_->transactController(axisNo_,command,reply);
next_poll = -1;
status = pC_->transactController(axisNo_, command, reply);
return status;
}
asynStatus EL734Axis::moveVelocity(double minVelocity, double maxVelocity, double acceleration)
{
asynStatus status;
//static const char *functionName = "EL734Axis::moveVelocity";
// static const char *functionName = "EL734Axis::moveVelocity";
char command[COMLEN], reply[COMLEN];
// asynPrint(pasynUser_, ASYN_TRACE_FLOW,
// "%s: minVelocity=%f, maxVelocity=%f, acceleration=%f\n",
// functionName, minVelocity, maxVelocity, acceleration);
errorReported = 0;
if (maxVelocity > 0.) {
if (maxVelocity > 0.)
{
/* This is a positive move */
sprintf(command, "FF %d", axisNo_);
} else {
/* This is a negative move */
sprintf(command, "FB %d", axisNo_);
}
status = pC_->transactController(axisNo_,command,reply);
else
{
/* This is a negative move */
sprintf(command, "FB %d", axisNo_);
}
status = pC_->transactController(axisNo_, command, reply);
setIntegerParam(pC_->motorStatusProblem_, false);
updateMsgTxtFromDriver("");
next_poll = -1;
return status;
}
asynStatus EL734Axis::stop(double acceleration )
asynStatus EL734Axis::stop(double acceleration)
{
asynStatus status = asynSuccess;
//static const char *functionName = "EL734Axis::stop";
// static const char *functionName = "EL734Axis::stop";
char command[COMLEN], reply[COMLEN];
bool moving = false;
if(errorReported == 0){
sprintf(command, "S %d", axisNo_);
status = pC_->transactController(axisNo_,command,reply);
errlogPrintf("Sent STOP on Axis %d\n", axisNo_);
updateMsgTxtFromDriver("Axis interrupted");
}
this->poll(&moving);
if (moving && errorReported == 0)
{
sprintf(command, "S %d", axisNo_);
status = pC_->transactController(axisNo_, command, reply);
asynPrint(pC_->pasynUserSelf, ASYN_TRACE_ERROR, "Sent STOP on Axis %d\n", axisNo_);
updateMsgTxtFromDriver("Axis interrupted");
errorReported = 1;
}
return status;
}
@ -346,11 +390,11 @@ asynStatus EL734Axis::stop(double acceleration )
asynStatus EL734Axis::setPosition(double position)
{
asynStatus status;
//static const char *functionName = "EL734Axis::setPosition";
// static const char *functionName = "EL734Axis::setPosition";
char command[COMLEN], reply[COMLEN];
sprintf(command, "U %d %f", axisNo_, position/1000.);
status = pC_->transactController(axisNo_,command,reply);
sprintf(command, "U %d %f", axisNo_, position / 1000.);
status = pC_->transactController(axisNo_, command, reply);
next_poll = -1;
return status;
@ -358,8 +402,8 @@ asynStatus EL734Axis::setPosition(double position)
asynStatus EL734Axis::setClosedLoop(bool closedLoop)
{
//static const char *functionName = "EL734Axis::setClosedLoop";
// static const char *functionName = "EL734Axis::setClosedLoop";
/*
This belongs into the Kingdom of Electronics.
We do not do this.
@ -369,32 +413,50 @@ asynStatus EL734Axis::setClosedLoop(bool closedLoop)
}
/** Polls the axis.
* This function reads the motor position, the limit status, the home status, the moving status,
* and the drive power-on status.
* It calls setIntegerParam() and setDoubleParam() for each item that it polls,
* and then calls callParamCallbacks() at the end.
* \param[out] moving A flag that is set indicating that the axis is moving (true) or done (false). */
* This function reads the motor position, the limit status, the home status, the moving status,
* and the drive power-on status.
* It calls setIntegerParam() and setDoubleParam() for each item that it polls,
* and then calls callParamCallbacks() at the end.
* \param[out] moving A flag that is set indicating that the axis is moving (true) or done (false). */
asynStatus EL734Axis::poll(bool *moving)
{
{
int msr, count;
float low, high;
asynStatus comStatus = asynSuccess;
char command[COMLEN], reply[COMLEN], errTxt[256];
// protect against excessive polling
if(time(NULL) < next_poll){
if (time(NULL) < next_poll)
{
*moving = false;
return asynSuccess;
}
// read hardware limits
sprintf(command, "H %d", axisNo_);
comStatus = pC_->transactController(axisNo_, command, reply);
if (comStatus == asynSuccess)
{
count = sscanf(reply, "%f %f", &low, &high);
if (count >= 2)
{
pC_->setDoubleParam(axisNo_, pC_->motorLowLimit_, low);
pC_->setDoubleParam(axisNo_, pC_->motorHighLimit_, high);
callParamCallbacks();
}
}
// Read the current motor position
setIntegerParam(pC_->motorStatusProblem_,false);
sprintf(command,"u %d", axisNo_);
comStatus = pC_->transactController(axisNo_,command,reply);
if(comStatus == asynError){
setIntegerParam(pC_->motorStatusProblem_,true);
setIntegerParam(pC_->motorStatusProblem_, false);
sprintf(command, "u %d", axisNo_);
comStatus = pC_->transactController(axisNo_, command, reply);
if (comStatus == asynError)
{
setIntegerParam(pC_->motorStatusProblem_, true);
goto skip;
}
if(strstr(reply,"*ES") != NULL){
if (strstr(reply, "*ES") != NULL)
{
*moving = false;
setIntegerParam(pC_->motorStatusDone_, true);
setIntegerParam(pC_->motorStatusProblem_, true);
@ -402,104 +464,126 @@ asynStatus EL734Axis::poll(bool *moving)
updateMsgTxtFromDriver("Emergency Stop Engaged");
comStatus = asynError;
goto skip;
} else if(strstr(reply,"?BSY") != NULL){
}
else if (strstr(reply, "?BSY") != NULL)
{
*moving = true;
setIntegerParam(pC_->motorStatusDone_, false);
goto skip;
}
count = sscanf(reply,"%lf", &position);
if(count != 1) {
if(!homing) {
snprintf(errTxt,sizeof(errTxt),"Bad reply %s when reading position for %d", reply, axisNo_);
count = sscanf(reply, "%lf", &position);
if (count != 1)
{
if (!homing)
{
snprintf(errTxt, sizeof(errTxt), "Bad reply %s when reading position for %d", reply, axisNo_);
setIntegerParam(pC_->motorStatusProblem_, true);
errorReported = 1;
updateMsgTxtFromDriver(errTxt);
comStatus = asynError;
goto skip;
}
} else {
errlogPrintf("Axis %d, reply %s, position %lf\n", axisNo_, reply, position);
setDoubleParam(pC_->motorPosition_, position*1000);
//setDoubleParam(pC_->motorEncoderPosition_, position);
}
else
{
asynPrint(pC_->pasynUserSelf, ASYN_TRACE_ERROR,
"Axis %d, reply %s, position %lf\n", axisNo_, reply, position);
setDoubleParam(pC_->motorPosition_, position * 1000);
// setDoubleParam(pC_->motorEncoderPosition_, position);
}
// Read the moving status of this motor
sprintf(command,"msr %d",axisNo_);
comStatus = pC_->transactController(axisNo_,command,reply);
if(comStatus == asynError){
setIntegerParam(pC_->motorStatusProblem_,true);
sprintf(command, "msr %d", axisNo_);
comStatus = pC_->transactController(axisNo_, command, reply);
if (comStatus == asynError)
{
setIntegerParam(pC_->motorStatusProblem_, true);
goto skip;
}
sscanf(reply,"%x",&msr);
sscanf(reply, "%x", &msr);
//errlogPrintf("Axis %d, reply %s, msr %d, oredmsr = %d, position = %lf\n",
// errlogPrintf("Axis %d, reply %s, msr %d, oredmsr = %d, position = %lf\n",
// axisNo_, reply, msr, oredMSR, position);
oredMSR |= msr;
if( (msr & 0x1) == 0){
if ((msr & 0x1) == 0)
{
// done: check for trouble
//errlogPrintf("Axis %d finished\n", axisNo_);
// errlogPrintf("Axis %d finished\n", axisNo_);
*moving = false;
setIntegerParam(pC_->motorStatusDone_, true);
next_poll = time(NULL)+IDLEPOLL;
if(oredMSR & 0x10){
next_poll = time(NULL) + IDLEPOLL;
if (oredMSR & 0x10)
{
setIntegerParam(pC_->motorStatusLowLimit_, true);
updateMsgTxtFromDriver("Lower Limit Hit");
errorReported = 1;
comStatus = asynError;
goto skip;
} else {
}
else
{
setIntegerParam(pC_->motorStatusLowLimit_, false);
}
if(oredMSR & 0x20){
if (oredMSR & 0x20)
{
setIntegerParam(pC_->motorStatusHighLimit_, true);
updateMsgTxtFromDriver("Upper Limit Hit");
errorReported = 1;
comStatus = asynError;
goto skip;
} else {
}
else
{
setIntegerParam(pC_->motorStatusHighLimit_, false);
}
if(homing){
if (homing)
{
setIntegerParam(pC_->motorStatusAtHome_, true);
}
if(oredMSR & 0x1000){
if (oredMSR & 0x1000)
{
setIntegerParam(pC_->motorStatusProblem_, true);
// errlogPrintf("Detected air cushion error on %d", axisNo_);
errlogSevPrintf(errlogMajor, "Air cushion problem on %d", axisNo_);
asynPrint(pC_->pasynUserSelf, ASYN_TRACE_ERROR, "Air cushion problem on %d", axisNo_);
updateMsgTxtFromDriver("Air cushion error");
errorReported = 1;
comStatus = asynError;
goto skip;
}
if(oredMSR & 0x100){
if (oredMSR & 0x100)
{
setIntegerParam(pC_->motorStatusProblem_, true);
errlogSevPrintf(errlogMajor, "Run failure at %d", axisNo_);
asynPrint(pC_->pasynUserSelf, ASYN_TRACE_ERROR, "Run failure at %d", axisNo_);
updateMsgTxtFromDriver("Run failure");
comStatus = asynError;
errorReported = 1;
goto skip;
}
if(oredMSR & 0x400){
if (oredMSR & 0x400)
{
setIntegerParam(pC_->motorStatusProblem_, true);
errlogSevPrintf(errlogMajor, "Positioning failure at %d", axisNo_);
asynPrint(pC_->pasynUserSelf, ASYN_TRACE_ERROR, "Positioning failure at %d", axisNo_);
updateMsgTxtFromDriver("Positioning failure");
comStatus = asynError;
errorReported = 1;
goto skip;
}
if(oredMSR & 0x200 || oredMSR & 0x80) {
errlogSevPrintf(errlogMinor, "Positioning fault at %d", axisNo_);
}
if (oredMSR & 0x200 || oredMSR & 0x80)
{
asynPrint(pC_->pasynUserSelf, ASYN_TRACE_WARNING, "Positioning fault at %d", axisNo_);
}
setIntegerParam(pC_->motorStatusProblem_, false);
} else {
}
else
{
*moving = true;
next_poll = -1;
setIntegerParam(pC_->motorStatusDone_, false);
}
}
skip:
skip:
callParamCallbacks();
return comStatus;
}
@ -508,9 +592,9 @@ asynStatus EL734Axis::poll(bool *moving)
static const iocshArg EL734CreateControllerArg0 = {"Port name", iocshArgString};
static const iocshArg EL734CreateControllerArg1 = {"EL734 port name", iocshArgString};
static const iocshArg EL734CreateControllerArg2 = {"Number of axes", iocshArgInt};
static const iocshArg * const EL734CreateControllerArgs[] = {&EL734CreateControllerArg0,
&EL734CreateControllerArg1,
&EL734CreateControllerArg2};
static const iocshArg *const EL734CreateControllerArgs[] = {&EL734CreateControllerArg0,
&EL734CreateControllerArg1,
&EL734CreateControllerArg2};
static const iocshFuncDef EL734CreateControllerDef = {"EL734CreateController", 3, EL734CreateControllerArgs};
static void EL734CreateContollerCallFunc(const iocshArgBuf *args)
{
@ -522,6 +606,7 @@ static void EL734Register(void)
iocshRegister(&EL734CreateControllerDef, EL734CreateContollerCallFunc);
}
extern "C" {
epicsExportRegistrar(EL734Register);
extern "C"
{
epicsExportRegistrar(EL734Register);
}

View File

@ -28,31 +28,32 @@ public:
asynStatus setClosedLoop(bool closedLoop);
private:
EL734Controller *pC_; /**< Pointer to the asynMotorController to which this axis belongs.
* Abbreviated because it is used very frequently */
EL734Controller *pC_; /**< Pointer to the asynMotorController to which this axis belongs.
* Abbreviated because it is used very frequently */
int oredMSR;
double position;
int homing;
time_t next_poll;
int errorReported;
friend class EL734Controller;
friend class EL734Controller;
};
class EL734Controller : public SINQController {
class EL734Controller : public SINQController
{
public:
EL734Controller(const char *portName, const char *EL734PortName, int numAxes);
void report(FILE *fp, int level);
EL734Axis* getAxis(asynUser *pasynUser);
EL734Axis* getAxis(int axisNo);
EL734Axis *getAxis(asynUser *pasynUser);
EL734Axis *getAxis(int axisNo);
friend class EL734Axis;
private:
asynUser *pasynUserController_;
friend class EL734Axis;
asynStatus transactController(int axis, char command[COMLEN], char reply[COMLEN]);
private:
asynUser *pasynUserController_;
void switchRemote();
asynStatus transactController(int axis, char command[COMLEN], char reply[COMLEN]);
void switchRemote();
};

View File

@ -279,6 +279,7 @@ EuroMoveAxis::EuroMoveAxis(EuroMoveController *pC, int axisNo)
pC_(pC)
{
motNo = axisNo;
next_poll = -1;
}

View File

@ -0,0 +1,954 @@
/*
Driver for the MasterMACS motor controller used at SINQ
For documentation see the standard SINQ place for hardware documentation or
Marcel Schildt
Mark Koennecke, March-August 2023
Mark Koenencke, More fixes in June 2024
*/
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <math.h>
#include <ctype.h>
#include <time.h>
#include <sys/time.h>
#include <unistd.h>
#include <iocsh.h>
#include <epicsThread.h>
#include <errlog.h>
#include <asynOctetSyncIO.h>
#include "MasterMACSDriver.h"
#include <epicsExport.h>
#define CHECK_BIT(var,pos) ((var) & (1 << pos))
#define ABS(x) (x < 0 ? -(x) : (x))
#define debug 0
#define timeDebug 0
double DoubleTime(void)
{
struct timeval now;
/* the resolution of this function is usec, if the machine supports this
and the mantissa of a double is 51 bits or more (31 bits for seconds
and 20 for microseconds)
*/
gettimeofday(&now, NULL);
return now.tv_sec + now.tv_usec / 1e6;
}
/** Creates a new MasterMACSController object.
* \param[in] portName The name of the asyn port that will be created for this driver
* \param[in] MasterMACSPortName The name of the drvAsynSerialPort that was created previously to connect to the MasterMACS controller
* \param[in] numAxes The number of axes that this controller supports
*/
MasterMACSController::MasterMACSController(const char *portName,
const char *MasterMACSPortName,
int numAxes,
int idlePoll, int busyPoll):SINQController
(portName, MasterMACSPortName, numAxes)
{
asynStatus status;
static const char *functionName =
"MasterMACSController::MasterMACSController";
char terminator[2] = "\x03";
/* Connect to MasterMACS controller */
status =
pasynOctetSyncIO->connect(MasterMACSPortName, 0,
&pasynUserController_, NULL);
if (status) {
asynPrint(this->pasynUserSelf, ASYN_TRACE_ERROR,
"%s: cannot connect to MasterMACS controller\n",
functionName);
}
pasynOctetSyncIO->setOutputEos(pasynUserController_, terminator,
strlen(terminator));
pasynOctetSyncIO->setInputEos(pasynUserController_, terminator,
strlen(terminator));
pAxes_ = (MasterMACSAxis **) (asynMotorController::pAxes_);
createParam(EnableAxisString, asynParamInt32, &enableAxis_);
createParam(AxisEnabledString, asynParamInt32, &axisEnabled_);
callParamCallbacks();
startPoller(busyPoll/1000., idlePoll/1000., 1);
setIdlePollPeriod(idlePoll/1000.);
setMovingPollPeriod(busyPoll/1000.);
}
/** Creates a new MasterMACSController object.
* Configuration command, called directly or from iocsh
* \param[in] portName The name of the asyn port that will be created for this driver
* \param[in] MasterMACSPortName The name of the drvAsynIPPPort that was created previously to connect to the MasterMACS controller
* \param[in] numAxes The number of axes that this controller supports
* \param[in] idlePoll Poll interval when idle in microseconds
* \param[in] busyPoll Poll interval when moving in microSeconds
*/
extern "C" int
MasterMACSCreateController(const char *portName,
const char *MasterMACSPortName, int numAxes, int idlePoll, int busyPoll)
{
MasterMACSController *pMasterMACSController
= new MasterMACSController(portName, MasterMACSPortName, numAxes, idlePoll, busyPoll);
pMasterMACSController = NULL;
return (asynSuccess);
}
/** Reports on status of the driver
* \param[in] fp The file pointer on which report information will be written
* \param[in] level The level of report detail desired
*
* If details > 0 then information is printed about each axis.
* After printing controller-specific information it calls asynMotorController::report()
*/
void
MasterMACSController::report(FILE * fp, int level)
{
fprintf(fp, "MasterMACS motor driver %s, numAxes=%d\n",
this->portName, numAxes_);
// Call the base class method
asynMotorController::report(fp, level);
}
/** Returns a pointer to an MasterMACSAxis object.
* Returns NULL if the axis number encoded in pasynUser is invalid.
* \param[in] pasynUser asynUser structure that encodes the axis index number. */
MasterMACSAxis *MasterMACSController::getAxis(asynUser * pasynUser)
{
return static_cast <
MasterMACSAxis * >(asynMotorController::getAxis(pasynUser));
}
/** Returns a pointer to an MasterMACSAxis object.
* Returns NULL if the axis number encoded in pasynUser is invalid.
* \param[in] axisNo Axis index number. */
MasterMACSAxis *MasterMACSController::getAxis(int axisNo)
{
return static_cast <
MasterMACSAxis * >(asynMotorController::getAxis(axisNo));
}
/**
* send a command to the MasterMACS and read the reply. Do some error and controller
* issue fixing on the way
* \param[in] command The command to send
* \param[out] reply The controllers reply
*/
asynStatus
MasterMACSController::transactController(int axisNo,
char command[COMLEN],
char reply[COMLEN])
{
asynStatus status;
size_t in, out;
int reason, len, idx, lenPayload;
unsigned int i;
double startTime, now;
char *mmacsData =
NULL, ackchar, mmacsResponse[COMLEN], hexResponse[256];
SINQAxis *axis = getAxis(axisNo);
pasynOctetSyncIO->flush(pasynUserController_);
/* read with a short timeout in order to remove duplicate messages
* from the line. This also serves to slow down communication
*/
pasynOctetSyncIO->read(pasynUserController_, mmacsResponse, 35, interMessageSleep/1000., &in, &reason);
/* pack data for MasterMACS */
len = strlen(command) + 6;
mmacsData = (char *) malloc(len * sizeof(char));
if (!mmacsData) {
errlogSevPrintf(errlogMajor,
"Failed to allocate memory in MasterMACSController::transactController");
return asynError;
}
mmacsData[0] = 0x05;
mmacsData[1] = (char) (len - 2);
mmacsData[2] = 0;
mmacsData[3] = 0x19;
memcpy(mmacsData + 4, command, strlen(command) * sizeof(char));
mmacsData[len - 2] = 0x0D;
/* 0x03 is appended by asyn */
/* send the stuff away ... */
if(debug) {
errlogSevPrintf(errlogMajor,"Sending command: %s\n", command);
}
startTime = DoubleTime();
status =
pasynOctetSyncIO->writeRead(pasynUserController_, mmacsData,
len - 1, mmacsResponse, 35, 20., &out,
&in, &reason);
if(timeDebug) {
now = DoubleTime();
if((now - startTime) > 1.) {
errlogSevPrintf(errlogMajor, "Unusual long response time %lf to command %s\n", (now - startTime), command);
}
}
if (status != asynSuccess) {
if (axis != NULL) {
errlogSevPrintf(errlogMajor,
"Lost connection to motor controller,a axis %d, reason %d",
axisNo, reason);
axis->updateMsgTxtFromDriver
("Lost connection to motor controller");
return status;
}
errlogSevPrintf(errlogMajor,
"Lost connection to motor controller without axis, reason %d",
reason);
return status;
}
free(mmacsData);
/* format and print the response in hex for debugging purposes */
if(debug) {
for(i = 0, idx = 0; i < in; i++){
sprintf(hexResponse + idx, "%02x ", (unsigned int)mmacsResponse[i]);
idx = strlen(hexResponse);
}
errlogSevPrintf(errlogMajor,"Reply in hex: %s\n", hexResponse);
}
/* Here we have read the data from the MasterMACS. We proceed to extract
* the payload and the state of the ACK byte and place that reply into data
*/
if ((in < 33)) {
errlogSevPrintf(errlogMajor,
"MasterMACS only sent %d bytes, 34 expected",
(int) in);
return asynError;
}
/*
* Unpack the MasterMACS message, start by finding <CR>
*/
for (i = 4, idx = 0; i < 34; i++) {
if (mmacsResponse[i] == 0x0d) {
idx = i;
break;
}
}
//errlogSevPrintf(errlogMajor, "Found <CR> at %d", idx);
/* one character before <CR> is the ACK, if it is there */
ackchar = mmacsResponse[idx - 1];
if (ackchar == 0x06) {
strcpy(reply, "ACK:");
} else if (ackchar == 0x15) {
strcpy(reply, "NAK:");
errlogSevPrintf(errlogMajor,
"MasterMACS responded with NAK to %s\n", command);
status = asynError;
} else {
/* the MasterMACS does not always send a ACK/NAK on read */
strcpy(reply, "NON:");
}
//errlogSevPrintf(errlogMajor, "Reply after testing ACK byte: %s ", reply);
/* copy data */
lenPayload = idx - 4;
memcpy(reply + 4, mmacsResponse + 4, lenPayload);
reply[4 + lenPayload] = (char) 0;
if(debug) {
errlogSevPrintf(errlogMajor, "Completed reply at end of transactController: %s ", reply);
}
return status;
}
/*--------------------------------------------------------------------------------------------*/
asynStatus
MasterMACSController::readInt32(asynUser * pasynUser,
epicsInt32 * value)
{
int function = pasynUser->reason;
MasterMACSAxis *pAxis = NULL;
int devStatus, isOn;
pAxis = (MasterMACSAxis *) (this->getAxis(pasynUser));
if (!pAxis) {
return asynError;
}
if (function == axisEnabled_) {
devStatus = pAxis->readStatus();
isOn = pAxis->isOn(devStatus);
// errlogPrintf("isOn in readInt32: %d, devStatus = %d\n", isOn, devStatus);
pAxis->setIntegerParam(enableAxis_, isOn);
pAxis->setIntegerParam(axisEnabled_, isOn);
*value = isOn;
callParamCallbacks();
return asynSuccess;
}
return asynMotorController::readInt32(pasynUser, value);
}
/*------------------------------------------------------------------------------------------*/
asynStatus
MasterMACSController::writeInt32(asynUser * pasynUser,
epicsInt32 value)
{
int function = pasynUser->reason;
asynStatus status = asynSuccess;
MasterMACSAxis *pAxis = NULL;
char command[64] = { 0 };
char response[64] = { 0 };
int devStatus;
bool moving;
time_t startTime;
pAxis = (MasterMACSAxis *) this->getAxis(pasynUser);
if (!pAxis) {
return asynError;
}
/* Set the parameter and readback in the parameter library. This may be
* overwritten when we read back the status at the end, but that's OK */
pAxis->setIntegerParam(function, value);
if(function == motorStop_){
errlogPrintf("Stop called with value %d\n", value);
double accel;
getDoubleParam(pAxis->axisNo_, motorAccel_, &accel);
status = pAxis->stop(accel);
return status;
}
if (function == enableAxis_) {
/*
* Read the status in order to prevent execssive commands
*/
devStatus = pAxis->readStatus();
if (value == 1 && !pAxis->isOn(devStatus) ) {
/* download parameters, does not work as of now */
/*
sprintf(command, "%dS85=1.", pAxis->axisNo_);
status = transactController(pAxis->axisNo_, command, response);
*/
/* actually enable */
sprintf(command, "%dS04=1", pAxis->axisNo_);
status = transactController(pAxis->axisNo_, command, response);
} else if(pAxis->isOn(devStatus)) {
// only send command when necessary
sprintf(command, "%dS04=0", pAxis->axisNo_);
status = transactController(pAxis->axisNo_, command, response);
} else {
// nothing to do
return status;
}
if (status == asynSuccess) {
pAxis->updateMsgTxtFromDriver("");
} else {
errlogPrintf("MMACS: Failure to enable or disable axis %d",
pAxis->axisNo_);
}
/*
* wait max 2 seconds for success of the operation
*/
startTime = time(NULL);
while(time(NULL) < startTime + 2.){
// wait for the change to happen
devStatus = pAxis->readStatus();
// errlogPrintf("MMACS: switching enable: target, devStatus, isOn: %d, %d, %d\n", value, devStatus, pAxis->isOn(devStatus));
if(pAxis->isOn(devStatus) == value){
pAxis->active = true;
pAxis->poll(&moving); // to update the Enable_RBV field
pAxis->active = false;
return asynSuccess;
}
usleep(200);
}
errlogPrintf("MMACS: Failed to dis/enable motor %d within 2 seconds\n", pAxis->axisNo_);
}
return asynMotorController::writeInt32(pasynUser, value);
}
// These are the MasterMACSAxis methods
/** Creates a new MasterMACSAxis object.
* \param[in] pC Pointer to the MasterMACSController to which this axis belongs.
* \param[in] axisNo Index number of this axis, range 0 to pC->numAxes_-1.
*
* Initializes register numbers, etc.
*/
MasterMACSAxis::MasterMACSAxis(MasterMACSController * pC, int axisNo):SINQAxis(pC, axisNo),
pC_(pC)
{
hasStarted = false;
active = false;
errorCodeFound = 0;
/*
* Try to read the initial enable status of the motor
*/
int devStatus = readStatus();
setIntegerParam(pC_->enableAxis_, isOn(devStatus));
setIntegerParam(pC_->axisEnabled_, isOn(devStatus));
lastPoll = -1;
}
/** Reports on status of the axis
* \param[in] fp The file pointer on which report information will be written
* \param[in] level The level of report detail desired
*
* After printing device-specific information calls asynMotorAxis::report()
*/
void MasterMACSAxis::report(FILE * fp, int level)
{
if (level > 0) {
fprintf(fp, " axis %d\n", axisNo_);
}
}
int MasterMACSAxis::readStatus()
{
char command[COMLEN], reply[COMLEN], *pPtr;
float fval;
int status;
/*
* The MasterMACS sends invalid responses with a low frequency.
* Therefore I send cached status responses in such a case in order
* to help the logic everywhere else in the code.
*/
sprintf(command, "%dR10", axisNo_);
status = pC_->transactController(axisNo_, command, reply);
if (status == asynError) {
return oldStatus;
}
pPtr = strstr(reply, "=");
if(pPtr) {
sscanf(pPtr + 1, "%f", &fval);
oldStatus = (int) fval;
return oldStatus;
} else {
errlogPrintf("MMACS: invalid status reponse %s on axis %d\n", reply, axisNo_);
return oldStatus;
}
}
int MasterMACSAxis::isOn(int axisStatus)
{
if (CHECK_BIT(axisStatus, 0) && CHECK_BIT(axisStatus, 1)) {
return 1;
}
return 0;
}
asynStatus
MasterMACSAxis::move(double position, int relative, double minVelocity,
double maxVelocity, double acceleration)
{
asynStatus status = asynSuccess;
char command[COMLEN], reply[COMLEN];
int devStatus;
//errlogPrintf("minVelocity = %f, maxVelocity = %f\n", minVelocity, maxVelocity);
memset(command, 0, COMLEN * sizeof(char));
/* clear motor error message */
updateMsgTxtFromDriver("");
/*
* reset error code
*/
if(errorCodeFound){
sprintf(command, "%dS17=0", axisNo_);
status = pC_->transactController(axisNo_, command, reply);
errorCodeFound = 0;
}
/*
* only start if the thing is On
*/
devStatus = readStatus();
/*
* set speed
*/
/* temporarily disabled in order to get the basic logic right
sprintf(command, "%dS05=%f", axisNo_, maxVelocity / 1000);
status = pC_->transactController(axisNo_, command, reply);
*/
if (relative) {
position += this->position;
}
if(isOn(devStatus) && active == false) {
errlogPrintf("Starting axis %d with destination %f\n", axisNo_,
position / 1000.);
/* send target position */
sprintf(command, "%dS02= %.3f", axisNo_, position / 1000.);
status = pC_->transactController(axisNo_, command, reply);
if (status == asynError) {
errlogPrintf("MMACS: failed to set target on %d\n", axisNo_);
updateMsgTxtFromDriver("Failed to send target position");
return status;
}
/* send move command */
sprintf(command, "%dS00=1", axisNo_);
status = pC_->transactController(axisNo_, command, reply);
if (status == asynError) {
errlogPrintf("MMACS: failed to start axis %d\n", axisNo_);
updateMsgTxtFromDriver("Failed to start axis");
return status;
}
hasStarted = true;
homing = 0;
active = true;
usleep(500);
lastPositionUpdate = time(NULL);
} else {
errlogPrintf("MMACS: axis %d disabled, cannot start\n", axisNo_);
}
return status;
}
asynStatus
MasterMACSAxis::home(double minVelocity, double maxVelocity,
double acceleration, int forwards)
{
asynStatus status;
char command[COMLEN], reply[COMLEN];
int devStatus;
memset(command, 0, COMLEN * sizeof(char));
/*
* test if the thing is On
*/
devStatus = readStatus();
updateMsgTxtFromDriver("");
if(isOn(devStatus)){
sprintf(command, "%dS00=9", axisNo_);
homing = 1;
status = pC_->transactController(axisNo_, command, reply);
hasStarted = true;
active = true;
lastPositionUpdate = time(NULL);
return status;
} else {
errlogPrintf("MMACS: cannot home disabled axis %d\n", axisNo_);
return asynError;
}
}
asynStatus
MasterMACSAxis::moveVelocity(double minVelocity, double maxVelocity,
double acceleration)
{
setIntegerParam(pC_->motorStatusProblem_, true);
errlogSevPrintf(errlogMajor,
"This controller does not support the jog feature");
return asynError;
}
asynStatus MasterMACSAxis::stop(double acceleration)
{
asynStatus status = asynSuccess;
char command[COMLEN], reply[COMLEN];
int devStatus;
memset(command, 0, COMLEN * sizeof(char));
devStatus = readStatus();
if(!CHECK_BIT(devStatus, 10)) {
// only try to stop when running ...
sprintf(command, "%dS00=8", axisNo_);
status = pC_->transactController(axisNo_, command, reply);
errlogPrintf("Sent STOP on Axis %d\n", axisNo_);
updateMsgTxtFromDriver("Axis interrupted");
}
return status;
}
asynStatus MasterMACSAxis::setPosition(double position)
{
setIntegerParam(pC_->motorStatusProblem_, true);
errlogSevPrintf(errlogMajor,
"This controller does not support setting position");
updateMsgTxtFromDriver("Controller does not support setPosition");
return asynError;
}
asynStatus MasterMACSAxis::setClosedLoop(bool closedLoop)
{
/*
This belongs into the Kingdom of Electronics.
We do not do this.
*/
return asynError;
}
/** Polls the axis.
* This function reads the motor position, the limit status, the home status, the moving status,
* and the drive power-on status.
* It calls setIntegerParam() and setDoubleParam() for each item that it polls,
* and then calls callParamCallbacks() at the end.
* \param[out] moving A flag that is set indicating that the axis is moving (true) or done (false). */
asynStatus MasterMACSAxis::poll(bool * moving)
{
asynStatus comStatus = asynSuccess;
char command[COMLEN], reply[COMLEN], *pPtr, buffer[80];
unsigned int errCode, derCode, devStatus = 0;
float errStatus;
struct tm* tm_info;
time_t timer;
/*
* EPICS always polls all motors on a controller when one motor is active.
* In order to reduce load on the controller we check if we are active and
* if not return old stuff
*/
if(active == false && time(NULL) < lastPoll + pC_->idlePollPeriod_) {
*moving = false;
return asynSuccess;
}
timer = time(NULL);
tm_info = localtime(&timer);
strftime(buffer, 26, "%Y-%m-%d %H:%M:%S", tm_info);
errlogPrintf("poll called at %s on axis %d \n",
buffer, axisNo_ );
lastPoll = time(NULL);
setIntegerParam(pC_->motorStatusProblem_, false);
memset(command, 0, COMLEN * sizeof(char));
// Read the current motor position
sprintf(command, "%dR12", axisNo_);
comStatus = pC_->transactController(axisNo_, command, reply);
if(comStatus != asynError) {
pPtr = strstr(reply, "=");
if(pPtr) {
sscanf(pPtr + 1, "%lf", &position);
setDoubleParam(pC_->motorPosition_, position * 1000.);
setDoubleParam(pC_->motorEncoderPosition_, position * 1000.);
/*
* keep track of position in order to check for a stuck motor later
*/
if(ABS(position - oldPosition) > 1){
oldPosition = position;
lastPositionUpdate = time(NULL);
}
} else {
errlogPrintf("MMACS: Invalid response asking position on %d\n", axisNo_);
}
} else {
errlogPrintf("MMACS: communication problem reading axis %d position\n", axisNo_);
*moving = false;
comStatus = asynError;
goto skip;
}
// Read the overall status of this motor */
devStatus = readStatus();
if(debug || timeDebug) {
errlogPrintf("Axis %d, position %lf, devStatus %d\n", axisNo_,
position, devStatus);
}
// Check for the thing being in a bad state
if(CHECK_BIT(devStatus, 6)) {
*moving = false;
active = false;
setIntegerParam(pC_->motorStatusDone_, true);
updateMsgTxtFromDriver("AXIS dead");
goto skip;
}
setIntegerParam(pC_->enableAxis_, isOn(devStatus));
setIntegerParam(pC_->axisEnabled_, isOn(devStatus));
// Check if the motor is disabled
if (!isOn(devStatus)) {
updateMsgTxtFromDriver("Axis disabled");
*moving = false;
active = false;
setIntegerParam(pC_->motorStatusDone_, true);
goto skip;
}
/*
* if the motor has never run, the status bit 10 is invalid
*/
if (!hasStarted) {
*moving = false;
setIntegerParam(pC_->motorStatusDone_, true);
active = false;
goto skip;
}
/*
* We may have a valid status bit...
*/
if (!CHECK_BIT(devStatus, 10)) {
/* we are still creeping along .... */
*moving = true;
setIntegerParam(pC_->motorStatusDone_, false);
if(time(NULL) > lastPositionUpdate + 120) {
// we are detecting a stuck motor
errlogPrintf("MMACS: axis %d is STUCK!!\n", axisNo_);
updateMsgTxtFromDriver("Axis STUCK!!");
setIntegerParam(pC_->motorStatusDone_, true);
*moving = false;
active = false;
}
goto skip;
}
/*we are done moving */
*moving = false;
active = false;
setIntegerParam(pC_->motorStatusDone_, true);
updateMsgTxtFromDriver("");
/* when homing, set the proper flag */
if (homing) {
setIntegerParam(pC_->motorStatusAtHome_, true);
}
/* check for limit switches*/
setIntegerParam(pC_->motorStatusLowLimit_, false);
setIntegerParam(pC_->motorStatusHighLimit_, false);
if (CHECK_BIT(devStatus, 11)) {
errlogSevPrintf(errlogMajor, "Limit bit in status code %d", axisNo_);
/* guessing which limit has been hit ... */
if (position > 0) {
updateMsgTxtFromDriver("Hit positive limit switch");
setIntegerParam(pC_->motorStatusHighLimit_, true);
} else {
updateMsgTxtFromDriver("Hit negative limit switch");
setIntegerParam(pC_->motorStatusLowLimit_, true);
}
goto skip;
}
/* check for error conditions */
if (CHECK_BIT(devStatus, 3)) {
/* read error codes */
sprintf(command, "%dR11", axisNo_);
comStatus = pC_->transactController(axisNo_, command, reply);
if (comStatus != asynError) {
pPtr = strstr(reply, "=");
if(pPtr) {
sscanf(pPtr + 1, "%f", &errStatus);
errCode = (unsigned int) errStatus;
} else {
errlogPrintf("MMACS: axis %d received invalid reply asking for error code \n", axisNo_);
errCode = 0;
goto skip;
}
} else {
errlogPrintf("MMACS: axis %d failed reading error code \n", axisNo_);
goto skip;
}
sprintf(command, "%dR18", axisNo_);
comStatus = pC_->transactController(axisNo_, command, reply);
if (comStatus != asynError) {
pPtr = strstr(reply, "=");
if(pPtr) {
sscanf(pPtr + 1, "%f", &errStatus);
derCode = (unsigned int) errStatus;
} else {
errlogPrintf("MMACS: malformed reply for R18: %s, on axis %d\n", reply, axisNo_);
derCode = 0;
goto skip;
}
} else {
errlogPrintf("MMACS: axis %d failed reading extended error code R18 \n", axisNo_);
goto skip;
}
if(debug) {
errlogPrintf("Axis %d, errCode(R11) %d, derCode(R18) %d\n", axisNo_,
errCode, derCode);
}
if (errCode == 0) {
errlogSevPrintf(errlogMajor,
"Fault bit in status code, but no error code on %d\n", axisNo_);
updateMsgTxtFromDriver ("Fault bit in status code without error code");
goto skip;
}
errorCodeFound = 1;
if (CHECK_BIT(errCode, 0)) {
errlogSevPrintf(errlogMajor, "CAN error on %d", axisNo_);
updateMsgTxtFromDriver("CAN error");
} else if (CHECK_BIT(errCode, 1)) {
errlogSevPrintf(errlogMajor, "Short circuit on %d", axisNo_);
updateMsgTxtFromDriver("Short circuit");
} else if (CHECK_BIT(errCode, 2)) {
errlogSevPrintf(errlogMajor, "Invalide Setup on %d", axisNo_);
updateMsgTxtFromDriver("Invalid Setup");
} else if (CHECK_BIT(errCode, 3)) {
errlogSevPrintf(errlogMajor, "Control error on %d", axisNo_);
updateMsgTxtFromDriver("Control error");
} else if (CHECK_BIT(errCode, 4)) {
errlogSevPrintf(errlogMajor, "CAN communication error on %d",
axisNo_);
updateMsgTxtFromDriver("CAN communication error");
} else if (CHECK_BIT(errCode, 5)) {
errlogSevPrintf(errlogMajor, "Feedback error on %d", axisNo_);
updateMsgTxtFromDriver("Feedback error");
} else if (CHECK_BIT(errCode, 6)) {
updateMsgTxtFromDriver("Hit positive limit switch");
setIntegerParam(pC_->motorStatusHighLimit_, true);
setIntegerParam(pC_->motorStatusProblem_, false);
} else if (CHECK_BIT(errCode, 7)) {
updateMsgTxtFromDriver("Hit negative limit switch");
setIntegerParam(pC_->motorStatusLowLimit_, true);
setIntegerParam(pC_->motorStatusProblem_, false);
} else if (CHECK_BIT(errCode, 8)) {
errlogSevPrintf(errlogMajor, "Over current %d", axisNo_);
updateMsgTxtFromDriver("Over current");
} else if (CHECK_BIT(errCode, 9)) {
errlogSevPrintf(errlogMajor, "I2T protection on %d", axisNo_);
updateMsgTxtFromDriver("I2t protection");
} else if (CHECK_BIT(errCode, 10)) {
errlogSevPrintf(errlogMajor, "Over heated motor on %d", axisNo_);
updateMsgTxtFromDriver("Motor overheated");
} else if (CHECK_BIT(errCode, 11)) {
errlogSevPrintf(errlogMajor, "Over temperature drive on %d",
axisNo_);
updateMsgTxtFromDriver("Over temperature drive");
} else if (CHECK_BIT(errCode, 12)) {
errlogSevPrintf(errlogMajor, "Over voltage on %d", axisNo_);
updateMsgTxtFromDriver("Over voltage");
} else if (CHECK_BIT(errCode, 13)) {
errlogSevPrintf(errlogMajor, "Under voltage on %d", axisNo_);
updateMsgTxtFromDriver("Under voltage");
} else if (CHECK_BIT(errCode, 14)) {
errlogSevPrintf(errlogMajor, "Command error on %d", axisNo_);
updateMsgTxtFromDriver("Command error");
} else if (CHECK_BIT(errCode, 15)) {
errlogSevPrintf(errlogMajor, "Motor disabled on %d", axisNo_);
updateMsgTxtFromDriver("Motor disabled");
}
}
skip:
callParamCallbacks();
return comStatus;
}
/** Code for iocsh registration */
static const iocshArg
MasterMACSCreateControllerArg0 = { "Port name", iocshArgString };
static const iocshArg
MasterMACSCreateControllerArg1 =
{ "MasterMACS port name", iocshArgString };
static const iocshArg
MasterMACSCreateControllerArg2 = { "Number of axes", iocshArgInt };
static const iocshArg
MasterMACSCreateControllerArg3 = { "idlePoll", iocshArgInt };
static const iocshArg
MasterMACSCreateControllerArg4 = { "busyPoll", iocshArgInt };
static const iocshArg *const
MasterMACSCreateControllerArgs[] = { &MasterMACSCreateControllerArg0,
&MasterMACSCreateControllerArg1,
&MasterMACSCreateControllerArg2,
&MasterMACSCreateControllerArg3,
&MasterMACSCreateControllerArg4
};
static const iocshFuncDef
MasterMACSCreateControllerDef =
{ "MasterMACSCreateController", 5, MasterMACSCreateControllerArgs };
static void MasterMACSCreateContollerCallFunc(const iocshArgBuf * args)
{
MasterMACSCreateController(args[0].sval, args[1].sval, args[2].ival,
args[3].ival, args[4].ival);
}
/**
* C wrapper for the MasterMACSAxis constructor.
* See MasterMAXSAxis::MasterMACSAxis.
*
*/
asynStatus MasterMACSCreateAxis(const char *MasterMACSPort, /* specify which controller by port name */
int axis)
{ /* axis number (start from 1). */
MasterMACSController *pC;
MasterMACSAxis *pAxis;
static const char *functionName = "MasterMACSCreateAxis";
pC = (MasterMACSController *) findAsynPortDriver(MasterMACSPort);
if (!pC) {
printf("%s:%s: Error port %s not found\n", "MasterMACSDriver",
functionName, MasterMACSPort);
return asynError;
}
pC->lock();
pAxis = new MasterMACSAxis(pC, axis);
pAxis = NULL;
pC->unlock();
return asynSuccess;
}
/* MasterMACSCreateAxis */
static const iocshArg
MasterMACSCreateAxisArg0 = { "Controller port name", iocshArgString };
static const iocshArg
MasterMACSCreateAxisArg1 = { "Axis number", iocshArgInt };
static const iocshArg *const
MasterMACSCreateAxisArgs[] = { &MasterMACSCreateAxisArg0,
&MasterMACSCreateAxisArg1
};
static const iocshFuncDef
configMasterMACSAxis =
{ "MasterMACSCreateAxis", 2, MasterMACSCreateAxisArgs };
static void configMasterMACSAxisCallFunc(const iocshArgBuf * args)
{
MasterMACSCreateAxis(args[0].sval, args[1].ival);
}
static void MasterMACSRegister(void)
{
iocshRegister(&MasterMACSCreateControllerDef,
MasterMACSCreateContollerCallFunc);
iocshRegister(&configMasterMACSAxis, configMasterMACSAxisCallFunc);
}
extern "C" {
epicsExportRegistrar(MasterMACSRegister);
}

View File

@ -0,0 +1,77 @@
/*
Driver for the MasterMACS motor controllers used at SINQ.
For documentation see the standard manuals area for SINQ or
Marcel Schildt.
The MasterMACS has a special line protocol which is implemented in
drvAsynMMACSPort.c. The driver will not work with a standard asyn IPPort,
only with the special one.
Mark Koennecke, March, August, 2023
*/
#include "SINQController.h"
#include "SINQAxis.h"
#define COMLEN 50
class MasterMACSAxis : public SINQAxis
{
public:
/* These are the methods we override from the base class */
MasterMACSAxis(class MasterMACSController *pC, int axis);
void report(FILE *fp, int level);
asynStatus move(double position, int relative, double min_velocity, double max_velocity, double acceleration);
asynStatus moveVelocity(double min_velocity, double max_velocity, double acceleration);
asynStatus home(double min_velocity, double max_velocity, double acceleration, int forwards);
asynStatus stop(double acceleration);
asynStatus poll(bool *moving);
asynStatus setPosition(double position);
asynStatus setClosedLoop(bool closedLoop);
private:
MasterMACSController *pC_; /**< Pointer to the asynMotorController to which this axis belongs.
* Abbreviated because it is used very frequently */
double position;
double oldPosition;
time_t lastPositionUpdate;
int homing;
int hasStarted; /* The motor status is invalid if the thing has not run once */
int isOn(int axisStatus);
int readStatus();
int errorCodeFound;
int oldStatus;
bool active;
time_t lastPoll;
friend class MasterMACSController;
};
#define EnableAxisString "ENABLE_AXIS"
#define AxisEnabledString "AXIS_ENABLED"
class MasterMACSController : public SINQController {
public:
MasterMACSController(const char *portName, const char *MasterMACSPortName,
int numAxes, int idlePoll, int busyPoll);
void report(FILE *fp, int level);
MasterMACSAxis* getAxis(asynUser *pasynUser);
MasterMACSAxis* getAxis(int axisNo);
// overloaded because we want to enable/disable the motor
asynStatus readInt32(asynUser *pasynUser, epicsInt32 *value);
asynStatus writeInt32(asynUser *pasynUser, epicsInt32 value);
friend class MasterMACSAxis;
private:
asynUser *pasynUserController_;
MasterMACSAxis **pAxes_; /**< Array of pointers to axis objects */
asynStatus transactController(int axis, char command[COMLEN], char reply[COMLEN]);
int enableAxis_;
int axisEnabled_;
};

View File

@ -138,7 +138,7 @@ NanotecAxis::NanotecAxis(NanotecController *pC, int axisNo, int busAddress)
pC_(pC)
{
this->busAddress = busAddress;
next_poll = -1;
}
@ -377,7 +377,7 @@ asynStatus NanotecAxis::poll(bool *moving)
asynStatus comStatus;
char command[COMLEN], reply[COMLEN];
char *pPtr;
int posVal, statVal;
int posVal, statVal, count = 0;
double lowLim, highLim;
@ -393,8 +393,14 @@ asynStatus NanotecAxis::poll(bool *moving)
if(comStatus) goto skip;
pPtr = strchr(reply,'C');
pPtr++;
posVal = atoi(pPtr);
if(pPtr){
pPtr++;
count = sscanf(pPtr,"%d", &posVal);
}
if(pPtr == NULL || count < 1) {
errlogPrintf("Invalid response %s for #C received for axis %d, address %d\n", reply, axisNo_, busAddress);
return asynError;
}
//errlogPrintf("Axis %d, reply %s, position %d\n", axisNo_, reply, posVal);
setDoubleParam(pC_->motorPosition_, (double)posVal);
@ -407,8 +413,14 @@ asynStatus NanotecAxis::poll(bool *moving)
if(comStatus) goto skip;
pPtr = strchr(reply,'$');
pPtr++;
statVal = atoi(pPtr);
if(pPtr) {
pPtr++;
count = sscanf(pPtr, "%d", &statVal);
}
if(pPtr == NULL || count < 1) {
errlogPrintf("Invalid response %s for #$ received for axis %d busAddress %d\n", reply, axisNo_, busAddress);
return asynError;
}
//errlogPrintf("Axis %d, reply %s, statVal = %d\n",
// axisNo_, reply, statVal);

View File

@ -77,12 +77,27 @@ PhytronController::PhytronController(const char *portName, const char *PhytronPo
pasynOctetSyncIO->setOutputEos(pasynUserController_,etx,strlen(etx));
pasynOctetSyncIO->setInputEos(pasynUserController_,etx,strlen(etx));
new PhytronAxis(this, 1, encX);
new PhytronAxis(this, 2, encY);
/* The special selector D selects the dose controlled axis version */
if(strcmp(sel, (const char *)"D") == 0) {
new PhytronDoseAxis(this, 1, encX);
new PhytronDoseAxis(this, 2, encY);
} else {
new PhytronAxis(this, 1, encX);
new PhytronAxis(this, 2, encY);
}
startPoller(1000./1000., IDLEPOLL, 2);
}
PhytronDoseController::PhytronDoseController(const char *portName, const char *PhytronPortName, const char *sel ,
int encX, int encY)
: PhytronController(portName, PhytronPortName, sel, encX, encY)
{
new PhytronDoseAxis(this, 1, encX);
new PhytronDoseAxis(this, 2, encY);
}
/** Creates a new PhytronController object.
* Configuration command, called directly or from iocsh
@ -97,6 +112,14 @@ extern "C" int PhytronCreateController(const char *portName, const char *Phytron
return(asynSuccess);
}
extern "C" int PhytronDoseCreateController(const char *portName, const char *PhytronPortName, const char *selector,
int encX, int encY)
{
new PhytronDoseController(portName, PhytronPortName,selector, encX, encY);
return(asynSuccess);
}
/** Reports on status of the driver
* \param[in] fp The file pointer on which report information will be written
* \param[in] level The level of report detail desired
@ -129,6 +152,23 @@ PhytronAxis* PhytronController::getAxis(int axisNo)
return static_cast<PhytronAxis*>(asynMotorController::getAxis(axisNo));
}
/** Returns a pointer to an PhytronDoseAxis object.
* Returns NULL if the axis number encoded in pasynUser is invalid.
* \param[in] pasynUser asynUser structure that encodes the axis index number. */
PhytronDoseAxis* PhytronDoseController::getAxis(asynUser *pasynUser)
{
return static_cast<PhytronDoseAxis*>(asynMotorController::getAxis(pasynUser));
}
/** Returns a pointer to an PhytronDoseAxis object.
* Returns NULL if the axis number encoded in pasynUser is invalid.
* \param[in] axisNo Axis index number. */
PhytronDoseAxis* PhytronDoseController::getAxis(int axisNo)
{
return static_cast<PhytronDoseAxis*>(asynMotorController::getAxis(axisNo));
}
/**
* send a command to the Phytron and read the reply. Do some error and controller
* issue fixing on the way
@ -145,7 +185,7 @@ asynStatus PhytronController::transactController(int axisNo,char command[COMLEN]
SINQAxis *axis = getAxis(axisNo);
pasynOctetSyncIO->flush(pasynUserController_);
/* pasynOctetSyncIO->flush(pasynUserController_); */
memset(myCommand,0,sizeof(myCommand));
@ -209,6 +249,9 @@ PhytronAxis::PhytronAxis(PhytronController *pC, int axisNo, int enc)
}
haveBrake = 0;
brakeIO = -1;
next_poll = -1;
homing = 0;
homing_direction = 0;
}
int PhytronAxis::setBrake(int brakeNO)
@ -569,6 +612,79 @@ asynStatus PhytronAxis::poll(bool *moving)
return comStatus;
}
/* The special PhytronDoseAxis code used when the speed is controlled through the neutron flux
*/
PhytronDoseAxis::PhytronDoseAxis(PhytronController *pC, int axisNo, int enc)
: PhytronAxis(pC, axisNo, enc)
{
if(axisNo == 1){
doseChar = '3';
} else {
doseChar = '4';
}
}
asynStatus PhytronDoseAxis::move(double position, int relative, double minVelocity, double maxVelocity, double acceleration)
{
asynStatus status;
//static const char *functionName = "PhytronDoseAxis::move";
char command[COMLEN], reply[COMLEN];
float realTarget;
updateMsgTxtFromDriver("");
/*
set conversion factor from neutron rate to speed
*/
sprintf(command, "R%c3S%f", doseChar, maxVelocity);
status = pC_->transactController(axisNo_,command,reply);
if(strstr(reply,"NACK") != NULL){
errlogSevPrintf(errlogMajor, "Speed not accepted on %d", axisNo_);
updateMsgTxtFromDriver("Invalid speed");
setIntegerParam(pC_->motorStatusProblem_, true);
return asynError;
}
/*
actually send a move command
*/
if (relative) {
position += this->position;
}
homing = 0;
/* set target */
realTarget = position/1000.;
if(realTarget > this->position) {
sprintf(command, "R%c1S%f", doseChar,realTarget);
} else {
sprintf(command, "R%c2S%f", doseChar,realTarget);
}
status = pC_->transactController(axisNo_,command,reply);
if(strstr(reply,"NACK") != NULL){
errlogSevPrintf(errlogMajor, "Drive command not acknowledged on %d", axisNo_);
updateMsgTxtFromDriver("Drive command not acknowledged");
setIntegerParam(pC_->motorStatusProblem_, true);
return asynError;
}
/* really start the move */
sprintf(command, "R%c01S", doseChar);
status = pC_->transactController(axisNo_,command,reply);
if(strstr(reply,"NACK") != NULL){
errlogSevPrintf(errlogMajor, "Drive start command not acknowledged on %d", axisNo_);
updateMsgTxtFromDriver("Drive command not acknowledged");
setIntegerParam(pC_->motorStatusProblem_, true);
return asynError;
}
next_poll = -1;
return status;
}
extern "C" asynStatus PhytronConfBrake(const char *port, /* specify which controller by port name */
int axis, /* axis: 0, 1 */
int brakeNO) /* brakeIO No, 1-8 */
@ -615,6 +731,21 @@ static void PhytronCreateContollerCallFunc(const iocshArgBuf *args)
PhytronCreateController(args[0].sval, args[1].sval, args[2].sval, args[3].ival,args[4].ival);
}
static const iocshArg PhytronDoseCreateControllerArg0 = {"Port name", iocshArgString};
static const iocshArg PhytronDoseCreateControllerArg1 = {"Phytron port name", iocshArgString};
static const iocshArg PhytronDoseCreateControllerArg2 = {"Phytron Selector", iocshArgString};
static const iocshArg PhytronDoseCreateControllerArg3 = {"EncoderX", iocshArgInt};
static const iocshArg PhytronDoseCreateControllerArg4 = {"EncoderY", iocshArgInt};
static const iocshArg * const PhytronDoseCreateControllerArgs[] = {&PhytronDoseCreateControllerArg0,
&PhytronDoseCreateControllerArg1,
&PhytronDoseCreateControllerArg2,
&PhytronDoseCreateControllerArg3,
&PhytronDoseCreateControllerArg4};
static const iocshFuncDef PhytronDoseCreateControllerDef = {"PhytronDoseCreateController", 5, PhytronDoseCreateControllerArgs};
static void PhytronDoseCreateContollerCallFunc(const iocshArgBuf *args)
{
PhytronDoseCreateController(args[0].sval, args[1].sval, args[2].sval, args[3].ival,args[4].ival);
}
/* PhytronConfigureBrake */
static const iocshArg phytronBrakeArg0 = {"Controller port name", iocshArgString};
@ -634,6 +765,7 @@ static void phytronBrakeCallFunc(const iocshArgBuf *args)
static void PhytronRegister(void)
{
iocshRegister(&PhytronCreateControllerDef, PhytronCreateContollerCallFunc);
iocshRegister(&PhytronDoseCreateControllerDef, PhytronDoseCreateContollerCallFunc);
iocshRegister(&phytronBrakeDef, phytronBrakeCallFunc);
}

View File

@ -10,6 +10,11 @@ Updated to go through SINQAxis for -MsgTxt support
Added a selector to support multiple phytrons on a connection
Mark Koennecke, January 2019
Added PhytronDoseAxis. The speed of this axis is controlled by the
neutron dose rate fed in as analog signal
Mark Koennecke, April 2023
*/
#include "SINQController.h"
@ -17,6 +22,10 @@ Mark Koennecke, January 2019
#define COMLEN 80
class PhytronController;
class PhytronDoseController;
class PhytronAxis : public SINQAxis
{
public:
@ -32,7 +41,7 @@ public:
asynStatus setClosedLoop(bool closedLoop);
int setBrake(int brakeIO);
private:
protected:
char phytronChar;
PhytronController *pC_; /**< Pointer to the asynMotorController to which this axis belongs.
* Abbreviated because it is used very frequently */
@ -47,6 +56,21 @@ private:
friend class PhytronController;
};
class PhytronDoseAxis : public PhytronAxis
{
// A special version of PhytronAxis where the speed is controlled by the neutron flux.
public:
PhytronDoseAxis(class PhytronController *pC, int axis, int enc);
asynStatus move(double position, int relative, double min_velocity, double max_velocity, double acceleration);
friend class PhytronDoseController;
protected:
char doseChar;
PhytronDoseController *pC_;
};
class PhytronController : public SINQController {
public:
PhytronController(const char *portName, const char *PhytronPortName, const char *selector,
@ -57,7 +81,8 @@ public:
PhytronAxis* getAxis(int axisNo);
friend class PhytronAxis;
private:
protected:
asynUser *pasynUserController_;
asynStatus transactController(int axisNo, char command[COMLEN], char reply[COMLEN]);
@ -65,3 +90,20 @@ friend class PhytronAxis;
const char *selector;
};
class PhytronDoseController : public PhytronController {
public:
PhytronDoseController(const char *portName, const char *PhytronPortName, const char *selector,
int encX, int encY);
PhytronDoseAxis* getAxis(asynUser *pasynUser);
PhytronDoseAxis* getAxis(int axisNo);
friend class PhytronDoseAxis;
protected:
asynUser *pasynUserController_;
const char *selector;
};

View File

@ -5,10 +5,16 @@
Code lifted from Torsten Boegershausen ESS code.
Mark Koennecke, March 2017
Added code to manage an interMessageSleep
Mark Koennecke, February 2024
*/
#include "SINQController.h"
#include "asynMotorController.h"
#include "epicsExport.h"
#include "iocsh.h"
SINQController::SINQController(const char *portName, const char *SINQPortName, int numAxes, const int& extraParams)
: asynMotorController(portName, numAxes+1, NUM_MOTOR_DRIVER_PARAMS+extraParams,
@ -20,6 +26,53 @@ SINQController::SINQController(const char *portName, const char *SINQPortName, i
{
createParam(motorMessageIsFromDriverString, asynParamInt32, &motorMessageIsFromDriver_);
createParam(motorMessageTextString, asynParamOctet, &motorMessageText_);
interMessageSleep=20;
}
/** Set the interMessageSleep at runtime */
asynStatus SINQController::setInterMessageSleep(int messageSleep)
{
lock();
interMessageSleep = messageSleep;
unlock();
return asynSuccess;
}
extern "C" {
asynStatus setInterMessageSleep(const char *portName, int messageSleep)
{
SINQController *pC;
static const char *functionName = "setIntermessageSleep";
pC = (SINQController*) findAsynPortDriver(portName);
if (!pC) {
printf("%s:%s: Error port %s not found\n", "SINQController", functionName, portName);
return asynError;
}
return pC->setInterMessageSleep(messageSleep);
}
/* setInterMessageSleep */
static const iocshArg setInterMessageSleepArg0 = {"Controller port name", iocshArgString};
static const iocshArg setInterMessageSleepArg1 = {"inter message sleep time", iocshArgInt};
static const iocshArg * const setInterMessageSleepArgs[] = {&setInterMessageSleepArg0,
&setInterMessageSleepArg1};
static const iocshFuncDef setInterMessageSleepDef = {"setInterMessageSleep", 2, setInterMessageSleepArgs};
static void setInterMessageSleepCallFunc(const iocshArgBuf *args)
{
setInterMessageSleep(args[0].sval, args[1].ival);
}
static void SINQControllerRegister(void)
{
iocshRegister(&setInterMessageSleepDef, setInterMessageSleepCallFunc);
}
epicsExportRegistrar(SINQControllerRegister);
} // extern C

View File

@ -18,11 +18,15 @@ class epicsShareClass SINQController : public asynMotorController
{
public:
SINQController(const char *portName, const char *SINQPortName, int numAxes, const int& extraParams=2);
asynStatus setInterMessageSleep(int messageSleep);
friend class SINQAxis;
protected:
int motorMessageIsFromDriver_;
int motorMessageText_;
int interMessageSleep; // minimum time between message to the controller in microseconds
// for slowing down communication to weak controllers.....
};

View File

@ -40,6 +40,8 @@
*/
#include <string.h>
#include <errno.h>
#include <unistd.h>
#include <asynOctetSyncIO.h>
#include <epicsEvent.h>
#include <epicsThread.h>
@ -118,7 +120,7 @@ static void dummyAsynCallback(asynUser *pasynUser)
static void connectSlaveRecords(EL737priv *priv)
{
char slaveName[PVNAME_SZ], errName[256];
char slaveName[PVNAME_SZ + 18], errName[256];
long status;
priv->dbInit = 1;
@ -166,7 +168,7 @@ static void connectSlaveRecords(EL737priv *priv)
errlogPrintf("dbNameToAddr succeded for %s, record access %s\n",slaveName, priv->threshCounter.precord->name);
}
snprintf(slaveName,sizeof(slaveName),"%s:Threshold", priv->psr->name);
snprintf(slaveName, sizeof(slaveName), "%s:Threshold", priv->psr->name);
errlogPrintf("Name of thresholdCounter variable: %s\n", slaveName);
status = dbNameToAddr(slaveName,&priv->threshold);
if(status!= 0){
@ -222,6 +224,7 @@ static long el737_init_record(scalerRecord *psr, CALLBACK *pcallback)
psr->pact = 1;
return 0;
}
pasynManager->autoConnect(dummyUser, 1);
status = pasynOctetSyncIO->connect(port, 0, &priv->asynContext, NULL);
if (status) {
asynPrint(dummyUser, ASYN_TRACE_ERROR,
@ -287,7 +290,7 @@ static long el737_write_preset(scalerRecord *psr, int signal, unsigned long val)
{
EL737priv *priv;
//errlogPrintf("Setting preset %d to %ld\n", signal, val);
// errlogPrintf("EL737: Setting preset %d to %ld\n", signal, val);
priv = (EL737priv *)psr->dpvt;
if(signal >= 0 && signal < 13){
@ -296,7 +299,7 @@ static long el737_write_preset(scalerRecord *psr, int signal, unsigned long val)
if(signal == THRESHVAL){
priv->sendThreshold = 1;
epicsEventSignal(priv->wakeUp);
//errlogPrintf("Setting threshold \n");
// errlogPrintf("EL737: Setting threshold \n");
}
return 0;
@ -309,7 +312,7 @@ static long el737_arm(scalerRecord *psr, int val)
priv = (EL737priv *)psr->dpvt;
priv->countCommand = val;
//errlogPrintf("Sending arm %d \n", val);
// errlogPrintf("EL737: Sending arm %d \n", val);
epicsEventSignal(priv->wakeUp);
@ -321,7 +324,7 @@ static long el737_done(scalerRecord *psr)
EL737priv *priv;
priv = (EL737priv *)psr->dpvt;
// errlogPrintf("Calling done with %d\n", psr->cnt);
// errlogPrintf("EL737: Calling done with %d\n", psr->cnt);
if(priv->counting && psr->cnt == 0){
priv->countCommand = 0;
epicsEventSignal(priv->wakeUp);
@ -343,6 +346,8 @@ static asynStatus el737_transactCommand(EL737priv *priv,char command[COMLEN],cha
pasynOctetSyncIO->flush(priv->asynContext);
// errlogPrintf("EL737: sending command %s\n", command);
strcpy(message,"");
status = pasynOctetSyncIO->writeRead(priv->asynContext, command, strlen(command),
reply,COMLEN, 1.,&out,&in,&reason);
@ -384,7 +389,13 @@ static asynStatus el737_transactCommand(EL737priv *priv,char command[COMLEN],cha
}
}
} else {
strncpy(message,"Lost communication",sizeof(message));
if(errno == EAGAIN){
errlogPrintf("Lost response to %s with EAGAIN\n", command);
} else {
snprintf(message,sizeof(message), "Lost communication with errno %d", errno);
/* force a reconnect */
pasynOctetSyncIO->disconnect(priv->asynContext);
}
}
if(priv->dbInit){
dbStatus = dbPutField(&priv->msgTxt, DBR_STRING,message, 1);
@ -399,7 +410,7 @@ static asynStatus el737_transactCommand(EL737priv *priv,char command[COMLEN],cha
static asynStatus sendStop(EL737priv *priv)
{
char command[COMLEN], reply[COMLEN];
//errlogPrintf("Sending stop\n");
// errlogPrintf("EL737: Sending stop\n");
strcpy(command,"S");
return el737_transactCommand(priv,command,reply);
}
@ -408,7 +419,8 @@ static void runEvents(EL737priv *priv)
{
char command[COMLEN], reply[COMLEN], errName[256];
int status;
long dbStatus, myThreshold, nElements = 1, options = 0, threshCounter;
long dbStatus, nElements = 1, options = 0, threshCounter;
long unsigned int myThreshold;
/*
This is the better way to set the threshold rather then the old way below which uses
@ -434,7 +446,7 @@ static void runEvents(EL737priv *priv)
}
sprintf(command,"DL %d %d", (int)threshCounter,
(int)myThreshold);
errlogPrintf("Sending threshold command %s\n", command);
// errlogPrintf("Sending threshold command %s\n", command);
status = el737_transactCommand(priv,command,reply);
sprintf(command,"DR %d", (int)threshCounter);
status = el737_transactCommand(priv, command,reply);
@ -449,36 +461,32 @@ static void runEvents(EL737priv *priv)
if(priv->sendThreshold == 1) {
// fallback when we do not have the DB fields or threshCounter is invalid
if(priv->presets[THRESHMON] <= 0) {
errlogPrintf("Invalid threshold preset monitor %ld, no threshold sent", priv->presets[THRESHMON]);
} else {
if(priv->presets[THRESHMON] > 0) {
sprintf(command,"DL %d %d", (int)priv->presets[THRESHMON],
(int)priv->presets[THRESHVAL]);
errlogPrintf("Sending threshold from presets, command %s\n", command);
// errlogPrintf("Sending threshold from presets, command %s\n", command);
status = el737_transactCommand(priv,command,reply);
sprintf(command,"DR %d", (int)priv->presets[THRESHMON]);
status = el737_transactCommand(priv, command,reply);
if(status == asynSuccess) {
priv->sendThreshold = 0;
priv->sendThreshold = 0;
}
}
}
//errlogPrintf("Doing a count command: %d\n", priv->countCommand);
errlogPrintf("EL737: Doing a count command: %d\n", priv->countCommand);
if(priv->countCommand == 1){
if(priv->presets[MODE] > 0) {
/* preset monitor */
sprintf(command,"MP %d", (int)priv->presets[MONITOR]);
//errlogPrintf("Starting preset monitor\n");
// errlogPrintf("EL737: Starting preset monitor\n");
} else {
/* preset time */
sprintf(command,"TP %f", priv->presets[TIMER]/1000.);
//errlogPrintf("Starting preset timer\n");
// errlogPrintf("EL737: Starting preset timer\n");
}
status = el737_transactCommand(priv,command,reply);
if(status == asynSuccess){
priv->counting = 1;
}
priv->counting = 1;
} else {
if(priv->counting) {
/* Stop */
@ -490,8 +498,8 @@ static void runEvents(EL737priv *priv)
if(status != asynSuccess){
errlogPrintf("devScalerEL737::el737Thread communication problem %s\n",
priv->asynContext->errorMessage);
errlogPrintf("devScalerEL737::el737Thread communication problem %s\n, errno =%d",
priv->asynContext->errorMessage, errno);
}
}
@ -505,8 +513,12 @@ static void updateValues(EL737priv *priv)
strcpy(command,"RA");
status = el737_transactCommand(priv,command,reply);
if(status != asynSuccess){
errlogPrintf("devScalerEL737::el737Thread communication problem %s\n",
priv->asynContext->errorMessage);
if(errno == EAGAIN){
errlogPrintf("devScalerEL737::el737Thread lost response to %s with EAGAIN\n", command);
} else {
errlogPrintf("devScalerEL737::el737Thread communication problem %s, errno = %d\n",
priv->asynContext->errorMessage, errno);
}
return;
}
status = sscanf(reply, "%f %ld %ld %ld %ld %ld %ld %ld %ld",
@ -544,11 +556,11 @@ static void el737Thread(void *param)
int rs, ctStatus;
long dbStatus, options, nElements = 1, pauseFlag = 0;
//errlogPrintf("Within EL737 thread \n");
// errlogPrintf("EL737: Within EL737 thread \n");
while(1){
evStatus = epicsEventWaitWithTimeout(priv->wakeUp,timeout);
//errlogPrintf("EL737 thread woke up with %d\n", evStatus);
// errlogPrintf("EL737 thread woke up with %d\n", evStatus);
if(evStatus == epicsEventWaitOK) {
/*
@ -556,7 +568,8 @@ static void el737Thread(void *param)
*/
runEvents(priv);
if(priv->counting == 1){
timeout = .1;
timeout = .2;
usleep(500);
}
} else {
/*
@ -575,7 +588,7 @@ static void el737Thread(void *param)
if(status != asynSuccess){
errlogPrintf("devScalerEL737::el737Thread communication problem %s\n",
priv->asynContext->errorMessage);
break;
continue;
}
sscanf(reply,"%d",&rs);
//errlogPrintf("RS = %d\n", rs);
@ -624,7 +637,7 @@ static void el737Thread(void *param)
if(status != asynSuccess){
errlogPrintf("devScalerEL737::el737Thread communication problem %s\n",
priv->asynContext->errorMessage);
break;
continue;
}
} else if(pauseFlag == 0 && ctStatus == 3){
strcpy(command,"CO");
@ -632,7 +645,7 @@ static void el737Thread(void *param)
if(status != asynSuccess){
errlogPrintf("devScalerEL737::el737Thread communication problem %s\n",
priv->asynContext->errorMessage);
break;
continue;
}
}
}

View File

@ -1,4 +1,4 @@
/********************************************
/*******************************************
* pmacAxis.cpp
*
* PMAC Asyn motor based on the
@ -16,7 +16,7 @@
* when the motor is hung, it wont start. I check for this and cause an alarm.
*
* Another mode where the motor is in trouble is if it stays too long in status
*5 or 6. I check and cause an alarm in this state too.
* 5 or 6. I check and cause an alarm in this state too.
*
* Mark Koennecke, February 2013
*
@ -28,6 +28,14 @@
*
* Michele Brambilla, February 2022
*
* Added driver for special AMOR detector axes
*
* Mark Koennecke, June 2023
*
* Added driver for GirderAxis
*
* Stefan Mathis, July 2024
*
********************************************/
#include <epicsExit.h>
@ -42,6 +50,7 @@
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <unistd.h>
#include<algorithm>
#include<cmath>
@ -180,7 +189,7 @@ asynStatus pmacAxis::getAxisInitialStatus(void) {
cmdStatus = pC_->lowLevelWriteRead(axisNo_, command, response);
if (cmdStatus) {
asynPrint(pC_->pasynUserSelf, ASYN_TRACE_ERROR,
"%s: Error: enaabling axis %d failed.\n", functionName,
"%s: Error: enabling axis %d failed.\n", functionName,
axisNo_);
return asynError;
}
@ -219,6 +228,11 @@ asynStatus pmacAxis::move(double position, int relative, double min_velocity,
status6Time = 0;
starting = 1;
/* set speed first */
sprintf(command, "Q%d04=%f", axisNo_, max_velocity);
status = pC_->lowLevelWriteRead(axisNo_, command, response);
/* send drive command */
sprintf(command, "P%2.2d23=0 Q%2.2d01=%12.4f M%2.2d=1", axisNo_, axisNo_,
realPosition, axisNo_);
@ -277,17 +291,22 @@ asynStatus pmacAxis::setPosition(double position) {
}
asynStatus pmacAxis::stop(double acceleration) {
asynStatus status = asynError;
asynStatus status = asynSuccess;
static const char *functionName = "pmacAxis::stopAxis";
bool moving = false;
pC_->debugFlow(functionName);
char command[128] = {0};
char response[32] = {0};
sprintf(command, "M%2.2d=8", axisNo_);
this->getAxisStatus(&moving);
status = pC_->lowLevelWriteRead(axisNo_, command, response);
if(moving) {
// only send a stop when actually moving
sprintf(command, "M%2.2d=8", axisNo_);
status = pC_->lowLevelWriteRead(axisNo_, command, response);
}
return status;
}
@ -339,7 +358,7 @@ static char *translateAxisError(int axErr) {
return strdup("command not allowed");
break;
case 6:
return strdup("watchdog triggere");
return strdup("watchdog triggered");
break;
case 7:
return strdup("current limit reached");
@ -474,7 +493,6 @@ asynStatus pmacAxis::getAxisStatus(bool *moving) {
*moving = false;
setIntegerParam(pC_->motorStatusMoving_, false);
setIntegerParam(pC_->motorStatusDone_, true);
setIntegerParam(pC_->motorStatusProblem_, true);
errlogPrintf("Axis %d did not start within 10 seconds!! BROKEN\n", axisNo_);
updateMsgTxtFromDriver("Axis did not start within 10 seconds");
starting = 0;
@ -582,7 +600,7 @@ asynStatus pmacHRPTAxis::getAxisStatus(bool *moving) {
char response[pC_->PMAC_MAXBUF_];
int cmdStatus = 0, nvals, crashSignal;
asynStatus result = pmacAxis::getAxisStatus(moving);
asynStatus result = pmacV3Axis::getAxisStatus(moving);
sprintf(command, "P%2.2d53", axisNo_);
cmdStatus = pC_->lowLevelWriteRead(axisNo_, command, response);
nvals = sscanf(response, "%d", &crashSignal);
@ -599,7 +617,6 @@ asynStatus pmacHRPTAxis::getAxisStatus(bool *moving) {
*moving = false;
setIntegerParam(pC_->motorStatusMoving_, false);
setIntegerParam(pC_->motorStatusDone_, true);
setIntegerParam(pC_->motorStatusProblem_, true);
}
return result;
}
@ -618,7 +635,7 @@ asynStatus pmacAxis::enable(int on) {
* ======================================================*/
SeleneAxis::SeleneAxis(SeleneController *pC, int axisNo, double limitTarget)
: pmacAxis(pC, axisNo, false) {
static const char *functionName = "pmacAxis::pmacAxis";
static const char *functionName = "SeleneAxis::SeleneAxis";
pC_->debugFlow(functionName);
@ -892,15 +909,10 @@ pmacV3Axis::pmacV3Axis(pmacController *pController, int axisNo)
};
asynStatus pmacV3Axis::poll(bool *moving) {
int status = 0;
asynStatus status = asynSuccess;
static const char *functionName = "pmacV3Axis::poll";
char message[132];
// Protect against excessive polling
if (time(NULL) < next_poll) {
return asynSuccess;
}
sprintf(message, "%s: Polling axis: %d", functionName, this->axisNo_);
pC_->debugFlow(message);
@ -912,14 +924,9 @@ asynStatus pmacV3Axis::poll(bool *moving) {
functionName, pC_->portName, axisNo_);
}
if (*moving) {
next_poll = time(NULL) + BUSYPOLL;
} else {
next_poll = time(NULL) + IDLEPOLL;
}
callParamCallbacks();
return status ? asynError : asynSuccess;
return status;
}
@ -944,14 +951,16 @@ asynStatus pmacV3Axis::getAxisStatus(bool *moving) {
if (cmdStatus || nvals != 3) {
asynPrint(pC_->pasynUserSelf, ASYN_TRACE_ERROR,
"drvPmacAxisGetStatus: Failed to read position and status, "
"Status: %d\nCommand :%s\nResponse:%s\n",
cmdStatus, command, response);
"Status: %d\nCommand :%s\nParsed: %d\nResponse:%s\n",
cmdStatus, command, nvals, response);
updateMsgTxtFromDriver("Cannot read Axis position and status");
}
pmacV3Controller *p3C_ = (pmacV3Controller *)pC_;
IsEnable = axStat != -3;
asynStatus st = setIntegerParam(p3C_->axisEnabled_, axStat > -1);
setIntegerParam(p3C_->axisEnabled_, IsEnable);
asynStatus st = setIntegerParam(p3C_->enableAxis_, IsEnable);
cmdStatus = cmdStatus > st ? cmdStatus : st;
int direction = 0;
@ -977,36 +986,17 @@ asynStatus pmacV3Axis::getAxisStatus(bool *moving) {
previous_position_ = position;
previous_direction_ = direction;
/* are we done? */
if ((axStat == 0 || axStat < 0) && starting == 0) {
if (axStat <= 0 && starting == 0) {
done = 1;
} else {
starting = 0;
done = 0;
}
// if (starting && time(NULL) > startTime + 10) {
// /*
// did not start in 10 seconds: messed up: cause an alarm
// */
// done = 1;
// *moving = false;
// st = setIntegerParam(pC_->motorStatusMoving_, false);
// cmdStatus = cmdStatus > st ? cmdStatus : st;
// st = setIntegerParam(pC_->motorStatusDone_, true);
// cmdStatus = cmdStatus > st ? cmdStatus : st;
// st = setIntegerParam(pC_->motorStatusProblem_, true);
// cmdStatus = cmdStatus > st ? cmdStatus : st;
// errlogPrintf("Axis %d did not start within 10 seconds!! BROKEN\n", axisNo_);
// updateMsgTxtFromDriver("Axis did not start within 10 seconds");
// starting = 0;
// return st;
// }
// /*
// code to test against too long status 5 or 6
// */
int EstimatedTimeOfArrival = 60;
int EstimatedTimeOfArrival = 120;
if (axStat == 5 || axStat == 6) {
if (status6Time == 0) {
status6Time = time(NULL);
@ -1014,20 +1004,11 @@ asynStatus pmacV3Axis::getAxisStatus(bool *moving) {
EstimatedTimeOfArrival =
std::ceil(2 * std::fabs(setpointPosition_ - position) / Speed);
}
else {
} else {
if (time(NULL) > status6Time + EstimatedTimeOfArrival) {
/* trigger error only when not moving */
if (abs(position - statusPos) < .1) {
// done = 1;
// *moving = false;
// st = setIntegerParam(pC_->motorStatusMoving_, false);
// cmdStatus = cmdStatus > st ? cmdStatus : st;
// st = setIntegerParam(pC_->motorStatusDone_, true);
// cmdStatus = cmdStatus > st ? cmdStatus : st;
// st = setIntegerParam(pC_->motorStatusProblem_, true);
// cmdStatus = cmdStatus > st ? cmdStatus : st;
done = 1;
errlogPrintf(
"Axis %d stayed in 5,6 for more than %d seconds BROKEN\n",
axisNo_, EstimatedTimeOfArrival);
@ -1082,6 +1063,7 @@ asynStatus pmacV3Axis::getAxisStatus(bool *moving) {
cmdStatus = cmdStatus > st ? cmdStatus : st;
nvals = sscanf(response, "%d %d %d", &axErr, &highLim, &lowLim);
if (st || nvals != 3) {
asynPrint(
pC_->pasynUserSelf, ASYN_TRACE_ERROR,
@ -1092,9 +1074,6 @@ asynStatus pmacV3Axis::getAxisStatus(bool *moving) {
"Cannot read Axis Error, high and low limit flag Status");
}
// errlogPrintf("Polling, axStat = %d, axErr = %d, position = %f\n", axStat,
// axErr, position);
sprintf(message, "poll results: axis %d, status %d, axErr %d, done = %d",
axisNo_, axStat, axErr, done);
pC_->debugFlow(message);
@ -1148,6 +1127,40 @@ asynStatus pmacV3Axis::getAxisStatus(bool *moving) {
axisProblemFlag = 0;
axisErrorCount = 0;
}
// test for more error conditions encoded in axStat
switch(axStat) {
case -4:
snprintf(message, sizeof(message), "PMAC: %s on %d",
"Emergency stop activated", axisNo_);
updateMsgTxtFromDriver(message);
asynPrint(pC_->pasynUserSelf, ASYN_TRACE_ERROR, "%s\n", message);
axisProblemFlag = 1;
break;
case -5:
snprintf(message, sizeof(message), "PMAC: %s on %d",
"Motor is not installed", axisNo_);
updateMsgTxtFromDriver(message);
asynPrint(pC_->pasynUserSelf, ASYN_TRACE_ERROR, "%s\n", message);
axisProblemFlag = 1;
break;
case -3:
snprintf(message, sizeof(message), "PMAC: %s on %d",
"Axis disabled", axisNo_);
updateMsgTxtFromDriver(message);
asynPrint(pC_->pasynUserSelf, ASYN_TRACE_ERROR, "%s\n", message);
axisProblemFlag = 1;
break;
case -2:
case -1:
snprintf(message, sizeof(message), "PMAC: %s on %d",
"Axis locked", axisNo_);
updateMsgTxtFromDriver(message);
asynPrint(pC_->pasynUserSelf, ASYN_TRACE_ERROR, "%s\n", message);
axisProblemFlag = 1;
break;
}
st = setIntegerParam(pC_->motorStatusProblem_, axisProblemFlag);
cmdStatus = cmdStatus > st ? cmdStatus : st;
@ -1163,4 +1176,582 @@ asynStatus pmacV3Axis::move(double position, int relative, double min_velocity,
}
updateMsgTxtFromDriver("");
return pmacAxis::move(position, relative, min_velocity, max_velocity, acceleration);
}
}
/*========================= Amor Detector Motor code ==============*/
AmorDetectorAxis::AmorDetectorAxis(pmacController *pC, int axisNo, int function)
: pmacAxis(pC, axisNo, false) {
static const char *functionName = "pmacAxis::pmacAxis";
pC_->debugFlow(functionName);
_function = function;
det_starting = false;
det_startTime = time(NULL);
callParamCallbacks();
/* Wake up the poller task which will make it do a poll,
* updating values for this axis to use the new resolution (stepSize_) */
pC_->wakeupPoller();
}
/*-----------------------------------------------------------------*/
asynStatus AmorDetectorAxis::moveVelocity(double min_velocity,
double max_velocity,
double acceleration)
{
updateMsgTxtFromDriver("Function moveVelocity not allowed");
return asynError;
}
/*------------------------------------------------------------------*/
asynStatus AmorDetectorAxis::home(double min_velocity,
double max_velocity,
double acceleration,
int forwards)
{
updateMsgTxtFromDriver("Cannot home AMOR detector motors");
return asynError;
}
/*-------------------------------------------------------------------*/
asynStatus AmorDetectorAxis::setPosition(double position)
{
updateMsgTxtFromDriver("Cannot setPosition() AMOR detector motors");
return asynError;
}
/*-------------------------------------------------------------------*/
asynStatus AmorDetectorAxis::move(double position, int relative,
double min_velocity, double max_velocity,
double acceleration)
{
asynStatus status = asynError;
static const char *functionName = "AmorDetectorAxis::move";
double realPosition;
int parkStatus;
time_t errorWait;
updateMsgTxtFromDriver("");
pC_->debugFlow(functionName);
char command[128] = {0};
char response[32] = {0};
pC_->debugFlow(functionName);
/* test if we are not in the change position */
sprintf(command,"P358");
pC_->lowLevelWriteRead(axisNo_, command, response);
sscanf(response, "%d", &parkStatus);
if(_function != ADPARK && parkStatus == 2) {
updateMsgTxtFromDriver("Cannot run motor when in park position");
errlogPrintf("Cannot run motor when in park position");
return asynError;
}
if (relative) {
realPosition = previous_position_ + position / MULT;
} else {
realPosition = position / MULT;
}
/* set target */
errlogPrintf("function %d, sending position %f\n", _function, realPosition);
switch(_function){
case ADCOM:
sprintf(command, "Q451=%f", realPosition);
break;
case ADCOZ:
sprintf(command, "Q454=%f", realPosition);
break;
case ADPARK:
// test if a reset is required first
sprintf(command, "P359");
pC_->lowLevelWriteRead(axisNo_, command, response);
sscanf(response, "%d", &parkStatus);
errlogPrintf("park status %d\n", parkStatus);
if(parkStatus != 0){
sprintf(command,"P352=2");
pC_->lowLevelWriteRead(axisNo_, command, response);
errlogPrintf("Park status %d, error reset: %s\n", parkStatus, command);
}
errorWait = time(NULL);
while(true){
usleep(5000);
sprintf(command, "P359");
pC_->lowLevelWriteRead(axisNo_, command, response);
sscanf(response, "%d", &parkStatus);
errlogPrintf("P359 = %d\n", parkStatus);
if(parkStatus == 0){
break;
}
if(time(NULL) > errorWait + 3){
errlogPrintf("Failed to clear error status after 3 seconds\n");
updateMsgTxtFromDriver("Failed to clear errorState in 3 seconds");
return asynError;
}
}
// now drive to the real place
if(realPosition == 0) {
sprintf(command, "P350=3");
} else if(realPosition < -90){
sprintf(command, "P350=2");
} else {
updateMsgTxtFromDriver("Can only reach -100, 0, nothing else");
return asynError;
}
errlogPrintf("Park position %f, park command: %s\n", realPosition, command);
}
status = pC_->lowLevelWriteRead(axisNo_, command, response);
if(status != asynSuccess) {
asynPrint(pC_->pasynUserSelf, ASYN_TRACE_ERROR,
"AmorDetectorAxis::move failed to send command %s", command);
updateMsgTxtFromDriver("Cannot send Amor Detector Command");
return status;
}
/* send move command */
if(_function != ADPARK) {
sprintf(command, "P350=1");
status = pC_->lowLevelWriteRead(axisNo_, command, response);
if(status != asynSuccess) {
asynPrint(pC_->pasynUserSelf, ASYN_TRACE_ERROR,
"AmorDetectorAxis::move failed to send command %s", command);
updateMsgTxtFromDriver("Cannot send Amor Detector Command");
}
}
det_starting = true;
det_startTime = time(NULL);
next_poll = BUSYPOLL;
return status;
}
/*-----------------------------------------------------------------------------*/
asynStatus AmorDetectorAxis::stop(double acceleration) {
asynStatus status = asynError;
static const char *functionName = "AmorDetectorAxis::stopAxis";
bool moving = false;
pC_->debugFlow(functionName);
char command[128] = {0};
char response[32] = {0};
this->poll(&moving);
det_starting = false;
if(moving) {
// only send a stop when actually moving
sprintf(command, "P350=8");
status = pC_->lowLevelWriteRead(axisNo_, command, response);
}
return status;
}
/*-------------------------------------------------------------------------------*/
asynStatus AmorDetectorAxis::poll(bool *moving)
{
asynStatus status = asynError;
static const char *functionName = "AmorDetectorAxis::poll";
int targetReached, errorCode, parkCode;
double position;
pC_->debugFlow(functionName);
char command[128] = {0};
char response[32] = {0};
char errorMessage[128] = {0};
/* read in position flag*/
sprintf(command, "P354");
status = pC_->lowLevelWriteRead(axisNo_, command, response);
if(status != asynSuccess){
asynPrint(pC_->pasynUserSelf, ASYN_TRACE_ERROR,
"AmorDetectorAxis::poll failed to read in position");
updateMsgTxtFromDriver("Failed to read Amor Detector in position");
return status;
}
sscanf(response, "%d", &targetReached);
//if(_function == ADCOM){
// errlogPrintf("startTime: %ld, current time: %ld, starting flag %d, targetReached %d\n", det_startTime, time(NULL),
// det_starting, targetReached);
//}
// The thing is sometimes slow to start, allow 7 seconds
// Especially coz is really slow to indicate
if(det_starting && time(NULL) > (det_startTime + 7)) {
det_starting = false;
//if(_function == ADCOM){
// errlogPrintf("Resetting starting flag after timeout\n");
//}
}
setIntegerParam(pC_->motorStatusProblem_, false);
if(targetReached == 0 && det_starting == false){
*moving = false;
setIntegerParam(pC_->motorStatusDone_, true);
next_poll = IDLEPOLL;
} else {
*moving = true;
if(targetReached == 1){
det_starting = false;
//if(_function == ADCOM){
// errlogPrintf("Resetting starting flag following targetReached flag\n");
//}
}
setIntegerParam(pC_->motorStatusDone_, false);
}
// read position
if(_function == ADCOM) {
strcpy(command,"Q0510");
} else if(_function == ADCOZ) {
strcpy(command, "Q454");
} else if(_function == ADPARK){
strcpy(command,"P358");
}
status = pC_->lowLevelWriteRead(axisNo_, command, response);
if(status != asynSuccess){
asynPrint(pC_->pasynUserSelf, ASYN_TRACE_ERROR,
"AmorDetectorAxis::poll failed to read value");
updateMsgTxtFromDriver("Failed to read Amor Detector value");
return status;
}
if(_function != ADPARK){
sscanf(response, "%lf", &position);
} else {
// analyse the park code to check where the thing is. This does
// not show good values while driving. But that is a limitation
// of the hardware.
sscanf(response, "%d", &parkCode);
if(parkCode == 1) {
position = 0;
} else {
position = -100;
}
// errlogPrintf("parkCode %d , position %f,targetREached = %d\n", parkCode, position, targetReached);
}
setDoubleParam(pC_->motorPosition_, position * 1000.);
setDoubleParam(pC_->motorEncoderPosition_, position * 1000.);
//if(_function == ADCOM){
// errlogPrintf("polling for function %d, position %f, pos*10^3 %f, targetREached = %d\n", _function, position,
// position*MULT, targetReached);
// }
// read error status
sprintf(command, "P359");
status = pC_->lowLevelWriteRead(axisNo_, command, response);
if(status == asynSuccess) {
sscanf(response, "%d", &errorCode);
switch(errorCode) {
case 0:
break;
case 1:
strcpy(errorMessage,"COZ not released");
break;
case 2:
strcpy(errorMessage,"Motor is in wrong state");
break;
case 3:
strcpy(errorMessage,"FTZ error while moving");
break;
case 4:
strcpy(errorMessage, "Break while moving");
break;
case 5:
strcpy(errorMessage, "No freigabe during move");
break;
default:
strcpy(errorMessage, "Unknown error code from: ");
strcat(errorMessage, response);
break;
}
if(strlen(errorMessage) > 1) {
asynPrint(pC_->pasynUserSelf, ASYN_TRACE_ERROR,
"AmorDetectorAxis::poll: %s", errorMessage);
updateMsgTxtFromDriver(errorMessage);
status = asynError;
}
}
callParamCallbacks();
return status;
}
/*-------------------------------------------------------------------------------*/
GirderAxis::GirderAxis(pmacController *pController, int axisNo)
: pmacV3Axis(pController, axisNo){
// GirderAxis expects a pmacV3Controller. Therefore it is checked whether the pointer pController can be cast
// to this object type. If this is not possible, the user made an error in the configuration files. This is documented
// in a corresponding error; after that, an exception is thrown to avoid returning an "illegal" instance of GirderAxis.
pmacV3Controller* pV3Controller = dynamic_cast<pmacV3Controller*>(pController);
if (pV3Controller == nullptr) {
errlogPrintf("A GirderAxis instance needs a pmacV3Controller. Please check the configuration files.");
throw std::invalid_argument("A GirderAxis instance needs a pmacV3Controller. Please check the configuration files.");
}
// Initial values for member variables
next_poll = -1;
previous_position_ = 0.0;
previous_direction_ = 0;
status6Time = 0;
statusPos = 0.0;
homing = 0;
axisErrorCount = 0;
};
/*-------------------------------------------------------------------------------*/
asynStatus GirderAxis::move(double position, int relative, double min_velocity, double max_velocity, double acceleration) {
// If the axis is not enabled, do nothing
if(!IsEnable) {
updateMsgTxtFromDriver("Error: axis disabled");
return asynError;
}
// =======================================
// Local variable declaration
asynStatus status = asynError;
char command[pC_->PMAC_MAXBUF_] = {0};
char response[pC_->PMAC_MAXBUF_] = {0};
double realPosition = 0;
// =======================================
updateMsgTxtFromDriver("");
static const char *functionName = "GirderAxis::move";
pC_->debugFlow(functionName);
if (relative) {
realPosition = previous_position_ + position / MULT;
} else {
realPosition = position / MULT;
}
startTime = time(NULL);
status6Time = 0;
starting = 1;
// Set target position
snprintf(command, sizeof(command), "Q251=%f", realPosition);
status = pC_->lowLevelWriteRead(axisNo_, command, response);
if (status == asynError) {
updateMsgTxtFromDriver("Error: Could not set target position");
return asynError;
}
// Start motion
snprintf(command, sizeof(command), "P150=1");
status = pC_->lowLevelWriteRead(axisNo_, command, response);
if (status == asynError) {
updateMsgTxtFromDriver("Error: Could not start motion");
return asynError;
}
next_poll = -1;
return status;
}
asynStatus GirderAxis::stop(double acceleration) {
// =======================================
// Local variable declaration
asynStatus status = asynSuccess;
static const char *functionName = "GirderAxis::stop";
bool moving = false;
char command[pC_->PMAC_MAXBUF_] = {0};
char response[pC_->PMAC_MAXBUF_] = {0};
// =======================================
pC_->debugFlow(functionName);
this->poll(&moving);
if(moving) {
// only send a stop when actually moving
snprintf(command, sizeof(command), "P150=8");
status = pC_->lowLevelWriteRead(axisNo_, command, response);
}
return status;
}
asynStatus GirderAxis::poll(bool *moving) {
// =======================================
// Local variable declaration
asynStatus status = asynSuccess;
static const char *functionName = "GirderAxis::poll";
char command[pC_->PMAC_MAXBUF_] = {0};
char response[pC_->PMAC_MAXBUF_] = {0};
char message[132], *axMessage;
double position = 0;
int moving_to_position = 0, nvals = 0, axisProblemFlag = 0, axStat = 0, axError = 0, isEnabled = 1;
asynStatus st;
// =======================================
// Check for girder axis specific errors
snprintf(command, sizeof(command), "P159");
status = pC_->lowLevelWriteRead(axisNo_, command, response);
sscanf(response, "P159=%d", &axError);
switch(axError) {
case 1:
snprintf(message, sizeof(message), "PMAC: %s on %d",
"Axis not switched on", axisNo_);
updateMsgTxtFromDriver(message);
asynPrint(pC_->pasynUserSelf, ASYN_TRACE_ERROR, "%s\n", message);
axisProblemFlag = 1;
isEnabled = 0;
break;
case 2:
snprintf(message, sizeof(message), "PMAC: %s on %d",
"Axis not ready for new command", axisNo_);
updateMsgTxtFromDriver(message);
asynPrint(pC_->pasynUserSelf, ASYN_TRACE_ERROR, "%s\n", message);
axisProblemFlag = 1;
break;
case 3:
snprintf(message, sizeof(message), "PMAC: %s on %d",
"Axis 1 ERROR during motion", axisNo_);
updateMsgTxtFromDriver(message);
asynPrint(pC_->pasynUserSelf, ASYN_TRACE_ERROR, "%s\n", message);
axisProblemFlag = 1;
break;
case 4:
snprintf(message, sizeof(message), "PMAC: %s on %d",
"Axis 2 ERROR during motion", axisNo_);
updateMsgTxtFromDriver(message);
asynPrint(pC_->pasynUserSelf, ASYN_TRACE_ERROR, "%s\n", message);
axisProblemFlag = 1;
break;
}
st = setIntegerParam(pC_->motorStatusProblem_, axisProblemFlag);
status = status > st ? status : st;
// Downcast the pointer pmacController to pmacV3Controller in a typesafe manner
pmacV3Controller* p3C_ = dynamic_cast<pmacV3Controller*>(pC_);
if (p3C_ == nullptr) {
errlogPrintf("A GirderAxis instance needs a pmacV3Controller. Please check the configuration files.");
throw std::invalid_argument("A GirderAxis instance needs a pmacV3Controller. Please check the configuration files.");
}
setIntegerParam(p3C_->axisEnabled_, isEnabled);
st = setIntegerParam(p3C_->enableAxis_, isEnabled);
status = status > st ? status : st;
int direction = 0;
/*
In GirderAxis::move, the user input is scaled by /MULT. Hence, the output of
Qxx10 needs to be scaled by *MULT
*/
st = setDoubleParam(pC_->motorPosition_, position * MULT);
status = status > st ? status : st;
st = setDoubleParam(pC_->motorEncoderPosition_, position * MULT);
status = status > st ? status : st;
// Calculate the current (or last) movement direction
if ((position - previous_position_) > 0) {
direction = 1;
} else if (position - previous_position_ == 0.0) {
direction = previous_direction_;
} else {
direction = 0;
}
st = setIntegerParam(pC_->motorStatusDirection_, direction);
status = status > st ? status : st;
// Store the position which was read out from the hardware together with the calculated direction for the next poll
previous_position_ = position;
previous_direction_ = direction;
// Is the axis currently moving?
snprintf(command, sizeof(command), "P154");
status = pC_->lowLevelWriteRead(axisNo_, command, response);
sscanf(response, "P154=%d", &moving_to_position);
/*
This code tests whether the axis is too long in status 5 or 6.
If the axis is in status 5 or 6, a time counter (status6Time) is started and updated during subsequent polls.
If status6Time exceeds the estimated arrival time of 120 seconds, a corresponding error is returned.
*/
int EstimatedTimeOfArrival = 120; // seconds
if (axStat == 5 || axStat == 6) {
if (status6Time == 0) {
status6Time = time(nullptr);
statusPos = position;
} else {
if (time(nullptr) > status6Time + EstimatedTimeOfArrival) {
/* trigger error only when not moving */
if (abs(position - statusPos) < .1) {
moving_to_position = 0;
errlogPrintf(
"Axis %d stayed in status 5 or 6 for more than %d seconds BROKEN\n",
axisNo_, EstimatedTimeOfArrival);
updateMsgTxtFromDriver("Axis stayed in status 5 or 6 for more than estimated time: BROKEN");
status6Time = 0;
return status;
} else {
status6Time = time(nullptr);
statusPos = position;
}
}
}
}
/*
If the axis is moving, set the corresponding flags in the pmacController (pC_) and end the poll.
*/
if (!moving_to_position) {
*moving = true;
st = setIntegerParam(pC_->motorStatusMoving_, true);
status = status > st ? status : st;
st = setIntegerParam(pC_->motorStatusDone_, false);
return status > st ? status : st;
} else {
*moving = false;
st = setIntegerParam(pC_->motorStatusMoving_, false);
status = status > st ? status : st;
st = setIntegerParam(pC_->motorStatusDone_, true);
status = status > st ? status : st;
}
/* Set any axis specific general problem bits. */
if (axError != 0) {
axisProblemFlag = 1;
if (axisErrorCount < 10) {
axMessage = translateAxisError(axError);
asynPrint(pC_->pasynUserSelf, ASYN_TRACE_ERROR,
"drvGirderAxisGetStatus: Axis %d is in deep trouble: axis error "
"code %d, translated: %s:, status code = %d\n",
axisNo_, axError, axMessage, axStat);
snprintf(message, sizeof(message), "GirderAxis error: %s", axMessage);
updateMsgTxtFromDriver(message);
if (axMessage != NULL) {
free(axMessage);
}
axisErrorCount++;
} else if (axisErrorCount == 10) {
asynPrint(pC_->pasynUserSelf, ASYN_TRACE_ERROR,
"Suppressing further axis error messages\n");
axisErrorCount++;
}
} else {
axisProblemFlag = 0;
axisErrorCount = 0;
}
st = setIntegerParam(pC_->motorStatusProblem_, axisProblemFlag);
status = status > st ? status : st;
return status;
}

View File

@ -1,160 +1,222 @@
/********************************************
* pmacAxis.cpp
*
* PMAC Asyn motor based on the
*
* PMAC Asyn motor based on the
* asynMotorAxis class.
*
*
* Matthew Pearson
* 23 May 2012
*
*
* Modified to use the MsgTxt field for SINQ
*
* Mark Koennecke, January 2019
*
* EXtended with special motor axis for the Selene
* EXtended with special motor axis for the Selene
* guide, Mark Koennecke, February 2020
********************************************/
#ifndef pmacAxis_H
#define pmacAxis_H
#include "SINQController.h"
#include "SINQAxis.h"
#include "SINQController.h"
class pmacController;
class SeleneController;
class pmacAxis : public SINQAxis
{
class pmacAxis : public SINQAxis {
public:
/* These are the methods we override from the base class */
pmacAxis(pmacController *pController, int axisNo, bool enable=true);
virtual ~pmacAxis();
asynStatus move(double position, int relative, double min_velocity, double max_velocity, double acceleration);
asynStatus moveVelocity(double min_velocity, double max_velocity, double acceleration);
asynStatus home(double min_velocity, double max_velocity, double acceleration, int forwards);
asynStatus stop(double acceleration);
asynStatus poll(bool *moving);
asynStatus setPosition(double position);
asynStatus enable(int on);
/* These are the methods we override from the base class */
pmacAxis(pmacController *pController, int axisNo, bool enable = true);
virtual ~pmacAxis();
asynStatus move(double position, int relative, double min_velocity,
double max_velocity, double acceleration);
asynStatus moveVelocity(double min_velocity, double max_velocity,
double acceleration);
asynStatus home(double min_velocity, double max_velocity,
double acceleration, int forwards);
asynStatus stop(double acceleration);
asynStatus poll(bool *moving);
asynStatus setPosition(double position);
asynStatus enable(int on);
protected:
pmacController *pC_;
asynStatus getAxisStatus(bool *moving);
asynStatus getAxisInitialStatus(void);
protected:
pmacController *pC_;
double setpointPosition_;
double encoderPosition_;
double currentVelocity_;
double velocity_;
double accel_;
double highLimit_;
double lowLimit_;
double scale_;
double previous_position_;
int previous_direction_;
int encoder_axis_;
int axisErrorCount;
asynStatus getAxisStatus(bool *moving);
asynStatus getAxisInitialStatus(void);
time_t startTime;
time_t status6Time;
int starting;
int homing;
double statusPos;
double setpointPosition_;
double encoderPosition_;
double currentVelocity_;
double velocity_;
double accel_;
double highLimit_;
double lowLimit_;
double scale_;
double previous_position_;
int previous_direction_;
int encoder_axis_;
int axisErrorCount;
time_t next_poll;
time_t startTime;
time_t status6Time;
int starting;
int homing;
double statusPos;
bool autoEnable;
friend class pmacController;
friend class pmacV3Controller;
time_t next_poll;
bool autoEnable;
friend class pmacController;
friend class pmacV3Controller;
};
/*----------------------------------------------------------------------------------------------*/
class pmacHRPTAxis : public pmacAxis
{
public:
pmacHRPTAxis(pmacController *pController, int axisNo) : pmacAxis(pController,axisNo) {};
/**
* Override getAxisStatus in order to read the special parameter indicating a
* slit blade crash at HRPT
*/
asynStatus getAxisStatus(bool *moving);
protected:
friend class pmacController;
};
/*--------------------------------------------------------------------------------------------*/
class SeleneAxis : public pmacAxis
{
class SeleneAxis : public pmacAxis {
public:
SeleneAxis(SeleneController *pController, int axisNo, double limitTarget);
asynStatus move(double position, int relative, double min_velocity, double max_velocity, double acceleration);
asynStatus setPosition(double position);
asynStatus home(double min_velocity, double max_velocity, double acceleration, int forwards);
SeleneAxis(SeleneController *pController, int axisNo, double limitTarget);
asynStatus move(double position, int relative, double min_velocity,
double max_velocity, double acceleration);
asynStatus setPosition(double position);
asynStatus home(double min_velocity, double max_velocity,
double acceleration, int forwards);
protected:
friend class SeleneController;
friend class pmacController;
private:
double limitTarget;
asynStatus getSeleneAxisInitialStatus(void);
protected:
friend class SeleneController;
friend class pmacController;
private:
double limitTarget;
asynStatus getSeleneAxisInitialStatus(void);
};
/*
Yet another special set of motors for the Selene Guide at AMOR. Each segment can be lifted or tilted. This is
two motors. One acts as a slave and only writes a new target, the other also sets a new target and sends the
actual movement command. Both motors are coordianted in the motor controller in order to avoid tension on
the guide elements. This gaves rise to the function code LIFTSLAVE and LIFTMASTER.
Yet another special set of motors for the Selene Guide at AMOR. Each segment
can be lifted or tilted. This is two motors. One acts as a slave and only
writes a new target, the other also sets a new target and sends the actual
movement command. Both motors are coordianted in the motor controller in order
to avoid tension on the guide elements. This gaves rise to the function code
LIFTSLAVE and LIFTMASTER.
In another mode the whole guide can be lifted or tilted. Then motor 1 and 6 get new values and one of them
sends the drive command. This causes all 6 motors to drive synchronously to their new targets. This is
implemented through the LIFTSEGMENT function code.
In another mode the whole guide can be lifted or tilted. Then motor 1 and 6
get new values and one of them sends the drive command. This causes all 6
motors to drive synchronously to their new targets. This is implemented
through the LIFTSEGMENT function code.
Mark Koennecke, February 2020
Mark Koennecke, February 2020
The axis should not be enabled automatically
Michele Brambilla, February 2020
Michele Brambilla, February 2020
*/
class LiftAxis : public pmacAxis
{
public:
LiftAxis(pmacController *pController, int axisNo) : pmacAxis((pmacController *)pController,axisNo) {};
asynStatus move(double position, int relative, double min_velocity, double max_velocity, double acceleration);
asynStatus poll(bool *moving);
asynStatus stop(double acceleration);
private:
int waitStart;
class LiftAxis : public pmacAxis {
public:
/*
The default constructor of LiftAxis just forwards to the pmacAxis
constructor, which has an optional argument "autoenable" with the default
value "true". However, we want that argument to be false, hence we provide
an explicit constructor.
*/
LiftAxis(pmacController *pController, int axisNo)
: pmacAxis((pmacController *)pController, axisNo, false) {};
asynStatus move(double position, int relative, double min_velocity,
double max_velocity, double acceleration);
asynStatus poll(bool *moving);
asynStatus stop(double acceleration);
private:
int waitStart;
};
/********************************************
/********************************************
* Protocol version 3 requires just some minor change
*
* Michele Brambilla, February 2022
********************************************/
class pmacV3Axis : public pmacAxis {
public:
public:
pmacV3Axis(pmacController *pController, int axisNo);
pmacV3Axis(pmacController *pController, int axisNo);
asynStatus move(double position, int relative, double min_velocity,
double max_velocity, double acceleration);
asynStatus poll(bool *moving);
asynStatus move(double position, int relative, double min_velocity,
double max_velocity, double acceleration);
asynStatus poll(bool *moving);
protected:
int IsEnable;
double Speed;
protected:
int IsEnable;
double Speed;
asynStatus getAxisStatus(bool *moving);
asynStatus getAxisStatus(bool *moving);
friend class pmacController;
friend class pmacV3Controller;
};
friend class pmacController;
friend class pmacV3Controller;
};
/*----------------------------------------------------------------------------------------------*/
class pmacHRPTAxis : public pmacV3Axis {
public:
pmacHRPTAxis(pmacController *pController, int axisNo)
: pmacV3Axis(pController, axisNo) {};
/**
* Override getAxisStatus in order to read the special parameter indicating
* a slit blade crash at HRPT
*/
asynStatus getAxisStatus(bool *moving);
protected:
friend class pmacController;
};
/*
* Special motors for the AMOR detector movement. The whole
* command set is different but on a pmac controller. This implements
* a coordinated movement of cox, coz and ftz in order not to break
* the flight tube which may have been mounted. This is mapped to three
* motors selected via the function code: com, the detector omega, coz,
* the detector z offset and park, for parking the flightpath.
*/
#define ADCOM 1
#define ADCOZ 2
#define ADPARK 3
class AmorDetectorAxis : public pmacAxis {
public:
AmorDetectorAxis(pmacController *pController, int axisNo, int function);
asynStatus move(double position, int relative, double min_velocity,
double max_velocity, double acceleration);
asynStatus poll(bool *moving);
asynStatus moveVelocity(double min_velocity, double max_velocity,
double acceleration);
asynStatus home(double min_velocity, double max_velocity,
double acceleration, int forwards);
asynStatus stop(double acceleration);
asynStatus setPosition(double position);
protected:
int _function;
int det_starting;
time_t det_startTime;
};
/*----------------------------------------------------------------------------------------------*/
class GirderAxis : public pmacV3Axis {
public:
GirderAxis(pmacController *pController, int axisNo);
asynStatus move(double position, int relative, double min_velocity,
double max_velocity, double acceleration);
asynStatus stop(double acceleration);
asynStatus poll(bool *moving);
protected:
int IsEnable;
friend class pmacController;
friend class pmacV3Controller;
};
#endif /* pmacAxis_H */

File diff suppressed because it is too large Load Diff

View File

@ -140,6 +140,8 @@ class pmacController : public SINQController {
friend class SeleneAxis;
friend class LiftAxis;
friend class pmacV3Axis;
friend class GirderAxis;
friend class AmorDetectorAxis;
};
#define NUM_PMAC_PARAMS (&LAST_PMAC_PARAM - &FIRST_PMAC_PARAM + 1)
@ -166,12 +168,14 @@ class SeleneController : public pmacController {
#define EnableAxisString "ENABLE_AXIS"
#define AxisEnabledString "AXIS_ENABLED"
#define ReReadEncoderPositionString "REREAD_ENCODER_POSITION"
#define ReReadEncoderPositionString_RBV "REREAD_ENCODER_POSITION_RBV"
class pmacV3Controller : public pmacController {
public:
pmacV3Controller(const char *portName, const char *lowLevelPortName,
int lowLevelPortAddress, int numAxes, double movingPollPeriod,
double idlePollPeriod, const int &extraParams = 2);
double idlePollPeriod, const int &extraParams = 4);
// overloaded because we want to enable/disable the motor
asynStatus writeInt32(asynUser *pasynUser, epicsInt32 value);
@ -181,12 +185,15 @@ public:
friend class pmacV3Axis;
friend class pmacAxis;
friend class GirderAxis;
protected:
pmacV3Axis **pAxes_; /**< Array of pointers to axis objects */
int enableAxis_;
int axisEnabled_;
int rereadEncoderPosition_;
int rereadEncoderPositionRBV_;
};
#endif /* pmacController_H */

View File

@ -5,8 +5,11 @@ registrar(EL734Register)
registrar(PhytronRegister)
registrar(EuroMoveRegister)
registrar(NanotecRegister)
registrar(pmacControllerRegister)
registrar(pmacAsynIPPortRegister)
# registrar(pmacControllerRegister)
registrar(C804ControllerRegister)
# registrar(pmacAsynIPPortRegister)
registrar(MasterMACSRegister)
registrar(SINQControllerRegister)
#--------------------------------------------------------
# With the PSI module build system, including these items actually

View File

@ -0,0 +1,23 @@
record(motor,"$(P)$(M)")
{
field(DESC,"$(DESC)")
field(DTYP,"$(DTYP)")
field(DIR,"$(DIR)")
field(VELO,"$(VELO)")
field(VBAS,"$(VBAS)")
field(ACCL,"$(ACCL)")
field(BDST,"$(BDST)")
field(BVEL,"$(BVEL)")
field(BACC,"$(BACC)")
field(OUT,"@asyn($(PORT),$(ADDR))")
field(MRES,"$(MRES)")
field(PREC,"$(PREC)")
field(EGU,"$(EGU)")
field(DHLM,"$(DHLM)")
field(DLLM,"$(DLLM)")
field(INIT,"$(INIT)")
field(TWV,"1")
field(RDBD,"$(RDBD)")
field(RTRY,"0")
}

View File

@ -0,0 +1,21 @@
grecord(motor,"$(P)$(M)")
{
field(DESC,"$(DESC)")
field(DTYP,"$(DTYP)")
field(DIR,"$(DIR)")
field(VELO,"$(VELO)")
field(VBAS,"$(VBAS)")
field(ACCL,"$(ACCL)")
field(BDST,"$(BDST)")
field(BVEL,"$(BVEL)")
field(BACC,"$(BACC)")
field(OUT,"#C$(C) S$(S) @")
field(MRES,"$(MRES)")
field(PREC,"$(PREC)")
field(EGU,"$(EGU)")
field(DHLM,"$(DHLM)")
field(DLLM,"$(DLLM)")
field(INIT,"$(INIT)")
field(TWV,"1")
}

View File

@ -0,0 +1,18 @@
# The message text
record(waveform, "$(P)$(M)-MsgTxt") {
field(DTYP, "asynOctetRead")
field(INP, "@asyn($(PORT),$(N),1) MOTOR_MESSAGE_TEXT")
field(FTVL, "CHAR")
field(NELM, "80")
field(SCAN, "I/O Intr")
}
record(ao,"$(P)m$(N)-Resolution"){
field(DESC,"m$(N) Resolution")
field(DOL,"$(P)m$(N).MRES CP MS")
field(OMSL,"closed_loop")
field(DTYP,"asynFloat64")
field(OUT,"@asyn($(PORT),$(N))MOTOR_RESOLUTION")
field(PREC,"3")
}

View File

@ -0,0 +1,48 @@
record(motor,"$(P)$(M)")
{
field(DESC,"$(DESC)")
field(DTYP,"$(DTYP)")
field(DIR,"$(DIR)")
field(VELO,"$(VELO)")
field(VMAX,"$(VMAX=$(VELO))")
field(VBAS,"$(VBAS)")
field(ACCL,"$(ACCL)")
field(BDST,"$(BDST)")
field(BVEL,"$(BVEL)")
field(BACC,"$(BACC)")
field(OUT,"@asyn($(PORT),$(ADDR))")
field(MRES,"$(MRES)")
field(PREC,"$(PREC)")
field(EGU,"$(EGU)")
field(DHLM,"$(DHLM)")
field(DLLM,"$(DLLM)")
field(INIT,"$(INIT)")
field(TWV,"1")
field(RDBD,"$(RDBD)")
}
# The message text
record(waveform, "$(P)$(M)-MsgTxt") {
field(DTYP, "asynOctetRead")
field(INP, "@asyn($(PORT),$(N),1) MOTOR_MESSAGE_TEXT")
field(FTVL, "CHAR")
field(NELM, "80")
field(SCAN, "I/O Intr")
}
# enable axis
record(longout, "$(P)$(M):Enable") {
field(DTYP, "asynInt32")
field(OUT, "@asyn($(PORT),$(N),1) ENABLE_AXIS")
field(PINI, "YES")
}
# enable axis
record(longin, "$(P)$(M):Enable_RBV") {
field(DTYP, "asynInt32")
field(INP, "@asyn($(PORT),$(N),1) AXIS_ENABLED")
field(PINI, "YES")
field(SCAN, "1 second")
}

13
testel734/mota.sub Normal file
View File

@ -0,0 +1,13 @@
file "$(TOP)/db/basic_asyn_motor.db"
{
pattern
{P, N, M, DTYP, PORT, ADDR, DESC, EGU, DIR, VELO, VBAS, ACCL, BDST, BVEL, BACC, MRES, PREC, DLLM, DHLM, INIT}
{KM36:mota:, 2, "m$(N)", "asynMotor", mota, 2, "sgl", degree, Pos, 2.0, 0.1, .2, 0, 1, .2, 1., 3, -20.0, 20.0, ""}
}
file "$(TOP)/db/motorMessage.db"
{
pattern
{P,N, M,PORT}
{KM36:mota:, 2, "m$(N)",mota}
}

12
testel734/motaspeed.sub Normal file
View File

@ -0,0 +1,12 @@
file "$(TOP)/db/sinq_asyn_motor.db"
{
pattern
{P, N, M, DTYP, PORT, ADDR, DESC, EGU, DIR, VELO, VBAS, ACCL, BDST, BVEL, BACC, MRES, PREC, DLLM, DHLM, INIT, RDBD}
{KM36:mota:,2,"m$(N)","asynMotor", mota, 2, "sgl", degree, Pos, 100, 0.1, .2, 0, 1, .2, 1., 3,-180.0,360.0,"", 0.2}}
file "$(TOP)/db/motorMessage.db"
{
pattern
{P,N, M,PORT}
{KM36:mota:, 2, "m$(N)",mota}
}

12
testel734/testel734.cmd Executable file
View File

@ -0,0 +1,12 @@
#!/usr/local/bin/iocsh
require sinq,koennecke
epicsEnvSet("TOP","/afs/psi.ch/project/sinqdev/sinqepicsapp/testel734")
drvAsynIPPortConfigure("serial1", "localhost:61000",0,0,0)
EL734CreateController("mota","serial1",3)
#dbLoadTemplate "mota.sub"
dbLoadTemplate "motaspeed.sub"

11
testel737.cmd Executable file
View File

@ -0,0 +1,11 @@
#!/usr/local/bin/iocsh
require sinq,koennecke
epicsEnvSet("TOP","/afs/psi.ch/project/sinqdev/sinqepicsapp")
#--------- load EL737 counter box
drvAsynIPPortConfigure("cter1","localhost:62000",0,0,0)
dbLoadRecords("$(TOP)/db/asynRecord.db","P=KM36:,R=cter1,PORT=cter1,ADDR=0,OMAX=80,IMAX=80")
dbLoadRecords("${TOP}/db/el737Record.db","P=KM36:counter,PORT=cter1,DESC=SANSCounter")

View File

@ -0,0 +1,9 @@
record(asyn,"$(P)$(R)")
{
field(DTYP,"asynRecordDevice")
field(PORT,"$(PORT)")
field(ADDR,"$(ADDR)")
field(OMAX,"$(OMAX)")
field(IMAX,"$(IMAX)")
}

View File

@ -0,0 +1,23 @@
record(motor,"$(P)$(M)")
{
field(DESC,"$(DESC)")
field(DTYP,"$(DTYP)")
field(DIR,"$(DIR)")
field(VELO,"$(VELO)")
field(VBAS,"$(VBAS)")
field(ACCL,"$(ACCL)")
field(BDST,"$(BDST)")
field(BVEL,"$(BVEL)")
field(BACC,"$(BACC)")
field(OUT,"@asyn($(PORT),$(ADDR))")
field(MRES,"$(MRES)")
field(PREC,"$(PREC)")
field(EGU,"$(EGU)")
field(DHLM,"$(DHLM)")
field(DLLM,"$(DLLM)")
field(INIT,"$(INIT)")
field(TWV,"1")
field(RDBD,"$(RDBD)")
field(RTRY,"0")
}

View File

@ -0,0 +1,21 @@
grecord(motor,"$(P)$(M)")
{
field(DESC,"$(DESC)")
field(DTYP,"$(DTYP)")
field(DIR,"$(DIR)")
field(VELO,"$(VELO)")
field(VBAS,"$(VBAS)")
field(ACCL,"$(ACCL)")
field(BDST,"$(BDST)")
field(BVEL,"$(BVEL)")
field(BACC,"$(BACC)")
field(OUT,"#C$(C) S$(S) @")
field(MRES,"$(MRES)")
field(PREC,"$(PREC)")
field(EGU,"$(EGU)")
field(DHLM,"$(DHLM)")
field(DLLM,"$(DLLM)")
field(INIT,"$(INIT)")
field(TWV,"1")
}

View File

@ -0,0 +1,18 @@
# The message text
record(waveform, "$(P)$(M)-MsgTxt") {
field(DTYP, "asynOctetRead")
field(INP, "@asyn($(PORT),$(N),1) MOTOR_MESSAGE_TEXT")
field(FTVL, "CHAR")
field(NELM, "80")
field(SCAN, "I/O Intr")
}
record(ao,"$(P)m$(N)-Resolution"){
field(DESC,"m$(N) Resolution")
field(DOL,"$(P)m$(N).MRES CP MS")
field(OMSL,"closed_loop")
field(DTYP,"asynFloat64")
field(OUT,"@asyn($(PORT),$(N))MOTOR_RESOLUTION")
field(PREC,"3")
}

14
testhuber/db/pmacV3.db Normal file
View File

@ -0,0 +1,14 @@
# enable axis
record(longout, "$(P)$(M):Enable") {
field(DTYP, "asynInt32")
field(OUT, "@asyn($(PORT),$(N),1) ENABLE_AXIS")
field(PINI, "YES")
}
# enable axis
record(longin, "$(P)$(M):Enable_RBV") {
field(DTYP, "asynInt32")
field(INP, "@asyn($(PORT),$(N),1) AXIS_ENABLED")
field(PINI, "YES")
field(SCAN, "1 second")
}

View File

@ -0,0 +1,47 @@
record(motor,"$(P)$(M)")
{
field(DESC,"$(DESC)")
field(DTYP,"$(DTYP)")
field(DIR,"$(DIR)")
field(VELO,"$(VELO)")
field(VBAS,"$(VBAS)")
field(ACCL,"$(ACCL)")
field(BDST,"$(BDST)")
field(BVEL,"$(BVEL)")
field(BACC,"$(BACC)")
field(OUT,"@asyn($(PORT),$(ADDR))")
field(MRES,"$(MRES)")
field(PREC,"$(PREC)")
field(EGU,"$(EGU)")
field(DHLM,"$(DHLM)")
field(DLLM,"$(DLLM)")
field(INIT,"$(INIT)")
field(TWV,"1")
field(RDBD,"$(RDBD)")
}
# The message text
record(waveform, "$(P)$(M)-MsgTxt") {
field(DTYP, "asynOctetRead")
field(INP, "@asyn($(PORT),$(N),1) MOTOR_MESSAGE_TEXT")
field(FTVL, "CHAR")
field(NELM, "80")
field(SCAN, "I/O Intr")
}
# enable axis
record(longout, "$(P)$(M):Enable") {
field(DTYP, "asynInt32")
field(OUT, "@asyn($(PORT),$(N),1) ENABLE_AXIS")
field(PINI, "YES")
}
# enable axis
record(longin, "$(P)$(M):Enable_RBV") {
field(DTYP, "asynInt32")
field(INP, "@asyn($(PORT),$(N),1) AXIS_ENABLED")
field(PINI, "YES")
field(SCAN, "5 second")
}

33
testhuber/huber.cmd Executable file
View File

@ -0,0 +1,33 @@
#!/ioc/tools/iocsh
require motorHuber,brambilla_m
# epicsEnvSet("TOP","/ioc/sinq-ioc/boa-ioc")
epicsEnvSet("TOP","/afs/psi.ch/project/sinqdev/sinqepicsapp/testhuber")
epicsEnvSet("STREAM_PROTOCOL_PATH","./db")
epicsEnvSet("MOTOR",".")
# require autosave, koennecke
# Configure IP Port
drvAsynIPPortConfigure("Huber1", "129.129.195.128:1234",0,0,0)
# Configure Controller
asynOctetSetInputEos( "Huber1", -1, "\r")
asynOctetSetOutputEos("Huber1", -1, "\r")
SMC9300CreateController("SMC1", "Huber1", 5, 50, 2000)
dbLoadTemplate "motor.substitutions.smc9300"
dbLoadRecords("$(TOP)/db/asynRecord.db","P=SQ:BOA:optics:,R=mcu,PORT=Huber1,ADDR=0,OMAX=80,IMAX=80")
#--------------------------------- restore autosave
#set_requestfile_path("$(TOP)", "")
#set_savefile_path("$(TOP)/autosave")
#save_restoreSet_Debug(0)
#set_pass0_restoreFile("$(TOP)/autosave/huber.sav","")
#set_pass1_restoreFile("$(TOP)/autosave/huber.sav","")

View File

@ -0,0 +1,19 @@
file "$(MOTOR)/db/basic_asyn_motor.db"
{
pattern
{P, N, M, DTYP, PORT, ADDR,DESC, EGU, DIR, VELO, VBAS, ACCL, BDST, BVEL, BACC, MRES, PREC, DHLM, DLLM, RDBD, INIT}
{SQ:BOA:optics:, 1, "trans1","asynMotor", SMC1, 1, "translation 1",mm, Pos, 200, 10, 20, 0, 1, .2, 0.0001, 4, 150, -150, 0.1, ""}
{SQ:BOA:optics:, 2, "lift1", "asynMotor", SMC1, 2, "lift 1", mm, Pos, 200, 10, 80, 0, 1, .2, 0.0001, 4, 100, 0, 0.1, ""}
{SQ:BOA:optics:, 3, "trans2","asynMotor", SMC1, 3, "translation 2",mm, Pos, 200, 10, 80, 0, 1, .2, 0.0001, 4, 150, -150, 0.1, ""}
{SQ:BOA:optics:, 4, "lift2", "asynMotor", SMC1, 4, "lift 2" ,mm, Pos, 200, 10, 80, 0, 1, .2, 0.0001, 4, 100, 0, 0.1, ""}
}
file "$(TOP)/db/motorMessage.db"
{
pattern
{P,N, M,PORT}
{SQ:BOA:optics:, 1, "trans1", SMC1}
{SQ:BOA:optics:, 2, "lift1", SMC1}
{SQ:BOA:optics:, 3, "trans2", SMC1}
{SQ:BOA:optics:, 4, "lift2", SMC1}
}

View File

@ -0,0 +1,9 @@
record(asyn,"$(P)$(R)")
{
field(DTYP,"asynRecordDevice")
field(PORT,"$(PORT)")
field(ADDR,"$(ADDR)")
field(OMAX,"$(OMAX)")
field(IMAX,"$(IMAX)")
}

View File

@ -0,0 +1,23 @@
record(motor,"$(P)$(M)")
{
field(DESC,"$(DESC)")
field(DTYP,"$(DTYP)")
field(DIR,"$(DIR)")
field(VELO,"$(VELO)")
field(VBAS,"$(VBAS)")
field(ACCL,"$(ACCL)")
field(BDST,"$(BDST)")
field(BVEL,"$(BVEL)")
field(BACC,"$(BACC)")
field(OUT,"@asyn($(PORT),$(ADDR))")
field(MRES,"$(MRES)")
field(PREC,"$(PREC)")
field(EGU,"$(EGU)")
field(DHLM,"$(DHLM)")
field(DLLM,"$(DLLM)")
field(INIT,"$(INIT)")
field(TWV,"1")
field(RDBD,"$(RDBD)")
field(RTRY,"0")
}

View File

@ -0,0 +1,21 @@
grecord(motor,"$(P)$(M)")
{
field(DESC,"$(DESC)")
field(DTYP,"$(DTYP)")
field(DIR,"$(DIR)")
field(VELO,"$(VELO)")
field(VBAS,"$(VBAS)")
field(ACCL,"$(ACCL)")
field(BDST,"$(BDST)")
field(BVEL,"$(BVEL)")
field(BACC,"$(BACC)")
field(OUT,"#C$(C) S$(S) @")
field(MRES,"$(MRES)")
field(PREC,"$(PREC)")
field(EGU,"$(EGU)")
field(DHLM,"$(DHLM)")
field(DLLM,"$(DLLM)")
field(INIT,"$(INIT)")
field(TWV,"1")
}

View File

@ -0,0 +1,18 @@
# The message text
record(waveform, "$(P)$(M)-MsgTxt") {
field(DTYP, "asynOctetRead")
field(INP, "@asyn($(PORT),$(N),1) MOTOR_MESSAGE_TEXT")
field(FTVL, "CHAR")
field(NELM, "80")
field(SCAN, "I/O Intr")
}
record(ao,"$(P)m$(N)-Resolution"){
field(DESC,"m$(N) Resolution")
field(DOL,"$(P)m$(N).MRES CP MS")
field(OMSL,"closed_loop")
field(DTYP,"asynFloat64")
field(OUT,"@asyn($(PORT),$(N))MOTOR_RESOLUTION")
field(PREC,"3")
}

14
testmmac/db/pmacV3.db Normal file
View File

@ -0,0 +1,14 @@
# enable axis
record(longout, "$(P)$(M):Enable") {
field(DTYP, "asynInt32")
field(OUT, "@asyn($(PORT),$(N),1) ENABLE_AXIS")
field(PINI, "YES")
}
# enable axis
record(longin, "$(P)$(M):Enable_RBV") {
field(DTYP, "asynInt32")
field(INP, "@asyn($(PORT),$(N),1) AXIS_ENABLED")
field(PINI, "YES")
field(SCAN, "1 second")
}

View File

@ -0,0 +1,47 @@
record(motor,"$(P)$(M)")
{
field(DESC,"$(DESC)")
field(DTYP,"$(DTYP)")
field(DIR,"$(DIR)")
field(VELO,"$(VELO)")
field(VBAS,"$(VBAS)")
field(ACCL,"$(ACCL)")
field(BDST,"$(BDST)")
field(BVEL,"$(BVEL)")
field(BACC,"$(BACC)")
field(OUT,"@asyn($(PORT),$(ADDR))")
field(MRES,"$(MRES)")
field(PREC,"$(PREC)")
field(EGU,"$(EGU)")
field(DHLM,"$(DHLM)")
field(DLLM,"$(DLLM)")
field(INIT,"$(INIT)")
field(TWV,"1")
field(RDBD,"$(RDBD)")
}
# The message text
record(waveform, "$(P)$(M)-MsgTxt") {
field(DTYP, "asynOctetRead")
field(INP, "@asyn($(PORT),$(N),1) MOTOR_MESSAGE_TEXT")
field(FTVL, "CHAR")
field(NELM, "80")
field(SCAN, "I/O Intr")
}
# enable axis
record(longout, "$(P)$(M):Enable") {
field(DTYP, "asynInt32")
field(OUT, "@asyn($(PORT),$(N),1) ENABLE_AXIS")
field(PINI, "YES")
}
# enable axis
record(longin, "$(P)$(M):Enable_RBV") {
field(DTYP, "asynInt32")
field(INP, "@asyn($(PORT),$(N),1) AXIS_ENABLED")
field(PINI, "YES")
field(SCAN, "5 second")
}

15
testmmac/mmacs.sub Normal file
View File

@ -0,0 +1,15 @@
file "$(TOP)/db/basic_asyn_motor.db"
{
pattern
{P, N, M, DTYP, PORT, ADDR, DESC, EGU, DIR, VELO, VBAS, ACCL, BDST, BVEL, BACC, MRES, PREC, DLLM, DHLM, INIT}
{KM36:mota:, 5, "m$(N)", "asynMotor", mota, 5, "m5", degree, Pos, 2.0, 0.1, .2, 0, 1, .2, 1., 3, -20.0, 20.0, ""}
{KM36:mota:, 6, "m$(N)", "asynMotor", mota, 6, "m6", degree, Pos, 2.0, 0.1, .2, 0, 1, .2, 1., 3, -20.0, 20.0, ""}
}
file "$(TOP)/db/motorMessage.db"
{
pattern
{P,N, M,PORT}
{KM36:mota:, 5, "m$(N)",mota}
{KM36:mota:, 6, "m$(N)",mota}
}

8
testmmac/mmacs2.sub Normal file
View File

@ -0,0 +1,8 @@
file "$(TOP)/db/sinq_asyn_motor.db"
{
pattern
{P, N, M, DTYP, PORT, ADDR, DESC, EGU, DIR, VELO, VBAS, ACCL, BDST, BVEL, BACC, MRES, PREC, DLLM, DHLM, RDBD, INIT}
{KM36:mota:,5,"m$(N)","asynMotor", mota, 5, "m5", degree, Pos, 2.0, 0.1, .2, 0, 1, .2, .001, 3,-400000.000,400000.000,.01, ""}
{KM36:mota:,6,"m$(N)","asynMotor", mota, 6, "m6", degree, Pos, 2.0, 0.1, .2, 0, 1, .2, .001, 3,-200000.000,200000.000,.01, ""}
}

13
testmmac/mota.sub Normal file
View File

@ -0,0 +1,13 @@
file "$(TOP)/db/basic_asyn_motor.db"
{
pattern
{P, N, M, DTYP, PORT, ADDR, DESC, EGU, DIR, VELO, VBAS, ACCL, BDST, BVEL, BACC, MRES, PREC, DLLM, DHLM, INIT}
{KM36:mota:, 2, "m$(N)", "asynMotor", mota, 2, "sgl", degree, Pos, 2.0, 0.1, .2, 0, 1, .2, 1., 3, -20.0, 20.0, ""}
}
file "$(TOP)/db/motorMessage.db"
{
pattern
{P,N, M,PORT}
{KM36:mota:, 2, "m$(N)",mota}
}

12
testmmac/motaspeed.sub Normal file
View File

@ -0,0 +1,12 @@
file "$(TOP)/db/sinq_asyn_motor.db"
{
pattern
{P, N, M, DTYP, PORT, ADDR, DESC, EGU, DIR, VELO, VBAS, ACCL, BDST, BVEL, BACC, MRES, PREC, DLLM, DHLM, INIT, RDBD}
{KM36:mota:,2,"m$(N)","asynMotor", mota, 2, "sgl", degree, Pos, 100, 0.1, .2, 0, 1, .2, 1., 3,-180.0,360.0,"", 0.2}}
file "$(TOP)/db/motorMessage.db"
{
pattern
{P,N, M,PORT}
{KM36:mota:, 2, "m$(N)",mota}
}

35
testmmac/testmmacs.cmd Executable file
View File

@ -0,0 +1,35 @@
#!/usr/local/bin/iocsh
require sinq,koennecke
require autosave,koennecke
epicsEnvSet("TOP","/afs/psi.ch/project/sinqdev/sinqepicsapp/testmmac")
set_requestfile_path("$(TOP)", "")
set_savefile_path("$(TOP)/autosave")
save_restoreSet_Debug(0)
#--------------------------------- restore autosave
set_pass0_restoreFile("$(TOP)/autosave/test.sav","")
set_pass1_restoreFile("$(TOP)/autosave/test.sav","")
#drvAsynMMACSPortConfigure("macs1", "localhost:8080",0,0,0)
#asynInterposeEosConfig("macs1", 0, 0, 0)
#dbLoadRecords("$(TOP)/db/asynRecord.db","P=KM36:,R=macs1,PORT=macs1,ADDR=0,OMAX=80,IMAX=80")
#asynSetTraceMask("macs1", 0, 255)
#asynSetTraceMask("macs1", 0, 255)
#drvAsynIPPortConfigure("macs1", "localhost:8080",0,0,0)
drvAsynIPPortConfigure("macs1", "Marcel1--MACS:1917")
MasterMACSCreateController("mota","macs1",7, 500, 10000)
MasterMACSCreateAxis("mota",5)
MasterMACSCreateAxis("mota",6)
dbLoadTemplate "mmacs2.sub"
iocInit()
setMovingPollPeriod("mota", .5)
setIdlePollPeriod("mota", 10. )
create_monitor_set("test.req", 10, "")

View File

@ -0,0 +1,9 @@
record(asyn,"$(P)$(R)")
{
field(DTYP,"asynRecordDevice")
field(PORT,"$(PORT)")
field(ADDR,"$(ADDR)")
field(OMAX,"$(OMAX)")
field(IMAX,"$(IMAX)")
}

View File

@ -0,0 +1,22 @@
record(motor,"$(P)$(M)")
{
field(DESC,"$(DESC)")
field(DTYP,"$(DTYP)")
field(DIR,"$(DIR)")
field(VELO,"$(VELO)")
field(VBAS,"$(VBAS)")
field(ACCL,"$(ACCL)")
field(BDST,"$(BDST)")
field(BVEL,"$(BVEL)")
field(BACC,"$(BACC)")
field(OUT,"@asyn($(PORT),$(ADDR))")
field(MRES,"$(MRES)")
field(PREC,"$(PREC)")
field(EGU,"$(EGU)")
field(DHLM,"$(DHLM)")
field(DLLM,"$(DLLM)")
field(INIT,"$(INIT)")
field(TWV,"1")
field(RTRY,"0")
}

19544
testpmacV3/db/huber.dbd Normal file

File diff suppressed because it is too large Load Diff

View File

@ -0,0 +1,9 @@
# The message text
record(waveform, "$(P)$(M)-MsgTxt") {
field(DTYP, "asynOctetRead")
field(INP, "@asyn($(PORT),$(N),1) MOTOR_MESSAGE_TEXT")
field(FTVL, "CHAR")
field(NELM, "80")
field(SCAN, "I/O Intr")
}

14
testpmacV3/db/pmacV3.db Normal file
View File

@ -0,0 +1,14 @@
# enable axis
record(longout, "$(P)$(M):Enable") {
field(DTYP, "asynInt32")
field(OUT, "@asyn($(PORT),$(N),1) ENABLE_AXIS")
field(PINI, "YES")
}
# enable axis
record(longin, "$(P)$(M):Enable_RBV") {
field(DTYP, "asynInt32")
field(INP, "@asyn($(PORT),$(N),1) AXIS_ENABLED")
field(PINI, "YES")
field(SCAN, "1 second")
}

View File

@ -0,0 +1,27 @@
file "$(TOP)/db/basic_asyn_motor.db"
{
pattern
{P, N, M, DTYP, PORT, ADDR, DESC, EGU, DIR, VELO, VBAS, ACCL, BDST, BVEL, BACC, MRES, PREC, DHLM, DLLM, INIT}
{SQ:TEST:mcu:, 2, "m$(N)", "asynMotor", mcu, 2, "m$(N)", degrees, Pos, 2.0, 0.1, .2, 0, 1, .2, .001, 3, 358., 2., ""}
{SQ:TEST:mcu:, 6, "m$(N)", "asynMotor", mcu, 6, "m$(N)", degrees, Pos, 2.0, 0.1, .2, 0, 1, .2, .001, 3, 358., 2., ""}
{SQ:TEST:mcu:,10, "m$(N)", "asynMotor", mcu,10, "m$(N)", degrees, Pos, 2.0, 0.1, .2, 0, 1, .2, .001, 3, 358., 2., ""}
}
file "$(TOP)/db/motorMessage.db"
{
pattern
{P,N, M,PORT}
{SQ:TEST:mcu:, 2, "m$(N)", mcu}
{SQ:TEST:mcu:, 6, "m$(N)", mcu}
{SQ:TEST:mcu:,10, "m$(N)", mcu}
}
file "$(TOP)/db/pmacV3.db"
{
pattern
{P,N, M,PORT}
{SQ:TEST:mcu:, 2, "m$(N)", mcu}
{SQ:TEST:mcu:, 6, "m$(N)", mcu}
{SQ:TEST:mcu:,10, "m$(N)", mcu}
}

25
testpmacV3/st.cmd Executable file
View File

@ -0,0 +1,25 @@
#!/ioc/tools/iocsh
require sinq,brambilla_m
#require sinq,koennecke
epicsEnvSet("TOP","/afs/psi.ch/project/sinqdev/sinqepicsapp/testpmacV3")
epicsEnvSet("EPICS_CA_ADDR_LIST","127.0.0.1")
epicsEnvSet("STREAM_PROTOCOL_PATH","$(TOP)/db")
var streamDebug 3
# motors
pmacAsynIPConfigure("pmacV3", "129.129.138.234:1025")
pmacV3CreateController("mcu","pmacV3",0,16,50,1000);
pmacV3CreateAxis("mcu",2,0);
pmacV3CreateAxis("mcu",6,0);
pmacV3CreateAxis("mcu",10,0);
dbLoadTemplate "$(TOP)/mcu.substitutions"
dbLoadRecords("$(TOP)/db/asynRecord.db","P=SQ:TEST:,R=mcu,PORT=pmacV3,ADDR=0,OMAX=80,IMAX=80")
iocInit

56
utils/convertsub.py Executable file
View File

@ -0,0 +1,56 @@
#!/usr/bin/python
"""
This is a little program which generates soft motor initialisation lines and iocsh morsels
from a motor substitutions file.
Mark Koennecke, December 2019
"""
import sys
import pdb
import os
if len(sys.argv) < 2:
print('Usage:\n\tconvertsub XXXX.substitutions')
sys.exit(1)
subLen = 0
motors = []
port = None
with open(sys.argv[1],'r') as fin:
line = fin.readline()
while line:
line = line.replace(' ','')
line = line.strip('{}')
l = line.split(',')
if line.find('DHLM') > 0: # that is the index discovery line
lowlimidx = l.index('DLLM')
highlimidx = l.index('DHLM')
mresidx = l.index('MRES')
addridx = l.index('ADDR')
prefix = l[0]
subLen = len(l)
portIdx = l.index('PORT')
elif subLen > 0:
l = line.split(',')
if subLen == len(l):
motors.append((l[addridx],l[mresidx],l[lowlimidx],l[highlimidx]))
port = l[portIdx]
else:
pass
line = fin.readline()
print('%d motors found' % len(motors))
fname = os.path.splitext(sys.argv[1])[0] + '.cmd'
out = open(fname,'w')
out.write('motorSimCreateController(\"%s\",%d)\n' % (port, len(motors)+2))
for mot in motors:
mres = float(mot[1])
highlim = int(float(mot[3])/mres)
lowlim = int(float(mot[2])/mres)
out.write('motorSimConfigAxis(\"%s\",%s,%d,%d,0,0)\n' %(port,mot[0],highlim,lowlim))
out.write('dbLoadTemplate(\"$(TOP)/%s\")\n' % (sys.argv[1]))
out.close()
print(fname + ' written')

213
utils/decodeMasterMACStatusR10.py Executable file
View File

@ -0,0 +1,213 @@
#!/usr/bin/env python3
# List of tuples which encodes the states given in the file description.
# Index first with the bit index, then with the bit value
interpretation = [
("Not ready to be switched on", "Ready to be switched on"), # Bit 0
("Not switched on", "Switched on"), # Bit 1
("Disabled", "Enabled"), # Bit 2
("Ok", "Fault condition set"), # Bit 3
("Motor supply voltage absent ", "Motor supply voltage present"), # Bit 4
("Motor performs quick stop", "Ok"), # Bit 5
("Switch on enabled", "Switch on disabled"), # Bit 6
("Ok", "RWarning: Movement function was called while motor is still moving. The function call is ignored"), # Bit 7
("Motor is idle", "Motor is currently moving"), # Bit 8
("Motor does not execute command messages (local mode)", "Motor does execute command messages (remote mode)"), # Bit 9
("Target not reached", "Target reached"), # Bit 10
("Ok", "Internal limit active"), # Bit 11
("Not specified", "Not specified"), # Bit 12
("Not specified", "Not specified"), # Bit 13
("No event set or event has not occurred yet", "Set event has occurred"), # Bit 14
("Axis off (power disabled)", "Axis on (power enabled)"), # Bit 15
]
def decode(value, big_endian: bool = False):
interpreted = []
bit_list = [(value >> shift_ind) & 1
for shift_ind in range(value.bit_length())] # little endian
if big_endian:
bit_list.reverse() # big endian
for (bit, interpretations) in zip(bit_list, interpretation):
interpreted.append(interpretations[bit])
return (bit_list, interpreted)
def print_decoded(bit_list, interpreted):
for (idx, (bit_value, msg)) in enumerate(zip(bit_list, interpreted)):
print(f"Bit {idx} = {bit_value}: {msg}")
def interactive():
# Imported here, because curses is not available in Windows. Using the
# interactive mode therefore fails on Windows, but at least the single
# command mode can be used (which would not be possible if we would import
# curses at the top level)
import curses
stdscr = curses.initscr()
curses.noecho()
curses.cbreak()
stdscr.keypad(True)
stdscr.scrollok(True)
stdscr.addstr(">> ")
stdscr.refresh()
history = [""]
ptr = len(history) - 1
while True:
c = stdscr.getch()
if c == curses.KEY_RIGHT:
(y, x) = stdscr.getyx()
if x < len(history[ptr]) + 3:
stdscr.move(y, x+1)
stdscr.refresh()
elif c == curses.KEY_LEFT:
(y, x) = stdscr.getyx()
if x > 3:
stdscr.move(y, x-1)
stdscr.refresh()
elif c == curses.KEY_UP:
if ptr > 0:
ptr -= 1
stdscr.addch("\r")
stdscr.clrtoeol()
stdscr.addstr(">> " + history[ptr])
elif c == curses.KEY_DOWN:
if ptr < len(history) - 1:
ptr += 1
stdscr.addch("\r")
stdscr.clrtoeol()
stdscr.addstr(">> " + history[ptr])
elif c == curses.KEY_ENTER or c == ord('\n') or c == ord('\r'):
if history[ptr] == 'quit':
break
# because of arrow keys move back to the end of the line
(y, x) = stdscr.getyx()
stdscr.move(y, 3+len(history[ptr]))
if history[ptr]:
result = interpret_inputs(history[ptr].split())
if result is None:
stdscr.addstr(f"\nBAD INPUT: Expected input of 'value [big_endian]', where 'value' is an int or a float and 'big_endian' is an optional boolean argument.")
else:
(arg, big_endian) = result
(bit_list, interpreted) = decode(arg, big_endian)
for (idx, (bit_value, msg)) in enumerate(zip(bit_list, interpreted)):
stdscr.addstr(f"\nBit {idx} = {bit_value}: {msg}")
stdscr.refresh()
if ptr == len(history) - 1 and history[ptr] != "":
history += [""]
else:
history[-1] = ""
ptr = len(history) - 1
stdscr.addstr("\n>> ")
stdscr.refresh()
else:
if ptr < len(history) - 1: # Modifying previous input
if len(history[-1]) == 0:
history[-1] = history[ptr]
ptr = len(history) - 1
else:
history += [history[ptr]]
ptr = len(history) - 1
if c == curses.KEY_BACKSPACE:
if len(history[ptr]) == 0:
continue
(y, x) = stdscr.getyx()
history[ptr] = history[ptr][0:x-4] + history[ptr][x-3:]
stdscr.addch("\r")
stdscr.clrtoeol()
stdscr.addstr(">> " + history[ptr])
stdscr.move(y, x-1)
stdscr.refresh()
else:
(y, x) = stdscr.getyx()
history[ptr] = history[ptr][0:x-3] + chr(c) + history[ptr][x-3:]
stdscr.addch("\r")
stdscr.clrtoeol()
stdscr.addstr(">> " + history[ptr])
stdscr.move(y, x+1)
stdscr.refresh()
# to quit
curses.nocbreak()
stdscr.keypad(False)
curses.echo()
curses.endwin()
def interpret_inputs(inputs):
number = None
big_endian = False
try:
number = int(float(inputs[0]))
if len(inputs) > 1:
second_arg = inputs[1]
if second_arg == "True" or second_arg == "true":
big_endian = True
elif second_arg == "False" or second_arg == "false":
big_endian = False
else:
big_endian = bool(int(second_arg))
return (number, big_endian)
except:
return None
if __name__ == "__main__":
from sys import argv
if len(argv) == 1:
# Start interactive mode
interactive()
else:
result = interpret_inputs(argv[1:])
if result is None:
print("""
Decode R10 message of MasterMACs
------------------
MasterMACs returns its status message (R10) as a floating-point number.
The bits of this float encode different states. These states are stored
in the interpretation variable.
This script can be used in two different ways:
Option 1: Single Command
------------------------
Usage: decodeMasterMACStatusR10.py value [big_endian]
'value' is the return value of a R10 command. This value is interpreted
bit-wise and the result is printed out. The optional second argument can
be used to specify whether the input value needs to be interpreted as
little or big endian. Default is False.
Option 2: CLI Mode
------------------
Usage: decodeMasterMACStatusR10.py
A prompt will be opened. Type in the return value of a R10 command, hit
enter and the interpretation will be printed in the prompt. After that,
the next value can be typed in. Type 'quit' to close the prompt.
""")
else:
print("Motor status")
print("============")
(arg, big_endian) = result
(bit_list, interpreted) = decode(arg, big_endian)
print_decoded(bit_list, interpreted)

178
utils/deltatau.py Executable file
View File

@ -0,0 +1,178 @@
#!/usr/bin/env python3
"""
This is some code for communicating with a Delta-Tau PMAC motor
controller as used at SINQ from python. Python 3 is assumed and
tested. We use the ethernet protocol of the Pmac. This code benefits
from the SICS driver for Pmac which in turn benefits from the driver
written by Pete Leicester at Diamond.
Usage: python36 deltatau.py pmachost:port command
Then returns the response for command.
Mark Koennecke, October 2019
"""
import struct
import socket
import curses
def packPmacCommand(command):
# 0x40 = VR_DOWNLOAD
# 0xBF = VR_PMAC_GETRESPONSE
buf = struct.pack('BBHHH',0x40,0xBF,0,0,socket.htons(len(command)))
buf = buf + bytes(command,'utf-8')
return buf
def readPmacReply(input):
msg = bytearray()
expectAck = True
while True:
b = input.recv(1)
bint = int.from_bytes(b,byteorder='little')
if bint == 2 or bint == 7: #STX or BELL
expectAck = False
continue
if expectAck and bint == 6: # ACK
return bytes(msg)
else:
if bint == 13 and not expectAck: # CR
return bytes(msg)
else:
msg.append(bint)
if __name__ == "__main__":
from sys import argv
try:
addr = argv[1].split(':')
s = socket.socket(socket.AF_INET,socket.SOCK_STREAM)
s.connect((addr[0],int(addr[1])))
if len(argv) == 3:
buf = packPmacCommand(argv[2])
s.send(buf)
reply = readPmacReply(s)
print(reply.decode('utf-8') + '\n')
else:
try:
stdscr = curses.initscr()
curses.noecho()
curses.cbreak()
stdscr.keypad(True)
stdscr.scrollok(True)
stdscr.addstr(">> ")
stdscr.refresh()
history = [""]
ptr = len(history) - 1
while True:
c = stdscr.getch()
if c == curses.KEY_RIGHT:
(y, x) = stdscr.getyx()
if x < len(history[ptr]) + 3:
stdscr.move(y, x+1)
stdscr.refresh()
elif c == curses.KEY_LEFT:
(y, x) = stdscr.getyx()
if x > 3:
stdscr.move(y, x-1)
stdscr.refresh()
elif c == curses.KEY_UP:
if ptr > 0:
ptr -= 1
stdscr.addch("\r")
stdscr.clrtoeol()
stdscr.addstr(">> " + history[ptr])
elif c == curses.KEY_DOWN:
if ptr < len(history) - 1:
ptr += 1
stdscr.addch("\r")
stdscr.clrtoeol()
stdscr.addstr(">> " + history[ptr])
elif c == curses.KEY_ENTER or c == ord('\n') or c == ord('\r'):
if history[ptr] == 'quit':
break
# because of arrow keys move back to the end of the line
(y, x) = stdscr.getyx()
stdscr.move(y, 3+len(history[ptr]))
if history[ptr]:
buf = packPmacCommand(history[ptr])
s.send(buf)
reply = readPmacReply(s)
stdscr.addstr("\n" + reply.decode('utf-8')[0:-1])
if ptr == len(history) - 1 and history[ptr] != "":
history += [""]
else:
history[-1] = ""
ptr = len(history) - 1
stdscr.addstr("\n>> ")
stdscr.refresh()
else:
if ptr < len(history) - 1: # Modifying previous input
if len(history[-1]) == 0:
history[-1] = history[ptr]
ptr = len(history) - 1
else:
history += [history[ptr]]
ptr = len(history) - 1
if c == curses.KEY_BACKSPACE:
if len(history[ptr]) == 0:
continue
(y, x) = stdscr.getyx()
history[ptr] = history[ptr][0:x-4] + history[ptr][x-3:]
stdscr.addch("\r")
stdscr.clrtoeol()
stdscr.addstr(">> " + history[ptr])
stdscr.move(y, x-1)
stdscr.refresh()
else:
(y, x) = stdscr.getyx()
history[ptr] = history[ptr][0:x-3] + chr(c) + history[ptr][x-3:]
stdscr.addch("\r")
stdscr.clrtoeol()
stdscr.addstr(">> " + history[ptr])
stdscr.move(y, x+1)
stdscr.refresh()
finally:
# to quit
curses.nocbreak()
stdscr.keypad(False)
curses.echo()
curses.endwin()
except:
print("""
Invalid Arguments
Option 1: Single Command
------------------------
Usage: deltatau.py pmachost:port command
This then returns the response for command.
Option 2: CLI Mode
------------------
Usage: deltatau.py pmachost:port
You can then type in a command, hit enter, and the response will see
the reponse, before being prompted to again enter a command. Type
'quit' to close prompt.
""")

54
utils/macmaster.py Executable file
View File

@ -0,0 +1,54 @@
#!/usr/bin/env python3
"""
Usage: macmaster.py macmasterhost port
Listen for commands to send and returns reponse
until exit has been typed
Mark Koennecke, March 2023
"""
import socket
import struct
class MasterMACS():
def __init__(self, host, port):
self._socke = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
self._socke.connect((host, int(port)))
def send(self, command):
buf = struct.pack('BBBB', 0x05, len(command) + 4, 0, 0x19)
buf += bytes(command, 'utf-8')
buf += struct.pack('BB', 0x0D, 0x03)
self._socke.send(buf)
def receive(self):
buf = self._socke.recv(35, socket.MSG_WAITALL)
if len(buf) < 35:
raise EOFException('Master MACS returned only %d bytes, 35 expected' % len(buf))
idx = buf.find(0x0D)
ackbyte = buf[idx-1]
if ackbyte == 0x06:
ack = 'ACK'
elif ackbyte == 0x15:
ack = 'NAK',
else:
ack = 'NO'
reply = buf[4:idx-1].decode('utf-8')
return ack, reply
if __name__ == "__main__":
from sys import argv, exit, stdin
if len(argv) < 3:
print('Usage:\n\tmacmaster.py machost macport')
exit(1)
mac = MasterMACS(argv[1], argv[2])
while(True):
# import pdb; pdb.set_trace()
line = stdin.readline()
if line.find('exit') >= 0:
exit(0)
mac.send(line.strip())
ack, reply = mac.receive()
print('%s, %s' %(ack, reply))

58
utils/physhell.tcl Executable file
View File

@ -0,0 +1,58 @@
#!/usr/bin/tclsh
# This is a little program which acts as a shell to the phytron.
# It connects to another program which is supposed to talk to the phytron controller
# It reads from stdin, packages the message into the phytron format and sends it to
# the phytron communication program. Then it reads from the phytron communication program
# and unpacks the reply.
#
# This is also a nice exampe for dealing with binary in Tcl
# Making the binary character only worked with %c
# The only way to do the comparison is with the string comare
#
# Mark Könnecke, September 2016
if {[llength $argv] < 1} {
puts stdout "Usage:\n\t physhell.tcl phytronprogram"
exit
}
set phprogram [lindex $argv 0]
set phyio [open "| $phprogram" "w+b"]
fconfigure $phyio -buffering none
fconfigure $phyio -translation {binary binary}
set etx [format "%c" 0x03]
set stx [format "%c" 0x02]
set ack [format "%c" 0x06]
set nack [format "%c" 0x05]
while {1} {
set inp [gets stdin]
puts -nonewline $phyio [format "%c%s%c" 0x02 $inp 0x03]
set mode start
set reply ""
while {[string compare $mode done] != 0 } {
set c [read $phyio 1]
switch $mode {
start {
if {[string compare $c $stx] == 0} {
set mode data
}
}
data {
if {[string compare $c $etx] == 0} {
puts stdout $reply
set mode done
} elseif {[string compare $c $nack] == 0} {
append reply NACK
} elseif {[string compare $c $ack] == 0} {
append reply ACK
} else {
append reply $c
}
}
}
}
}

114
utils/syncEL734Sub.py Executable file
View File

@ -0,0 +1,114 @@
#!/usr/bin/env python3
"""
This little program synchronizes an EPICS substitutions file with values read
from the EL734 motor controller. Some research showed that the EPICS motorRecord
seems to have no means of reading configuration data like limits or speeds or such
from the device support. Than it is the easiest to read the data with this small
program and update the substitutions file with it.
Mark Koennecke, January 2023
"""
import socket
import sys
import time
socke = None
sockfd = None
# --------------- communication section
def connect(host, port):
global socke, sockfd
socke = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
# socke.connect(('localhost', 5050))
socke.connect((host, port))
socke.settimeout(2.)
sockfd = socke.makefile(mode='rw', newline='\r')
sockfd.write('RMT 1\r')
sockfd.flush()
sockfd.write('ECHO 0\r')
sockfd.flush()
sockfd.readline()
def transact(command):
global socke, sockfd
sockfd.write(command + '\r')
sockfd.flush()
time.sleep(.1)
return sockfd.readline()
def find_commas(rawline):
comma_list = []
for i in range(len(rawline)):
if rawline[i] == ',':
comma_list.append(i)
return comma_list
def pretty_line(newlist, comma_list):
"""
adds spaces to match the tabbing of the pattern line
"""
newline = ''
for item, idx in zip(newlist, comma_list):
length = len(newline) + 1 + len(item)
if length < idx:
newline += ' ' * (idx - length)
newline += item
newline += ','
newline += newlist[-1]
return newline[0:-1]
def fix_line(par_list, index_list):
# import pdb; pdb.set_trace()
addridx = index_list.index('ADDR')
motNo = par_list[addridx]
limits = transact('H ' + motNo)
time.sleep(.1)
l = limits.split()
lowidx = index_list.index('DLLM')
# import pdb; pdb.set_trace()
par_list[lowidx] = l[0]
highidx = index_list.index('DHLM')
par_list[highidx] = l[1]
speed = transact('J ' + motNo)
vidx = index_list.index('VELO')
par_list[vidx] = speed.strip()
# vidx = index_list.index('VMAX')
# par_list[vidx] = speed.strip()
return par_list
def scan_substitution_file(filename):
index_list = None
# import pdb; pdb.set_trace()
with open(filename, 'r') as fin:
rawline = fin.readline()
while rawline:
line = rawline.replace(' ','')
line = line.strip('{}')
l = line.split(',')
if line.find('DHLM') > 0:
index_list = l
comma_list = find_commas(rawline)
sys.stdout.write(rawline)
elif not index_list:
sys.stdout.write(rawline)
else:
if len(l) == len(index_list):
newlist = fix_line(l, index_list)
# newline = ','.join(newlist)
newline = pretty_line(newlist, comma_list)
sys.stdout.write('{' + newline + '\n')
else:
sys.stdout.write(rawline)
rawline = fin.readline()
#------------------ main
if len(sys.argv) < 4:
print('Usage:\n\tsyncEL734sub.py <host> <port> <substitutions-file>\n')
sys.exit(1)
connect(sys.argv[1], int(sys.argv[2]))
scan_substitution_file(sys.argv[3])
socke.close()

98
utils/syncMasterMAC.py Executable file
View File

@ -0,0 +1,98 @@
#!/usr/bin/env python3
"""
This little program synchronizes an EPICS substitutions file with values read
from a MasterMAC motor controller.
Mark Koennecke, February 2023
"""
import sys
import time
from macmaster import MasterMACS
def find_commas(rawline):
comma_list = []
for i in range(len(rawline)):
if rawline[i] == ',':
comma_list.append(i)
return comma_list
def pretty_line(newlist, comma_list):
"""
adds spaces to match the tabbing of the patter line
"""
newline = ''
for item, idx in zip(newlist, comma_list):
length = len(newline) + 1 + len(item)
if length < idx:
newline += ' ' * (idx - length)
newline += item
newline += ','
newline += newlist[-1]
return newline[0:-1]
def transact(command):
global mac
mac.send(command)
time.sleep(.1)
return mac.receive()
def fix_line(par_list, index_list):
# import pdb; pdb.set_trace()
addridx = index_list.index('ADDR')
motNo = int(par_list[addridx])
ack, reply = transact('%dR24' % motNo)
if ack == 'NO':
idx = reply.find('=')
lowlim = reply[idx+1:]
lowidx = index_list.index('DLLM')
par_list[lowidx] = lowlim.strip()
ack, reply = transact('%dR23' % motNo)
if ack == 'NO':
idx = reply.find('=')
highlim = reply[idx+1:]
highidx = index_list.index('DHLM')
par_list[highidx] = highlim.strip()
ack, reply = transact('%dR05' % motNo)
if ack == 'NO':
idx = reply.find('=')
speed = reply[idx+1:]
speedidx = index_list.index('VELO')
par_list[speedidx] = speed.strip()
return par_list
def scan_substitution_file(filename):
index_list = None
# import pdb; pdb.set_trace()
with open(filename, 'r') as fin:
rawline = fin.readline()
# import pdb; pdb.set_trace()
while rawline:
line = rawline.replace(' ','')
line = line.strip('{}')
l = line.split(',')
if line.find('DHLM') > 0:
index_list = l
comma_list = find_commas(rawline)
sys.stdout.write(rawline)
elif not index_list:
sys.stdout.write(rawline)
else:
if len(l) == len(index_list):
newlist = fix_line(l, index_list)
# newline = ','.join(newlist)
newline = pretty_line(newlist, comma_list)
sys.stdout.write('{' + newline + '\n')
else:
sys.stdout.write(rawline)
rawline = fin.readline()
#------------------ main
if len(sys.argv) < 4:
print('Usage:\n\tsyncMMACSub.py <host> <port> <substitutions-file>\n')
sys.exit(1)
mac = MasterMACS(sys.argv[1], sys.argv[2])
scan_substitution_file(sys.argv[3])

104
utils/syncPmacSub.py Executable file
View File

@ -0,0 +1,104 @@
#!/usr/bin/env python3
"""
This little program synchronizes an EPICS substitutions file with values read
from the delta tau motor controller. Some research showed that the EPICS motorRecord
seems to have no means of reading configuration data like limits or speeds or such
from the device support. Than it is the easiest to read the data with this small
program and update the substitutions file with it.
Mark Koennecke, January 2023
"""
import socket
import sys
from deltatau import packPmacCommand, readPmacReply
socke = None
# --------------- communication section
def connect(host, port):
global socke, sockfd
socke = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
# socke.connect(('localhost', 5050))
socke.connect((host, port))
socke.settimeout(1.)
def transact(command):
global socke
data = packPmacCommand(command)
socke.send(data)
reply = readPmacReply(socke)
return reply.decode('utf-8')
def find_commas(rawline):
comma_list = []
for i in range(len(rawline)):
if rawline[i] == ',':
comma_list.append(i)
return comma_list
def pretty_line(newlist, comma_list):
"""
adds spaces to match the tabbing of the patter line
"""
newline = ''
for item, idx in zip(newlist, comma_list):
length = len(newline) + 1 + len(item)
if length < idx:
newline += ' ' * (idx - length)
newline += item
newline += ','
newline += newlist[-1]
return newline[0:-1]
def fix_line(par_list, index_list):
# import pdb; pdb.set_trace()
addridx = index_list.index('ADDR')
motNo = int(par_list[addridx])
lowlim = transact('Q%d14' % motNo)
lowidx = index_list.index('DLLM')
par_list[lowidx] = lowlim.strip()
highlim = transact('Q%d13' % motNo)
highidx = index_list.index('DHLM')
par_list[highidx] = highlim.strip()
speed = transact('Q%d03' % motNo)
speedidx = index_list.index('VELO')
par_list[speedidx] = speed.strip()
return par_list
def scan_substitution_file(filename):
index_list = None
# import pdb; pdb.set_trace()
with open(filename, 'r') as fin:
rawline = fin.readline()
while rawline:
line = rawline.replace(' ','')
line = line.strip('{}')
l = line.split(',')
if line.find('DHLM') > 0:
index_list = l
comma_list = find_commas(rawline)
sys.stdout.write(rawline)
elif not index_list:
sys.stdout.write(rawline)
else:
if len(l) == len(index_list):
newlist = fix_line(l, index_list)
# newline = ','.join(newlist)
newline = pretty_line(newlist, comma_list)
sys.stdout.write('{' + newline + '\n')
else:
sys.stdout.write(rawline)
rawline = fin.readline()
#------------------ main
if len(sys.argv) < 4:
print('Usage:\n\tsyncPmacSub.py <host> <port> <substitutions-file>\n')
sys.exit(1)
connect(sys.argv[1], int(sys.argv[2]))
scan_substitution_file(sys.argv[3])
socke.close()