22.3.2.1. User-defined Program Host Mode

22.3.2.1.1. Step I (In RecurDyn)

Step to open the RecurDyn model

  1. The model is provided in the RecurDyn installation directory <Install Dir>\Help\Examples\GCoSim\External\CPP\.

  2. Create a new working directory.

  3. Copy the RecurDyn model(mouse.rdyn) from the directory paste to the newly created working directory.

  4. Run RecurDyn.

  5. Open the RecurDyn model. And then, the mouse model appears in the working window.

    ../_images/image0624.png

    Figure 22.67 Mouse model

Step to verify the mechanical system model

Prior to applying a control system to this model, make sure that the mouse model runs without any errors.

  1. Select Dynamic/Kinematic icon of the Simulation group in the Analysis tab.

  2. Enter End time and Step.

  3. Click Simulate.

  4. Save the RecurDyn model and terminate the RecurDyn program.

22.3.2.1.2. Step II (In RecurDyn)

Step to identify GPlant inputs

The input of interest is the single rotational axial force is applied to the wheel Revolute Joint to move the mouse.

  1. Click the GPlant_In icon of the Control group in the Communicator tab. The GPlant Input List dialog box appears as shown in Figure 22.68.

    ../_images/gplantInput_list.png

    Figure 22.68 GPlant Input List dialog box

  2. Add a GPlant input as the name of the actuator. For more information, click here.

  3. Add an expression function as a following figure.

    ../_images/image0655.png

    Figure 22.69 Expression dialog box

  4. Open the dialog box of Rotational Axial force by double clicking RotationalAxial1 in the Database window. And then input the expression function defined in the upper step.

    ../_images/image0664.png

    Figure 22.70 Rotational Axial force dialog box

Step to identify GPlant outputs

The output of interest is the displacement and velocity between the ball and the Mouse body pointer. This distance is in the X direction.

  1. Click the. GPlant_Output icon of the Control group in the Communicator tab. The GPlant Output List dialog box appears as shown in Figure 22.71.

    ../_images/GeneralPlantOutput_List.png

    Figure 22.71 GPlant Output dialog box

  2. Add GPlant outputs as expression functions. For more information, click here. At this dialog, the defined expression functions are as following figures.

    ../_images/image0685.png
    ../_images/image0694.png

    Figure 22.72 Expression dialog box

Step to select the interface option

  1. Save the RecurDyn model in the normal fashion.

  2. Click the General icon of the Control group in the Communicator tab. The CoSim dialog box appears.

    ../_images/image0173.png

    Figure 22.73 External dialog box

  3. Edit Sampling Period (Control Time Step).

  4. Click OK.

  5. Close the RecurDyn Program after save the RecurDyn model.

22.3.2.1.3. Step III (In User-defined Program)

Step to design control system

  1. Run the Microsoft Visual Studio.

  2. Create a new project from the File Menu.

    ../_images/image0833.png

    Figure 22.74 File Menu

  3. Open the New Project window as shown.

  4. Select Win32 Console Application.

  5. Define the Location as new working directory.

    ../_images/image0844.png

    Figure 22.75 New Project dialog box

  6. Select the option as Empty project of the Console Application.

    ../_images/image0854.png

    Figure 22.76 Select the option of Console Application

  7. The sample test code is provided in the RecurDyn installation directory <Install dir>\Help\Examples\GCoSim\External\CPP\.

  8. Copy the sample test code(GCoSim_RDClient.cpp) from the directory paste to the working directory.

  9. The library and DLL files are provided in the RecurDyn installation directory <Install dir>\Toolkits\Controls\GCoSim\External\.

  10. Copy the library file(RDGCoSim.lib/RDGCS_ClientAPI.lib) and DLL file(RDGCoSim.dll/RDGCS_ClientAPI.dll) from the directory paste to the working directory.

    Note

    If you want to see functions provided by the library file(RDGCoSim.lib/RDGCS_ClientAPI.lib) and DLL file(RDGCoSim.dll/RDGCS_ClientAPI.dll), click here.

    ../_images/image0864.png

    Figure 22.77 New working directory

  11. Add an example source file (GCoSim_RDClient.cpp) from a working directory.

    ../_images/image0873.png

    Figure 22.78 Add an example source file

  12. Change the solution platform to x64.

    ../_images/image0884.png
    ../_images/image0893.png

    Figure 22.79 Change the platform

  13. Link the RDGCoSim.lib/RDGCS_ClientAPI.lib in your project. (This file contains the interface functions such as a RunRecurdyn, CommunicateRecurDyn, FreeCosimMemory functions)

    ../_images/image0903.png
    ../_images/image0913.png

    Figure 22.80 Link Library Files

  14. Implement your own codes in this source file (GCoSim_RDClient.cpp).

  15. Create your EXE file by using the Rebuild Solution option of Visual Studio.

=============== Interface Function ==========================

RunRecurDyn:
  • char* chInterface

  • int iNPin

  • int iNPout

  • char* chDirectory

  • char* chmodelname

  • double controltimestep

  • double endtime

  • int plotStep

  • int staticanalysis

  • int RecurDynShow

  • int RecurDynAni

  • int* seqIf

  • int* errorcode

This function must be called only one before interfacing.

Parameter Name

Unit

IO type

Type

Description

chInterface

[-]

Input

char*

The unique Name for Interface to each other.

iNPin

[-]

Input

int

The Number of Plant Input.

iNPout

[-]

Input

int

The Number of Plant Output.

chDirectory

[-]

Input

char*

Is the RecurDyn program installed directory.

chmodelname

[-]

Input

char*

Is the RecurDyn model name

controltimestep

[-]

Input

double

Is the interface step

endtime

[-]

Input

double

Is the simulation end time

plotStep

[-]

Input

int

Is the RecurDyn request step

staticanalysis

[-]

Input

int

0: dynamic analysis

1: static analysis

RecurDynShow

[-]

Input

int

0: no

1: show the RecurDyn program

RecurDynAni

[-]

Input

int

0: no

1: show the animation

seqIf

[-]

Output

int

The ID (sequence) of created interface.

errorcode

[-]

Output

int

0: no error

1: The model can’t be simulated.

2: RecurDyn can’t be executed.

CommunicateRecurDyn:
  • int seqIf

  • double sim_time

  • int iNPin

  • int iNPout

  • double* dPIN

  • double* dPOUT

  • double* rd_time

  • int* errorcode

This function must be called once at interface time step.

Parameter Name

Unit

IO type

Type

Description

seqIf

[-]

input

int

The ID (sequence) of created interface.

sim_time

[-]

Input

double

Is the simulation time

iNPin

[-]

Input

int

Is the number of plant input

iNPout

[-]

Input

int

Is the number of plant output

dPIN

[-]

Input

double

Is the values of plant input

dPOUT

[-]

Output

double

Is the values of plant output

rd_time

[-]

Output

double

Is the RecurDyn simulation time

error code

[-]

Output

int

0: no error

1: The model is abnormally stopped

2: The RecurDyn don’t respond.

3: Interface time don’t match.

FreeCosimMemory:
  • int seqIf : The ID (sequence) of created interface, input type.

This function must be called only one after interfacing.

22.3.2.1.4. Step IV (In User-defined Program)

Step to do the co-simulation

  1. Copy the built EXE and library and DLL files to the working directory of the RecurDyn model.

    Note

    The built EXE and library and DLL files should be in the same folder with the RecurDyn model.

  2. Execute the built EXE file to start the simulation.

    ../_images/image_RDClientExternal_Exe.png

    Figure 22.81 Working directory

22.3.2.1.5. Step V (In RecurDyn)

Step to see the Animation Result

  1. Execute RecurDyn.

  2. Open the mouse.rdyn and then import the RAN file to see the animation.

    ../_images/image1044.png

    Figure 22.82 Show the animation

Step to see the Plot Result

  1. To plot the output data, click the Result icon of Plot group in Analysis tab.

  2. Import the RPLT file to see the plot data.

  3. Expand the GPlant Input item in the database window on the right side. Expand the Control_torque. Double-click on Value in the section and adjust the label and axes to get the plot below.

    ../_images/image0794.png

    Figure 22.83 Plot of Value-Control_torque

  4. Expand the GPlant Output item. Expand the displacement and velocity and double-click on each Value.

    ../_images/image0804.png

    Figure 22.84 Plot of Value-displacement and Value-velocity