I want to simulate the torque-free motion of the axis-symmetric(Ix=Iy) body.
Analytical solution of angular velocity
Wx(t) = Wx(0) Cos(lt) - Wy(0)Sin(lt)
Wy(t) = Wx(0) Sin(lt) + Wy(0)Sin(lt)
Wz(t) = Wz(0)
Let's consider Ix = Iy =0.33 and Iz = 0.34 and rest is zero .
Wx(0) = 50 deg/sec
Wy(0) = 50 deg/sec
Wz(0) = 1
Output angular velocity in x and y should be sinusoidal with variation from 70.71 deg to -70.71 deg.
Please find the code to simulate the above.
But I cannot see the system is behaving. All three axis is constant, and there is a slight variation but not with 70.71 deg to -70.71 deg. Would you please help me to find out where I am doing wrong?
Let me know if you need any more information on it.
Code: Select all
int main() {
try { // Create the system.
std::ofstream MyFile("Torque_Free_motion.txt");
MyFile << "time,ang_x,ang_y,ang_z" << "\n";
MultibodySystem system;
SimbodyMatterSubsystem matter(system);
GeneralForceSubsystem forces(system);
Force::UniformGravity gravity(forces, matter, Vec3(0, 0, 0));
Force::DiscreteForces discreteForces(forces, matter);
Body::Rigid pendulumBody(MassProperties(2.0, Vec3(0.0), Inertia(0.33,0.33,0.34)));
pendulumBody.addDecoration(Transform(), DecorativeBrick(Vec3(0.1)));
MobilizedBody masterMobod = matter.Ground();
MobilizedBody::Free pendulum(masterMobod, Transform(Vec3(0, 0, 0)),
pendulumBody, Transform(Vec3(0, 0, 0)));
// Set up visualization.
Visualizer viz(system);
viz.setGroundHeight(0);
viz.setShowShadows(true);
viz.setBackgroundType(Visualizer::SolidColor); // default is white
system.addEventReporter(new Visualizer::Reporter(viz, 1));
// Initialize the system and state.
State state = system.realizeTopology();
// Simulate it.
VerletIntegrator integ(system);
integ.setAccuracy(1e-6); // ask for *lots* of accuracy here (default is 1e-3)
TimeStepper ts(system, integ);
ts.initialize(state);
// Adding initial angular velocity
double ang_x = 50 * Pi / 180;
double ang_y = 50 * Pi / 180;
double ang_z = 1 * Pi / 180;
pendulum.setUToFitAngularVelocity(integ.updAdvancedState(), Vec3(ang_x, ang_y, ang_z));
system.realize(integ.getAdvancedState(), Stage::Acceleration);
for (int i = 0; i < 1000; ++i) {
auto angular_velocity = pendulum.getBodyAngularVelocity(integ.getAdvancedState());
auto simTime = integ.getTime();
MyFile<<simTime<<','<<180/Pi *angular_velocity[0] <<','<<180/Pi *angular_velocity[1]<<','<<180/Pi *angular_velocity[2]<<"\n";
std::cout << "angular_velocity\t" << simTime << ',' << 180 / Pi * angular_velocity[0] << ','
<< 180 / Pi * angular_velocity[1] << ',' << 180 / Pi * angular_velocity[2] << "\n";
ts.stepTo(i);
}
MyFile.close();
} catch (const std::exception &e) {
std::printf("EXCEPTION THROWN: %s\n", e.what());
exit(1);
} catch (...) {
std::printf("UNKNOWN EXCEPTION THROWN\n");
exit(1);
}
return 0;
}