StateServer.c
Go to the documentation of this file.
1 // StateServer.c
2 //
3 /*
4 * Software License Agreement (BSD License)
5 *
6 * Copyright (c) 2013, Yaskawa America, Inc.
7 * All rights reserved.
8 *
9 * Redistribution and use in binary form, with or without modification,
10 * is permitted provided that the following conditions are met:
11 *
12 * * Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * * Neither the name of the Yaskawa America, Inc., nor the names
16 * of its contributors may be used to endorse or promote products derived
17 * from this software without specific prior written permission.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
20 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
23 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 * POSSIBILITY OF SUCH DAMAGE.
30 */
31 
32 #include "MotoROS.h"
33 
34 //-----------------------
35 // Function Declarations
36 //-----------------------
37 void Ros_StateServer_SendState(Controller* controller, int connectionIndex);
38 BOOL Ros_StateServer_SendMsgToAllClient(Controller* controller, int connectionIndex, SimpleMsg* sendMsg, int msgSize);
39 void Ros_StateServer_StopConnection(Controller* controller, int connectionIndex);
40 
41 //-----------------------
42 // Function implementation
43 //-----------------------
44 
45 //-----------------------------------------------------------------------
46 // Start the task for a new state server connection:
47 // - Ros_StateServer_SendState: Task that broadcasts controller & robot state to the connected client
48 //-----------------------------------------------------------------------
50 {
51  int connectionIndex;
52  int sockOpt;
53 
54  printf("Starting new connection to the State Server\r\n");
55 
56 ATTEMPT_STATE_CONNECTION:
57  //look for next available connection slot
58  for (connectionIndex = 0; connectionIndex < MAX_STATE_CONNECTIONS; connectionIndex++)
59  {
60  if (controller->sdStateConnections[connectionIndex] == INVALID_SOCKET)
61  {
62  controller->sdStateConnections[connectionIndex] = sd;
63  break;
64  }
65  }
66 
67  if (connectionIndex == MAX_STATE_CONNECTIONS)
68  {
69  if (Ros_MotionServer_HasDataInQueue(controller)) //another client is actively controlling motion; likely monitoring the state also
70  {
71  puts("Too many State server connections... not accepting last attempt.");
72  mpClose(sd);
73  return;
74  }
75  else
76  {
77  puts("Too many State server connections... closing old connection.");
78  Ros_StateServer_StopConnection(controller, 0); //close socket, cleanup resources, and delete tasks
79  goto ATTEMPT_STATE_CONNECTION;
80  }
81  }
82 
83  sockOpt = 1;
84  mpSetsockopt(sd, SOL_SOCKET, SO_KEEPALIVE, (char*)&sockOpt, sizeof(sockOpt));
85 
86  //start task that will send the controller state
87  controller->tidStateSendState[connectionIndex] = mpCreateTask(MP_PRI_TIME_NORMAL, MP_STACK_SIZE,
89  (int)controller, connectionIndex, 0, 0, 0, 0, 0, 0, 0, 0);
90 
91  //set feedback signal
92  if (controller->tidStateSendState[connectionIndex] != INVALID_TASK)
94  else
95  {
96  mpSetAlarm(8004, "MOTOROS FAILED TO CREATE TASK", 3);
97  Ros_StateServer_StopConnection(controller, connectionIndex);
98  }
99 }
100 
101 void Ros_StateServer_StopConnection(Controller* controller, int connectionIndex)
102 {
103  int tid, i;
104  BOOL bAtLeastOne;
105 
106  //close this connection
107  mpClose(controller->sdStateConnections[connectionIndex]);
108  //mark connection as invalid
109  controller->sdStateConnections[connectionIndex] = INVALID_SOCKET;
110 
111  // Stop message receiption task
112  tid = controller->tidStateSendState[connectionIndex];
113  controller->tidStateSendState[connectionIndex] = INVALID_TASK;
114 
115  //update I/O bit that indicates if any state connections are active
116  bAtLeastOne = FALSE;
117  for (i = 0; i < MAX_STATE_CONNECTIONS; i++)
118  {
119  if (controller->sdStateConnections[i] != INVALID_SOCKET)
120  bAtLeastOne = TRUE;
121  }
123 
124  mpDeleteTask(tid);
125 }
126 
127 
128 //-----------------------------------------------------------------------
129 // Send state (robot position and controller status) as long as there is
130 // an active connection
131 //-----------------------------------------------------------------------
132 void Ros_StateServer_SendState(Controller* controller, int connectionIndex)
133 {
134  int groupNo;
135  SimpleMsg sendMsg;
136  SimpleMsg sendMsgFEx;
137  int msgSize, fexMsgSize = 0;
138  BOOL bOkToSendExFeedback;
139  BOOL bSuccesfulSend;
140 
141  printf("Starting State Server Send State task\r\n");
142  printf("Controller number of group = %d\r\n", controller->numGroup);
143 
144  while(TRUE) //loop will break when there is a transmission error
145  {
146  Ros_SimpleMsg_JointFeedbackEx_Init(controller->numGroup, &sendMsgFEx);
147  bOkToSendExFeedback = TRUE;
148 
149  // Send feedback position for each control group
150  for(groupNo=0; groupNo < controller->numGroup; groupNo++)
151  {
152  msgSize = Ros_SimpleMsg_JointFeedback(controller->ctrlGroups[groupNo], &sendMsg);
153  fexMsgSize = Ros_SimpleMsg_JointFeedbackEx_Build(groupNo, &sendMsg, &sendMsgFEx);
154  if(msgSize > 0)
155  {
156  bSuccesfulSend = Ros_StateServer_SendMsgToAllClient(controller, connectionIndex, &sendMsg, msgSize);
157  if (!bSuccesfulSend)
158  break;
159  }
160  else
161  {
162  printf("Ros_SimpleMsg_JointFeedback returned a message size of 0\r\n");
163  bOkToSendExFeedback = FALSE;
164  }
165  }
166 
167  if (controller->numGroup < 2) //only send the ROS_MSG_MOTO_JOINT_FEEDBACK_EX message if we have multiple control groups
168  bOkToSendExFeedback = FALSE;
169 
170  if (bOkToSendExFeedback) //send extended-feedback message
171  {
172  bSuccesfulSend = Ros_StateServer_SendMsgToAllClient(controller, connectionIndex, &sendMsgFEx, fexMsgSize);
173  if (!bSuccesfulSend)
174  break;
175  }
176 
177  // Send controller/robot status
178  msgSize = Ros_Controller_StatusToMsg(controller, &sendMsg);
179  if(msgSize > 0)
180  {
181  bSuccesfulSend = Ros_StateServer_SendMsgToAllClient(controller, connectionIndex, &sendMsg, msgSize);
182  if (!bSuccesfulSend)
183  break;
184  }
186  }
187 
188  printf("State Server Send State task was terminated\r\n");
189  Ros_StateServer_StopConnection(controller, connectionIndex);
190 }
191 
192 
193 //-----------------------------------------------------------------------
194 // Send state message to all active connections
195 // return TRUE if message was send to at least one client
196 //-----------------------------------------------------------------------
197 BOOL Ros_StateServer_SendMsgToAllClient(Controller* controller, int connectionIndex, SimpleMsg* sendMsg, int msgSize)
198 {
199  int ret;
200 
201  ret = mpSend(controller->sdStateConnections[connectionIndex], (char*)(sendMsg), msgSize, 0);
202  if(ret <= 0)
203  {
204  printf("StateServer Send failure. Closing state server connection.\r\n");
205  return FALSE;
206  }
207 
208  return TRUE;
209 }
void Ros_Controller_SetIOState(ULONG signal, BOOL status)
Definition: Controller.c:793
int tidStateSendState[MAX_STATE_CONNECTIONS]
Definition: Controller.h:134
void Ros_StateServer_SendState(Controller *controller, int connectionIndex)
Definition: StateServer.c:132
#define INVALID_TASK
Definition: Controller.h:68
int groupNo
CtrlGroup * ctrlGroups[MP_GRP_NUM]
Definition: Controller.h:116
#define MAX_STATE_CONNECTIONS
Definition: Controller.h:59
int Ros_SimpleMsg_JointFeedbackEx_Build(int groupIndex, SimpleMsg *src_msgFeedback, SimpleMsg *dst_msgExtendedFeedback)
BOOL Ros_StateServer_SendMsgToAllClient(Controller *controller, int connectionIndex, SimpleMsg *sendMsg, int msgSize)
Definition: StateServer.c:197
void Ros_Sleep(float milliseconds)
Definition: Controller.c:999
int sdStateConnections[MAX_STATE_CONNECTIONS]
Definition: Controller.h:135
int Ros_Controller_StatusToMsg(Controller *controller, SimpleMsg *sendMsg)
Definition: Controller.c:656
#define IO_FEEDBACK_STATESERVERCONNECTED
Definition: Controller.h:44
int numGroup
Definition: Controller.h:114
#define STATE_UPDATE_MIN_PERIOD
Definition: StateServer.h:35
void Ros_SimpleMsg_JointFeedbackEx_Init(int numberOfGroups, SimpleMsg *sendMsg)
Definition: SimpleMessage.c:90
void Ros_StateServer_StopConnection(Controller *controller, int connectionIndex)
Definition: StateServer.c:101
#define INVALID_SOCKET
Definition: Controller.h:67
BOOL Ros_MotionServer_HasDataInQueue(Controller *controller)
int Ros_SimpleMsg_JointFeedback(CtrlGroup *ctrlGroup, SimpleMsg *sendMsg)
Definition: SimpleMessage.c:51
void Ros_StateServer_StartNewConnection(Controller *controller, int sd)
Definition: StateServer.c:49


motoman_driver
Author(s): Jeremy Zoss (Southwest Research Institute), Ted Miller (MotoROS) (Yaskawa Motoman), Eric Marcil (MotoROS) (Yaskawa Motoman)
autogenerated on Sat May 8 2021 02:27:44