jjshoots / PyFlyt

UAV Flight Simulator for Reinforcement Learning Research
https://taijunjet.com/PyFlyt/documentation.html
MIT License
97 stars 18 forks source link

Model output to Crazyflie Control #61

Open zcase opened 4 days ago

zcase commented 4 days ago

Hi great repo! Do you have an example using the stablebaselines3 algorithms such as PPO that controls an actual crazyflie drone? I just trained a model using the (vp, vq, vr, T) flight mode and would like to integrate into a crazyflie. I was just curious if you already had a working example.

jjshoots commented 4 days ago

Hi! Thanks for dropping by.

I don't have an end-to-end sim-train-to-real example, but I have code that shows how to interact with the crazyflie using setpoints here.

The code is fairly undocumented as of now, so feel free to leave this issue open and leave questions if you want.

zcase commented 1 day ago

ok thank you. Quick question about you the values used to train models. are all those in the global frame or body frame?

Digging into the code found here:

    def update_state(self) -> None:
        """Updates the current state of the UAV.

        This includes: ang_vel, ang_pos, lin_vel, lin_pos.
        """
        lin_pos, ang_pos = self.p.getBasePositionAndOrientation(self.Id)
        lin_vel, ang_vel = self.p.getBaseVelocity(self.Id)

        # express vels in local frame
        rotation = np.array(self.p.getMatrixFromQuaternion(ang_pos)).reshape(3, 3).T
        lin_vel = np.matmul(rotation, lin_vel)
        ang_vel = np.matmul(rotation, ang_vel)

        # ang_pos in euler form
        ang_pos = self.p.getEulerFromQuaternion(ang_pos)

        # update the main body
        self.body.state_update(rotation)

        # create the state
        self.state = np.stack([ang_vel, ang_pos, lin_vel, lin_pos], axis=0)

        # update auxiliary information
        self.aux_state = self.motors.get_states()

it looks like the following are expressed in world/global coordinates:

And that the following are expressed in the body/local coordinates:

Is this correct?

Also all of these are in radians correct?

jet-sony commented 1 day ago

Yep, both of those assumptions are correct. Positions are always in global while velocities are local.