ChimeraTK-DeviceAccess 03.25.00
Loading...
Searching...
No Matches
testUioBackendWithHardware.cpp
Go to the documentation of this file.
1// SPDX-FileCopyrightText: Deutsches Elektronen-Synchrotron DESY, MSK, ChimeraTK Project <chimeratk-support@desy.de>
2// SPDX-License-Identifier: LGPL-3.0-or-later
3
4#include "Device.h"
5#include "Utilities.h"
6
7#include <iostream>
8
9#define EXPECTED_MOTOR_ID 268435504
10
11/*
12 * This test code needs to be executed on a Xilinx ZCU102 evaluation board, using the hardware project
13 * files from git@gitlab.msktools.desy.de:fpgafw/projects/test/test_bsp_motctrl.git (Tag: 0.1.0, Commit ID: a6160e40)
14 */
15
16/*
17 * All information needed to access the device is
18 * the device alias and the register names
19 * (plus a .dmap file)
20 */
21
22int main() {
23 /*
24 * Before you use a device you have to tell DeviceAccess
25 * which dmap file to use.
26 */
27 ChimeraTK::setDMapFilePath("uioBackendTest.dmap");
28
29 /*
30 * Create a device. Make sure a device alias is present
31 * in the dmap file.
32 */
33 ChimeraTK::Device myDevice("MOTCTRL");
34
35 myDevice.open();
36
38 "MOTOR_CONTROL/MOTOR_POSITION", 0, {ChimeraTK::AccessMode::wait_for_new_data});
39
40 myDevice.close();
41 myDevice.open();
42
43 myDevice.activateAsyncRead();
44
45 /*
46 * Registers are defined by a path, which consists of a hierarchy of
47 * names separated by '/'. In this is example it is Module/Register.
48 * In this basic example we use a register which contains a single value
49 * (a scalar).
50 */
51
53 myDevice.getScalarRegisterAccessor<uint32_t>("MOTOR_CONTROL/ID");
54
56 myDevice.getScalarRegisterAccessor<uint32_t>("MOTOR_CONTROL/MOTOR_MAX_ACC");
57
58 ChimeraTK::ScalarRegisterAccessor<uint32_t> maximumAccelerationReadBack =
59 myDevice.getScalarRegisterAccessor<uint32_t>("MOTOR_CONTROL/MOTOR_MAX_ACC");
60
62 myDevice.getScalarRegisterAccessor<uint32_t>("MOTOR_CONTROL/MOTOR_MAX_VEL");
63
65 myDevice.getScalarRegisterAccessor<uint32_t>("MOTOR_CONTROL/MOTOR_BASE_VEL");
66
68 myDevice.getScalarRegisterAccessor<uint32_t>("MOTOR_CONTROL/MOTOR_PULSE_WIDTH");
69
71 myDevice.getScalarRegisterAccessor<int32_t>("MOTOR_CONTROL/MOTOR_DESTINATION");
72
74 myDevice.getScalarRegisterAccessor<uint32_t>("MOTOR_CONTROL/MOTOR_START");
75
77 myDevice.getScalarRegisterAccessor<uint32_t>("MOTOR_CONTROL/MOTOR_POSITION_RESET");
78
79 /* Check read access*/
80 motorControlId.read();
81 if(motorControlId != EXPECTED_MOTOR_ID) {
82 std::cerr << "Read check failed: Module ID is expected to be 0x" << std::hex << EXPECTED_MOTOR_ID
83 << ", but it is 0x" << (uint32_t)motorControlId << "!" << std::endl;
84 exit(-1);
85 }
86
87 /* Configure motor control logic*/
88 maximumAcceleration = 2000;
89 maximumAcceleration.write();
90
91 maximumVelocity = 2000;
92 maximumVelocity.write();
93
94 baseVelocity = 0;
95 baseVelocity.write();
96
97 pulseWidth = 200;
98 pulseWidth.write();
99
100 /* Check write access*/
101 maximumAccelerationReadBack.read();
102 if(maximumAcceleration != maximumAccelerationReadBack) {
103 std::cerr << "Write check failed: Maximum acceleration is expected to be " << maximumAcceleration << ", but it is "
104 << maximumAccelerationReadBack << "!" << std::endl;
105 exit(-1);
106 }
107
108 /* Read back configuration*/
109 maximumAcceleration.read();
110 maximumVelocity.read();
111 baseVelocity.read();
112 pulseWidth.read();
113
114 std::cout << "maximumAcceleration = " << maximumAcceleration << std::endl;
115 std::cout << "maximumVelocity = " << maximumVelocity << std::endl;
116 std::cout << "baseVelocity = " << baseVelocity << std::endl;
117 std::cout << "pulseWidth = " << pulseWidth << std::endl;
118
119 /* Move motor*/
120 motorStart = 0;
121 motorStart.write();
122
123 resetMotorPosition = 1;
124 resetMotorPosition.write();
125 resetMotorPosition = 0;
126 resetMotorPosition.write();
127
128 motorDestination = 0;
129 motorDestination.write();
130
131 motorPosition.read();
132 std::cout << "Motor at position " << motorPosition << std::endl;
133
134 for(size_t i = 0; i < 10; i++) {
135 // Set new target position
136 motorDestination += 5000;
137 motorDestination.write();
138 std::cout << std::endl << "Target position is " << motorDestination << std::endl;
139
140 // Start motor movement
141 motorStart = 1;
142 motorStart.write();
143 motorStart = 0;
144 motorStart.write();
145
146 // Wait until motor reached position
147 motorPosition.read();
148 std::cout << "Motor at position " << motorPosition << std::endl;
149 }
150
151 myDevice.close();
152
153 return 0;
154}
Class allows to read/write registers from device.
Definition Device.h:39
void close()
Close the device.
Definition Device.cc:66
ScalarRegisterAccessor< UserType > getScalarRegisterAccessor(const RegisterPath &registerPathName, size_t wordOffsetInRegister=0, const AccessModeFlags &flags=AccessModeFlags({})) const
Get a ScalarRegisterObject object for the given register.
Definition Device.h:266
void activateAsyncRead() noexcept
Activate asyncronous read for all transfer elements where AccessMode::wait_for_new_data is set.
Definition Device.cc:91
void open(std::string const &aliasName)
Open a device by the given alias name from the DMAP file.
Definition Device.cc:58
Accessor class to read and write scalar registers transparently by using the accessor object like a v...
bool write(ChimeraTK::VersionNumber versionNumber={})
Write the data to device.
void read()
Read the data from the device.
@ wait_for_new_data
Make any read blocking until new data has arrived since the last read.
void setDMapFilePath(std::string dmapFilePath)
Set the location of the dmap file.
#define EXPECTED_MOTOR_ID