|
| 1 | +import {RawPidController} from "../raw"; |
| 2 | +import {Rotation, RotationOps, Vector, VectorOps} from "../math"; |
| 3 | +import {Collider, ColliderSet, InteractionGroups, Shape} from "../geometry"; |
| 4 | +import {QueryFilterFlags, QueryPipeline, World} from "../pipeline"; |
| 5 | +import {IntegrationParameters, RigidBody, RigidBodySet} from "../dynamics"; |
| 6 | + |
| 7 | +// TODO: unify with the JointAxesMask |
| 8 | +/** |
| 9 | + * An enum representing the possible joint axes controlled by a PidController. |
| 10 | + * They can be ORed together, like: |
| 11 | + * PidAxesMask.LinX || PidAxesMask.LinY |
| 12 | + * to get a pid controller that only constraints the translational X and Y axes. |
| 13 | + * |
| 14 | + * Possible axes are: |
| 15 | + * |
| 16 | + * - `X`: X translation axis |
| 17 | + * - `Y`: Y translation axis |
| 18 | + * - `Z`: Z translation axis |
| 19 | + * - `AngX`: X angular rotation axis (3D only) |
| 20 | + * - `AngY`: Y angular rotation axis (3D only) |
| 21 | + * - `AngZ`: Z angular rotation axis |
| 22 | + */ |
| 23 | +export enum PidAxesMask { |
| 24 | + None = 0, |
| 25 | + LinX = 1 << 0, |
| 26 | + LinY = 1 << 1, |
| 27 | + LinZ = 1 << 2, |
| 28 | + // #if DIM3 |
| 29 | + AngX = 1 << 3, |
| 30 | + AngY = 1 << 4, |
| 31 | + // #endif |
| 32 | + AngZ = 1 << 5, |
| 33 | + // #if DIM2 |
| 34 | + AllLin = PidAxesMask.LinX | PidAxesMask.LinY, |
| 35 | + AllAng = PidAxesMask.AngZ, |
| 36 | + // #endif |
| 37 | + // #if DIM3 |
| 38 | + AllLin = PidAxesMask.LinX | PidAxesMask.LinY | PidAxesMask.LinZ, |
| 39 | + AllAng = PidAxesMask.AngX | PidAxesMask.AngY | PidAxesMask.AngZ, |
| 40 | + // #endif |
| 41 | + All = PidAxesMask.AllLin | PidAxesMask.AllAng, |
| 42 | +} |
| 43 | + |
| 44 | +/** |
| 45 | + * A controller for controlling dynamic bodies using the |
| 46 | + * Proportional-Integral-Derivative correction model. |
| 47 | + */ |
| 48 | +export class PidController { |
| 49 | + private raw: RawPidController; |
| 50 | + |
| 51 | + private params: IntegrationParameters; |
| 52 | + private bodies: RigidBodySet; |
| 53 | + |
| 54 | + constructor( |
| 55 | + params: IntegrationParameters, |
| 56 | + bodies: RigidBodySet, |
| 57 | + kp: number, |
| 58 | + ki: number, |
| 59 | + kd: number, |
| 60 | + axes: PidAxesMask, |
| 61 | + ) { |
| 62 | + this.params = params; |
| 63 | + this.bodies = bodies; |
| 64 | + this.raw = new RawPidController(kp, ki, kd, axes); |
| 65 | + } |
| 66 | + |
| 67 | + /** @internal */ |
| 68 | + public free() { |
| 69 | + if (!!this.raw) { |
| 70 | + this.raw.free(); |
| 71 | + } |
| 72 | + |
| 73 | + this.raw = undefined; |
| 74 | + } |
| 75 | + |
| 76 | + public setKp(kp: number, axes: PidAxesMask) { |
| 77 | + this.raw.set_kp(kp, axes); |
| 78 | + } |
| 79 | + |
| 80 | + public setKi(ki: number, axes: PidAxesMask) { |
| 81 | + this.raw.set_kp(ki, axes); |
| 82 | + } |
| 83 | + |
| 84 | + public setKd(kd: number, axes: PidAxesMask) { |
| 85 | + this.raw.set_kp(kd, axes); |
| 86 | + } |
| 87 | + |
| 88 | + public setAxes(axes: PidAxesMask) { |
| 89 | + this.raw.set_axes_mask(axes); |
| 90 | + } |
| 91 | + |
| 92 | + public resetIntegrals() { |
| 93 | + this.raw.reset_integrals(); |
| 94 | + } |
| 95 | + |
| 96 | + public applyLinearCorrection( |
| 97 | + body: RigidBody, |
| 98 | + targetPosition: Vector, |
| 99 | + targetLinvel: Vector, |
| 100 | + ) { |
| 101 | + let rawPos = VectorOps.intoRaw(targetPosition); |
| 102 | + let rawVel = VectorOps.intoRaw(targetLinvel); |
| 103 | + this.raw.apply_linear_correction( |
| 104 | + this.params.dt, |
| 105 | + this.bodies.raw, |
| 106 | + body.handle, |
| 107 | + rawPos, |
| 108 | + rawVel, |
| 109 | + ); |
| 110 | + rawPos.free(); |
| 111 | + rawVel.free(); |
| 112 | + } |
| 113 | + |
| 114 | + // #if DIM2 |
| 115 | + public applyAngularCorrection( |
| 116 | + body: RigidBody, |
| 117 | + targetRotation: number, |
| 118 | + targetAngVel: number, |
| 119 | + ) { |
| 120 | + this.raw.apply_angular_correction( |
| 121 | + this.params.dt, |
| 122 | + this.bodies.raw, |
| 123 | + body.handle, |
| 124 | + targetRotation, |
| 125 | + targetAngVel, |
| 126 | + ); |
| 127 | + } |
| 128 | + // #endif |
| 129 | + |
| 130 | + // #if DIM3 |
| 131 | + public applyAngularCorrection( |
| 132 | + body: RigidBody, |
| 133 | + targetRotation: Rotation, |
| 134 | + targetAngVel: Vector, |
| 135 | + ) { |
| 136 | + let rawPos = RotationOps.intoRaw(targetRotation); |
| 137 | + let rawVel = VectorOps.intoRaw(targetAngVel); |
| 138 | + this.raw.apply_angular_correction( |
| 139 | + this.params.dt, |
| 140 | + this.bodies.raw, |
| 141 | + body.handle, |
| 142 | + rawPos, |
| 143 | + rawVel, |
| 144 | + ); |
| 145 | + rawPos.free(); |
| 146 | + rawVel.free(); |
| 147 | + } |
| 148 | + // #endif |
| 149 | + |
| 150 | + public linearCorrection( |
| 151 | + body: RigidBody, |
| 152 | + targetPosition: Vector, |
| 153 | + targetLinvel: Vector, |
| 154 | + ): Vector { |
| 155 | + let rawPos = VectorOps.intoRaw(targetPosition); |
| 156 | + let rawVel = VectorOps.intoRaw(targetLinvel); |
| 157 | + let correction = this.raw.linear_correction( |
| 158 | + this.params.dt, |
| 159 | + this.bodies.raw, |
| 160 | + body.handle, |
| 161 | + rawPos, |
| 162 | + rawVel, |
| 163 | + ); |
| 164 | + rawPos.free(); |
| 165 | + rawVel.free(); |
| 166 | + |
| 167 | + return VectorOps.fromRaw(correction); |
| 168 | + } |
| 169 | + |
| 170 | + // #if DIM2 |
| 171 | + public angularCorrection( |
| 172 | + body: RigidBody, |
| 173 | + targetRotation: number, |
| 174 | + targetAngVel: number, |
| 175 | + ): number { |
| 176 | + return this.raw.angular_correction( |
| 177 | + this.params.dt, |
| 178 | + this.bodies.raw, |
| 179 | + body.handle, |
| 180 | + targetRotation, |
| 181 | + targetAngVel, |
| 182 | + ); |
| 183 | + } |
| 184 | + // #endif |
| 185 | + |
| 186 | + // #if DIM3 |
| 187 | + public angularCorrection( |
| 188 | + body: RigidBody, |
| 189 | + targetRotation: Rotation, |
| 190 | + targetAngVel: Vector, |
| 191 | + ): Vector { |
| 192 | + let rawPos = RotationOps.intoRaw(targetRotation); |
| 193 | + let rawVel = VectorOps.intoRaw(targetAngVel); |
| 194 | + let correction = this.raw.angular_correction( |
| 195 | + this.params.dt, |
| 196 | + this.bodies.raw, |
| 197 | + body.handle, |
| 198 | + rawPos, |
| 199 | + rawVel, |
| 200 | + ); |
| 201 | + rawPos.free(); |
| 202 | + rawVel.free(); |
| 203 | + |
| 204 | + return VectorOps.fromRaw(correction); |
| 205 | + } |
| 206 | + // #endif |
| 207 | +} |
0 commit comments