Skip to content

Commit

Permalink
adding Jump to inverted pendulum controller, (wip)
Browse files Browse the repository at this point in the history
so far, the robot fails when it is on the air, because the controller does not know what do do.
the goal is to sendthe arm to the rest position, and the wheel to zero angular velocity.

so far this fails.
  • Loading branch information
Julio Jerez committed Feb 20, 2024
1 parent 45f5dd8 commit da574e6
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 4 deletions.
25 changes: 23 additions & 2 deletions newton-4.00/applications/ndSandbox/demos/ndUnicycle.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ namespace ndUnicycle
m_topBoxOmega,
m_jointAngle,
m_wheelOmega,
m_isOnAir,
m_stateSize
};

Expand Down Expand Up @@ -202,6 +203,12 @@ namespace ndUnicycle
#endif

m_model->ApplyActions(actions);

ndFloat32 applyJumpImpule = ndRand();
if (applyJumpImpule > 0.999f)
{
//m_model->ApplyRandomJump();
}
}

void GetObservation(ndBrainFloat* const observation)
Expand Down Expand Up @@ -323,16 +330,30 @@ namespace ndUnicycle
if (HasSupportContact())
{
//ignore wheel omega when robot is landed
state[m_isOnAir] = ndReal(0.0f);
state[m_wheelOmega] = ndReal(0.0f);

}
else
{
ndAssert(0);
ndVector wheelOmega (m_wheel->GetOmega());
state[m_isOnAir] = ndReal(1.0f);
state[m_wheelOmega] = wheelOmega.m_z;
}
}

void ApplyRandomJump() const
{
if (HasSupportContact())
{
ndBodyDynamic* const boxBody = GetRoot()->m_body->GetAsBodyDynamic();

ndVector upVector(0.0f, 1.0f, 0.0f, 0.0f);
ndVector impulse(upVector.Scale(boxBody->GetMassMatrix().m_w * 2.0f));
boxBody->ApplyImpulsePair(impulse, ndVector::m_zero, m_timestep);
}
}

void ApplyActions(ndBrainFloat* const actions) const
{
ndFloat32 legAngle = ndFloat32(actions[m_softLegControl]) * ND_MAX_LEG_ANGLE_STEP + m_legJoint->GetAngle();
Expand Down Expand Up @@ -367,7 +388,7 @@ namespace ndUnicycle
}
else
{
ndAssert(0);
//ndTrace((" calculate jump rweard \n"));
return 0.0f;
}
}
Expand Down
2 changes: 1 addition & 1 deletion newton-4.00/sdk/dBrain/ndBrainAgentContinueVPG_Trainer.h
Original file line number Diff line number Diff line change
Expand Up @@ -471,7 +471,7 @@ void ndBrainAgentContinueVPG_Trainer<statesDim, actionDim>::OptimizeStep()
m_trajectory.SetCount(0);
}

bool isTeminal = IsTerminal() || (m_trajectory.GetCount() == (m_extraTrajectorySteps + m_maxTrajectorySteps));
bool isTeminal = IsTerminal() || (m_trajectory.GetCount() >= (m_extraTrajectorySteps + m_maxTrajectorySteps));
if (isTeminal)
{
SaveTrajectory();
Expand Down
2 changes: 1 addition & 1 deletion newton-4.00/sdk/dBrain/ndBrainAgentDiscreteVPG_Trainer.h
Original file line number Diff line number Diff line change
Expand Up @@ -477,7 +477,7 @@ void ndBrainAgentDiscreteVPG_Trainer<statesDim, actionDim>::OptimizeStep()
m_trajectory.SetCount(0);
}

bool isTeminal = IsTerminal() || (m_trajectory.GetCount() == (m_extraTrajectorySteps + m_maxTrajectorySteps));
bool isTeminal = IsTerminal() || (m_trajectory.GetCount() >= (m_extraTrajectorySteps + m_maxTrajectorySteps));
if (isTeminal)
{
SaveTrajectory();
Expand Down

0 comments on commit da574e6

Please sign in to comment.