StateServer.c
Go to the documentation of this file.
00001 // StateServer.c
00002 //
00003 /*
00004 * Software License Agreement (BSD License) 
00005 *
00006 * Copyright (c) 2013, Yaskawa America, Inc.
00007 * All rights reserved.
00008 *
00009 * Redistribution and use in binary form, with or without modification,
00010 * is permitted provided that the following conditions are met:
00011 *
00012 *       * Redistributions in binary form must reproduce the above copyright
00013 *       notice, this list of conditions and the following disclaimer in the
00014 *       documentation and/or other materials provided with the distribution.
00015 *       * Neither the name of the Yaskawa America, Inc., nor the names 
00016 *       of its contributors may be used to endorse or promote products derived
00017 *       from this software without specific prior written permission.
00018 *
00019 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
00020 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00021 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
00022 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
00023 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
00024 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
00025 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
00026 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
00027 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
00028 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
00029 * POSSIBILITY OF SUCH DAMAGE.
00030 */ 
00031 
00032 #include "MotoPlus.h"
00033 #include "ParameterExtraction.h"
00034 #include "CtrlGroup.h"
00035 #include "SimpleMessage.h"
00036 #include "Controller.h"
00037 #include "StateServer.h"
00038 
00039 //-----------------------
00040 // Function Declarations
00041 //-----------------------
00042 void Ros_StateServer_StartNewConnection(Controller* controller, int sd);
00043 void Ros_StateServer_SendState(Controller* controller);
00044 BOOL Ros_StateServer_SendMsgToAllClient(Controller* controller, SimpleMsg* sendMsg, int msgSize);
00045 
00046 //-----------------------
00047 // Function implementation
00048 //-----------------------
00049 
00050 //-----------------------------------------------------------------------
00051 // Start the tasks for a new state server connection:
00052 // - WaitForSimpleMsg: Task that waits to receive new SimpleMessage
00053 // - AddToIncQueueProcess: Task that take data from a message and generate Incmove  
00054 //-----------------------------------------------------------------------
00055 void Ros_StateServer_StartNewConnection(Controller* controller, int sd)
00056 {
00057         int connectionIndex;
00058 
00059         printf("Starting new connection to the State Server\r\n");
00060         
00061     //look for next available connection slot
00062     for (connectionIndex = 0; connectionIndex < MAX_STATE_CONNECTIONS; connectionIndex++)
00063     {
00064         if (controller->sdStateConnections[connectionIndex] == INVALID_SOCKET)
00065         {
00066                 //Start the new connection in a different task.
00067                 //Each task's memory will be unique IFF the data is on the stack.
00068                 //Any global or heap stuff will not be unique.
00069                 controller->sdStateConnections[connectionIndex] = sd;
00070                         
00071                 // If not started
00072                         if(controller->tidStateSendState == INVALID_TASK)
00073                         {
00074                                 //start task that will send the controller state
00075                                 controller->tidStateSendState = mpCreateTask(MP_PRI_TIME_NORMAL, MP_STACK_SIZE, 
00076                                                                                         (FUNCPTR)Ros_StateServer_SendState,
00077                                                                                         (int)controller, 0, 0, 0, 0, 0, 0, 0, 0, 0);
00078         
00079                                 //set feedback signal
00080                                 if(controller->tidStateSendState != INVALID_TASK)
00081                                         Ros_Controller_SetIOState(IO_FEEDBACK_STATESERVERCONNECTED, TRUE);
00082                         }
00083                 
00084                 break;
00085             }
00086     }
00087         
00088     if (connectionIndex == MAX_STATE_CONNECTIONS)
00089     {
00090         printf("Too many State server connections... not accepting last attempt.\r\n");
00091         mpClose(sd);
00092     }
00093 }
00094 
00095 
00096 //-----------------------------------------------------------------------
00097 // Send state (robot position) as long as there is an active connection
00098 //-----------------------------------------------------------------------
00099 void Ros_StateServer_SendState(Controller* controller)
00100 {
00101         BOOL bHasConnections = TRUE;
00102         int groupNo;
00103         SimpleMsg sendMsg;
00104         int msgSize;
00105         
00106         printf("Starting State Server Send State task\r\n");
00107         printf("Controller number of group = %d\r\n", controller->numGroup);
00108         
00109         while(bHasConnections)
00110         {
00111                 // Send position for each control group
00112                 for(groupNo=0; groupNo < controller->numGroup; groupNo++)
00113                 {
00114                         msgSize = Ros_SimpleMsg_JointFeedback(controller->ctrlGroups[groupNo], &sendMsg);
00115                         if(msgSize > 0)
00116                         {
00117                                 bHasConnections = Ros_StateServer_SendMsgToAllClient(controller, &sendMsg, msgSize);
00118                         }
00119                         else
00120                         {
00121                                 printf("Ros_SimpleMsg_JointFeedback returned a message size of 0\r\n");
00122                         }
00123                 }
00124 
00125                 // Send controller/robot status
00126                 if(bHasConnections)
00127                 {
00128                         msgSize = Ros_Controller_StatusToMsg(controller, &sendMsg);
00129                         if(msgSize > 0)
00130                         {
00131                                 bHasConnections = Ros_StateServer_SendMsgToAllClient(controller, &sendMsg, msgSize);
00132                         }
00133                 }
00134                 mpTaskDelay(STATE_UPDATE_MIN_PERIOD);
00135         }
00136         
00137         // Terminate this task
00138         controller->tidStateSendState = INVALID_TASK;
00139         Ros_Controller_SetIOState(IO_FEEDBACK_STATESERVERCONNECTED, FALSE);
00140         printf("State Server Send State task was terminated\r\n");
00141         mpDeleteSelf;
00142 }
00143 
00144 
00145 //-----------------------------------------------------------------------
00146 // Send state message to all active connections
00147 // return TRUE if message was send to at least one client
00148 //-----------------------------------------------------------------------
00149 BOOL Ros_StateServer_SendMsgToAllClient(Controller* controller, SimpleMsg* sendMsg, int msgSize)
00150 {
00151         int index;
00152         int ret;
00153         BOOL bHasConnections = FALSE;
00154         
00155         // Check all active connections
00156         for(index = 0; index < MAX_STATE_CONNECTIONS; index++)
00157         {
00158                 if(controller->sdStateConnections[index] != INVALID_SOCKET)
00159                 {
00160                         ret = mpSend(controller->sdStateConnections[index], (char*)(sendMsg), msgSize, 0);
00161                         if(ret <= 0)
00162                         {
00163                                 printf("StateServer Send failure.  Closing state server connection.\r\n");
00164                                 mpClose(controller->sdStateConnections[index]);
00165                                 controller->sdStateConnections[index] = INVALID_SOCKET;
00166                         }
00167                         else
00168                         {
00169                                 bHasConnections = TRUE;
00170                         }
00171                 }
00172         }
00173         return bHasConnections;
00174 }


motoman_driver
Author(s): Jeremy Zoss (Southwest Research Institute)
autogenerated on Wed Aug 26 2015 12:37:33