Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Model output to Crazyflie Control #61

Open
zcase opened this issue Sep 20, 2024 · 11 comments
Open

Model output to Crazyflie Control #61

zcase opened this issue Sep 20, 2024 · 11 comments

Comments

@zcase
Copy link

zcase commented Sep 20, 2024

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
Copy link
Owner

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
Copy link
Author

zcase commented Sep 23, 2024

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:

  • lin_pos
  • ang_pos

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

  • lin_vel
  • ang_vel

Is this correct?

Also all of these are in radians correct?

@jet-sony
Copy link
Contributor

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

@zcase
Copy link
Author

zcase commented Oct 4, 2024

sweet thank you,

When using the default 0 mode with angular velocities, based off pybullet having things in radians, I assume the first 3 are in radians and is the T in percentage?

@jjshoots
Copy link
Owner

jjshoots commented Oct 5, 2024

Yep, that's correct. More concisely, T is in ratio, ie: 1.0 is 100% and 0.0 is 0%.

@zcase
Copy link
Author

zcase commented Oct 7, 2024

@jjshoots so does 0 mean hover thrust or no motor power?

@jjshoots
Copy link
Owner

jjshoots commented Oct 8, 2024

0 means motor stop. You can actually input negative thrust too, in which case, the motor will spin in reverse and force and torques will be reversed. This allows us to simulate 3D drones.

However, for the current quadx API, there is a motor min-throttle clip here: https://github.com/jjshoots/PyFlyt/blob/master/PyFlyt/core/drones/quadx.py#L493

@zcase
Copy link
Author

zcase commented Oct 8, 2024

@jjshoots ok that makes sense. I just wanted to verify.

Now is there a way to vary the mass of the drone during reset? I can't seem to find anything that loads in the urdf mass for something like the hover training.

@jjshoots
Copy link
Owner

jjshoots commented Oct 9, 2024

Masses are defined in the URDF by default in pybullet. But if you want to change it during runtime, you could do something like this: https://github.com/jjshoots/PyFlyt/blob/master/PyFlyt/core/abstractions/boosters.py#L207

@zcase
Copy link
Author

zcase commented Oct 28, 2024

@jjshoots SO I tried your CrazyFlyt and when I connected it to the hardware with the flowdeck it just flipped on the ground and didn't really take off at all. I couldn't even get it to hover with the simple example you have. Did you do anything special to get it to work with the real hardware?

@jjshoots
Copy link
Owner

I assume you have tried just hovering normally and verifying that it works that way? There are a number of things that could cause it to just flip, common examples are flipped props, reversed sensors, etc. I would try doing a normal flight using simple bluetooth control via the crazyflie's official implementation first.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants