forked from jcook3701/teknic_motor_controller
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathExample-MultiThreaded.cpp
228 lines (189 loc) · 7.57 KB
/
Example-MultiThreaded.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
///*******************************************************************************
/**
Example-MultiThreaded
The main function for Multi-Threaded ClearPath-SC example. The only command
line argument is the port number where the network is attached. This
main function opens the port, prints some basic information about the
nodes that are found, checks that they are all in full-access mode,
then creates the Supervisor object (which has its own thread) to run
the show. This main thread then waits for the user to hit a key to
end the program.
**/
//******************************************************************************
#include <stdio.h>
#include <ctime>
#include <chrono>
#include <string>
#include <iostream>
#include "Axis.h"
#include "Supervisor.h"
// Send message and wait for newline
void msgUser(const char *msg) {
std::cout << msg;
getchar();
}
/*****************************************************************************
* Function Name: AttentionDetected
* Description: This is the port-level attention handler function.
* This handler simply prints out the attention information
* to the console window.
* Parameters:
* Input: detected - contains the attention information
* Return: none
*****************************************************************************/
void MN_DECL AttentionDetected(const mnAttnReqReg &detected)
{
// Make a local, non-const copy for printing purposes
mnAttnReqReg myAttns = detected;
// Create a buffer to hold the attentionReg information
char attnStringBuf[512];
// Load the buffer with the string representation of the attention information
myAttns.AttentionReg.StateStr(attnStringBuf, 512);
// Print it out to the console
printf(" --> ATTENTION: port %d, node=%d, attn=%s\n",
detected.MultiAddr >> 4, detected.MultiAddr, attnStringBuf);
}
#if _MSC_VER
#pragma warning(disable:4996)
#endif
// A nice way of printing out the system time
string CurrentTimeStr() {
time_t now = time(NULL);
return string(ctime(&now));
}
#define CURRENT_TIME_STR CurrentTimeStr().c_str()
#if _MSC_VER
#pragma warning(default:4996)
#endif
int main(int argc, char* argv[])
{
msgUser("Multithreaded Example starting. Press Enter to continue.");
size_t portCount = 0;
std::vector<std::string> comHubPorts;
//Create the SysManager object. This object will coordinate actions among various ports
// and within nodes. In this example we use this object to setup and open our port.
SysManager myMgr; //Create System Manager myMgr
//This will try to open the port. If there is an error/exception during the port opening,
//the code will jump to the catch loop where detailed information regarding the error will be displayed;
//otherwise the catch loop is skipped over
try
{
SysManager::FindComHubPorts(comHubPorts);
printf("Found %d SC Hubs\n", comHubPorts.size());
for (portCount = 0; portCount < comHubPorts.size() && portCount < NET_CONTROLLER_MAX; portCount++) {
myMgr.ComHubPort(portCount, comHubPorts[portCount].c_str()); //define the first SC Hub port (port 0) to be associated
// with COM portnum (as seen in device manager)
}
if (portCount > 0) {
//printf("\n I will now open port \t%i \n \n", portnum);
myMgr.PortsOpen(portCount); //Open the port
for (size_t i = 0; i < portCount; i++) {
IPort &myPort = myMgr.Ports(i);
printf(" Port[%d]: state=%d, nodes=%d\n",
myPort.NetNumber(), myPort.OpenState(), myPort.NodeCount());
}
}
else {
printf("Unable to locate SC hub port\n");
msgUser("Press any key to continue."); //pause so the user can see the error message; waits for user to press a key
return -1; //This terminates the main program
}
// Create a list of axes - one per node
vector<Axis*> listOfAxes;
// Assume that the nodes are of the right type and that this app has full control
bool nodeTypesGood = true, accessLvlsGood = true;
for (size_t iPort = 0; iPort < portCount; iPort++){
// Get a reference to the port, to make accessing it easier
IPort &myPort = myMgr.Ports(iPort);
// Enable the attentions for this port
myPort.Adv.Attn.Enable(true);
// The attentions will be handled by the individual nodes, but register
// a handler at the port level, just for illustrative purposes.
myPort.Adv.Attn.AttnHandler(AttentionDetected);
for (unsigned iNode = 0; iNode < myPort.NodeCount(); iNode++){
// Get a reference to the node, to make accessing it easier
INode &theNode = myPort.Nodes(iNode);
theNode.Motion.AccLimit = 100000;
// Make sure we are talking to a ClearPath SC
if (theNode.Info.NodeType() != IInfo::CLEARPATH_SC_ADV) {
printf("---> ERROR: Uh-oh! Node %d is not a ClearPath-SC Advanced Motor\n", iNode);
nodeTypesGood = false;
}
if (nodeTypesGood) {
// Create an axis for this node
listOfAxes.push_back(new Axis(&theNode));
if (!theNode.Setup.AccessLevelIsFull()) {
printf("---> ERROR: Oh snap! Access level is not good for node %u\n", iNode);
accessLvlsGood = false;
}
else {
// Set the move distance based on where it is in the network
if(iNode == 0) {
listOfAxes.at(iNode)->SetMoveRevs(24);
}
if(iNode == 1) {
listOfAxes.at(iNode)->SetMoveRevs(-24);
}
// Set the trigger group indicator
theNode.Motion.Adv.TriggerGroup(1);
}
}
}
/*
#if 0
// Set the last node in the ring to a longer move
listOfAxes.at(listOfAxes.size()-1)->SetMoveRevs(listOfAxes.size()*10);
#endif
*/
}
// If we have full access to the nodes and they are all ClearPath-SC advanced nodes,
// then continue with the example
if (nodeTypesGood && accessLvlsGood){
// Create the supervisor thread, giving it access to the list of axes
Supervisor theSuper(listOfAxes, myMgr);
theSuper.CreateThread();
printf("\nMachine starting: %s\n", CURRENT_TIME_STR);
// Everything is running - wait for the user to press a key to end it
msgUser("Press any key to stop the machine."); //pause so the user can see the error message; waits for user to press a key
printf("Machine stopping: %s\n", CURRENT_TIME_STR);
// Tell the supervisor to stop
theSuper.Quit();
theSuper.Terminate();
printf("\nFinalStats:\tnumMoves\t\tPosition\n");
}
else{
// If something is wrong with the nodes, tell the user about it and quit
if (!nodeTypesGood){
printf("\n\tFAILURE: Please attach only ClearPath-SC Advanced nodes.\n\n");
}
else if (!accessLvlsGood){
printf("\n\tFAILURE: Please get full access on all your nodes.\n\n");
}
}
// Delete the list of axes that were created
for (size_t iAxis = 0; iAxis < listOfAxes.size(); iAxis++){
delete listOfAxes.at(iAxis);
}
// Close down the ports
myMgr.PortsClose();
}
catch (mnErr& theErr) {
fprintf(stderr, "Caught error: addr=%d, err=0x%0x\nmsg=%s\n",
theErr.TheAddr, theErr.ErrorCode, theErr.ErrorMsg);
printf("Caught error: addr=%d, err=0x%08x\nmsg=%s\n",
theErr.TheAddr, theErr.ErrorCode, theErr.ErrorMsg);
msgUser("Press any key to continue."); //pause so the user can see the error message; waits for user to press a key
return(2);
}
catch (...) {
fprintf(stderr, "Error generic caught\n");
printf("Generic error caught\n");
msgUser("Press any key to continue."); //pause so the user can see the error message; waits for user to press a key
return(3);
}
// Good-bye
msgUser("Press any key to continue."); //pause so the user can see the error message; waits for user to press a key
return 0;
}
// *
//******************************************************************************