# MV-7010: Co-Simulation with Activate - Quadrotor Control

In this tutorial, you will learn how to use MotionSolve and Activate in a co-simulation to control a Quadrotor model.

Activate is a software solution for multi-disciplinary, dynamic system modeling and simulation. The software is especially useful for signal-processing and controller design that requires both continuous-time and discrete-time components.

A co-simulation enables MotionSolve and Activate models to communicate with each other during simulation. An ideal use case for co-simulation is the development of a control system for a multibody dynamics model.

A quadrotor model is a multirotor helicopter that uses 2 sets of identical fixed pitched propellers (2 clockwise and 2 counter-clockwise) to control lift and torque.

Control of vehicle motion is achieved by altering the rotation rate of one or more rotor discs, thereby changing its torque load and thrust or lift characteristics.

In MotionView you will create the quadrotor frame with 4 superblades and add the rotors motions. A set of forces will be used to represent the wind disturbance.

Activate, through co-simulation with MotionSolve, will implement a controller to impose altitude and direction in quadrotor trying to compensate against the wind effect.

## Review the Model

1. Start a new MotionView session.
3. Review the model.

The Quadrotor model has 9 bodies, one frame called vehicle, 4 propellers and 4 rotors. Associated with them are their respective graphics.

To connect these 9 bodies there are 4 fixed joints connecting the rotors with the vehicle and 4 revolute joints connecting the propellers with the rotors.

## Specify the Propeller's Motion

1. Right-click Motions to display the Add Motion or MotionPair dialog.
2. For Label and Variable, enter Prop_Left_Rotation and mot_Prop_Left_Rotation, and click OK.
3. From the Connectivity tab, double-click Joint. Select Propeller_Left and click OK.
4. Change the Property to Velocity.
5. Repeat the steps 1-4 to create motion in the other propellers.
Label Variable On Joint Property
Prop_Right_Rotation mot_Prop_Right_Rotation Propeller_Right Velocity
Prop_Front_Rotation mot_Prop_Front_Rotation Propeller_Front Velocity
Prop_Rear_Rotation mot_Prop_Rear_Rotation Propeller_Rear Velocity

In a quadrotor, the velocity of the propeller is defined by the controller of pitch and roll of the vehicle.

To define the velocity value of each propeller motion, create three solver variables that are use further in the co-simulation.

## Specify the Velocity of the Propellers

1. Before you can specify the velocity, you must first create the solver variables.
1. Right-click SolverVariable from the toolbar to display the Add SolverVariable dialog.
2. For Label, enter Throttle_Command.
3. For Variable, enter sv_Throttle_Command and click OK.
4. Repeat the steps a-c to create the Pitch and Roll solver variables.
Label
Variable
Roll_Command
sv_Roll_Command
Pitch_Command
sv_Pitch_Command
2. Now you can update the propeller motion velocity.
1. From the Project Browser, browse to the Motions folder and select Prop_Left_Rotation motion.
2. From the Motion panel, go to the Properties tab.
3. From the Properties tab, change Define by to Expression and enter:
VARVAL({sv_Throttle_Command.idstring})+VARVAL({sv_Roll_Command.idstring})
4. Repeat the steps above for Propeller Right, Front and Rear, following the table entries below:
Motion
Expression
Prop_Right_Rotation
VARVAL({sv_Throttle_Command.idstring})-VARVAL({sv_Roll_Command.idstring})
Prop_Front_Rotation
-VARVAL({sv_Throttle_Command.idstring}) -VARVAL({sv_Pitch_Command.idstring})
Prop_Rear_Rotation
-VARVAL({sv_Throttle_Command.idstring})+ VARVAL({sv_Pitch_Command.idstring})
Note: The lateral propellers (left and right) control the Roll of the vehicle and the vertical propellers control the Pitch. The signal difference in the expressions determine the rotation direction of the quadrotor.

To represent the wind effect and thrust of the quadrotor, a set of forces and moments must be defined.

The wind disturbance is represented as a combination of torques in the X and Y axle applied at the vehicle center.

The thrust of each propeller is represented as a Z force following this equation:(1) ${F}_{z}={T}_{f}*{\omega }_{z}{}^{2}$

Where,

${F}_{z}$ = Thrust force

${T}_{f}$ = Thrust factor (Defined considering the propeller geometry, air density,m and spin area. For this analysis Tf= 2.1998e-5).

${\omega }_{z}$ = Propeller Angular velocity

1. Create the wind disturbance force.
1. Right-click Force to display the Add Force dialog.
2. For Label, enter Wind_Disturbance.
3. For Variable, enter frc_Wind_Disturbance and click OK.
4. From the Connectivity tab, set Force to Action only and Property to Rotational.
5. For Body, select Vehicle and for Point, select Pivot.
6. From the Rot Properties tab, set Tx and Ty to 0.5.
2. Create the thrust force.
1. Right-click Force to display the Add Force dialog.
2. For Label, enter Thrust_Left.
3. For Variable, enter frc_Thrust_Left and click OK.
4. From the Connectivity tab, set Force to Action only and Property to Translational.
5. For Body, select the body Rotor_Left, for Point, select the point Left_Motor_Center, and for Ref Marker select the marker Left_Rotor.
6. From the Trans Properties tab, change Fz to Expression and enter the expression: 
7. Repeat the steps to create the thrust force for the right, front, and rear.
Thrust Right
Label
Thrust_Right
Variable
frc_Thrust_Right
Body
Rotor_Right
Point
Right_Motor_Center
Ref Marker
Right_Rotor
Fz Expression
Thrust Front
Label
Thrust_Front
Variable
frc_Thrust_Front
Body
Rotor_Front
Point
Front_Motor_Center
Ref Marker
Front_Rotor
Fz Expression
Thrust Rear
Label
Thrust_Rear
Variable
frc_Thrust_Rear
Body
Rotor_Rear
Point
Rear_Motor_Center
Ref Marker
Rear_Rotor
Fz Expression
3. To save your model, click Save As and save it with the name Quadrotor_tutorial.mdl

## Create Control Inputs and Plant Outputs

To modify the MBS model to work in a Co-Simulation Mode you need to add solver arrays and solver variables entities in MotionView model. The solver variables contain the individual plant input and output values. The solver arrays define the plant input and output to communicate with Activate.

1. The individual plant input solver variables were already defined with the propellers motion in Specify the Velocity of the Propellers. They are Throttle_Command, Roll_Command and Pitch_Command.
2. Next, you will define the individual plant output values, which are Altitude, Roll Angle, Pitch Angle, X position, Y position, X velocity and Y velocity.
1. Right-click SolverVariable from the toolbar to display the Add SolverVariable dialog and add the following solver variables:
Label Variable Expression
Altitude sv_Altitude
DZ({m_Vehicle.idstring})
Roll_Angle sv_Roll_Angle
AY({m_Vehicle.idstring})
Pitch_Angle sv_Pitch_Angle
AX({m_Vehicle.idstring})
PosX sv_PosX
DX({m_Vehicle.idstring})
PosY sv_PosY
DY({m_Vehicle.idstring})
VX sv_VX
VX({m_Vehicle.idstring})
VY sv_VY
VY({m_Vehicle.idstring})
Note: Remember to set the Type in the SolverVariable Properties tab to Expression.
3. Create two solver arrays, ControlInput and PlantOutput, to communicate with Activate.
1. Right-click SolverArray from the toolbar to display the Add SolverArray dialog.
2. For Label, enter ControlInput.
3. For Variable, enter sa_ControlInput and click OK.
4. From the Properties tab, change the Array type to Plant Input and enter 2 for Append (two more signals).
5. Select Roll_Command for SolverVariable 1, Throttle_Command for SolverVariable 2 and Pitch_Command for SolverVariable 3 in this order:
6. Right-click SolverArray from the toolbar to display the Add SolverArray dialog.
7. For Label, enter PlantOutput.
8. For Variable, enter sa_PlantOutput and click OK.
9. From the Properties tab, change the Array type to Plant Output and click Append 6 times to append 6 more signals.
10. For the seven solver variables, select the Roll_Angle, Altitude, Pitch_Angle, PosX, PosY, VX, and VY variables in this order.

## Run the MBS Model in MotionSolve for Verification

1. Click Run Solver , rename the MotionSolve input to Quadrotor_tutorial.xml and run the model.
2. Click Animate to animate the simulation results and confirm that the model works as intended before continuing to the next step.
Note: The controller is not yet connected to the model, so the input force and torque are zero.

## Connect MotionSolve with Activate

1. From the All Programs menu, launch Activate.
2. From the menu bar, select File > Open.
3. Open Quadrotor_Control_Start .scm and review the model.
The system has 3 sections of control: Altitude of the quadrotor, X position and Y position. The X and Y position control is defined by a target path, for this model it is a slope of 0.025.

These three sections of controls defined by seven PIDs are the plant input for the MBS model. In MotionView, it's defined as Roll_Command, Throttle_Command and Pitch_Command.

The plant output, Roll_Angle, Altitude, Pitch_Angle, PosX, PosY, VX, and VY, are input for the PID controls.

For example, in the X position target control, you will see three inputs which come from the plant output (PosX, VX, and Roll Angle).
4. To enable co-simulation between Activate and MotionSolve, from Activate, select File > Preferences > Paths. In the dialog that is displayed, define the MotionSolve and MotionView license paths.
5. From the Palette Browser, select Palettes > Activate > CoSimulation, and drag-and-drop the MotionSolve block into the current diagram and connect the blocks.
6. From the diagram, double-click the MS Plant block. From the dialog that is displayed, define the following block properties:
1. XML or MDL input filename: Browse the path to your MotionView .mdl model or MotionSolve .xml file (Quadrotor_tutorial.mdl or Quadrotor_tutorial.xml) by clicking the file selector.
Note: Activate allows you to define either MDL or XML as the input for co-simulation with MotionSolve. When you choose MDL as the input file name, MDL is converted to XML on run time so you may notice a delay as the co-simulation is beginning.
2. MRF output filename: Specify the path for the output MRF file (Quadrotor_tutorial.mrf). Use the forward slash, /, in your path definition.
3. Direct feed through: Clear (default) the checkbox. If the object is selected and the simulation runs and encounters an algebraic loop, the option should be cleared to break the algebraic loop.
The following parameters are populated automatically after you load the input model, *.mdl or *.xml:
• Inputs Row Size: The value 3 indicates that three signals are supplied from Activate to the MotionSolve model. These signals correspond to the solver array PlantInput, with variables Roll_Command, Throttle_Command and Pitch_Command.
• Outputs Row Size: The value 7 indicates that the MotionSolve model is sending out seven signals from one port, which is why the Demux block is added to separate the signals. The signals correspond to the solver array PlantOutput with variables Roll_Angle, Altitude, Pitch_Angle, PosX, PosY, VX and VY.
Co-simulation with Activate can be done in two ways:
Local co-simulation
In this case, both MotionSolve and Activate are installed on the same machine. You do not need any additional setup and the fields “MotionSolve server IP address” and “MotionSolve server port number” can be left at their default values as shown in Fig. 19 above. Upon starting the co-simulation from Activate, the connection between the two solvers is made automatically, based on the paths specified in Step 4.
Remote co-simulation between Activate and MotionSolve
In this case, MotionSolve and Activate can be located on different machines that are accessible over the network. The following section describes how remote co-simulation can be accomplished.
It will be assumed that Activate is installed on Machine A and MotionSolve is installed on Machine B, where Machine A and B are not the same. Such a scenario is useful when the MotionSolve model is located on Machine B and transferring the model to Machine A to do a local co-simulation is not feasible.
Note: For successful co-simulation, a valid installation of MotionSolve is still needed on Machine A.
Machine A
1. Complete Step 4 (setting up the paths) on Machine A. These should point to a valid install of MotionSolve on Machine A.
2. Within the MSPlant block, specify the exact name (without the path) of the MotionSolve model that is located on Machine B.
3. Within the MSPlant block, specify the IP address and port number for Machine B using the fields “MotionSolve server IP address” and “MotionSolve server port number”
4. Click OK.
Machine B
1. Setup the following MotionSolve environment variables within a command prompt window.
• NUSOL_DLL_DIR: points to the location of the MotionSolve binaries (typically <altair>/hwsolvers/motionsolve/bin/win64).
• PATH: Append the path specified for NUSOL_DLL_DIR to the system path.
• RADFLEX_PATH: points to the location of the MotionSolve license libraries (typically <altair>/hwsolvers/common/bin/win64).
• Navigate to the directory where the MotionSolve model resides.
• Invoke the MotionSolve server command “msdaemon” by typing in “msdaemon” in the command prompt. This starts the MotionSolve server in “listening” mode.

To co-simulate, continue following the rest of this tutorial.

7. Click and drag to connect the output of the block Mux to Control Input of the MS Plant. Similarly, connect the Plant Output end of the MS Plant to the input end of Dmux. All required blocks are now present and connected in the modeling window.
8. From the ribbon's Simulate tool group, select Setup tool. From the Simulation Parameters dialog, in the Final Time field, enter 12 so that your simulation runs for twelve seconds.
Note: Since Activate is the main, or calling, solver in this co-simulation scenario, if the simulation end time in Activate is different than the MotionSolve simulation end time, the MotionSolve simulation end time will be modified at run time to match the end time specified in the Activate dialog above.
9. From the ribbon, click Run .
The Scope blocks in the model generate the following plots, which illustrate the altitude, roll, pitch, x position, y position, and the path.
 Figure 22. Altitude Figure 23. Roll Figure 24. Pitch Figure 25. X Position Figure 26. Y Position Figure 27. X Velocity Figure 28. Y Velocity Figure 29. XY Path
10. Launch HyperView and animate the Quadrotor_tutorial.h3d file to see the quadrotor behavior.
11. Before exiting the applications, save your work in Activate as quadrotor_complete.scm.